search MVC reorganized
[living-lab-site.git] / application / controllers / catalog.php
1 <?php
2
3 /**
4  * Class Catalog controls video hierarchy and searching
5  *
6  * @category    Controller
7  * @author              Călin-Andrei Burloiu
8  */
9 class Catalog extends CI_Controller {
10         
11         public function __construct()
12         {
13                 parent::__construct();
14                 
15                 //$this->lang->load('catalog');
16         }       
17         
18         public function index()
19         {
20                 // **
21                 // ** LOADING MODEL
22                 // **
23                 // Retrieve videos summary.
24                 $this->load->model('videos_model');
25                 foreach ($this->config->item('categories') as $id => $name)
26                 {
27                         // Videos
28                         $vs_data['videos'] = $this->videos_model->get_videos_summary(
29                                 $id, 0, $this->config->item('videos_per_row'));
30                         
31                         // Category
32                         $vs_data['category_name'] = $name;
33                         $vs_data['category_title'] = $name ?
34                                 $this->lang->line("ui_categ_$name") : $name;
35                         $vs_data['category_id'] = $id;
36                         
37                         // Pagination (not required)
38                         $vs_data['pagination'] = '';
39                         
40                         $data['videos_summaries'][] = 
41                                 $this->load->view('catalog/videos_summary_view', 
42                                 $vs_data, TRUE);
43                 }
44                 
45                 $params = array(        'title' => $this->config->item('site_name'),
46                                                         'css' => array(
47                                                                 'catalog.css'
48                                                         ),
49                                                         //'js' => array(),
50                                                         //'metas' => array('description'=>'','keywords'=>'')
51                                                         );
52                 $this->load->library('html_head_params', $params);
53                 
54                 // **
55                 // ** LOADING VIEWS
56                 // **
57                 $this->load->view('html_begin', $this->html_head_params);
58                 $this->load->view('header', array('selected_menu' => 'home'));
59                 
60                 $main_params['content'] = $this->load->view('catalog/index_view', $data, TRUE);
61                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
62                 $this->load->view('main', $main_params);
63                 
64                 $this->load->view('footer');
65                 $this->load->view('html_end');
66         }
67         
68         public function test($page = 0)
69         {
70                 $params = array(        'title' => 'Test - '. $this->config->item('site_name'),
71                                                         //'css' => array(),
72                                                         //'js' => array(),
73                                                         //'metas' => array('description'=>'','keywords'=>'')
74                         );
75                 $this->load->library('html_head_params', $params);
76                 
77                 // **
78                 // ** LOADING VIEWS
79                 // **
80                 $this->load->view('html_begin', $this->html_head_params);
81                 $this->load->view('header', array('selected_menu' => 'home'));
82                 
83                 $this->load->view('echo', array('output'=>'Test Page', 'clear'=>TRUE));
84                 
85                 $this->load->view('footer');
86                 $this->load->view('html_end');
87         }
88         
89         public function category($category_name, $offset = 0)
90         {
91                 // **
92                 // ** LOADING MODEL
93                 // **
94                 // Video Category
95                 $vs_data = $this->_get_category_data($category_name);
96                 
97                 // Retrieve videos summary.
98                 $this->load->model('videos_model');
99                 $vs_data['videos'] = $this->videos_model->get_videos_summary(
100                         $vs_data['category_id'], intval($offset),
101                         $this->config->item('videos_per_page'));
102                 
103                 // Pagination
104                 $this->load->library('pagination');
105                 $pg_config['base_url'] = site_url("catalog/category/$category_name/");
106                 $pg_config['uri_segment'] = 4;
107                 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
108                         $vs_data['category_id']);
109                 $pg_config['per_page'] = $this->config->item('videos_per_page');
110                 $this->pagination->initialize($pg_config);
111                 $vs_data['pagination'] = $this->pagination->create_links();
112                 
113                 // Video Summary
114 //              $data['video_summary'] = $this->load->view('catalog/videos_summary_view',
115 //                      $vs_data, TRUE);
116                 
117                 $params = array(        'title' => $this->config->item('site_name'),
118                                                         'css' => array(
119                                                                 'catalog.css'
120                                                         ),
121                                                         //'js' => array(),
122                                                         //'metas' => array('description'=>'','keywords'=>'')
123                                                         );
124                 $this->load->library('html_head_params', $params);
125                 
126                 // **
127                 // ** LOADING VIEWS
128                 // **
129                 $this->load->view('html_begin', $this->html_head_params);
130                 $this->load->view('header');
131                 
132 //              $main_params['content'] = $this->load->view('catalog/category_view', $data, TRUE);
133                 $main_params['content'] = 
134                         $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
135                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
136                 $this->load->view('main', $main_params);
137                 
138                 $this->load->view('footer');
139                 $this->load->view('html_end');
140         }
141         
142         public function search($search_query = "", $offset = 0, $category_name = NULL)
143         {
144                 // Redirect to an URL which contains search string if data was passed
145                 // via POST method and not via URL segments.
146                 $str_post_search = $this->input->post('search', TRUE);
147                 if ($search_query === "" && $str_post_search !== FALSE) 
148                         redirect('catalog/search/'. $this->input->post('search', TRUE));
149                 
150                 // **
151                 // ** LOADING MODEL
152                 // **
153                 // Category
154                 $results_data = $this->_get_category_data($category_name);
155                 if ($results_data === NULL)
156                         $results_data = array('category_id'=>NULL);
157                 
158                 $results_data['search_query'] = $search_query;
159                 
160                 // Check if search string is valid.
161                 if (strlen($search_query) < 4)
162                 {
163                         $results_data['videos'] = NULL;
164                 }
165                 else
166                 {
167                         // Retrieve search results.
168                         $this->load->model('videos_model');
169                         $results_data['count'] = $this->videos_model->search_videos(
170                                 $search_query);
171                         $results_data['videos'] = $this->videos_model->search_videos(
172                                 $search_query, intval($offset),
173                                 $this->config->item('search_results_per_page'),
174                                 $results_data['category_id']);
175                         if ($results_data['videos'] === NULL)
176                                 $results_data['videos'] = array();
177         
178                         // Pagination
179                         $this->load->library('pagination');
180                         $pg_config['base_url'] = site_url("catalog/search/$search_query/");
181                         $pg_config['uri_segment'] = 4;
182                         $pg_config['total_rows'] = $results_data['count'];
183                         $pg_config['per_page'] =
184                                 $this->config->item('search_results_per_page');
185                         $this->pagination->initialize($pg_config);
186                         $results_data['pagination'] = $this->pagination->create_links();
187                 }
188                         
189                 $params = array(        'title' => $this->config->item('site_name'),
190                                                         'css' => array(
191                                                                 'catalog.css'
192                                                         ),
193                                                         //'js' => array(),
194                                                         //'metas' => array('description'=>'','keywords'=>'')
195                                                         );
196                 $this->load->library('html_head_params', $params);
197                 
198                 // **
199                 // ** LOADING VIEWS
200                 // **
201                 $this->load->view('html_begin', $this->html_head_params);
202                 $this->load->view('header');
203                 
204                 // Search Results
205                 $main_params['content'] = 
206                         $this->load->view('catalog/search_results_view',
207                                 $results_data, TRUE);
208                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
209                 $this->load->view('main', $main_params);
210                 
211                 $this->load->view('footer');
212                 $this->load->view('html_end');
213         }
214         
215         public function _get_category_data($category_name)
216         {
217                 if ($category_name === NULL)
218                         return NULL;
219                 
220                 $categories = $this->config->item('categories');
221                 $category_id = array_search($category_name, $categories);
222                 $results_data['category_name'] = $category_name;
223                 $results_data['category_id'] = $category_id;
224                 $results_data['category_title'] = $category_name ?
225                         $this->lang->line("ui_categ_$category_name") : $category_name;
226                 
227                 return $results_data;
228         } 
229 }
230
231 /* End of file catalog.php */
232 /* Location: ./application/controllers/catalog.php */