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