Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
frostealth committed Jun 2, 2015
0 parents commit a92f197
Show file tree
Hide file tree
Showing 8 changed files with 397 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.DS_Store
thumbs.db
*.class
*.pyc
*.pyo
*.swp
.project
.settings
.idea
composer.phar
vendor/*
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "strong-squirrel/yii2-actions",
"type": "yii2-extension",
"license": "MIT",
"authors": [
{
"name": "Ivan Kudinov",
"email": "[email protected]",
"homepage": "http://frostealth.ru"
}
],
"minimum-stability": "stable",
"require": {
"php": ">=5.4",
"yiisoft/yii2": "2.0.*"
},
"autoload": {
"psr-4": {
"strong_squirrel\\actions\\": "src/"
}
}
}
83 changes: 83 additions & 0 deletions src/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace strong_squirrel\actions;

use yii\base\InvalidConfigException;
use yii\db\ActiveRecordInterface;
use yii\web\NotFoundHttpException;

/**
* Class Action
*
* @package strong_squirrel\actions
*/
abstract class Action extends \yii\base\Action
{
/**
* @var string class name of the model which will be handled by this action.
* The model class must implement [[ActiveRecordInterface]].
* This property must be set.
*/
public $modelClass;

/**
* @var callable
* The signature of the callable should be:
*
* ```php
* function ($id, $action) {
* // $id is the primary key value.
* // $action is the action object currently running
* }
* ```
*
* The callable should return the model found, or throw an exception if not found.
*/
public $findModel;

/**
* @var callable
* The signature of the callable should be as follows,
*
* ```php
* function ($action, $model = null) {
* // $model is the requested model instance.
* // If null, it means no specific model (e.g. IndexAction)
* }
* ```
*/
public $checkAccess;

/**
* @inheritdoc
*/
public function init()
{
if ($this->modelClass === null) {
throw new InvalidConfigException(get_class($this) . '::$modelClass must be set.');
}
}

/**
* @param string $id
*
* @return ActiveRecordInterface
* @throws NotFoundHttpException
*/
public function findModel($id)
{
if ($this->findModel !== null) {
$model = call_user_func($this->findModel, $id, $this);
} else {
/** @var ActiveRecordInterface $modelClass */
$modelClass = $this->modelClass;
$model = $modelClass::findOne($id);
}

if (empty($model)) {
throw new NotFoundHttpException('Object not found.');
}

return $model;
}
}
64 changes: 64 additions & 0 deletions src/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace strong_squirrel\actions;

use yii\db\ActiveRecord;

/**
* Class Create
*
* @package strong_squirrel\actions
*/
class Create extends Action
{
/**
* @var string the scenario to be assigned to the new model before it is validated and saved.
*/
public $scenario = ActiveRecord::SCENARIO_DEFAULT;

/**
* @var string the name of the view action.
*/
public $view = 'create';

/**
* @var callable
* The signature of the callable should be:
*
* ```php
* function ($model) {
* // $model is the requested model instance.
* return $this->redirect(['my-action', 'id' => $model->getPrimaryKey()]);
* }
* ```
*/
public $afterSave;

/**
* @return mixed
*/
public function run()
{
if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id);
}

/** @var ActiveRecord $model */
$model = new $this->modelClass([
'scenario' => $this->scenario,
]);

if ($model->load(\Yii::$app->getRequest()->post()) && $model->save()) {
$afterSave = $this->afterSave;
if (empty($afterSave)) {
$afterSave = function (ActiveRecord $model) {
return $this->controller->redirect(['view', 'id' => $model->getPrimaryKey()]);
};
}

return call_user_func($afterSave, $model);
}

return $this->controller->render($this->view, ['model' => $model]);
}
}
55 changes: 55 additions & 0 deletions src/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace strong_squirrel\actions;

use yii\web\ServerErrorHttpException;

/**
* Class Delete
*
* @package strong_squirrel\actions
*/
class Delete extends Action
{
/**
* @var callable
* The signature of the callable should be:
*
* ```php
* function ($model) {
* // $model is the requested model instance.
* return $this->redirect(['my-action');
* }
* ```
*/
public $afterDelete;

/**
* @param string $id
*
* @return mixed
* @throws ServerErrorHttpException
* @throws \yii\web\NotFoundHttpException
*/
public function run($id)
{
$model = $this->findModel($id);

if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id, $model);
}

if ($model->delete() === false) {
throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');
}

$afterDelete = $this->afterDelete;
if (empty($afterDelete)) {
$afterDelete = function () {
return $this->controller->redirect(['index']);
};
}

return call_user_func($afterDelete, $model);
}
}
64 changes: 64 additions & 0 deletions src/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace strong_squirrel\actions;

use yii\data\ActiveDataProvider;

/**
* Class Index
*
* @package strong_squirrel\actions
*/
class Index extends Action
{
/**
* @var string the name of the view action.
*/
public $view = 'index';

/**
* @var callable a PHP callable that will be called to prepare a data provider that
* should return a collection of the models. If not set, [[prepareDataProvider()]] will be used instead.
* The signature of the callable should be:
*
* ```php
* function ($action) {
* // $action is the action object currently running
* }
* ```
*
* The callable should return an instance of [[ActiveDataProvider]].
*/
public $prepareDataProvider;

/**
* @return string
*/
public function run()
{
if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id);
}

return $this->controller->render($this->view, [
'dataProvider' => $this->prepareDataProvider(),
]);
}

/**
* @return ActiveDataProvider
*/
protected function prepareDataProvider()
{
if ($this->prepareDataProvider !== null) {
return call_user_func($this->prepareDataProvider, $this);
}

/** @var \yii\db\ActiveRecord $modelClass */
$modelClass = $this->modelClass;

return new ActiveDataProvider([
'query' => $modelClass::find(),
]);
}
}
65 changes: 65 additions & 0 deletions src/Update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace strong_squirrel\actions;

use yii\db\ActiveRecord;

/**
* Class Update
*
* @package strong_squirrel\actions
*/
class Update extends Action
{
/**
* @var string the scenario to be assigned to the new model before it is validated and saved.
*/
public $scenario = ActiveRecord::SCENARIO_DEFAULT;

/**
* @var string the name of the view action.
*/
public $view = 'update';

/**
* @var callable
* The signature of the callable should be:
*
* ```php
* function ($model) {
* // $model is the requested model instance.
* return $this->redirect(['my-action', 'id' => $model->getPrimaryKey()]);
* }
* ```
*/
public $afterUpdate;

/**
* @param string $id
*
* @return mixed
* @throws \yii\web\NotFoundHttpException
*/
public function run($id)
{
/** @var ActiveRecord $model */
$model = $this->findModel($id);

if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id, $model);
}

if ($model->load(\Yii::$app->getRequest()->post()) && $model->save()) {
$afterUpdate = $this->afterUpdate;
if (empty($afterUpdate)) {
$afterUpdate = function (ActiveRecord $model) {
return $this->controller->redirect(['view', 'id' => $model->getPrimaryKey()]);
};
}

return call_user_func($afterUpdate, $model);
}

return $this->controller->render($this->view, ['model' => $model]);
}
}
Loading

0 comments on commit a92f197

Please sign in to comment.