user profile page; account activation backend (without UI)
[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, TRUE);
64                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
65                 $this->load->view('main', $main_params);
66                 
67                 $this->load->view('footer');
68                 $this->load->view('html_end');
69         }
70         
71         public function test($page = 0)
72         {
73                 $this->load->model('users_model');
74                 echo Users_model::gen_activation_code('calin.burloiu');
75         }
76         
77         public function category($category_name, $ordering = 'hottest', $offset = 0)
78         {
79                 // **
80                 // ** LOADING MODEL
81                 // **
82                 // Video Category
83                 $category_data = Catalog::_get_category_data($category_name);
84                 
85                 // Retrieve videos summary.
86                 $this->load->model('videos_model');
87                 $vs_data['videos'] = $this->videos_model->get_videos_summary(
88                         $category_data['category_id'], NULL, intval($offset),
89                         $this->config->item('videos_per_page'), $ordering);
90                 
91                 $vs_data['ordering'] = $ordering;
92                 
93                 // Pagination
94                 $this->load->library('pagination');
95                 $pg_config['base_url'] = site_url("catalog/category/$category_name/$ordering/");
96                 $pg_config['uri_segment'] = 5;
97                 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
98                         $category_data['category_id']);
99                 $pg_config['per_page'] = $this->config->item('videos_per_page');
100                 $this->pagination->initialize($pg_config);
101                 $vs_data['pagination'] = $this->pagination->create_links();
102                 $vs_data['category_name'] = $category_data['category_name'];
103                 $vs_data['title'] = $category_data['category_title'];
104                 
105                 $params = array(        'title' =>
106                                                                 $category_data['category_title'].' &ndash; '
107                                                                         . $this->config->item('site_name'),
108                                                         'css' => array(
109                                                                 'catalog.css'
110                                                         )
111                                                         //'metas' => array('description'=>'','keywords'=>'')
112                                                         );
113                 $this->load->library('html_head_params', $params);
114                 
115                 // **
116                 // ** LOADING VIEWS
117                 // **
118                 $this->load->view('html_begin', $this->html_head_params);
119                 $this->load->view('header', array(
120                         'search_category_name'=> $vs_data['category_name']
121                 ));
122                 
123                 $main_params['content'] = 
124                         $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
125                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
126                 $this->load->view('main', $main_params);
127                 
128                 $this->load->view('footer');
129                 $this->load->view('html_end');
130         }
131         
132         public function search($search_query = "", $offset = 0, $category_name = NULL)
133         {
134                 $this->load->model('videos_model');
135                 $this->load->library('security');
136                 
137                 // Redirect to an URL which contains search string if data was passed
138                 // via POST method and not via URL segments.
139                 $str_post_search = $this->input->post('search');
140                 $str_post_category = $this->input->post('search-category');
141                 if ($search_query === "" && $str_post_search !== FALSE) 
142                 {
143                         redirect('catalog/search/'
144                                 . $this->videos_model->encode_search_query($str_post_search)
145                                 . '/0'
146                                 . ($str_post_category === FALSE ? '' : "/$str_post_category"));
147                         return;
148                 }
149
150                 // **
151                 // ** LOADING MODEL
152                 // **
153                 // Search query is encoded for URL and must be decoded.
154                 $enc_search_query = $search_query;
155                 $search_query = $this->videos_model->decode_search_query($search_query);
156                 
157                 // Security filtering
158                 $search_query = $this->security->xss_clean($search_query);
159                 $results_data['search_query'] = $search_query;
160
161                 // Category
162                 $results_data = Catalog::_get_category_data($category_name);
163                 if ($results_data === NULL)
164                         $results_data = array('category_id'=>NULL);
165                 
166                 // Page header data
167                 $header_data['search_query'] = $search_query;
168                 if ($category_name !== NULL)
169                 {
170                         $header_data['search_category_name'] = $results_data['category_name'];
171                 }
172                 
173                 // Check if search string is valid.
174                 if (strlen($search_query) === 0)
175                 {
176                         //$results_data['videos'] = NULL;
177                         $this->error($this->lang->line('error_search_query_empty'),
178                                 $header_data);
179                         return;
180                 }
181                 else
182                 {
183                         // Retrieve search results.
184                         $results_data['count'] = $this->videos_model->search_videos(
185                                 $search_query, 0, 0, $results_data['category_id']);
186                         $results_data['videos'] = $this->videos_model->search_videos(
187                                 $search_query, intval($offset),
188                                 $this->config->item('search_results_per_page'),
189                                 $results_data['category_id']);
190                         if ($results_data['videos'] === NULL)
191                                 $results_data['videos'] = array();
192         
193                         // Pagination
194                         $this->load->library('pagination');
195                         $pg_config['base_url'] = site_url("catalog/search/$enc_search_query/");
196                         $pg_config['uri_segment'] = 4;
197                         $pg_config['total_rows'] = $results_data['count'];
198                         $pg_config['per_page'] =
199                                 $this->config->item('search_results_per_page');
200                         $this->pagination->initialize($pg_config);
201                         $results_data['pagination'] = $this->pagination->create_links();
202                 }
203                 
204                 // HTML head parameters
205                 $params = array(        'title' => 'Search Results &ndash; '
206                                                                 . $this->config->item('site_name'),
207                                                         'css' => array(
208                                                                 'catalog.css'
209                                                         ),
210                                                         //'js' => array(),
211                                                         //'metas' => array('description'=>'','keywords'=>'')
212                                                         );
213                 $this->load->library('html_head_params', $params);
214                 
215                 // **
216                 // ** LOADING VIEWS
217                 // **
218                 $this->load->view('html_begin', $this->html_head_params);
219                 $this->load->view('header', $header_data);
220                 
221                 // Search Results
222                 $main_params['content'] = 
223                         $this->load->view('catalog/search_results_view',
224                                 $results_data, TRUE);
225                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
226                 $this->load->view('main', $main_params);
227                 
228                 $this->load->view('footer');
229                 $this->load->view('html_end');
230         }
231         
232         public function error($msg, $header_data)
233         {
234                 $params = array(        'title' => 'Error &ndash; '
235                                                                 . $this->config->item('site_name'),
236                                                         //'css' => array(),
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                 $main_params['content'] = $this->load->view('error_view', array('msg'=>$msg), TRUE);
249                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
250                 $this->load->view('main', $main_params);
251                 
252                 $this->load->view('footer');
253                 $this->load->view('html_end');
254         }
255         
256         public static function _get_category_data($category_name)
257         {
258                 $ci =& get_instance();
259                 
260                 if ($category_name === NULL)
261                         return NULL;
262                 
263                 $categories = $ci->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                         $ci->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 */