/** * 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'); //local files if (Theme::get('cdn_files') == FALSE) { $this->template->styles = array('css/datepicker.css' => 'screen'); $this->template->scripts['footer'] = array('js/bootstrap-datepicker.js', 'js/jquery.validate.min.js', 'js/oc-panel/edit_ad.js'); } else { $this->template->styles = array('http://cdn.jsdelivr.net/bootstrap.datepicker/0.1/css/datepicker.css' => 'screen'); $this->template->scripts['footer'] = array('http://cdn.jsdelivr.net/bootstrap.datepicker/0.1/js/bootstrap-datepicker.js', 'js/jquery.validate.min.js', 'js/oc-panel/edit_ad.js'); } Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Home'))->set_url(Route::url('default'))); $form = new Model_Ad($this->request->param('id')); //find all, for populating form select fields list($categories, $order_categories) = Model_Category::get_all(); list($locations, $order_locations) = Model_Location::get_all(); if (Auth::instance()->logged_in() && Auth::instance()->get_user()->id_user == $form->id_user || Auth::instance()->logged_in() && Auth::instance()->get_user()->id_role == 10) { $extra_payment = core::config('payment'); Breadcrumbs::add(Breadcrumb::factory()->set_title("Update")); $this->template->content = View::factory('oc-panel/profile/edit_ad', array('ad' => $form, 'locations' => $locations, 'order_locations' => $order_locations, 'categories' => $categories, 'order_categories' => $order_categories, 'extra_payment' => $extra_payment, 'fields' => Model_Field::get_all())); if ($this->request->post()) { $cat = new Model_Category(); $loc = new Model_Location(); // deleting single image by path $deleted_image = core::post('img_delete'); if ($deleted_image) { $img_path = $form->gen_img_path($form->id_ad, $form->created); if (!is_dir($img_path)) { return FALSE; } else { //delete formated image unlink($img_path . $deleted_image . '.jpg'); //delete original image $orig_img = str_replace('thumb_', '', $deleted_image); unlink($img_path . $orig_img . ".jpg"); $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'update', 'id' => $form->id_ad))); } } // end of img delete $data = array('_auth' => $auth = Auth::instance(), 'title' => $title = Model_Ad::banned_words(core::post('title')), 'seotitle' => $seotitle = core::post('title'), 'loc' => $loc = core::post('location'), 'description' => $description = Model_Ad::banned_words(core::post('description')), 'price' => $price = floatval(str_replace(',', '.', core::post('price'))), 'address' => $address = core::post('address'), 'website' => $website = core::post('website'), 'phone' => $phone = core::post('phone'), 'has_images' => 0, 'user' => $user = new Model_User()); // append to $data new custom values foreach ($_POST as $name => $field) { // get by prefix if (strpos($name, 'cf_') !== false) { $data[$name] = $field; //checkbox when selected return string 'on' as a value if ($field == 'on') { $data[$name] = 1; } } } //insert data if (core::post('title') != $form->title) { if ($form->has_images == 1) { $current_path = $form->gen_img_path($form->id_ad, $form->created); // rename current image path to match new seoname rename($current_path, $form->gen_img_path($form->id_ad, $form->created)); } $seotitle = $form->gen_seo_title($data['title']); $form->seotitle = $seotitle; } else { $form->seotitle = $form->seotitle; } $form->title = $data['title']; $form->id_location = $data['loc']; //$form->id_category = $data['cat']; $form->description = $data['description']; // $form->status = $data['status']; $form->price = $data['price']; $form->address = $data['address']; $form->website = $data['website']; $form->phone = $data['phone']; // set custom values foreach ($data as $key => $value) { // get only custom values with prefix if (strpos($key, 'cf_') !== false) { $form->{$key} = $value; } } // d($data['cf_radio']); $obj_ad = new Model_Ad(); // IMAGE UPLOAD // in case something wrong happens user is redirected to edit advert. $filename = NULL; $counter = 0; for ($i = 0; $i < core::config("advertisement.num_images"); $i++) { $counter++; if (isset($_FILES['image' . $i])) { $img_files = $_FILES['image' . $i]; $filename = $obj_ad->save_image($img_files, $form->id_ad, $form->created, $form->seotitle, $counter); } if ($filename) { $form->has_images = 1; try { $form->save(); } catch (Exception $e) { throw new HTTP_Exception_500($e->getMessage()); } } if ($filename = FALSE) { $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'update', 'id' => $form->id_ad))); } } try { // if user changes category, do payment first // moderation 2 -> payment on, moderation 5 -> payment with moderation // data['cat'] -> category selected , last_known_ad->id_category -> obj of current ad (before save) $moderation = core::config('general.moderation'); $last_known_ad = $obj_ad->where('id_ad', '=', $this->request->param('id'))->limit(1)->find(); if ($moderation == Model_Ad::PAYMENT_ON || $moderation == Model_Ad::PAYMENT_MODERATION) { // PAYMENT METHOD ACTIVE $payment_order = new Model_Order(); $advert_have_order = $payment_order->where('id_ad', '=', $this->request->param('id')); if ($data['cat'] == $last_known_ad->id_category) { // check if he payed when ad was created (is successful), // if not give him alert that he didn't payed, and ad will not be published until he do $cat_check = $cat->where('id_category', '=', $last_known_ad->id_category)->limit(1)->find(); // current category $advert_have_order->and_where('description', '=', $cat_check->seoname)->limit(1)->find(); if ($advert_have_order->loaded()) { if ($advert_have_order->status != Model_Order::STATUS_PAID) { // order is not payed, $form->status = 0; Alert::set(Alert::INFO, __('Advertisement is updated, but it won\'t be published until payment is done.')); } else { if ($moderation == Model_Ad::PAYMENT_ON) { $form->status = 1; Alert::set(Alert::SUCCESS, __('Advertisement is updated!')); } else { if ($moderation == 5) { Alert::set(Alert::SUCCESS, __('Advertisement is updated!')); } } } } $form->save(); $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'update', 'id' => $form->id_ad))); } else { // user have pending order with new category(possible that he previously tried to do the same action) $cat_check = $cat->where('id_category', '=', $data['cat'])->limit(1)->find(); // newly selected category $advert_have_order->and_where('description', '=', $cat_check->seoname)->limit(1)->find(); if ($advert_have_order->loaded()) { // sanity check -> we don't want to charge him twice for same category if ($advert_have_order->status != Model_Order::STATUS_PAID) { $this->request->redirect(Route::url('default', array('controller' => 'payment_paypal', 'action' => 'form', 'id' => $advert_have_order->id_order))); } else { if ($moderation == Model_Ad::PAYMENT_ON) { $form->status = 1; Alert::set(Alert::SUCCESS, __('Advertisement is updated!')); } else { if ($moderation == Model_Ad::PAYMENT_MODERATION) { Alert::set(Alert::SUCCESS, __('Advertisement is updated!')); } } } $form->save(); } else { $order_id = $payment_order->make_new_order($data, Auth::instance()->get_user()->id_user, $form->seotitle); if ($order_id == NULL) { if ($moderation == Model_Ad::PAYMENT_ON) { // publish $form->status = 1; } } else { // redirect to payment $this->request->redirect(Route::url('default', array('controller' => 'payment_paypal', 'action' => 'form', 'id' => $order_id))); // @TODO - check route } $form->save(); } } } // save ad $form->status = $last_known_ad->status; $form->save(); Alert::set(Alert::SUCCESS, __('Advertisement is updated')); $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'update', 'id' => $form->id_ad))); } catch (Exception $e) { //throw 500 throw new HTTP_Exception_500($e->getMessage()); } } } else { Alert::set(Alert::ERROR, __('You dont have permission to access this link')); $this->request->redirect(Route::url('default')); } // QR!!! $qr = new Qr($this->request->param('id')); $qr->reset(); $f = $qr->qr(); $qr->calendar(); // $qr->map(); $qr->website(); $qr->contact(); Alert::set(Alert::SUCCESS, "Wrote " . print_r($f, true)); }
/** * [image_path Get directory path of specific advert.] * @param [array] $data [all values of one advert.] * @return [array] [array of dir. path where images of advert. are ] */ public function image_path($data) { $obj_ad = new Model_Ad(); $directory = $obj_ad->gen_img_path($data->id_ad, $data->created); $path = array(); if (is_dir($directory)) { $filename = array_diff(scandir($directory, 1), array('..', '.')); //return all file names , and store in array foreach ($filename as $filename) { array_push($path, $directory . $filename); } } else { return FALSE; } return $path; }
/** * image_path make unique dir path with a given date and id * * @param [int] $id [unique id, and folder name] * @return [string] [directory path] */ public function image_path($id, $created) { if ($created !== NULL) { $obj_ad = new Model_Ad(); $path = $obj_ad->gen_img_path($id, $created); } else { $date = Date::format(time(), 'Y/m/d'); $parse_data = explode("/", $date); // make array with date values $path = "images/"; // root upload folder for ($i = 0; $i < count($parse_data); $i++) { $path .= $parse_data[$i] . '/'; // append, to create path } $path .= $id . '/'; } if (!is_dir($path)) { // check if path exists mkdir($path, 0755, TRUE); } return $path; }