login works; working at register
[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(
59                         'selected_menu'=> 'home'
60                 ));
61                 
62                 $main_params['content'] = $this->load->view('catalog/index_view', $data, TRUE);
63                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
64                 $this->load->view('main', $main_params);
65                 
66                 $this->load->view('footer');
67                 $this->load->view('html_end');
68         }
69         
70         public function test($page = 0)
71         {
72                 echo $this->uri->segment(1);
73         }
74         
75         public function category($category_name, $ordering = 'hottest', $offset = 0)
76         {
77                 // **
78                 // ** LOADING MODEL
79                 // **
80                 // Video Category
81                 $vs_data = $this->_get_category_data($category_name);
82                 
83                 // Retrieve videos summary.
84                 $this->load->model('videos_model');
85                 $vs_data['videos'] = $this->videos_model->get_videos_summary(
86                         $vs_data['category_id'], intval($offset),
87                         $this->config->item('videos_per_page'), $ordering);
88                 
89                 $vs_data['ordering'] = $ordering;
90                 
91                 // Pagination
92                 $this->load->library('pagination');
93                 $pg_config['base_url'] = site_url("catalog/category/$category_name/$ordering/");
94                 $pg_config['uri_segment'] = 5;
95                 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
96                         $vs_data['category_id']);
97                 $pg_config['per_page'] = $this->config->item('videos_per_page');
98                 $this->pagination->initialize($pg_config);
99                 $vs_data['pagination'] = $this->pagination->create_links();
100                 
101                 // Video Summary
102 //              $data['video_summary'] = $this->load->view('catalog/videos_summary_view',
103 //                      $vs_data, TRUE);
104                 
105                 $params = array(        'title' =>
106                                                                 $vs_data['category_title'].' &ndash; '
107                                                                         . $this->config->item('site_name'),
108                                                         'css' => array(
109                                                                 'catalog.css'
110                                                         ),
111                                                         //'js' => array(),
112                                                         //'metas' => array('description'=>'','keywords'=>'')
113                                                         );
114                 $this->load->library('html_head_params', $params);
115                 
116                 // **
117                 // ** LOADING VIEWS
118                 // **
119                 $this->load->view('html_begin', $this->html_head_params);
120                 $this->load->view('header', array(
121                         'search_category_name'=> $vs_data['category_name']
122                 ));
123                 
124 //              $main_params['content'] = $this->load->view('catalog/category_view', $data, TRUE);
125                 $main_params['content'] = 
126                         $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
127                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
128                 $this->load->view('main', $main_params);
129                 
130                 $this->load->view('footer');
131                 $this->load->view('html_end');
132         }
133         
134         public function search($search_query = "", $offset = 0, $category_name = NULL)
135         {
136                 $this->load->model('videos_model');
137                 $this->load->library('security');
138                 
139                 // Redirect to an URL which contains search string if data was passed
140                 // via POST method and not via URL segments.
141                 $str_post_search = $this->input->post('search');
142                 $str_post_category = $this->input->post('search-category');
143                 if ($search_query === "" && $str_post_search !== FALSE) 
144                 {
145                         redirect('catalog/search/'
146                                 . $this->videos_model->encode_search_query($str_post_search)
147                                 . '/0'
148                                 . ($str_post_category === FALSE ? '' : "/$str_post_category"));
149                         return;
150                 }
151
152                 // **
153                 // ** LOADING MODEL
154                 // **
155                 // Search query is encoded for URL and must be decoded.
156                 $enc_search_query = $search_query;
157                 $search_query = $this->videos_model->decode_search_query($search_query);
158                 
159                 // Security filtering
160                 $search_query = $this->security->xss_clean($search_query);
161                 $results_data['search_query'] = $search_query;
162
163                 // Category
164                 $results_data = $this->_get_category_data($category_name);
165                 if ($results_data === NULL)
166                         $results_data = array('category_id'=>NULL);
167                 
168                 // Page header data
169                 $header_data['search_query'] = $search_query;
170                 if ($category_name !== NULL)
171                 {
172                         $header_data['search_category_name'] = $results_data['category_name'];
173                 }
174                 
175                 // Check if search string is valid.
176                 if (strlen($search_query) === 0)
177                 {
178                         //$results_data['videos'] = NULL;
179                         $this->error($this->lang->line('error_search_query_empty'),
180                                 $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),
190                                 $this->config->item('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',
226                                 $results_data, 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('msg'=>$msg), TRUE);
251                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
252                 $this->load->view('main', $main_params);
253                 
254                 $this->load->view('footer');
255                 $this->load->view('html_end');
256         }
257         
258         public function _get_category_data($category_name)
259         {
260                 if ($category_name === NULL)
261                         return NULL;
262                 
263                 $categories = $this->config->item('categories');
264                 $category_id = array_search($category_name, $categories);
265                 $results_data['category_name'] = $category_name;
266                 $results_data['category_id'] = $category_id;
267                 $results_data['category_title'] = $category_name ?
268                         $this->lang->line("ui_categ_$category_name") : $category_name;
269                 
270                 return $results_data;
271         } 
272 }
273
274 /* End of file catalog.php */
275 /* Location: ./application/controllers/catalog.php */