public function IsValid()
 {
     $this->__load();
     return parent::IsValid();
 }
Esempio n. 2
0
 /**
  * Send invitation to users
  */
 public function inviteAction()
 {
     try {
         $globalContext = new \Sb\Context\Model\Context();
         $user = $globalContext->getConnectedUser();
         $continue = true;
         $emails = ArrayHelper::getSafeFromArray($_POST, 'Emails', null);
         $message = ArrayHelper::getSafeFromArray($_POST, 'Message', null);
         if (!$emails || !$message) {
             Flash::addItem(__("Vous devez renseigner tous les champs obligatoires.", "s1b"));
             $continue = false;
         }
         if ($continue) {
             $emailsListFromPost = explode(",", $emails);
             // Getting emails list
             if ($emailsListFromPost) {
                 // Looping through all emails for validating all of them
                 // At the end of the loop:
                 // we will have an array of emails to be processed
                 // and flag to process or not the sendings
                 $emailsList = array();
                 foreach ($emailsListFromPost as $emailToInvite) {
                     $addEmail = true;
                     $emailToInvite = strtolower(trim($emailToInvite));
                     if ($emailToInvite != "") {
                         // Testing if the email is valid
                         if (!StringHelper::isValidEmail($emailToInvite)) {
                             Flash::addItem(sprintf(__("%s n'est pas un email valide.", "s1b"), $emailToInvite));
                             // We will stop invitation sending
                             $continue = false;
                             // Current email not added to the array of emails to be processed
                             $addEmail = false;
                         } else {
                             // Testing if the email does not match an existing user
                             $userInDb = UserDao::getInstance()->getByEmail($emailToInvite);
                             if ($userInDb && !$userInDb->getDeleted()) {
                                 $friendRequestUrl = HTTPHelper::Link(Urls::USER_FRIENDS_REQUEST, array("fid" => $userInDb->getId()));
                                 Flash::addItem(sprintf(__("Un utilisateur existe déjà avec l'email : %s. <a class=\"link\" href=\"%s\">Envoyer lui une demande d'ami</a>", "s1b"), $emailToInvite, $friendRequestUrl));
                                 // We will stop invitation sending
                                 $continue = false;
                                 // Current email not added to the array of emails to be processed
                                 $addEmail = false;
                             } else {
                                 // Testing if invitations have been sent to that guest (email) by the current user
                                 $invitations = InvitationDao::getInstance()->getListForSenderAndGuestEmail($user, $emailToInvite);
                                 if ($invitations && count($invitations) > 0) {
                                     Flash::addItem(sprintf(__("Vous avez déjà envoyé une invitation à cet email : %s.", "s1b"), $emailToInvite));
                                     // We will stop invitation sending
                                     $continue = false;
                                     // Current email not added to the array of emails to be processed
                                     $addEmail = false;
                                 }
                             }
                         }
                     }
                     if ($addEmail) {
                         $emailsList[] = $emailToInvite;
                     }
                 }
                 if ($continue) {
                     // Looping through all emails for sending invitation
                     $initialMessage = $message;
                     foreach ($emailsList as $emailToInvite) {
                         $emailToInvite = strtolower(trim($emailToInvite));
                         if ($emailToInvite != "") {
                             $sendInvitation = true;
                             // Testing again if invitations have been sent to that guest (email) by the current user
                             $invitations = InvitationDao::getInstance()->getListForSenderAndGuestEmail($user, $emailToInvite);
                             if ($invitations && count($invitations) > 0) {
                                 $sendInvitation = false;
                             }
                             if ($sendInvitation) {
                                 // Getting existing guests matching email, and take first 1
                                 $guest = null;
                                 $guests = GuestDao::getInstance()->getListByEmail($emailToInvite);
                                 if ($guests && count($guests) > 0) {
                                     $guest = $guests[0];
                                 }
                                 $token = sha1(uniqid(rand()));
                                 // Send invite email
                                 $message = str_replace("\n", "<br/>", $initialMessage);
                                 $message .= "<br/><br/>";
                                 $message .= sprintf(__("<a href=\"%s\">S'inscrire</a> ou <a href=\"%s\">Refuser</a>", "s1b"), HTTPHelper::Link(Urls::SUBSCRIBE), HTTPHelper::Link(Urls::REFUSE_INVITATION, array("Token" => $token, "Email" => $emailToInvite)));
                                 $message .= "<br/><br/>";
                                 $message .= "<strong>" . sprintf(__("L'équipe %s", "s1b"), Constants::SITENAME) . "<strong/>";
                                 MailSvc::getNewInstance($user->getEmail(), $user->getEmail())->send($emailToInvite, sprintf(__("Invitation à rejoindre %s", "s1b"), Constants::SITENAME), $message);
                                 // Create invitation
                                 $invitation = new Invitation();
                                 $invitation->setSender($user);
                                 $invitation->setToken($token);
                                 $invitation->setLast_modification_date(new \DateTime());
                                 if ($guest) {
                                     // Updating guest
                                     $guest->addInvitations($invitation);
                                     GuestDao::getInstance()->update($guest);
                                 } else {
                                     // Create guest
                                     $guest = new Guest();
                                     $guest->setEmail($emailToInvite);
                                     $guest->setCreation_date(new \DateTime());
                                     $guest->addInvitations($invitation);
                                     GuestDao::getInstance()->add($guest);
                                 }
                                 Flash::addItem(sprintf(__("Une invitation a été envoyée à %s.", "s1b"), $emailToInvite));
                             }
                         }
                     }
                     HTTPHelper::redirectToHome();
                 }
             }
         }
         // If we arrive here : an error occured, then redirect to the invite form
         HTTPHelper::redirect(Urls::USER_FRIENDS_INVITE, array("emails" => $emails));
     } 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 borrowFromGuestAction()
 {
     try {
         $globalContext = new \Sb\Context\Model\Context();
         if ($this->validateUserInputForm()) {
             $bookForm = new BookForm($_POST);
             // testing if book can be found in db by id
             if ($bookForm->getId()) {
                 $bookInDb = BookDao::getInstance()->get($bookForm->getId());
             }
             // testing if book can be found in db by isbn10, isbn13, asin
             if (!$bookInDb) {
                 $bookInDb = BookDao::getInstance()->getOneByCodes($bookForm->getISBN10(), $bookForm->getISBN13(), $bookForm->getASIN());
             }
             // getting the book data from post and adding to db
             if (!$bookInDb) {
                 // Récupération du Book depuis le POST
                 $bookInDb = new Book();
                 BookMapper::map($bookInDb, $_POST, "book_");
                 // book not in db : need to add it
                 $bookInDb->setCreationDate(new \DateTime());
                 $bookInDb->setLastModificationDate(new \DateTime());
                 BookDao::getInstance()->add($bookInDb);
             }
             if ($bookInDb) {
                 $guestName = ArrayHelper::getSafeFromArray($_POST, "guest_name", null);
                 $guestEmail = ArrayHelper::getSafeFromArray($_POST, "guest_email", null);
                 $guest = new Guest();
                 $guest->setName($guestName);
                 $guest->setEmail($guestEmail);
                 $guest->setCreation_date(new \DateTime());
                 if ($guestEmail) {
                     $friendToBorrowInDb = UserDao::getInstance()->getByEmail($guestEmail);
                     if ($friendToBorrowInDb) {
                         Flash::addItem(sprintf(__("Un utilisateur existe déjà avec l'email que vous avez saisi. Nous vous proposons de lui envoyer une demande d'ami. Vous pourrez ensuite lui emprunter ce livre directement depuis sa bibliothèque. <a class=\"link\" href=\"%s\">Envoyer une demande d'ami</a>", "s1b"), HTTPHelper::Link(Urls::USER_FRIENDS_REQUEST, array("fid" => $friendToBorrowInDb->getId()))));
                         HTTPHelper::redirectToReferer();
                     } else {
                         $token = sha1(uniqid(rand()));
                         // Send invite email
                         $message = __(sprintf("%s vous invite à rejoindre %s, réseau communautaire autour du livre et de la lecture.", sprintf("%s %s", $globalContext->getConnectedUser()->getFirstName(), $globalContext->getConnectedUser()->getLastName()), $_SERVER["SERVER_NAME"]), "s1b");
                         $message .= "<br/><br/>";
                         $message .= sprintf(__("Il a utilisé %s pour noter qu'il vous a emprunté \"%s\"."), Constants::SITENAME, $bookInDb->getTitle());
                         $message .= "<br/><br/>";
                         $message .= __("Venez échanger sur vos lectures et coups de coeur, chercher l'inspiration grâce aux recommandations, gérer et partager votre bibliothèque avec vos amis et trouver dans leurs listes d'envies des idées de cadeaux.");
                         $message .= "<br/><br/>";
                         $subscriptionLink = HTTPHelper::Link(Urls::SUBSCRIBE);
                         $refuseInvitationLink = HTTPHelper::Link(Urls::REFUSE_INVITATION, array("Token" => $token, "Email" => $guestEmail));
                         $message .= sprintf(__("L'inscription est gratuite ! Rejoignez-nous... <a href=\"%s\">S'inscrire</a> ou <a href=\"%s\">Refuser l'invitation</a>"), $subscriptionLink, $refuseInvitationLink);
                         $message .= "<br/><br/>";
                         $message .= sprintf(__("<strong>L'équipe Cherbouquin</strong>", "s1b"), Constants::SITENAME);
                         MailSvc::getInstance()->send($guestEmail, sprintf(__("Invitation à rejoindre %s", "s1b"), Constants::SITENAME), $message);
                         // Create invitation in DB
                         $invitation = new Invitation();
                         $invitation->setSender($globalContext->getConnectedUser());
                         $invitation->setGuest($guest);
                         $invitation->setCreation_date(new \DateTime());
                         $invitation->setToken($token);
                         InvitationDao::getInstance()->add($invitation);
                         Flash::addItem(__("Un email d'invitation a été envoyé à votre ami.", "s1b"));
                     }
                 } else {
                     GuestDao::getInstance()->add($guest);
                 }
                 // Testing if the user has the book in his lib but marked as deleted
                 $userBookBorrower = UserBookDao::getInstance()->getByBookIdAndUserId($globalContext->getConnectedUser()->getId(), $bookInDb->getId());
                 if ($userBookBorrower && $userBookBorrower->getIs_deleted()) {
                     $userBookBorrower->setIs_deleted(false);
                     $userBookBorrower->setLastModificationDate(new \DateTime());
                     UserBookDao::getInstance()->update($userBookBorrower);
                     Flash::addItem(sprintf(__("Vous aviez déjà le livre \"%s\" dans votre bibliothèque mais l'aviez supprimé. Il a été rajouté.", "s1b"), $bookInDb->getTitle()));
                 } else {
                     // Create userbook for connected user
                     $userBookBorrower = new UserBook();
                     $userBookBorrower->setUser($globalContext->getConnectedUser());
                     $userBookBorrower->setBook($bookInDb);
                     $userBookBorrower->setCreationDate(new \DateTime());
                     $userBookBorrower->setBorrowedOnce(true);
                     UserBookDao::getInstance()->add($userBookBorrower);
                     Flash::addItem(__("Le livre a été ajouté à votre bibliothèque.", "s1b"));
                 }
                 $lending = new Lending();
                 $lending->setBorrower_userbook($userBookBorrower);
                 $lending->setGuest($guest);
                 $lending->setCreationDate(new \DateTime());
                 $lending->setState(LendingState::ACTIV);
                 $lending->setStartDate(new \DateTime());
                 LendingDao::getInstance()->add($lending);
             }
             HTTPHelper::redirectToLibrary();
         } else {
             HTTPHelper::redirectToReferer();
         }
     } 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");
     }
 }