Development

#924: actions.class.php

You must first sign up to be able to contribute.

Ticket #924: actions.class.php

File actions.class.php, 21.1 kB (added by Leon.van.der.Ree, 1 year ago)
Line 
1 [?php
2
3 /**
4  * <?php echo $this->getGeneratedModuleName() ?> actions.
5  *
6  * @package    ##PROJECT_NAME##
7  * @subpackage <?php echo $this->getGeneratedModuleName() ?>
8
9  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
10  * @version    SVN: $Id: actions.class.php 3501 2007-02-18 10:28:17Z fabien $
11  */
12 class <?php echo $this->getGeneratedModuleName() ?>Actions extends sfActions
13 {
14   public function executeIndex()
15   {
16     return $this->forward('<?php echo $this->getModuleName() ?>', 'list');
17   }
18
19   public function executeList()
20   {
21     $this->processSort();
22
23     $this->processFilters();
24
25 <?php if ($this->getParameterValue('list.filters')): ?>
26     $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/<?php echo $this->getSingularName() ?>/filters');
27 <?php endif; ?>
28
29     // pager
30     $this->pager = new sfPropelPager('<?php echo $this->getClassName() ?>', <?php echo $this->getParameterValue('list.max_per_page', 20) ?>);
31     $c = new Criteria();
32 <?php if ($fields = $this->getParameterValue('list.fields')): ?>
33 <?php foreach ($fields as $key => $field): ?>
34 <?php if ($join_fields= $this->getParameterValue('list.fields.'.$key.'.join_fields')): ?>
35     $c->addJoin(<?=$join_fields[0]?>,<?=$join_fields[1]?>);
36 <?php endif; ?>
37 <?php endforeach; ?>
38 <?php endif; ?>
39     $this->addSortCriteria($c);
40     $this->addFiltersCriteria($c);
41     $this->pager->setCriteria($c);
42     $this->pager->setPage($this->getRequestParameter('page', 1));
43 <?php if ($this->getParameterValue('list.peer_method')): ?>
44     $this->pager->setPeerMethod('<?php echo $this->getParameterValue('list.peer_method') ?>');
45 <?php endif; ?>
46 <?php if ($this->getParameterValue('list.peer_count_method')): ?>
47     $this->pager->setPeerCountMethod('<?php echo $this->getParameterValue('list.peer_count_method') ?>');
48 <?php endif; ?>
49     $this->pager->init();
50   }
51
52   public function executeCreate()
53   {
54     return $this->forward('<?php echo $this->getModuleName() ?>', 'edit');
55   }
56
57   public function executeSave()
58   {
59     return $this->forward('<?php echo $this->getModuleName() ?>', 'edit');
60   }
61
62   public function executeEdit()
63   {
64     $this-><?php echo $this->getSingularName() ?> = $this->get<?php echo $this->getClassName() ?>OrCreate();
65
66     if ($this->getRequest()->getMethod() == sfRequest::POST)
67     {
68       $this->update<?php echo $this->getClassName() ?>FromRequest();
69
70       $this->save<?php echo $this->getClassName() ?>($this-><?php echo $this->getSingularName() ?>);
71
72       $this->setFlash('notice', 'Your modifications have been saved');
73
74       if ($this->getRequestParameter('save_and_add'))
75       {
76         return $this->redirect('<?php echo $this->getModuleName() ?>/create');
77       }
78       else if ($this->getRequestParameter('save_and_list'))
79       {
80         return $this->redirect('<?php echo $this->getModuleName() ?>/list');
81       }
82       else
83       {
84         return $this->redirect('<?php echo $this->getModuleName() ?>/edit?<?php echo $this->getPrimaryKeyUrlParams('this->') ?>);
85       }
86     }
87     else
88     {
89       $this->labels = $this->getLabels();
90     }
91   }
92
93   public function executeDelete()
94   {
95     $this-><?php echo $this->getSingularName() ?> = <?php echo $this->getClassName() ?>Peer::retrieveByPk(<?php echo $this->getRetrieveByPkParamsForAction(40) ?>);
96     $this->forward404Unless($this-><?php echo $this->getSingularName() ?>);
97
98     try
99     {
100       $this->delete<?php echo $this->getClassName() ?>($this-><?php echo $this->getSingularName() ?>);
101     }
102     catch (PropelException $e)
103     {
104       $this->getRequest()->setError('delete', 'Could not delete the selected <?php echo sfInflector::humanize($this->getSingularName()) ?>. Make sure it does not have any associated items.');
105       return $this->forward('<?php echo $this->getModuleName() ?>', 'list');
106     }
107
108 <?php foreach ($this->getColumnCategories('edit.display') as $category): ?>
109 <?php foreach ($this->getColumns('edit.display', $category) as $name => $column): ?>
110 <?php $input_type = $this->getParameterValue('edit.fields.'.$column->getName().'.type') ?>
111 <?php if ($input_type == 'admin_input_file_tag'): ?>
112 <?php $upload_dir = $this->replaceConstants($this->getParameterValue('edit.fields.'.$column->getName().'.upload_dir')) ?>
113       $currentFile = sfConfig::get('sf_upload_dir')."/<?php echo $upload_dir ?>/".$this-><?php echo $this->getSingularName() ?>->get<?php echo $column->getPhpName() ?>();
114       if (is_file($currentFile))
115       {
116         unlink($currentFile);
117       }
118
119 <?php endif; ?>
120 <?php endforeach; ?>
121 <?php endforeach; ?>
122     return $this->redirect('<?php echo $this->getModuleName() ?>/list');
123   }
124
125   public function handleErrorEdit()
126   {
127     $this->preExecute();
128     $this-><?php echo $this->getSingularName() ?> = $this->get<?php echo $this->getClassName() ?>OrCreate();
129     $this->update<?php echo $this->getClassName() ?>FromRequest();
130
131     $this->labels = $this->getLabels();
132
133     return sfView::SUCCESS;
134   }
135
136   protected function save<?php echo $this->getClassName() ?>($<?php echo $this->getSingularName() ?>)
137   {
138     $<?php echo $this->getSingularName() ?>->save();
139
140 <?php foreach ($this->getColumnCategories('edit.display') as $category): ?>
141 <?php foreach ($this->getColumns('edit.display', $category) as $name => $column): $type = $column->getCreoleType(); ?>
142 <?php $name = $column->getName() ?>
143 <?php if ($column->isPrimaryKey()) continue ?>
144 <?php $credentials = $this->getParameterValue('edit.fields.'.$column->getName().'.credentials') ?>
145 <?php $input_type = $this->getParameterValue('edit.fields.'.$column->getName().'.type') ?>
146 <?php
147
148 $user_params = $this->getParameterValue('edit.fields.'.$column->getName().'.params');
149 $user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params);
150 $through_class = isset($user_params['through_class']) ? $user_params['through_class'] : '';
151
152 ?>
153 <?php if ($through_class): ?>
154 <?php
155
156 $class = $this->getClassName();
157 $related_class = sfPropelManyToMany::getRelatedClass($class, $through_class);
158 $related_table = constant($related_class.'Peer::TABLE_NAME');
159 $middle_table = constant($through_class.'Peer::TABLE_NAME');
160 $this_table = constant($class.'Peer::TABLE_NAME');
161
162 $related_column = sfPropelManyToMany::getRelatedColumn($class, $through_class);
163 $column = sfPropelManyToMany::getColumn($class, $through_class);
164
165 ?>
166 <?php if ($input_type == 'admin_double_list' || $input_type == 'admin_check_list' || $input_type == 'admin_select_list'): ?>
167 <?php if ($credentials): $credentials = str_replace("\n", ' ', var_export($credentials, true)) ?>
168     if ($this->getUser()->hasCredential(<?php echo $credentials ?>))
169     {
170 <?php endif; ?>
171       // Update many-to-many for "<?php echo $name ?>"
172       $c = new Criteria();
173       $c->add(<?php echo $through_class ?>Peer::<?php echo strtoupper($column->getColumnName()) ?>, $<?php echo $this->getSingularName() ?>->getPrimaryKey());
174       <?php echo $through_class ?>Peer::doDelete($c);
175
176       $ids = $this->getRequestParameter('associated_<?php echo $name ?>');
177       if (is_array($ids))
178       {
179         foreach ($ids as $id)
180         {
181           $<?php echo ucfirst($through_class) ?> = new <?php echo $through_class ?>();
182           $<?php echo ucfirst($through_class) ?>->set<?php echo $column->getPhpName() ?>($<?php echo $this->getSingularName() ?>->getPrimaryKey());
183           $<?php echo ucfirst($through_class) ?>->set<?php echo $related_column->getPhpName() ?>($id);
184           $<?php echo ucfirst($through_class) ?>->save();
185         }
186       }
187
188 <?php if ($credentials): ?>
189     }
190 <?php endif; ?>
191 <?php endif; ?>
192 <?php endif; ?>
193 <?php endforeach; ?>
194 <?php endforeach; ?>
195   }
196
197   protected function delete<?php echo $this->getClassName() ?>($<?php echo $this->getSingularName() ?>)
198   {
199     $<?php echo $this->getSingularName() ?>->delete();
200   }
201
202   protected function update<?php echo $this->getClassName() ?>FromRequest()
203   {
204     $<?php echo $this->getSingularName() ?> = $this->getRequestParameter('<?php echo $this->getSingularName() ?>');
205
206 <?php foreach ($this->getColumnCategories('edit.display') as $category): ?>
207 <?php foreach ($this->getColumns('edit.display', $category) as $name => $column): $type = $column->getCreoleType(); ?>
208 <?php $name = $column->getName() ?>
209 <?php if ($column->isPrimaryKey()) continue ?>
210 <?php $credentials = $this->getParameterValue('edit.fields.'.$column->getName().'.credentials') ?>
211 <?php $input_type = $this->getParameterValue('edit.fields.'.$column->getName().'.type') ?>
212 <?php if ($credentials): $credentials = str_replace("\n", ' ', var_export($credentials, true)) ?>
213     if ($this->getUser()->hasCredential(<?php echo $credentials ?>))
214     {
215 <?php endif; ?>
216 <?php if ($input_type == 'admin_input_file_tag'): ?>
217 <?php $upload_dir = $this->replaceConstants($this->getParameterValue('edit.fields.'.$column->getName().'.upload_dir')) ?>
218     $currentFile = sfConfig::get('sf_upload_dir')."/<?php echo $upload_dir ?>/".$this-><?php echo $this->getSingularName() ?>->get<?php echo $column->getPhpName() ?>();
219     if (!$this->getRequest()->hasErrors() && isset($<?php echo $this->getSingularName() ?>['<?php echo $name ?>_remove']))
220     {
221       $this-><?php echo $this->getSingularName() ?>->set<?php echo $column->getPhpName() ?>('');
222       if (is_file($currentFile))
223       {
224         unlink($currentFile);
225       }
226     }
227
228     if (!$this->getRequest()->hasErrors() && $this->getRequest()->getFileSize('<?php echo $this->getSingularName() ?>[<?php echo $name ?>]'))
229     {
230 <?php elseif ($type != CreoleTypes::BOOLEAN): ?>
231     if (isset($<?php echo $this->getSingularName() ?>['<?php echo $name ?>']))
232     {
233 <?php endif; ?>
234 <?php if ($input_type == 'admin_input_file_tag'): ?>
235 <?php if ($this->getParameterValue('edit.fields.'.$column->getName().'.filename')): ?>
236       $fileName = "<?php echo str_replace('"', '\\"', $this->replaceConstants($this->getParameterValue('edit.fields.'.$column->getName().'.filename'))) ?>";
237 <?php else: ?>
238       $fileName = md5($this->getRequest()->getFileName('<?php echo $this->getSingularName() ?>[<?php echo $name ?>]').time().rand(0, 99999));
239 <?php endif; ?>
240       $ext = $this->getRequest()->getFileExtension('<?php echo $this->getSingularName() ?>[<?php echo $name ?>]');
241       if (is_file($currentFile))
242       {
243         unlink($currentFile);
244       }
245       $this->getRequest()->moveFile('<?php echo $this->getSingularName() ?>[<?php echo $name ?>]', sfConfig::get('sf_upload_dir')."/<?php echo $upload_dir ?>/".$fileName.$ext);
246       $this-><?php echo $this->getSingularName() ?>->set<?php echo $column->getPhpName() ?>($fileName.$ext);
247 <?php elseif ($type == CreoleTypes::DATE || $type == CreoleTypes::TIMESTAMP): ?>
248       if ($<?php echo $this->getSingularName() ?>['<?php echo $name ?>'])
249       {
250         try
251         {
252           $dateFormat = new sfDateFormat($this->getUser()->getCulture());
253           <?php $inputPattern  = $type == CreoleTypes::DATE ? 'd' : 'g'; ?>
254           <?php $outputPattern = $type == CreoleTypes::DATE ? 'i' : 'I'; ?>
255           if (!is_array($<?php echo $this->getSingularName() ?>['<?php echo $name ?>']))
256           {
257             $value = $dateFormat->format($<?php echo $this->getSingularName() ?>['<?php echo $name ?>'], '<?php echo $outputPattern ?>', $dateFormat->getInputPattern('<?php echo $inputPattern ?>'));
258           }
259           else
260           {
261             $value_array = $<?php echo $this->getSingularName() ?>['<?php echo $name ?>'];
262             $value = $value_array['year'].'-'.$value_array['month'].'-'.$value_array['day'].(isset($value_array['hour']) ? ' '.$value_array['hour'].':'.$value_array['minute'].(isset($value_array['second']) ? ':'.$value_array['second'] : '') : '');
263           }
264           $this-><?php echo $this->getSingularName() ?>->set<?php echo $column->getPhpName() ?>($value);
265         }
266         catch (sfException $e)
267         {
268           // not a date
269         }
270       }
271       else
272       {
273         $this-><?php echo $this->getSingularName() ?>->set<?php echo $column->getPhpName() ?>(null);
274       }
275 <?php elseif ($type == CreoleTypes::BOOLEAN): ?>
276     $this-><?php echo $this->getSingularName() ?>->set<?php echo $column->getPhpName() ?>(isset($<?php echo $this->getSingularName() ?>['<?php echo $name ?>']) ? $<?php echo $this->getSingularName() ?>['<?php echo $name ?>'] : 0);
277 <?php elseif ($column->isForeignKey()): ?>
278     $this-><?php echo $this->getSingularName() ?>->set<?php echo $column->getPhpName() ?>($<?php echo $this->getSingularName() ?>['<?php echo $name ?>'] ? $<?php echo $this->getSingularName() ?>['<?php echo $name ?>'] : null);
279 <?php else: ?>
280       $this-><?php echo $this->getSingularName() ?>->set<?php echo $column->getPhpName() ?>($<?php echo $this->getSingularName() ?>['<?php echo $name ?>']);
281 <?php endif; ?>
282 <?php if ($type != CreoleTypes::BOOLEAN): ?>
283     }
284 <?php endif; ?>
285 <?php if ($credentials): ?>
286       }
287 <?php endif; ?>
288 <?php endforeach; ?>
289 <?php endforeach; ?>
290   }
291
292   protected function get<?php echo $this->getClassName() ?>OrCreate(<?php echo $this->getMethodParamsForGetOrCreate() ?>)
293   {
294     if (<?php echo $this->getTestPksForGetOrCreate() ?>)
295     {
296       $<?php echo $this->getSingularName() ?> = new <?php echo $this->getClassName() ?>();
297     }
298     else
299     {
300       $<?php echo $this->getSingularName() ?> = <?php echo $this->getClassName() ?>Peer::retrieveByPk(<?php echo $this->getRetrieveByPkParamsForGetOrCreate() ?>);
301
302       $this->forward404Unless($<?php echo $this->getSingularName() ?>);
303     }
304
305     return $<?php echo $this->getSingularName() ?>;
306   }
307
308   protected function processFilters()
309   {
310 <?php if ($this->getParameterValue('list.filters')): ?>
311     if ($this->getRequest()->hasParameter('filter'))
312     {
313       $filters = $this->getRequestParameter('filters');
314 <?php foreach ($this->getColumns('list.filters') as $column): $type = $column->getCreoleType() ?>
315 <?php if ($type == CreoleTypes::DATE || $type == CreoleTypes::TIMESTAMP): ?>
316       if (isset($filters['<?php echo $column->getName() ?>']['from']) && $filters['<?php echo $column->getName() ?>']['from'] !== '')
317       {
318         $filters['<?php echo $column->getName() ?>']['from'] = sfI18N::getTimestampForCulture($filters['<?php echo $column->getName() ?>']['from'], $this->getUser()->getCulture());
319       }
320       if (isset($filters['<?php echo $column->getName() ?>']['to']) && $filters['<?php echo $column->getName() ?>']['to'] !== '')
321       {
322         $filters['<?php echo $column->getName() ?>']['to'] = sfI18N::getTimestampForCulture($filters['<?php echo $column->getName() ?>']['to'], $this->getUser()->getCulture());
323       }
324 <?php endif; ?>
325 <?php endforeach; ?>
326
327       $this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/<?php echo $this->getSingularName() ?>/filters');
328       $this->getUser()->getAttributeHolder()->add($filters, 'sf_admin/<?php echo $this->getSingularName() ?>/filters');
329     }
330 <?php endif; ?>
331   }
332
333   protected function processSort()
334   {
335     if ($this->getRequestParameter('sort'))
336     {
337       $this->getUser()->setAttribute('sort', $this->getRequestParameter('sort'), 'sf_admin/<?php echo $this->getSingularName() ?>/sort');
338       $this->getUser()->setAttribute('type', $this->getRequestParameter('type', 'asc'), 'sf_admin/<?php echo $this->getSingularName() ?>/sort');
339     }
340
341     if (!$this->getUser()->getAttribute('sort', null, 'sf_admin/<?php echo $this->getSingularName() ?>/sort'))
342     {
343 <?php if ($sort = $this->getParameterValue('list.sort')): ?>
344 <?php if (is_array($sort)): ?>
345       $this->getUser()->setAttribute('sort', '<?php echo $sort[0] ?>', 'sf_admin/<?php echo $this->getSingularName() ?>/sort');
346       $this->getUser()->setAttribute('type', '<?php echo $sort[1] ?>', 'sf_admin/<?php echo $this->getSingularName() ?>/sort');
347 <?php else: ?>
348       $this->getUser()->setAttribute('sort', '<?php echo $sort ?>', 'sf_admin/<?php echo $this->getSingularName() ?>/sort');
349       $this->getUser()->setAttribute('type', 'asc', 'sf_admin/<?php echo $this->getSingularName() ?>/sort');
350 <?php endif; ?>
351 <?php endif; ?>
352     }
353   }
354
355   protected function addFiltersCriteria($c)
356   {
357 <?php if ($this->getParameterValue('list.filters')): ?>
358 <?php foreach ($this->getColumns('list.filters') as $column): $type = $column->getCreoleType() ?>
359 <?php if (($column->isPartial() || $column->isComponent()) && $this->getParameterValue('list.fields.'.$column->getName().'.filter_criteria_disabled')) continue ?>
360     if (isset($this->filters['<?php echo $column->getName() ?>_is_empty']))
361     {
362       $criterion = $c->getNewCriterion(<?php echo $this->getPeerClassName() ?>::<?php echo strtoupper($column->getName()) ?>, '');
363       $criterion->addOr($c->getNewCriterion(<?php echo $this->getPeerClassName() ?>::<?php echo strtoupper($column->getName()) ?>, null, Criteria::ISNULL));
364       $c->add($criterion);
365     }
366 <?php if ($type == CreoleTypes::DATE || $type == CreoleTypes::TIMESTAMP): ?>
367     else if (isset($this->filters['<?php echo $column->getName() ?>']))
368     {
369       if (isset($this->filters['<?php echo $column->getName() ?>']['from']) && $this->filters['<?php echo $column->getName() ?>']['from'] !== '')
370       {
371 <?php if ($type == CreoleTypes::DATE): ?>
372         $criterion = $c->getNewCriterion(<?php echo $this->getPeerClassName() ?>::<?php echo strtoupper($column->getName()) ?>, date('Y-m-d', $this->filters['<?php echo $column->getName() ?>']['from']), Criteria::GREATER_EQUAL);
373 <?php else: ?>
374         $criterion = $c->getNewCriterion(<?php echo $this->getPeerClassName() ?>::<?php echo strtoupper($column->getName()) ?>, $this->filters['<?php echo $column->getName() ?>']['from'], Criteria::GREATER_EQUAL);
375 <?php endif; ?>
376       }
377       if (isset($this->filters['<?php echo $column->getName() ?>']['to']) && $this->filters['<?php echo $column->getName() ?>']['to'] !== '')
378       {
379         if (isset($criterion))
380         {
381 <?php if ($type == CreoleTypes::DATE): ?>
382           $criterion->addAnd($c->getNewCriterion(<?php echo $this->getPeerClassName() ?>::<?php echo strtoupper($column->getName()) ?>, date('Y-m-d', $this->filters['<?php echo $column->getName() ?>']['to']), Criteria::LESS_EQUAL));
383 <?php else: ?>
384           $criterion->addAnd($c->getNewCriterion(<?php echo $this->getPeerClassName() ?>::<?php echo strtoupper($column->getName()) ?>, $this->filters['<?php echo $column->getName() ?>']['to'], Criteria::LESS_EQUAL));
385 <?php endif; ?>
386         }
387         else
388         {
389 <?php if ($type == CreoleTypes::DATE): ?>
390           $criterion = $c->getNewCriterion(<?php echo $this->getPeerClassName() ?>::<?php echo strtoupper($column->getName()) ?>, date('Y-m-d', $this->filters['<?php echo $column->getName() ?>']['to']), Criteria::LESS_EQUAL);
391 <?php else: ?>
392           $criterion = $c->getNewCriterion(<?php echo $this->getPeerClassName() ?>::<?php echo strtoupper($column->getName()) ?>, $this->filters['<?php echo $column->getName() ?>']['to'], Criteria::LESS_EQUAL);
393 <?php endif; ?>
394         }
395       }
396
397       if (isset($criterion))
398       {
399         $c->add($criterion);
400       }
401     }
402 <?php else: ?>
403     else if (isset($this->filters['<?php echo $column->getName() ?>']) && $this->filters['<?php echo $column->getName() ?>'] !== '')
404     {
405 <?php if ($type == CreoleTypes::CHAR || $type == CreoleTypes::VARCHAR || $type == CreoleTypes::LONGVARCHAR): ?>
406       $c->add(<?php echo $this->getPeerClassName() ?>::<?php echo strtoupper($column->getName()) ?>, strtr($this->filters['<?php echo $column->getName() ?>'], '*', '%'), Criteria::LIKE);
407 <?php else: ?>
408       $c->add(<?php echo $this->getPeerClassName() ?>::<?php echo strtoupper($column->getName()) ?>, $this->filters['<?php echo $column->getName() ?>']);
409 <?php endif; ?>
410     }
411 <?php endif; ?>
412 <?php endforeach; ?>
413 <?php endif; ?>
414   }
415
416   protected function addSortCriteria($c)
417   {
418     if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/<?php echo $this->getSingularName() ?>/sort'))
419     {
420       switch ($sort_column)
421       {
422 <?php if ($fields = $this->getParameterValue('list.fields')): ?>
423 <?php foreach ($fields as $key => $field): ?>
424 <?php if ($this->getParameterValue('list.fields.'.$key.'.sort_column')): ?>
425         case '<?=$key?>':
426           <?php $column = $this->getParameterValue('list.fields.'.$key.'.sort_column');?>
427 <?php if ( is_array($column) ) : ?> 
428           $sort_columns = Array(<?php
429             foreach ($column as $c)
430             {
431               echo $c;
432               if (next($column)) echo ", ";
433             } ?>);
434 <?php else: ?> 
435           $sort_columns = Array(<?= $column ?>);
436 <?php endif; ?>
437           break;
438 <?php endif; ?>
439 <?php endforeach; ?>
440 <?php endif; ?>
441         default:
442           $sort_columns = Array(<?php echo $this->getClassName() ?>Peer::translateFieldName($sort_column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME));
443           break;
444       }
445      
446       foreach ($sort_columns as $sort_column) {
447         if ($this->getUser()->getAttribute('type', null, 'sf_admin/<?php echo $this->getSingularName() ?>/sort') == 'asc')
448         {
449           $c->addAscendingOrderByColumn($sort_column);
450         }
451         else
452         {
453           $c->addDescendingOrderByColumn($sort_column);
454         }
455       }
456     }
457   }
458
459   protected function getLabels()
460   {
461