Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
noumo committed Nov 20, 2015
1 parent d314307 commit a2b816a
Show file tree
Hide file tree
Showing 26 changed files with 436 additions and 701 deletions.
17 changes: 14 additions & 3 deletions actions/SortAction.php → actions/BaseSortAction.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php
namespace yii\easyii\actions;

class SortAction extends \yii\base\Action
class BaseSortAction extends \yii\base\Action
{
public $model;
public $attribute;
public $direction;
public $addititonalEquality = [];

public function run($id)
{
$modelClass = $this->model;
$modelClass = $this->model ? $this->model : $this->controller->modelClass;
$attribute = $this->attribute;

if(!$this->direction){
Expand All @@ -24,7 +25,17 @@ public function run($id)
$eq = '<';
$orderDir = SORT_DESC;
}
$modelSwap = $modelClass::find()->where([$eq, $attribute, $model->{$attribute}])->orderBy([$attribute => $orderDir])->limit(1)->one();

$query = $modelClass::find()->orderBy([$attribute => $orderDir])->limit(1);

$where = [$eq, $attribute, $model->{$attribute}];
if (count($this->addititonalEquality)) {
$where = ['and', $where];
foreach ($this->addititonalEquality as $item) {
$where[] = [$item => $model->{$item}];
}
}
$modelSwap = $query->where($where)->one();

if(!empty($modelSwap)) {
$newValue = $modelSwap->{$attribute};
Expand Down
5 changes: 4 additions & 1 deletion actions/ChangeStatusAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class ChangeStatusAction extends \yii\base\Action

public function run($id)
{
$modelClass = $this->model;
$modelClass = $this->model ? $this->model : $this->controller->modelClass;
if($this->status === null){
$this->status = $this->id == 'off' ? 0 : 1;
}

if(($model = $modelClass::findOne($id))){
$model->status = $this->status;
Expand Down
2 changes: 1 addition & 1 deletion actions/ClearImageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ClearImageAction extends \yii\base\Action

public function run($id)
{
$modelClass = $this->model;
$modelClass = $this->model ? $this->model : $this->controller->modelClass;
$model = $modelClass::findOne($id);

if($model === null){
Expand Down
2 changes: 1 addition & 1 deletion actions/DeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DeleteAction extends \yii\base\Action

public function run($id)
{
$modelClass = $this->model;
$modelClass = $this->model ? $this->model : $this->controller->modelClass;
if(($model = $modelClass::findOne($id))){
$model->delete();
} else {
Expand Down
7 changes: 7 additions & 0 deletions actions/SortByDateAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace yii\easyii\actions;

class SortByDateAction extends BaseSortAction
{
public $attribute = 'time';
}
7 changes: 7 additions & 0 deletions actions/SortByNumAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace yii\easyii\actions;

class SortByNumAction extends BaseSortAction
{
public $attribute = 'order_num';
}
136 changes: 53 additions & 83 deletions components/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@
*/
class CategoryController extends Controller
{
/** @var string */
public $categoryClass;

/** @var string */
public $moduleName;

/** @var string */
/** @var string */
public $viewRoute = '/items';

public function actions()
Expand All @@ -33,12 +30,10 @@ public function actions()
'on' => [
'class' => ChangeStatusAction::className(),
'model' => $className,
'status' => $className::STATUS_ON
],
'off' => [
'class' => ChangeStatusAction::className(),
'model' => $className,
'status' => $className::STATUS_OFF
],
];
}
Expand Down Expand Up @@ -69,33 +64,30 @@ public function actionCreate($parent = null)
$model = new $class;

if ($model->load(Yii::$app->request->post())) {
if(Yii::$app->request->isAjax){
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
else{
} else {
$model->status = $class::STATUS_ON;

$parent = (int)Yii::$app->request->post('parent', null);
if($parent > 0 && ($parentCategory = $class::findOne($parent))){
if ($parent > 0 && ($parentCategory = $class::findOne($parent))) {
$model->order_num = $parentCategory->order_num;
$model->appendTo($parentCategory);
} else {
$model->attachBehavior('sortable', SortableModel::className());
$model->makeRoot();
}

if(!$model->hasErrors()){
if (!$model->hasErrors()) {
$this->flash('success', Yii::t('easyii', 'Category created'));
return $this->redirect(['/admin/'.$this->moduleName, 'id' => $model->primaryKey]);
}
else{
return $this->redirect(['/admin/' . $this->moduleName, 'id' => $model->primaryKey]);
} else {
$this->flash('error', Yii::t('easyii', 'Create error. {0}', $model->formatErrors()));
return $this->refresh();
}
}
}
else {
} else {
return $this->render('@easyii/views/category/create', [
'model' => $model,
'parent' => $parent
Expand All @@ -112,28 +104,21 @@ public function actionCreate($parent = null)
*/
public function actionEdit($id)
{
$class = $this->categoryClass;

if(!($model = $class::findOne($id))){
return $this->redirect(['/admin/' . $this->moduleName]);
}
$model = $this->findCategory($id);

if ($model->load(Yii::$app->request->post())) {
if(Yii::$app->request->isAjax){
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
else{
if($model->save()){
} else {
if ($model->save()) {
$this->flash('success', Yii::t('easyii', 'Category updated'));
}
else{
} else {
$this->flash('error', Yii::t('easyii', 'Update error. {0}', $model->formatErrors()));
}
return $this->refresh();
}
}
else {
} else {
return $this->render('@easyii/views/category/edit', [
'model' => $model
]);
Expand All @@ -148,16 +133,13 @@ public function actionEdit($id)
*/
public function actionDelete($id)
{
$class = $this->categoryClass;
if(($model = $class::findOne($id))){
$children = $model->children()->all();
$model->deleteWithChildren();
foreach($children as $child) {
$child->afterDelete();
}
} else {
$this->error = Yii::t('easyii', 'Not found');
$model = $this->findCategory($id);
$children = $model->children()->all();
$model->deleteWithChildren();
foreach ($children as $child) {
$child->afterDelete();
}

return $this->formatResponse(Yii::t('easyii', 'Category deleted'));
}

Expand Down Expand Up @@ -193,48 +175,41 @@ public function actionDown($id)
*/
private function move($id, $direction)
{
$model = $this->findCategory($id);
$modelClass = $this->categoryClass;

if(($model = $modelClass::findOne($id)))
{
$up = $direction == 'up';
$orderDir = $up ? SORT_ASC : SORT_DESC;
$up = $direction == 'up';
$orderDir = $up ? SORT_ASC : SORT_DESC;

if($model->depth == 0){
if ($model->depth == 0) {

$swapCat = $modelClass::find()->where([$up ? '>' : '<', 'order_num', $model->order_num])->orderBy(['order_num' => $orderDir])->one();
if($swapCat)
{
$modelClass::updateAll(['order_num' => '-1'], ['order_num' => $swapCat->order_num]);
$modelClass::updateAll(['order_num' => $swapCat->order_num], ['order_num' => $model->order_num]);
$modelClass::updateAll(['order_num' => $model->order_num], ['order_num' => '-1']);
$model->trigger(\yii\db\ActiveRecord::EVENT_AFTER_UPDATE);
$swapCat = $modelClass::find()->where([$up ? '>' : '<', 'order_num', $model->order_num])->orderBy(['order_num' => $orderDir])->one();
if ($swapCat) {
$modelClass::updateAll(['order_num' => '-1'], ['order_num' => $swapCat->order_num]);
$modelClass::updateAll(['order_num' => $swapCat->order_num], ['order_num' => $model->order_num]);
$modelClass::updateAll(['order_num' => $model->order_num], ['order_num' => '-1']);
$model->trigger(\yii\db\ActiveRecord::EVENT_AFTER_UPDATE);
}
} else {
$where = [
'and',
['tree' => $model->tree],
['depth' => $model->depth],
[($up ? '<' : '>'), 'lft', $model->lft]
];

$swapCat = $modelClass::find()->where($where)->orderBy(['lft' => ($up ? SORT_DESC : SORT_ASC)])->one();
if ($swapCat) {
if ($up) {
$model->insertBefore($swapCat);
} else {
$model->insertAfter($swapCat);
}
} else {
$where = [
'and',
['tree' => $model->tree],
['depth' => $model->depth],
[($up ? '<' : '>'), 'lft', $model->lft]
];

$swapCat = $modelClass::find()->where($where)->orderBy(['lft' => ($up ? SORT_DESC : SORT_ASC)])->one();
if($swapCat)
{
if($up) {
$model->insertBefore($swapCat);
} else {
$model->insertAfter($swapCat);
}

$swapCat->update();
$model->update();
}
$swapCat->update();
$model->update();
}
}
else {
$this->flash('error', Yii::t('easyii', 'Not found'));
}
return $this->back();
}

Expand All @@ -247,20 +222,15 @@ private function move($id, $direction)
*/
public function changeStatus($id, $status)
{
$model = $this->findCategory($id);
$modelClass = $this->categoryClass;
$ids = [];
$ids = [$model->primaryKey];

if(($model = $modelClass::findOne($id))){
$ids[] = $model->primaryKey;
foreach($model->children()->all() as $child){
$ids[] = $child->primaryKey;
}
$modelClass::updateAll(['status' => $status], ['in', 'category_id', $ids]);
$model->trigger(\yii\db\ActiveRecord::EVENT_AFTER_UPDATE);
}
else{
$this->error = Yii::t('easyii', 'Not found');
foreach ($model->children()->all() as $child) {
$ids[] = $child->primaryKey;
}
$modelClass::updateAll(['status' => $status], ['in', 'category_id', $ids]);
$model->trigger(\yii\db\ActiveRecord::EVENT_AFTER_UPDATE);

return $this->formatResponse(Yii::t('easyii', 'Status successfully changed'));
}
Expand Down
Loading

0 comments on commit a2b816a

Please sign in to comment.