private function addUserBook(\Sb\Db\Model\Book $book, \Sb\Db\Model\User $user, \Sb\Config\Model\Config $config) { $userBookDao = UserBookDao::getInstance(); $userBook = $userBookDao->getByBookIdAndUserId($user->getId(), $book->getId()); // Testing if the user : // - doesn't already have that book or // - have it but is deleted : in this case we will undelete the book if ($userBook && !$userBook->getIs_deleted()) { $returnMsg = __("Vous avez déjà ce livre dans votre bibliothèque.", "s1b"); } else { // Getting current user current nb userbooks in libary $userNbUserBooks = count($user->getNotDeletedUserBooks()); if ($userNbUserBooks >= $config->getMaximumNbUserBooksForPublic()) { $returnMsg = sprintf(__("Vous ne pouvez pas avoir plus de %s livres dans votre bibliothèque.", "s1b"), $config->getMaximumNbUserBooksForPublic()); } else { // Ajout du UserBook $existingUserBook = false; $userBook = UserBookDao::getInstance()->getByBookIdAndUserId($user->getId(), $book->getId()); // testing if the user already had the book but deleted it : // if yes, then the userbook is undeleted if ($userBook && $userBook->getIs_deleted()) { $userBook->setIs_deleted(false); $existingUserBook = true; } else { // Création du UserBoook $userBook = new \Sb\Db\Model\UserBook(); $userBook->setCreationDate(new \DateTime()); } // Updating userbook data $userBook->setLastModificationDate(new \DateTime()); $userBook->setUser($user); $userBook->setBook($book); $bookLink = \Sb\Helpers\HTTPHelper::Link($book->getLink()); // Persisting userbook in DB $addOk = false; if ($existingUserBook) { if (UserBookDao::getInstance()->update($userBook)) { $editUserBookLink = \Sb\Helpers\HTTPHelper::Link(\Sb\Entity\Urls::USER_BOOK_EDIT, array("ubid" => $userBook->getId())); $returnMsg = sprintf(__("Vous aviez déjà le livre \"%s\" dans votre bibliothèque mais l'aviez supprimé. Il a été rajouté.<br/><a class=\"link\" href=\"%s\">Remplir votre fiche de lecture</a> ou <a class=\"link\" href=\"%s\">Voir ce livre</a>", "s1b"), $book->getTitle(), $editUserBookLink, $bookLink); $addOk = true; } } else { if (UserBookDao::getInstance()->add($userBook)) { $editUserBookLink = \Sb\Helpers\HTTPHelper::Link(\Sb\Entity\Urls::USER_BOOK_EDIT, array("ubid" => $userBook->getId())); $returnMsg = sprintf(__("Le livre \"%s\" a été ajouté à votre bibliothèque.<br/><a class=\"link\" href=\"%s\">Remplir votre fiche de lecture</a> ou <a class=\"link\" href=\"%s\">Voir ce livre</a>", "s1b"), $book->getTitle(), $editUserBookLink, $bookLink); $addOk = true; } } if ($addOk) { try { $userEvent = new \Sb\Db\Model\UserEvent(); $userEvent->setItem_id($userBook->getId()); $userEvent->setType_id(\Sb\Entity\EventTypes::USERBOOK_ADD); $userEvent->setUser($user); \Sb\Db\Dao\UserEventDao::getInstance()->add($userEvent); } catch (\Exception $exc) { Trace::addItem("Une erreur s'est produite lors de l'ajout de l'événement suite à l'ajout d'un livre " . $exc->getMessage()); } } } } return $returnMsg; }
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; }