Example #1
0
 /**
  * action product goal after buying
  * @return void 
  */
 public function action_goal()
 {
     if (!Auth::instance()->logged_in()) {
         $this->redirect(Route::get('oc-panel')->uri());
     }
     $user = Auth::instance()->get_user();
     $order = new Model_Order();
     $order->where('id_order', '=', $this->request->param('id'))->where('id_user', '=', $user->id_user)->limit(1)->find();
     if ($order->loaded()) {
         $product = $order->product;
         Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Home'))->set_url(Route::url('default')));
         Breadcrumbs::add(Breadcrumb::factory()->set_title($product->category->name)->set_url(Route::url('list', array('category' => $product->category->seoname))));
         Breadcrumbs::add(Breadcrumb::factory()->set_title($product->title)->set_url(Route::url('product', array('seotitle' => $product->seotitle, 'category' => $product->category->seoname))));
         Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Thanks')));
         $this->template->title = $product->title . ' - ' . $product->category->name;
         $this->template->meta_description = $product->description;
         $thanks_message = NULL;
         if (core::config('payment.thanks_page') != '') {
             $thanks_message = Model_Content::get_by_title(core::config('payment.thanks_page'));
         }
         $this->template->bind('content', $content);
         $this->template->content = View::factory('pages/product/goal', array('product' => $product, 'thanks_message' => $thanks_message, 'order' => $order, 'price_paid' => $order->amount));
     } else {
         Alert::set(Alert::INFO, __('Order not found.'));
         $this->redirect(Route::url('default'));
     }
 }
Example #2
0
 /**
  * [action_form] generates the form to pay at paypal
  */
 public function action_pay()
 {
     $this->auto_render = FALSE;
     $order_id = $this->request->param('id');
     $order = new Model_Order();
     $order->where('id_order', '=', $order_id)->where('status', '=', Model_Order::STATUS_CREATED)->limit(1)->find();
     if ($order->loaded()) {
         // case when selling advert
         if ($order->id_product == Model_Order::PRODUCT_AD_SELL) {
             $paypal_account = $order->ad->paypal_account();
             $currency = i18n::get_intl_currency_symbol();
             if (isset($order->ad->cf_shipping) and Valid::numeric($order->ad->cf_shipping) and $order->ad->cf_shipping > 0) {
                 $order->amount = $order->amount + $order->ad->cf_shipping;
             }
         } else {
             $paypal_account = core::config('payment.paypal_account');
             $currency = core::config('payment.paypal_currency');
         }
         $paypal_url = Core::config('payment.sandbox') ? Paypal::url_sandbox_gateway : Paypal::url_gateway;
         $paypal_data = array('order_id' => $order_id, 'amount' => number_format($order->amount, 2, '.', ''), 'site_name' => core::config('general.site_name'), 'site_url' => URL::base(TRUE), 'paypal_url' => $paypal_url, 'paypal_account' => $paypal_account, 'paypal_currency' => $currency, 'item_name' => $order->description);
         $this->template = View::factory('paypal', $paypal_data);
         $this->response->body($this->template->render());
     } else {
         Alert::set(Alert::INFO, __('Order could not be loaded'));
         $this->redirect(Route::url('default'));
     }
 }
Example #3
0
 public function enrolAction()
 {
     $this->mTemplate->sSectionTitle = $this->getLang('section_title_orders');
     if (!$this->oAuth->isLoggedIn()) {
         $this->redirect('/user/login/');
         echo ' ';
         return;
     }
     // gdy nie wybrano zadnej daty
     $iDay = date('j');
     $iMonth = date('n');
     $iYear = date('Y');
     $iTime = time();
     $iNext = 0;
     // jezeli wybrano inny tydzien
     if (func_num_args() > 0) {
         $iNext = (int) func_get_arg(0);
         $iTime = mktime(0, 0, 0, $iMonth, $iDay + $iNext * 7, $iYear);
     }
     $aWeek = array();
     // pobieramy kalendarz
     $aMonth = $this->getMonthData($iTime);
     $iCurrentTime = strtotime(date('Y-m-d', $iTime));
     // drukujemy formularz
     $this->oCurrentUser = $this->oAuth->getLoggedInUser();
     $sDayEnd = $this->oCurrentUser->get('account_id')->day_end;
     // zapisujemy
     if (isset($_POST['submit'])) {
         $aMeals = $this->getMealsForWeek($this->oCurrentUser->account_id, $iTime);
         $this->saveEnrol($this->oCurrentUser->account_id, $this->oCurrentUser->user_id, $aMeals, $sDayEnd);
         return;
     }
     // wyszukujemy posików i grupujemy po dacie
     $aMeals = $this->getMealsForWeek($this->oCurrentUser->account_id, $iTime, 'date');
     //		$aMeals = $this->getMealsForWeek( $this->oCurrentUser->account_id, $iYear, $iMonth, $iWeek, 'date' );
     // wyszukujemy zamowienia
     $aOrderedMeals = array();
     $oOrder = new Model_Order();
     $aOrders = $oOrder->where('user_id', $this->oCurrentUser->user_id)->getAll();
     foreach ($aOrders as $aOrder) {
         $aOrderedMeals[] = $aOrder['meal_id'];
     }
     foreach ($aMeals as $sDate => $aMeal) {
         $aWeek[$sDate]['aMeals'][0] = array('name' => $this->getLang('no_order'), 'meal_id' => 0, 'optional' => 0, 'bChecked' => false, 'bDisabled' => strtotime($sDate . ' ' . $sDayEnd) < time());
         $aWeekDays = $this->getLang('Catering.week_days');
         $aWeek[$sDate]['sWeekday'] = $aWeekDays[date('w', strtotime($sDate))];
         foreach ($aMeal as $iMealId => $aMealData) {
             $aWeek[$sDate]['aMeals'][] = array('name' => $aMealData['lname'], 'fname' => $aMealData['fname'], 'meal_id' => $iMealId, 'price' => $aMealData['price'] . $this->getLang('currency'), 'optional' => $aMealData['optional'], 'bChecked' => in_array($iMealId, $aOrderedMeals) ? true : false, 'bDisabled' => strtotime($sDate . ' ' . $sDayEnd) < time());
             if (strtotime($sDate . ' ' . $sDayEnd) > time()) {
                 $aData['submit'] = $this->getLang('save');
             }
         }
     }
     $aData['aWeek'] = $aWeek;
     $aData['sNextText'] = $this->getLang('next');
     $aData['sNextLink'] = '/catering/enrol/' . ($iNext + 1) . '/';
     $aData['sPrevText'] = $this->getLang('prev');
     $aData['sPrevLink'] = '/catering/enrol/' . ($iNext - 1) . '/';
     $this->mTemplate->content = View::factory('catering/enrol', $aData)->render();
 }
Example #4
0
 /**
  * [action_form] generates the form to pay at paypal
  */
 public function action_form()
 {
     $this->auto_render = FALSE;
     $order_id = $this->request->param('id');
     $order = new Model_Order();
     $order->where('id_order', '=', $order_id)->where('status', '=', Model_Order::STATUS_CREATED)->limit(1)->find();
     if ($order->loaded()) {
         // dependant on product we have different names
         if ($order->id_product == Paypal::to_featured) {
             $item_name = __('Advertisement to featured');
         } else {
             if ($order->id_product == Paypal::to_top) {
                 $item_name = __('Advertisement to top');
             } else {
                 $item_name = $order->description . __(' category');
             }
         }
         $paypal_url = Core::config('payment.sandbox') ? Paypal::url_sandbox_gateway : Paypal::url_gateway;
         $paypal_data = array('order_id' => $order_id, 'amount' => number_format($order->amount, 2, '.', ''), 'site_name' => core::config('general.site_name'), 'site_url' => URL::base(TRUE), 'paypal_url' => $paypal_url, 'paypal_account' => core::config('payment.paypal_account'), 'paypal_currency' => core::config('payment.paypal_currency'), 'item_name' => $item_name);
         $this->template = View::factory('paypal', $paypal_data);
         $this->response->body($this->template->render());
     } else {
         Alert::set(Alert::INFO, __('Order could not be loaded'));
         $this->request->redirect(Route::url('default'));
     }
 }
Example #5
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;
 }
Example #6
0
 public function action_pay()
 {
     $this->auto_render = FALSE;
     $order_id = $this->request->param('id');
     $order = new Model_Order();
     $order->where('id_order', '=', $order_id)->where('status', '=', Model_Order::STATUS_CREATED)->limit(1)->find();
     if ($order->loaded()) {
         $paypal_url = Core::config('payment.sandbox') ? Paypal::url_sandbox_gateway : Paypal::url_gateway;
         $paypal_data = array('order_id' => $order_id, 'amount' => number_format($order->amount, 2, '.', ''), 'site_name' => core::config('general.site_name'), 'return_url' => Route::url('default', array('controller' => 'product', 'action' => 'goal', 'id' => $order->id_order)), 'paypal_url' => $paypal_url, 'paypal_account' => core::config('payment.paypal_account'), 'paypal_currency' => $order->currency, 'item_name' => $order->product->title);
         $this->template = View::factory('paypal', $paypal_data);
         $this->response->body($this->template->render());
     } else {
         Alert::set(Alert::INFO, __('Order could not be loaded'));
         $this->redirect(Route::url('default'));
     }
 }
Example #7
0
 public function action_new()
 {
     $errors = NULL;
     $user = Auth::instance()->get_user();
     //create new ticket
     if ($_POST) {
         //if post save
         $id_order = core::post('order');
         //check if that order still have support...no cheating!! :D
         $order = new Model_Order();
         $order->where('id_order', '=', $id_order)->where('id_user', '=', $user->id_user)->where('support_date', '>', Date::unix2mysql())->where('status', '=', Model_Order::STATUS_PAID)->limit(1)->find();
         $validation = Validation::factory($this->request->post())->rule('title', 'not_empty')->rule('title', 'min_length', array(':value', 2))->rule('title', 'max_length', array(':value', 145))->rule('description', 'not_empty')->rule('description', 'min_length', array(':value', 50))->rule('description', 'max_length', array(':value', 1000))->rule('order', 'not_empty')->rule('order', 'numeric');
         if ($validation->check() and $order->loaded()) {
             $ticket = new Model_Ticket();
             $ticket->id_user = $user->id_user;
             $ticket->id_order = $id_order;
             $ticket->id_product = $order->product->id_product;
             $ticket->title = core::post('title');
             $ticket->description = core::post('description');
             $ticket->ip_address = ip2long(Request::$client_ip);
             $ticket->save();
             //send email to notify_url @todo we should add a config on general to select who gets the emails by default.
             if (core::config('email.new_sale_notify')) {
                 Email::send(core::config('email.notify_email'), '', 'New Ticket: ' . $ticket->title, Route::url('oc-panel', array('controller' => 'support', 'action' => 'ticket', 'id' => $ticket->id_ticket)) . '\\n\\n' . $ticket->description, core::config('email.notify_email'), '');
             }
             $user->email('new-ticket', array('[TITLE]' => $ticket->title, '[URL.QL]' => $user->ql('oc-panel', array('controller' => 'support', 'action' => 'ticket', 'id' => $ticket->id_ticket))));
             Alert::set(Alert::SUCCESS, __('Ticket created.'));
             $this->redirect(Route::url('oc-panel', array('controller' => 'support', 'action' => 'index')));
         } else {
             $errors = $validation->errors('ad');
         }
     }
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('New Ticket')));
     $this->template->title = __('New Ticket');
     //get orders with support
     $orders = new Model_Order();
     $orders = $orders->where('id_user', '=', $user->id_user)->where('support_date', '>', Date::unix2mysql())->where('status', '=', Model_Order::STATUS_PAID)->find_all();
     if ($orders->count() == 0) {
         Alert::set(Alert::ALERT, __('You do not have any purchase with support active.'));
         $this->redirect(Route::url('oc-panel', array('controller' => 'support', 'action' => 'index')));
     }
     $this->template->bind('content', $content);
     $this->template->content = View::factory('oc-panel/pages/support/new', array('orders' => $orders));
     $content->errors = $errors;
 }
Example #8
0
 /**
  * creates an order
  * @param  Model_Ad $ad    
  * @param  Model_User $user          
  * @param  integer   $id_product  
  * @param  numeric   $amount      
  * @param  string   $currency    
  * @param  string   $description 
  * @return Model_Order                
  */
 public static function new_order(Model_Ad $ad, $user, $id_product, $amount, $currency = NULL, $description = NULL, $featured_days = NULL)
 {
     if ($currency === NULL) {
         $currency = core::config('payment.paypal_currency');
     }
     if ($description === NULL) {
         $description = Model_Order::product_desc($id_product);
     }
     //get if theres an unpaid order for this product and this ad
     $order = new Model_Order();
     $order->where('id_ad', '=', $ad->id_ad)->where('id_user', '=', $user->id_user)->where('status', '=', Model_Order::STATUS_CREATED)->where('id_product', '=', $id_product)->where('amount', '=', $amount)->where('currency', '=', $currency)->limit(1)->find();
     //if no unpaid create order
     if (!$order->loaded()) {
         //add coupon ID and discount only if not AD_SELL
         if (Model_Coupon::valid($id_product)) {
             $amount = Model_Coupon::price($id_product, $amount);
             $order->id_coupon = Model_Coupon::current()->id_coupon;
         }
         //create order
         $order = new Model_Order();
         $order->id_user = $user->id_user;
         $order->id_ad = $ad->id_ad;
         $order->id_product = $id_product;
         $order->currency = $currency;
         $order->amount = $amount;
         $order->description = $description;
         //store how many days the ad is featured
         if ($featured_days !== NULL and is_numeric($featured_days)) {
             $order->featured_days = $featured_days;
         }
         try {
             $order->save();
         } catch (Exception $e) {
             throw HTTP_Exception::factory(500, $e->getMessage());
         }
         //send email to user with link to pay
         $url_checkout = $user->ql('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order));
         $replace = array('[ORDER.ID]' => $order->id_order, '[ORDER.DESC]' => $order->description, '[URL.CHECKOUT]' => $url_checkout);
         //$user->email('new-order',$replace);
     }
     return $order;
 }
Example #9
0
 /**
  * returns and order for the given product, great to check if was paid or not
  * @param  int  $id_product Model_Order::PRODUCT_
  * @return boolean/Model_Order             false if not found, Model_Order if found
  */
 public function get_order($id_product = Model_Order::PRODUCT_CATEGORY)
 {
     if ($this->loaded()) {
         //get if theres an unpaid order for this product and this ad
         $order = new Model_Order();
         $order->where('id_ad', '=', $this->id_ad)->where('id_user', '=', $this->user->id_user)->where('id_product', '=', $id_product)->limit(1)->find();
         return $order->loaded() ? $order : FALSE;
     }
     return FALSE;
 }
Example #10
0
 /**
  * Number of product purchased
  * @return int
  */
 public function number_of_orders()
 {
     //get all orders
     if ($this->loaded()) {
         $orders = new Model_Order();
         $number_of_orders = $orders->where('id_product', '=', $this->id_product)->find_all()->count();
         return $number_of_orders;
     }
     return FALSE;
 }
Example #11
0
 public function action_order()
 {
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('My Purchases'))->set_url(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'orders'))));
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Order')));
     $this->template->title = __('View Order');
     $user = Auth::instance()->get_user();
     $id_order = $this->request->param('id');
     $order = new Model_Order();
     $order->where('id_order', '=', $id_order);
     //if admin we do not verify the user
     if ($user->id_role != Model_Role::ROLE_ADMIN) {
         $order->where('id_user', '=', $user->id_user);
     }
     $order->find();
     if (!$order->loaded()) {
         Alert::set(ALERT::WARNING, __('Order could not be loaded'));
         $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'orders')));
     }
     $this->template->bind('content', $content);
     $this->template->content = View::factory('oc-panel/profile/order');
     $content->order = $order;
     $content->product = $order->id_product;
     $content->user = $user;
     if (core::get('print') == 1) {
         $this->template->scripts['footer'] = array('js/oc-panel/order.js');
     }
 }
Example #12
0
 /**
  * 
  * Display reviews advert. 
  * @throws HTTP_Exception_404
  * 
  */
 public function action_reviews()
 {
     $seotitle = $this->request->param('seotitle', NULL);
     if ($seotitle !== NULL and Core::config('advertisement.reviews') == 1) {
         $ad = new Model_Ad();
         $ad->where('seotitle', '=', $seotitle)->where('status', '!=', Model_Ad::STATUS_SPAM)->limit(1)->cached()->find();
         if ($ad->loaded()) {
             $errors = NULL;
             //adding a new review
             if ($this->request->post() and Auth::instance()->logged_in()) {
                 $user = Auth::instance()->get_user();
                 //only able to review if bought the product
                 if (Core::config('advertisement.reviews_paid') == 1) {
                     $order = new Model_Order();
                     $order->where('id_ad', '=', $ad->id_ad)->where('id_user', '=', $user->id_user)->where('id_product', '=', Model_Order::PRODUCT_AD_SELL)->where('status', '=', Model_Order::STATUS_PAID)->find();
                     if (!$order->loaded()) {
                         Alert::set(Alert::ERROR, __('You can only add a review if you bought this product'));
                         $this->redirect(Route::url('ad-review', array('seotitle' => $ad->seotitle)));
                     }
                 }
                 //not allowing to review to yourself
                 if ($user->id_user == $ad->id_user) {
                     Alert::set(Alert::ERROR, __('You can not review yourself.'));
                     $this->redirect(Route::url('ad-review', array('seotitle' => $ad->seotitle)));
                 }
                 $review = new Model_Review();
                 $review->where('id_ad', '=', $ad->id_ad)->where_open()->or_where('id_user', '=', $user->id_user)->or_where('ip_address', '=', ip2long(Request::$client_ip))->where_close()->find();
                 //d($review);
                 if (!$review->loaded()) {
                     if (captcha::check('review')) {
                         $validation = Validation::factory($this->request->post())->rule('rate', 'numeric')->rule('description', 'not_empty')->rule('description', 'min_length', array(':value', 5))->rule('description', 'max_length', array(':value', 1000));
                         if ($validation->check()) {
                             $rate = core::post('rate');
                             if ($rate > Model_Review::RATE_MAX) {
                                 $rate = Model_Review::RATE_MAX;
                             } elseif ($rate < 0) {
                                 $rate = 0;
                             }
                             $review = new Model_Review();
                             $review->id_user = $user->id_user;
                             $review->id_ad = $ad->id_ad;
                             $review->description = core::post('description');
                             $review->status = Model_Review::STATUS_ACTIVE;
                             $review->ip_address = ip2long(Request::$client_ip);
                             $review->rate = $rate;
                             $review->save();
                             //email product owner?? notify him of new review
                             $ad->user->email('ad-review', array('[AD.TITLE]' => $ad->title, '[RATE]' => $review->rate, '[DESCRIPTION]' => $review->description, '[URL.QL]' => $ad->user->ql('ad-review', array('seotitle' => $ad->seotitle))));
                             $ad->recalculate_rate();
                             $ad->user->recalculate_rate();
                             Alert::set(Alert::SUCCESS, __('Thanks for your review!'));
                         } else {
                             $errors = $validation->errors('ad');
                             foreach ($errors as $f => $err) {
                                 Alert::set(Alert::ALERT, $err);
                             }
                         }
                     } else {
                         Alert::set(Alert::ERROR, __('Wrong Captcha'));
                     }
                 } else {
                     Alert::set(Alert::ERROR, __('You already added a review'));
                 }
             }
             $this->template->scripts['footer'][] = 'js/jquery.raty.min.js';
             $this->template->scripts['footer'][] = 'js/review.js';
             Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Home'))->set_url(Route::url('default')));
             Breadcrumbs::add(Breadcrumb::factory()->set_title($ad->title)->set_url(Route::url('ad', array('seotitle' => $ad->seotitle, 'category' => $ad->category->seoname))));
             $this->template->title = $ad->title . ' - ' . __('Reviews');
             Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Reviews')));
             $this->template->meta_description = text::removebbcode($ad->description);
             $permission = TRUE;
             //permission to add hit to advert and give access rights.
             $auth_user = Auth::instance();
             if (!$auth_user->logged_in() or $auth_user->get_user()->id_user != $ad->id_user and ($auth_user->get_user()->id_role != Model_Role::ROLE_ADMIN and $auth_user->get_user()->id_role != Model_Role::ROLE_MODERATOR) or $auth_user->get_user()->id_role != Model_Role::ROLE_ADMIN and $auth_user->get_user()->id_role != Model_Role::ROLE_MODERATOR) {
                 $permission = FALSE;
                 $user = NULL;
             } else {
                 $user = $auth_user->get_user()->id_user;
             }
             $captcha_show = core::config('advertisement.captcha');
             if ($ad->get_first_image() !== NULL) {
                 Controller::$image = $ad->get_first_image();
             }
             $reviews = new Model_Review();
             $reviews = $reviews->where('id_ad', '=', $ad->id_ad)->where('status', '=', Model_Review::STATUS_ACTIVE)->find_all();
             $this->template->bind('content', $content);
             $this->template->content = View::factory('pages/ad/reviews', array('ad' => $ad, 'permission' => $permission, 'captcha_show' => $captcha_show, 'user' => $user, 'reviews' => $reviews, 'errors' => $errors));
         } else {
             //throw 404
             throw HTTP_Exception::factory(404, __('Page not found'));
         }
     } else {
         //throw 404
         throw HTTP_Exception::factory(404, __('Page not found'));
     }
 }
Example #13
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');
     //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));
 }
Example #14
0
 public function action_affiliate()
 {
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Affiliate')));
     $this->template->title = __('Affiliate Panel');
     $user = Auth::instance()->get_user();
     //Hack so the admin can see the stats for any user! cool!
     if ($user->id_role == Model_Role::ROLE_ADMIN) {
         $id_user = $this->request->param('id');
         if (is_numeric($id_user)) {
             $user = new Model_User($id_user);
         }
     }
     $this->template->styles = array('//cdn.jsdelivr.net/bootstrap.datepicker/0.1/css/datepicker.css' => 'screen');
     $this->template->scripts['footer'] = array('//cdn.jsdelivr.net/bootstrap.datepicker/0.1/js/bootstrap-datepicker.js', 'js/oc-panel/stats/dashboard.js');
     $this->template->bind('content', $content);
     $this->template->content = View::factory('oc-panel/profile/affiliate');
     $content->user = $user;
     // list of all products to build affiliate links
     $products = new Model_Product();
     $products = $products->where('status', '=', Model_Product::STATUS_ACTIVE)->find_all();
     $content->products = $products;
     //change paypal account->profile, put a warning if he didnt set it yet with a link
     if (!valid::email($user->paypal_email)) {
         Alert::set(Alert::INFO, __('Please set your paypal email at your profile'));
     }
     //list all his payments->orders paid
     $payments = new Model_Order();
     $content->payments = $payments->where('id_user', '=', $user->id_user)->where('id_product', 'is', NULL)->where('status', '=', Model_Order::STATUS_PAID)->order_by('pay_date', 'DESC')->find_all();
     //see stats
     ////////////////////
     //total earned commissions
     $query = DB::select(DB::expr('SUM(amount) total'))->from('affiliates')->where('id_user', '=', $user->id_user)->group_by('id_user')->execute();
     $total_earnings = $query->as_array();
     $content->total_earnings = isset($total_earnings[0]['total']) ? $total_earnings[0]['total'] : 0;
     //total since last payment
     $last_payment_date = DB::select('pay_date')->from('orders')->where('id_user', '=', $user->id_user)->where('id_product', 'is', NULL)->where('status', '=', Model_Order::STATUS_PAID)->order_by('pay_date', 'ASC')->limit(1)->execute();
     $last_payment_date = $last_payment_date->as_array();
     $content->last_payment_date = isset($last_payment_date[0]['pay_date']) ? $last_payment_date[0]['pay_date'] : NULL;
     $content->last_earnings = 0;
     if ($content->last_payment_date != NULL) {
         //commissions since last payment
         $query = DB::select(DB::expr('SUM(amount) total'))->from('affiliates')->where('id_user', '=', $user->id_user)->where('created', 'between', array($content->last_payment_date, Date::unix2mysql()))->where('status', '=', Model_Affiliate::STATUS_CREATED)->group_by('id_user')->execute();
         $last_earnings = $query->as_array();
         $content->last_earnings = isset($last_earnings[0]['total']) ? $last_earnings[0]['total'] : 0;
     }
     //due to pay, is commisions with to pay date bigger than today
     //commissions due to pay
     $query = DB::select(DB::expr('SUM(amount) total'))->from('affiliates')->where('id_user', '=', $user->id_user)->where('date_to_pay', '<', Date::unix2mysql())->where('status', '=', Model_Affiliate::STATUS_CREATED)->group_by('id_user')->execute();
     $due_to_pay = $query->as_array();
     $content->due_to_pay = isset($due_to_pay[0]['total']) ? $due_to_pay[0]['total'] : 0;
     //Getting the dates and range
     $from_date = Core::post('from_date', strtotime('-1 month'));
     $to_date = Core::post('to_date', time());
     //we assure is a proper time stamp if not we transform it
     if (is_string($from_date) === TRUE) {
         $from_date = strtotime($from_date);
     }
     if (is_string($to_date) === TRUE) {
         $to_date = strtotime($to_date);
     }
     //mysql formated dates
     $my_from_date = Date::unix2mysql($from_date);
     $my_to_date = Date::unix2mysql($to_date);
     //dates range we are filtering
     $dates = Date::range($from_date, $to_date, '+1 day', 'Y-m-d', array('date' => 0, 'count' => 0), 'date');
     //dates displayed in the form
     $content->from_date = date('Y-m-d', $from_date);
     $content->to_date = date('Y-m-d', $to_date);
     //visits created last XX days
     $query = DB::select(DB::expr('DATE(created) date'))->select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('id_affiliate', '=', $user->id_user)->where('created', 'between', array($my_from_date, $my_to_date))->group_by(DB::expr('DATE( created )'))->order_by('date', 'asc')->execute();
     $visits = $query->as_array('date');
     //commissions created last XX days
     $query = DB::select(DB::expr('DATE(created) date'))->select(DB::expr('SUM(amount) total'))->from('affiliates')->where('id_user', '=', $user->id_user)->where('created', 'between', array($my_from_date, $my_to_date))->group_by(DB::expr('DATE( created )'))->order_by('date', 'asc')->execute();
     $earnings = $query->as_array('date');
     $stats_daily = array();
     foreach ($dates as $date) {
         $count_views = isset($visits[$date['date']]['count']) ? $visits[$date['date']]['count'] : 0;
         $earned = isset($earnings[$date['date']]['total']) ? $earnings[$date['date']]['total'] : 0;
         $stats_daily[] = array('date' => $date['date'], 'views' => $count_views, '$' => $earned);
     }
     $content->stats_daily = $stats_daily;
     ////////////////////////////////////////////////
     //list paginated with commissions
     /////////////////////////////////
     $commissions = new Model_Affiliate();
     $commissions = $commissions->where('id_user', '=', $user->id_user);
     $pagination = Pagination::factory(array('view' => 'oc-panel/crud/pagination', 'total_items' => $commissions->count_all(), 'items_per_page' => 100))->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action(), 'id' => $this->request->param('id')));
     $pagination->title($this->template->title);
     $commissions = $commissions->order_by('created', 'desc')->limit($pagination->items_per_page)->offset($pagination->offset)->find_all();
     $pagination = $pagination->render();
     $content->pagination = $pagination;
     $content->commissions = $commissions;
     //////////////////////////////////
 }
Example #15
0
 protected function generateEnrolData($iUserId, $iWeekNumber = 0)
 {
     // gdy nie wybrano zadnej daty
     $iDay = date('j');
     $iMonth = date('n');
     $iYear = date('Y');
     $iTime = time();
     // jezeli wybrano inny tydzien
     if ($iWeekNumber != 0) {
         $iWeekNumber = (int) $iWeekNumber;
         $iTime = mktime(0, 0, 0, $iMonth, $iDay + $iWeekNumber * 7, $iYear);
         $iDay = date('j', $iTime);
         $iMonth = date('n', $iTime);
         $iYear = date('Y', $iTime);
     }
     $iWeekDay = date('w', $iTime) == 0 ? 7 : date('w', $iTime);
     // obliczamy poczatek i koniec tygodnia
     $iStartTime = mktime(0, 0, 0, $iMonth, $iDay - ($iWeekDay - 1), $iYear);
     $sStartDate = date('Y-m-d', $iStartTime);
     $iEndTime = mktime(0, 0, 0, $iMonth, $iDay + (7 - $iWeekDay), $iYear);
     $sEndDate = date('Y-m-d', $iEndTime);
     $aWeek = array();
     $oMeal = new Model_Meal();
     // zapisujemy
     if (isset($_POST['submit']) and !isset($_POST['user_id'])) {
         $aMeals = $oMeal->getMeals($this->oCurrentUser->account_id, $sStartDate, $sEndDate);
         $bSaved = $this->saveOrders($this->oCurrentUser->account_id, $iUserId, $aMeals);
         if ($bSaved) {
             $aMeta = $this->mTemplate->aMeta;
             $aMeta[] = '<meta http-equiv="refresh" content="1;url=' . $this->mTemplate->anchor() . '" />';
             $this->mTemplate->aMeta = $aMeta;
             return $this->getLang('Catering.save_meals_successfully');
         } else {
             return $this->getLang('Catering.save_meals_failed');
         }
     }
     // wyszukujemy posików i grupujemy po dacie
     $aMeals = $oMeal->getMeals($this->oCurrentUser->account_id, $sStartDate, $sEndDate, 'date');
     // wyszukujemy zamowienia
     $aOrderedMeals = array();
     $oOrder = new Model_Order();
     $aOrders = $oOrder->where('user_id', $iUserId)->getAll();
     foreach ($aOrders as $aOrder) {
         $aOrderedMeals[] = $aOrder['meal_id'];
     }
     foreach ($aMeals as $sDate => $aMeal) {
         $aWeek[$sDate]['aMeals'][0] = array('name' => $this->getLang('Catering.no_order'), 'meal_id' => 0, 'optional' => 0, 'bChecked' => false, 'bDisabled' => false);
         $aWeek[$sDate]['sWeekday'] = date('l', strtotime($sDate));
         foreach ($aMeal as $iMealId => $aMealData) {
             $aWeek[$sDate]['aMeals'][] = array('name' => $aMealData['lname'], 'fname' => $aMealData['fname'], 'meal_id' => $iMealId, 'price' => $aMealData['price'] . $this->getLang('Catering.currency'), 'optional' => $aMealData['optional'], 'bChecked' => in_array($iMealId, $aOrderedMeals) ? true : false, 'bDisabled' => false);
         }
     }
     $aData['aWeek'] = $aWeek;
     $aData['sNextText'] = $this->getLang('Catering.next');
     $aData['sNextLink'] = '/account/orders/' . $iUserId . '/' . ($iWeekNumber + 1) . '/';
     $aData['sPrevText'] = $this->getLang('Catering.prev');
     $aData['sPrevLink'] = '/account/orders/' . $iUserId . '/' . ($iWeekNumber - 1) . '/';
     $aData['submit'] = 'Zapisz';
     return $aData;
 }