1<?php 2 3App::uses('CrudAction', 'Crud.Controller/Crud'); 4 5/** 6 * Handles 'Delete' Crud actions 7 * 8 * Licensed under The MIT License 9 * For full copyright and license information, please see the LICENSE.txt 10 */ 11class DeleteCrudAction extends CrudAction { 12 13/** 14 * Default settings for 'add' actions 15 * 16 * `enabled` Is this crud action enabled or disabled 17 * 18 * `findMethod` The default `Model::find()` method for reading data 19 * 20 * @var array 21 */ 22 protected $_settings = array( 23 'enabled' => true, 24 'findMethod' => 'count', 25 'messages' => array( 26 'success' => array( 27 'text' => 'Successfully deleted {name}' 28 ), 29 'error' => array( 30 'text' => 'Could not delete {name}' 31 ) 32 ), 33 'api' => array( 34 'success' => array( 35 'code' => 200 36 ), 37 'error' => array( 38 'code' => 400 39 ) 40 ) 41 ); 42 43/** 44 * Constant representing the scope of this action 45 * 46 * @var integer 47 */ 48 const ACTION_SCOPE = CrudAction::SCOPE_RECORD; 49 50/** 51 * HTTP DELETE handler 52 * 53 * @throws NotFoundException If record not found 54 * @param string $id 55 * @return void 56 */ 57 protected function _delete($id = null) { 58 if (!$this->_validateId($id)) { 59 return false; 60 } 61 62 $request = $this->_request(); 63 $model = $this->_model(); 64 65 $query = array(); 66 $query['conditions'] = array($model->escapeField() => $id); 67 68 $findMethod = $this->_getFindMethod('count'); 69 $subject = $this->_trigger('beforeFind', compact('id', 'query', 'findMethod')); 70 $query = $subject->query; 71 72 $count = $model->find($subject->findMethod, $query); 73 if (empty($count)) { 74 $this->_trigger('recordNotFound', compact('id')); 75 76 $message = $this->message('recordNotFound', array('id' => $id)); 77 $exceptionClass = $message['class']; 78 throw new $exceptionClass($message['text'], $message['code']); 79 } 80 81 $subject = $this->_trigger('beforeDelete', compact('id')); 82 if ($subject->stopped) { 83 $this->setFlash('error'); 84 return $this->_redirect($subject, array('action' => 'index')); 85 } 86 87 if ($model->delete($id)) { 88 $this->setFlash('success'); 89 $subject = $this->_trigger('afterDelete', array('id' => $id, 'success' => true)); 90 } else { 91 $this->setFlash('error'); 92 $subject = $this->_trigger('afterDelete', array('id' => $id, 'success' => false)); 93 } 94 95 $this->_redirect($subject, array('action' => 'index')); 96 } 97 98/** 99 * HTTP POST handler 100 * 101 * @param mixed $id 102 * @return void 103 */ 104 protected function _post($id = null) { 105 return $this->_delete($id); 106 } 107 108} 109