search MVC reorganized
authorCalin Burloiu <calin.burloiu@gmail.com>
Tue, 6 Sep 2011 14:55:50 +0000 (17:55 +0300)
committerCalin Burloiu <calin.burloiu@gmail.com>
Tue, 6 Sep 2011 14:55:50 +0000 (17:55 +0300)
application/config/p2p-tube.php
application/controllers/catalog.php
application/language/english/ui_lang.php
application/models/videos_model.php
application/views/catalog/search_results_view.php [new file with mode: 0644]
application/views/header.php
css/catalog.css

index 24281e8..9d7813f 100644 (file)
@@ -94,7 +94,7 @@ $config['categories'] = array(1 => 'movies', 2 => 'tech-talks', 3 => 'events', 4
 
 /*
 |--------------------------------------------------------------------------
-| Videos per page
+| Videos per Page
 |--------------------------------------------------------------------------
 |
 | The number of video icons shown per page (as in catalog/category).
@@ -104,10 +104,20 @@ $config['videos_per_page'] = 16;
 
 /*
 |--------------------------------------------------------------------------
-| Videos per row
+| Videos per Row
 |--------------------------------------------------------------------------
 |
 | The number of video icons shown on a single line (as in home page).
 |
 */
 $config['videos_per_row'] = 4;
+
+/*
+|--------------------------------------------------------------------------
+| Search Results per Page
+|--------------------------------------------------------------------------
+|
+| The number of search results shown per page (as in catalog/search).
+|
+*/
+$config['search_results_per_page'] = 10;
index 816be74..4966152 100644 (file)
@@ -58,7 +58,7 @@ class Catalog extends CI_Controller {
                $this->load->view('header', array('selected_menu' => 'home'));
                
                $main_params['content'] = $this->load->view('catalog/index_view', $data, TRUE);
-               $main_params['side'] = $this->load->view('side_default.php', NULL, TRUE);
+               $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
                $this->load->view('main', $main_params);
                
                $this->load->view('footer');
@@ -92,12 +92,7 @@ class Catalog extends CI_Controller {
                // ** LOADING MODEL
                // **
                // Video Category
-               $categories = $this->config->item('categories');
-               $category_id = array_search($category_name, $categories);
-               $vs_data['category_name'] = $category_name;
-               $vs_data['category_id'] = $category_id;
-               $vs_data['category_title'] = $category_name ?
-               $this->lang->line("ui_categ_$category_name") : $category_name;          
+               $vs_data = $this->_get_category_data($category_name);
                
                // Retrieve videos summary.
                $this->load->model('videos_model');
@@ -110,14 +105,14 @@ class Catalog extends CI_Controller {
                $pg_config['base_url'] = site_url("catalog/category/$category_name/");
                $pg_config['uri_segment'] = 4;
                $pg_config['total_rows'] = $this->videos_model->get_videos_count(
-                       $category_id);
+                       $vs_data['category_id']);
                $pg_config['per_page'] = $this->config->item('videos_per_page');
                $this->pagination->initialize($pg_config);
                $vs_data['pagination'] = $this->pagination->create_links();
                
                // Video Summary
-               $data['video_summary'] = $this->load->view('catalog/videos_summary_view',
-                       $vs_data, TRUE);
+//             $data['video_summary'] = $this->load->view('catalog/videos_summary_view',
+//                     $vs_data, TRUE);
                
                $params = array(        'title' => $this->config->item('site_name'),
                                                        'css' => array(
@@ -134,41 +129,63 @@ class Catalog extends CI_Controller {
                $this->load->view('html_begin', $this->html_head_params);
                $this->load->view('header');
                
-               $main_params['content'] = $this->load->view('catalog/category_view', $data, TRUE);
-               $main_params['side'] = $this->load->view('side_default.php', NULL, TRUE);
+//             $main_params['content'] = $this->load->view('catalog/category_view', $data, TRUE);
+               $main_params['content'] = 
+                       $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
+               $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
                $this->load->view('main', $main_params);
                
                $this->load->view('footer');
                $this->load->view('html_end');
        }
        
-       public function search($str_search = "")
+       public function search($search_query = "", $offset = 0, $category_name = NULL)
        {
-               // TODO get query string from URL.
-               if ($str_search === "") 
+               // Redirect to an URL which contains search string if data was passed
+               // via POST method and not via URL segments.
+               $str_post_search = $this->input->post('search', TRUE);
+               if ($search_query === "" && $str_post_search !== FALSE) 
                        redirect('catalog/search/'. $this->input->post('search', TRUE));
-
+               
                // **
                // ** LOADING MODEL
                // **
-               // Video Category
-               // TODO
-               $vs_data['category_name'] = "";
-               $vs_data['category_title'] = "Search Results for &laquo;$str_search&raquo;";
-
-               // Retrieve videos summary.
-               $this->load->model('videos_model');
-               $vs_data['videos'] = $this->videos_model->search_videos(
-                       $str_search);
-               if ($vs_data['videos'] === NULL)
-                       $vs_data['videos'] = array();
-
-               $vs_data['pagination'] = '';
+               // Category
+               $results_data = $this->_get_category_data($category_name);
+               if ($results_data === NULL)
+                       $results_data = array('category_id'=>NULL);
                
-               // Video Summary
-               $data['video_summary'] = $this->load->view('catalog/videos_summary_view',
-                       $vs_data, TRUE);
+               $results_data['search_query'] = $search_query;
                
+               // Check if search string is valid.
+               if (strlen($search_query) < 4)
+               {
+                       $results_data['videos'] = NULL;
+               }
+               else
+               {
+                       // Retrieve search results.
+                       $this->load->model('videos_model');
+                       $results_data['count'] = $this->videos_model->search_videos(
+                               $search_query);
+                       $results_data['videos'] = $this->videos_model->search_videos(
+                               $search_query, intval($offset),
+                               $this->config->item('search_results_per_page'),
+                               $results_data['category_id']);
+                       if ($results_data['videos'] === NULL)
+                               $results_data['videos'] = array();
+       
+                       // Pagination
+                       $this->load->library('pagination');
+                       $pg_config['base_url'] = site_url("catalog/search/$search_query/");
+                       $pg_config['uri_segment'] = 4;
+                       $pg_config['total_rows'] = $results_data['count'];
+                       $pg_config['per_page'] =
+                               $this->config->item('search_results_per_page');
+                       $this->pagination->initialize($pg_config);
+                       $results_data['pagination'] = $this->pagination->create_links();
+               }
+                       
                $params = array(        'title' => $this->config->item('site_name'),
                                                        'css' => array(
                                                                'catalog.css'
@@ -184,14 +201,31 @@ class Catalog extends CI_Controller {
                $this->load->view('html_begin', $this->html_head_params);
                $this->load->view('header');
                
-               $main_params['content'] = $this->load->view('catalog/category_view', $data, TRUE);
-               $main_params['side'] = $this->load->view('side_default.php', NULL, TRUE);
+               // Search Results
+               $main_params['content'] = 
+                       $this->load->view('catalog/search_results_view',
+                               $results_data, TRUE);
+               $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
                $this->load->view('main', $main_params);
                
                $this->load->view('footer');
                $this->load->view('html_end');
-
        }
+       
+       public function _get_category_data($category_name)
+       {
+               if ($category_name === NULL)
+                       return NULL;
+               
+               $categories = $this->config->item('categories');
+               $category_id = array_search($category_name, $categories);
+               $results_data['category_name'] = $category_name;
+               $results_data['category_id'] = $category_id;
+               $results_data['category_title'] = $category_name ?
+                       $this->lang->line("ui_categ_$category_name") : $category_name;
+               
+               return $results_data;
+       } 
 }
 
 /* End of file catalog.php */
index fd366ea..05f8ea1 100644 (file)
@@ -9,7 +9,10 @@ $lang['ui_nav_menu_contact'] = 'Contact';
 $lang['ui_nav_menu_log_in'] = 'Log In';
 $lang['ui_nav_menu_register'] = 'Register';
 
+// Search
 $lang['ui_search'] = 'Search';
+$lang['ui_search_results_for'] = 'Search Results for';
+$lang['ui_results'] = 'result(s)';
 
 // Install Video Plugins
 $lang['ui_install'] = 'Install ';
index 0d9fe7b..69e7328 100644 (file)
@@ -83,7 +83,7 @@ class Videos_model extends CI_Model {
                        $category_id);
                
                if ($query->num_rows() > 0)
-                       return $row = $query->row()->count;
+                       return $query->row()->count;
                
                // Error
                return NULL;
@@ -199,25 +199,53 @@ class Videos_model extends CI_Model {
                return $thumbs;
        }
 
-       public function search_videos($str_search)
+       /**
+        * Searches videos in DB based on a search query string and returns an
+        * associative array of results.
+        * If count is zero the function only return the number of results.
+        * @param string $search_query
+        * @param int $offset
+        * @param int $count
+        * @param int $category_id      if NULL, all categories are searched
+        * @return array        an associative array with the same keys as that from
+        * get_videos_summary's result, but with two additional keys: 
+        * description and date.
+        */
+       public function search_videos($search_query, $offset = 0, $count = 0, 
+                                                                       $category_id = NULL)
        {
-               // TODO offset, count for search
-               $offset = 0;
-               $count = 100;
+               if ($count === 0)
+               {
+                       $selected_columns = "COUNT(*) count";
+                       $limit = "";
+               }
+               else
+               { 
+                       $selected_columns = "id, name, title, duration, user_id, views, thumbs_count, default_thumb, date, description";
+                       $limit = "LIMIT $offset, $count";
+               }
+               
+               if ($category_id !== NULL)
+                       $category_cond = "category_id = '$category_id' AND ";
+               else
+                       $category_cond = "";
 
-               $str_search = trim($str_search);
+               $search_query = trim($search_query);
                
                $query = $this->db->query(
-                       "SELECT id, name, title, duration, user_id, views, thumbs_count,
-                               default_thumb
+                       "SELECT $selected_columns
                        FROM `videos`
-                       WHERE MATCH (title, description, tags) AGAINST (?)
-                       ORDER BY name
-                       LIMIT ?, ?",
-                       array($str_search, $offset, $count)); 
+                       WHERE  $category_cond MATCH (title, description, tags) AGAINST (?)
+                       $limit",
+                       array($search_query)); 
                
                if ($query->num_rows() > 0)
-                       $videos = $query->result_array();
+               {
+                       if ($count === 0)
+                               return $query->row()->count;
+                       else
+                               $videos = $query->result_array();
+               }
                else
                        return NULL;
                
diff --git a/application/views/catalog/search_results_view.php b/application/views/catalog/search_results_view.php
new file mode 100644 (file)
index 0000000..38f036d
--- /dev/null
@@ -0,0 +1,40 @@
+<div class="search_results">
+       <h1><?php echo $this->lang->line('ui_search_results_for')
+               . " &laquo;$search_query&raquo;" ?></h1>
+               
+       <p><?php echo "$count ". $this->lang->line('ui_results'); ?></p>
+
+       <?php echo $pagination ?>
+
+       <?php foreach($videos as $video):
+               $thumb_src = $video['thumbs'][ $video['default_thumb'] ];
+               ?>
+       <div class="video-icon">
+               <div class="video-thumb ui-widget-content ui-corner-all">
+                       <a href="<?php echo $video['video_url'] ?>">
+                               <img src="<?php echo $thumb_src ?>" />
+                               <div class="video-duration"><?php echo $video['duration'] ?></div>
+                       </a>
+               </div>
+               <div class="video-title">
+                       <a href="<?php echo $video['video_url'] ?>">                    
+                       <?php echo $video['shorted_title'] ?></a>
+               </div>          
+               <div class="video-views">
+                       <?php echo $video['views'] . ' '
+                               . ($video['views'] == 1 ? 
+                                       $this->lang->line('ui_view') : 
+                                       $this->lang->line('ui_views') );
+                       ?>
+               </div>
+               <div class="video-username">
+                       <?php echo $this->lang->line('ui_from') . ' TODO' //TODO ?>
+               </div>
+       </div>
+       <?php endforeach ?>
+
+       <?php echo $pagination ?>
+       
+       <div style="clear: both"></div>
+
+</div>
\ No newline at end of file
index d7b616e..14047e1 100644 (file)
@@ -68,21 +68,22 @@ endif ?>
                // Fake JS submit via CI URI segments
                var fakeSubmit = function() {
                        var searchQuery = $('#search').val();
-                       searchQuery = searchQuery.replace(/\+/g, '%2B');        // +
+                       searchQuery = searchQuery.replace(/\*/g, '*2A');  // *
+                       searchQuery = searchQuery.replace(/\+/g, '*2B');        // +
                        searchQuery = searchQuery.replace(/\s/g, '+');  // <white spaces>
-                       searchQuery = searchQuery.replace(/>/g, '%3E'); // >
-                       searchQuery = searchQuery.replace(/\</g, '%3C');        // <
-                       searchQuery = searchQuery.replace(/\(/g, '%28');        // (
-                       searchQuery = searchQuery.replace(/\)/g, '%29');        // )
-                       searchQuery = searchQuery.replace(/~/g, '%7E'); // ~ 
-                       searchQuery = searchQuery.replace(/\*/g, '%2A');  // *
-                       searchQuery = searchQuery.replace(/"/g, '%22'); // " 
+                       searchQuery = searchQuery.replace(/>/g, '*3E'); // >
+                       searchQuery = searchQuery.replace(/\</g, '*3C');        // <
+                       searchQuery = searchQuery.replace(/\(/g, '*28');        // (
+                       searchQuery = searchQuery.replace(/\)/g, '*29');        // )
+                       searchQuery = searchQuery.replace(/~/g, '*7E'); // ~ 
+                       searchQuery = searchQuery.replace(/"/g, '*22'); // " 
                        searchQuery = encodeURI(searchQuery);
+                       searchQuery = searchQuery.replace(/\*/g, '%');  // *
 
                        alert(searchQuery);
                        
-                       //window.location = "<?php echo site_url('catalog/search') ?>
-                       //      + searchQuery;
+                       window.location = "<?php echo site_url('catalog/search') ?>/
+                               + searchQuery;
                };
                
                $('#button-js-quick-search')
index 591b4d0..da17393 100644 (file)
@@ -4,6 +4,12 @@
        margin-bottom: 1em;
 }
 
+.search-results
+{
+       position: relative;
+       margin-bottom: 1em;
+}
+
 .category-title
 {
        /* border-bottom: 1px solid rgb(108,162,222); */