UI.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_keys((array)$firstRow);
  104. }
  105. // if (empty($cols)) return;
  106. $hiddenCols = V::get('hidden_cols', array(), $params);
  107. $tableAttrs = [ 'class' => "table table-bordered table-hover" ];
  108. $html_id = V::get('__html_id', '', $params);
  109. if ($html_id) $tableAttrs['id'] = $html_id;
  110. self::startTag('table', $tableAttrs); echo "\n";
  111. if ($caption) { self::tag('caption', null, $caption); echo "\n"; }
  112. if (!empty($cols)) {
  113. self::startTag('thead', null); echo "\n";
  114. self::startTag('tr', null); echo "\n";
  115. if ($showLp) { self::tag('th', [ 'style' => "padding:2px" ], "Lp."); echo "\n"; }
  116. foreach ($cols as $colName) {
  117. if (in_array($colName, $hiddenCols)) continue;
  118. self::tag('th', [ 'style' => "padding:2px"], $colName); echo "\n";
  119. }
  120. self::endTag('tr'); echo "\n";
  121. self::endTag('thead'); echo "\n";
  122. }
  123. self::startTag('tbody', null); echo "\n";
  124. if (empty($rows)) {
  125. self::startTag('tr'); echo "\n";
  126. self::tag('td', [ 'style' => "padding:2px" ], V::get('empty_msg', "Brak danych", $params)); echo "\n";
  127. self::endTag('tr'); echo "\n";
  128. } else {
  129. $i = 0;
  130. foreach ($rows as $row) {
  131. $i++;
  132. $trAttrs = array();
  133. if (!empty($row['__js_on_click'])) $trAttrs['onClick'] = $row['__js_on_click'];
  134. self::startTag('tr', $trAttrs); echo "\n";
  135. if ($showLp) { self::tag('th', [ 'style' => "padding:2px; color:#ccc" ], $i); echo "\n"; }
  136. foreach ($cols as $colName) {
  137. if (in_array($colName, $hiddenCols)) continue;
  138. self::tag('td', [ 'style' => "padding:2px" ], V::get($colName, '', $row)); echo "\n";
  139. }
  140. self::endTag('tr'); echo "\n";
  141. }
  142. }
  143. self::endTag('tbody'); echo "\n";
  144. self::endTag('table'); echo "\n";
  145. }
  146. public static function startContainer($attrs = array()) {// echo '<div class="container">' . "\n";
  147. $attrs['class'] = (!empty($attrs['class']))
  148. ? $attrs['class'] . ' ' . 'container'
  149. : 'container';
  150. self::startTag('div', $attrs, "\n");
  151. }
  152. public static function endContainer() { self::endTag('div', "\n"); }
  153. public static function startTag($tag, $attrs = array(), $addWhiteSpace = false) {
  154. $outAttrs = '';
  155. if (is_array($attrs)) {
  156. foreach ($attrs as $attrName => $val) $outAttrs .= " {$attrName}=\"{$val}\"";
  157. }
  158. echo '<' . $tag . $outAttrs . '>' . self::whiteSpace($addWhiteSpace);
  159. }
  160. public static function whiteSpace($addWhiteSpace = false) {
  161. return (!$addWhiteSpace)
  162. ? ''
  163. : (true === $addWhiteSpace) ? " " : $addWhiteSpace;
  164. }
  165. public static function endTag($tag, $addWhiteSpace = false) {
  166. echo '</' . $tag . '>' . self::whiteSpace($addWhiteSpace);
  167. }
  168. public static function tag($tag, $attrs = array(), $childrens = array(), $addWhiteSpace = false) {
  169. $whiteSpace = self::whiteSpace($addWhiteSpace);
  170. self::startTag($tag, $attrs);
  171. echo $whiteSpace;
  172. if (!empty($childrens) && is_array($childrens)) throw new Exception("UI::tag() children as nodes not implemented".json_encode($childrens));
  173. if (is_scalar($childrens)) echo $childrens;
  174. echo $whiteSpace;
  175. self::endTag($tag);
  176. echo $whiteSpace;
  177. }
  178. public static function emptyTag($tag, $attrs = array(), $addWhiteSpace = false) {
  179. $outAttrs = '';
  180. if (is_array($attrs)) {
  181. foreach ($attrs as $attrName => $val) $outAttrs .= " {$attrName}=\"{$val}\"";
  182. }
  183. echo '<' . $tag . $outAttrs . '/>' . self::whiteSpace($addWhiteSpace);
  184. }
  185. public static function link($type, $content, $href, $attrs = array()) {
  186. $attrs['class'] = V::get('class', '', $attrs);
  187. $attrs['class'] .= "btn btn-{$type}";
  188. if (!empty($attrs['className'])) {
  189. foreach ($attrs['className'] as $cls => $bool) {
  190. if ($bool) $attrs['class'] .= " {$cls}";
  191. }
  192. unset($attrs['className']);
  193. }
  194. $attrs['href'] = $href;
  195. UI::tag('a', $attrs, $content);
  196. }
  197. public static function jsAjaxTable($params) {
  198. }
  199. public static function price($value, $dec = ',') {
  200. // TODO: if not number type - string wwith wrong format - try to convert?
  201. return number_format($value, 2, $dec, ' ');
  202. }
  203. public static function inlineJS($jsFile, $jsonVars = []) {
  204. if (!file_exists($jsFile)) throw new Exception("js file '" . basename($jsFile) . "' not exists!");
  205. UI::startTag('script', [], "\n");
  206. foreach ($jsonVars as $name => $var) {
  207. echo "var {$name} = " . json_encode($var) . ";\n";
  208. }
  209. include $jsFile;
  210. UI::endTag('script', "\n");
  211. }
  212. }