UI.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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', [
  286. 'type'=>'submit',
  287. 'class' => 'btn ' . V::get('class', 'btn-default', $params),
  288. 'style' => V::get('style', '', $params)
  289. ], $label);
  290. return self::h('form', [
  291. 'action' => V::get('action', '', $params),
  292. 'method' => V::get('method', 'post', $params),
  293. 'style' => V::get('form.style', 'display:inline', $params),
  294. 'class' => "form-inline"
  295. ],
  296. $childrens
  297. );
  298. }
  299. public static function hButtonAjax($label, $jsEventPrefix, $params = []) {
  300. if (!empty($params['data'])) foreach ($params['data'] as $k => $v) $childrens[] = self::h('input', ['type'=>'hidden', 'name'=>$k, 'value'=>$v]);
  301. $query = V::get('data', '', $params);
  302. return self::h('a', [
  303. 'class' => V::get('class', 'btn btn-default', $params),
  304. 'style' => V::get('style', '', $params),
  305. 'href' => V::get('href', '', $params),
  306. 'onClick' => "return p5UI__hButtonAjax(this, 'p5UIBtnAjax:{$jsEventPrefix}', '', '" . http_build_query($query) . "')",
  307. ], $label);
  308. }
  309. public static function hButtonAjaxOnResponse($jsEventPrefix, $jsCode) {
  310. echo self::h('script', [], "
  311. jQuery(document).on('p5UIBtnAjax:{$jsEventPrefix}:response', function(e, n, payload) {
  312. {$jsCode}
  313. })
  314. ");
  315. }
  316. public static function hButtonAjaxJsFunction() {
  317. echo UI::h('script', [], "
  318. function p5UI__hButtonAjax(n, eventNamespace, url, query) {
  319. var dbg = " . ( DBG::isActive() ? 1 : 0 ) . ";
  320. var jqNode = jQuery(n);
  321. var state = {
  322. href: url || n.href,
  323. data: query || ''
  324. }
  325. jQuery(document).trigger('p5UIBtnAjax:' + eventNamespace + ':click', [n, state])
  326. if (jqNode.hasClass('disabled')) { // bootstrap already prevent this action
  327. if (dbg) console.log('WARNING: btn disabled - waiting for response - Cancel?')
  328. return false
  329. }
  330. jqNode.addClass('disabled btn-loading')
  331. window.fetch(state.href, {
  332. method: 'POST',
  333. headers: {
  334. 'Content-Type': 'application/x-www-form-urlencoded' // query string
  335. },
  336. credentials: 'same-origin',
  337. body: state.data // new URLSearchParams(state.data)
  338. }).then(function(response) {
  339. return response.json()
  340. }).then(function(payload) {
  341. jqNode.removeClass('disabled btn-loading');
  342. jQuery(document).trigger(eventNamespace + ':response', [n, payload]);
  343. }).catch(function(e) {
  344. jQuery(document).trigger(eventNamespace + ':response', [n, 'error' + e]);
  345. jqNode.removeClass('disabled btn-loading');
  346. p5UI__notifyAjaxCallback({
  347. type: 'error',
  348. msg: 'Request error ' + e
  349. });
  350. console.log('loadDataAjax:fetch: ERR:', e);
  351. })
  352. return false;
  353. }
  354. ");
  355. }
  356. public static function h($tagName, $params = [], $childrens = []) {
  357. $emptyTags = [];
  358. $emptyTags[] = 'hr';
  359. $emptyTags[] = 'br';
  360. $emptyTags[] = 'input';
  361. $emptyTags[] = 'link';
  362. $emptyTags[] = 'area';
  363. $emptyTags[] = 'base';
  364. $emptyTags[] = 'col';
  365. $emptyTags[] = 'embed';
  366. $emptyTags[] = 'img';
  367. $emptyTags[] = 'keygen';
  368. $emptyTags[] = 'meta';
  369. $emptyTags[] = 'param';
  370. $emptyTags[] = 'source';
  371. $emptyTags[] = 'track';
  372. $emptyTags[] = 'wbr';
  373. if (in_array($tagName, $emptyTags)) return '<' . $tagName . (empty($params) ? '' : ' ' . self::hAttributes($params)) . '/>';
  374. return '<' . $tagName . (empty($params) ? '' : ' ' . self::hAttributes($params)) . '>' . self::hChildrens($childrens) . '</' . $tagName . '>';
  375. }
  376. public static function hAttributes($params = []) {
  377. $attr = [];
  378. foreach ($params as $k => $v) {
  379. if (is_array($v)) {
  380. $attr[] = "{$k}=\"" . implode(" ", $v) . "\"";
  381. } else {
  382. $attr[] = "{$k}=\"{$v}\"";
  383. }
  384. }
  385. return implode(" ", $attr);
  386. }
  387. public static function hChildrens($childrens = []) {
  388. if (empty($childrens)) return '';
  389. if (is_string($childrens)) return $childrens;
  390. if (!is_array($childrens)) throw new Exception("Unsupported children type");
  391. return array_reduce(
  392. $childrens,
  393. function ($curry, $child) {
  394. return "{$curry}{$child}";
  395. },
  396. ""
  397. );
  398. }
  399. /**
  400. * @param $taskPerm - 'C', 'W'
  401. */
  402. public static function hGetFormItem($acl, $fieldName, $taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
  403. Lib::loadClass('Typespecial');
  404. if (!$acl->isAllowed($fieldID, $taskPerm, $record)) {
  405. switch ($taskPerm) {
  406. case 'R': return "Brak uprawnień do odczytu";
  407. case 'W': return "Brak uprawnień do zapisu";
  408. default: return "Brak uprawnień do tego pola ({$taskPerm})";
  409. }
  410. }
  411. if ($fieldName == 'ID') return ''; // TODO: hide primaryKey?
  412. $colType = $acl->getFieldTypeById($fieldID);
  413. if (!$colType) return "Error - unknown type";
  414. $html = new stdClass();
  415. $html->_params = array();
  416. $html->tag = 'input';
  417. $html->childrens = [];
  418. $html->attrs = array();
  419. $html->attrs['id'] = $fName;
  420. $html->attrs['name'] = $fName;
  421. $html->attrs['type'] = 'text';
  422. $html->attrs['value'] = htmlspecialchars($fValue);
  423. if (isset($params['tabindex'])) {
  424. $html->attrs['tabindex'] = $params['tabindex'];
  425. }
  426. if (!$acl->hasFieldPerm($fieldID, $taskPerm)) {
  427. $html->attrs['disabled'] = 'disabled';
  428. }
  429. $maxGrid = V::get('maxGrid', 10, $params);
  430. if (substr($colType['type'], 0, 3) == 'int'
  431. || substr($colType['type'], 0, 7) == 'tinyint'
  432. || substr($colType['type'], 0, 8) == 'smallint'
  433. || substr($colType['type'], 0, 6) == 'bigint'
  434. ) {
  435. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
  436. $html->attrs['type'] = 'number';
  437. $html->attrs['class'][] = 'input-small';
  438. }
  439. else if (substr($colType['type'], 0, 6) == 'double') {
  440. $html->attrs['type'] = 'text';
  441. $html->attrs['class'][] = 'input-small';
  442. }
  443. else if (substr($colType['type'], 0, 7) == 'decimal') {
  444. $html->attrs['type'] = 'text';
  445. $html->attrs['class'][] = 'input-small';
  446. }
  447. else if (substr($colType['type'], 0, 7) == 'varchar'
  448. || substr($colType['type'], 0, 4) == 'char'
  449. ) {
  450. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
  451. $html->attrs['type'] = 'text';
  452. $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  453. if ($maxLength > 0) {
  454. $html->attrs['maxlength'] = $maxLength;
  455. }
  456. $valLength = strlen($fValue);
  457. if (isset($params['widthClass'])) {
  458. if ($params['widthClass'] == 'inside-modal') {
  459. $html->attrs['style'] = 'width:98%;';
  460. } else {
  461. $html->attrs['style'] = 'width:98%;';
  462. }
  463. } else {
  464. /*
  465. if ($maxLength < 11) {
  466. $html->attrs['class'][] = 'span2';
  467. } else if ($maxLength < 31) {
  468. $html->attrs['class'][] = 'span5';
  469. } else if ($maxLength < 51) {
  470. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  471. } else if ($maxLength < 101) {
  472. $html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
  473. } else {
  474. $html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
  475. }
  476. */
  477. }
  478. if ($maxLength > 255) {// Fix for long varchar - use textarea
  479. $html->tag = 'textarea';
  480. $html->childrens[] = htmlspecialchars($fValue);
  481. $html->attrs['rows'] = '3';
  482. unset($html->attrs['type']);
  483. unset($html->attrs['value']);
  484. }
  485. }
  486. else if (substr($colType['type'], 0, 4) == 'date') {
  487. $testDatePicker = true;
  488. if ($testDatePicker) {
  489. $html->attrs['type'] = 'text';
  490. $html->_params[] = 'date';
  491. if (substr($colType['type'], 0, 8) == 'datetime') {
  492. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  493. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  494. $html->attrs['maxlength'] = 19;
  495. } else {
  496. $html->attrs['class'][] = 'se_type-date';// datetimepicker';
  497. $html->attrs['maxlength'] = 10;
  498. }
  499. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  500. $html->attrs['value'] = '';
  501. }
  502. } else {
  503. $html->attrs['type'] = 'date';
  504. }
  505. }
  506. else if ($colType['type'] == 'time') {
  507. $testDatePicker = true;
  508. if ($testDatePicker) {
  509. $html->attrs['type'] = 'text';
  510. $html->_params[] = 'time';
  511. $html->attrs['class'][] = 'se_type-time';// datetimepicker';
  512. $html->attrs['data-format'] = 'hh:mm:ss';
  513. $html->attrs['maxlength'] = 8;
  514. if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
  515. $html->attrs['value'] = '';
  516. }
  517. } else {
  518. $html->attrs['type'] = 'time';
  519. }
  520. }
  521. else if ($colType['type'] == 'timestamp') {
  522. $testDatePicker = true;
  523. if ($testDatePicker) {
  524. $html->attrs['type'] = 'text';
  525. $html->_params[] = 'date';
  526. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  527. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  528. $html->attrs['maxlength'] = 19;
  529. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  530. $html->attrs['value'] = '';
  531. }
  532. } else {
  533. $html->attrs['type'] = 'date';
  534. }
  535. }
  536. else if (substr($colType['type'], 0, 4) == 'enum') {
  537. unset($html->attrs['type']);
  538. unset($html->attrs['value']);
  539. $html->tag = 'select';
  540. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  541. $selValue = $fValue;
  542. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  543. if ($taskPerm == 'C') {
  544. $selValue = $colType['default'];
  545. } else if ($taskPerm == 'W' && $acl->isAllowed($fieldID, 'R', $record)) {
  546. $selValue = $colType['default'];
  547. }
  548. }
  549. $html->childrens[] = [ 'option', [ 'value' => "" ], "" ];
  550. if (!empty($selValue) && !in_array($selValue, $values)) {
  551. $html->childrens[] = [ 'option', [ 'value' => $selValue, 'selected' => "selected" ], $selValue ];
  552. }
  553. foreach ($values as $val) {
  554. $html->childrens[] = [ 'option', array_merge(
  555. [ 'value' => $val ],
  556. ($selValue == $val)
  557. ? [ 'selected' => "selected" ]
  558. : []
  559. ), $val ];
  560. }
  561. }
  562. else if (substr($colType['type'], 0, 4) == 'text'
  563. || substr($colType['type'], 0, 8) == 'tinytext'
  564. || substr($colType['type'], 0, 10) == 'mediumtext'
  565. || substr($colType['type'], 0, 8) == 'longtext'
  566. ) {
  567. $html->tag = 'textarea';
  568. $html->childrens[] = htmlspecialchars($fValue);
  569. if (isset($params['widthClass'])) {
  570. if ($params['widthClass'] == 'inside-modal') {
  571. $html->attrs['style'] = 'width:98%;';
  572. } else {
  573. $html->attrs['style'] = 'width:98%;';
  574. }
  575. } else {
  576. //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  577. }
  578. $html->attrs['rows'] = '3';
  579. unset($html->attrs['type']);
  580. unset($html->attrs['value']);
  581. }
  582. else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
  583. else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
  584. else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
  585. else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
  586. else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
  587. else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
  588. else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
  589. else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
  590. else {
  591. return 'unknown Type "'.$colType['type'].'"';
  592. }
  593. $html->attrs['class'][] = 'form-control';
  594. if (!empty($html->attrs['class'])) $html->attrs['class'] = implode(" ", $html->attrs['class']);
  595. $nodeHtml = (in_array($html->tag, array('select', 'textarea')))
  596. ? [ $html->tag, $html->attrs, $html->childrens ]
  597. : $nodeHtml = [ $html->tag, $html->attrs ]
  598. ;
  599. if (in_array('date', $html->_params)) {
  600. $nodeHtml = [ 'div', [ 'class' => "input-group" ], [
  601. $nodeHtml,
  602. [ 'span', [ 'class' => "input-group-addon" ], [
  603. [ 'span', [ 'class' => "glyphicon glyphicon-calendar" ] ]
  604. ] ]
  605. ] ];
  606. }
  607. else if (in_array('time', $html->_params)) {
  608. $nodeHtml = [ 'div', [ 'class' => "input-group" ], [
  609. $nodeHtml,
  610. [ 'span', [ 'class' => "input-group-addon" ], [
  611. [ 'span', [ 'class' => "glyphicon glyphicon-time" ] ]
  612. ] ]
  613. ] ];
  614. }
  615. if (true == V::get('appendBack', '', $params)
  616. && !in_array('date', $html->_params)
  617. && !in_array('time', $html->_params)
  618. ) {
  619. if ($html->tag == 'input' && $taskPerm == 'W') {
  620. $nodeHtml = [ 'div', [ 'class' => "input-group show-last-value" ], [
  621. $nodeHtml,
  622. [ 'span', [ 'class' => "input-group-addon button-appendBack", 'title' => htmlspecialchars($fValue) ], [
  623. [ 'span', [ 'class' => "glyphicon glyphicon-arrow-left" ] ]
  624. ] ]
  625. ] ];
  626. }
  627. }
  628. $typeSpecial = Typespecial::getInstance($fieldID, $fieldName);
  629. if ($typeSpecial) {
  630. $tsParams = array();
  631. $tsValue = V::get('typespecialValue', '', $params);
  632. if (!empty($tsValue)) {
  633. $tsParams['typespecialValue'] = $tsValue;
  634. }
  635. $nodeHtml = [ 'div', [ 'class' => "field-with-typespecial" ], [
  636. $nodeHtml,
  637. $typeSpecial->hGetFormItem($acl, $fieldName, $acl->_zasobID, $fName, $fValue, $tsParams, $record),
  638. ] ];
  639. }
  640. return $nodeHtml;
  641. }
  642. public static function convertHtmlToArray($html) {
  643. $nodes = [];
  644. // <a href="index.php?_route=Users&_task=userGroups&usrLogin=michal.podejko">ustal stanowisko</a>
  645. // [ 'a', [ 'href' => "index.php?_route=Users&_task=userGroups&usrLogin=michal.podejko" ], "ustal stanowisko" ]
  646. $DBG = 0;
  647. $pos = 0;
  648. // TODO: while (true)
  649. if ('<' === substr($html, $pos, 1)) { // parse tag
  650. $tagName = ''; $attrs = []; $content = [];
  651. $endTagOpen = strpos($html, '>', $pos + 1);
  652. $endTagName = min(strpos($html, ' ', $pos + 1), $endTagOpen); // '<tag>' or '<tag attr="..">'
  653. if (false === $pos) throw new Exception("Error Processing Html - missing tagName");
  654. $tagName = substr($html, $pos + 1, $endTagName - $pos - 1);
  655. if($DBG){echo "\ntagName: '{$tagName}'";}
  656. if ('>' === substr($html, $endTagName, 1)) {
  657. } else if (' ' === substr($html, $endTagName, 1)) {
  658. if (false === $endTagOpen) throw new Exception("Error Processing Html - missing open tag end char");
  659. $attrs = UI::convertHtmlAttrsToArray(trim(substr($html, $endTagName + 1, $endTagOpen - $endTagName - 1)));
  660. } else {
  661. throw new Exception("Error Processing Html - unexpected end tag name char '" . substr($html, $endTagName, 1) . "'");
  662. }
  663. if($DBG){echo "\nattrs: '" . json_encode($attrs), "'";}
  664. // TODO: empty tags '<br>', '<br/>', '<br />', '<input ... />', '<input ... >', img, hr, etc.
  665. // TODO: nested same tags '<tagName> ... <tagName> ... </tagName> ... </tagName>'
  666. $closeTagStart = strpos($html, "</{$tagName}>", $endTagOpen + 1);
  667. if (false === $closeTagStart) throw new Exception("Error Processing Html - missing close tagName");
  668. if($DBG){echo "\nDBG \$endTagOpen: " . substr($html, $endTagOpen, 5) . "...";}
  669. if($DBG){echo "\nDBG \$endTagOpen: " . substr($html, $endTagOpen) . ".EOL";}
  670. if($DBG){echo "\nDBG \$closeTagStart strpos(\$html, '</{$tagName}>', {$endTagOpen} + 1) = '{$closeTagStart}': " . substr($html, $closeTagStart, 5) . "...";}
  671. $content = substr($html, $endTagOpen + 1, $closeTagStart - $endTagOpen - 1);
  672. $tag = [ $tagName, $attrs, $content ];
  673. if($DBG){echo "\n\$tag: ";print_r($tag);}
  674. $nodes = $tag;
  675. }
  676. return $nodes;
  677. }
  678. public static function convertHtmlAttrsToArray($strAttrs) {
  679. $attrs = [];
  680. if (!preg_match_all('((\w+)=\"([^"]*)\")', $strAttrs, $matches)) {
  681. // echo "DBG:: empty attrs or wrong syntax";
  682. return [];
  683. }
  684. $total = (count($matches) - 1) / 2;
  685. // echo "\n\$matches (total = {$total}) = ";print_r($matches);
  686. for ($i = 0; $i < $total; $i++) {
  687. $idx = $i * 2 + 1;
  688. // echo "\n\$attrs[ '{$matches[$idx][0]}' ] = '{$matches[$idx+1][0]}';";
  689. $attrs[ $matches[ $idx ][0] ] = $matches[ $idx + 1 ][0];
  690. }
  691. return $attrs;
  692. }
  693. }