31bdc51c1a1d63747fccc7d00d719462a70603f0
[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, NULL, 0, $this->config->item('videos_per_row'));
30
31                         // Category
32                         $vs_data['category_name'] = $name;
33                         $vs_data['category_id'] = $id;
34                         $videos_summary['category_name'] = $name;
35                         $videos_summary['category_title'] = $name ?
36                                         $this->lang->line("ui_categ_$name") : $name;
37
38                         // Pagination (not required)
39                         $vs_data['pagination'] = '';
40
41                         $videos_summary['content'] = $this->load->view(
42                                         'catalog/videos_summary_view', $vs_data, TRUE);
43                         $data['videos_summaries'][] = $videos_summary;
44                 }
45
46                 $params = array('title' => $this->config->item('site_name'),
47                         'css' => array(
48                                 'catalog.css'
49                         ),
50                                 //'js' => array(),
51                                 //'metas' => array('description'=>'','keywords'=>'')
52                 );
53                 $this->load->library('html_head_params', $params);
54
55                 // **
56                 // ** LOADING VIEWS
57                 // **
58                 $this->load->view('html_begin', $this->html_head_params);
59                 $this->load->view('header', array(
60                         'selected_menu' => 'home'
61                 ));
62
63                 $main_params['content'] = $this->load->view('catalog/index_view', $data,
64                                 TRUE);
65                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
66                 $this->load->view('main', $main_params);
67
68                 $this->load->view('footer');
69                 $this->load->view('html_end');
70         }
71
72         public function test()
73         {
74                 $this->load->helper('av_info');
75                 
76                 var_dump(get_video_dar('./data/media/test.ogv'));
77         }
78
79         public function category($category_name, $ordering = 'hottest', $offset = 0)
80         {
81                 // **
82                 // ** LOADING MODEL
83                 // **
84                 // Video Category
85                 $category_data = Catalog::_get_category_data($category_name);
86
87                 // Retrieve videos summary.
88                 $this->load->model('videos_model');
89                 $vs_data['videos'] = $this->videos_model->get_videos_summary(
90                                 $category_data['category_id'], NULL, intval($offset), 
91                                 $this->config->item('videos_per_page'), $ordering);
92
93                 $vs_data['ordering'] = $ordering;
94
95                 // Pagination
96                 $this->load->library('pagination');
97                 $pg_config['base_url'] = site_url(
98                                 "catalog/category/$category_name/$ordering/");
99                 $pg_config['uri_segment'] = 5;
100                 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
101                                 $category_data['category_id']);
102                 $pg_config['per_page'] = $this->config->item('videos_per_page');
103                 $this->pagination->initialize($pg_config);
104                 $vs_data['pagination'] = $this->pagination->create_links();
105                 $vs_data['category_name'] = $category_data['category_name'];
106                 $vs_data['title'] = $category_data['category_title'];
107
108                 $params = array('title' =>
109                         $category_data['category_title'] . ' &ndash; '
110                         . $this->config->item('site_name'),
111                         'css' => array(
112                                 'catalog.css'
113                         )
114                                 //'metas' => array('description'=>'','keywords'=>'')
115                 );
116                 $this->load->library('html_head_params', $params);
117
118                 // **
119                 // ** LOADING VIEWS
120                 // **
121                 $this->load->view('html_begin', $this->html_head_params);
122                 $this->load->view('header', array(
123                         'search_category_name' => $vs_data['category_name']
124                 ));
125
126                 $main_params['content'] =
127                                 $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
128                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
129                 $this->load->view('main', $main_params);
130
131                 $this->load->view('footer');
132                 $this->load->view('html_end');
133         }
134
135         public function search($search_query = "", $offset = 0, $category_name = NULL)
136         {
137                 $this->load->model('videos_model');
138                 $this->load->library('security');
139
140                 // Redirect to an URL which contains search string if data was passed
141                 // via POST method and not via URL segments.
142                 $str_post_search = $this->input->post('search');
143                 $str_post_category = $this->input->post('search-category');
144                 if ($search_query === "" && $str_post_search !== FALSE)
145                 {
146                         redirect('catalog/search/'
147                                         . $this->videos_model->encode_search_query($str_post_search)
148                                         . '/0'
149                                         . ($str_post_category === FALSE ? '' : "/$str_post_category"));
150                         return;
151                 }
152
153                 // **
154                 // ** LOADING MODEL
155                 // **
156                 // Search query is encoded for URL and must be decoded.
157                 $enc_search_query = $search_query;
158                 $search_query = $this->videos_model->decode_search_query($search_query);
159
160                 // Security filtering
161                 $search_query = $this->security->xss_clean($search_query);
162                 $results_data['search_query'] = $search_query;
163
164                 // Category
165                 $results_data = Catalog::_get_category_data($category_name);
166                 if ($results_data === NULL)
167                         $results_data = array('category_id' => NULL);
168
169                 // Page header data
170                 $header_data['search_query'] = $search_query;
171                 if ($category_name !== NULL)
172                 {
173                         $header_data['search_category_name'] = $results_data['category_name'];
174                 }
175
176                 // Check if search string is valid.
177                 if (strlen($search_query) === 0)
178                 {
179                         //$results_data['videos'] = NULL;
180                         $this->error($this->lang->line('error_search_query_empty'), $header_data);
181                         return;
182                 }
183                 else
184                 {
185                         // Retrieve search results.
186                         $results_data['count'] = $this->videos_model->search_videos(
187                                         $search_query, 0, 0, $results_data['category_id']);
188                         $results_data['videos'] = $this->videos_model->search_videos(
189                                         $search_query, intval($offset), $this->config->item(
190                                                         'search_results_per_page'), 
191                                         $results_data['category_id']);
192                         if ($results_data['videos'] === NULL)
193                                 $results_data['videos'] = array();
194
195                         // Pagination
196                         $this->load->library('pagination');
197                         $pg_config['base_url'] = site_url("catalog/search/$enc_search_query/");
198                         $pg_config['uri_segment'] = 4;
199                         $pg_config['total_rows'] = $results_data['count'];
200                         $pg_config['per_page'] =
201                                         $this->config->item('search_results_per_page');
202                         $this->pagination->initialize($pg_config);
203                         $results_data['pagination'] = $this->pagination->create_links();
204                 }
205
206                 // HTML head parameters
207                 $params = array('title' => 'Search Results &ndash; '
208                         . $this->config->item('site_name'),
209                         'css' => array(
210                                 'catalog.css'
211                         ),
212                                 //'js' => array(),
213                                 //'metas' => array('description'=>'','keywords'=>'')
214                 );
215                 $this->load->library('html_head_params', $params);
216
217                 // **
218                 // ** LOADING VIEWS
219                 // **
220                 $this->load->view('html_begin', $this->html_head_params);
221                 $this->load->view('header', $header_data);
222
223                 // Search Results
224                 $main_params['content'] =
225                                 $this->load->view('catalog/search_results_view', $results_data,
226                                                 TRUE);
227                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
228                 $this->load->view('main', $main_params);
229
230                 $this->load->view('footer');
231                 $this->load->view('html_end');
232         }
233
234         public function error($msg, $header_data)
235         {
236                 $params = array('title' => 'Error &ndash; '
237                         . $this->config->item('site_name'),
238                                 //'css' => array(),
239                                 //'js' => array(),
240                                 //'metas' => array('description'=>'','keywords'=>'')
241                 );
242                 $this->load->library('html_head_params', $params);
243
244                 // **
245                 // ** LOADING VIEWS
246                 // **
247                 $this->load->view('html_begin', $this->html_head_params);
248                 $this->load->view('header', $header_data);
249
250                 $main_params['content'] = $this->load->view('error_view', array(
251                         'msg' => $msg), TRUE);
252                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
253                 $this->load->view('main', $main_params);
254
255                 $this->load->view('footer');
256                 $this->load->view('html_end');
257         }
258
259         public static function _get_category_data($category_name)
260         {
261                 $ci = & get_instance();
262
263                 if ($category_name === NULL)
264                         return NULL;
265
266                 $categories = $ci->config->item('categories');
267                 $category_id = array_search($category_name, $categories);
268                 $results_data['category_name'] = $category_name;
269                 $results_data['category_id'] = $category_id;
270                 $results_data['category_title'] = $category_name ?
271                                 $ci->lang->line("ui_categ_$category_name") : $category_name;
272
273                 return $results_data;
274         }
275
276 }
277
278 /* End of file catalog.php */
279 /* Location: ./application/controllers/catalog.php */