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