コード例 #1
0
 /**
  * Edit advertisement: Update
  *
  * All post fields are validated
  */
 public function action_update()
 {
     //template header
     $this->template->title = __('Edit advertisement');
     $this->template->meta_description = __('Edit advertisement');
     Controller::$full_width = TRUE;
     //local files
     if (Theme::get('cdn_files') == FALSE) {
         $this->template->styles = array('css/jquery.sceditor.default.theme.min.css' => 'screen', '//cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/css/selectize.bootstrap3.min.css' => 'screen', '//cdn.jsdelivr.net/sweetalert/1.1.3/sweetalert.css' => 'screen');
         $this->template->scripts['footer'] = array('js/jquery.sceditor.bbcode.min.js', '//maps.google.com/maps/api/js?sensor=false&libraries=geometry&v=3.7', '//cdn.jsdelivr.net/gmaps/0.4.15/gmaps.min.js', '//cdn.jsdelivr.net/sweetalert/1.1.3/sweetalert.min.js', '//cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/js/standalone/selectize.min.js', 'js/canvasResize.js', 'js/oc-panel/edit_ad.js');
     } else {
         $this->template->styles = array('css/jquery.sceditor.default.theme.min.css' => 'screen', '//cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/css/selectize.bootstrap3.min.css' => 'screen', '//cdn.jsdelivr.net/sweetalert/1.1.3/sweetalert.css' => 'screen');
         $this->template->scripts['footer'] = array('js/jquery.sceditor.bbcode.min.js', '//maps.google.com/maps/api/js?sensor=false&libraries=geometry&v=3.7', '//cdn.jsdelivr.net/gmaps/0.4.15/gmaps.min.js', '//cdn.jsdelivr.net/sweetalert/1.1.3/sweetalert.min.js', '//cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/js/standalone/selectize.min.js', 'js/canvasResize.js', 'js/oc-panel/edit_ad.js');
     }
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('My ads'))->set_url(Route::url('oc-panel', array('controller' => 'myads', 'action' => 'index'))));
     $form = new Model_Ad($this->request->param('id'));
     if ($form->loaded() and (Auth::instance()->get_user()->id_user == $form->id_user or Auth::instance()->get_user()->id_role == Model_Role::ROLE_ADMIN or Auth::instance()->get_user()->id_role == Model_Role::ROLE_MODERATOR)) {
         // deleting single image by path
         if (is_numeric($deleted_image = core::request('img_delete'))) {
             $form->delete_image($deleted_image);
             $this->redirect(Route::url('oc-panel', array('controller' => 'myads', 'action' => 'update', 'id' => $form->id_ad)));
         }
         // end of img delete
         // set primary image
         if (is_numeric($primary_image = core::request('primary_image'))) {
             $form->set_primary_image($primary_image);
             $this->redirect(Route::url('oc-panel', array('controller' => 'myads', 'action' => 'update', 'id' => $form->id_ad)));
         }
         $original_category = $form->category;
         $extra_payment = core::config('payment');
         if ($this->request->post()) {
             $data = $this->request->post();
             //to make it backward compatible with older themes: UGLY!!
             if (isset($data['category']) and is_numeric($data['category'])) {
                 $data['id_category'] = $data['category'];
                 unset($data['category']);
             }
             if (isset($data['location']) and is_numeric($data['location'])) {
                 $data['id_location'] = $data['location'];
                 unset($data['location']);
             }
             $return = $form->save_ad($data);
             //there was an error on the validation
             if (isset($return['validation_errors']) and is_array($return['validation_errors'])) {
                 foreach ($return['validation_errors'] as $f => $err) {
                     Alert::set(Alert::ALERT, $err);
                 }
             } elseif (isset($return['error'])) {
                 Alert::set($return['error_type'], $return['error']);
             } elseif (isset($return['message'])) {
                 // IMAGE UPLOAD
                 // in case something wrong happens user is redirected to edit advert.
                 $filename = NULL;
                 for ($i = 0; $i < core::config("advertisement.num_images"); $i++) {
                     if (Core::post('base64_image' . $i)) {
                         $filename = $form->save_base64_image(Core::post('base64_image' . $i));
                     } elseif (isset($_FILES['image' . $i])) {
                         $filename = $form->save_image($_FILES['image' . $i]);
                     }
                 }
                 if ($filename !== NULL) {
                     $form->last_modified = Date::unix2mysql();
                     try {
                         $form->save();
                     } catch (Exception $e) {
                         throw HTTP_Exception::factory(500, $e->getMessage());
                     }
                 }
                 Alert::set(Alert::SUCCESS, $return['message']);
                 //redirect user to pay
                 if (isset($return['checkout_url']) and !empty($return['checkout_url'])) {
                     $this->redirect($return['checkout_url']);
                 }
             }
             $this->redirect(Route::url('oc-panel', array('controller' => 'myads', 'action' => 'update', 'id' => $form->id_ad)));
         }
         //get all orders
         $orders = new Model_Order();
         $orders = $orders->where('id_user', '=', $form->id_user)->where('status', '=', Model_Order::STATUS_CREATED)->where('id_ad', '=', $form->id_ad)->find_all();
         Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Update')));
         $this->template->content = View::factory('oc-panel/profile/edit_ad', array('ad' => $form, 'extra_payment' => $extra_payment, 'orders' => $orders));
     } else {
         Alert::set(Alert::ERROR, __('You dont have permission to access this link'));
         $this->redirect(Route::url('default'));
     }
 }