/
category.php
57 lines (49 loc) · 1.69 KB
/
category.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
use Fuel\Core\View;
use Fuel\Core\Response;
use Fuel\Core\Input;
/**
* The Home Controller
*
* @package app
* @extends Controller
*/
class Controller_Category extends Controller_Base_Core
{
public function before()
{
parent::before();
}
public function after($response)
{
$response = parent::after($response);
return $response;
}
public function action_index($code = null)
{
if (empty($code) || !Model_Base_Category::valid_by(array(
array('code' => $code),
array('status' => 1)
))) {
Response::redirect('/');
}
$category_id = Model_Base_Category::get_id_by_code($code);
$total_page = ceil(Model_Base_Product::count_by_category($category_id) / _DEFAULT_LIMIT_);
View::set_global('total_page', $total_page);
$this->data['products'] = Model_Base_Product::get_by_category($category_id);
$this->template->title = 'Category Page';
$this->template->content = View::forge($this->layout . '/category/list', $this->data);
}
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);
}
}