public function submitAction() { try { $globalContext = new \Sb\Context\Model\Context(); if ($_REQUEST['LendingType'] == "NEW") { $userBookId = $_POST['ubid']; // getting userbook lent $userBook = UserBookDao::getInstance()->get($userBookId); $userBook->setLentOnce(true); // getting borrower userbook (new one) // checking if borrower alreday have the book $borrowerId = $_POST['BorrowerId']; $userBookBorrower = UserBookDao::getInstance()->getByBookIdAndUserId($borrowerId, $userBook->getBook()->getId()); // if not creating a new one if (!$userBookBorrower) { $userBookBorrower = new UserBook(); $userBookBorrower->setCreationDate(new \DateTime()); $userBookBorrower->setLastModificationDate(new \DateTime()); $userBookBorrower->setBook($userBook->getBook()); $borrower = UserDao::getInstance()->get($borrowerId); $userBookBorrower->setUser($borrower); } $userBookBorrower->setIs_deleted(false); // set is_deleted to false in case the borrower already had the book but deleted it in the past $userBookBorrower->setBorrowedOnce(true); // creating lending $lending = new Lending(); $lending->setUserbook($userBook); $lending->setBorrower_userbook($userBookBorrower); $lending->setStartDate(new \DateTime()); $lending->setCreationDate(new \DateTime()); $lending->setLastModificationDate(new \DateTime()); $lending->setState(LendingState::ACTIV); if (LendingDao::getInstance()->add($lending)) { Trace::addItem("Lending créé avec succès."); Flash::addItem(__("Les informations de prêt ont bien été mises à jour.", "s1b")); try { $userEvent = new UserEvent(); $userEvent->setNew_value($lending->getId()); $userEvent->setType_id(EventTypes::USER_LEND_USERBOOK); $userEvent->setUser($globalContext->getConnectedUser()); UserEventDao::getInstance()->add($userEvent); } catch (Exception $exc) { Trace::addItem("erreur lors de l'ajout de l'évènement suite au prêt : " . $exc->getMessages()); } } } else { // editing a lending -> ending it $lendingId = $_POST["LendingId"]; $lending = LendingDao::getInstance()->get($lendingId); if ($lending) { // Testing if the user editing the lending is either the lender or the borrower $canEditLending = false; if ($lending->getUserbook() && $lending->getUserbook()->getUser()->getId() == $globalContext->getConnectedUser()->getId()) { $canEditLending = true; } if ($lending->getBorrower_userbook() && $lending->getBorrower_userbook()->getUser()->getId() == $globalContext->getConnectedUser()->getId()) { $canEditLending = true; } if ($canEditLending) { $lending->setEndDate(new \DateTime()); // End date set to today $userIsLender = $lending->getUserbook() && $lending->getUserbook()->getUser()->getId() == $globalContext->getConnectedUser()->getId(); $userIsBorrower = $lending->getBorrower_userbook() && $lending->getBorrower_userbook()->getUser()->getId() == $globalContext->getConnectedUser()->getId(); $isBorrowedToGuest = $lending->getGuest(); if ($userIsLender) { $lending->setState(LendingState::IN_ACTIVE); // user is the lender, State set to IN_ACTIVE } elseif ($userIsBorrower) { if (!$isBorrowedToGuest) { $lending->setState(LendingState::WAITING_INACTIVATION); } else { $lending->setState(LendingState::IN_ACTIVE); } // user is the borrower but is borrowed to a guest, State set to IN_ACTIVE } $lending->setLastModificationDate(new \DateTime()); if (LendingDao::getInstance()->update($lending)) { // Send email to owner to remind him that he needs to validate the lending end if ($userIsBorrower && !$isBorrowedToGuest) { MailSvc::getInstance()->send($lending->getUserbook()->getUser()->getEmail(), __("Prêt en attente de retour de validation", "s1b"), $this->emailReturnValidationRequiredBody($lending->getUserbook()->getBook()->getTitle(), $lending->getBorrower_userbook()->getUser()->getUserName())); } Trace::addItem("Mise à jour (FIN) du lending correctement."); if ($userIsBorrower && !$isBorrowedToGuest) { Flash::addItem(__("Les informations de prêt ont bien été mises à jour mais le retour doit être validé par le prêteur.", "share1book")); } else { Flash::addItem(__("Les informations de prêt ont bien été mises à jour.", "s1b")); } } } } } HTTPHelper::redirectToLibrary(); } 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"); } }
public function setUserbook(\Sb\Db\Model\UserBook $userbook) { $this->__load(); return parent::setUserbook($userbook); }
public function borrowAction() { try { $globalContext = new \Sb\Context\Model\Context(); $idUserBook = $_GET['ubid']; if ($idUserBook) { $userBook = UserBookDao::getInstance()->get($idUserBook); if ($userBook) { $bookId = $userBook->getBook()->getId(); // We check that the userbook we want to bororow is really owned by a friend $userBookCheck = UserBookDao::getInstance()->getBookInFriendsUserBook($bookId, $globalContext->getConnectedUser()->getId()); if ($userBookCheck) { // We check if the book is owned by the user we want to borrow the book from if ($userBook->getIsOwned()) { // We check that the book is not currently lent (no lending or an inactive lending) if (!$userBook->getActiveLending()) { $existingUserBook = UserBookDao::getInstance()->getByBookIdAndUserId($globalContext->getConnectedUser()->getId(), $bookId); // We check that the connect user doesn't already have the book if ($existingUserBook) { // the user already had that book but had deleted it if ($existingUserBook->getIs_deleted()) { $newUserBook = $existingUserBook; $newUserBook->setIs_deleted(false); $newUserBook->setLastModificationDate(new \DateTime()); $newUserBook->setBorrowedOnce(true); $newUserBookPersisted = UserBookDao::getInstance()->update($newUserBook); Flash::addItem(__("Vous aviez déjà ce livre dans votre bibliothèque mais l'aviez supprimé.", "s1b")); } else { Flash::addItem(__("Vous avez déjà ce livre dans votre bibliothèque.", "s1b")); // Redirect to the main library page HTTPHelper::redirectToLibrary(); } } else { // We create a userbook for the connected user $newUserBook = new UserBook(); $newUserBook->setBook($userBook->getBook()); $newUserBook->setCreationDate(new \DateTime()); $newUserBook->setLastModificationDate(new \DateTime()); $newUserBook->setUser($globalContext->getConnectedUser()); $newUserBook->setBorrowedOnce(true); $newUserBookPersisted = UserBookDao::getInstance()->add($newUserBook); } if ($newUserBookPersisted) { // update lent userbook with Lent Once = 1 $userBook->setLentOnce(true); UserBookDao::getInstance()->update($userBook); // Lending line creation $lending = new Lending(); $lending->setUserbook($userBook); $lending->setBorrower_userbook($newUserBook); $lending->setStartDate(new \DateTime()); $lending->setState(LendingState::ACTIV); $lendingId = LendingDao::getInstance()->Add($lending); // if ok : prepare flash message if ($lendingId) { try { $userEvent = new UserEvent(); $userEvent->setNew_value($lending->getId()); $userEvent->setType_id(EventTypes::USER_BORROW_USERBOOK); $userEvent->setUser($globalContext->getConnectedUser()); UserEventDao::getInstance()->add($userEvent); } catch (Exception $exc) { Trace::addItem("erreur lors de l'ajout de l'évènement suite au prêt : " . $exc->getMessages()); } Flash::addItem(sprintf(__("Le livre %s a été emprunté à %s et ajouté à votre bibliothèque.", "sharebook"), $userBook->getBook()->getTitle(), $userBook->getUser()->getFirstName() . " " . $userBook->getUser()->getLastName())); } } } else { Flash::addItem(__("Ce livre fait l'objet d'un prêt en cours", "s1b")); } } else { Flash::addItem(__("Ce livre n'est pas possédé par l'utilisateur à qui vous tentez d'emprunter ce livre.", "s1b")); } } else { Flash::addItem(__("Vous n'êtes pas amis avec le propriétaire de ce livre.", "s1b")); } } else { Flash::addItem(__("Le livre que vous voulez emprunter n'existe pas dans la base.", "s1b")); } } // Redirect to the main library page HTTPHelper::redirectToLibrary(); } 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"); } }