1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
{
/** 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}";
}
}
|