TermController::actionAjaxCreateNonHierarchical()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 6
nc 2
nop 0
1
<?php
2
/**
3
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace backend\controllers;
9
10
use common\models\Term;
11
use common\models\TermRelationship;
12
use Yii;
13
use yii\filters\AccessControl;
14
use yii\filters\VerbFilter;
15
use yii\helpers\Html;
16
use yii\web\Controller;
17
use yii\web\NotFoundHttpException;
18
use yii\web\Response;
19
20
/**
21
 * TermController implements the CRUD actions for Term model.
22
 *
23
 * @author Agiel K. Saputra <[email protected]>
24
 * @since 0.1.0
25
 */
26
class TermController extends Controller
27
{
28
    /**
29
     * @inheritdoc
30
     */
31 View Code Duplication
    public function behaviors()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        return [
34
            'access' => [
35
                'class' => AccessControl::className(),
36
                'rules' => [
37
                    [
38
                        'actions' => ['ajax-create-hierarchical', 'ajax-create-non-hierarchical', 'bulk-action'],
39
                        'allow' => true,
40
                        'roles' => ['editor'],
41
                    ],
42
                    [
43
                        'actions' => ['ajax-search'],
44
                        'allow' => true,
45
                        'roles' => ['subscriber'],
46
                    ],
47
                ],
48
            ],
49
            'verbs' => [
50
                'class' => VerbFilter::className(),
51
                'actions' => [
52
                    'ajax-create-hierarchical' => ['post'],
53
                    'ajax-create-non-hierarchical' => ['post'],
54
                    'bulk-action' => ['post'],
55
                    'ajax-search' => ['post'],
56
                ],
57
            ],
58
        ];
59
    }
60
61
    /**
62
     * Create a new Term model for hierarchical Taxonomy through AJAX request.
63
     */
64
    public function actionAjaxCreateHierarchical()
65
    {
66
        $item = '';
67
        $term = new Term();
68
        $termRelationship = new TermRelationship();
69
70
        if ($term->load(Yii::$app->request->post())) {
71
            if ($termRelationship->load(Yii::$app->request->post()) && $termRelationship->post_id) {
72
                $term->count = 1;
73
                if ($term->save()) {
74
                    $termRelationship->term_id = $term->id;
75 View Code Duplication
                    if ($termRelationship->save()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
                        $item = $item
77
                            . '<br />'
78
                            . Html::label(Html::checkbox('termIds[]', true, ['value' => $term->id]) . $term->name);
79
                    }
80
                }
81 View Code Duplication
            } elseif ($term->save()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
                $item = $item
83
                    . '<br />'
84
                    . Html::label(Html::checkbox('termIds[]', true, ['value' => $term->id]) . $term->name);
85
            }
86
        }
87
88
        return $item;
89
    }
90
91
    /**
92
     * Create a new Term for non-hierarchical Taxonomy through Selectize box.
93
     *
94
     * @return string
95
     */
96
    public function actionAjaxCreateNonHierarchical()
97
    {
98
        $model = new Term();
99
        Yii::$app->response->format = Response::FORMAT_JSON;
100
101
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
102
            return ['id' => $model->id, 'name' => $model->name];
103
        }
104
105
        return [];
106
    }
107
108
    /**
109
     * Search Term through Ajax with JSON as response.
110
     *
111
     * @return string
112
     */
113
    public function actionAjaxSearch()
114
    {
115
        Yii::$app->response->format = Response::FORMAT_JSON;
116
117
        if ($term = Yii::$app->request->post('Term')) {
118
            $model = Term::find()
119
                ->select(['id', 'name'])
120
                ->where(['like', 'name', $term['name']])
121
                ->andWhere(['taxonomy_id' => $term['taxonomy_id']])
122
                ->limit('10')
123
                ->all();
124
            if ($model) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $model of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
125
                return ($model);
126
            }
127
        }
128
129
        return [];
130
    }
131
132
    /**
133
     * Bulk action for Term triggered when button 'Apply' clicked.
134
     * The action depends on the value of the dropdown next to the button.
135
     * Only accept POST HTTP method.
136
     */
137 View Code Duplication
    public function actionBulkAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139
        if (Yii::$app->request->post('action') == 'deleted') {
140
            foreach (Yii::$app->request->post('ids', []) as $id) {
141
                $this->findModel($id)->delete();
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $this->findModel($id) (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
142
            }
143
        }
144
    }
145
146
    /**
147
     * Finds the Term model based on its primary key value.
148
     * If the model is not found, a 404 HTTP exception will be thrown.
149
     *
150
     * @param integer $id
151
     * @return Term the loaded model
152
     * @throws NotFoundHttpException if the model cannot be found
153
     */
154
    protected function findModel($id)
155
    {
156
        if (($model = Term::findOne($id)) !== null) {
157
            return $model;
158
        }
159
160
        throw new NotFoundHttpException('The requested page does not exist.');
161
    }
162
}
163