1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Base controller class for controllers that operate mainly on a specific type |
5
|
|
|
* of model. It provides some methods that are usually needed by derived |
6
|
|
|
* classes. |
7
|
|
|
* |
8
|
|
|
* @author Sam Stenvall <[email protected]> |
9
|
|
|
* @copyright Copyright © Sam Stenvall 2013- |
10
|
|
|
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0 |
11
|
|
|
*/ |
12
|
|
|
abstract class ModelController extends Controller |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Generic "admin" action. It renders the "admin" view and handles |
17
|
|
|
* attribute filtering. |
18
|
|
|
*/ |
19
|
|
|
public function actionAdmin() |
20
|
|
|
{ |
21
|
|
|
$modelClass = $this->getModelClass(); |
22
|
|
|
|
23
|
|
|
$model = $modelClass::model(); |
24
|
|
|
$model->scenario = 'search'; |
25
|
|
|
|
26
|
|
|
if (isset($_GET[$modelClass])) |
27
|
|
|
{ |
28
|
|
|
$model->unsetAttributes(); |
29
|
|
|
$model->attributes = $_GET[$modelClass]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->render('admin', array( |
33
|
|
|
'model'=>$model, |
34
|
|
|
)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Attempts to save the specified model with attribute values from POST. If |
39
|
|
|
* the saving fails or if no POST data is available it returns false. |
40
|
|
|
* @param CActiveRecord $model the model |
41
|
|
|
* @return boolean whether the model was saved |
42
|
|
|
*/ |
43
|
|
|
protected function saveFromPost(&$model) |
44
|
|
|
{ |
45
|
|
|
if (isset($_POST[$this->getModelClass()])) |
46
|
|
|
{ |
47
|
|
|
$model->attributes = $_POST[$this->getModelClass()]; |
48
|
|
|
|
49
|
|
|
if ($model->save()) |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Finds and returns a model with the specified ID |
58
|
|
|
* @param mixed $id a primary key |
59
|
|
|
* @return CActiveRecord the model |
60
|
|
|
* @throws PageNotFoundException if the model is not found |
61
|
|
|
*/ |
62
|
|
|
protected function loadModel($id) |
63
|
|
|
{ |
64
|
|
|
$modelClass = $this->getModelClass(); |
65
|
|
|
$model = $modelClass::model()->findByPk($id); |
66
|
|
|
|
67
|
|
|
if ($model === null) |
68
|
|
|
throw new PageNotFoundException(); |
69
|
|
|
|
70
|
|
|
return $model; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Performs a standard redirect after a model has been deleted |
75
|
|
|
*/ |
76
|
|
|
protected function redirectOnDelete() |
77
|
|
|
{ |
78
|
|
|
if (!isset($_GET['ajax'])) |
79
|
|
|
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string the name of the model class this controller represents |
84
|
|
|
*/ |
85
|
|
|
private function getModelClass() |
86
|
|
|
{ |
87
|
|
|
$controllerClass = get_class($this); |
88
|
|
|
return substr($controllerClass, 0, -(strlen('Controller'))); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|