add video model and controller partially implemented
authorCălin-Andrei Burloiu <calin.burloiu@gmail.com>
Mon, 13 Feb 2012 21:56:30 +0000 (23:56 +0200)
committerCălin-Andrei Burloiu <calin.burloiu@gmail.com>
Mon, 13 Feb 2012 21:56:30 +0000 (23:56 +0200)
application/config/form_validation.php
application/controllers/video.php
application/helpers/av_info_helper.php
application/helpers/video_helper.php
application/language/english/video_lang.php
application/models/videos_model.php
application/views/video/upload_view.php

index 85c5ca8..6c5c1e6 100644 (file)
@@ -129,7 +129,7 @@ $config = array(
                array(
                        'field'=>'video-upload-file',
                        'label'=>'lang:video_upload_file',
-                       'rules'=>'callback__required_upload|callback__valid_upload'
+                       'rules'=>'callback__valid_upload'
                ),
                array(
                        'field'=>'video-title',
index eb4ed80..eb89005 100644 (file)
@@ -18,38 +18,22 @@ class Video extends CI_Controller {
        
        public function index()
        {
-               
        }
        
-       public function test($video_id)
+       public function test()
        {
-               // Display page.
-               $params = array(        'title' => $this->config->item('site_name'),
-                                                                       'css' => array(
-                                                                               'video.css'
-               ),
-                                                                       'js' => array(
-                                                                               'jquery.ui.ajax_links_maker.js'
-               ),
-               //'metas' => array('description'=>'','keywords'=>'')
-               );
-               $this->load->library('html_head_params', $params);
-               
-               // **
-               // ** LOADING VIEWS
-               // **
-               $this->load->view('html_begin', $this->html_head_params);
-               $this->load->view('header');
-               
-               $main_params['content'] =
-                       $this->load->view('echo', array('output'=> 
-                               $this->_ajax_comment(TRUE, $video_id)),
-                       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');
+               $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
+               $r->setOptions(array('cookies' => array('lang' => 'de')));
+               $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
+               $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
+               try
+               {
+                       echo $r->send()->getBody();
+               }
+               catch (HttpException $ex) 
+               {
+                       echo $ex;
+               }
        }
        
        /**
@@ -117,68 +101,6 @@ class Video extends CI_Controller {
                $this->load->view('footer');
                $this->load->view('html_end');
        }
-        
-//     public function upload()
-//     {
-//             $this->load->library('form_validation');
-//
-//             $this->form_validation->set_error_delimiters('<span class="error">',
-//                             '</span>');
-//             $error_upload = '';
-//
-//             if ($this->form_validation->run('upload'))
-//             {
-//                     if ($_FILES['video-upload-file']['tmp_name'])
-//                     {
-//                             // Upload library
-//                             $config_upload['upload_path'] = './data/upload';
-//                             $this->load->library('upload', $config_upload);
-//
-//                             $b_validation = $this->upload->do_upload('video-upload-file');
-//                             $error_upload = 
-//                                     $this->upload->display_errors('<span class="error">',
-//                                                     '</span>');
-//                     }
-//                     else
-//                     {
-//                             $b_validation = FALSE;
-//                     }
-//             }
-//             else
-//                     $b_validation = FALSE;
-//
-//             if ($b_validation === FALSE)
-//             {
-//                     $params = array('title' =>
-//                                                             $this->lang->line('ui_nav_menu_upload')
-//                                                                     .' &ndash; '
-//                                                                     . $this->config->item('site_name'),
-//                                                     //'metas' => array('description'=>'')
-//                     );
-//                     $this->load->library('html_head_params', $params);
-//
-//                     // **
-//                     // ** LOADING VIEWS
-//                     // **
-//                     $this->load->view('html_begin', $this->html_head_params);
-//                     $this->load->view('header',
-//                                     array('selected_menu' => 'upload'));
-//
-//                     $main_params['content'] = $this->load->view(
-//                                     'video/upload_view',
-//                                     array('error_upload'=> $error_upload),
-//                                     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');
-//             }
-//             else
-//             {
-//
-//             }
-//     }
                
        public function upload()
        {
@@ -186,7 +108,8 @@ class Video extends CI_Controller {
 
                $this->form_validation->set_error_delimiters('<span class="error">',
                                '</span>');
-               $error_upload = '';
+               
+               // TODO check if user is logged in
 
                if ($this->form_validation->run('upload') === FALSE)
                {
@@ -206,9 +129,7 @@ class Video extends CI_Controller {
                                        array('selected_menu' => 'upload'));
 
                        $main_params['content'] = $this->load->view(
-                                       'video/upload_view',
-                                       array('error_upload'=> $error_upload),
-                                       TRUE);
+                                       'video/upload_view', array(), TRUE);
                        $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
                        $this->load->view('main', $main_params);
 
@@ -217,7 +138,20 @@ class Video extends CI_Controller {
                }
                else
                {
-
+                       $this->load->model('videos_model');
+                       $this->load->helper('video');
+                       
+                       $file_name = './data/upload/'. $_FILES['video-upload-file']['name'];
+                       $av_info = get_av_info($file_name);
+                       
+                       // TODO category_id, user_id
+//                     $this->videos_model->add_video(
+//                                     $this->input->post('video-title'),
+//                                     $this->input->post('video-description'),
+//                                     $this->input->post('video-tags'),
+//                                     $av_info, 0, 1);
+                       
+                       // TODO call CIS
                }
        }
        
@@ -351,16 +285,6 @@ class Video extends CI_Controller {
                return TRUE;
        }
        
-       public function _required_upload($file)
-       {
-               if ($_FILES['video-upload-file']['tmp_name'])
-               {
-                       return TRUE;
-               }
-               
-               return FALSE;
-       }
-       
        public function _valid_upload($file)
        {
                if ($_FILES['video-upload-file']['tmp_name'])
@@ -375,12 +299,17 @@ class Video extends CI_Controller {
                        {
                                return TRUE;
                        }
-                       
-                       $this->form_validation->set_message('_valid_upload',
-                                       $this->upload->display_errors('<span class="error">',
-                                                       '</span>'));
+                       else
+                       {
+                               $this->form_validation->set_message('_valid_upload',
+                                               $this->upload->display_errors('<span class="error">',
+                                                               '</span>'));
+                               return FALSE;
+                       }
                }
                
+               $this->form_validation->set_message('_valid_upload',
+                               $this->lang->line('_required_upload'));
                return FALSE;
        }
        
index 013dab3..b3f3a55 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 /*
+ * OBSOLETE!
  * These functions retrieve information about audio and video files.
  * Depends on MediaInfo CLI program (http://mediainfo.sourceforge.net/)
  * 
index 64efee5..b845898 100644 (file)
@@ -100,5 +100,26 @@ function get_closest_res($haystack, $needle, $access_function = NULL)
        return $i_min;
 }
 
+/**
+ * Returns information about an Audio/Video file.
+ * 
+ * @param string $file_name Audio/Video file
+ * @return dictionary a dictionary of audio/video properties with keys:
+ * <ul>
+ *   <li>width</li>
+ *   <li>height</li>
+ *   <li>dar (display aspect ratio)</li>
+ *   <li>duration (formated as [HH:]mm:ss)</li>
+ *   <li>size (in bytes)</li>
+ * </ul>
+ */
+function get_av_info($file_name)
+{
+       // TODO use ffprobe to return width, height, DAR, duration and size of a video
+       
+       return array('width'=> 800, 'height'=> 600, 'dar'=> '16:9',
+                       'duration'=> '00:10', 'size'=> 1101693);
+}
+
 /* End of file video_helper.php */
 /* Location: ./application/helpers/video_helper.php */
\ No newline at end of file
index 056b751..459a5df 100644 (file)
@@ -22,6 +22,7 @@ $lang['video_description'] = 'Description';
 $lang['video_tags'] = 'Tags';
 $lang['video_tags_hint'] = 'comma separated tags';
 $lang['video_submit_upload'] = 'Upload';
+$lang['video_msg_video_uploaded'] = 'Your video has been uploaded and is now being processed.';
 
 
 /* End of file video_lang.php */
index afcfd3d..b03cd16 100644 (file)
@@ -166,11 +166,11 @@ class Videos_model extends CI_Model {
         * If $name does not match with the video's `name` from the DB an error is
         * marked in the key 'err'. If it's NULL it is ignored.
         *
-        * @access              public
-        * @param               string $id      video's `id` column from `videos` DB table
-        * @param               string $name    video's `name` column from `videos` DB
+        * @access public
+        * @param string $id    video's `id` column from `videos` DB table
+        * @param string $name  video's `name` column from `videos` DB
         * table. NULL means there is no name provided.
-        * @return              array   an associative list with information about a video
+        * @return array        an associative list with information about a video
         * with the following keys:
         * <ul>
         *   <li>all columns form DB with some exceptions that are overwritten or new</li>
@@ -248,6 +248,68 @@ class Videos_model extends CI_Model {
                return $video;
        }
        
+       public function compute_video_name($title)
+       {
+               $name = str_replace(' ', '-', $title);
+               
+               return urlencode($name);
+       }
+       
+       /**
+        * Adds a new uploaded video to the DB.
+        * 
+        * @param type $title
+        * @param type $description
+        * @param type $tags comma separated tags
+        * @param type $av_info a dictionary of video properties containing keys
+        * width, height, dar (display aspect ratio), duration and size; is can be
+        * returned with function get_av_info from video helper
+        */
+       public function add_video($title, $description, $tags, $av_info,
+                       $category_id, $user_id)
+       {
+               $name = $this->compute_video_name($title);
+               
+               // Tags.
+               $json_tags = array();
+               $tok = strtok($tags, ',');
+               while ($tok != FALSE)
+               {
+                       $json_tags[trim($tok)] = 0;
+                       
+                       $tok = strtok(',');
+               }
+               $json_tags = json_encode($json_tags);
+               
+               // TODO formats
+               $json_formats = '[{"res":"1280x720","ext":"ogv","dar":"16:9"},'
+                               . '{"res":"1067x600","ext":"ogv","dar":"16:9"}]';
+               
+               
+               $query = $this->db->query("INSERT INTO `videos`
+                               (name, title, description, duration, formats, category_id,
+                                               user_id, tags, date)
+                               VALUES ('$name', '$title', '$description', '"
+                                               . $av_info['duration']. "', '$json_formats', $category_id,
+                                               $user_id, '$json_tags', utc_timestamp())");
+               if ($query === FALSE)
+                       return FALSE;
+               
+               // Find out the id of the new video added.
+               $query = $this->db->query("SELECT id from `videos`
+                               WHERE name = '$name'");
+               if ($query->num_rows() === 0)
+                       return FALSE;
+               $video_id = $query->row()->id;
+               
+               // Activation code.
+               $activation_code = Videos_model::gen_activation_code();
+               
+               $query = $this->db->query("INSERT INTO `videos_unactivated`
+                               (video_id, activation_code)
+                               VALUES ($video_id, '$activation_code')");
+       }
+       
        /**
         * Retrieves comments for a video.
         * 
@@ -617,6 +679,19 @@ class Videos_model extends CI_Model {
                return (preg_match('/\*|\+|\-|>|\<|\(|\)|~|"/', $search_query) == 0
                        ? FALSE : TRUE);
        }
+       
+       public static function gen_activation_code()
+       {
+               $ci =& get_instance();
+               
+               $activation_code = substr(
+                       sha1($ci->config->item('encryption_key')
+                               . mt_rand()),
+                       0,
+                       16);
+               
+               return $activation_code;
+       }
 }
 
 /* End of file videos_model.php */
index 702251a..51a3db1 100644 (file)
@@ -12,7 +12,6 @@
                <td><input type="file" name="video-upload-file" size="32" /></td>
        </tr>
        <tr><td></td><td><?php echo form_error('video-upload-file') ?></td></tr>
-       <!--<tr><td></td><td><?php echo $error_upload ?></td></tr>-->
        
        <tr><td></td><td>&nbsp;</td></tr>