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