Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
FriendsSearchResultWidget.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  *
11  * Shows user search result. Use in pair with FriendsSearchWidget
12  */
14 {
15 
16  private $_friendships = array();
17 
18  public function run()
19  {
20  $models = $this->_getPreparedModels();
21  if (!empty($models['UserSearch']) || !empty($models['Profile'])) {
22  $criteria = new CDbCriteria;
23  $criteria->compare('status', User::STATUS_ACTIVE);
24 
25  if (isset($models['UserSearch'])) {
26  $user = new UserSearch();
27  $user->attributes = $models['UserSearch'];
28  $user->validate();
29  foreach ($models['UserSearch'] as $name => $value) {
30  if (!$user->hasErrors($name) && $value == $user->$name) {
31  $criteria->compare($name, $value, true);
32  }
33  }
34  if (!Yii::app()->user->isGuest) {
35  $criteria->addCondition('id != :id');
36  $criteria->params[':id'] = Yii::app()->user->id;
37  }
38  }
39 
40  $profile = new Profile();
41  if(!isset($profile->phone)){
42  $alter_phone = "ALTER TABLE {{profiles}} ADD `phone` varchar(255) NOT NULL DEFAULT '+43'";
43  Yii::app()->db->createCommand($alter_phone)->execute();
44 
45  }
46  if (isset($models['Profile'])) {
47  $profile->attributes = $models['Profile'];
48  $profile->validate();
49  $criteria->together = true;
50  $criteria->with = array('profile');
51  foreach ($models['Profile'] as $name => $value) {
52  if (!$profile->hasErrors($name) && $value == $profile->$name) {
53  $criteria->compare("profile.$name", $value, true);
54  }
55  }
56  }
57  if ($profile->hasAttribute('find_me_in_phone_book')) {
58  $criteria->compare("profile.find_me_in_phone_book", true);
59  }
60  $dataProvider = new CActiveDataProvider('User', array(
61  'criteria' => $criteria,
62  'pagination' => array( // 'pageSize' => $this->module->user_list_page_size,
63  ),
64  ));
65 
66  if (!Yii::app()->user->isGuest) {
67  $this->_friendships = Friendship::model()->findAll('user1_id = :user_id or user2_id = :user_id', array(':user_id' => Yii::app()->user->id));
68  }
69  } else {
70  $dataProvider = null;
71  }
72 
73  $this->render('FriendsSearchResultWidget', array('dataProvider' => $dataProvider));
74  }
75 
76  private function _getPreparedModels()
77  {
78  $preparedModels = array();
79  foreach (array('UserSearch', 'Profile') as $model) {
80  if (isset($_GET[$model])) {
81  $preparedModels[$model] = array_filter($_GET[$model], function ($var) {
82  $var = trim($var);
83  return !empty($var);
84  });
85  }
86  }
87  return $preparedModels;
88  }
89 
90  public function friendshipStatus($user)
91  {
92  foreach ($this->_friendships as $f) {
93  if ($f->user1_id == $user->id || $f->user2_id == $user->id) {
94  if ($f->status == Friendship::STATUS_APPROVED) {
95  return 'friends';
96  } else {
97  return 'waiting';
98  }
99  }
100  }
101 
102  return 'not friends';
103  }
104 
105 
106 }