1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Views;
13
14 use ICanBoogie\I18n;
15
16 use Brickrouge\Document;
17 use Brickrouge\Element;
18
19 20 21
22 class ViewEditorElement extends Element implements \Icybee\Modules\Editor\EditorElement
23 {
24 static protected function add_assets(Document $document)
25 {
26 parent::add_assets($document);
27
28 $document->css->add(__DIR__ . '/assets/editor.css');
29 $document->js->add(__DIR__ . '/assets/editor.js');
30 }
31
32 public function __construct(array $attributes)
33 {
34 parent::__construct
35 (
36 'div', $attributes + array
37 (
38 'class' => 'view-editor'
39 )
40 );
41 }
42
43 public function render_inner_html()
44 {
45 global $core;
46
47 $selected_category = null;
48 $selected_subcategory = null;
49 $selected_view = $this['value'];
50
51 $categories = array();
52 $descriptors = $core->modules->descriptors;
53 $views = $core->views;
54
55 foreach ($views as $id => $view)
56 {
57 list($module_id, $type) = explode('/', $id) + array(1 => null);
58
59 if (!isset($core->modules[$module_id]))
60 {
61 continue;
62 }
63
64 $category = 'Misc';
65 $subcategory = 'Misc';
66
67 if ($type !== null && isset($descriptors[$module_id]))
68 {
69 $descriptor = $descriptors[$module_id];
70
71 if (isset($descriptor[Module::T_CATEGORY]))
72 {
73 $category = $descriptors[$module_id][Module::T_CATEGORY];
74 $category = I18n\t($category, array(), array('scope' => 'module_category'));
75 }
76
77 $subcategory = $descriptor[Module::T_TITLE];
78 $subcategory = I18n\t(strtr($module_id, '.', '_'), array(), array('scope' => 'module_title', 'default' => $subcategory));
79 }
80
81 $categories[$category][$subcategory][$id] = $view;
82
83 if ($id == $selected_view)
84 {
85 $selected_category = $category;
86 $selected_subcategory = $subcategory;
87 }
88 }
89
90 uksort($categories, 'ICanBoogie\unaccent_compare_ci');
91
92 foreach ($categories as $category => $subcategories)
93 {
94 uksort($subcategories, 'ICanBoogie\unaccent_compare_ci');
95
96 $categories[$category] = $subcategories;
97 }
98
99 $rendered_categories = $this->render_categories($categories, $selected_category);
100 $rendered_subcategories = $this->render_subcategories($categories, $selected_category, $selected_subcategory);
101 $rendered_views = $this->render_views($categories, $selected_category, $selected_subcategory, $selected_view);
102
103 return parent::render_inner_html() . <<<EOT
104 <table>
105 <tr>
106 <td class="view-editor-categories">$rendered_categories</td>
107 <td class="view-editor-subcategories">$rendered_subcategories</td>
108 <td class="view-editor-views">$rendered_views</td>
109 </tr>
110 </table>
111 EOT;
112 }
113
114 protected function render_categories(array $categories, $selected)
115 {
116 $html = '';
117
118 foreach ($categories as $category => $dummy)
119 {
120 $html .= '<li' . ($category == $selected ? ' class="active selected"' : '') . '><a href="#select">' . \ICanBoogie\escape($category) . '</a></li>';
121 }
122
123 return '<ul>' . $html . '</ul>';
124 }
125
126 protected function render_subcategories(array $categories, $selected_category, $selected_subcategory)
127 {
128 $html = '';
129
130 foreach ($categories as $category => $subcategories)
131 {
132 $html .= '<ul' . ($category == $selected_category ? ' class="active selected"' : '') . '>';
133
134 foreach ($subcategories as $subcategory => $views)
135 {
136 $html .= '<li' . ($subcategory == $selected_subcategory ? ' class="active selected"' : '') . '><a href="#select">' . \ICanBoogie\escape($subcategory) . '</a></li>';
137 }
138
139 $html .= '</ul>';
140 }
141
142 return $html;
143 }
144
145 protected function render_views(array $categories, $selected_category, $selected_subcategory, $selected_view)
146 {
147 global $core;
148
149 $html = '';
150 $context = $core->site->path;
151 $name = $this['name'];
152
153 foreach ($categories as $category => $subcategories)
154 {
155 foreach ($subcategories as $subcategory => $views)
156 {
157 $active = '';
158 $items = array();
159
160 foreach ($views as $id => $view)
161 {
162 if (!$view['title'])
163 {
164 continue;
165 }
166
167 $title = I18n\t($view['title'], $view['title args']);
168
169 $description = null;
170
171 if (isset($view['description']))
172 {
173 $description = $view['description'];
174
175
176
177 $description = strtr
178 (
179 $description, array
180 (
181 '#{url}' => $context . '/admin/'
182 )
183 );
184 }
185
186 if ($id == $selected_view)
187 {
188 $active = ' class="active"';
189 }
190
191 $items[] = new Element
192 (
193 Element::TYPE_RADIO, array
194 (
195 Element::LABEL => $title,
196 Element::DESCRIPTION => $description,
197
198 'name' => $name,
199 'value' => $id,
200 'checked' => ($id == $selected_view)
201 )
202 );
203 }
204
205 $html .= "<ul$active><li>" . implode('</li><li>', $items) . '</li></ul>';
206 }
207 }
208
209 return $html;
210 }
211 }