Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
AdminController.php
1 <?php
2 
3 class AdminController extends Controller
4 {
5  public $defaultAction = 'admin';
6 
7  private $_model;
8 
9  /**
10  * Allows to set some filters
11  *
12  * @return array action filters
13  */
14  public function filters()
15  {
16  return CMap::mergeArray(
17  parent::filters(),
18  array(
19  'accessControl', // perform access control for CRUD operations
20  )
21  );
22  }
23 
24  /**
25  * Specifies the access control rules.
26  * This method is used by the 'accessControl' filter.
27  *
28  * @return array access control rules
29  */
30  public function accessRules()
31  {
32  return array(
33  array('allow', // allow authenticated users to access all actions
34  'roles'=>array('Admin'),
35  ),
36  array('deny', // deny all users
37  'users' => array('*'),
38  ),
39  );
40  }
41 
42  /**
43  * Manages all models.
44  *
45  * @return void
46  */
47  public function actionAdmin()
48  {
49  $model = new User('search');
50  $model->status = null;
51  $model->is_bulk = null;
52  if (isset($_GET['User'])) {
53  $model->attributes = $_GET['User'];
54  }
55  $this->render('index', array('model' => $model,));
56  }
57 
58 
59  /**
60  * Displays a particular model.
61  *
62  * @return void
63  */
64  public function actionView()
65  {
66  $model = $this->loadModel();
67 
68  $attributes = array(
69  'id',
70  'username',
71  );
72 
73  array_push($attributes,
74  'password',
75  'email',
76  'activkey',
77  array(
78  'name' => 'createtime',
79  'value' => $model->createtime,
80  ),
81  array(
82  'name' => 'lastvisit',
83  'value' => (($model->lastvisit) ? $model->lastvisit : UserModule::t("Not visited")),
84  ),
85  array(
86  'name' => 'status',
87  'value' => User::itemAlias("UserStatus", $model->status),
88  )
89  );
90 
91  $profileFields = ProfileField::model()->forAdmin()->sort()->findAll();
92 
93  if ($profileFields) {
94  foreach ($profileFields as $field) {
95  $value = null;
96  $varname = $field->varname;
97  if(in_array($varname, array("send_me_notifications", "find_me_in_phone_book")) ){
98  $value = ($model->profile->$varname == 1)? true : false;
99  }else{
100  $value = ($field->widgetView($model->profile)) ? $field->widgetView($model->profile) : (($field->range) ? Profile::range($field->range, $model->profile->getAttribute($field->varname)) : $model->profile->getAttribute($field->varname));
101  }
102  array_push($attributes, array(
103  'label' => UserModule::t($field->title),
104  'name' => $field->varname,
105  'value' => $value,
106  ));
107  unset($value);
108  }
109  }
110 
111  foreach ($attributes as $i => &$attribute) {
112  if(!is_null($attribute)&&(in_array($attribute, array("password","activkey")))){
113  unset($attributes[$i]);
114  }
115  elseif (is_null($attribute['value'])) {
116  $attribute['value'] = '';
117  }
118  }
119 
120  $this->render(
121  'view', array(
122  'model' => $model,
123  'attributes' => $attributes,
124  )
125  );
126  }
127 
128  /**
129  * Creates a new model.
130  * If creation is successful, the browser will be redirected to the 'view' page.
131  *
132  * @return void
133  */
134  public function actionCreate()
135  {
136  $model = new User;
137  $profile = new Profile;
138  if (isset($_POST['User'])) {
139  $model->attributes = $_POST['User'];
140  $model->activkey = Yii::app()->controller->module->encrypting(microtime() . $model->password);
141  $model->createtime = date('Y-m-d H:i:s');
142  $model->lastvisit = date('Y-m-d H:i:s');
143  $profile->attributes = $_POST['Profile'];
144  $profile->user_id = 0;
145  if ($model->validate() && $profile->validate()) {
146  $model->password = Yii::app()->controller->module->encrypting($model->password);
147  if ($model->save()) {
148  $profile->user_id = $model->id;
149  $profile->save();
150  }
151  $this->redirect(array('view', 'id' => $model->id));
152  } else {
153  $profile->validate();
154  }
155  }
156 
157  $this->render(
158  'create', array(
159  'model' => $model,
160  'profile' => $profile,
161  )
162  );
163  }
164 
165  /**
166  * Updates a particular model.
167  * If update is successful, the browser will be redirected to the 'view' page.
168  *
169  * @return void
170  */
171  public function actionUpdate()
172  {
173  $model = $this->loadModel();
174  $profile = $model->profile;
175  if (isset($_POST['User'])) {
176  $model->attributes = $_POST['User'];
177  $profile->attributes = $_POST['Profile'];
178 
179  if ($model->validate() && $profile->validate()) {
180  $old_password = User::model()->notsafe()->findByPk($model->id);
181  if ($old_password->password != $model->password) {
182  $model->password = Yii::app()->controller->module->encrypting($model->password);
183  $model->activkey = Yii::app()->controller->module->encrypting(microtime() . $model->password);
184  }
185  $model->save();
186  $profile->save();
187  $this->redirect(array('view', 'id' => $model->id));
188  } else {
189  $profile->validate();
190  }
191  }
192 
193  $this->render(
194  'update', array(
195  'model' => $model,
196  'profile' => $profile,
197  )
198  );
199  }
200 
201 
202  /**
203  * Deletes a particular model.
204  * If deletion is successful, the browser will be redirected to the 'index' page.
205  *
206  * @throws CHttpException
207  * @return void
208  */
209  public function actionDelete()
210  {
211  if (Yii::app()->request->isPostRequest) {
212  // we only allow deletion via POST request
213  $model = $this->loadModel();
214  $profile = Profile::model()->findByPk($model->id);
215 
216  if ($account = Account::model()->findByAttributes(array('user_id'=>$model->id))) {
217  $account->delete();
218  }
219 
220  $profile->delete();
221  $model->delete();
222  // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
223  if (!isset($_POST['ajax'])) {
224  $this->redirect(array('/user/admin'));
225  }
226  } else {
227  throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
228  }
229  }
230 
231  /**
232  * Renders ManagePersonalisation view
233  *
234  * @return void
235  */
236  public function actionManagePersonalisation()
237  {
238  $this->render('managePersonalisation');
239  }
240 
241 
242  /**
243  * Returns the data model based on the primary key given in the GET variable.
244  * If the data model is not found, an HTTP exception will be raised.
245  *
246  * @throws CHttpException
247  * @return void
248  */
249  public function loadModel()
250  {
251  if ($this->_model === null) {
252  if (isset($_GET['id'])) {
253  $this->_model = RegistrationForm::model()->notsafe()->findbyPk($_GET['id']);
254  }
255  if ($this->_model===null) {
256  throw new CHttpException(404, 'The requested page does not exist.');
257  }
258  }
259  return $this->_model;
260  }
261 
262 }