DeleteAction.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-08-13 01:08
  7. */
  8. namespace backend\actions;
  9. use Yii;
  10. use stdClass;
  11. use Closure;
  12. use backend\actions\helpers\Helper;
  13. use yii\base\Exception;
  14. use yii\web\BadRequestHttpException;
  15. use yii\web\MethodNotAllowedHttpException;
  16. use yii\web\Response;
  17. use yii\web\UnprocessableEntityHttpException;
  18. /**
  19. * backend delete
  20. * only permit POST request, but can assign value throw query or body for need delete record.
  21. *
  22. * Class DeleteAction
  23. * @package backend\actions
  24. */
  25. class DeleteAction extends \yii\base\Action
  26. {
  27. /**
  28. * @var string|array primary key(s) name
  29. */
  30. public $primaryKeyIdentity = "id";
  31. /**
  32. * @var Closure the real delete logic, usually will call service layer delete method
  33. */
  34. public $doDelete;
  35. /**
  36. * delete
  37. *
  38. * @throws BadRequestHttpException
  39. * @throws MethodNotAllowedHttpException
  40. * @throws UnprocessableEntityHttpException
  41. * @throws Exception
  42. */
  43. public function run()
  44. {
  45. if (Yii::$app->getRequest()->getIsPost()) {//for safety, delete need POST
  46. if( !is_string($this->primaryKeyIdentity) ){
  47. throw new Exception(__CLASS__ . "::primaryKeyIdentity only permit string");
  48. }
  49. $data = Yii::$app->getRequest()->post($this->primaryKeyIdentity, null);
  50. if ($data === null) {//不在post参数,则为单个删除
  51. $data = Yii::$app->getRequest()->get($this->primaryKeyIdentity, null);
  52. }
  53. if (!$data) {
  54. throw new BadRequestHttpException(Yii::t('app', "{$this->primaryKeyIdentity} doesn't exist"));
  55. }
  56. if( is_string($data) ){
  57. if( (strpos($data, "{") === 0 && strpos(strrev($data), "}") === 0) || (strpos($data, "[") === 0 && strpos(strrev($data), "]") === 0) ){
  58. $data = json_decode($data, true);
  59. }else{
  60. $data = [$data];
  61. }
  62. }
  63. !isset($data[0]) && $data = [$data];
  64. $errors = [];
  65. foreach ($data as $id){
  66. $deleteResult = call_user_func_array($this->doDelete, [$id, $this]);
  67. if($deleteResult !== true && $deleteResult !== "" && $deleteResult !== null){
  68. $errors[]= Helper::getErrorString($deleteResult);
  69. }
  70. }
  71. if (count($errors) == 0) {
  72. if( Yii::$app->getRequest()->getIsAjax() ) {
  73. Yii::$app->getResponse()->format = Response::FORMAT_JSON;
  74. return ['code'=>0, 'msg'=>'success', 'data'=>new stdClass()];
  75. }else {
  76. return $this->controller->redirect(Yii::$app->getRequest()->getReferrer());
  77. }
  78. } else {
  79. if( Yii::$app->getRequest()->getIsAjax() ){
  80. Yii::$app->getResponse()->format = Response::FORMAT_JSON;
  81. throw new UnprocessableEntityHttpException(implode("<br>", $errors));
  82. }else {
  83. Yii::$app->getSession()->setFlash('error', implode("<br>", $errors));
  84. }
  85. }
  86. } else {
  87. throw new MethodNotAllowedHttpException(Yii::t('app', "Delete must be POST http method"));
  88. }
  89. }
  90. }