search improved but not finished
[living-lab-site.git] / application / models / videos_model.php
index ec7bb39..0d9fe7b 100644 (file)
@@ -16,8 +16,6 @@ class Videos_model extends CI_Model {
                        $this->load->library('singleton_db');
                        $this->db = $this->singleton_db->connect();
                }
-               
-               $this->load->helper('url');
        }
        
        /**
@@ -176,6 +174,21 @@ class Videos_model extends CI_Model {
                return $video;
        }
        
+       /**
+        * Increment a video parameter from DB: `views`, `likes` or `dislikes`.
+        * 
+        * @param int $id       DB video id
+        * @param string $param DB parameter column name.
+        * @return void
+        */
+       public function inc_video_var($id, $var)
+       {
+               // TODO error report if query returns FALSE
+               $this->db->query('UPDATE `videos` '
+                                               . 'SET `'. $var. '`=`'. $var. '`+1 '
+                                               . 'WHERE id='. $id); 
+       }
+       
        public function get_thumbs($name, $count)
        {
                $thumbs = array();
@@ -185,6 +198,51 @@ class Videos_model extends CI_Model {
                
                return $thumbs;
        }
+
+       public function search_videos($str_search)
+       {
+               // TODO offset, count for search
+               $offset = 0;
+               $count = 100;
+
+               $str_search = trim($str_search);
+               
+               $query = $this->db->query(
+                       "SELECT id, name, title, duration, user_id, views, thumbs_count,
+                               default_thumb
+                       FROM `videos`
+                       WHERE MATCH (title, description, tags) AGAINST (?)
+                       ORDER BY name
+                       LIMIT ?, ?",
+                       array($str_search, $offset, $count)); 
+               
+               if ($query->num_rows() > 0)
+                       $videos = $query->result_array();
+               else
+                       return NULL;
+               
+               $this->load->helper('text');
+               
+               foreach ($videos as & $video)
+               {
+                       // P2P-Tube Video URL
+                       $video['video_url'] = site_url(sprintf("watch/%d/%s",
+                               $video['id'], $video['name']));
+                       
+                       // Thumbnails
+                       $video['thumbs'] = $this->get_thumbs($video['name'], 
+                               $video['thumbs_count']);
+                               
+                       // Ellipsized title
+                       //$video['shorted_title'] = ellipsize($video['title'], 45, 0.75);
+                       $video['shorted_title'] = character_limiter($video['title'], 50);
+                       
+                       // TODO: user information
+                       $video['user_name'] = 'TODO';
+               }
+               
+               return $videos;
+       }
 }
 
 /* End of file videos_model.php */