Example #1
0
 public static function _validation_valid_category($val)
 {
     if (self::_empty($val)) {
         return true;
     }
     return Model_Base_Category::valid_field('id', $val);
 }
Example #2
0
 public function action_home()
 {
     $this->data['highlight_product'] = Model_Base_Product::get_by(array('where' => array(array('highlight', '=', 1), array('status', '=', 1)), 'limit' => 5));
     $this->data['new_product'] = Model_Base_Product::get_by(array('where' => array(array('status', '=', 1)), 'limit' => 8));
     $this->data['product_category'] = Model_Base_Category::get_all(array('where' => array(array('parent_category_id', '=', 0), array('status', '=', 1))));
     foreach ($this->data['product_category'] as $key => $value) {
         $this->data['product_category'][$key]['product'] = Model_Base_Product::get_by_category($value['id'], 0, 4);
     }
     $this->template->title = 'Home Page';
     $this->template->content = View::forge($this->layout . '/home/home', $this->data);
 }
Example #3
0
 public function init()
 {
     View::set_global('base_url', Config::get('base_url'));
     View::set_global('controller', Request::active()->controller);
     View::set_global('action', Request::active()->action);
     View::set_global('category', Model_Base_Category::get_all(array('where' => array(array('status', '=', 1)))));
     View::set_global('head', View::forge($this->layout . '/global/head'));
     View::set_global('header', View::forge($this->layout . '/global/header'));
     View::set_global('footer', View::forge($this->layout . '/global/footer'));
     View::set_global('script', View::forge($this->layout . '/global/script'));
 }
Example #4
0
 public function post_list()
 {
     $page = (int) Input::post('page') !== 0 ? (int) Input::post('page') : 1;
     $code = Input::post('code');
     $category_id = Model_Base_Category::get_id_by_code($code);
     $total = Model_Base_Product::count_by_category($category_id);
     $limit = _DEFAULT_LIMIT_;
     $offset = $page * $limit - $limit < $total ? $page * $limit - $limit : _DEFAULT_OFFSET_;
     $this->data['products'] = Model_Base_Product::get_by_category($category_id, $offset, $limit);
     $this->data['success'] = true;
     return $this->response($this->data);
 }
Example #5
0
 public function post_list()
 {
     $page = (int) Input::post('page') !== 0 ? (int) Input::post('page') : 1;
     $category_id = Input::post('category_id');
     $type = !empty($category_id) && Model_Base_Category::valid_field('id', $category_id) ? true : false;
     $total = $type ? Model_Base_Product::admin_count_by_category($category_id) : Model_Base_Product::count_all();
     $limit = _DEFAULT_LIMIT_;
     $offset = $page * $limit - $limit < $total ? $page * $limit - $limit : _DEFAULT_OFFSET_;
     $this->data['product'] = $type ? Model_Base_Product::admin_get_by_category($category_id, $offset, $limit) : Model_Base_Product::get_all($offset, $limit);
     $this->data['success'] = true;
     return $this->response($this->data);
 }
Example #6
0
 public function post_status()
 {
     $val = Validation::forge();
     $val->add_callable('MyRules');
     $val->add_field('status', Lang::get('label.status'), 'required|valid_category_status');
     $val->add_field('category_id', Lang::get('label.category'), 'required|valid_category');
     if ($val->run()) {
         Model_Base_Category::update($val->validated('category_id'), array('status' => $val->validated('status')));
         $this->data['success'] = true;
     } else {
         $this->data['errors'] = $val->error_message();
     }
     return $this->response($this->data);
 }
Example #7
0
 public static function run($type = null, $options = array())
 {
     $data = array();
     $photo_name = array();
     try {
         Upload::process();
     } catch (Exception $e) {
         $data['error'] = Lang::get('notice.upload.no_file');
         return $data;
     }
     if (Upload::is_valid()) {
         Upload::save();
     }
     foreach (Upload::get_errors() as $file) {
         $data['error'] = $file['errors']['0']['message'];
         return $data;
     }
     foreach (Upload::get_files() as $file) {
         $resize = self::resize_photo($file['saved_to'], $file['saved_as'], $type);
         $photo_name[] = $file['saved_as'];
     }
     if ($resize) {
         switch ($type) {
             case 'icon':
                 if (Model_Base_User::update($options['user_id'], array('user_photo' => $photo_name[0]))) {
                     $old_photo = $file['saved_to'] . $type . '/' . $options['user_photo'];
                     if (File::exists($old_photo)) {
                         File::delete($old_photo);
                     }
                     $data['photo_name'] = _PATH_ICON_ . $photo_name[0];
                 } else {
                     $data['error'] = Lang::get('notice.upload.save_icon_error');
                 }
                 break;
             case 'category':
                 $old_photo = $options['type'] === 'new' ?: $file['saved_to'] . $type . '/' . Model_Category::find($options['category_id'])->category_photo;
                 if (Model_Base_Category::update($options['category_id'], array('category_photo' => $photo_name[0]))) {
                     if (File::exists($old_photo)) {
                         File::delete($old_photo);
                     }
                     $data['photo_name'] = _PATH_CATEGORY_ . $photo_name[0];
                 } else {
                     $data['error'] = Lang::get('notice.upload.save_category_error');
                 }
                 break;
             case 'product':
                 $old_photo = $file['saved_to'] . $type . '/' . Model_Product::find($options['product_id'])->product_photo;
                 if (Model_Base_Product::update($options['product_id'], array('product_photo' => $photo_name[0]))) {
                     if (File::exists($old_photo)) {
                         File::delete($old_photo);
                     }
                     $data['photo_name'] = _PATH_PRODUCT_ . $photo_name[0];
                 } else {
                     $data['error'] = Lang::get('notice.upload.save_product_error');
                 }
                 break;
             case 'photo':
                 if ($options['type'] === 'sub_product_photo') {
                     $photo_props = array('product_id' => $options['product_id'], 'photo_name' => $photo_name[0]);
                     if ($photo_id = Model_Base_Photo::insert($photo_props)) {
                         $data['photo_id'] = $photo_id;
                         $data['photo_name'] = _PATH_PHOTO_ . $photo_name[0];
                     } else {
                         $data['error'] = Lang::get('notice.upload.save_photo_error');
                     }
                 }
                 break;
             default:
                 $data['error'] = Lang::get('system_error');
                 break;
         }
     } else {
         $data['error'] = Lang::get('system_error');
     }
     return $data;
 }
Example #8
0
 public static function count_by_category($category_id)
 {
     $category_ids = Model_Base_Category::get_all_child_category_id($category_id);
     $ids = join(',', $category_ids);
     $sql = "\n                SELECT `p`.`id`\n                FROM `product_categories` `pc`\n                LEFT JOIN `products` `p` ON `pc`.`product_id` = `p`.`id`\n                WHERE `pc`.`category_id` IN ({$ids})\n                AND `p`.`status` = 1\n                GROUP BY `p`.`id`\n            ";
     try {
         $query = DB::query($sql)->execute()->as_array();
         return count($query);
     } catch (Exception $e) {
         Log::write('ERROR', $e->getMessage());
     }
     return false;
 }