UI.php 26 KB

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