1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace nkostadinov\taxonomy\controllers; |
4
|
|
|
|
5
|
|
|
use nkostadinov\taxonomy\models\TaxonomyTerms; |
6
|
|
|
use Yii; |
7
|
|
|
use nkostadinov\taxonomy\models\TaxonomyDef; |
8
|
|
|
use nkostadinov\taxonomy\models\TaxonomyDefSearch; |
9
|
|
|
use yii\base\InvalidConfigException; |
10
|
|
|
use yii\data\ActiveDataProvider; |
11
|
|
|
use yii\helpers\Url; |
12
|
|
|
use yii\web\Controller; |
13
|
|
|
use yii\web\NotFoundHttpException; |
14
|
|
|
use yii\filters\VerbFilter; |
15
|
|
|
|
16
|
|
|
// fcgi doesn't have STDIN and STDOUT defined by default |
17
|
|
|
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r')); |
18
|
|
|
defined('STDOUT') or define('STDOUT', fopen('php://stdout', 'w')); |
19
|
|
|
|
20
|
|
|
class Migrator extends \yii\console\controllers\MigrateController |
21
|
|
|
{ |
22
|
|
|
public function __construct($id, $module, $config = []) |
23
|
|
|
{ |
24
|
|
|
parent::__construct($id, $module, $config); // TODO: Change the autogenerated stub |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function runTaxonomy($class, $migrationPath) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
ob_start(); |
30
|
|
|
//$this->migrationPath = Yii::getAlias($migrationPath); |
31
|
|
|
if($this->beforeAction('up')) |
|
|
|
|
32
|
|
|
$this->migrateUp($class); |
33
|
|
|
$result = ob_get_contents(); |
34
|
|
|
return $result; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function removeTaxonomy($class, $migrationPath) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
ob_start(); |
40
|
|
|
//$this->migrationPath = Yii::getAlias($migrationPath); |
41
|
|
|
if($this->beforeAction('down')) |
|
|
|
|
42
|
|
|
$this->migrateDown($class); |
43
|
|
|
$result = ob_get_contents(); |
44
|
|
|
return $result; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* DefController implements the CRUD actions for TaxonomyDef model. |
50
|
|
|
*/ |
51
|
|
|
class DefController extends Controller |
52
|
|
|
{ |
53
|
|
|
public function behaviors() |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'verbs' => [ |
57
|
|
|
'class' => VerbFilter::className(), |
|
|
|
|
58
|
|
|
'actions' => [ |
59
|
|
|
'delete' => ['post'], |
60
|
|
|
], |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Lists all TaxonomyDef models. |
67
|
|
|
* @return mixed |
68
|
|
|
* @throws InvalidConfigException |
69
|
|
|
*/ |
70
|
|
|
public function actionIndex() |
71
|
|
|
{ |
72
|
|
|
if(!$this->getComponent()->isInstalled()) |
73
|
|
|
throw new InvalidConfigException("Please run the migration first!"); |
74
|
|
|
|
75
|
|
|
$searchModel = new TaxonomyDefSearch(); |
76
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
77
|
|
|
|
78
|
|
|
return $this->render('index', [ |
79
|
|
|
'searchModel' => $searchModel, |
80
|
|
|
'dataProvider' => $dataProvider, |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Displays a single TaxonomyDef model. |
86
|
|
|
* @param integer $id |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
|
public function actionView($id) |
90
|
|
|
{ |
91
|
|
|
$termProvider = new ActiveDataProvider([ |
92
|
|
|
'query' => TaxonomyTerms::find()->andFilterWhere(['taxonomy_id' => $id]), |
93
|
|
|
'sort' => [ |
94
|
|
|
// Set the default sort by name ASC and created_at DESC. |
95
|
|
|
'defaultOrder' => [ |
96
|
|
|
'total_count' => SORT_DESC, |
97
|
|
|
] |
98
|
|
|
], |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
return $this->render('view', [ |
102
|
|
|
'model' => $this->findModel($id), |
103
|
|
|
'termProvider' => $termProvider |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Creates a new TaxonomyDef model. |
109
|
|
|
* If creation is successful, the browser will be redirected to the 'view' page. |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public function actionCreate() |
113
|
|
|
{ |
114
|
|
|
$model = new TaxonomyDef(); |
115
|
|
|
$definitions = $this->getComponent()->getDefinitions(); |
116
|
|
|
|
117
|
|
|
if ($model->load(Yii::$app->request->post())) { |
118
|
|
|
//install the term |
119
|
|
|
$term = Yii::createObject($model->attributes); |
120
|
|
|
//$this->getComponent()->getTerm($model->name); |
121
|
|
|
$term->install(); |
122
|
|
|
$migration = new Migrator('migrate', Yii::$app); |
123
|
|
|
$messsage = $migration->runTaxonomy($term->migration, $term->migrationPath); |
124
|
|
|
Yii::$app->session->setFlash('info', $messsage, true); |
125
|
|
|
|
126
|
|
|
return $this->redirect(['index', 'id' => $model->id]); |
127
|
|
|
} else { |
128
|
|
|
return $this->render('create', [ |
129
|
|
|
'model' => $model, |
130
|
|
|
'definitions' => $definitions, |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Updates an existing TaxonomyDef model. |
137
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
138
|
|
|
* @param integer $id |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
|
|
public function actionUpdate($id) |
142
|
|
|
{ |
143
|
|
|
$model = $this->findModel($id); |
144
|
|
|
$definitions = $this->getComponent()->getDefinitions(); |
145
|
|
|
|
146
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
147
|
|
|
return $this->redirect(['view', 'id' => $model->id]); |
148
|
|
|
} else { |
149
|
|
|
return $this->render('update', [ |
150
|
|
|
'model' => $model, |
151
|
|
|
'definitions' => $definitions, |
152
|
|
|
]); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Deletes an existing TaxonomyDef model. |
158
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
159
|
|
|
* @param integer $id |
160
|
|
|
* @return mixed |
161
|
|
|
*/ |
162
|
|
|
public function actionDelete($id) |
163
|
|
|
{ |
164
|
|
|
$model = $this->findModel($id); |
165
|
|
|
//UNinstall the term |
166
|
|
|
$term = $this->getComponent()->getTerm($model->name); |
167
|
|
|
$migration = new Migrator('migrate', Yii::$app); |
168
|
|
|
$messsage = $migration->removeTaxonomy($term->migration, $term->migrationPath); |
169
|
|
|
Yii::$app->session->setFlash('info', $messsage, true); |
170
|
|
|
|
171
|
|
|
$term->uninstall(); |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
return $this->redirect(['index']); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Finds the TaxonomyDef model based on its primary key value. |
179
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
180
|
|
|
* @param integer $id |
181
|
|
|
* @return TaxonomyDef the loaded model |
182
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
183
|
|
|
*/ |
184
|
|
|
protected function findModel($id) |
185
|
|
|
{ |
186
|
|
|
if (($model = TaxonomyDef::findOne($id)) !== null) { |
|
|
|
|
187
|
|
|
return $model; |
188
|
|
|
} else { |
189
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function getComponent() |
194
|
|
|
{ |
195
|
|
|
if(\Yii::$app->has($this->module->component)) |
|
|
|
|
196
|
|
|
return \Yii::$app->{$this->module->component}; |
|
|
|
|
197
|
|
|
else |
198
|
|
|
throw new InvalidConfigException("Cannot find taxonomy component({$this->module->component})"); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function actionInstall() |
202
|
|
|
{ |
203
|
|
|
if(!$this->getComponent()->isInstalled() and \Yii::$app->request->isPost) { |
204
|
|
|
//start installation |
205
|
|
|
if($this->getComponent()) { |
206
|
|
|
$this->getComponent()->install(); |
207
|
|
|
|
208
|
|
|
$this->redirect(['/'.$this->module->id . '/' . $this->id . '/index']); |
209
|
|
|
} else |
210
|
|
|
throw new InvalidConfigException("Cannot find taxonomy component({$this->module->component})"); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
return $this->render('install'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function actionInstallterm($term) |
216
|
|
|
{ |
217
|
|
|
$term = $this->getComponent()->getTerm($term); |
218
|
|
|
$term->install(); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.