public function savepasswordAction()
 {
     if ($datas = $this->getRequest()->getPost()) {
         $html = '';
         try {
             $isNew = true;
             $password = new LoyaltyCard_Model_Password();
             $application = $this->getApplication();
             $password_id = $datas['password_id'];
             if (!empty($datas['password_id'])) {
                 $password->find($datas['password_id']);
                 if ($password->getAppId() != $application->getId()) {
                     throw new Exception($this->_("An error occurred while saving the password. Please try again later"));
                 }
                 $isNew = false;
             } else {
                 $datas['app_id'] = $application->getId();
             }
             if (empty($datas['is_deleted'])) {
                 if (empty($datas['name'])) {
                     throw new Exception($this->_('Please enter a name'));
                 }
                 if (empty($datas['password'])) {
                     throw new Exception($this->_('Please enter a password'));
                 }
                 if (strlen($datas['password']) < 4 or !ctype_digit($datas['password'])) {
                     throw new Exception($this->_('Your password must be 4 digits'));
                 }
                 $password->setPassword(sha1($datas['password']));
                 if ($datas['password']) {
                     unset($datas['password']);
                 }
             } else {
                 if (!$password->getId()) {
                     throw new Exception($this->_('An error occurred while saving the password. Please try again later.'));
                 }
             }
             $password->addData($datas)->save();
             $html = array('success' => 1, 'id' => $password->getId());
             if (!empty($datas['is_deleted'])) {
                 $html['is_deleted'] = 1;
                 $html['id'] = $password_id;
             } else {
                 if ($isNew) {
                     $html['is_new'] = 1;
                     $html['name'] = $password->getName();
                 }
             }
             //                }
             //                else {
             //                    $employee->delete();
             //                    $html = array();
             //                }
         } catch (Exception $e) {
             $html = array('message' => $e->getMessage());
         }
         $this->getLayout()->setHtml(Zend_Json::encode($html));
     }
 }
Ejemplo n.º 2
0
 public function validateAction()
 {
     try {
         $html = array();
         if ($datas = Zend_Json::decode($this->getRequest()->getRawBody())) {
             $application_id = $this->getApplication()->getAppId();
             // Récupération de l'option_value en cours
             $option_value = $this->getCurrentOptionValue();
             // Récupération du client en cours
             $customer_id = $this->getSession()->getCustomerId();
             // Si le client n'est pas connecté
             if (empty($customer_id)) {
                 throw new Exception($this->_('You must be logged in to validate points'));
             }
             $customer_card_id = $datas['customer_card_id'];
             // Récupération de la carte de fidélité de l'utilisateur en cours
             $card = new LoyaltyCard_Model_Customer();
             // Récupère la carte du client ou, à défaut, en créé une nouvelle
             $cards = $card->findAllByOptionValue($option_value->getId(), $customer_id);
             foreach ($cards as $tmp_card) {
                 // Si la carte n'existe pas, customer_card_id == 0
                 if ($tmp_card->getCustomerCardId() == $customer_card_id) {
                     $card = $tmp_card;
                 }
             }
             // Déclaration des variables annexes
             $password_entered = $datas['password'];
             $nbr = !empty($datas['number_of_points']) ? $datas['number_of_points'] : 1;
             // Ou si le mot de passe est vide ou non numérique
             if ($card->getValueId() != $option_value->getId() or empty($password_entered) or !preg_match('/[0-9]/', $nbr)) {
                 throw new Exception($this->_('An error occurred while validating point. Please try again later.'));
             }
             // Récupération du mot de passe
             $password = new LoyaltyCard_Model_Password();
             $password->findByPassword($password_entered, $application_id);
             // Test si le mot de passe a été trouvé
             if (!$password->getId()) {
                 // Ajoute une erreur à la carte en cours
                 $card->addError();
                 // Calcul le nombre de tentative restante
                 $only = 3 - $card->getNumberOfError();
                 // S'il reste au moins 1 tentative de saisie, on envoie un message d'erreur
                 if ($only > 0) {
                     $html['customer_card_id'] = $card->getCustomerCardId();
                     throw new Exception($this->_('Wrong password. Be carefull! %d remaining attempt%s before locking your card. Ask the store person for validating your point', $only, $only > 1 ? 's' : ''));
                 } else {
                     // Sinon, on ferme le clavier et on annonce que la carte est bloquée
                     $html['close_pad'] = true;
                     $html['card_is_locked'] = true;
                     throw new Exception($this->_('You have exceeded the number of attempts to validate points. Your card is locked for 24h'));
                 }
             } else {
                 // S'il reste des points à valider
                 if ($card->getNumberOfPoints() < $card->getMaxNumberOfPoints()) {
                     // On met à jour la carte de fidélité du client
                     $card->setNumberOfPoints($card->getNumberOfPoints() + $nbr)->setCustomerId($this->getSession()->getCustomerId())->setNumberOfError(0)->setLastError(null)->save();
                     // On log l'employé ayant validé le ou les points
                     $card->createLog($password->getId(), $nbr);
                     // On renvoie un message de validation
                     $s = $nbr > 1 ? 's' : '';
                     $msg = $this->_('Point%s successfully validated', $s, $s);
                     $html = array('success' => true, 'message' => $msg, 'close_pad' => true, 'number_of_points' => $card->getNumberOfPoints());
                 } else {
                     $card->setIsUsed(1)->setUsedAt($card->formatDate(null, 'y-MM-dd HH:mm:ss'))->setValidateBy($password->getId())->save();
                     $html = array('success' => true, 'message' => $this->_('You just finished your card'), 'promotion_id_to_remove' => $card->getId(), 'close_pad' => true);
                 }
                 if ($this->getSession()->getCustomer()->canPostSocialMessage()) {
                     $html['canPostMessage'] = true;
                 }
             }
         }
     } catch (Exception $e) {
         $html['error'] = 1;
         $html['message'] = $e->getMessage();
     }
     $this->_sendHtml($html);
 }