Skip to content

Commit

Permalink
feat(actions): Add DeleteAction
Browse files Browse the repository at this point in the history
  • Loading branch information
Adegoke Obasa committed Jun 13, 2017
1 parent 7c34815 commit 1163ed6
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Action/DeleteAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @author Adegoke Obasa <goke@cottacush.com>
*/

namespace CottaCush\Yii2\Action;

use CottaCush\Yii2\Controller\BaseController;
use yii\base\Action;

class DeleteAction extends Action
{
public $returnUrl = '';
public $successMessage = '';
public $model;

const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 0;

/**
* @author Akinwunmi Taiwo <taiwo@cottacush.com>
* @return \yii\web\Response
*/
public function run()
{
/** @var BaseController $controller */
$controller = $this->controller;

$referrerUrl = $controller->getRequest()->referrer;
$controller->isPostCheck($referrerUrl);
$modelToDelete = $this->model;

if (!$modelToDelete) {
$controller->flashError('Record not found');
} else {
$modelToDelete->is_active = self::STATUS_INACTIVE;
if (!$modelToDelete->update()) {
$controller->flashError($modelToDelete->getErrors());
} else {
$controller->flashSuccess($this->successMessage);
}
}

return $controller->redirect($this->returnUrl);
}
}

0 comments on commit 1163ed6

Please sign in to comment.