UI.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. <?php
  2. Lib::loadClass('Theme');
  3. class UI {
  4. public static function getTitle() {
  5. $title = 'SE';
  6. $host = $_SERVER['SERVER_NAME'];
  7. if (substr($host, 0, 5) == 'biuro') {
  8. $host = substr($host, 6);
  9. }
  10. $title = "{$host}-SE";
  11. return $title;
  12. }
  13. public static function gora() {
  14. UI::startHtml();
  15. }
  16. public static function startHtml() {
  17. Lib::loadClass('S');
  18. UI::loadTemplate('_layout_gora');
  19. }
  20. public static function dol() {
  21. UI::endHtml();
  22. }
  23. public static function fixFooterPosition($type) {
  24. $fixFooterPosition = true;// from config?
  25. if (!$fixFooterPosition) return;
  26. switch ($type) {
  27. case 'footer_style': return 'position:absolute; bottom:0; left:0; width:100%; ';
  28. case 'body_style': return 'position:relative; padding-bottom:32px;';
  29. case 'footer_js_tag': return "\n<script>document.body.style.minHeight = '' + (window.innerHeight - 2) + 'px';</script>";
  30. }
  31. }
  32. public static function endHtml() {
  33. Theme::footer();
  34. echo "\n</body></html>";
  35. }
  36. public static function menu() {
  37. if (!User::logged()) return;
  38. if (User::hasAccess('menu')) {
  39. Theme::top();
  40. }
  41. else {
  42. UI::loadTemplate('menuLevel6');
  43. }
  44. }
  45. public static function loadTemplate($tmplName, $data = array()) {
  46. if ('defaultPage' === $tmplName) { // TODO: replace UI::loadTemplate('defaultPage') => Theme::home($data)
  47. Theme::home($data);
  48. return;
  49. }
  50. if ('login' === $tmplName) { // TODO: replace UI::loadTemplate('login') => Theme::home($data)
  51. Theme::login($data);
  52. return;
  53. }
  54. if ('logout' === $tmplName) { // TODO: replace UI::loadTemplate('logout') => Theme::home($data)
  55. Theme::logout($data);
  56. return;
  57. }
  58. if (is_array($data) && !empty($data)) {
  59. extract($data);
  60. }
  61. include APP_PATH_LIB . "/tmpl/{$tmplName}.php";
  62. }
  63. public static function hotKeyDBG($str) {}
  64. public static function showMessagesForTable($tblName) {
  65. if (empty($tblName)) return;
  66. Lib::loadClass('Router');
  67. $msgsRoute = Router::getRoute('Msgs');
  68. $msgs = $msgsRoute->getActiveMessagesForTable($tblName);
  69. if (!empty($msgs)) {
  70. self::loadTemplate('msgsForTable', array('msgs' => $msgs));
  71. }
  72. }
  73. public static function alert($alertType, $msg, $outputHtml = true) {
  74. if (!$outputHtml) {
  75. $type = ('danger' == $alertType) ? "ERROR" : strtoupper($alertType);
  76. echo "{$type}: {$msg}\n";
  77. return;
  78. }
  79. UI::tag('div', ['class'=>"alert alert-{$alertType}"], $msg, "\n");
  80. }
  81. public static function setTitleJsTag($title) { self::setTitle($title); }
  82. public static function setTitle($title) { self::tag('script', null, "document.title = '{$title}';", "\n"); }
  83. /**
  84. * $params - Array
  85. * $params['caption'] (optional) -> <caption>...</caption>
  86. * $params['cols'] (optional) -> cols, if not set read from first row
  87. * $params['rows'] -> rows, if not set - empty table
  88. * $params['rows'] -> rows, if not set - empty table
  89. * $params['disable_lp'] -> disable lp. col
  90. */
  91. public static function table($params) {
  92. $cols = V::get('cols', array(), $params);
  93. $rows = V::get('rows', array(), $params);
  94. $cols_help = V::get('cols_help', array(), $params);
  95. $cols_label = V::get('cols_label', array(), $params);
  96. $caption = V::get('caption', '', $params);
  97. $cellPadding = V::get('cell_padding', 2, $params, 'int');
  98. $showLp = (!V::get('disable_lp', false, $params));
  99. $cssClassTable = V::get('@class', 'table table-bordered table-hover', $params);
  100. $countCols = 1;
  101. if (empty($cols) && !empty($rows)) {
  102. $firstRow = array();
  103. foreach ($rows as $row) {
  104. $firstRow = $row;
  105. break;
  106. }
  107. $cols = array_filter(
  108. array_keys((array)$firstRow),
  109. function ($col) {
  110. return ('@' != substr($col, 0, 1));
  111. }
  112. );
  113. }
  114. $countCols = count($cols);
  115. $countCols = ($showLp) ? $countCols + 1 : $countCols;
  116. {
  117. $help = array();
  118. foreach ($cols as $name) {
  119. $helpMsg = V::get($name, '', $cols_help);
  120. if (empty($helpMsg)) continue;
  121. $help[$name] = self::h('i', [
  122. 'class' => "glyphicon glyphicon-question-sign",
  123. 'title' => $helpMsg
  124. ], "");
  125. }
  126. }
  127. {
  128. $label = array();
  129. foreach ($cols as $name) {
  130. $label[$name] = V::get($name, $name, $cols_label);
  131. }
  132. }
  133. // if (empty($cols)) return;
  134. $hiddenCols = V::get('hidden_cols', array(), $params);
  135. $tableAttrs = [ 'class' => $cssClassTable ];
  136. $html_id = V::get('__html_id', '', $params);
  137. if ($html_id) $tableAttrs['id'] = $html_id;
  138. self::startTag('table', $tableAttrs); echo "\n";
  139. if ($caption) { self::tag('caption', null, $caption); echo "\n"; }
  140. if (!empty($cols)) {
  141. self::startTag('thead', null); echo "\n";
  142. self::startTag('tr', null); echo "\n";
  143. if ($showLp) { self::tag('th', [ 'style' => "padding:{$cellPadding}px" ], "Lp."); echo "\n"; }
  144. foreach ($cols as $colName) {
  145. if (in_array($colName, $hiddenCols)) continue;
  146. echo self::h('th', [ 'style' => "padding:{$cellPadding}px" ], [
  147. $label[$colName],
  148. " " . V::get($colName, '', $help)
  149. ]);
  150. echo "\n";
  151. }
  152. self::endTag('tr'); echo "\n";
  153. self::endTag('thead'); echo "\n";
  154. }
  155. $tbodyAttrs = [];
  156. if (array_key_exists('@tbody.id', $params)) $tbodyAttrs['id'] = $params['@tbody.id'];
  157. self::startTag('tbody', $tbodyAttrs); echo "\n";
  158. if (empty($rows)) {
  159. self::startTag('tr'); echo "\n";
  160. self::tag('td', [ 'style' => "padding:{$cellPadding}px", 'colspan' => $countCols ], V::get('empty_msg', "Brak danych", $params)); echo "\n";
  161. self::endTag('tr'); echo "\n";
  162. } else {
  163. $i = 0;
  164. foreach ($rows as $row) {
  165. $i++;
  166. $trAttrs = array();
  167. if (!empty($row['@onClick'])) $trAttrs['onClick'] = $row['@onClick'];
  168. if (!empty($row['@class'])) $trAttrs['class'] = $row['@class'];
  169. if (!empty($row['@style'])) $trAttrs['style'] = $row['@style'];
  170. if (!empty($row['@data'])) foreach ($row['@data'] as $k => $v) $trAttrs["data-{$k}"] = $v;
  171. self::startTag('tr', $trAttrs); echo "\n";
  172. if ($showLp) { self::tag('th', [ 'style' => "padding:2px; color:#ccc" ], $i); echo "\n"; }
  173. foreach ($cols as $colName) {
  174. $rowAttrs = [ 'style' => "padding:{$cellPadding}px" ];
  175. if (!empty($row["@onClick[{$colName}]"])) $rowAttrs['onClick'] = $row["@onClick[{$colName}]"];
  176. if (!empty($row["@class[{$colName}]"])) $rowAttrs['class'] = $row["@class[{$colName}]"];
  177. if (!empty($row["@style[{$colName}]"])) $rowAttrs['style'] .= "; " . $row["@style[{$colName}]"];
  178. if (in_array($colName, $hiddenCols)) continue;
  179. self::tag('td', $rowAttrs, V::get($colName, '', $row)); echo "\n";
  180. }
  181. self::endTag('tr'); echo "\n";
  182. }
  183. }
  184. self::endTag('tbody'); echo "\n";
  185. self::endTag('table'); echo "\n";
  186. }
  187. public static function startContainer($attrs = array()) {// echo '<div class="container">' . "\n";
  188. $attrs['class'] = (!empty($attrs['class']))
  189. ? $attrs['class'] . ' ' . 'container'
  190. : 'container';
  191. self::startTag('div', $attrs, "\n");
  192. }
  193. public static function endContainer() { self::endTag('div', "\n"); }
  194. public static function startTag($tag, $attrs = array(), $addWhiteSpace = false) {
  195. $outAttrs = '';
  196. if (is_array($attrs)) {
  197. foreach ($attrs as $attrName => $val) $outAttrs .= " {$attrName}=\"{$val}\"";
  198. }
  199. echo '<' . $tag . $outAttrs . '>' . self::whiteSpace($addWhiteSpace);
  200. }
  201. public static function whiteSpace($addWhiteSpace = false) {
  202. return (!$addWhiteSpace)
  203. ? ''
  204. : (true === $addWhiteSpace) ? " " : $addWhiteSpace;
  205. }
  206. public static function endTag($tag, $addWhiteSpace = false) {
  207. echo '</' . $tag . '>' . self::whiteSpace($addWhiteSpace);
  208. }
  209. public static function tag($tag, $attrs = array(), $childrens = array(), $addWhiteSpace = false) {
  210. $whiteSpace = self::whiteSpace($addWhiteSpace);
  211. self::startTag($tag, $attrs);
  212. echo $whiteSpace;
  213. if (!empty($childrens) && is_array($childrens)) throw new Exception("UI::tag() children as nodes not implemented".json_encode($childrens));
  214. if (is_scalar($childrens)) echo $childrens;
  215. echo $whiteSpace;
  216. self::endTag($tag);
  217. echo $whiteSpace;
  218. }
  219. public static function emptyTag($tag, $attrs = array(), $addWhiteSpace = false) {
  220. $outAttrs = '';
  221. if (is_array($attrs)) {
  222. foreach ($attrs as $attrName => $val) $outAttrs .= " {$attrName}=\"{$val}\"";
  223. }
  224. echo '<' . $tag . $outAttrs . '/>' . self::whiteSpace($addWhiteSpace);
  225. }
  226. public static function link($type, $content, $href, $attrs = array()) {
  227. $attrs['class'] = V::get('class', '', $attrs);
  228. $attrs['class'] .= "btn btn-{$type}";
  229. if (!empty($attrs['className'])) {
  230. foreach ($attrs['className'] as $cls => $bool) {
  231. if ($bool) $attrs['class'] .= " {$cls}";
  232. }
  233. unset($attrs['className']);
  234. }
  235. $attrs['href'] = $href;
  236. UI::tag('a', $attrs, $content);
  237. }
  238. public static function jsAjaxTable($params) {
  239. }
  240. public static function price($value, $dec = ',') {
  241. // TODO: if not number type - string wwith wrong format - try to convert?
  242. return number_format($value, 2, $dec, ' ');
  243. }
  244. public static function inlineJS($jsFile, $jsonVars = []) {
  245. if (!file_exists($jsFile)) throw new Exception("js file '" . basename($jsFile) . "' not exists!");
  246. UI::startTag('script', [], "\n");
  247. echo "(function (global) {" . "\n";
  248. foreach ($jsonVars as $name => $var) {
  249. echo "var {$name} = " . json_encode($var) . ";\n";
  250. }
  251. echo file_get_contents($jsFile);
  252. echo "})(window)" . "\n";
  253. UI::endTag('script', "\n");
  254. }
  255. public static function inlineRawJS($jsFile) {
  256. if (!file_exists($jsFile)) throw new Exception("js file '" . basename($jsFile) . "' not exists!");
  257. UI::startTag('script', [], "\n");
  258. echo file_get_contents($jsFile);
  259. UI::endTag('script', "\n");
  260. }
  261. public static function inlineCSS($cssFile) {
  262. UI::startTag('style', ['type'=>"text/css"], "\n");
  263. echo file_get_contents($cssFile);
  264. UI::endTag('style', "\n");
  265. }
  266. public static function includeView($viewPath, $data = array()) {
  267. if (!file_exists($viewPath)) throw new Exception("view file '" . basename($viewPath) . "' not exists!");
  268. if (false === strpos($viewPath, APP_PATH_ROOT)) throw new Exception("Access Denied to include view '" . basename($viewPath) . "'!");
  269. if (is_array($data) && !empty($data)) {
  270. extract($data);
  271. }
  272. include $viewPath;
  273. }
  274. public static function postButton($label, $params = []) {
  275. UI::startTag('form', [
  276. 'action' => V::get('action', '', $params),
  277. 'method' => V::get('method', 'post', $params),
  278. 'style' => "display:inline"
  279. ]);
  280. foreach (V::get('data', [], $params, 'array') as $name => $value) {
  281. UI::emptyTag('input', ['type'=>'hidden', 'name'=>$name, 'value'=>$value]);
  282. }
  283. UI::tag('button', ['type'=>'submit', 'class' => 'btn ' . V::get('class', 'btn-default btn-xs', $params)], $label);
  284. UI::endTag('form');
  285. }
  286. public static function hButtonPost($label, $params = [], $childrens = []) {
  287. if (!empty($params['data'])) foreach ($params['data'] as $k => $v) $childrens[] = self::h('input', ['type'=>'hidden', 'name'=>$k, 'value'=>$v]);
  288. if (!empty($params['fields'])) {
  289. foreach ($params['fields'] as $fieldParams) {
  290. $childrens[] = self::h('input', $fieldParams);
  291. }
  292. }
  293. $childrens[] = self::h('button', array_merge(
  294. [
  295. 'type'=>'submit',
  296. 'class' => 'btn ' . V::get('class', 'btn-default', $params),
  297. 'style' => V::get('style', '', $params)
  298. ],
  299. (!empty($params['title'])) ? ['title' => $params['title']] : []
  300. ), $label);
  301. return self::h('form', [
  302. 'action' => V::get('action', '', $params),
  303. 'method' => V::get('method', 'post', $params),
  304. 'style' => V::get('form.style', 'display:inline', $params),
  305. 'class' => "form-inline"
  306. ], $childrens);
  307. }
  308. public static function hButtonAjax($label, $jsEventPrefix, $params = []) {
  309. if (!empty($params['data'])) foreach ($params['data'] as $k => $v) $childrens[] = self::h('input', ['type'=>'hidden', 'name'=>$k, 'value'=>$v]);
  310. $query = V::get('data', '', $params);
  311. return self::h('a', [
  312. 'class' => V::get('class', 'btn btn-default', $params),
  313. 'style' => V::get('style', '', $params),
  314. 'href' => V::get('href', '', $params),
  315. 'onClick' => "return p5UI__hButtonAjax(this, 'p5UIBtnAjax:{$jsEventPrefix}', '', '" . http_build_query($query) . "')",
  316. ], $label);
  317. }
  318. public static function hButtonAjaxOnResponse($jsEventPrefix, $jsCode) {
  319. echo self::h('script', [], "
  320. jQuery(document).on('p5UIBtnAjax:{$jsEventPrefix}:response', function(e, n, payload) {
  321. {$jsCode}
  322. })
  323. ");
  324. }
  325. public static function hButtonAjaxJsFunction() {
  326. echo UI::h('script', [], "
  327. function p5UI__hButtonAjax(n, eventNamespace, url, query) {
  328. var dbg = " . ( DBG::isActive() ? 1 : 0 ) . ";
  329. var jqNode = jQuery(n);
  330. var state = {
  331. href: url || n.href,
  332. data: query || ''
  333. }
  334. jQuery(document).trigger('p5UIBtnAjax:' + eventNamespace + ':click', [n, state])
  335. if (jqNode.hasClass('disabled')) { // bootstrap already prevent this action
  336. if (dbg) console.log('WARNING: btn disabled - waiting for response - Cancel?')
  337. return false
  338. }
  339. jqNode.addClass('disabled btn-loading')
  340. window.fetch(state.href, {
  341. method: 'POST',
  342. headers: {
  343. 'Content-Type': 'application/x-www-form-urlencoded' // query string
  344. },
  345. credentials: 'same-origin',
  346. body: state.data // new URLSearchParams(state.data)
  347. }).then(function(response) {
  348. return response.json()
  349. }).then(function(payload) {
  350. jqNode.removeClass('disabled btn-loading');
  351. jQuery(document).trigger(eventNamespace + ':response', [n, payload]);
  352. }).catch(function(e) {
  353. jQuery(document).trigger(eventNamespace + ':response', [n, 'error' + e]);
  354. jqNode.removeClass('disabled btn-loading');
  355. p5UI__notifyAjaxCallback({
  356. type: 'error',
  357. msg: 'Request error ' + e
  358. });
  359. console.log('loadDataAjax:fetch: ERR:', e);
  360. })
  361. return false;
  362. }
  363. ");
  364. }
  365. public static function h($tagName, $params = [], $childrens = []) {
  366. $emptyTags = [];
  367. $emptyTags[] = 'hr';
  368. $emptyTags[] = 'br';
  369. $emptyTags[] = 'input';
  370. $emptyTags[] = 'link';
  371. $emptyTags[] = 'area';
  372. $emptyTags[] = 'base';
  373. $emptyTags[] = 'col';
  374. $emptyTags[] = 'embed';
  375. $emptyTags[] = 'img';
  376. $emptyTags[] = 'keygen';
  377. $emptyTags[] = 'meta';
  378. $emptyTags[] = 'param';
  379. $emptyTags[] = 'source';
  380. $emptyTags[] = 'track';
  381. $emptyTags[] = 'wbr';
  382. if (in_array($tagName, $emptyTags)) return '<' . $tagName . (empty($params) ? '' : ' ' . self::hAttributes($params)) . '/>';
  383. return '<' . $tagName . (empty($params) ? '' : ' ' . self::hAttributes($params)) . '>' . self::hChildrens($childrens) . '</' . $tagName . '>';
  384. }
  385. public static function hAttributes($params = []) {
  386. $attr = [];
  387. if (null === $params) return '';
  388. if (!is_array($params)) {
  389. try {
  390. throw new Exception("Wrong params type in UI::hAttributes");
  391. } catch (Exception $e) {
  392. DBG::log($e);
  393. }
  394. }
  395. foreach ($params as $k => $v) {
  396. if (is_array($v)) {
  397. $attr[] = "{$k}=\"" . implode(" ", $v) . "\"";
  398. } else {
  399. $attr[] = "{$k}=\"{$v}\"";
  400. }
  401. }
  402. return implode(" ", $attr);
  403. }
  404. public static function hChildrens($childrens = []) {
  405. if (empty($childrens)) {
  406. if (is_int($childrens)) return "{$childrens}";
  407. if (is_string($childrens)) return $childrens;
  408. return '';
  409. }
  410. if (is_scalar($childrens)) return "{$childrens}";
  411. if (!is_array($childrens)) throw new Exception("Unsupported children type");
  412. return array_reduce(
  413. $childrens,
  414. function ($curry, $child) {
  415. return "{$curry}{$child}";
  416. },
  417. ""
  418. );
  419. }
  420. /**
  421. * @param $taskPerm - 'C', 'W'
  422. */
  423. public static function hGetFormItem($acl, $fieldName, $taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
  424. Lib::loadClass('Typespecial');
  425. if (!$acl->isAllowed($fieldID, $taskPerm, $record)) {
  426. switch ($taskPerm) {
  427. case 'R': return "Brak uprawnień do odczytu";
  428. case 'W': return "Brak uprawnień do zapisu";
  429. default: return "Brak uprawnień do tego pola ({$taskPerm})";
  430. }
  431. }
  432. if ($fieldName == 'ID') return ''; // TODO: hide primaryKey?
  433. $colType = $acl->getFieldTypeById($fieldID);
  434. if (!$colType) return "Error - unknown type";
  435. $html = new stdClass();
  436. $html->_params = array();
  437. $html->tag = 'input';
  438. $html->childrens = [];
  439. $html->attrs = array();
  440. $html->attrs['id'] = $fName;
  441. $html->attrs['name'] = $fName;
  442. $html->attrs['type'] = 'text';
  443. $html->attrs['value'] = $fValue;// BUG htmlspecialchars($fValue); - convert chars in edit form (" to &quot; and & to &amp;)
  444. if (isset($params['tabindex'])) {
  445. $html->attrs['tabindex'] = $params['tabindex'];
  446. }
  447. if (!$acl->hasFieldPerm($fieldID, $taskPerm)) {
  448. $html->attrs['disabled'] = 'disabled';
  449. }
  450. $maxGrid = V::get('maxGrid', 10, $params);
  451. if (substr($colType['type'], 0, 3) == 'int'
  452. || substr($colType['type'], 0, 7) == 'tinyint'
  453. || substr($colType['type'], 0, 8) == 'smallint'
  454. || substr($colType['type'], 0, 6) == 'bigint'
  455. ) {
  456. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
  457. $html->attrs['type'] = 'number';
  458. $html->attrs['class'][] = 'input-small';
  459. }
  460. else if (substr($colType['type'], 0, 6) == 'double') {
  461. $html->attrs['type'] = 'text';
  462. $html->attrs['class'][] = 'input-small';
  463. }
  464. else if (substr($colType['type'], 0, 7) == 'decimal') {
  465. $html->attrs['type'] = 'text';
  466. $html->attrs['class'][] = 'input-small';
  467. }
  468. else if (substr($colType['type'], 0, 7) == 'varchar'
  469. || substr($colType['type'], 0, 4) == 'char'
  470. ) {
  471. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
  472. $html->attrs['type'] = 'text';
  473. $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  474. if ($maxLength > 0) {
  475. $html->attrs['maxlength'] = $maxLength;
  476. }
  477. $valLength = strlen($fValue);
  478. if (isset($params['widthClass'])) {
  479. if ($params['widthClass'] == 'inside-modal') {
  480. $html->attrs['style'] = 'width:98%;';
  481. } else {
  482. $html->attrs['style'] = 'width:98%;';
  483. }
  484. } else {
  485. /*
  486. if ($maxLength < 11) {
  487. $html->attrs['class'][] = 'span2';
  488. } else if ($maxLength < 31) {
  489. $html->attrs['class'][] = 'span5';
  490. } else if ($maxLength < 51) {
  491. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  492. } else if ($maxLength < 101) {
  493. $html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
  494. } else {
  495. $html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
  496. }
  497. */
  498. }
  499. if ($maxLength > 255) {// Fix for long varchar - use textarea
  500. $html->tag = 'textarea';
  501. $html->childrens[] = htmlspecialchars($fValue);
  502. $html->attrs['rows'] = '3';
  503. unset($html->attrs['type']);
  504. unset($html->attrs['value']);
  505. }
  506. }
  507. else if (substr($colType['type'], 0, 4) == 'date') {
  508. $testDatePicker = true;
  509. if ($testDatePicker) {
  510. $html->attrs['type'] = 'text';
  511. $html->_params[] = 'date';
  512. if (substr($colType['type'], 0, 8) == 'datetime') {
  513. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  514. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  515. $html->attrs['maxlength'] = 19;
  516. } else {
  517. $html->attrs['class'][] = 'se_type-date';// datetimepicker';
  518. $html->attrs['maxlength'] = 10;
  519. }
  520. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  521. $html->attrs['value'] = '';
  522. }
  523. } else {
  524. $html->attrs['type'] = 'date';
  525. }
  526. }
  527. else if ($colType['type'] == 'time') {
  528. $testDatePicker = true;
  529. if ($testDatePicker) {
  530. $html->attrs['type'] = 'text';
  531. $html->_params[] = 'time';
  532. $html->attrs['class'][] = 'se_type-time';// datetimepicker';
  533. $html->attrs['data-format'] = 'hh:mm:ss';
  534. $html->attrs['maxlength'] = 8;
  535. if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
  536. $html->attrs['value'] = '';
  537. }
  538. } else {
  539. $html->attrs['type'] = 'time';
  540. }
  541. }
  542. else if ($colType['type'] == 'timestamp') {
  543. $testDatePicker = true;
  544. if ($testDatePicker) {
  545. $html->attrs['type'] = 'text';
  546. $html->_params[] = 'date';
  547. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  548. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  549. $html->attrs['maxlength'] = 19;
  550. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  551. $html->attrs['value'] = '';
  552. }
  553. } else {
  554. $html->attrs['type'] = 'date';
  555. }
  556. }
  557. else if (substr($colType['type'], 0, 4) == 'enum') {
  558. unset($html->attrs['type']);
  559. unset($html->attrs['value']);
  560. $html->tag = 'select';
  561. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  562. $selValue = $fValue;
  563. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  564. if ($taskPerm == 'C') {
  565. $selValue = $colType['default'];
  566. } else if ($taskPerm == 'W' && $acl->isAllowed($fieldID, 'R', $record)) {
  567. $selValue = $colType['default'];
  568. }
  569. }
  570. $html->childrens[] = [ 'option', [ 'value' => "" ], "" ];
  571. if (!empty($selValue) && !in_array($selValue, $values)) {
  572. $html->childrens[] = [ 'option', [ 'value' => $selValue, 'selected' => "selected" ], $selValue ];
  573. }
  574. foreach ($values as $val) {
  575. $html->childrens[] = [ 'option', array_merge(
  576. [ 'value' => $val ],
  577. ($selValue == $val)
  578. ? [ 'selected' => "selected" ]
  579. : []
  580. ), $val ];
  581. }
  582. }
  583. else if (substr($colType['type'], 0, 4) == 'text'
  584. || substr($colType['type'], 0, 8) == 'tinytext'
  585. || substr($colType['type'], 0, 10) == 'mediumtext'
  586. || substr($colType['type'], 0, 8) == 'longtext'
  587. ) {
  588. $html->tag = 'textarea';
  589. $html->childrens[] = htmlspecialchars($fValue);
  590. if (isset($params['widthClass'])) {
  591. if ($params['widthClass'] == 'inside-modal') {
  592. $html->attrs['style'] = 'width:98%;';
  593. } else {
  594. $html->attrs['style'] = 'width:98%;';
  595. }
  596. } else {
  597. //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  598. }
  599. $html->attrs['rows'] = '3';
  600. unset($html->attrs['type']);
  601. unset($html->attrs['value']);
  602. }
  603. else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
  604. else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
  605. else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
  606. else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
  607. else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
  608. else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
  609. else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
  610. else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
  611. else {
  612. return 'unknown Type "'.$colType['type'].'"';
  613. }
  614. $html->attrs['class'][] = 'form-control';
  615. if (!empty($html->attrs['class'])) $html->attrs['class'] = implode(" ", $html->attrs['class']);
  616. $nodeHtml = (in_array($html->tag, array('select', 'textarea')))
  617. ? [ $html->tag, $html->attrs, $html->childrens ]
  618. : $nodeHtml = [ $html->tag, $html->attrs ]
  619. ;
  620. if (in_array('date', $html->_params)) {
  621. $nodeHtml = [ 'div', [ 'class' => "input-group" ], [
  622. $nodeHtml,
  623. [ 'span', [ 'class' => "input-group-addon" ], [
  624. [ 'span', [ 'class' => "glyphicon glyphicon-calendar" ] ]
  625. ] ]
  626. ] ];
  627. }
  628. else if (in_array('time', $html->_params)) {
  629. $nodeHtml = [ 'div', [ 'class' => "input-group" ], [
  630. $nodeHtml,
  631. [ 'span', [ 'class' => "input-group-addon" ], [
  632. [ 'span', [ 'class' => "glyphicon glyphicon-time" ] ]
  633. ] ]
  634. ] ];
  635. }
  636. if (true == V::get('appendBack', '', $params)
  637. && !in_array('date', $html->_params)
  638. && !in_array('time', $html->_params)
  639. ) {
  640. if ($html->tag == 'input' && $taskPerm == 'W') {
  641. $nodeHtml = [ 'div', [ 'class' => "input-group show-last-value" ], [
  642. $nodeHtml,
  643. [ 'span', [ 'class' => "input-group-addon button-appendBack", 'title' => htmlspecialchars($fValue) ], [
  644. [ 'span', [ 'class' => "glyphicon glyphicon-arrow-left" ] ]
  645. ] ]
  646. ] ];
  647. }
  648. }
  649. $typeSpecial = Typespecial::getInstance($fieldID, $fieldName);
  650. if ($typeSpecial) {
  651. $tsParams = array();
  652. $tsValue = V::get('typespecialValue', '', $params);
  653. if (!empty($tsValue)) {
  654. $tsParams['typespecialValue'] = $tsValue;
  655. }
  656. $nodeHtml = [ 'div', [ 'class' => "field-with-typespecial" ], [
  657. $nodeHtml,
  658. $typeSpecial->hGetFormItem($acl, $fieldName, $acl->_zasobID, $fName, $fValue, $tsParams, $record),
  659. ] ];
  660. }
  661. return $nodeHtml;
  662. }
  663. public static function convertHtmlToArray($html) {
  664. $nodes = [];
  665. // <a href="index.php?_route=Users&_task=userGroups&usrLogin=michal.podejko">ustal stanowisko</a>
  666. // [ 'a', [ 'href' => "index.php?_route=Users&_task=userGroups&usrLogin=michal.podejko" ], "ustal stanowisko" ]
  667. $DBG = 0;
  668. $pos = 0;
  669. // TODO: while (true)
  670. if ('<' === substr($html, $pos, 1)) { // parse tag
  671. $tagName = ''; $attrs = []; $content = [];
  672. $endTagOpen = strpos($html, '>', $pos + 1);
  673. $endTagName = min(strpos($html, ' ', $pos + 1), $endTagOpen); // '<tag>' or '<tag attr="..">'
  674. if (false === $pos) throw new Exception("Error Processing Html - missing tagName");
  675. $tagName = substr($html, $pos + 1, $endTagName - $pos - 1);
  676. if($DBG){echo "\ntagName: '{$tagName}'";}
  677. if ('>' === substr($html, $endTagName, 1)) {
  678. } else if (' ' === substr($html, $endTagName, 1)) {
  679. if (false === $endTagOpen) throw new Exception("Error Processing Html - missing open tag end char");
  680. $attrs = UI::convertHtmlAttrsToArray(trim(substr($html, $endTagName + 1, $endTagOpen - $endTagName - 1)));
  681. } else {
  682. throw new Exception("Error Processing Html - unexpected end tag name char '" . substr($html, $endTagName, 1) . "'");
  683. }
  684. if($DBG){echo "\nattrs: '" . json_encode($attrs), "'";}
  685. // TODO: empty tags '<br>', '<br/>', '<br />', '<input ... />', '<input ... >', img, hr, etc.
  686. // TODO: nested same tags '<tagName> ... <tagName> ... </tagName> ... </tagName>'
  687. $closeTagStart = strpos($html, "</{$tagName}>", $endTagOpen + 1);
  688. if (false === $closeTagStart) throw new Exception("Error Processing Html - missing close tagName");
  689. if($DBG){echo "\nDBG \$endTagOpen: " . substr($html, $endTagOpen, 5) . "...";}
  690. if($DBG){echo "\nDBG \$endTagOpen: " . substr($html, $endTagOpen) . ".EOL";}
  691. if($DBG){echo "\nDBG \$closeTagStart strpos(\$html, '</{$tagName}>', {$endTagOpen} + 1) = '{$closeTagStart}': " . substr($html, $closeTagStart, 5) . "...";}
  692. $content = substr($html, $endTagOpen + 1, $closeTagStart - $endTagOpen - 1);
  693. $tag = [ $tagName, $attrs, $content ];
  694. if($DBG){echo "\n\$tag: ";print_r($tag);}
  695. $nodes = $tag;
  696. }
  697. return $nodes;
  698. }
  699. public static function convertHtmlAttrsToArray($strAttrs) {
  700. $attrs = [];
  701. if (!preg_match_all('((\w+)=\"([^"]*)\")', $strAttrs, $matches)) {
  702. // echo "DBG:: empty attrs or wrong syntax";
  703. return [];
  704. }
  705. $total = (count($matches) - 1) / 2;
  706. // echo "\n\$matches (total = {$total}) = ";print_r($matches);
  707. for ($i = 0; $i < $total; $i++) {
  708. $idx = $i * 2 + 1;
  709. // echo "\n\$attrs[ '{$matches[$idx][0]}' ] = '{$matches[$idx+1][0]}';";
  710. $attrs[ $matches[ $idx ][0] ] = $matches[ $idx + 1 ][0];
  711. }
  712. return $attrs;
  713. }
  714. /**
  715. * @param array $params
  716. * @param bool $params['showMenu']
  717. * @param bool $params['showContainer']
  718. * @param string $params['containerClass'] : [ 'fluid' ], default ''
  719. */
  720. public static function layout($callback, $params = []) {
  721. $params['showMenu'] = V::get('showMenu', true, $params, 'bool');
  722. $params['showContainer'] = V::get('showContainer', true, $params, 'bool');
  723. $params['containerClass'] = V::get('containerClass', '', $params);
  724. UI::gora(); // Theme::head();
  725. if ($params['showMenu']) UI::menu(); // TODO: Theme::top()
  726. if ($params['showContainer']) UI::startContainer( $params['containerClass'] ? [ 'class' => $params['containerClass'] ] : [] );
  727. try {
  728. call_user_func($callback);
  729. } catch (Exception $e) {
  730. DBG::log($e);
  731. UI::alert('danger', $e->getMessage());
  732. }
  733. if ($params['showContainer']) UI::endContainer();
  734. UI::dol(); // UI::dol must include Theme::footer();
  735. }
  736. }