Пример #1
0
 public function actionAddFriend()
 {
     $a = array();
     if (Yii::app()->request->isAjaxRequest) {
         $model = new UserFriends();
         $post = $_POST['UserFriends'];
         if (isset($post)) {
             $model->attributes = $post;
             $model->user_id = Yii::app()->user->id;
             if ($model->validate()) {
                 if ($model->save(false, false)) {
                     $a = array('message' => 'Заявка успешно подана!', 'errorCode' => 0);
                 } else {
                     $a = array('message' => Yii::t('core', 'ERROR'), 'errorCode' => 1);
                 }
             } else {
                 $a = array('message' => $model->errors['user_id'], 'errorCode' => 2);
             }
         } else {
             $a = array('message' => Yii::t('core', 'ERROR'), 'errorCode' => 3);
         }
     } else {
         $a = array('message' => Yii::t('core', 'ERROR'), 'errorCode' => 4);
     }
     echo CJSON::encode($a);
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new UserFriends();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['UserFriends'])) {
         $model->attributes = $_POST['UserFriends'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 public function actionAddFriend()
 {
     $id = Yii::app()->user->id;
     if (!$id > 0) {
         throw new CException('Not Found', 404);
     }
     $u_id = $_GET['id'];
     $f_id = $_GET['f_id'];
     $friendship_a = UserFriends::model()->find('status = 2 and user_id = ' . $u_id . ' and friend_id = ' . $f_id);
     $friendship_b = UserFriends::model()->find('status = 3 and user_id = ' . $f_id . ' and friend_id = ' . $u_id);
     $is_friends_a = UserFriends::model()->find('status = 1 and user_id = ' . $u_id . ' and friend_id = ' . $f_id);
     $is_friends_b = UserFriends::model()->find('status = 1 and user_id = ' . $f_id . ' and friend_id = ' . $u_id);
     if ($friendship_a && $friendship_b) {
         echo json_encode(array('status' => 'ok', 'data' => '<font class="blue">Запрос дружбы уже отправлен</font>'));
     } elseif ($is_friends_a && $is_friends_b) {
         echo json_encode(array('status' => 'ok', 'data' => '<font class="blue">Вы уже друзья</font>'));
     } else {
         $add_friend_a = new UserFriends();
         $add_friend_a->id = NULL;
         $add_friend_a->user_id = $u_id;
         $add_friend_a->friend_id = $f_id;
         $add_friend_a->status = '2';
         $add_friend_a->save();
         $add_friend_b = new UserFriends();
         $add_friend_b->id = NULL;
         $add_friend_b->user_id = $f_id;
         $add_friend_b->friend_id = $u_id;
         $add_friend_b->status = '3';
         $add_friend_b->save();
         echo json_encode(array('status' => 'ok', 'data' => '<font class="grey">Вы отправили заявку, </font><a onclick="deleteRequest(this,\'' . $this->createUrl('profile/profile/DeleteRequest', array('id' => $u_id, 'f_id' => $f_id)) . '\')" href="#" class="blue">' . Yii::t('site', 'Удалить вашу заявку?') . '<a>'));
     }
 }