Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
FriendsController.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  */
12 {
13  public function filters()
14  {
15  return array(
16  'accessControl', // perform access control for CRUD operations
17  );
18  }
19 
20  public function accessRules()
21  {
22  return array(
23  array('allow',
24  'users' => array('@'),
25  ),
26  array('deny', // deny all users
27  'users' => array('?'),
28  ),
29  );
30  }
31 
32  /**
33  * Initialise friendship between two users
34  *
35  * @param string $return_url return url
36  *
37  * @throws CHttpException
38  */
39  public function actionAdd($return_url)
40  {
41  if (!isset($_GET['key'], $_GET['user_id']) || FriendsModule::encrypting($_GET['user_id']) != $_GET['key']) {
42  throw new CHttpException(400);
43  }
44 
45  $friendship = Friendship::model()->findByPk(array('user1_id' => Yii::app()->user->id, 'user2_id' => $_GET['user_id']));
46  if ($friendship) {
47  $message = FriendsModule::t('You have already sent request.');
48  if ($friendship->status == Friendship::STATUS_NOT_APPROVED) {
49  $message .= FriendsModule::t('Not approved yet.');
50  } elseif ($friendship->status == Friendship::STATUS_APPROVED) {
51  $message .= FriendsModule::t('Approved yet.');
52  } elseif ($friendship->status == Friendship::STATUS_CANCELED) {
53  $message .= FriendsModule::t('Canceled by user.');
54  }
55  Yii::app()->user->setFlash('friends-error', $message);
56  $this->redirect($return_url);
57  }
58 
59  $friendship = new Friendship();
60  $friendship->user1_id = Yii::app()->user->id;
61  $friendship->user2_id = $_GET['user_id'];
62  $friendship->status = Friendship::STATUS_NOT_APPROVED;
63  $friendship->save();
64 
65  Yii::app()->user->setFlash('friends-success', FriendsModule::t("Your friend request was sent"));
66  $title = FriendsModule::t('Friendship request');
67  $notification = new Notification(
68  $title,
69  $this->renderMail('/mail_templates/add', $title, array('friendship' => $friendship)),
70  array(new EmailRecipient($friendship->friend(Yii::app()->user->id)->email, $this->module->notificationsEmail))
71  );
72  Yii::app()->notificationManager->notifyAbout($notification);
73 
74  $this->redirect($return_url);
75 
76  }
77 
78  /**
79  * Approve friendship between two users
80  *
81  * @param string $return_url return url
82  *
83  * @throws CHttpException
84  */
85  public function actionApprove($return_url = '')
86  {
87  if (!isset($_GET['key'], $_GET['user_id']) || FriendsModule::encrypting($_GET['user_id']) != $_GET['key']) {
88  throw new CHttpException(400);
89  }
90 
91  $friendship = Friendship::model()->findByPk(array('user1_id' => $_GET['user_id'], 'user2_id' => Yii::app()->user->id));
92  if (!$friendship) {
93  Yii::app()->user->setFlash('friends-error', FriendsModule::t("Bad request"));
94  $this->redirect(Yii::app()->getModule('user')->friendListUrl);
95  }
96  if ($friendship->status != Friendship::STATUS_APPROVED) {
97  $friendship->status = Friendship::STATUS_APPROVED;
98  $friendship->save();
99  Yii::app()->user->setFlash('friends-success', FriendsModule::t("Friend request was approved"));
100 
101  $title = FriendsModule::t('Friendship approved');
102  $notification = new Notification(
103  $title,
104  $this->renderMail('/mail_templates/approve', $title, array('friendship' => $friendship)),
105  array(new EmailRecipient($friendship->friend(Yii::app()->user->id)->email, $this->module->notificationsEmail))
106  );
107  Yii::app()->notificationManager->notifyAbout($notification);
108  } else {
109  Yii::app()->user->setFlash('friends-error', FriendsModule::t("Friend request already approved"));
110  }
111 
112  if ($return_url) {
113  $this->redirect($return_url);
114  } else {
115  $this->redirect(Yii::app()->getModule('user')->friendListUrl);
116  }
117  }
118 
119  /**
120  * Cancel friendship between two users
121  *
122  * @param string $return_url return url
123  *
124  * @throws CHttpException
125  */
126  public function actionCancel($return_url = '')
127  {
128  if (!isset($_GET['key'], $_GET['user_id']) || FriendsModule::encrypting($_GET['user_id']) != $_GET['key']) {
129  throw new CHttpException(400);
130  }
131 
132  $friendship = Friendship::model()->findByPk(array('user1_id' => $_GET['user_id'], 'user2_id' => Yii::app()->user->id));
133  if(!isset($friendship)){
134  $friendship = Friendship::model()->findByPk(array('user2_id' => $_GET['user_id'], 'user1_id' => Yii::app()->user->id));
135  }
136 
137  $friendship->delete();
138  Yii::app()->user->setFlash('friends-success', FriendsModule::t("Friend request was canceled"));
139 
140  $title = FriendsModule::t('Friendship canceled');
141  $notification = new Notification(
142  $title,
143  $this->renderMail('/mail_templates/cancel', $title, array('friendship' => $friendship)),
144  array(new EmailRecipient($friendship->friend(Yii::app()->user->id)->email, $this->module->notificationsEmail))
145  );
146  Yii::app()->notificationManager->notifyAbout($notification);
147 
148  if ($return_url) {
149  $this->redirect($return_url);
150  } else {
151  $this->redirect(Yii::app()->getModule('user')->friendListUrl);
152  }
153  }
154 
155 }