| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
- class UI {
- public static function getTitle() {
- $title = 'SE';
- $host = $_SERVER['SERVER_NAME'];
- if (substr($host, 0, 5) == 'biuro') {
- $host = substr($host, 6);
- }
- $title = "{$host}-SE";
- return $title;
- }
- public static function gora() {
- UI::startHtml();
- }
- public static function startHtml() {
- Lib::loadClass('S');
- UI::loadTemplate('_layout_gora');
- }
- public static function dol() {
- UI::endHtml();
- }
- public static function fixFooterPosition($type) {
- $fixFooterPosition = true;// from config?
- if (!$fixFooterPosition) return;
- switch ($type) {
- case 'footer_style': return 'position:absolute; bottom:0; left:0; width:100%; ';
- case 'body_style': return 'position:relative; padding-bottom:32px;';
- case 'footer_js_tag': return "\n<script>document.body.style.minHeight = '' + (window.innerHeight - 2) + 'px';</script>";
- }
- }
- public static function endHtml() {
- $version = (file_exists(APP_PATH_ROOT . '/VERSION'))? file_get_contents(APP_PATH_ROOT . '/VERSION') : null;
- if ($version) {
- 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>';
- }
- echo UI::fixFooterPosition('footer_js_tag');
- echo "\n</body></html>";
- }
- public static function menu() {
- if (!User::logged()) return;
- if (User::hasAccess('menu')) {
- Lib::loadClass('ProcesMenu');
- $procesMenu = ProcesMenu::getInstance();
- $procesMenu->show();
- // if (!V::get('MENU_INIT', '', $_GET)) {
- // Lib::loadClass('UserActivity');
- // //echo UserActivity::showListInContainer();
- // }
- }
- else {
- UI::loadTemplate('menuLevel6');
- }
- }
- public static function loadTemplate($tmplName, $data = array()) {
- if (is_array($data) && !empty($data)) {
- extract($data);
- }
- include APP_PATH_LIB . "/tmpl/{$tmplName}.php";
- }
- public static function hotKeyDBG($str) {
- if (User::hasAccess('dbg')) {
- echo '<span class="hidden-dbg">' . htmlspecialchars($str) . '</span>';
- }
- }
- public static function showMessagesForTable($tblName) {
- if (empty($tblName)) return;
- Lib::loadClass('Router');
- $msgsRoute = Router::getRoute('Msgs');
- $msgs = $msgsRoute->getActiveMessagesForTable($tblName);
- if (!empty($msgs)) {
- self::loadTemplate('msgsForTable', array('msgs' => $msgs));
- }
- }
- public static function alert($alertType, $msg, $outputHtml = true) {
- if (!$outputHtml) {
- $type = ('danger' == $alertType) ? "ERROR" : strtoupper($alertType);
- echo "{$type}: {$msg}\n";
- return;
- }
- UI::tag('div', ['class'=>"alert alert-{$alertType}"], $msg, "\n");
- }
- public static function setTitleJsTag($title) { self::setTitle($title); }
- public static function setTitle($title) { self::tag('script', null, "document.title = '{$title}';", "\n"); }
- /**
- * $params - Array
- * $params['caption'] (optional) -> <caption>...</caption>
- * $params['cols'] (optional) -> cols, if not set read from first row
- * $params['rows'] -> rows, if not set - empty table
- * $params['rows'] -> rows, if not set - empty table
- * $params['disable_lp'] -> disable lp. col
- */
- public static function table($params) {
- $cols = V::get('cols', array(), $params);
- $rows = V::get('rows', array(), $params);
- $cols_help = V::get('cols_help', array(), $params);
- $caption = V::get('caption', '', $params);
- $showLp = (!V::get('disable_lp', false, $params));
- $countCols = 1;
- if (empty($cols) && !empty($rows)) {
- $firstRow = array();
- foreach ($rows as $row) {
- $firstRow = $row;
- break;
- }
- $cols = array_filter(
- array_keys((array)$firstRow),
- function ($col) {
- return ('@' != substr($col, 0, 1));
- }
- );
- }
- $countCols = count($cols);
- $countCols = ($showLp) ? $countCols + 1 : $countCols;
- {
- $help = array();
- foreach ($cols as $name) {
- $helpMsg = V::get($name, '', $cols_help);
- if (empty($helpMsg)) continue;
- $help[$name] = self::h('i', [
- 'class' => "glyphicon glyphicon-question-sign",
- 'title' => $helpMsg
- ], "");
- }
- }
- // if (empty($cols)) return;
- $hiddenCols = V::get('hidden_cols', array(), $params);
- $tableAttrs = [ 'class' => "table table-bordered table-hover" ];
- $html_id = V::get('__html_id', '', $params);
- if ($html_id) $tableAttrs['id'] = $html_id;
- self::startTag('table', $tableAttrs); echo "\n";
- if ($caption) { self::tag('caption', null, $caption); echo "\n"; }
- if (!empty($cols)) {
- self::startTag('thead', null); echo "\n";
- self::startTag('tr', null); echo "\n";
- if ($showLp) { self::tag('th', [ 'style' => "padding:2px" ], "Lp."); echo "\n"; }
- foreach ($cols as $colName) {
- if (in_array($colName, $hiddenCols)) continue;
- echo self::h('th', [ 'style' => "padding:2px" ], [
- $colName,
- " " . V::get($colName, '', $help)
- ]);
- echo "\n";
- }
- self::endTag('tr'); echo "\n";
- self::endTag('thead'); echo "\n";
- }
- self::startTag('tbody', null); echo "\n";
- if (empty($rows)) {
- self::startTag('tr'); echo "\n";
- self::tag('td', [ 'style' => "padding:2px", 'colspan' => $countCols ], V::get('empty_msg', "Brak danych", $params)); echo "\n";
- self::endTag('tr'); echo "\n";
- } else {
- $i = 0;
- foreach ($rows as $row) {
- $i++;
- $trAttrs = array();
- if (!empty($row['@onClick'])) $trAttrs['onClick'] = $row['@onClick'];
- if (!empty($row['@class'])) $trAttrs['class'] = $row['@class'];
- if (!empty($row['@style'])) $trAttrs['style'] = $row['@style'];
- self::startTag('tr', $trAttrs); echo "\n";
- if ($showLp) { self::tag('th', [ 'style' => "padding:2px; color:#ccc" ], $i); echo "\n"; }
- foreach ($cols as $colName) {
- if (in_array($colName, $hiddenCols)) continue;
- self::tag('td', [ 'style' => "padding:2px" ], V::get($colName, '', $row)); echo "\n";
- }
- self::endTag('tr'); echo "\n";
- }
- }
- self::endTag('tbody'); echo "\n";
- self::endTag('table'); echo "\n";
- }
- public static function startContainer($attrs = array()) {// echo '<div class="container">' . "\n";
- $attrs['class'] = (!empty($attrs['class']))
- ? $attrs['class'] . ' ' . 'container'
- : 'container';
- self::startTag('div', $attrs, "\n");
- }
- public static function endContainer() { self::endTag('div', "\n"); }
- public static function startTag($tag, $attrs = array(), $addWhiteSpace = false) {
- $outAttrs = '';
- if (is_array($attrs)) {
- foreach ($attrs as $attrName => $val) $outAttrs .= " {$attrName}=\"{$val}\"";
- }
- echo '<' . $tag . $outAttrs . '>' . self::whiteSpace($addWhiteSpace);
- }
- public static function whiteSpace($addWhiteSpace = false) {
- return (!$addWhiteSpace)
- ? ''
- : (true === $addWhiteSpace) ? " " : $addWhiteSpace;
- }
- public static function endTag($tag, $addWhiteSpace = false) {
- echo '</' . $tag . '>' . self::whiteSpace($addWhiteSpace);
- }
- public static function tag($tag, $attrs = array(), $childrens = array(), $addWhiteSpace = false) {
- $whiteSpace = self::whiteSpace($addWhiteSpace);
- self::startTag($tag, $attrs);
- echo $whiteSpace;
- if (!empty($childrens) && is_array($childrens)) throw new Exception("UI::tag() children as nodes not implemented".json_encode($childrens));
- if (is_scalar($childrens)) echo $childrens;
- echo $whiteSpace;
- self::endTag($tag);
- echo $whiteSpace;
- }
- public static function emptyTag($tag, $attrs = array(), $addWhiteSpace = false) {
- $outAttrs = '';
- if (is_array($attrs)) {
- foreach ($attrs as $attrName => $val) $outAttrs .= " {$attrName}=\"{$val}\"";
- }
- echo '<' . $tag . $outAttrs . '/>' . self::whiteSpace($addWhiteSpace);
- }
- public static function link($type, $content, $href, $attrs = array()) {
- $attrs['class'] = V::get('class', '', $attrs);
- $attrs['class'] .= "btn btn-{$type}";
- if (!empty($attrs['className'])) {
- foreach ($attrs['className'] as $cls => $bool) {
- if ($bool) $attrs['class'] .= " {$cls}";
- }
- unset($attrs['className']);
- }
- $attrs['href'] = $href;
- UI::tag('a', $attrs, $content);
- }
- public static function jsAjaxTable($params) {
- }
- public static function price($value, $dec = ',') {
- // TODO: if not number type - string wwith wrong format - try to convert?
- return number_format($value, 2, $dec, ' ');
- }
- public static function inlineJS($jsFile, $jsonVars = []) {
- if (!file_exists($jsFile)) throw new Exception("js file '" . basename($jsFile) . "' not exists!");
- UI::startTag('script', [], "\n");
- echo "(function (global) {" . "\n";
- foreach ($jsonVars as $name => $var) {
- echo "var {$name} = " . json_encode($var) . ";\n";
- }
- include $jsFile;
- echo "})(window)" . "\n";
- UI::endTag('script', "\n");
- }
- public static function includeView($viewPath, $data = array()) {
- if (!file_exists($viewPath)) throw new Exception("view file '" . basename($viewPath) . "' not exists!");
- if (false === strpos($viewPath, APP_PATH_ROOT)) throw new Exception("Access Denied to include view '" . basename($viewPath) . "'!");
- if (is_array($data) && !empty($data)) {
- extract($data);
- }
- include $viewPath;
- }
- public static function postButton($label, $params = []) {
- UI::startTag('form', [
- 'action' => V::get('action', '', $params),
- 'method' => V::get('method', 'post', $params),
- 'style' => "display:inline"
- ]);
- foreach (V::get('data', [], $params, 'array') as $name => $value) {
- UI::emptyTag('input', ['type'=>'hidden', 'name'=>$name, 'value'=>$value]);
- }
- UI::tag('button', ['type'=>'submit', 'class' => 'btn ' . V::get('class', 'btn-default btn-xs', $params)], $label);
- UI::endTag('form');
- }
- public static function hButtonPost($label, $params = []) {
- $fields = [];
- if (!empty($params['data'])) foreach ($params['data'] as $k => $v) $fields[] = self::h('input', ['type'=>'hidden', 'name'=>$k, 'value'=>$v]);
- if (!empty($params['fields'])) {
- foreach ($params['fields'] as $fieldParams) {
- $fields[] = self::h('input', $fieldParams);
- }
- }
- $fields[] = self::h('button', [
- 'type'=>'submit',
- 'class' => 'btn ' . V::get('class', 'btn-default', $params),
- 'style' => V::get('style', '', $params)
- ], $label);
- return self::h('form', [
- 'action' => V::get('action', '', $params),
- 'method' => V::get('method', 'post', $params),
- 'style' => V::get('form.style', 'display:inline', $params),
- 'class' => "form-inline"
- ],
- $fields
- );
- }
- public static function h($tagName, $params = [], $childrens = []) {
- $emptyTags = [];
- $emptyTags[] = 'hr';
- $emptyTags[] = 'br';
- $emptyTags[] = 'input';
- $emptyTags[] = 'link';
- $emptyTags[] = 'area';
- $emptyTags[] = 'base';
- $emptyTags[] = 'col';
- $emptyTags[] = 'embed';
- $emptyTags[] = 'img';
- $emptyTags[] = 'keygen';
- $emptyTags[] = 'meta';
- $emptyTags[] = 'param';
- $emptyTags[] = 'source';
- $emptyTags[] = 'track';
- $emptyTags[] = 'wbr';
- if (in_array($tagName, $emptyTags)) return '<' . $tagName . (empty($params) ? '' : ' ' . self::hAttributes($params)) . '/>';
- return '<' . $tagName . (empty($params) ? '' : ' ' . self::hAttributes($params)) . '>' . self::hChildrens($childrens) . '</' . $tagName . '>';
- }
- public static function hAttributes($params = []) {
- $attr = [];
- foreach ($params as $k => $v) {
- if (is_array($v)) {
- $attr[] = "{$k}=\"" . implode(" ", $v) . "\"";
- } else {
- $attr[] = "{$k}=\"{$v}\"";
- }
- }
- return implode(" ", $attr);
- }
- public static function hChildrens($childrens = []) {
- if (empty($childrens)) return '';
- if (is_string($childrens)) return $childrens;
- if (!is_array($childrens)) throw new Exception("Unsupported children type");
- return array_reduce(
- $childrens,
- function ($curry, $child) {
- return "{$curry}{$child}";
- },
- ""
- );
- }
- }
|