UI.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. $fValue = (string)$fValue;
  425. Lib::loadClass('Typespecial');
  426. DBG::log(['$fieldName'=>$fieldName, '$taskPerm'=>$taskPerm, '$fieldID'=>$fieldID, '$fName'=>$fName, '$fValue'=>$fValue, '$params'=>$params, '$record'=>$record], 'array', "hGetFormItem()");
  427. if (!$acl->isAllowed($fieldID, $taskPerm, $record)) {
  428. switch ($taskPerm) {
  429. case 'R': return "Brak uprawnień do odczytu";
  430. case 'W': return "Brak uprawnień do zapisu";
  431. case 'C': return "Brak uprawnień";
  432. default: return "Brak uprawnień do tego pola ({$taskPerm})";
  433. }
  434. }
  435. if ($fieldName == 'ID') return ''; // TODO: hide primaryKey?
  436. // $colType = $acl->getFieldTypeById($fieldID);
  437. // if (!$colType) return "Error - unknown type";
  438. $xsdType = $acl->getXsdFieldType($fieldName);
  439. DBG::log("DBG: field({$fieldName}) xsdType({$xsdType})");
  440. $html = new stdClass();
  441. $html->_params = array();
  442. $html->tag = 'input';
  443. $html->childrens = [];
  444. $html->attrs = array();
  445. $html->attrs['id'] = $fName;
  446. $html->attrs['name'] = $fName;
  447. $html->attrs['type'] = 'text';
  448. $html->attrs['value'] = $fValue;// BUG htmlspecialchars($fValue); - convert chars in edit form (" to &quot; and & to &amp;)
  449. if (isset($params['tabindex'])) $html->attrs['tabindex'] = $params['tabindex'];
  450. // if (!$acl->hasFieldPerm($fieldID, $taskPerm)) {
  451. // $html->attrs['disabled'] = 'disabled';
  452. // }
  453. $maxGrid = V::get('maxGrid', 10, $params);
  454. switch ($xsdType) {
  455. case 'xsd:long':
  456. case 'xsd:int':
  457. case 'xsd:integer': {
  458. $html->attrs['type'] = 'number';
  459. $html->attrs['class'][] = 'input-small';
  460. } break;
  461. case 'xsd:decimal':
  462. case 'p5:price': {
  463. $html->attrs['type'] = 'text';
  464. $html->attrs['class'][] = 'input-small';
  465. } break;
  466. case 'xsd:string':
  467. case 'p5:www_link':
  468. case 'p5:string': {
  469. $html->attrs['type'] = 'text';
  470. $maxLength = (int)$acl->getXsdFieldParam($fieldName, 'maxLength');
  471. if ($maxLength > 0) {
  472. $html->attrs['maxlength'] = $maxLength;
  473. }
  474. $valLength = strlen($fValue);
  475. if (isset($params['widthClass'])) {
  476. if ($params['widthClass'] == 'inside-modal') {
  477. $html->attrs['style'] = 'width:98%;';
  478. } else {
  479. $html->attrs['style'] = 'width:98%;';
  480. }
  481. }
  482. if ($maxLength > 255) {// Fix for long varchar - use textarea
  483. $html->tag = 'textarea';
  484. $html->childrens[] = $fValue;
  485. $html->attrs['rows'] = '3';
  486. unset($html->attrs['type']);
  487. unset($html->attrs['value']);
  488. }
  489. } break;
  490. case 'xsd:dateTime':
  491. case 'xsd:date': {
  492. $testDatePicker = true;
  493. if ($testDatePicker) {
  494. $html->attrs['type'] = 'text';
  495. $html->_params[] = 'date';
  496. if ('xsd:dateTime' === $xsdType) {
  497. $html->attrs['class'][] = 'se_type-datetime'; // datetimepicker';
  498. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  499. $html->attrs['maxlength'] = 19;
  500. } else {
  501. $html->attrs['class'][] = 'se_type-date'; // datetimepicker';
  502. $html->attrs['maxlength'] = 10;
  503. }
  504. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  505. $html->attrs['value'] = '';
  506. }
  507. } else {
  508. $html->attrs['type'] = 'date';
  509. }
  510. } break;
  511. case 'xsd:time': {
  512. $testDatePicker = true;
  513. if ($testDatePicker) {
  514. $html->attrs['type'] = 'text';
  515. $html->_params[] = 'time';
  516. $html->attrs['class'][] = 'se_type-time';// datetimepicker';
  517. $html->attrs['data-format'] = 'hh:mm:ss';
  518. $html->attrs['maxlength'] = 8;
  519. if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
  520. $html->attrs['value'] = '';
  521. }
  522. } else {
  523. $html->attrs['type'] = 'time';
  524. }
  525. } break;
  526. // case 'timestamp': { // TODO: timestamp is xsd:integer or xsd:dateTime?
  527. // $testDatePicker = true;
  528. // if ($testDatePicker) {
  529. // $html->attrs['type'] = 'text';
  530. // $html->_params[] = 'date';
  531. // $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  532. // $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  533. // $html->attrs['maxlength'] = 19;
  534. // if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  535. // $html->attrs['value'] = '';
  536. // }
  537. // } else {
  538. // $html->attrs['type'] = 'date';
  539. // }
  540. // } break;
  541. case 'p5:enum': {
  542. DBG::log($acl->getField($fieldID), 'array', "\$field($fieldName)");
  543. unset($html->attrs['type']);
  544. unset($html->attrs['value']);
  545. $html->tag = 'select';
  546. $defaultValue = $acl->getFieldDefaultValue($fieldName);
  547. $values = $acl->getEnumerations($fieldName);
  548. // $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  549. $selValue = $fValue;
  550. if (empty($selValue) && $selValue !== '0' && !empty($defaultValue)) {
  551. if ($taskPerm == 'C') {
  552. $selValue = $defaultValue;
  553. } else if ($taskPerm == 'W' && $acl->isAllowed($fieldID, 'R', $record)) {
  554. $selValue = $defaultValue;
  555. }
  556. }
  557. $html->childrens[] = [ 'option', [ 'value' => "" ], "" ];
  558. if (!empty($selValue) && !array_key_exists($selValue, $values)) {
  559. $html->childrens[] = [ 'option', [ 'value' => $selValue, 'selected' => "selected" ], $selValue ];
  560. }
  561. foreach ($values as $val => $label) {
  562. $html->childrens[] = [ 'option', array_merge(
  563. [ 'value' => $val ],
  564. (!empty($selValue) && $selValue == $val) ? [ 'selected' => "selected" ] : []
  565. ), $label ];
  566. }
  567. } break;
  568. case 'p5:text': {
  569. $html->tag = 'textarea';
  570. $html->childrens[] = $fValue;
  571. if (isset($params['widthClass'])) {
  572. if ($params['widthClass'] == 'inside-modal') {
  573. $html->attrs['style'] = 'width:98%;';
  574. } else {
  575. $html->attrs['style'] = 'width:98%;';
  576. }
  577. } else {
  578. //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  579. }
  580. $html->attrs['rows'] = '3';
  581. unset($html->attrs['type']);
  582. unset($html->attrs['value']);
  583. } break;
  584. case 'gml:GeometryPropertyType':
  585. case 'gml:LineStringPropertyType':
  586. case 'gml:PointPropertyType':
  587. case 'gml:PolygonPropertyType': {
  588. return '...';
  589. } break;
  590. case 'xsd:base64Binary': {
  591. return '...';
  592. } break;
  593. default: {
  594. if ('ref:' === substr($xsdType, 0, 4)) {
  595. return "Funkcja w trakcie przygotowanie (ref do '" . substr($xsdType, 4) . "')";
  596. }
  597. return "unknown Type \"{$xsdType}\"";
  598. }
  599. }
  600. $html->attrs['class'][] = 'form-control';
  601. if (!empty($html->attrs['class'])) $html->attrs['class'] = implode(" ", $html->attrs['class']);
  602. $nodeHtml = (in_array($html->tag, array('select', 'textarea')))
  603. ? [ $html->tag, $html->attrs, $html->childrens ]
  604. : $nodeHtml = [ $html->tag, $html->attrs ]
  605. ;
  606. if (in_array('date', $html->_params)) {
  607. $nodeHtml = [ 'div', [ 'class' => "input-group" ], [
  608. $nodeHtml,
  609. [ 'span', [ 'class' => "input-group-addon" ], [
  610. [ 'span', [ 'class' => "glyphicon glyphicon-calendar" ] ]
  611. ] ]
  612. ] ];
  613. }
  614. else if (in_array('time', $html->_params)) {
  615. $nodeHtml = [ 'div', [ 'class' => "input-group" ], [
  616. $nodeHtml,
  617. [ 'span', [ 'class' => "input-group-addon" ], [
  618. [ 'span', [ 'class' => "glyphicon glyphicon-time" ] ]
  619. ] ]
  620. ] ];
  621. }
  622. if (true == V::get('appendBack', '', $params)
  623. && !in_array('date', $html->_params)
  624. && !in_array('time', $html->_params)
  625. ) {
  626. if ($html->tag == 'input' && $taskPerm == 'W') {
  627. $nodeHtml = [ 'div', [ 'class' => "input-group show-last-value" ], [
  628. $nodeHtml,
  629. [ 'span', [ 'class' => "input-group-addon button-appendBack", 'title' => htmlspecialchars($fValue) ], [
  630. [ 'span', [ 'class' => "glyphicon glyphicon-arrow-left" ] ]
  631. ] ]
  632. ] ];
  633. }
  634. }
  635. $typeSpecial = Typespecial::getInstance($fieldID, $fieldName);
  636. if ($typeSpecial) {
  637. $tsParams = array();
  638. $tsValue = V::get('typespecialValue', '', $params);
  639. if (!empty($tsValue)) {
  640. $tsParams['typespecialValue'] = $tsValue;
  641. }
  642. $nodeHtml = [ 'div', [ 'class' => "field-with-typespecial" ], [
  643. $nodeHtml,
  644. $typeSpecial->hGetFormItem($acl, $fieldName, $acl->_zasobID, $fName, $fValue, $tsParams, $record),
  645. ] ];
  646. }
  647. return $nodeHtml;
  648. }
  649. public static function convertHtmlToArray($html) {
  650. $nodes = [];
  651. // <a href="index.php?_route=Users&_task=userGroups&usrLogin=michal.podejko">ustal stanowisko</a>
  652. // [ 'a', [ 'href' => "index.php?_route=Users&_task=userGroups&usrLogin=michal.podejko" ], "ustal stanowisko" ]
  653. $DBG = 0;
  654. $pos = 0;
  655. // TODO: while (true)
  656. if ('<' === substr($html, $pos, 1)) { // parse tag
  657. $tagName = ''; $attrs = []; $content = [];
  658. $endTagOpen = strpos($html, '>', $pos + 1);
  659. $endTagName = min(strpos($html, ' ', $pos + 1), $endTagOpen); // '<tag>' or '<tag attr="..">'
  660. if (false === $pos) throw new Exception("Error Processing Html - missing tagName");
  661. $tagName = substr($html, $pos + 1, $endTagName - $pos - 1);
  662. if($DBG){echo "\ntagName: '{$tagName}'";}
  663. if ('>' === substr($html, $endTagName, 1)) {
  664. } else if (' ' === substr($html, $endTagName, 1)) {
  665. if (false === $endTagOpen) throw new Exception("Error Processing Html - missing open tag end char");
  666. $attrs = UI::convertHtmlAttrsToArray(trim(substr($html, $endTagName + 1, $endTagOpen - $endTagName - 1)));
  667. } else {
  668. throw new Exception("Error Processing Html - unexpected end tag name char '" . substr($html, $endTagName, 1) . "'");
  669. }
  670. if($DBG){echo "\nattrs: '" . json_encode($attrs), "'";}
  671. // TODO: empty tags '<br>', '<br/>', '<br />', '<input ... />', '<input ... >', img, hr, etc.
  672. // TODO: nested same tags '<tagName> ... <tagName> ... </tagName> ... </tagName>'
  673. $closeTagStart = strpos($html, "</{$tagName}>", $endTagOpen + 1);
  674. if (false === $closeTagStart) throw new Exception("Error Processing Html - missing close tagName");
  675. if($DBG){echo "\nDBG \$endTagOpen: " . substr($html, $endTagOpen, 5) . "...";}
  676. if($DBG){echo "\nDBG \$endTagOpen: " . substr($html, $endTagOpen) . ".EOL";}
  677. if($DBG){echo "\nDBG \$closeTagStart strpos(\$html, '</{$tagName}>', {$endTagOpen} + 1) = '{$closeTagStart}': " . substr($html, $closeTagStart, 5) . "...";}
  678. $content = substr($html, $endTagOpen + 1, $closeTagStart - $endTagOpen - 1);
  679. $tag = [ $tagName, $attrs, $content ];
  680. if($DBG){echo "\n\$tag: ";print_r($tag);}
  681. $nodes = $tag;
  682. }
  683. return $nodes;
  684. }
  685. public static function convertHtmlAttrsToArray($strAttrs) {
  686. $attrs = [];
  687. if (!preg_match_all('((\w+)=\"([^"]*)\")', $strAttrs, $matches)) {
  688. // echo "DBG:: empty attrs or wrong syntax";
  689. return [];
  690. }
  691. $total = (count($matches) - 1) / 2;
  692. // echo "\n\$matches (total = {$total}) = ";print_r($matches);
  693. for ($i = 0; $i < $total; $i++) {
  694. $idx = $i * 2 + 1;
  695. // echo "\n\$attrs[ '{$matches[$idx][0]}' ] = '{$matches[$idx+1][0]}';";
  696. $attrs[ $matches[ $idx ][0] ] = $matches[ $idx + 1 ][0];
  697. }
  698. return $attrs;
  699. }
  700. /**
  701. * @param array $params
  702. * @param bool $params['showMenu']
  703. * @param bool $params['showContainer']
  704. * @param string $params['containerClass'] : [ 'fluid' ], default ''
  705. */
  706. public static function layout($callback, $params = []) {
  707. $params['showMenu'] = V::get('showMenu', true, $params, 'bool');
  708. $params['showContainer'] = V::get('showContainer', true, $params, 'bool');
  709. $params['containerClass'] = V::get('containerClass', '', $params);
  710. UI::gora(); // Theme::head();
  711. if ($params['showMenu']) UI::menu(); // TODO: Theme::top()
  712. if ($params['showContainer']) UI::startContainer( $params['containerClass'] ? [ 'class' => $params['containerClass'] ] : [] );
  713. try {
  714. call_user_func($callback);
  715. } catch (Exception $e) {
  716. DBG::log($e);
  717. UI::alert('danger', $e->getMessage());
  718. }
  719. if ($params['showContainer']) UI::endContainer();
  720. UI::dol(); // UI::dol must include Theme::footer();
  721. }
  722. }