search category dropdown adde to the interface
[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, $ordering = 'hottest', $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'), $ordering);
102                 
103                 $vs_data['ordering'] = $ordering;
104                 
105                 // Pagination
106                 $this->load->library('pagination');
107                 $pg_config['base_url'] = site_url("catalog/category/$category_name/$ordering/");
108                 $pg_config['uri_segment'] = 5;
109                 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
110                         $vs_data['category_id']);
111                 $pg_config['per_page'] = $this->config->item('videos_per_page');
112                 $this->pagination->initialize($pg_config);
113                 $vs_data['pagination'] = $this->pagination->create_links();
114                 
115                 // Video Summary
116 //              $data['video_summary'] = $this->load->view('catalog/videos_summary_view',
117 //                      $vs_data, TRUE);
118                 
119                 $params = array(        'title' => $this->config->item('site_name'),
120                                                         'css' => array(
121                                                                 'catalog.css'
122                                                         ),
123                                                         //'js' => array(),
124                                                         //'metas' => array('description'=>'','keywords'=>'')
125                                                         );
126                 $this->load->library('html_head_params', $params);
127                 
128                 // **
129                 // ** LOADING VIEWS
130                 // **
131                 $this->load->view('html_begin', $this->html_head_params);
132                 $this->load->view('header', array(
133                         'search_category_name'=>$vs_data['category_name'],
134                 ));
135                 
136 //              $main_params['content'] = $this->load->view('catalog/category_view', $data, TRUE);
137                 $main_params['content'] = 
138                         $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
139                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
140                 $this->load->view('main', $main_params);
141                 
142                 $this->load->view('footer');
143                 $this->load->view('html_end');
144         }
145         
146         public function search($search_query = "", $offset = 0, $category_name = NULL)
147         {
148                 $this->load->model('videos_model');
149                 $this->load->library('security');
150                 
151                 // Redirect to an URL which contains search string if data was passed
152                 // via POST method and not via URL segments.
153                 $str_post_search = $this->input->post('search');
154                 $str_post_category = $this->input->post('search-category');
155                 if ($search_query === "" && $str_post_search !== FALSE) 
156                 {
157                         redirect('catalog/search/'
158                                 . $this->videos_model->encode_search_query($str_post_search)
159                                 . '/0'
160                                 . ($str_post_category === FALSE ? '' : "/$str_post_category"));
161                         return;
162                 }
163
164                 // **
165                 // ** LOADING MODEL
166                 // **
167                 // Search query is encoded for URL and must be decoded.
168                 $enc_search_query = $search_query;
169                 $search_query = $this->videos_model->decode_search_query($search_query);
170                 
171                 // Security filtering
172                 $search_query = $this->security->xss_clean($search_query);
173                 $results_data['search_query'] = $search_query;
174
175                 // Category
176                 $results_data = $this->_get_category_data($category_name);
177                 if ($results_data === NULL)
178                         $results_data = array('category_id'=>NULL);
179                 
180                 // Page header data
181                 $header_data['search_query'] = $search_query;
182                 if ($category_name !== NULL)
183                 {
184                         $header_data['search_category_name'] = $results_data['category_name'];
185                 }
186                 
187                 // Check if search string is valid.
188                 if (strlen($search_query) === 0)
189                 {
190                         //$results_data['videos'] = NULL;
191                         $this->error($this->lang->line('error_search_query_empty'),
192                                 $header_data);
193                         return;
194                 }
195                 else
196                 {
197                         // Retrieve search results.
198                         $results_data['count'] = $this->videos_model->search_videos(
199                                 $search_query, 0, 0, $results_data['category_id']);
200                         $results_data['videos'] = $this->videos_model->search_videos(
201                                 $search_query, intval($offset),
202                                 $this->config->item('search_results_per_page'),
203                                 $results_data['category_id']);
204                         if ($results_data['videos'] === NULL)
205                                 $results_data['videos'] = array();
206         
207                         // Pagination
208                         $this->load->library('pagination');
209                         $pg_config['base_url'] = site_url("catalog/search/$enc_search_query/");
210                         $pg_config['uri_segment'] = 4;
211                         $pg_config['total_rows'] = $results_data['count'];
212                         $pg_config['per_page'] =
213                                 $this->config->item('search_results_per_page');
214                         $this->pagination->initialize($pg_config);
215                         $results_data['pagination'] = $this->pagination->create_links();
216                 }
217                 
218                 // HTML head parameters
219                 $params = array(        'title' => $this->config->item('site_name'),
220                                                         'css' => array(
221                                                                 'catalog.css'
222                                                         ),
223                                                         //'js' => array(),
224                                                         //'metas' => array('description'=>'','keywords'=>'')
225                                                         );
226                 $this->load->library('html_head_params', $params);
227                 
228                 // **
229                 // ** LOADING VIEWS
230                 // **
231                 $this->load->view('html_begin', $this->html_head_params);
232                 $this->load->view('header', $header_data);
233                 
234                 // Search Results
235                 $main_params['content'] = 
236                         $this->load->view('catalog/search_results_view',
237                                 $results_data, TRUE);
238                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
239                 $this->load->view('main', $main_params);
240                 
241                 $this->load->view('footer');
242                 $this->load->view('html_end');
243         }
244         
245         public function error($msg, $header_data)
246         {
247                 $params = array(        'title' => 'Error - '. $this->config->item('site_name'),
248                         //'css' => array(),
249                         //'js' => array(),
250                         //'metas' => array('description'=>'','keywords'=>'')
251                 );
252                 $this->load->library('html_head_params', $params);
253                 
254                 // **
255                 // ** LOADING VIEWS
256                 // **
257                 $this->load->view('html_begin', $this->html_head_params);
258                 $this->load->view('header', $header_data);
259                 
260                 $main_params['content'] = $this->load->view('error_view', array('msg'=>$msg), TRUE);
261                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
262                 $this->load->view('main', $main_params);
263                 
264                 $this->load->view('footer');
265                 $this->load->view('html_end');
266         }
267         
268         public function _get_category_data($category_name)
269         {
270                 if ($category_name === NULL)
271                         return NULL;
272                 
273                 $categories = $this->config->item('categories');
274                 $category_id = array_search($category_name, $categories);
275                 $results_data['category_name'] = $category_name;
276                 $results_data['category_id'] = $category_id;
277                 $results_data['category_title'] = $category_name ?
278                         $this->lang->line("ui_categ_$category_name") : $category_name;
279                 
280                 return $results_data;
281         } 
282 }
283
284 /* End of file catalog.php */
285 /* Location: ./application/controllers/catalog.php */