Example #1
0
 /**
  * Execute the action
  */
 public function execute()
 {
     $this->id = $this->getParameter('id', 'int');
     // does the item exist
     if ($this->id !== null && BackendAddressesModel::exists($this->id)) {
         parent::execute();
         $this->record = (array) BackendAddressesModel::get($this->id);
         BackendAddressesModel::delete($this->id);
         BackendAddressesModel::deleteGroupsFromAddress($this->id);
         // delete the image
         \SpoonFile::delete(FRONTEND_FILES_PATH . '/Addresses/Images/Source/' . $this->record['image']);
         BackendSearchModel::removeIndex($this->getModule(), $this->id);
         BackendModel::triggerEvent($this->getModule(), 'after_delete', array('id' => $this->id));
         $this->redirect(BackendModel::createURLForAction('index') . '&report=deleted&var=' . urlencode($this->record['id']));
     } else {
         $this->redirect(BackendModel::createURLForAction('index') . '&error=non-existing');
     }
 }
Example #2
0
 /**
  * Validate the form
  */
 protected function validateForm()
 {
     if ($this->frm->isSubmitted()) {
         $this->frm->cleanupFields();
         // validation
         $fields = $this->frm->getFields();
         //			$fields['name']->isFilled(BL::err('FieldIsRequired'));
         $this->meta->validate();
         if ($this->frm->isCorrect()) {
             $item['meta_id'] = $this->meta->save();
             $item['company'] = $fields['company']->getValue();
             $item['name'] = $fields['name']->getValue();
             $item['firstname'] = $fields['firstname']->getValue();
             $item['email'] = $fields['email']->getValue();
             $item['address'] = $fields['address']->getValue();
             $item['zipcode'] = $fields['zipcode']->getValue();
             $item['city'] = $fields['city']->getValue();
             $item['country'] = $fields['country']->getValue();
             $item['phone'] = $fields['phone']->getValue();
             $item['fax'] = $fields['fax']->getValue();
             $item['website'] = str_replace("http://", "", $fields['website']->getValue());
             $item['zipcodes'] = $fields['zipcodes']->getValue();
             $item['remark'] = $fields['remark']->getValue();
             //$item['text'] = $fields['text']->getValue();
             //$item['assort'] = $fields['assort']->getValue();
             //$item['open'] = $fields['open']->getValue();
             //$item['closed'] = $fields['closed']->getValue();
             //$item['visit'] = $fields['visit']->getValue();
             //$item['size'] = $fields['size']->getValue();
             $item['language'] = BL::getWorkingLanguage();
             $item['hidden'] = $fields['hidden']->getValue();
             if ($item['country'] == '') {
                 $item['country'] = 'BE';
             }
             //--Create url
             $url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($item['address'] . ', ' . $item['zipcode'] . ' ' . $item['city'] . ', ' . \SpoonLocale::getCountry($item['country'], BL::getWorkingLanguage())) . '&sensor=false';
             //--Get lat
             $geocode = json_decode(\SpoonHTTP::getContent($url));
             //--Sleep between the requests
             sleep(0.05);
             //--Check result
             $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;
             $item['image'] = $this->record['image'];
             // the image path
             $imagePath = FRONTEND_FILES_PATH . '/Addresses/Images';
             // create folders if needed
             if (!\SpoonDirectory::exists($imagePath . '/Source')) {
                 \SpoonDirectory::create($imagePath . '/Source');
             }
             if (!\SpoonDirectory::exists($imagePath . '/128x128')) {
                 \SpoonDirectory::create($imagePath . '/128x128');
             }
             if (!\SpoonDirectory::exists($imagePath . '/400x300')) {
                 \SpoonDirectory::create($imagePath . '/400x300');
             }
             if (!\SpoonDirectory::exists($imagePath . '/800x')) {
                 \SpoonDirectory::create($imagePath . '/800x');
             }
             // if the image should be deleted
             if ($this->frm->getField('delete_image')->isChecked()) {
                 // delete the image
                 \SpoonFile::delete($imagePath . '/Source/' . $item['image']);
                 // reset the name
                 $item['image'] = null;
             }
             // new image given?
             if ($this->frm->getField('image')->isFilled()) {
                 // delete the old image
                 \SpoonFile::delete($imagePath . '/Source/' . $this->record['image']);
                 // build the image name
                 $item['image'] = $this->meta->getURL() . '.' . $this->frm->getField('image')->getExtension();
                 // upload the image & generate thumbnails
                 $this->frm->getField('image')->generateThumbnails($imagePath, $item['image']);
             } elseif ($item['image'] != null) {
                 // get the old file extension
                 $imageExtension = \SpoonFile::getExtension($imagePath . '/Source/' . $item['image']);
                 // get the new image name
                 $newName = $this->meta->getURL() . '.' . $imageExtension;
                 // only change the name if there is a difference
                 if ($newName != $item['image']) {
                     // loop folders
                     foreach (BackendModel::getThumbnailFolders($imagePath, true) as $folder) {
                         // move the old file to the new name
                         \SpoonFile::move($folder['path'] . '/' . $item['image'], $folder['path'] . '/' . $newName);
                     }
                     // assign the new name to the database
                     $item['image'] = $newName;
                 }
             }
             BackendAddressesModel::update($this->id, $item);
             $item['id'] = $this->id;
             //--Add the languages
             foreach ((array) BackendModel::get('fork.settings')->get('Core', 'languages') as $key => $language) {
                 $itemLanguage = array();
                 $itemLanguage['id'] = $item['id'];
                 $itemLanguage['language'] = $language;
                 $itemLanguage['text'] = $this->frm->getField('text_' . $language)->getValue();
                 $itemLanguage['opening_hours'] = $this->frm->getField('opening_hours_' . $language)->getValue();
                 BackendAddressesModel::updateLanguage($itemLanguage);
             }
             if (isset($fields["groups"])) {
                 //--Get all the groups
                 $groups = $fields["groups"]->getValue();
                 BackendAddressesModel::deleteGroupsFromAddress($item['id']);
                 foreach ($groups as $value) {
                     $groupAddress = array();
                     $groupAddress["address_id"] = $item['id'];
                     $groupAddress["group_id"] = $value;
                     //--Add user to the group
                     BackendAddressesModel::insertAddressToGroup($groupAddress);
                 }
             }
             BackendSearchModel::saveIndex($this->getModule(), $item['id'], array('title' => $item['name'], 'text' => $item['name']));
             BackendModel::triggerEvent($this->getModule(), 'after_edit', $item);
             $this->redirect(BackendModel::createURLForAction('index') . '&report=edited&highlight=row-' . $item['id']);
         }
     }
 }