Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Friendship.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  * This class represents friendship between two Users
11  *
12  * @property int $user1_id
13  * @property int $user2_id
14  * @property int $status
15  * @property User $user1
16  * @property User $user2
17  */
18 class Friendship extends CActiveRecord
19 {
20 
21  const STATUS_CANCELED = -1;
22  const STATUS_NOT_APPROVED = 0;
23  const STATUS_APPROVED = 1;
24 
25  /**
26  * Returns the static model of the specified AR class.
27  *
28  * @param string $className active record class name
29  *
30  * @return array|CActiveRecord
31  */
32  public static function model($className = __CLASS__)
33  {
34  return parent::model($className);
35  }
36 
37  /**
38  * Returns the name of the associated database table.
39  * By default this method returns the class name as the table name.
40  * You may override this method if the table is not named after this convention.
41  *
42  * @return string the table name
43  */
44  public function tableName()
45  {
46  return '{{friendship}}';
47  }
48 
49  public function rules()
50  {
51  return array(
52  array('user1_id, user2_id, status', 'required'),
53  array('user1_id, user2_id, status', 'numerical', 'integerOnly' => true)
54  );
55  }
56 
57  public function relations()
58  {
59  return array(
60  'user1' => array(CActiveRecord::BELONGS_TO, 'User', 'user1_id'),
61  'user2' => array(CActiveRecord::BELONGS_TO, 'User', 'user2_id'),
62  );
63  }
64 
65 
66  /**
67  * Returns friend User model.
68  *
69  * @param int $user_id id of some user in this friendship
70  *
71  * @return null|User his friend
72  */
73  public function friend($user_id)
74  {
75  if ($user_id == $this->user1_id) {
76  return $this->user2;
77  } elseif ($user_id == $this->user2_id) {
78  return $this->user1;
79  }
80  return null;
81  }
82 
83  /**
84  * Get User model of user with $user_id
85  *
86  * @param int $user_id id of some user in this friendship
87  *
88  * @return null|User his model
89  */
90  public function me($user_id)
91  {
92  if ($user_id == $this->user1->id) {
93  return $this->user1;
94  } elseif ($user_id == $this->user2->id) {
95  return $this->user2;
96  }
97  return null;
98  }
99 
100  /**
101  * Check if user with $user_id make friend request or not
102  *
103  * @param int $user_id id of needed user
104  *
105  * @return bool
106  */
107  public function myRequest($user_id)
108  {
109  if ($user_id == $this->user1->id) {
110  return true;
111  }
112 
113  return false;
114  }
115  /**
116  * If the user was removed after friendship with him was
117  * created gportal will destruct such relations to avoid errors
118  *
119  */
120 
121  protected function afterFind()
122  {
124  if(!isset($this->user1) || !isset($this->user2)){
125  $this->delete();
126  }
127  }
128 
129 }