Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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 sfValidatorPropelChoice(array('multiple' => true, '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
    $count = 0;
51
    foreach (<?php echo constant($this->getModelClass().'::PEER') ?>::retrieveByPks($ids) as $object)
52
    {
53
      $this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $object)));
54
 
55
      $object->delete();
56
      if ($object->isDeleted())
57
      {
58
        $count++;
59
      }
60
    }
61
 
62
    if ($count >= count($ids))
63
    {
64
      $this->getUser()->setFlash('notice', 'The selected items have been deleted successfully.');
65
    }
66
    else
67
    {
68
      $this->getUser()->setFlash('error', 'A problem occurs when deleting the selected items.');
69
    }
70
 
71
    $this->redirect('@<?php echo $this->getUrlForAction('list') ?>');
72
  }