search queries with less than 4 characters find videos via LIKE sql command
[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('selected_menu' => 'home'));
59                 
60                 $main_params['content'] = $this->load->view('catalog/index_view', $data, TRUE);
61                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
62                 $this->load->view('main', $main_params);
63                 
64                 $this->load->view('footer');
65                 $this->load->view('html_end');
66         }
67         
68         public function test($page = 0)
69         {
70                 $params = array(        'title' => 'Test - '. $this->config->item('site_name'),
71                                                         //'css' => array(),
72                                                         //'js' => array(),
73                                                         //'metas' => array('description'=>'','keywords'=>'')
74                         );
75                 $this->load->library('html_head_params', $params);
76                 
77                 // **
78                 // ** LOADING VIEWS
79                 // **
80                 $this->load->view('html_begin', $this->html_head_params);
81                 $this->load->view('header', array('selected_menu' => 'home'));
82                 
83                 $this->load->view('echo', array('output'=>'Test Page', 'clear'=>TRUE));
84                 
85                 $this->load->view('footer');
86                 $this->load->view('html_end');
87         }
88         
89         public function category($category_name, $ordering = 'hottest', $offset = 0)
90         {
91                 // **
92                 // ** LOADING MODEL
93                 // **
94                 // Video Category
95                 $vs_data = $this->_get_category_data($category_name);
96                 
97                 // Retrieve videos summary.
98                 $this->load->model('videos_model');
99                 $vs_data['videos'] = $this->videos_model->get_videos_summary(
100                         $vs_data['category_id'], intval($offset),
101                         $this->config->item('videos_per_page'), $ordering);
102                 
103                 $vs_data['ordering'] = $ordering;
104                 
105                 // Pagination
106                 $this->load->library('pagination');
107                 $pg_config['base_url'] = site_url("catalog/category/$category_name/$ordering/");
108                 $pg_config['uri_segment'] = 5;
109                 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
110                         $vs_data['category_id']);
111                 $pg_config['per_page'] = $this->config->item('videos_per_page');
112                 $this->pagination->initialize($pg_config);
113                 $vs_data['pagination'] = $this->pagination->create_links();
114                 
115                 // Video Summary
116 //              $data['video_summary'] = $this->load->view('catalog/videos_summary_view',
117 //                      $vs_data, TRUE);
118                 
119                 $params = array(        'title' => $this->config->item('site_name'),
120                                                         'css' => array(
121                                                                 'catalog.css'
122                                                         ),
123                                                         //'js' => array(),
124                                                         //'metas' => array('description'=>'','keywords'=>'')
125                                                         );
126                 $this->load->library('html_head_params', $params);
127                 
128                 // **
129                 // ** LOADING VIEWS
130                 // **
131                 $this->load->view('html_begin', $this->html_head_params);
132                 $this->load->view('header', array(
133                         'search_category_name'=>$vs_data['category_name'],
134                         'search_category_title'=>$vs_data['category_title']
135                 ));
136                 
137 //              $main_params['content'] = $this->load->view('catalog/category_view', $data, TRUE);
138                 $main_params['content'] = 
139                         $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
140                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
141                 $this->load->view('main', $main_params);
142                 
143                 $this->load->view('footer');
144                 $this->load->view('html_end');
145         }
146         
147         public function search($search_query = "", $offset = 0, $category_name = NULL)
148         {
149                 // Redirect to an URL which contains search string if data was passed
150                 // via POST method and not via URL segments.
151                 $str_post_search = $this->input->post('search', TRUE);
152                 if ($search_query === "" && $str_post_search !== FALSE) 
153                         redirect('catalog/search/'. $this->input->post('search', TRUE));
154
155                 $this->load->model('videos_model');
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                 $results_data['search_query'] = $search_query;
164
165                 // Category
166                 $results_data = $this->_get_category_data($category_name);
167                 if ($results_data === NULL)
168                         $results_data = array('category_id'=>NULL);
169                 
170                 // Page header data
171                 $header_data['search_query'] = $search_query;
172                 if ($category_name !== NULL)
173                 {
174                         $header_data['search_category_name'] = $results_data['category_name'];
175                         $header_data['search_category_title'] = $results_data['category_title'];
176                 }
177                 
178                 // Check if search string is valid.
179                 if (strlen($search_query) === 0)
180                 {
181                         //$results_data['videos'] = NULL;
182                         $this->error($this->lang->line('error_search_query_empty'),
183                                 $header_data);
184                         return;
185                 }
186                 else
187                 {
188                         // Retrieve search results.
189                         $results_data['count'] = $this->videos_model->search_videos(
190                                 $search_query, 0, 0, $results_data['category_id']);
191                         $results_data['videos'] = $this->videos_model->search_videos(
192                                 $search_query, intval($offset),
193                                 $this->config->item('search_results_per_page'),
194                                 $results_data['category_id']);
195                         if ($results_data['videos'] === NULL)
196                                 $results_data['videos'] = array();
197         
198                         // Pagination
199                         $this->load->library('pagination');
200                         $pg_config['base_url'] = site_url("catalog/search/$enc_search_query/");
201                         $pg_config['uri_segment'] = 4;
202                         $pg_config['total_rows'] = $results_data['count'];
203                         $pg_config['per_page'] =
204                                 $this->config->item('search_results_per_page');
205                         $this->pagination->initialize($pg_config);
206                         $results_data['pagination'] = $this->pagination->create_links();
207                 }
208                 
209                 // HTML head parameters
210                 $params = array(        'title' => $this->config->item('site_name'),
211                                                         'css' => array(
212                                                                 'catalog.css'
213                                                         ),
214                                                         //'js' => array(),
215                                                         //'metas' => array('description'=>'','keywords'=>'')
216                                                         );
217                 $this->load->library('html_head_params', $params);
218                 
219                 // **
220                 // ** LOADING VIEWS
221                 // **
222                 $this->load->view('html_begin', $this->html_head_params);
223                 $this->load->view('header', $header_data);
224                 
225                 // Search Results
226                 $main_params['content'] = 
227                         $this->load->view('catalog/search_results_view',
228                                 $results_data, TRUE);
229                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
230                 $this->load->view('main', $main_params);
231                 
232                 $this->load->view('footer');
233                 $this->load->view('html_end');
234         }
235         
236         public function error($msg, $header_data)
237         {
238                 $params = array(        'title' => 'Error - '. $this->config->item('site_name'),
239                         //'css' => array(),
240                         //'js' => array(),
241                         //'metas' => array('description'=>'','keywords'=>'')
242                 );
243                 $this->load->library('html_head_params', $params);
244                 
245                 // **
246                 // ** LOADING VIEWS
247                 // **
248                 $this->load->view('html_begin', $this->html_head_params);
249                 $this->load->view('header', $header_data);
250                 
251                 $main_params['content'] = $this->load->view('error_view', array('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 function _get_category_data($category_name)
260         {
261                 if ($category_name === NULL)
262                         return NULL;
263                 
264                 $categories = $this->config->item('categories');
265                 $category_id = array_search($category_name, $categories);
266                 $results_data['category_name'] = $category_name;
267                 $results_data['category_id'] = $category_id;
268                 $results_data['category_title'] = $category_name ?
269                         $this->lang->line("ui_categ_$category_name") : $category_name;
270                 
271                 return $results_data;
272         } 
273 }
274
275 /* End of file catalog.php */
276 /* Location: ./application/controllers/catalog.php */