UI.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. self::startTag('tbody', null); echo "\n";
  149. if (empty($rows)) {
  150. self::startTag('tr'); echo "\n";
  151. self::tag('td', [ 'style' => "padding:2px", 'colspan' => $countCols ], V::get('empty_msg', "Brak danych", $params)); echo "\n";
  152. self::endTag('tr'); echo "\n";
  153. } else {
  154. $i = 0;
  155. foreach ($rows as $row) {
  156. $i++;
  157. $trAttrs = array();
  158. if (!empty($row['@onClick'])) $trAttrs['onClick'] = $row['@onClick'];
  159. if (!empty($row['@class'])) $trAttrs['class'] = $row['@class'];
  160. if (!empty($row['@style'])) $trAttrs['style'] = $row['@style'];
  161. self::startTag('tr', $trAttrs); echo "\n";
  162. if ($showLp) { self::tag('th', [ 'style' => "padding:2px; color:#ccc" ], $i); echo "\n"; }
  163. foreach ($cols as $colName) {
  164. $rowAttrs = [ 'style' => "padding:{$cellPadding}px" ];
  165. if (!empty($row["@onClick[{$colName}]"])) $rowAttrs['onClick'] = $row["@onClick[{$colName}]"];
  166. if (!empty($row["@class[{$colName}]"])) $rowAttrs['class'] = $row["@class[{$colName}]"];
  167. if (!empty($row["@style[{$colName}]"])) $rowAttrs['style'] .= "; " . $row["@style[{$colName}]"];
  168. if (in_array($colName, $hiddenCols)) continue;
  169. self::tag('td', $rowAttrs, V::get($colName, '', $row)); echo "\n";
  170. }
  171. self::endTag('tr'); echo "\n";
  172. }
  173. }
  174. self::endTag('tbody'); echo "\n";
  175. self::endTag('table'); echo "\n";
  176. }
  177. public static function startContainer($attrs = array()) {// echo '<div class="container">' . "\n";
  178. $attrs['class'] = (!empty($attrs['class']))
  179. ? $attrs['class'] . ' ' . 'container'
  180. : 'container';
  181. self::startTag('div', $attrs, "\n");
  182. }
  183. public static function endContainer() { self::endTag('div', "\n"); }
  184. public static function startTag($tag, $attrs = array(), $addWhiteSpace = false) {
  185. $outAttrs = '';
  186. if (is_array($attrs)) {
  187. foreach ($attrs as $attrName => $val) $outAttrs .= " {$attrName}=\"{$val}\"";
  188. }
  189. echo '<' . $tag . $outAttrs . '>' . self::whiteSpace($addWhiteSpace);
  190. }
  191. public static function whiteSpace($addWhiteSpace = false) {
  192. return (!$addWhiteSpace)
  193. ? ''
  194. : (true === $addWhiteSpace) ? " " : $addWhiteSpace;
  195. }
  196. public static function endTag($tag, $addWhiteSpace = false) {
  197. echo '</' . $tag . '>' . self::whiteSpace($addWhiteSpace);
  198. }
  199. public static function tag($tag, $attrs = array(), $childrens = array(), $addWhiteSpace = false) {
  200. $whiteSpace = self::whiteSpace($addWhiteSpace);
  201. self::startTag($tag, $attrs);
  202. echo $whiteSpace;
  203. if (!empty($childrens) && is_array($childrens)) throw new Exception("UI::tag() children as nodes not implemented".json_encode($childrens));
  204. if (is_scalar($childrens)) echo $childrens;
  205. echo $whiteSpace;
  206. self::endTag($tag);
  207. echo $whiteSpace;
  208. }
  209. public static function emptyTag($tag, $attrs = array(), $addWhiteSpace = false) {
  210. $outAttrs = '';
  211. if (is_array($attrs)) {
  212. foreach ($attrs as $attrName => $val) $outAttrs .= " {$attrName}=\"{$val}\"";
  213. }
  214. echo '<' . $tag . $outAttrs . '/>' . self::whiteSpace($addWhiteSpace);
  215. }
  216. public static function link($type, $content, $href, $attrs = array()) {
  217. $attrs['class'] = V::get('class', '', $attrs);
  218. $attrs['class'] .= "btn btn-{$type}";
  219. if (!empty($attrs['className'])) {
  220. foreach ($attrs['className'] as $cls => $bool) {
  221. if ($bool) $attrs['class'] .= " {$cls}";
  222. }
  223. unset($attrs['className']);
  224. }
  225. $attrs['href'] = $href;
  226. UI::tag('a', $attrs, $content);
  227. }
  228. public static function jsAjaxTable($params) {
  229. }
  230. public static function price($value, $dec = ',') {
  231. // TODO: if not number type - string wwith wrong format - try to convert?
  232. return number_format($value, 2, $dec, ' ');
  233. }
  234. public static function inlineJS($jsFile, $jsonVars = []) {
  235. if (!file_exists($jsFile)) throw new Exception("js file '" . basename($jsFile) . "' not exists!");
  236. UI::startTag('script', [], "\n");
  237. echo "(function (global) {" . "\n";
  238. foreach ($jsonVars as $name => $var) {
  239. echo "var {$name} = " . json_encode($var) . ";\n";
  240. }
  241. include $jsFile;
  242. echo "})(window)" . "\n";
  243. UI::endTag('script', "\n");
  244. }
  245. public static function includeView($viewPath, $data = array()) {
  246. if (!file_exists($viewPath)) throw new Exception("view file '" . basename($viewPath) . "' not exists!");
  247. if (false === strpos($viewPath, APP_PATH_ROOT)) throw new Exception("Access Denied to include view '" . basename($viewPath) . "'!");
  248. if (is_array($data) && !empty($data)) {
  249. extract($data);
  250. }
  251. include $viewPath;
  252. }
  253. public static function postButton($label, $params = []) {
  254. UI::startTag('form', [
  255. 'action' => V::get('action', '', $params),
  256. 'method' => V::get('method', 'post', $params),
  257. 'style' => "display:inline"
  258. ]);
  259. foreach (V::get('data', [], $params, 'array') as $name => $value) {
  260. UI::emptyTag('input', ['type'=>'hidden', 'name'=>$name, 'value'=>$value]);
  261. }
  262. UI::tag('button', ['type'=>'submit', 'class' => 'btn ' . V::get('class', 'btn-default btn-xs', $params)], $label);
  263. UI::endTag('form');
  264. }
  265. public static function hButtonPost($label, $params = [], $childrens = []) {
  266. if (!empty($params['data'])) foreach ($params['data'] as $k => $v) $childrens[] = self::h('input', ['type'=>'hidden', 'name'=>$k, 'value'=>$v]);
  267. if (!empty($params['fields'])) {
  268. foreach ($params['fields'] as $fieldParams) {
  269. $childrens[] = self::h('input', $fieldParams);
  270. }
  271. }
  272. $childrens[] = self::h('button', [
  273. 'type'=>'submit',
  274. 'class' => 'btn ' . V::get('class', 'btn-default', $params),
  275. 'style' => V::get('style', '', $params)
  276. ], $label);
  277. return self::h('form', [
  278. 'action' => V::get('action', '', $params),
  279. 'method' => V::get('method', 'post', $params),
  280. 'style' => V::get('form.style', 'display:inline', $params),
  281. 'class' => "form-inline"
  282. ],
  283. $childrens
  284. );
  285. }
  286. public static function h($tagName, $params = [], $childrens = []) {
  287. $emptyTags = [];
  288. $emptyTags[] = 'hr';
  289. $emptyTags[] = 'br';
  290. $emptyTags[] = 'input';
  291. $emptyTags[] = 'link';
  292. $emptyTags[] = 'area';
  293. $emptyTags[] = 'base';
  294. $emptyTags[] = 'col';
  295. $emptyTags[] = 'embed';
  296. $emptyTags[] = 'img';
  297. $emptyTags[] = 'keygen';
  298. $emptyTags[] = 'meta';
  299. $emptyTags[] = 'param';
  300. $emptyTags[] = 'source';
  301. $emptyTags[] = 'track';
  302. $emptyTags[] = 'wbr';
  303. if (in_array($tagName, $emptyTags)) return '<' . $tagName . (empty($params) ? '' : ' ' . self::hAttributes($params)) . '/>';
  304. return '<' . $tagName . (empty($params) ? '' : ' ' . self::hAttributes($params)) . '>' . self::hChildrens($childrens) . '</' . $tagName . '>';
  305. }
  306. public static function hAttributes($params = []) {
  307. $attr = [];
  308. foreach ($params as $k => $v) {
  309. if (is_array($v)) {
  310. $attr[] = "{$k}=\"" . implode(" ", $v) . "\"";
  311. } else {
  312. $attr[] = "{$k}=\"{$v}\"";
  313. }
  314. }
  315. return implode(" ", $attr);
  316. }
  317. public static function hChildrens($childrens = []) {
  318. if (empty($childrens)) return '';
  319. if (is_string($childrens)) return $childrens;
  320. if (!is_array($childrens)) throw new Exception("Unsupported children type");
  321. return array_reduce(
  322. $childrens,
  323. function ($curry, $child) {
  324. return "{$curry}{$child}";
  325. },
  326. ""
  327. );
  328. }
  329. }