示例#1
0
 public function prepareUserBookEvents(UserBook $oldUserBook, UserBookForm $newUserBook)
 {
     $userEvents = array();
     try {
         if ($oldUserBook->getRating() != $newUserBook->getRating()) {
             $userEvent = new \Sb\Db\Model\UserEvent();
             $userEvent->setItem_id($oldUserBook->getId());
             $userEvent->setUser($oldUserBook->getUser());
             $userEvent->setNew_value($newUserBook->getRating());
             $userEvent->setOld_value($oldUserBook->getRating());
             $userEvent->setType_id(\Sb\Entity\EventTypes::USERBOOK_RATING_CHANGE);
             $userEvents[] = $userEvent;
         }
         if ($oldUserBook->getIsBlowOfHeart() != $newUserBook->getIsBlowOfHeart()) {
             $userEvent = new \Sb\Db\Model\UserEvent();
             $userEvent->setItem_id($oldUserBook->getId());
             $userEvent->setUser($oldUserBook->getUser());
             $userEvent->setNew_value($newUserBook->getIsBlowOfHeart());
             $userEvent->setOld_value($oldUserBook->getIsBlowOfHeart());
             $userEvent->setType_id(\Sb\Entity\EventTypes::USERBOOK_BLOWOFHEART_CHANGE);
             $userEvents[] = $userEvent;
         }
         if ($oldUserBook->getHyperlink() != $newUserBook->getHyperLink()) {
             $userEvent = new \Sb\Db\Model\UserEvent();
             $userEvent->setItem_id($oldUserBook->getId());
             $userEvent->setUser($oldUserBook->getUser());
             // Removing http:// or https:// from url
             $newHyperlink = str_replace("http://", "", $newUserBook->getHyperLink());
             $newHyperlink = str_replace("https://", "", $newHyperlink);
             $userEvent->setNew_value($newHyperlink);
             $userEvent->setOld_value($oldUserBook->getHyperlink());
             $userEvent->setType_id(\Sb\Entity\EventTypes::USERBOOK_HYPERLINK_CHANGE);
             $userEvents[] = $userEvent;
         }
         $oldReadingStateId = $oldUserBook->getReadingState() ? $oldUserBook->getReadingState()->getId() : -1;
         if ($oldReadingStateId != $newUserBook->getReadingStateId()) {
             $userEvent = new \Sb\Db\Model\UserEvent();
             $userEvent->setItem_id($oldUserBook->getId());
             $userEvent->setUser($oldUserBook->getUser());
             $userEvent->setNew_value($newUserBook->getReadingStateId());
             $userEvent->setOld_value($oldUserBook->getReadingState() ? $oldUserBook->getReadingState()->getId() : null);
             $userEvent->setType_id(\Sb\Entity\EventTypes::USERBOOK_READINGSTATE_CHANGE);
             $userEvents[] = $userEvent;
         }
         if ($oldUserBook->getReview() != $newUserBook->getReview()) {
             $userEvent = new \Sb\Db\Model\UserEvent();
             $userEvent->setItem_id($oldUserBook->getId());
             $userEvent->setUser($oldUserBook->getUser());
             $userEvent->setNew_value($newUserBook->getReview());
             $userEvent->setOld_value($oldUserBook->getReview());
             $userEvent->setType_id(\Sb\Entity\EventTypes::USERBOOK_REVIEW_CHANGE);
             $userEvents[] = $userEvent;
         }
         if ($oldUserBook->getIsWished() != $newUserBook->getIsWished()) {
             $userEvent = new \Sb\Db\Model\UserEvent();
             $userEvent->setItem_id($oldUserBook->getId());
             $userEvent->setUser($oldUserBook->getUser());
             $userEvent->setNew_value($newUserBook->getIsWished());
             $userEvent->setOld_value($oldUserBook->getIsWished());
             $userEvent->setType_id(\Sb\Entity\EventTypes::USERBOOK_WISHEDSTATE_CHANGE);
             $userEvents[] = $userEvent;
         }
     } catch (\Exception $exc) {
         Trace::addItem("Une erreur s'est produite lors de la préparation des événements liés au userbook");
     }
     return $userEvents;
 }
 public function submitAction()
 {
     try {
         /* @var $globalContext \Sb\Context\Model\Context */
         $globalContext = new \Sb\Context\Model\Context();
         /* @var $user Sb\Db\Model\User */
         $user = $globalContext->getConnectedUser();
         // getting form data
         $userBookForm = new UserBookForm($_POST);
         // getting userbook in DB
         $userBook = UserBookDao::getInstance()->get($userBookForm->getId());
         // Getting the events related to the userbook changes
         $userEvents = UserEventSvc::getInstance()->prepareUserBookEvents($userBook, $userBookForm);
         // On vérifit la correspondance du user
         if ($user->getId() != $userBook->getUser()->getId()) {
             Flash::addItem(__("Le livre que vous souhaitez éditer ne correspond pas à l'utilisateur connecté.", "share1book"));
             HTTPHelper::redirectToLibrary();
         }
         // updating userbook members
         $userBook->setReview($userBookForm->getReview());
         $userBook->setIsBlowOfHeart($userBookForm->getIsBlowOfHeart());
         $userBook->setIsOwned($userBookForm->getIsOwned());
         $userBook->setIsWished($userBookForm->getIsWished());
         $userBook->setRating($userBookForm->getRating());
         $userBook->setNb_of_pages($userBookForm->getNb_of_pages());
         $userBook->setNb_of_pages_read($userBookForm->getNb_of_pages_read());
         $readingState = ReadingStateDao::getInstance()->get($userBookForm->getReadingStateId());
         if ($userBookForm->getReadingDate()) {
             $userBook->setReadingDate($userBookForm->getReadingDate());
         }
         $userBook->setReadingState($readingState);
         $userBook->setHyperlink($userBookForm->getHyperLink());
         if ($userBookForm->getTags()) {
             $tags = new \Doctrine\Common\Collections\ArrayCollection();
             foreach ($userBookForm->getTags() as $tagId) {
                 $tag = TagDao::getInstance()->get($tagId);
                 $tags->add($tag);
             }
             $userBook->setTags($tags);
         }
         // Mise à jour du UserBook
         if (UserBookDao::getInstance()->update($userBook)) {
             // persisting the userevent related to the userbook changes
             UserEventSvc::getInstance()->persistAll($userEvents);
             // Add review as chronicle
             if ($this->_reviewIsModified($userEvents) && $user->IsBlogger()) {
                 ChronicleSvc::getInstance()->addOrUpdateFromUserBook($userBook);
             }
             Flash::addItem(sprintf(__('Le livre "%s" a été mis à jour.', "s1b"), urldecode($userBook->getBook()->getTitle())));
         } else {
             Flash::addItem(__('Une erreur s\'est produite lors de la mise à jour de votre fiche de lecture', 's1b'));
         }
         $referer = ArrayHelper::getSafeFromArray($_POST, "referer", null);
         if ($referer) {
             HTTPHelper::redirectToUrl($referer);
         } else {
             HTTPHelper::redirectToLibrary();
         }
     } catch (\Exception $e) {
         Trace::addItem(sprintf("Une erreur s'est produite dans \"%s->%s\", MESSAGE : %s , TRACE : %s\"", get_class(), __FUNCTION__, $e->getMessage(), $e->getTraceAsString()));
         $this->forward("error", "error", "default");
     }
 }