Exemplo n.º 1
0
 /**
  * Validate the form
  */
 protected function validateForm()
 {
     if ($this->frm->isSubmitted()) {
         $this->frm->cleanupFields();
         // validation
         $fields = $this->frm->getFields();
         $fields['title']->isFilled(BL::err('FieldIsRequired'));
         $fields['begin_date_date']->isFilled(BL::err('FieldIsRequired'));
         $fields['begin_date_time']->isFilled(BL::err('FieldIsRequired'));
         $fields['begin_date_date']->isValid(BL::err('DateIsInvalid'));
         $fields['begin_date_time']->isValid(BL::err('TimeIsInvalid'));
         $fields['end_date_date']->isFilled(BL::err('FieldIsRequired'));
         $fields['end_date_time']->isFilled(BL::err('FieldIsRequired'));
         $fields['end_date_date']->isValid(BL::err('DateIsInvalid'));
         $fields['end_date_time']->isValid(BL::err('TimeIsInvalid'));
         $fields['category_id']->isFilled(BL::err('FieldIsRequired'));
         if ($fields['price']->isFilled()) {
             $fields['price']->isPrice(BL::err('InvalidValue'));
         }
         // validate meta
         $this->meta->validate();
         $this->media->validate();
         if ($this->frm->isCorrect()) {
             $item['id'] = $this->id;
             $item['language'] = BL::getWorkingLanguage();
             $item['title'] = $fields['title']->getValue();
             $item['text'] = $fields['text']->getValue();
             $item['introduction'] = $fields['introduction']->getValue();
             $item['begin_date'] = BackendModel::getUTCDate(null, BackendModel::getUTCTimestamp($this->frm->getField('begin_date_date'), $this->frm->getField('begin_date_time')));
             $item['end_date'] = BackendModel::getUTCDate(null, BackendModel::getUTCTimestamp($this->frm->getField('end_date_date'), $this->frm->getField('end_date_time')));
             $item['category_id'] = $this->frm->getField('category_id')->getValue();
             $item['price'] = $fields['price']->getValue();
             $item['whole_day'] = $fields['whole_day']->getChecked() ? 'Y' : 'N';
             $item['recurring'] = $fields['recurring']->getChecked() ? 'Y' : 'N';
             $item['allow_subscriptions'] = $fields['subscriptions']->getValue();
             $item['google_maps'] = $fields['google_maps']->getChecked() ? 'Y' : 'N';
             $item['location_name'] = $fields['name']->getValue();
             $item['street'] = $fields['street']->getValue();
             $item['number'] = $fields['number']->getValue();
             $item['zip'] = $fields['zip']->getValue();
             $item['city'] = $fields['city']->getValue();
             $item['country'] = $fields['country']->getValue();
             $item['meta_id'] = $this->meta->save();
             // geocode address
             $url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($item['street'] . ' ' . $item['number'] . ', ' . $item['zip'] . ' ' . $item['city'] . ', ' . \SpoonLocale::getCountry($item['country'], BL::getWorkingLanguage())) . '&sensor=false';
             $geocode = json_decode(\SpoonHTTP::getContent($url));
             $item['lat'] = isset($geocode->results[0]->geometry->location->lat) ? $geocode->results[0]->geometry->location->lat : null;
             $item['lng'] = isset($geocode->results[0]->geometry->location->lng) ? $geocode->results[0]->geometry->location->lng : null;
             // update item
             BackendAgendaModel::update($item);
             $item['id'] = $this->id;
             // recurring item
             if ($item['recurring'] == 'Y') {
                 $recurringItem['id'] = $this->recurringOptions['id'];
                 $recurringItem['agenda_id'] = $item['id'];
                 $recurringItem['type'] = $fields['type']->getValue();
                 $recurringItem['interval'] = $fields['interval']->getValue();
                 $recurringItem['ends_on'] = $fields['ends_on']->getValue();
                 // if recurring type is weekly, get days checked
                 if ($recurringItem['type'] == 1) {
                     $days = $fields['days']->getChecked();
                     $recurringItem['days'] = implode(",", $days);
                 }
                 // if item ends on x amount of times
                 if ($recurringItem['ends_on'] == 1) {
                     $recurringItem['frequency'] = $fields['frequency']->getValue();
                 } else {
                     if ($recurringItem['ends_on'] == 2) {
                         // item ends on specific date
                         // check date/time fields
                         if ($fields['recurr_end_date_date']->isFilled() || $fields['recurr_end_date_time']->isFilled()) {
                             $recurringItem['end_date'] = BackendModel::getUTCDate(null, BackendModel::getUTCTimestamp($this->frm->getField('recurr_end_date_date'), $this->frm->getField('recurr_end_date_time')));
                         }
                     }
                 }
                 // update if options exist
                 if (BackendAgendaModel::existsRecurringOptions($recurringItem['id'], $recurringItem['agenda_id'])) {
                     BackendAgendaModel::updateRecurringOptions($recurringItem);
                 } else {
                     // insert new options
                     BackendAgendaModel::insertRecurringOptions($recurringItem);
                 }
             }
             // add search index
             BackendSearchModel::saveIndex($this->getModule(), $item['id'], array('title' => $item['title'], 'Text' => $item['text']));
             BackendModel::triggerEvent($this->getModule(), 'after_edit', $item);
             $this->redirect(BackendModel::createURLForAction('index') . '&report=edited&highlight=row-' . $item['id']);
         }
     }
 }