diff options
Diffstat (limited to 'public/app/views/simple/Render.php')
| -rw-r--r-- | public/app/views/simple/Render.php | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/public/app/views/simple/Render.php b/public/app/views/simple/Render.php new file mode 100644 index 0000000..79a93a0 --- /dev/null +++ b/public/app/views/simple/Render.php @@ -0,0 +1,182 @@ +<?php +/** Wiki Render Class + * ----------------------------------------------------------------------------- + * + * + automates the creation of common html elements used in Wiki + * + * + methods are called statically (without object-instantiation) + * + * + Render class constructs the Render::all_breadcrumbs() super-array + * + * ----------------------------------------------------------------------------- + */ +class Render_example +{ + + /** Render::template($data) + * (as any render function in anom framework) + * + * @param $file (string) : filename [with path] of a php/html (acting as template) + * @param $data : array of variable-name:value pairs (extracted/used into template) + */ + static function template($file, $data) { + + if (file_exists($file)) { + extract( $data ); + require( $file ); + + } else { + echo "<!-- view ". $file ." is missing -->"; + } + + } + + + /** all_breadcrumbs + * ------------------------------------------------------------------------- + * + * returns an array of all breadcrumbs + * where array-key of each record is category[id] + * + * NOTE: + * --- + * Render::all_breadcrumbs returns an indexed super-array; + * each array item includes a banch of information: [ + * breadcrumb, + * rec: [ id , title ], + * parents: [ [id, title] , ... ] + * childs: [ [id, title] , ... ], + * level + * ] + * + * Use Cases: + * --- + * as a super-array, the output can be used in many cases + * for example... + * into <select-option> form elements + * .. while selecting category for a post + * .. or editing a category + * or directry referring to category's parents/childs + * + * Arguments: + * --- + * @param $tree (array) : category tree (with childs and parents parts) + * @param $detimiter (string, optional) : string to split breadcrumb's path-nodes + * @param $exception (int, optional) : id of category to exclude (subcategories shall be excluded too) + * @param $l (int, not-pass) : depth level of the node; DO NOT SET (takes values automaticaly) + * @return array of breadcrumbs + * ------------------------------------------------------------------------- + */ + static public function all_breadcrumbs($tree, $delimiter = " / ", $exception = 0, $l = 0) + { + $all = []; // results array + + foreach($tree as $node) { // loop through all nodes + + if (intval($node['rec']['id']) != $exception) { // if node is not exception + + // construct breadcrumb html of node + // --- -- -- - - - + $breadcrumb = ""; + foreach($node['parents'] as $par) { // first: join path titles + $breadcrumb .= $par['title'] . $delimiter; + } + $breadcrumb .= $node['rec']['title']; // last: append title + + // make a new super record + // --- -- -- - - - + $all[$node['rec']['id']] = [ // set record is as key + 'breadcrumb' => $breadcrumb, // add breadcrump to results + 'rec' => $node['rec'], // + node info + 'parents' => $node['parents'], // + parents array + 'childs' => self::first_level_childs($node), // + direct childs + 'level' => $l // + level + ]; + + // recursively traverse children nodes + // --- -- -- - - - + if (isset($node['childs']) && $node['childs'] != []) { + $child_breadcrumbs = self::all_breadcrumbs( + $node['childs'], + $delimiter, + $exception, + $l+1 + ); + + $all = $all + $child_breadcrumbs; // concatenate arrays (keep array-keys) + } + } + + } + return $all; + + } + + /** first_level_childs + * --- -- -- - - - + * used by all_breadcrumbs() + */ + static private function first_level_childs($node) + { + $childs = []; + if ($node['childs'] == []) { + return []; + } + foreach($node['childs'] as $key => $kid) { + $childs[] = [ + 'id' => $kid['rec']['id'], + 'title' => $kid['rec']['title'] + ]; + } + return $childs; + } + + + /** date_box + * creates a pretty date box; outputs greek months + * + * @param $date (string) in 'Y-m-d H:i:s' format + * @return html (string) of the date-box + */ + static public function date_box($date) + { + $months_Gr = ['', + 'Ιαν', 'Φεβ', 'Μαρ', 'Απρ', + 'Μάι', 'Ιουν', 'Ιουλ', 'Αυγ', + 'Σεπ', 'Οκτ', 'Νοε', 'Δεκ' + ]; + $date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $date); + $day = $date->format("j"); + $month = $months_Gr[intval($date->format("m"))]; + $year = $date->format("Y"); + + // print_r([$date, $day, $month, $year]); die(); + + return "<div class='date'> + <p class='day'>{$day}</p> + <p class='month'>{$month}</p> + <p class='year'>{$year}</p> + </div>"; + } + + /** date_friendly + * outputs an unformated greek-language date string + * + * @param $date (string) in 'Y-m-d H:i:s' format + */ + static public function date_friendly($date, $display_time = false) + { + $months_Gr = ['', + 'Ιαν', 'Φεβ', 'Μαρ', 'Απρ', 'Μάι', 'Ιουν', + 'Ιουλ', 'Αυγ', 'Σεπ', 'Οκτ', 'Νοε', 'Δεκ' + ]; + $date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $date); + $day = $date->format("j"); + $month = $months_Gr[intval($date->format("m"))]; + $year = $date->format("Y"); + $time = ($display_time) ? $date->format("H:i") : ''; + + return "{$day} {$month} {$year} {$time}"; + } + +} |
