HosipLan

Untitled

Oct 4th, 2011
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. class DeleteItemsProcess extends Nette\Object
  2. {
  3.     private $model;
  4.  
  5.     public function __construct(Nette\Http\User $user, ItemsModel $model)
  6.     {
  7.         if(!$user->isInRole('administrator')) {
  8.             throw new Nette\InvalidStateException("User is not allowed to delete items");
  9.         }
  10.  
  11.         $this->model = $model;
  12.     }
  13.  
  14.     public function run($itemId)
  15.     {
  16.         $item = $this->model->getItem($itemId);
  17.         $item->delete();
  18.  
  19.         $contents = $this->model->findAllContents(NULL, array('item_id' => $itemId));
  20.         foreach ($contents as $content){
  21.             $content->delete();
  22.         }
  23.     }
  24. }
  25.  
  26.  
  27. // signal
  28.  
  29.  
  30. public function handleDelete($id)
  31. {
  32.     try {
  33.         $process = new DeleteItemsProcess($this->getUser(), $this->model);
  34.         $process->run($id);
  35.         $this->flashMessage('Item has been deleted');
  36.  
  37.     } catch (Nette\InvalidStateException $e) { }
  38.  
  39.     $this->redirect('this');
  40. }
  41.  
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment