UI.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. if (null === $params) return '';
  389. if (!is_array($params)) {
  390. try {
  391. throw new Exception("Wrong params type in UI::hAttributes");
  392. } catch (Exception $e) {
  393. DBG::log($e);
  394. }
  395. }
  396. foreach ($params as $k => $v) {
  397. if (is_array($v)) {
  398. $attr[] = "{$k}=\"" . implode(" ", $v) . "\"";
  399. } else {
  400. $attr[] = "{$k}=\"{$v}\"";
  401. }
  402. }
  403. return implode(" ", $attr);
  404. }
  405. public static function hChildrens($childrens = []) {
  406. if (empty($childrens)) return '';
  407. if (is_string($childrens)) return $childrens;
  408. if (!is_array($childrens)) throw new Exception("Unsupported children type");
  409. return array_reduce(
  410. $childrens,
  411. function ($curry, $child) {
  412. return "{$curry}{$child}";
  413. },
  414. ""
  415. );
  416. }
  417. /**
  418. * @param $taskPerm - 'C', 'W'
  419. */
  420. public static function hGetFormItem($acl, $fieldName, $taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
  421. Lib::loadClass('Typespecial');
  422. if (!$acl->isAllowed($fieldID, $taskPerm, $record)) {
  423. switch ($taskPerm) {
  424. case 'R': return "Brak uprawnień do odczytu";
  425. case 'W': return "Brak uprawnień do zapisu";
  426. default: return "Brak uprawnień do tego pola ({$taskPerm})";
  427. }
  428. }
  429. if ($fieldName == 'ID') return ''; // TODO: hide primaryKey?
  430. $colType = $acl->getFieldTypeById($fieldID);
  431. if (!$colType) return "Error - unknown type";
  432. $html = new stdClass();
  433. $html->_params = array();
  434. $html->tag = 'input';
  435. $html->childrens = [];
  436. $html->attrs = array();
  437. $html->attrs['id'] = $fName;
  438. $html->attrs['name'] = $fName;
  439. $html->attrs['type'] = 'text';
  440. $html->attrs['value'] = htmlspecialchars($fValue);
  441. if (isset($params['tabindex'])) {
  442. $html->attrs['tabindex'] = $params['tabindex'];
  443. }
  444. if (!$acl->hasFieldPerm($fieldID, $taskPerm)) {
  445. $html->attrs['disabled'] = 'disabled';
  446. }
  447. $maxGrid = V::get('maxGrid', 10, $params);
  448. if (substr($colType['type'], 0, 3) == 'int'
  449. || substr($colType['type'], 0, 7) == 'tinyint'
  450. || substr($colType['type'], 0, 8) == 'smallint'
  451. || substr($colType['type'], 0, 6) == 'bigint'
  452. ) {
  453. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
  454. $html->attrs['type'] = 'number';
  455. $html->attrs['class'][] = 'input-small';
  456. }
  457. else if (substr($colType['type'], 0, 6) == 'double') {
  458. $html->attrs['type'] = 'text';
  459. $html->attrs['class'][] = 'input-small';
  460. }
  461. else if (substr($colType['type'], 0, 7) == 'decimal') {
  462. $html->attrs['type'] = 'text';
  463. $html->attrs['class'][] = 'input-small';
  464. }
  465. else if (substr($colType['type'], 0, 7) == 'varchar'
  466. || substr($colType['type'], 0, 4) == 'char'
  467. ) {
  468. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
  469. $html->attrs['type'] = 'text';
  470. $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  471. if ($maxLength > 0) {
  472. $html->attrs['maxlength'] = $maxLength;
  473. }
  474. $valLength = strlen($fValue);
  475. if (isset($params['widthClass'])) {
  476. if ($params['widthClass'] == 'inside-modal') {
  477. $html->attrs['style'] = 'width:98%;';
  478. } else {
  479. $html->attrs['style'] = 'width:98%;';
  480. }
  481. } else {
  482. /*
  483. if ($maxLength < 11) {
  484. $html->attrs['class'][] = 'span2';
  485. } else if ($maxLength < 31) {
  486. $html->attrs['class'][] = 'span5';
  487. } else if ($maxLength < 51) {
  488. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  489. } else if ($maxLength < 101) {
  490. $html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
  491. } else {
  492. $html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
  493. }
  494. */
  495. }
  496. if ($maxLength > 255) {// Fix for long varchar - use textarea
  497. $html->tag = 'textarea';
  498. $html->childrens[] = htmlspecialchars($fValue);
  499. $html->attrs['rows'] = '3';
  500. unset($html->attrs['type']);
  501. unset($html->attrs['value']);
  502. }
  503. }
  504. else if (substr($colType['type'], 0, 4) == 'date') {
  505. $testDatePicker = true;
  506. if ($testDatePicker) {
  507. $html->attrs['type'] = 'text';
  508. $html->_params[] = 'date';
  509. if (substr($colType['type'], 0, 8) == 'datetime') {
  510. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  511. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  512. $html->attrs['maxlength'] = 19;
  513. } else {
  514. $html->attrs['class'][] = 'se_type-date';// datetimepicker';
  515. $html->attrs['maxlength'] = 10;
  516. }
  517. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  518. $html->attrs['value'] = '';
  519. }
  520. } else {
  521. $html->attrs['type'] = 'date';
  522. }
  523. }
  524. else if ($colType['type'] == 'time') {
  525. $testDatePicker = true;
  526. if ($testDatePicker) {
  527. $html->attrs['type'] = 'text';
  528. $html->_params[] = 'time';
  529. $html->attrs['class'][] = 'se_type-time';// datetimepicker';
  530. $html->attrs['data-format'] = 'hh:mm:ss';
  531. $html->attrs['maxlength'] = 8;
  532. if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
  533. $html->attrs['value'] = '';
  534. }
  535. } else {
  536. $html->attrs['type'] = 'time';
  537. }
  538. }
  539. else if ($colType['type'] == 'timestamp') {
  540. $testDatePicker = true;
  541. if ($testDatePicker) {
  542. $html->attrs['type'] = 'text';
  543. $html->_params[] = 'date';
  544. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  545. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  546. $html->attrs['maxlength'] = 19;
  547. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  548. $html->attrs['value'] = '';
  549. }
  550. } else {
  551. $html->attrs['type'] = 'date';
  552. }
  553. }
  554. else if (substr($colType['type'], 0, 4) == 'enum') {
  555. unset($html->attrs['type']);
  556. unset($html->attrs['value']);
  557. $html->tag = 'select';
  558. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  559. $selValue = $fValue;
  560. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  561. if ($taskPerm == 'C') {
  562. $selValue = $colType['default'];
  563. } else if ($taskPerm == 'W' && $acl->isAllowed($fieldID, 'R', $record)) {
  564. $selValue = $colType['default'];
  565. }
  566. }
  567. $html->childrens[] = [ 'option', [ 'value' => "" ], "" ];
  568. if (!empty($selValue) && !in_array($selValue, $values)) {
  569. $html->childrens[] = [ 'option', [ 'value' => $selValue, 'selected' => "selected" ], $selValue ];
  570. }
  571. foreach ($values as $val) {
  572. $html->childrens[] = [ 'option', array_merge(
  573. [ 'value' => $val ],
  574. ($selValue == $val)
  575. ? [ 'selected' => "selected" ]
  576. : []
  577. ), $val ];
  578. }
  579. }
  580. else if (substr($colType['type'], 0, 4) == 'text'
  581. || substr($colType['type'], 0, 8) == 'tinytext'
  582. || substr($colType['type'], 0, 10) == 'mediumtext'
  583. || substr($colType['type'], 0, 8) == 'longtext'
  584. ) {
  585. $html->tag = 'textarea';
  586. $html->childrens[] = htmlspecialchars($fValue);
  587. if (isset($params['widthClass'])) {
  588. if ($params['widthClass'] == 'inside-modal') {
  589. $html->attrs['style'] = 'width:98%;';
  590. } else {
  591. $html->attrs['style'] = 'width:98%;';
  592. }
  593. } else {
  594. //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  595. }
  596. $html->attrs['rows'] = '3';
  597. unset($html->attrs['type']);
  598. unset($html->attrs['value']);
  599. }
  600. else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
  601. else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
  602. else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
  603. else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
  604. else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
  605. else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
  606. else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
  607. else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
  608. else {
  609. return 'unknown Type "'.$colType['type'].'"';
  610. }
  611. $html->attrs['class'][] = 'form-control';
  612. if (!empty($html->attrs['class'])) $html->attrs['class'] = implode(" ", $html->attrs['class']);
  613. $nodeHtml = (in_array($html->tag, array('select', 'textarea')))
  614. ? [ $html->tag, $html->attrs, $html->childrens ]
  615. : $nodeHtml = [ $html->tag, $html->attrs ]
  616. ;
  617. if (in_array('date', $html->_params)) {
  618. $nodeHtml = [ 'div', [ 'class' => "input-group" ], [
  619. $nodeHtml,
  620. [ 'span', [ 'class' => "input-group-addon" ], [
  621. [ 'span', [ 'class' => "glyphicon glyphicon-calendar" ] ]
  622. ] ]
  623. ] ];
  624. }
  625. else if (in_array('time', $html->_params)) {
  626. $nodeHtml = [ 'div', [ 'class' => "input-group" ], [
  627. $nodeHtml,
  628. [ 'span', [ 'class' => "input-group-addon" ], [
  629. [ 'span', [ 'class' => "glyphicon glyphicon-time" ] ]
  630. ] ]
  631. ] ];
  632. }
  633. if (true == V::get('appendBack', '', $params)
  634. && !in_array('date', $html->_params)
  635. && !in_array('time', $html->_params)
  636. ) {
  637. if ($html->tag == 'input' && $taskPerm == 'W') {
  638. $nodeHtml = [ 'div', [ 'class' => "input-group show-last-value" ], [
  639. $nodeHtml,
  640. [ 'span', [ 'class' => "input-group-addon button-appendBack", 'title' => htmlspecialchars($fValue) ], [
  641. [ 'span', [ 'class' => "glyphicon glyphicon-arrow-left" ] ]
  642. ] ]
  643. ] ];
  644. }
  645. }
  646. $typeSpecial = Typespecial::getInstance($fieldID, $fieldName);
  647. if ($typeSpecial) {
  648. $tsParams = array();
  649. $tsValue = V::get('typespecialValue', '', $params);
  650. if (!empty($tsValue)) {
  651. $tsParams['typespecialValue'] = $tsValue;
  652. }
  653. $nodeHtml = [ 'div', [ 'class' => "field-with-typespecial" ], [
  654. $nodeHtml,
  655. $typeSpecial->hGetFormItem($acl, $fieldName, $acl->_zasobID, $fName, $fValue, $tsParams, $record),
  656. ] ];
  657. }
  658. return $nodeHtml;
  659. }
  660. public static function convertHtmlToArray($html) {
  661. $nodes = [];
  662. // <a href="index.php?_route=Users&_task=userGroups&usrLogin=michal.podejko">ustal stanowisko</a>
  663. // [ 'a', [ 'href' => "index.php?_route=Users&_task=userGroups&usrLogin=michal.podejko" ], "ustal stanowisko" ]
  664. $DBG = 0;
  665. $pos = 0;
  666. // TODO: while (true)
  667. if ('<' === substr($html, $pos, 1)) { // parse tag
  668. $tagName = ''; $attrs = []; $content = [];
  669. $endTagOpen = strpos($html, '>', $pos + 1);
  670. $endTagName = min(strpos($html, ' ', $pos + 1), $endTagOpen); // '<tag>' or '<tag attr="..">'
  671. if (false === $pos) throw new Exception("Error Processing Html - missing tagName");
  672. $tagName = substr($html, $pos + 1, $endTagName - $pos - 1);
  673. if($DBG){echo "\ntagName: '{$tagName}'";}
  674. if ('>' === substr($html, $endTagName, 1)) {
  675. } else if (' ' === substr($html, $endTagName, 1)) {
  676. if (false === $endTagOpen) throw new Exception("Error Processing Html - missing open tag end char");
  677. $attrs = UI::convertHtmlAttrsToArray(trim(substr($html, $endTagName + 1, $endTagOpen - $endTagName - 1)));
  678. } else {
  679. throw new Exception("Error Processing Html - unexpected end tag name char '" . substr($html, $endTagName, 1) . "'");
  680. }
  681. if($DBG){echo "\nattrs: '" . json_encode($attrs), "'";}
  682. // TODO: empty tags '<br>', '<br/>', '<br />', '<input ... />', '<input ... >', img, hr, etc.
  683. // TODO: nested same tags '<tagName> ... <tagName> ... </tagName> ... </tagName>'
  684. $closeTagStart = strpos($html, "</{$tagName}>", $endTagOpen + 1);
  685. if (false === $closeTagStart) throw new Exception("Error Processing Html - missing close tagName");
  686. if($DBG){echo "\nDBG \$endTagOpen: " . substr($html, $endTagOpen, 5) . "...";}
  687. if($DBG){echo "\nDBG \$endTagOpen: " . substr($html, $endTagOpen) . ".EOL";}
  688. if($DBG){echo "\nDBG \$closeTagStart strpos(\$html, '</{$tagName}>', {$endTagOpen} + 1) = '{$closeTagStart}': " . substr($html, $closeTagStart, 5) . "...";}
  689. $content = substr($html, $endTagOpen + 1, $closeTagStart - $endTagOpen - 1);
  690. $tag = [ $tagName, $attrs, $content ];
  691. if($DBG){echo "\n\$tag: ";print_r($tag);}
  692. $nodes = $tag;
  693. }
  694. return $nodes;
  695. }
  696. public static function convertHtmlAttrsToArray($strAttrs) {
  697. $attrs = [];
  698. if (!preg_match_all('((\w+)=\"([^"]*)\")', $strAttrs, $matches)) {
  699. // echo "DBG:: empty attrs or wrong syntax";
  700. return [];
  701. }
  702. $total = (count($matches) - 1) / 2;
  703. // echo "\n\$matches (total = {$total}) = ";print_r($matches);
  704. for ($i = 0; $i < $total; $i++) {
  705. $idx = $i * 2 + 1;
  706. // echo "\n\$attrs[ '{$matches[$idx][0]}' ] = '{$matches[$idx+1][0]}';";
  707. $attrs[ $matches[ $idx ][0] ] = $matches[ $idx + 1 ][0];
  708. }
  709. return $attrs;
  710. }
  711. }