Exemplo n.º 1
0
 /**
  * Validate the form
  */
 private function validateForm()
 {
     // get settings
     $subscriptionsAllowed = isset($this->settings['allow_subscriptions']) && $this->settings['allow_subscriptions'];
     // subscriptions aren't allowed so we don't have to validate
     if (!$subscriptionsAllowed) {
         return false;
     }
     // is the form submitted
     if ($this->frm->isSubmitted()) {
         // cleanup the submitted fields, ignore fields that were added by hackers
         $this->frm->cleanupFields();
         // does the key exists?
         if (\SpoonSession::exists('agenda_subscription_' . $this->record['id'])) {
             // calculate difference
             $diff = time() - (int) \SpoonSession::get('agenda_subscription_' . $this->record['id']);
             // calculate difference, it it isn't 10 seconds the we tell the user to slow down
             if ($diff < 10 && $diff != 0) {
                 $this->frm->getField('message')->addError(FL::err('CommentTimeout'));
             }
         }
         // validate required fields
         $this->frm->getField('name')->isFilled(FL::err('NameIsRequired'));
         $this->frm->getField('email')->isEmail(FL::err('EmailIsRequired'));
         // no errors?
         if ($this->frm->isCorrect()) {
             // get module setting
             $moderationEnabled = isset($this->settings['moderation']) && $this->settings['moderation'];
             // reformat data
             $name = $this->frm->getField('name')->getValue();
             $email = $this->frm->getField('email')->getValue();
             // build array
             $subscription['agenda_id'] = $this->record['id'];
             $subscription['language'] = FRONTEND_LANGUAGE;
             $subscription['created_on'] = FrontendModel::getUTCDate();
             $subscription['name'] = $name;
             $subscription['email'] = $email;
             $subscription['status'] = 'subscribed';
             // get URL for article
             $permaLink = $this->record['full_url'];
             $redirectLink = $permaLink;
             // is moderation enabled
             if ($moderationEnabled) {
                 // if the commenter isn't moderated before alter the subscription status so it will appear in the moderation queue
                 if (!FrontendAgendaModel::isModerated($name, $email)) {
                     $subscription['status'] = 'moderation';
                 }
             }
             // insert comment
             $subscription['id'] = FrontendAgendaModel::insertSubscription($subscription);
             // trigger event
             FrontendModel::triggerEvent('agenda', 'after_add_subscription', array('subscription' => $subscription));
             // append a parameter to the URL so we can show moderation
             if (strpos($redirectLink, '?') === false) {
                 if ($subscription['status'] == 'moderation') {
                     $redirectLink .= '?subscription=moderation#' . FL::act('Subscribe');
                 }
                 if ($subscription['status'] == 'subscribed') {
                     $redirectLink .= '?subscription=true#subscription-' . $subscription['id'];
                 }
             } else {
                 if ($subscription['status'] == 'moderation') {
                     $redirectLink .= '&subscription=moderation#' . FL::act('Subscribe');
                 }
                 if ($subscription['status'] == 'subscribed') {
                     $redirectLink .= '&subscription=true#comment-' . $subscription['id'];
                 }
             }
             // set title
             $subscription['agenda_title'] = $this->record['title'];
             $subscription['agenda_url'] = $this->record['url'];
             // notify the admin
             FrontendAgendaModel::notifyAdmin($subscription);
             // store timestamp in session so we can block excessive usage
             \SpoonSession::set('agenda_subscription_' . $this->record['id'], time());
             // store author-data in cookies
             try {
                 Cookie::set('subscription_author', $name);
                 Cookie::set('subscription_email', $email);
             } catch (Exception $e) {
                 // settings cookies isn't allowed, but because this isn't a real problem we ignore the exception
             }
             // redirect
             $this->redirect($redirectLink);
         }
     }
 }