Пример #1
0
 public function action_create()
 {
     try {
         if (!Valid::email(core::request('email'))) {
             $this->_error(__('Invalid email'), 501);
         } elseif (!is_numeric(core::request('id_product'))) {
             $this->_error(__('Invalid product'), 501);
         } else {
             $product = new Model_Product(core::request('id_product'));
             if ($product->loaded()) {
                 $user = Model_User::create_email(core::request('email'), core::request('name'));
                 $order = Model_Order::new_order($user, $product);
                 $order->confirm_payment(core::request('paymethod', 'API'), core::request('txn_id'), core::request('pay_date'), core::request('amount'), core::request('currency'), core::request('fee'));
                 //adding the notes
                 $order->notes = core::request('notes');
                 $order->save();
                 $this->rest_output(array('order' => self::get_order_array($order)));
             } else {
                 $this->_error(__('Something went wrong'), 501);
             }
         }
     } catch (Kohana_HTTP_Exception $khe) {
         $this->_error($khe);
     }
 }
Пример #2
0
 /**
  * [new_order description]
  * @param  Model_User    $user    [description]
  * @param  Model_Product $product [description]
  * @param  boolean       check_match_product, if set to false will update the order with the product if different
  * @return [type]                 [description]
  */
 public static function new_order(Model_User $user, Model_Product $product, $match_product = TRUE)
 {
     $order = new Model_Order();
     if ($user->loaded() and $product->loaded()) {
         //get if theres an unpaid order for this user we wwill use it..
         $order->where('id_user', '=', $user->id_user)->where('status', '=', Model_Order::STATUS_CREATED);
         //also check that matches the product for the order
         if ($match_product === TRUE) {
             $order->where('id_product', '=', $product->id_product)->where('amount', '=', $product->final_price())->where('currency', '=', $product->currency);
         }
         $order->limit(1)->find();
         //order didnt exist so lets create it.
         if ($order->loaded() === FALSE) {
             //create order
             $order = new Model_Order();
             $order->id_user = $user->id_user;
         }
         // no matter what happens if product is different save! this will also save the order if its new ;)
         if ($order->id_product != $product->id_product) {
             $order->ip_address = ip2long(Request::$client_ip);
             $order->id_product = $product->id_product;
             $order->currency = $product->currency;
             //add coupon ID and discount
             if (Model_Coupon::current()->loaded()) {
                 $order->id_coupon = Model_Coupon::current()->id_coupon;
             }
             $order->amount = $product->final_price();
             $order->VAT = euvat::vat_percentage();
             $order->VAT_number = $user->VAT_number;
             $order->country = $user->country;
             $order->city = $user->city;
             $order->postal_code = $user->postal_code;
             $order->address = $user->address;
             try {
                 $order->save();
             } catch (Exception $e) {
                 throw HTTP_Exception::factory(500, $e->getMessage());
             }
         }
     }
     return $order;
 }
Пример #3
0
 public function action_update()
 {
     //template header
     $this->template->title = __('Edit Product');
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Edit Product')));
     $this->template->styles = array('css/sortable.css' => 'screen', '//cdn.jsdelivr.net/bootstrap.datepicker/0.1/css/datepicker.css' => 'screen', '//cdn.jsdelivr.net/jquery.fileupload/9.5.2/css/jquery.fileupload.css' => 'screen', 'css/jasny-bootstrap.min.css' => 'screen');
     $this->template->scripts['footer'] = array('//cdn.jsdelivr.net/bootstrap.datepicker/0.1/js/bootstrap-datepicker.js', 'js/jasny-bootstrap.min.js', 'js/oc-panel/products.js', 'js/jquery-sortable-min.js', '//cdn.jsdelivr.net/jquery.fileupload/9.5.2/js/vendor/jquery.ui.widget.js', '//cdn.jsdelivr.net/jquery.fileupload/9.5.2/js/jquery.iframe-transport.js', '//cdn.jsdelivr.net/jquery.fileupload/9.5.2/js/jquery.fileupload.js');
     $cats = Model_Category::get_as_array();
     $order = Model_Category::get_multidimensional();
     $obj_product = new Model_Product($this->request->param('id'));
     if ($obj_product->loaded()) {
         // get currencies from product, returns array
         $currency = $obj_product::get_currency();
         $this->template->content = View::factory('oc-panel/pages/products/update', array('product' => $obj_product, 'categories' => $cats, 'order_categories' => $order, 'currency' => $currency));
         if ($product = $this->request->post()) {
             // save product file
             if (isset($_FILES['file_name'])) {
                 if ($file = $_FILES['file_name']) {
                     $file = $obj_product->save_product($file);
                     if ($file != FALSE) {
                         $obj_product->file_name = $file;
                     } else {
                         Alert::set(Alert::INFO, __('Product is not uploaded.'));
                     }
                 }
             }
             // deleting single image by path
             $deleted_image = core::post('img_delete');
             if (is_numeric($deleted_image)) {
                 $img_path = $obj_product->gen_img_path($obj_product->id_product, $obj_product->created);
                 $img_seoname = $obj_product->seotitle;
                 // delete image from Amazon S3
                 if (core::config('image.aws_s3_active')) {
                     require_once Kohana::find_file('vendor', 'amazon-s3-php-class/S3', 'php');
                     $s3 = new S3(core::config('image.aws_access_key'), core::config('image.aws_secret_key'));
                     //delete original image
                     $s3->deleteObject(core::config('image.aws_s3_bucket'), $img_path . $img_seoname . '_' . $deleted_image . '.jpg');
                     //delete formated image
                     $s3->deleteObject(core::config('image.aws_s3_bucket'), $img_path . 'thumb_' . $img_seoname . '_' . $deleted_image . '.jpg');
                     //re-ordering image file names
                     for ($i = $deleted_image; $i < $obj_product->has_images; $i++) {
                         //rename original image
                         $s3->copyObject(core::config('image.aws_s3_bucket'), $img_path . $img_seoname . '_' . ($i + 1) . '.jpg', core::config('image.aws_s3_bucket'), $img_path . $img_seoname . '_' . $i . '.jpg', S3::ACL_PUBLIC_READ);
                         $s3->deleteObject(core::config('image.aws_s3_bucket'), $img_path . $img_seoname . '_' . ($i + 1) . '.jpg');
                         //rename formated image
                         $s3->copyObject(core::config('image.aws_s3_bucket'), $img_path . 'thumb_' . $img_seoname . '_' . ($i + 1) . '.jpg', core::config('image.aws_s3_bucket'), $img_path . 'thumb_' . $img_seoname . '_' . $i . '.jpg', S3::ACL_PUBLIC_READ);
                         $s3->deleteObject(core::config('image.aws_s3_bucket'), $img_path . 'thumb_' . $img_seoname . '_' . ($i + 1) . '.jpg');
                     }
                 }
                 if (!is_dir($img_path)) {
                     return FALSE;
                 } else {
                     //delete original image
                     @unlink($img_path . $img_seoname . '_' . $deleted_image . '.jpg');
                     //delete formated image
                     @unlink($img_path . 'thumb_' . $img_seoname . '_' . $deleted_image . '.jpg');
                     //re-ordering image file names
                     for ($i = $deleted_image; $i < $obj_product->has_images; $i++) {
                         rename($img_path . $img_seoname . '_' . ($i + 1) . '.jpg', $img_path . $img_seoname . '_' . $i . '.jpg');
                         rename($img_path . 'thumb_' . $img_seoname . '_' . ($i + 1) . '.jpg', $img_path . 'thumb_' . $img_seoname . '_' . $i . '.jpg');
                     }
                 }
                 $obj_product->has_images = $obj_product->has_images > 0 ? $obj_product->has_images - 1 : 0;
                 $obj_product->updated = Date::unix2mysql();
                 try {
                     $obj_product->save();
                 } catch (Exception $e) {
                     throw HTTP_Exception::factory(500, $e->getMessage());
                 }
                 $this->redirect(Route::url('oc-panel', array('controller' => 'product', 'action' => 'update', 'id' => $obj_product->id_product)));
             }
             // end of img delete
             //delete product file
             $product_delete = core::post('product_delete');
             if ($product_delete) {
                 $p_path = $obj_product->get_file($obj_product->file_name);
                 if (!is_file($p_path)) {
                     return FALSE;
                 } else {
                     @chmod($p_path, 0755);
                     //delete product
                     unlink($p_path);
                     $obj_product->file_name = '';
                     $obj_product->save();
                     $this->redirect(Route::url('oc-panel', array('controller' => 'product', 'action' => 'update', 'id' => $obj_product->id_product)));
                 }
             }
             $product['status'] = (!isset($_POST['status']) or core::post('status') === NULL) ? Model_Product::STATUS_NOACTIVE : Model_Product::STATUS_ACTIVE;
             $product['updated'] = Date::unix2mysql();
             //we do this so we assure use the entire day , nasty
             $product['offer_valid'] .= ' 23:59:59';
             $product['featured'] .= ' 23:59:59';
             // each field in edit product
             foreach ($product as $field => $value) {
                 // do not include submit
                 if ($field != 'submit' and $field != 'notify') {
                     // check if its different, and set it is
                     if ($value != $obj_product->{$field}) {
                         $obj_product->{$field} = $value;
                         // if title is changed, make new seotitle
                         if ($field == 'title') {
                             $seotitle = $obj_product->gen_seotitle($product['title']);
                             $obj_product->seotitle = $seotitle;
                         }
                     }
                 }
             }
             // save product or trow exeption
             try {
                 $obj_product->save();
                 Alert::set(Alert::SUCCESS, __('Product saved.'));
                 Sitemap::generate();
                 //notify users of new update
                 if ($this->request->post('notify')) {
                     //get users with that product
                     $query = DB::select('email')->select('name')->from(array('users', 'u'))->join(array('orders', 'o'), 'INNER')->on('u.id_user', '=', 'o.id_user')->where('u.status', '=', Model_User::STATUS_ACTIVE)->where('o.status', '=', Model_Order::STATUS_PAID)->where('o.id_product', '=', $obj_product->id_product)->execute();
                     $users = $query->as_array();
                     if (count($users) > 0) {
                         //download link
                         $download = '';
                         if ($obj_product->has_file() == TRUE) {
                             $download = '\\n\\n==== ' . __('Download') . ' ====\\n' . Route::url('oc-panel', array('controller' => 'profile', 'action' => 'orders'));
                         }
                         //theres an expire? 0 = unlimited
                         $expire = '';
                         $expire_hours = Core::config('product.download_hours');
                         $expire_times = Core::config('product.download_times');
                         if (($expire_hours > 0 or $expire_times > 0) and $obj_product->has_file() == TRUE) {
                             if ($expire_hours > 0 and $expire_times > 0) {
                                 $expire = sprintf(__('Your download expires in %u hours and can be downloaded %u times.'), $expire_hours, $expire_times);
                             } elseif ($expire_hours > 0) {
                                 $expire = sprintf(__('Your download expires in %u hours.'), $expire_hours);
                             } elseif ($expire_times > 0) {
                                 $expire = sprintf(__('Can be downloaded %u times.'), $expire_times);
                             }
                             $expire = '\\n' . $expire;
                         }
                         if (!Email::content($users, '', NULL, NULL, 'product-update', array('[TITLE]' => $obj_product->title, '[URL.PRODUCT]' => Route::url('product', array('seotitle' => $obj_product->seotitle, 'category' => $obj_product->category->seoname)), '[DOWNLOAD]' => $download, '[EXPIRE]' => $expire, '[VERSION]' => $obj_product->version))) {
                             Alert::set(Alert::ERROR, __('Error on mail delivery, not sent'));
                         } else {
                             Alert::set(Alert::SUCCESS, __('Email sent to all the users'));
                         }
                     } else {
                         Alert::set(Alert::ERROR, __('Mail not sent'));
                     }
                 }
             } catch (Exception $e) {
                 throw HTTP_Exception::factory(500, $e->getMessage());
             }
             // save images
             if (isset($_FILES)) {
                 foreach ($_FILES as $file_name => $file) {
                     if ($file_name != 'file_name') {
                         $file = $obj_product->save_image($file);
                     }
                     if ($file) {
                         $obj_product->has_images++;
                     }
                 }
                 //since theres images save the ad again...
                 try {
                     $obj_product->save();
                 } catch (Exception $e) {
                     throw HTTP_Exception::factory(500, $e->getMessage());
                 }
             }
         }
     }
 }
Пример #4
0
 public function action_buy()
 {
     if (!Auth::instance()->logged_in()) {
         $this->redirect(Route::get('oc-panel')->uri());
     }
     $user = Auth::instance()->get_user();
     $id_product = $this->request->param('id');
     if (is_numeric($id_product)) {
         $product = new Model_Product($id_product);
         if ($product->loaded() and $product->status == Model_Product::STATUS_ACTIVE) {
             //generates a new order if none was existent
             $order = Model_Order::new_order($user, $product);
             //its paid plan?
             if ($product->final_price() > 0) {
                 // redirect to checkout payment
                 $this->redirect(Route::url('default', array('controller' => 'product', 'action' => 'checkout', 'id' => $order->id_order)));
             } else {
                 //mark as paid
                 $order->confirm_payment();
                 //if theres download redirect him to the file
                 if ($product->has_file() == TRUE) {
                     $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'download', 'id' => $order->id_order)));
                 } else {
                     $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'orders')));
                 }
             }
         }
     }
     //default redirect
     $this->redirect(Route::get('oc-panel')->uri());
 }
Пример #5
0
 public function action_import()
 {
     if ($this->request->post()) {
         ini_set('auto_detect_line_endings', true);
         $csv = $_FILES['file_source']['tmp_name'];
         if (($handle = fopen($csv, "r")) !== FALSE) {
             $i = 0;
             while (($data = fgetcsv($handle, 0, ";")) !== false) {
                 //avoid first line
                 if ($i != 0) {
                     list($email, $pay_date, $product_seotitle, $amount, $currency) = $data;
                     $pay_date = Date::from_format($pay_date, 'd/m/yy', 'Y-m-d H:i:s');
                     $user = Model_User::create_email($email, substr($email, 0, strpos($email, '@')));
                     $product = new Model_Product();
                     $product->where('seotitle', '=', $product_seotitle)->limit(1)->find();
                     if ($product->loaded()) {
                         $order = Model_Order::new_order($user, $product);
                         $order->confirm_payment('import', NULL, $pay_date, $amount, $currency);
                     }
                 }
                 $i++;
             }
         }
         fclose($handle);
         //redirect to orders
         Alert::set(Alert::SUCCESS, __('Import correct'));
         $this->redirect(Route::url('oc-panel', array('controller' => 'order', 'action' => 'index')));
     }
     //template header
     $this->template->title = __('Import Orders');
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Import Orders')));
     $this->template->content = View::factory('oc-panel/pages/order/import');
 }