1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee;
13
14 use ICanBoogie\ActiveRecord;
15 use ICanBoogie\I18n;
16 use ICanBoogie\Operation;
17 use ICanBoogie\Route;
18
19 use Brickrouge\Button;
20 use Brickrouge\Element;
21 use Brickrouge\Form;
22
23 24 25 26 27 28 29
30 class DeleteBlock extends Form
31 {
32 static protected function add_assets(\Brickrouge\Document $document)
33 {
34 parent::add_assets($document);
35
36 $document->css->add('delete.css');
37 }
38
39 40 41 42 43
44 protected $module;
45
46 47 48 49 50
51 protected $key;
52
53 54 55 56 57 58 59
60 public function __construct(Module $module, array $attributes=array(), array $params=array())
61 {
62 $this->module = $module;
63 $this->key = current($params);
64
65 parent::__construct
66 (
67 $attributes + array
68 (
69 Form::HIDDENS => array
70 (
71 Operation::DESTINATION => $module->id,
72 Operation::NAME => Module::OPERATION_DELETE,
73 Operation::KEY => $this->key,
74
75 'redirect_to' => \ICanBoogie\Routing\contextualize("/admin/{$module->id}")
76 ),
77
78 self::ACTIONS => array
79 (
80 new Button
81 (
82 'Delete', array
83 (
84 'class' => 'btn-primary btn-danger',
85 'type' => 'submit'
86 )
87 )
88 ),
89
90 self::CHILDREN => array
91 (
92 $this->title_element,
93 $this->question_element,
94 $this->preview_element,
95 $this->dependencies_element
96 )
97 )
98 );
99 }
100
101 public function __toString()
102 {
103 try
104 {
105 $record = $this->record;
106 }
107 catch (\Exception $e)
108 {
109 try
110 {
111 $message = I18n\t('Unknown record id: %key', array('%key' => $this->key));
112
113 return <<<EOT
114 <div class="block-alert block--delete">
115 $this->title_element
116 <div class="alert alert-error">$message</div>
117 </div>
118 EOT;
119 }
120 catch (\Exception $e)
121 {
122 return \ICanBoogie\Debug::format_alert($e);
123 }
124 }
125
126 return parent::__toString();
127 }
128
129 130 131 132 133
134 protected function get_title()
135 {
136 return I18n\t('Delete a record');
137 }
138
139 140 141 142 143
144 protected function get_title_element()
145 {
146 return new Element('h1', array(Element::INNER_HTML => \Brickrouge\escape($this->title), 'class' => 'block-title'));
147 }
148
149 150 151 152 153
154 protected function lazy_get_record()
155 {
156 return $this->module->model[$this->key];
157 }
158
159 160 161 162 163
164 protected function get_record_name()
165 {
166
167 }
168
169 170 171 172 173
174 protected function get_question()
175 {
176 $record_name = $this->record_name;
177
178 if ($record_name)
179 {
180 $record_name = '<q>' . $record_name . '</q>';
181 }
182 else
183 {
184 $record_name = I18n\t('record_name', array(), array('default' => 'this record'));
185 }
186
187 return I18n\t('Are you sure you want to delete :name?', array('name' => $record_name));
188 }
189
190 191 192 193 194
195 protected function get_question_element()
196 {
197 return new Element('p', array(Element::INNER_HTML => $this->question));
198 }
199
200 201 202 203 204 205 206
207 protected function render_preview(\ICanBoogie\ActiveRecord $record)
208 {
209
210 }
211
212 213 214 215 216
217 protected function get_preview()
218 {
219 return $this->render_preview($this->record);
220 }
221
222 223 224 225 226
227 protected function get_preview_element()
228 {
229 return $this->preview ? new Element('div', array(Element::INNER_HTML => $this->preview, 'class' => 'preview')) : null;
230 }
231
232 233 234 235 236 237 238
239 protected function render_dependencies(array $dependencies)
240 {
241 $html = null;
242
243 foreach ($dependencies as $module_id => $by_module)
244 {
245 $flat_id = strtr($module_id, '.', '_');
246
247 $html .= '<li>';
248 $html .= '<strong>' . I18n\t(count($by_module) == 1 ? 'one' : 'other', array(), array('scope' => "$flat_id.name")) . '</strong>';
249 $html .= '<ul>';
250
251 foreach ($by_module as $key => $dependency)
252 {
253 $html .= '<li><a href="' . $dependency['edit_url'] . '">' . $dependency['title'] . '</a></li>';
254 }
255
256 $html .= '</ul>';
257 $html .= '</li>';
258 }
259
260 if (!$html)
261 {
262 return null;
263 }
264
265 $p = I18n\t('The following dependencies were found, they will also be deleted:');
266
267 return <<<EOT
268 <p>$p</p>
269 <ul>$html</ul>
270 EOT;
271 }
272
273 274 275 276 277
278 protected function get_dependencies()
279 {
280 $record = $this->record;
281 $dependencies = array();
282
283 new \ICanBoogie\ActiveRecord\CollectDependenciesEvent($record, $dependencies);
284
285 return $this->render_dependencies($dependencies);
286 }
287
288 289 290 291 292
293 protected function get_dependencies_element()
294 {
295 return $this->dependencies ? new Element('div', array(Element::INNER_HTML => $this->dependencies, 'class' => 'dependencies')) : null;
296 }
297
298 299 300 301 302 303
304 protected function decorate($html)
305 {
306 return $html;
307 }
308 }
309
310 311 312
313
314 namespace ICanBoogie\ActiveRecord;
315
316 use ICanBoogie\Route;
317
318 319 320
321 class CollectDependenciesEvent extends \ICanBoogie\Event
322 {
323 324 325 326 327
328 public $dependencies;
329
330 331 332 333 334 335
336 public function __construct(\ICanBoogie\ActiveRecord $target, array &$dependencies)
337 {
338 $this->dependencies = &$dependencies;
339
340 parent::__construct($target, 'collect_dependencies');
341 }
342
343 344 345 346 347 348 349 350 351 352 353
354 public function add($module_id, $key, $title, $edit_url=null, $view_url=null)
355 {
356 if ($edit_url === true)
357 {
358 $edit_url = \ICanBoogie\Routing\contextualize("/admin/$module_id/$key/edit");
359 }
360
361 $this->dependencies[$module_id][$key] = array('title' => $title, 'edit_url' => $edit_url, 'view_url' => $view_url);
362 }
363 }