UI.php 28 KB

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