/**
  * expired featured ads
  * @return void
  */
 public static function renew()
 {
     if (Core::config('general.subscriptions') == TRUE) {
         //get expired subscription that are active
         $subscriptions = new Model_Subscription();
         $subscriptions = $subscriptions->where('status', '=', 1)->where('expire_date', '<=', Date::unix2mysql())->order_by('created', 'desc')->find_all();
         foreach ($subscriptions as $s) {
             //disable the plan
             $s->status = 0;
             try {
                 $s->save();
             } catch (Exception $e) {
                 throw HTTP_Exception::factory(500, $e->getMessage());
             }
             $plan = $s->plan;
             if ($plan->loaded() and $plan->status == 1) {
                 //generate a new order
                 $order = Model_Order::new_order(NULL, $s->user, $plan->id_plan, $plan->price, core::config('payment.paypal_currency'), __('Subscription to ') . $plan->name);
                 //free plan no checkout
                 if ($plan->price == 0) {
                     $order->confirm_payment('cash');
                 } else {
                     $checkout_url = $s->user->ql('default', array('controller' => 'plan', 'action' => 'checkout', 'id' => $order->id_order));
                     $s->user->email('plan-expired', array('[PLAN.NAME]' => $plan->name, '[URL.CHECKOUT]' => $checkout_url));
                 }
             }
             //if plan loaded
         }
         //end foreach
     }
     //if subscription active
 }
Esempio n. 2
0
 /**
  * Automatically executed before the widget action. Can be used to set
  * class properties, do authorization checks, and execute other custom code.
  *
  * @return  void
  */
 public function before()
 {
     $ads = new Model_Ad();
     $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED);
     //if ad have passed expiration time dont show
     if (core::config('advertisement.expire_date') > 0) {
         $ads->where(DB::expr('DATE_ADD( published, INTERVAL ' . core::config('advertisement.expire_date') . ' DAY)'), '>', Date::unix2mysql());
     }
     switch ($this->ads_type) {
         case 'popular':
             $id_ads = array_keys(Model_Visit::popular_ads());
             if (count($id_ads) > 0) {
                 $ads->where('id_ad', 'IN', $id_ads);
             }
             break;
         case 'featured':
             $ads->where('featured', 'IS NOT', NULL)->where('featured', '>', Date::unix2mysql())->order_by('featured', 'desc');
             break;
         case 'latest':
         default:
             $ads->order_by('published', 'desc');
             break;
     }
     $ads = $ads->limit($this->ads_limit)->cached()->find_all();
     //die(print_r($ads));
     $this->ads = $ads;
 }
Esempio n. 3
0
 public function action_index()
 {
     //template header
     $this->template->title = '';
     // $this->template->meta_keywords    = 'keywords';
     $this->template->meta_description = Core::config('general.site_description');
     //setting main view/template and render pages
     // swith to decide on ads_in_home
     $ads = new Model_Ad();
     $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED);
     switch (core::config('advertisement.ads_in_home')) {
         case 2:
             $id_ads = array_keys(Model_Visit::popular_ads());
             if (count($id_ads) > 0) {
                 $ads->where('id_ad', 'IN', $id_ads);
             }
             break;
         case 1:
             $ads->where('featured', 'IS NOT', NULL)->where('featured', 'BETWEEN', array(DB::expr('NOW()'), Date::unix2mysql(time() + core::config('payment.featured_days') * 24 * 60 * 60)))->order_by('featured', 'desc');
             break;
         case 0:
         default:
             $ads->order_by('published', 'desc');
             break;
     }
     //if ad have passed expiration time dont show
     if (core::config('advertisement.expire_date') > 0) {
         $ads->where(DB::expr('DATE_ADD( published, INTERVAL ' . core::config('advertisement.expire_date') . ' DAY)'), '>', DB::expr('NOW()'));
     }
     $ads = $ads->limit(Theme::get('num_home_latest_ads', 4))->cached()->find_all();
     $this->ads = $ads;
     $categs = Model_Category::get_category_count();
     $this->template->bind('content', $content);
     $this->template->content = View::factory('pages/home', array('ads' => $ads, 'categs' => $categs));
 }
Esempio n. 4
0
 /**
  * Automatically executed before the widget action. Can be used to set
  * class properties, do authorization checks, and execute other custom code.
  *
  * @return  void
  */
 public function before()
 {
     $ads = new Model_Ad();
     $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED);
     $ads->where('featured', 'IS NOT', NULL)->where('featured', '>', Date::unix2mysql())->order_by('featured', 'desc');
     $ads = $ads->limit($this->ads_limit)->find_all();
     $this->ads = $ads;
 }
Esempio n. 5
0
 /**
  * Automatically executed before the widget action. Can be used to set
  * class properties, do authorization checks, and execute other custom code.
  *
  * @return  void
  */
 public function before()
 {
     $ads = new Model_Ad();
     $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED);
     $ads->where('featured', 'IS NOT', NULL)->where('featured', 'BETWEEN', array(DB::expr('NOW()'), Date::unix2mysql(time() + core::config('payment.featured_days') * 24 * 60 * 60)))->order_by('featured', 'desc');
     $ads = $ads->limit($this->ads_limit)->cached()->find_all();
     $this->ads = $ads;
 }
 public function action_index()
 {
     if (core::config('general.auto_locate')) {
         Theme::$scripts['footer'][] = '//maps.google.com/maps/api/js?sensor=false&libraries=geometry&v=3.7';
         Theme::$scripts['footer'][] = '//cdn.jsdelivr.net/gmaps/0.4.15/gmaps.min.js';
     }
     //template header
     $this->template->title = '';
     // $this->template->meta_keywords    = 'keywords';
     if (core::config('general.site_description') != '') {
         $this->template->meta_description = core::config('general.site_description');
     } else {
         $this->template->meta_description = core::config('general.site_name') . ' ' . __('official homepage, get your post listed now.');
     }
     //setting main view/template and render pages
     // swith to decide on ads_in_home
     $ads = new Model_Ad();
     $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED);
     $ads_in_home = core::config('advertisement.ads_in_home');
     //in case we do not count visits we cant show popular
     if (core::config('advertisement.count_visits') == 0 and $ads_in_home == 2) {
         $ads_in_home = 0;
     }
     switch ($ads_in_home) {
         case 2:
             $id_ads = array_keys(Model_Visit::popular_ads());
             if (count($id_ads) > 0) {
                 $ads->where('id_ad', 'IN', $id_ads);
             }
             break;
         case 1:
             $ads->where('featured', 'IS NOT', NULL)->where('featured', '>=', Date::unix2mysql())->order_by('featured', 'desc');
             break;
         case 4:
             $ads->where('featured', 'IS NOT', NULL)->where('featured', '>=', Date::unix2mysql())->order_by(DB::expr('RAND()'));
             break;
         case 0:
         default:
             $ads->order_by('published', 'desc');
             break;
     }
     //if ad have passed expiration time dont show
     if (core::config('advertisement.expire_date') > 0) {
         $ads->where(DB::expr('DATE_ADD( published, INTERVAL ' . core::config('advertisement.expire_date') . ' DAY)'), '>', Date::unix2mysql());
     }
     $ads = $ads->limit(Theme::get('num_home_latest_ads', 4))->cached()->find_all();
     $categs = Model_Category::get_category_count();
     $locats = Model_Location::get_location_count();
     $auto_locats = NULL;
     if (core::config('general.auto_locate') and Model_User::get_userlatlng()) {
         $auto_locats = new Model_Location();
         $auto_locats = $auto_locats->select(array(DB::expr('degrees(acos(sin(radians(' . $_COOKIE['mylat'] . ')) * sin(radians(`latitude`)) + cos(radians(' . $_COOKIE['mylat'] . ')) * cos(radians(`latitude`)) * cos(radians(abs(' . $_COOKIE['mylng'] . ' - `longitude`))))) * 111.321'), 'distance'))->where('latitude', 'IS NOT', NULL)->where('longitude', 'IS NOT', NULL)->having('distance', '<=', '100')->order_by('distance', 'desc')->find_all()->as_array();
     }
     $this->template->bind('content', $content);
     $this->template->content = View::factory('pages/home', array('ads' => $ads, 'categs' => $categs, 'locats' => $locats, 'auto_locats' => $auto_locats));
 }
Esempio n. 7
0
 /**
  * Insert a new object to the database - Overwrite!
  * @param  Validation $validation Validation object
  * @throws Kohana_Exception
  * @return ORM
  */
 public function create(Validation $validation = NULL)
 {
     //Hack to use the PHP date time instead of the MySQL for created
     $cols = $this->list_columns();
     // the column created exists and we didnt pass any value before
     if (isset($cols['created']) and !array_key_exists('created', $this->_changed)) {
         //add the value, forcing it so wont use the DB default ;)
         $this->set('created', Date::unix2mysql());
     }
     return parent::create($validation);
 }
Esempio n. 8
0
 public function action_index()
 {
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Newsletter')));
     $this->template->title = __('Newsletter');
     //count all users
     $user = new Model_User();
     $user->where('status', '=', Model_User::STATUS_ACTIVE);
     $count_all_users = $user->count_all();
     //count support expired
     $query = DB::select(DB::expr('COUNT(id_order) count'))->from('orders')->where('status', '=', Model_Order::STATUS_PAID)->where('support_date', '<', Date::unix2mysql())->execute();
     $count_support_expired = $query->as_array();
     $count_support_expired = $count_support_expired[0]['count'];
     //count license expired
     $query = DB::select(DB::expr('COUNT(id_license) count'))->from('licenses')->where('valid_date', 'IS NOT', NULL)->where('valid_date', '<', Date::unix2mysql())->execute();
     $count_license_expired = $query->as_array();
     $count_license_expired = $count_license_expired[0]['count'];
     //orders per product, not accuarate since 1 user could buy more than 1 product but will do
     $query = DB::select(DB::expr('COUNT(id_order) count'))->select('p.title')->select('p.id_product')->from(array('products', 'p'))->join(array('orders', 'o'))->using('id_product')->where('o.status', '=', Model_Order::STATUS_PAID)->group_by('p.id_product')->execute();
     $products = $query->as_array();
     //post done sending newsletter
     if ($this->request->post() and Core::post('subject') != NULL) {
         $users = array();
         if (core::post('send_all') == 'on') {
             $query = DB::select('email')->select('name')->from('users')->where('status', '=', Model_User::STATUS_ACTIVE)->execute();
             $users = array_merge($users, $query->as_array());
         }
         if (Theme::get('premium') == 1) {
             if (core::post('send_expired_support') == 'on') {
                 $query = DB::select('email')->select('name')->from(array('users', 'u'))->join(array('orders', 'o'))->using('id_user')->where('o.status', '=', Model_Order::STATUS_PAID)->where('o.support_date', '<', Date::unix2mysql())->where('u.subscriber', '=', 1)->group_by('u.id_user')->execute();
                 $users = array_merge($users, $query->as_array());
             }
             if (core::post('send_expired_license') == 'on') {
                 $query = DB::select('email')->select('name')->from(array('licenses', 'l'))->join(array('users', 'u'))->using('id_user')->where('l.valid_date', 'IS NOT', NULL)->where('l.valid_date', '<', Date::unix2mysql())->where('u.subscriber', '=', 1)->group_by('u.id_user')->execute();
                 $users = array_merge($users, $query->as_array());
             }
             if (is_numeric(core::post('send_product'))) {
                 $query = DB::select('email')->select('name')->from(array('users', 'u'))->join(array('orders', 'o'))->using('id_user')->where('o.id_product', '=', core::post('send_product'))->where('o.status', '=', Model_Order::STATUS_PAID)->where('u.subscriber', '=', 1)->group_by('u.id_user')->execute();
                 $users = array_merge($users, $query->as_array());
             }
         }
         //NOTE $users may have duplicated emails, but phpmailer takes care of not sending the email 2 times to same recipient
         //sending!
         if (count($users) > 0) {
             if (!Email::send($users, '', Core::post('subject'), Kohana::$_POST_ORIG['description'], Core::post('from'), Core::post('from_email'))) {
                 Alert::set(Alert::ERROR, __('Error on mail delivery, not sent'));
             } else {
                 Alert::set(Alert::SUCCESS, __('Email sent'));
             }
         } else {
             Alert::set(Alert::ERROR, __('Mail not sent'));
         }
     }
     $this->template->content = View::factory('oc-panel/pages/newsletter', array('count_all_users' => $count_all_users, 'count_support_expired' => $count_support_expired, 'count_license_expired' => $count_license_expired, 'products' => $products));
 }
Esempio n. 9
0
 /**
  * expired featured ads
  * @return void
  */
 public static function renew()
 {
     if (Core::config('general.subscriptions') == TRUE) {
         //get expired subscription that are active
         $subscriptions = new Model_Subscription();
         $subscriptions = $subscriptions->where('status', '=', 1)->where('expire_date', '<=', Date::unix2mysql())->order_by('created', 'desc')->find_all();
         foreach ($subscriptions as $s) {
             //disable the plan
             $s->status = 0;
             try {
                 $s->save();
             } catch (Exception $e) {
                 throw HTTP_Exception::factory(500, $e->getMessage());
             }
             $plan = $s->plan;
             if ($plan->loaded() and $plan->status == 1) {
                 //generate a new order
                 $order = Model_Order::new_order(NULL, $s->user, $plan->id_plan, $plan->price, core::config('payment.paypal_currency'), __('Subscription to ') . $plan->name);
                 //free plan no checkout
                 if ($plan->price == 0) {
                     $order->confirm_payment('cash');
                 } else {
                     $paid = FALSE;
                     //customers who paid with sripe we can charge the recurrency
                     if ($order->user->stripe_agreement != NULL) {
                         StripeKO::init();
                         // Create the charge on Stripe's servers - this will charge the user's card
                         try {
                             $charge = \Stripe\Charge::create(array("amount" => StripeKO::money_format($order->amount), "currency" => $order->currency, 'customer' => $order->user->stripe_agreement, "description" => $order->description, "metadata" => array("id_order" => $order->id_order)));
                             $paid = TRUE;
                         } catch (Exception $e) {
                             // The card has been declined
                             Kohana::$log->add(Log::ERROR, 'Stripe The card has been declined');
                             $paid = FALSE;
                         }
                     }
                     if ($paid === TRUE) {
                         //mark as paid
                         $order->confirm_payment('stripe', $charge->id);
                     } else {
                         $checkout_url = $s->user->ql('default', array('controller' => 'plan', 'action' => 'checkout', 'id' => $order->id_order));
                         $s->user->email('plan-expired', array('[PLAN.NAME]' => $plan->name, '[URL.CHECKOUT]' => $checkout_url));
                     }
                 }
             }
             //if plan loaded
         }
         //end foreach
     }
     //if subscription active
 }
Esempio n. 10
0
 /**
  * expired featured ads
  * @return void
  */
 public static function expired_featured()
 {
     //find expired ads of yesterday
     $ads = new Model_Ad();
     $ads = $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED)->where('featured', '<', Date::unix2mysql())->where('featured', 'IS NOT', NULL)->find_all();
     foreach ($ads as $ad) {
         $edit_url = $ad->user->ql('oc-panel', array('controller' => 'myads', 'action' => 'update', 'id' => $ad->id_ad));
         $ad->user->email('ad-expired', array('[AD.NAME]' => $ad->title, '[URL.EDITAD]' => $edit_url));
         //unset featured ad
         $ad->featured = NULL;
         try {
             $ad->save();
         } catch (Exception $e) {
             throw HTTP_Exception::factory(500, $e->getMessage());
         }
     }
 }
Esempio n. 11
0
 /**
  *
  * view affiliates and payments
  */
 public function action_pay()
 {
     //create an order and mark it as paid to the user_id
     if (is_numeric($this->request->param('id'))) {
         //get the user
         $user = new Model_User($this->request->param('id'));
         if ($user->loaded()) {
             //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();
             $due_to_pay = isset($due_to_pay[0]['total']) ? $due_to_pay[0]['total'] : 0;
             if ($due_to_pay > 0) {
                 //create the order
                 $order = new Model_Order();
                 $order->id_user = $user->id_user;
                 $order->amount = $due_to_pay * -1;
                 //we add the order as a negative, since we pay, we don't get paid.
                 $order->currency = 'USD';
                 $order->paymethod = 'paypal';
                 $order->pay_date = Date::unix2mysql();
                 $order->notes = 'Affiliate Commissions';
                 $order->status = Model_Order::STATUS_PAID;
                 try {
                     $order->save();
                     //update the commissions
                     DB::update('affiliates')->set(array('date_paid' => Date::unix2mysql(), 'status' => Model_Affiliate::STATUS_PAID, 'id_order_payment' => $order->id_order))->where('id_user', '=', $user->id_user)->where('date_to_pay', '<', Date::unix2mysql())->where('status', '=', Model_Affiliate::STATUS_CREATED)->execute();
                     Alert::set(Alert::SUCCESS, __('Commission Paid'));
                 } catch (Exception $e) {
                 }
             }
         }
     }
     $this->template->title = __('Affiliates Payments');
     $query = DB::select(DB::expr('SUM(amount) total'))->select('id_user')->from('affiliates')->where('date_to_pay', '<', Date::unix2mysql())->where('status', '=', Model_Affiliate::STATUS_CREATED)->group_by('id_user')->having('total', '>=', core::config('affiliate.payment_min'))->execute();
     $users_to_pay = $query->as_array('id_user');
     $total_to_pay = 0;
     foreach ($users_to_pay as $key => $value) {
         $total_to_pay += $value['total'];
     }
     $users = new Model_User();
     if (count($users_to_pay)) {
         $users = $users->where('id_user', 'in', array_keys($users_to_pay))->where('status', '=', Model_User::STATUS_ACTIVE)->find_all();
     }
     $this->render('oc-panel/pages/affiliate/pay', array('users' => $users, 'total_to_pay' => $total_to_pay, 'users_to_pay' => $users_to_pay));
 }
Esempio n. 12
0
 public function action_index()
 {
     $this->before('/pages/maps');
     $this->template->title = __('Map');
     $this->template->height = Core::get('height', '100%');
     $this->template->width = Core::get('width', '100%');
     $this->template->zoom = Core::get('zoom', core::config('advertisement.map_zoom'));
     $this->template->height_thumb = Core::config('image.height_thumb') / 4;
     $this->template->width_thumb = Core::config('image.width_thumb') / 4;
     if (Model_User::get_userlatlng()) {
         $this->template->center_lon = $_COOKIE['mylng'];
         $this->template->center_lat = $_COOKIE['mylat'];
     } else {
         $this->template->center_lon = Core::get('lon', core::config('advertisement.center_lon'));
         $this->template->center_lat = Core::get('lat', core::config('advertisement.center_lat'));
     }
     $ads = new Model_Ad();
     $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED)->where('address', 'IS NOT', NULL)->where('latitude', 'IS NOT', NULL)->where('longitude', 'IS NOT', NULL);
     //filter by category
     if (core::get('category') !== NULL) {
         $category = new Model_Category();
         $category->where('seoname', '=', core::get('category'))->cached()->limit(1)->find();
         if ($category->loaded()) {
             $ads->where('id_category', 'IN', $category->get_siblings_ids());
         }
     }
     //filter by location
     if (core::get('location') !== NULL) {
         $location = new Model_location();
         $location->where('seoname', '=', core::get('location'))->cached()->limit(1)->find();
         if ($location->loaded()) {
             $ads->where('id_location', 'IN', $location->get_siblings_ids());
         }
     }
     //if ad have passed expiration time dont show
     if (core::config('advertisement.expire_date') > 0) {
         $ads->where(DB::expr('DATE_ADD( published, INTERVAL ' . core::config('advertisement.expire_date') . ' DAY)'), '>', Date::unix2mysql());
     }
     //if only 1 ad
     if (is_numeric(core::get('id_ad'))) {
         $ads = $ads->where('id_ad', '=', core::get('id_ad'));
     }
     $ads = $ads->order_by('published', 'desc')->limit(Core::config('advertisement.map_elements'))->find_all();
     $this->template->ads = $ads;
 }
Esempio n. 13
0
 /**
  * Handle GET requests.
  */
 public function action_get()
 {
     try {
         if (($coupon_name = $this->request->param('id')) != NULL) {
             $coupon = new Model_Coupon();
             $coupon->where('name', '=', $coupon_name)->where('number_coupons', '>', 0)->where('valid_date', '>', Date::unix2mysql())->where('status', '=', 1);
             //filter by product
             if (is_numeric(core::request('id_product'))) {
                 $coupon->where('id_product', '=', core::request('id_product'));
             }
             $coupon = $coupon->limit(1)->find();
             $this->rest_output(array('coupon' => $coupon->loaded() ? $coupon->as_array() : FALSE));
         } else {
             $this->_error('You need to specify a coupon');
         }
     } catch (Kohana_HTTP_Exception $khe) {
         $this->_error($khe);
     }
 }
Esempio n. 14
0
 /**
  * new order therefore new subscription created
  * @param  Model_Order $order 
  * @return void             
  */
 public static function new_order(Model_Order $order)
 {
     $plan = new Model_Plan($order->id_product);
     //disable all the previous membership
     DB::update('subscriptions')->set(array('status' => 0))->where('id_user', '=', $order->id_user)->execute();
     //create a new subscription for this product
     $subscription = new Model_Subscription();
     $subscription->id_order = $order->id_order;
     $subscription->id_user = $order->id_user;
     $subscription->id_plan = $plan->id_plan;
     $subscription->amount_ads = $plan->amount_ads;
     $subscription->amount_ads_left = $plan->amount_ads;
     $subscription->expire_date = Date::unix2mysql(strtotime('+' . $plan->days . ' days'));
     $subscription->status = 1;
     try {
         $subscription->save();
     } catch (Exception $e) {
         throw HTTP_Exception::factory(500, $e->getMessage());
     }
 }
Esempio n. 15
0
 public function action_index()
 {
     //template header
     $this->template->title = '';
     // $this->template->meta_keywords    = 'keywords';
     $this->template->meta_description = Core::config('general.site_name') . ' ' . __('official homepage for the online store');
     $products = new Model_Product();
     $products->where('status', '=', Model_Product::STATUS_ACTIVE);
     switch (core::config('product.products_in_home')) {
         case 3:
             $id_products = Model_Review::best_rated();
             $array_ids = array();
             foreach ($id_products as $id => $id_product) {
                 $array_ids = $id_product;
             }
             if (count($id_products) > 0) {
                 $products->where('id_product', 'IN', $array_ids);
             }
             break;
         case 2:
             $id_products = array_keys(Model_Visit::popular_products());
             if (count($id_products) > 0) {
                 $products->where('id_product', 'IN', $id_products);
             }
             break;
         case 1:
             $products->where('featured', 'IS NOT', NULL)->where('featured', '>', Date::unix2mysql())->order_by('featured', 'desc');
             break;
         case 0:
         default:
             $products->order_by('created', 'desc');
             break;
     }
     $products = $products->limit(Theme::get('num_home_products', 21))->cached()->find_all();
     $categs = Model_Category::get_category_count();
     $this->template->bind('content', $content);
     $this->template->content = View::factory('pages/home', array('products' => $products, 'categs' => $categs));
 }
Esempio n. 16
0
 /**
  * Automatically executed before the widget action. Can be used to set
  * class properties, do authorization checks, and execute other custom code.
  *
  * @return  void
  */
 public function before()
 {
     $ads = new Model_Ad();
     $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED);
     switch ($this->ads_type) {
         case 'popular':
             $id_ads = array_keys(Model_Visit::popular_ads());
             if (count($id_ads) > 0) {
                 $ads->where('id_ad', 'IN', $id_ads);
             }
             break;
         case 'featured':
             $ads->where('featured', 'IS NOT', NULL)->where('featured', 'BETWEEN', array(DB::expr('NOW()'), Date::unix2mysql(time() + core::config('payment.featured_days') * 24 * 60 * 60)))->order_by('featured', 'desc');
             break;
         case 'latest':
         default:
             $ads->order_by('published', 'desc');
             break;
     }
     $ads = $ads->limit($this->ads_limit)->cached()->find_all();
     //die(print_r($ads));
     $this->ads = $ads;
 }
Esempio n. 17
0
 /**
  * Automatically executed before the widget action. Can be used to set
  * class properties, do authorization checks, and execute other custom code.
  *
  * @return  void
  */
 public function before()
 {
     $products = new Model_Product();
     $products->where('status', '=', Model_Product::STATUS_ACTIVE);
     switch ($this->products_type) {
         case 'popular':
             $id_products = array_keys(Model_Visit::popular_products());
             if (count($id_products) > 0) {
                 $products->where('id_product', 'IN', $id_products);
             }
             break;
         case 'featured':
             $products->where('featured', 'IS NOT', NULL)->where('featured', '>', Date::unix2mysql())->order_by('featured', 'desc');
             break;
         case 'latest':
         default:
             $products->order_by('created', 'desc');
             break;
     }
     $products = $products->limit($this->products_limit)->cached()->find_all();
     //die(print_r($products));
     $this->products = $products;
 }
Esempio n. 18
0
 public function action_icon()
 {
     //get icon
     if (isset($_FILES['category_icon'])) {
         $icon = $_FILES['category_icon'];
     } else {
         $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'index')));
     }
     $category = new Model_Category($this->request->param('id'));
     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'));
     }
     if (core::post('icon_delete') and $category->delete_icon() == TRUE) {
         Alert::set(Alert::SUCCESS, __('Icon deleted.'));
         $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $category->id_category)));
     }
     // end of icon delete
     if (!Upload::valid($icon) or !Upload::not_empty($icon) or !Upload::type($icon, explode(',', core::config('image.allowed_formats'))) or !Upload::size($icon, core::config('image.max_image_size') . 'M')) {
         if (Upload::not_empty($icon) && !Upload::type($icon, explode(',', core::config('image.allowed_formats')))) {
             Alert::set(Alert::ALERT, $icon['name'] . ' ' . sprintf(__('Is not valid format, please use one of this formats "%s"'), core::config('image.allowed_formats')));
             $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $category->id_category)));
         }
         if (!Upload::size($icon, core::config('image.max_image_size') . 'M')) {
             Alert::set(Alert::ALERT, $icon['name'] . ' ' . sprintf(__('Is not of valid size. Size is limited to %s MB per image'), core::config('image.max_image_size')));
             $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $category->id_category)));
         }
         Alert::set(Alert::ALERT, $icon['name'] . ' ' . __('Image is not valid. Please try again.'));
         $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $category->id_category)));
     } else {
         if ($icon != NULL) {
             // saving/uploading img file to dir.
             $path = 'images/categories/';
             $root = DOCROOT . $path;
             //root folder
             $icon_name = $category->seoname . '.png';
             // if folder does not exist, try to make it
             if (!file_exists($root) and !@mkdir($root, 0775, true)) {
                 // mkdir not successful ?
                 Alert::set(Alert::ERROR, __('Image folder is missing and cannot be created with mkdir. Please correct to be able to upload images.'));
                 return;
                 // exit function
             }
             // save file to root folder, file, name, dir
             if ($file = Upload::save($icon, $icon_name, $root)) {
                 // put icon to Amazon S3
                 if (core::config('image.aws_s3_active')) {
                     $s3->putObject($s3->inputFile($file), core::config('image.aws_s3_bucket'), $path . $icon_name, S3::ACL_PUBLIC_READ);
                 }
                 // update category info
                 $category->has_image = 1;
                 $category->last_modified = Date::unix2mysql();
                 $category->save();
                 Alert::set(Alert::SUCCESS, $icon['name'] . ' ' . __('Icon is uploaded.'));
             } else {
                 Alert::set(Alert::ERROR, $icon['name'] . ' ' . __('Icon file could not been saved.'));
             }
             $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $category->id_category)));
         }
     }
 }
Esempio n. 19
0
 public function action_unsubscribe()
 {
     $email_encoded = $this->request->param('id');
     $user = new Model_User();
     //mail encoded
     if ($email_encoded !== NULL) {
         //decode emails
         $email_encoded = Base64::fix_from_url($email_encoded);
         $encrypt = new Encrypt(Core::config('auth.hash_key'), MCRYPT_MODE_NOFB, MCRYPT_RIJNDAEL_128);
         $email = $encrypt->decode($email_encoded);
         if (Valid::email($email, TRUE)) {
             //check we have this email in the DB
             $user = new Model_User();
             $user = $user->where('email', '=', $email)->limit(1)->find();
         } else {
             Alert::set(Alert::INFO, __('Not valid email.'));
         }
     } elseif (Auth::instance()->logged_in()) {
         $user = Auth::instance()->get_user();
     }
     //lets unsubscribe the user
     if ($user->loaded()) {
         $user->subscriber = 0;
         $user->last_modified = Date::unix2mysql();
         try {
             $user->save();
             Alert::set(Alert::SUCCESS, __('You have successfuly unsubscribed'));
         } catch (Exception $e) {
             //throw 500
             throw HTTP_Exception::factory(500, $e->getMessage());
         }
     } else {
         Alert::set(Alert::INFO, __('Pleae login to unsubscribe.'));
     }
     //smart redirect
     if (Auth::instance()->logged_in()) {
         $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'edit')));
     } else {
         $this->redirect(Route::url('default'));
     }
 }
Esempio n. 20
0
 /**
  * creates a new ad
  * @param  array $data 
  * @param  model_user $user 
  * @return array       
  */
 public static function new_ad($data, $user)
 {
     $return_message = '';
     $checkout_url = '';
     //akismet spam filter
     if (isset($data['title']) and isset($data['description']) and core::akismet($data['title'], $user->email, $data['description']) == TRUE) {
         // is user marked as spammer? Make him one :)
         if (core::config('general.black_list')) {
             $user->user_spam();
         }
         return array('error' => __('This post has been considered as spam! We are sorry but we can not publish this advertisement.'), 'error_type' => Alert::ALERT);
     }
     //akismet
     $ad = new Model_Ad();
     $ad->id_user = $user->id_user;
     $ad->values($data);
     $ad->seotitle = $ad->gen_seo_title($ad->title);
     $ad->created = Date::unix2mysql();
     try {
         $ad->save();
     } catch (ORM_Validation_Exception $e) {
         return array('validation_errors' => $e->errors('ad'));
     } catch (Exception $e) {
         return array('error' => $e->getMessage(), 'error_type' => Alert::ALERT);
     }
     /////////// NOTIFICATION Emails,messages to user and Status of the ad
     // depending on user flow (moderation mode), change usecase
     $moderation = core::config('general.moderation');
     //calculate how much he needs to pay in case we have payment on
     if ($moderation == Model_Ad::PAYMENT_ON or $moderation == Model_Ad::PAYMENT_MODERATION) {
         // check category price, if 0 check parent
         if ($ad->category->price == 0) {
             $cat_parent = new Model_Category($ad->category->id_category_parent);
             //category without price
             if ($cat_parent->price == 0) {
                 //swapping moderation since theres no price :(
                 if ($moderation == Model_Ad::PAYMENT_ON) {
                     $moderation = Model_Ad::POST_DIRECTLY;
                 } elseif ($moderation == Model_Ad::PAYMENT_MODERATION) {
                     $moderation = Model_Ad::MODERATION_ON;
                 }
             } else {
                 $amount = $cat_parent->price;
             }
         } else {
             $amount = $ad->category->price;
         }
     }
     //where and what we say to the user depending ont he moderation
     switch ($moderation) {
         case Model_Ad::PAYMENT_ON:
         case Model_Ad::PAYMENT_MODERATION:
             $ad->status = Model_Ad::STATUS_NOPUBLISHED;
             $order = Model_Order::new_order($ad, $user, Model_Order::PRODUCT_CATEGORY, $amount, NULL, Model_Order::product_desc(Model_Order::PRODUCT_CATEGORY) . ' ' . $ad->category->name);
             // redirect to invoice
             $return_message = __('Please pay before we publish your advertisement.');
             $checkout_url = Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order));
             break;
         case Model_Ad::EMAIL_MODERATION:
         case Model_Ad::EMAIL_CONFIRMATION:
             $ad->status = Model_Ad::STATUS_UNCONFIRMED;
             $url_ql = $user->ql('oc-panel', array('controller' => 'myads', 'action' => 'confirm', 'id' => $ad->id_ad));
             $user->email('ads-confirm', array('[URL.QL]' => $url_ql, '[AD.NAME]' => $ad->title));
             $return_message = __('Advertisement is posted but first you need to activate. Please check your email!');
             break;
         case Model_Ad::MODERATION_ON:
             $ad->status = Model_Ad::STATUS_NOPUBLISHED;
             $url_ql = $user->ql('oc-panel', array('controller' => 'myads', 'action' => 'update', 'id' => $ad->id_ad));
             $user->email('ads-notify', array('[URL.QL]' => $url_ql, '[AD.NAME]' => $ad->title));
             // email to notify user of creating, but it is in moderation currently
             $return_message = __('Advertisement is received, but first administrator needs to validate. Thank you for being patient!');
             break;
         case Model_Ad::POST_DIRECTLY:
         default:
             $ad->status = Model_Ad::STATUS_PUBLISHED;
             $ad->published = $ad->created;
             $url_cont = $user->ql('contact');
             $url_ad = $user->ql('ad', array('category' => $ad->category->seoname, 'seotitle' => $ad->seotitle));
             $user->email('ads-user-check', array('[URL.CONTACT]' => $url_cont, '[URL.AD]' => $url_ad, '[AD.NAME]' => $ad->title));
             Model_Subscribe::notify($ad);
             $return_message = __('Advertisement is posted. Congratulations!');
             break;
     }
     //save the last changes on status
     $ad->save();
     //notify admins new ad
     $ad->notify_admins();
     return array('message' => $return_message, 'checkout_url' => $checkout_url, 'ad' => $ad);
 }
Esempio n. 21
0
 /**
  * Total paid orders filtered by product
  * @param  integer    $id_product
  * @param  timestamp  $from_date
  * @param  timestamp  $to_date
  * @param  boolean    $past_period calculate past period (period = $to_date - $from_date)
  * @return integer
  */
 private function paid_orders_by_product($id_product, $from_date, $to_date, $past_period = FALSE)
 {
     if ($past_period) {
         $original_from_date = $from_date;
         $original_to_date = $to_date;
         $from_date = $original_from_date - ($original_to_date - $original_from_date);
         $to_date = $original_to_date - ($original_to_date - $original_from_date);
     }
     $query = DB::select(DB::expr('count(id_product) total'))->from('orders')->where('pay_date', 'between', array(Date::unix2mysql($from_date), Date::unix2mysql($to_date)))->where('id_product', '=', $id_product)->where('status', '=', Model_Order::STATUS_PAID);
     $query = $query->execute();
     $result = $query->as_array();
     return isset($result[0]['total']) ? $result[0]['total'] : 0;
 }
Esempio n. 22
0
        echo Route::url('default', array('action' => 'to_top', 'controller' => 'ad', 'id' => $widget->ad->id_ad));
        ?>
">
                    <?php 
        echo _e('Go Top!');
        ?>
 <?php 
        echo i18n::money_format(core::config('payment.pay_to_go_on_top'), core::config('payment.paypal_currency'));
        ?>
                </a>
            <?php 
    }
    ?>
        
            <?php 
    if (core::config('payment.to_featured') != FALSE and $widget->ad->featured < Date::unix2mysql()) {
        ?>
                <a class="btn btn-danger center-block" type="button" href="<?php 
        echo Route::url('default', array('action' => 'to_featured', 'controller' => 'ad', 'id' => $widget->ad->id_ad));
        ?>
">
                    <?php 
        echo _e('Go Featured!');
        ?>
 <?php 
        echo i18n::money_format(Model_Order::get_featured_price(), core::config('payment.paypal_currency'));
        ?>
                </a>
            <?php 
    }
    ?>
Esempio n. 23
0
 /**
  * creates the licenses for the purchase
  * @param  Model_User    $user    
  * @param  Model_Order   $order   
  * @param  Model_Product $product 
  * @return string                 
  */
 public static function generate($user, Model_Order $order, Model_Product $product)
 {
     $license = date('Ymd') . '-' . $order->id_order . '-';
     //until when the license is valid/expires
     if ($product->license_days > 0) {
         $license_valid = Date::unix2mysql(strtotime('+' . $product->license_days . ' day'));
     } else {
         $license_valid = NULL;
     }
     //never expires
     //we create a license for amount specified on product
     for ($i = 0; $i < $product->licenses; $i++) {
         $l = new self();
         $l->id_user = $user->id_user;
         $l->id_product = $product->id_product;
         $l->id_order = $order->id_order;
         $l->license = $license . strtoupper(Text::random('alnum', 40 - strlen($license)));
         $l->valid_date = $license_valid;
         $l->status = self::STATUS_ACTIVE;
         $l->save();
     }
     $licenses = new self();
     $licenses = $licenses->where('id_user', '=', $user->id_user)->where('id_order', '=', $order->id_order)->find_all();
     return $licenses;
 }
Esempio n. 24
0
 public function action_stats()
 {
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('My ads'))->set_url(Route::url('oc-panel', array('controller' => 'myads', 'action' => 'index'))));
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Stats')));
     Controller::$full_width = TRUE;
     $this->template->scripts['footer'] = array('js/chart.min.js', 'js/chart.js-php.js', 'js/oc-panel/stats/dashboard.js');
     $this->template->title = __('Stats');
     $this->template->bind('content', $content);
     $content = View::factory('oc-panel/profile/stats');
     $list_ad = array();
     $advert = new Model_Ad();
     //single stats for 1 ad
     if (is_numeric($id_ad = $this->request->param('id'))) {
         $advert = new Model_Ad($id_ad);
         if ($advert->loaded()) {
             //if admin or moderator user is the advert user ;) hack!!
             if ($this->user->id_role == Model_Role::ROLE_ADMIN or $this->user->id_role == Model_Role::ROLE_MODERATOR) {
                 $user = $advert->user;
             } else {
                 $user = $this->user;
             }
             if ($user->id_user !== $advert->id_user) {
                 Alert::set(Alert::ALERT, __("This is not your advertisement."));
                 HTTP::redirect(Route::url('oc-panel', array('controller' => 'myads', 'action' => 'index')));
             }
             Breadcrumbs::add(Breadcrumb::factory()->set_title($advert->title));
             // make a list of 1 ad (array), and than pass this array to query (IN).. To get correct visits
             $list_ad[] = $id_ad;
         }
     }
     //we didnt filter by ad, so lets get them all!
     if (empty($list_ad)) {
         $ads = new Model_Ad();
         $collection_of_user_ads = $ads->where('id_user', '=', $this->user->id_user)->find_all();
         $list_ad = array();
         foreach ($collection_of_user_ads as $key) {
             // make a list of ads (array), and than pass this array to query (IN).. To get correct visits
             $list_ad[] = $key->id_ad;
         }
     }
     // if user doesn't have any ads
     if (empty($list_ad)) {
         $list_ad = array(NULL);
     }
     $content->advert = $advert;
     //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);
     /////////////////////CONTACT STATS////////////////////////////////
     //visits created last XX days
     $query = DB::select(DB::expr('DATE(created) date'))->select(DB::expr('COUNT(contacted) count'))->from('visits')->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->where('created', 'between', array($my_from_date, $my_to_date))->group_by(DB::expr('DATE( created )'))->order_by('date', 'asc')->execute();
     $contacts_dates = $query->as_array('date');
     //Today
     $query = DB::select(DB::expr('COUNT(contacted) count'))->from('visits')->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->where(DB::expr('DATE( created )'), '=', DB::expr('CURDATE()'))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute();
     $contacts = $query->as_array();
     $content->contacts_today = isset($contacts[0]['count']) ? $contacts[0]['count'] : 0;
     //Yesterday
     $query = DB::select(DB::expr('COUNT(contacted) count'))->from('visits')->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->where(DB::expr('DATE( created )'), '=', date('Y-m-d', strtotime('-1 day')))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute();
     $contacts = $query->as_array();
     $content->contacts_yesterday = isset($contacts[0]['count']) ? $contacts[0]['count'] : 0;
     //
     //Last 30 days contacts
     $query = DB::select(DB::expr('COUNT(contacted) count'))->from('visits')->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->where('created', 'between', array(date('Y-m-d', strtotime('-30 day')), date::unix2mysql()))->execute();
     $contacts = $query->as_array();
     $content->contacts_month = isset($contacts[0]['count']) ? $contacts[0]['count'] : 0;
     //total contacts
     $query = DB::select(DB::expr('COUNT(contacted) count'))->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->from('visits')->execute();
     $contacts = $query->as_array();
     $content->contacts_total = isset($contacts[0]['count']) ? $contacts[0]['count'] : 0;
     /////////////////////VISITS STATS////////////////////////////////
     //visits created last XX days
     $query = DB::select(DB::expr('DATE(created) date'))->select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('id_ad', 'in', $list_ad)->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');
     $stats_daily = array();
     foreach ($dates as $date) {
         $count_contants = isset($contacts_dates[$date['date']]['count']) ? $contacts_dates[$date['date']]['count'] : 0;
         $count_visits = isset($visits[$date['date']]['count']) ? $visits[$date['date']]['count'] : 0;
         $stats_daily[] = array('date' => $date['date'], 'views' => $count_visits, 'contacts' => $count_contants);
     }
     $content->stats_daily = $stats_daily;
     //Today
     $query = DB::select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('id_ad', 'in', $list_ad)->where(DB::expr('DATE( created )'), '=', DB::expr('CURDATE()'))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute();
     $visits = $query->as_array();
     $content->visits_today = isset($visits[0]['count']) ? $visits[0]['count'] : 0;
     //Yesterday
     $query = DB::select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('id_ad', 'in', $list_ad)->where(DB::expr('DATE( created )'), '=', date('Y-m-d', strtotime('-1 day')))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute();
     $visits = $query->as_array();
     $content->visits_yesterday = isset($visits[0]['count']) ? $visits[0]['count'] : 0;
     //Last 30 days visits
     $query = DB::select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('id_ad', 'in', $list_ad)->where('created', 'between', array(date('Y-m-d', strtotime('-30 day')), date::unix2mysql()))->execute();
     $visits = $query->as_array();
     $content->visits_month = isset($visits[0]['count']) ? $visits[0]['count'] : 0;
     //total visits
     $query = DB::select(DB::expr('COUNT(id_visit) count'))->where('id_ad', 'in', $list_ad)->from('visits')->execute();
     $visits = $query->as_array();
     $content->visits_total = isset($visits[0]['count']) ? $visits[0]['count'] : 0;
 }
Esempio n. 25
0
 public function action_notifications()
 {
     $this->auto_render = FALSE;
     $this->template = View::factory('js');
     $user = Auth::instance()->get_user();
     $user->notification_date = Date::unix2mysql();
     $user->save();
     $this->template->content = __('Saved');
 }
Esempio n. 26
0
    /**
     * saves a translation
     * @param  string $language          
     * @param  array $translation_array 
     * @param  array $data_translated   
     * @return bool                    
     */
    public function save_translation($language, $translation_array, $data_translated)
    {
        //.po to .mo script
        require_once Kohana::find_file('vendor', 'php-mo/php-mo', 'php');
        //we save always in the custom file
        $mo_translation = i18n::get_language_custom_path($language);
        //changing the translation_array with the posted values
        foreach ($data_translated as $key => $value) {
            if (isset($translation_array[$key]['translated'])) {
                $translation_array[$key]['translated'] = $value;
            }
        }
        //let's generate a proper .po file for the mo converter
        $out = '';
        foreach ($translation_array as $key => $values) {
            list($id, $original, $translated) = array_values($values);
            if ($translated != '') {
                //only adding translated items
                $out .= '#: String ' . $key . PHP_EOL;
                $out .= 'msgid "' . $original . '"' . PHP_EOL;
                $out .= 'msgstr "' . $translated . '"' . PHP_EOL;
                $out .= PHP_EOL;
            }
        }
        //write the generated .po to file
        if (File::write($mo_translation, $out) === FALSE) {
            return FALSE;
        }
        //generate the .mo from the .po file
        phpmo_convert($mo_translation);
        //we regenerate the file again to be poedit friendly
        $out = 'msgid ""
msgstr ""
"Project-Id-Version: ' . Core::VERSION . '\\n"
"POT-Creation-Date: ' . Date::unix2mysql() . '\\n"
"PO-Revision-Date: ' . Date::unix2mysql() . '\\n"
"Last-Translator: ' . $this->user->name . ' <' . $this->user->email . '>\\n"
"Language-Team: en\\n"
"Language: ' . strtolower(substr($language, 0, 2)) . '\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=' . i18n::$charset . '\\n"
"Content-Transfer-Encoding: 8bit\\n"
"X-Generator: Open Classifieds ' . Core::VERSION . '\\n"' . PHP_EOL . PHP_EOL;
        foreach ($translation_array as $key => $values) {
            list($id, $original, $translated) = array_values($values);
            //only adding translated items
            $out .= '#: String ' . $key . PHP_EOL;
            $out .= 'msgid "' . $original . '"' . PHP_EOL;
            $out .= 'msgstr "' . $translated . '"' . PHP_EOL;
            $out .= PHP_EOL;
        }
        //write the generated .po to file
        file_put_contents($mo_translation, $out, LOCK_EX);
        return TRUE;
    }
Esempio n. 27
0
 /**
  * Handle GET requests.
  */
 public function action_index()
 {
     try {
         if (is_numeric($this->request->param('id'))) {
             $this->action_get();
         } else {
             $output = array();
             $ads = new Model_Ad();
             $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED);
             //search with lat and long!! nice!
             if (isset($this->_params['latitude']) and isset($this->_params['longitude'])) {
                 $ads->select(array(DB::expr('degrees(acos(sin(radians(' . $this->_params['latitude'] . ')) * sin(radians(`latitude`)) + cos(radians(' . $this->_params['latitude'] . ')) * cos(radians(`latitude`)) * cos(radians(abs(' . $this->_params['longitude'] . ' - `longitude`))))) * 69.172'), 'distance'))->where('latitude', 'IS NOT', NULL)->where('longitude', 'IS NOT', NULL);
                 //we unset the search by lat and long if not will be duplicated
                 unset($this->_filter_params['latitude']);
                 unset($this->_filter_params['longitude']);
             }
             //only published ads
             $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED);
             //if ad have passed expiration time dont show
             if (core::config('advertisement.expire_date') > 0) {
                 $ads->where(DB::expr('DATE_ADD( published, INTERVAL ' . core::config('advertisement.expire_date') . ' DAY)'), '>', Date::unix2mysql());
             }
             //make a search with q? param
             if (isset($this->_params['q']) and strlen($this->_params['q'])) {
                 if (core::config('general.search_by_description') == TRUE) {
                     $ads->where_open()->where('title', 'like', '%' . $this->_params['q'] . '%')->or_where('description', 'like', '%' . $this->_params['q'] . '%')->where_close();
                 } else {
                     $ads->where('title', 'like', '%' . $this->_params['q'] . '%');
                 }
             }
             //getting all the ads of a category.
             if (isset($this->_filter_params['id_category']) and is_numeric($this->_filter_params['id_category']['value'])) {
                 $category = new Model_Category($this->_filter_params['id_category']['value']);
                 if ($category->loaded()) {
                     $ads->where('id_category', 'in', $category->get_siblings_ids());
                     unset($this->_filter_params['id_category']);
                 }
             }
             //getting all the ads of a location.
             if (isset($this->_filter_params['id_location']) and is_numeric($this->_filter_params['id_location']['value'])) {
                 $location = new Model_Location($this->_filter_params['id_location']['value']);
                 if ($location->loaded()) {
                     $ads->where('id_location', 'in', $location->get_siblings_ids());
                     unset($this->_filter_params['id_location']);
                 }
             }
             //filter results by param, verify field exists and has a value
             $ads->api_filter($this->_filter_params);
             //how many? used in header X-Total-Count
             $count = $ads->count_all();
             //by default sort by published date
             if (empty($this->_sort)) {
                 $this->_sort['published'] = 'desc';
             }
             //after counting sort values
             $ads->api_sort($this->_sort);
             //we add the order by in case was specified, this is not a column so we need to do it manually
             if (isset($this->_sort['distance']) and isset($this->_params['latitude']) and isset($this->_params['longitude'])) {
                 $ads->order_by('distance', $this->_sort['distance']);
             }
             //pagination with headers
             $pagination = $ads->api_pagination($count, $this->_params['items_per_page']);
             $ads = $ads->cached()->find_all();
             //as array
             foreach ($ads as $ad) {
                 $a = $ad->as_array();
                 $a['price'] = i18n::money_format($ad->price);
                 $a['thumb'] = $ad->get_first_image();
                 $a['customfields'] = Model_Field::get_by_category($ad->id_category);
                 //sorting by distance, lets add it!
                 if (isset($ad->distance)) {
                     $a['distance'] = i18n::format_measurement($ad->distance);
                 }
                 $a['url'] = Route::url('ad', array('category' => $ad->category->seoname, 'seotitle' => $ad->seotitle));
                 $output[] = $a;
             }
             $this->rest_output(array('ads' => $output), 200, $count, $pagination !== FALSE ? $pagination : NULL);
         }
     } catch (Kohana_HTTP_Exception $khe) {
         $this->_error($khe);
     }
 }
Esempio n. 28
0
            <li><a href="?<?php 
    echo http_build_query(['sort' => 'published-asc'] + Request::current()->query());
    ?>
"><?php 
    echo __('Oldest');
    ?>
</a></li>
        </ul>
    </div>
    <div class="clearfix"></div>
    
  <?php 
    foreach ($ads as $ad) {
        ?>
      <?php 
        if ($ad->featured >= Date::unix2mysql(time())) {
            ?>
          <article class="list well clearfix featured ">
              <span class="label label-danger pull-right"><?php 
            echo __('Featured');
            ?>
</span>
      <?php 
        } else {
            ?>
          <article class="list well clearfix">
      <?php 
        }
        ?>
          <div class="pull-right favorite" id="fav-<?php 
        echo $ad->id_ad;
Esempio n. 29
0
 /**
  * confirm payment for order
  *
  * @param string    $id_order [unique indentifier of order]
  * @param string    $txn_id id of the transaction depending on provider
  */
 public function confirm_payment($paymethod = 'paypal', $txn_id = NULL)
 {
     // update orders
     if ($this->loaded()) {
         $ad = $this->ad;
         $this->status = self::STATUS_PAID;
         $this->pay_date = Date::unix2mysql();
         $this->paymethod = $paymethod;
         $this->txn_id = $txn_id;
         try {
             $this->save();
         } catch (Exception $e) {
             throw HTTP_Exception::factory(500, $e->getMessage());
         }
         //if saved delete coupon from session and -- number of coupons.
         Model_Coupon::sale($this->coupon);
         //send email to site owner! new sale!!
         if (core::config('email.new_ad_notify') == TRUE) {
             $url_ad = Route::url('ad', array('category' => $ad->category->seoname, 'seotitle' => $ad->seotitle));
             $replace = array('[AD.TITLE]' => $ad->title, '[URL.AD]' => $url_ad, '[ORDER.ID]' => $this->id_order, '[PRODUCT.ID]' => $this->id_product);
             Email::content(core::config('email.notify_email'), core::config('general.site_name'), core::config('email.notify_email'), core::config('general.site_name'), 'ads-sold', $replace);
         }
         //depending on the product different actions
         switch ($this->id_product) {
             case Model_Order::PRODUCT_AD_SELL:
                 $ad->sale($this);
                 break;
             case Model_Order::PRODUCT_TO_TOP:
                 $ad->to_top();
                 break;
             case Model_Order::PRODUCT_TO_FEATURED:
                 $ad->to_feature($this->featured_days);
                 break;
             case Model_Order::PRODUCT_CATEGORY:
                 $ad->paid_category();
                 break;
         }
     }
 }
Esempio n. 30
0
 public function action_index()
 {
     //if not god redirect him to the normal profile page
     if (Auth::instance()->get_user()->id_role != Model_Role::ROLE_ADMIN) {
         HTTP::redirect(Route::url('oc-panel', array('controller' => 'myads', 'action' => 'index')));
     }
     Core::ocacu();
     $this->template->title = __('Welcome');
     Breadcrumbs::add(Breadcrumb::factory()->set_title($this->template->title));
     $this->template->bind('content', $content);
     $content = View::factory('oc-panel/home');
     /////////////////////RSS////////////////////////////////
     //try to get the RSS from the cache
     $rss_url = 'http://feeds.feedburner.com/OpenClassifieds';
     $content->rss = Feed::parse($rss_url, 10);
     /////////////////////ADS////////////////////////////////
     $content->res = new Model_Ad();
     //filter ads by status
     $content->res = $content->res->where('status', '=', Core::get('status', Model_Ad::STATUS_PUBLISHED));
     $content->res = $content->res->order_by('created', 'desc')->limit(10)->find_all();
     /////////////////////STATS////////////////////////////////
     //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);
     //ads published last XX days
     $query = DB::select(DB::expr('DATE(published) date'))->select(DB::expr('COUNT(id_ad) count'))->from('ads')->where('status', '=', Model_Ad::STATUS_PUBLISHED)->where('published', 'between', array($my_from_date, $my_to_date))->group_by(DB::expr('DATE( published )'))->order_by('date', 'asc')->execute();
     $ads_dates = $query->as_array('date');
     //Today
     $query = DB::select(DB::expr('COUNT(id_ad) count'))->from('ads')->where('status', '=', Model_Ad::STATUS_PUBLISHED)->where(DB::expr('DATE( created )'), '=', DB::expr('CURDATE()'))->group_by(DB::expr('DATE( published )'))->order_by('published', 'asc')->execute();
     $ads = $query->as_array();
     $content->ads_today = isset($ads[0]['count']) ? $ads[0]['count'] : 0;
     //Yesterday
     $query = DB::select(DB::expr('COUNT(id_ad) count'))->from('ads')->where('status', '=', Model_Ad::STATUS_PUBLISHED)->where(DB::expr('DATE( created )'), '=', date('Y-m-d', strtotime('-1 day')))->group_by(DB::expr('DATE( published )'))->order_by('published', 'asc')->execute();
     $ads = $query->as_array();
     $content->ads_yesterday = isset($ads[0]['count']) ? $ads[0]['count'] : 0;
     //Last 30 days ads
     $query = DB::select(DB::expr('COUNT(id_ad) count'))->from('ads')->where('status', '=', Model_Ad::STATUS_PUBLISHED)->where('published', 'between', array(date('Y-m-d', strtotime('-30 day')), date::unix2mysql()))->execute();
     $ads = $query->as_array();
     $content->ads_month = isset($ads[0]['count']) ? $ads[0]['count'] : 0;
     //total ads
     $query = DB::select(DB::expr('COUNT(id_ad) count'))->from('ads')->where('status', '=', Model_Ad::STATUS_PUBLISHED)->execute();
     $ads = $query->as_array();
     $content->ads_total = isset($ads[0]['count']) ? $ads[0]['count'] : 0;
     /////////////////////VISITS STATS////////////////////////////////
     //visits created last XX days
     $query = DB::select(DB::expr('DATE(created) date'))->select(DB::expr('COUNT(id_visit) count'))->from('visits')->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');
     $stats_daily = array();
     foreach ($dates as $date) {
         $count_views = isset($visits[$date['date']]['count']) ? $visits[$date['date']]['count'] : 0;
         $count_ads = isset($ads_dates[$date['date']]['count']) ? $ads_dates[$date['date']]['count'] : 0;
         $stats_daily[] = array('date' => $date['date'], 'views' => $count_views, 'ads' => $count_ads);
     }
     $content->stats_daily = $stats_daily;
     //Today
     $query = DB::select(DB::expr('COUNT(id_visit) count'))->from('visits')->where(DB::expr('DATE( created )'), '=', DB::expr('CURDATE()'))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute();
     $ads = $query->as_array();
     $content->visits_today = isset($ads[0]['count']) ? $ads[0]['count'] : 0;
     //Yesterday
     $query = DB::select(DB::expr('COUNT(id_visit) count'))->from('visits')->where(DB::expr('DATE( created )'), '=', date('Y-m-d', strtotime('-1 day')))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute();
     $ads = $query->as_array();
     $content->visits_yesterday = isset($ads[0]['count']) ? $ads[0]['count'] : 0;
     //Last 30 days visits
     $query = DB::select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('created', 'between', array(date('Y-m-d', strtotime('-30 day')), date::unix2mysql()))->execute();
     $visits = $query->as_array();
     $content->visits_month = isset($visits[0]['count']) ? $visits[0]['count'] : 0;
     //total visits
     $query = DB::select(DB::expr('COUNT(id_visit) count'))->from('visits')->execute();
     $visits = $query->as_array();
     $content->visits_total = isset($visits[0]['count']) ? $visits[0]['count'] : 0;
     /////////////////////ORDERS STATS////////////////////////////////
     //orders created last XX days
     $query = DB::select(DB::expr('DATE(created) date'))->select(DB::expr('COUNT(id_order) count'))->select(DB::expr('SUM(amount) total'))->from('orders')->where('created', 'between', array($my_from_date, $my_to_date))->where('status', '=', Model_Order::STATUS_PAID)->group_by(DB::expr('DATE( created )'))->order_by('date', 'asc')->execute();
     $orders = $query->as_array('date');
     $stats_orders = array();
     foreach ($dates as $date) {
         $count_orders = isset($orders[$date['date']]['count']) ? $orders[$date['date']]['count'] : 0;
         $count_sum = isset($orders[$date['date']]['total']) ? $orders[$date['date']]['total'] : 0;
         $stats_orders[] = array('date' => $date['date'], '#orders' => $count_orders, '$' => $count_sum);
     }
     $content->stats_orders = $stats_orders;
     //Today
     $query = DB::select(DB::expr('COUNT(id_order) count'))->from('orders')->where(DB::expr('DATE( created )'), '=', DB::expr('CURDATE()'))->where('status', '=', Model_Order::STATUS_PAID)->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute();
     $ads = $query->as_array();
     $content->orders_yesterday = isset($ads[0]['count']) ? $ads[0]['count'] : 0;
     //Yesterday
     $query = DB::select(DB::expr('COUNT(id_order) count'))->from('orders')->where(DB::expr('DATE( created )'), '=', date('Y-m-d', strtotime('-1 day')))->where('status', '=', Model_Order::STATUS_PAID)->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute();
     $ads = $query->as_array();
     $content->orders_today = isset($ads[0]['count']) ? $ads[0]['count'] : 0;
     //Last 30 days orders
     $query = DB::select(DB::expr('COUNT(id_order) count'))->from('orders')->where('created', 'between', array(date('Y-m-d', strtotime('-30 day')), date::unix2mysql()))->where('status', '=', Model_Order::STATUS_PAID)->execute();
     $orders = $query->as_array();
     $content->orders_month = isset($orders[0]['count']) ? $orders[0]['count'] : 0;
     //total orders
     $query = DB::select(DB::expr('COUNT(id_order) count'))->from('orders')->where('status', '=', Model_Order::STATUS_PAID)->execute();
     $orders = $query->as_array();
     $content->orders_total = isset($orders[0]['count']) ? $orders[0]['count'] : 0;
 }