UI.php 26 KB

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