-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a92f197
Showing
8 changed files
with
397 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
Oops, something went wrong.