| 1 |
public function executeBatch(sfWebRequest $request) |
|---|
| 2 |
{ |
|---|
| 3 |
$request->checkCSRFProtection(); |
|---|
| 4 |
|
|---|
| 5 |
if (!$ids = $request->getParameter('ids')) |
|---|
| 6 |
{ |
|---|
| 7 |
$this->getUser()->setFlash('error', 'You must at least select one item.'); |
|---|
| 8 |
|
|---|
| 9 |
$this->redirect('@<?php echo $this->getUrlForAction('list') ?>'); |
|---|
| 10 |
} |
|---|
| 11 |
|
|---|
| 12 |
if (!$action = $request->getParameter('batch_action')) |
|---|
| 13 |
{ |
|---|
| 14 |
$this->getUser()->setFlash('error', 'You must select an action to execute on the selected items.'); |
|---|
| 15 |
|
|---|
| 16 |
$this->redirect('@<?php echo $this->getUrlForAction('list') ?>'); |
|---|
| 17 |
} |
|---|
| 18 |
|
|---|
| 19 |
if (!method_exists($this, $method = 'execute'.ucfirst($action))) |
|---|
| 20 |
{ |
|---|
| 21 |
throw new InvalidArgumentException(sprintf('You must create a "%s" method for action "%s"', $method, $action)); |
|---|
| 22 |
} |
|---|
| 23 |
|
|---|
| 24 |
if (!$this->getUser()->hasCredential($this->configuration->getCredentials($action))) |
|---|
| 25 |
{ |
|---|
| 26 |
$this->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action')); |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
$validator = new sfValidatorDoctrineChoiceMany(array('model' => '<?php echo $this->getModelClass() ?>')); |
|---|
| 30 |
try |
|---|
| 31 |
{ |
|---|
| 32 |
// validate ids |
|---|
| 33 |
$ids = $validator->clean($ids); |
|---|
| 34 |
|
|---|
| 35 |
// execute batch |
|---|
| 36 |
$this->$method($request); |
|---|
| 37 |
} |
|---|
| 38 |
catch (sfValidatorError $e) |
|---|
| 39 |
{ |
|---|
| 40 |
$this->getUser()->setFlash('error', 'A problem occurs when deleting the selected items as some items do not exist anymore.'); |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
$this->redirect('@<?php echo $this->getUrlForAction('list') ?>'); |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
protected function executeBatchDelete(sfWebRequest $request) |
|---|
| 47 |
{ |
|---|
| 48 |
$ids = $request->getParameter('ids'); |
|---|
| 49 |
|
|---|
| 50 |
$records = Doctrine_Query::create() |
|---|
| 51 |
->from('<?php echo $this->getModelClass() ?>') |
|---|
| 52 |
->whereIn('<?php echo $this->getPrimaryKeys(true) ?>', $ids) |
|---|
| 53 |
->execute(); |
|---|
| 54 |
|
|---|
| 55 |
foreach ($records as $record) |
|---|
| 56 |
{ |
|---|
| 57 |
$record->delete(); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
$this->getUser()->setFlash('notice', 'The selected items have been deleted successfully.'); |
|---|
| 61 |
$this->redirect('@<?php echo $this->getUrlForAction('list') ?>'); |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|