UI.php 11 KB

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