UI.php 11 KB

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