1 <?php
2
3 class AdminController extends Controller
4 {
5 public $defaultAction = 'admin';
6 public $layout='//layouts/column2';
7
8 private $_model;
9
10 11 12
13 public function filters()
14 {
15 return CMap::mergeArray(parent::filters(),array(
16 'accessControl',
17 ));
18 }
19 20 21 22 23
24 public function accessRules()
25 {
26 return array(
27 array('allow',
28 'actions'=>array('admin','delete','create','update','view'),
29 'users'=>UserModule::getAdmins(),
30 ),
31 array('deny',
32 'users'=>array('*'),
33 ),
34 );
35 }
36 37 38
39 public function actionAdmin()
40 {
41 $model=new User('search');
42 $model->unsetAttributes();
43 if(isset($_GET['User']))
44 $model->attributes=$_GET['User'];
45
46 $this->render('index',array(
47 'model'=>$model,
48 ));
49 50 51 52 53 54 55 56 57
58 }
59
60
61 62 63
64 public function actionView()
65 {
66 $model = $this->loadModel();
67 $this->render('view',array(
68 'model'=>$model,
69 ));
70 }
71
72 73 74 75
76 public function actionCreate()
77 {
78 $model=new User;
79 $profile=new Profile;
80 $this->performAjaxValidation(array($model,$profile));
81 if(isset($_POST['User']))
82 {
83 $model->attributes=$_POST['User'];
84 $model->activkey=Yii::app()->controller->module->encrypting(microtime().$model->password);
85 $profile->attributes=$_POST['Profile'];
86 $profile->user_id=0;
87 if($model->validate()&&$profile->validate()) {
88 $model->password=Yii::app()->controller->module->encrypting($model->password);
89 if($model->save()) {
90 $profile->user_id=$model->id;
91 $profile->save();
92 }
93 $this->redirect(array('view','id'=>$model->id));
94 } else $profile->validate();
95 }
96
97 $this->render('create',array(
98 'model'=>$model,
99 'profile'=>$profile,
100 ));
101 }
102
103 104 105 106
107 public function actionUpdate()
108 {
109 $model=$this->loadModel();
110 $profile=$model->profile;
111 $this->performAjaxValidation(array($model,$profile));
112 if(isset($_POST['User']))
113 {
114 $model->attributes=$_POST['User'];
115 $profile->attributes=$_POST['Profile'];
116
117 if($model->validate()&&$profile->validate()) {
118 $old_password = User::model()->notsafe()->findByPk($model->id);
119 if ($old_password->password!=$model->password) {
120 $model->password=Yii::app()->controller->module->encrypting($model->password);
121 $model->activkey=Yii::app()->controller->module->encrypting(microtime().$model->password);
122 }
123 $model->save();
124 $profile->save();
125 $this->redirect(array('view','id'=>$model->id));
126 } else $profile->validate();
127 }
128
129 $this->render('update',array(
130 'model'=>$model,
131 'profile'=>$profile,
132 ));
133 }
134
135
136 137 138 139
140 public function actionDelete()
141 {
142 if(Yii::app()->request->isPostRequest)
143 {
144
145 $model = $this->loadModel();
146 $profile = Profile::model()->findByPk($model->id);
147 $profile->delete();
148 $model->delete();
149
150 if(!isset($_POST['ajax']))
151 $this->redirect(array('/user/admin'));
152 }
153 else
154 throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
155 }
156
157 158 159 160
161 protected function performAjaxValidation($validate)
162 {
163 if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
164 {
165 echo CActiveForm::validate($validate);
166 Yii::app()->end();
167 }
168 }
169
170
171 172 173 174
175 public function loadModel()
176 {
177 if($this->_model===null)
178 {
179 if(isset($_GET['id']))
180 $this->_model=User::model()->notsafe()->findbyPk($_GET['id']);
181 if($this->_model===null)
182 throw new CHttpException(404,'The requested page does not exist.');
183 }
184 return $this->_model;
185 }
186
187 }