コード例 #1
0
 /**
  * Show pending friends request and allow to accepet or refuse it
  */
 public function pendingRequestsAction()
 {
     try {
         $globalContext = new \Sb\Context\Model\Context();
         $user = $globalContext->getConnectedUser();
         $this->view->user = $user;
         // Show pending requests
         if (!$_POST) {
             $totalPendingRequests = $user->getPendingFriendShips();
             if ($totalPendingRequests && count($totalPendingRequests) > 0) {
                 // Preparing pagination
                 $paginatedList = new PaginatedList($totalPendingRequests, 6);
                 $this->view->firstItemIdx = $paginatedList->getFirstPage();
                 $this->view->lastItemIdx = $paginatedList->getLastPage();
                 $this->view->nbItemsTot = $paginatedList->getTotalPages();
                 $this->view->navigation = $paginatedList->getNavigationBar();
                 $this->view->pendingRequests = $paginatedList->getItems();
             }
         } else {
             $friendShipId = ArrayHelper::getSafeFromArray($_POST, 'friendShipId', null);
             $Title = ArrayHelper::getSafeFromArray($_POST, 'Title', null);
             $Message = ArrayHelper::getSafeFromArray($_POST, 'Message', null);
             $Refused = ArrayHelper::getSafeFromArray($_POST, 'Refused', null);
             if ($friendShipId) {
                 if ($Refused == 0) {
                     // Update the requested friendship
                     $friendShip = FriendShipDao::getInstance()->get($friendShipId);
                     if ($friendShip) {
                         $friendShip->setAccepted(true);
                         $friendShip->setValidated(true);
                         if (FriendShipDao::getInstance()->update($friendShip)) {
                             // Add the userEvent
                             try {
                                 $userEvent = new UserEvent();
                                 $userEvent->setNew_value($user->getId());
                                 $userEvent->setType_id(EventTypes::USER_ADD_FRIEND);
                                 $userEvent->setUser($friendShip->getUser_source());
                                 UserEventDao::getInstance()->add($userEvent);
                             } catch (\Exception $exc) {
                                 Trace::addItem("Erreur lors de l'ajout de l'événement : " . $exc->getMEssage());
                             }
                         }
                     }
                     // Create a friendship on the other side
                     $inverseFriendShip = new FriendShip();
                     $inverseFriendShip->setAccepted(true);
                     $inverseFriendShip->setValidated(true);
                     $inverseFriendShip->setCreationDate(new \DateTime());
                     $inverseFriendShip->setUser_source($user);
                     $inverseFriendShip->setUser_target($friendShip->getUser_source());
                     if (FriendShipDao::getInstance()->add($inverseFriendShip)) {
                         // Add the userEvent
                         try {
                             $userEvent = new UserEvent();
                             $userEvent->setNew_value($friendShip->getUser_source()->getId());
                             $userEvent->setType_id(EventTypes::USER_ADD_FRIEND);
                             $userEvent->setUser($user);
                             UserEventDao::getInstance()->add($userEvent);
                         } catch (\Exception $exc) {
                             Trace::addItem("Erreur lors de l'ajout de l'événement : " . $exc->getMEssage());
                         }
                     }
                     // Send email to the requesting user
                     MailSvc::getInstance()->send($friendShip->getUser_source()->getEmail(), __("Demande d'ami", "s1b"), MailHelper::friendShipAcceptationEmailBody($user->getFirstName() . " " . $user->getLastName()));
                     // add a message in requesting user internal mailbox
                     $message = new \Sb\Db\Model\Message();
                     $message->setDate(new \DateTime());
                     $message->setMessage($Message);
                     $message->setTitle($Title);
                     $message->setRecipient($friendShip->getUser_source());
                     $message->setSender($user);
                     MessageDao::getInstance()->add($message);
                     // redirect to pending request page
                     Flash::addItem("Demande acceptée.");
                     HTTPHelper::redirect(Urls::USER_FRIENDS_PENDING_REQUEST);
                 } elseif ($Refused == 1) {
                     // update the requested friendship
                     $friendShip = FriendShipDao::getInstance()->get($friendShipId);
                     if ($friendShip) {
                         $friendShip->setAccepted(false);
                         $friendShip->setValidated(true);
                         FriendShipDao::getInstance()->update($friendShip);
                     }
                     // send email to the requesting user
                     MailSvc::getInstance()->send($friendShip->getUser_source()->getEmail(), __("Votre demande d'ami a été refusée", "s1b"), MailHelper::friendShipDenyEmailBody($user->getFirstName() . " " . $user->getLastName()));
                     // add a message in requesting user internal mailbox
                     $message = new Message();
                     $message->setDate(new \DateTime());
                     $message->setMessage($Message);
                     $message->setTitle($Title);
                     $message->setRecipient($friendShip->getUser_source());
                     $message->setSender($user);
                     MessageDao::getInstance()->add($message);
                     // redirect to pending request page
                     Flash::addItem(__("Demande refusée.", "s1b"));
                     HTTPHelper::redirect(Urls::USER_FRIENDS_PENDING_REQUEST);
                 }
             } else {
                 Flash::addItem(__("Vous devez sélectionner une demande d'ami.", "s1b"));
                 HTTPHelper::redirect(Urls::USER_FRIENDS_PENDING_REQUEST);
             }
         }
     } catch (\Exception $e) {
         Trace::addItem(sprintf("Une erreur s'est produite dans \"%s->%s\", TRACE : %s\"", get_class(), __FUNCTION__, $e->getTraceAsString()));
         $this->forward("error", "error", "default");
     }
 }
コード例 #2
0
 public function setAccepted($accepted)
 {
     $this->__load();
     return parent::setAccepted($accepted);
 }