uploading works, but AV info is not automatically detected and video activation featu...
[living-lab-site.git] / application / controllers / video.php
index eb4ed80..366b92f 100644 (file)
@@ -9,6 +9,8 @@
  */
 class Video extends CI_Controller {
 
+       protected $uploaded_file;
+       
        public function __construct()
        {
                parent::__construct();
@@ -18,38 +20,12 @@ class Video extends CI_Controller {
        
        public function index()
        {
-               
+               //phpinfo();
        }
        
-       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');
+               var_dump($this->session->userdata('user_id'));
        }
        
        /**
@@ -117,77 +93,25 @@ 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()
        {
+               $user_id = $this->session->userdata('user_id');
+               
+               // Action not possible if an user is not logged in.
+               if (!$user_id)
+               {
+                       $this->load->helper('message');
+                       show_error_msg_page($this, 
+                               $this->lang->line('ui_msg_login_restriction'));
+                       return;
+               }
+               
                $this->load->library('form_validation');
 
                $this->form_validation->set_error_delimiters('<span class="error">',
                                '</span>');
-               $error_upload = '';
-
+               
                if ($this->form_validation->run('upload') === FALSE)
                {
                        $params = array('title' =>
@@ -206,9 +130,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 +139,39 @@ class Video extends CI_Controller {
                }
                else
                {
-
+                       $this->load->model('videos_model');
+                       $this->load->helper('video');
+                       $this->config->load('content_ingestion');
+                       
+                       $file_name = $this->uploaded_file;
+                       $av_info = get_av_info($file_name);
+                       $name = urlencode(str_replace(' ', '-',
+                                       $this->input->post('video-title')));
+                       $category_id = $this->input->post('video-category');
+                       
+                       // Prepare formats
+                       $formats = $this->config->item('formats');
+                       $prepared_formats = prepare_formats($formats, $av_info,
+                                       $this->config->item('elim_dupl_res'));
+                       
+                       // Add video to DB.
+                       $activation_code = $this->videos_model->add_video($name,
+                                       $this->input->post('video-title'),
+                                       $this->input->post('video-description'),
+                                       $this->input->post('video-tags'),
+                                       $av_info['duration'],
+                                       $prepared_formats['db_formats'], $category_id, $user_id);
+                       
+                       // Send a content ingestion request to
+                       // CIS (Content Ingestion Server).
+                       $this->_send_content_ingestion($activation_code,
+                                       $file_name,
+                                       $name, $av_info['size'],
+                                       $prepared_formats['transcode_configs']);
+                       
+                       $this->load->helper('message');
+                       show_info_msg_page($this, 
+                               $this->lang->line('video_msg_video_uploaded'));
                }
        }
        
@@ -317,6 +271,50 @@ class Video extends CI_Controller {
                        return $output;
        }
        
+       /**
+        * Request content_ingest to the CIS in order to start the content
+        * ingestion process.
+        * 
+        * @param string $activation_code
+        * @param string $raw_video_fn uploaded video file name
+        * @param string $name
+        * @param int $raw_video_size uploaded video file size in bytes
+        * @param array $transcode_configs dictionary which must be included in
+        * the JSON data that needs to be sent to CIS
+        * @return mixed return the HTTP content (body) on success and FALSE
+        * otherwise
+        */
+       protected function _send_content_ingestion($activation_code, $raw_video_fn,
+                       $name, $raw_video_size, $transcode_configs)
+       {
+               $this->config->load('content_ingestion');
+               
+               $url = $this->config->item('cis_url') . 'ingest_content';
+               $data = array(
+                       'code'=>$activation_code,
+                       'raw_video'=>$raw_video_fn,
+                       'name'=>$name,
+                       'weight'=>$raw_video_size,
+                       'transcode_configs'=>$transcode_configs,
+                       'thumbs'=>$this->config->item('thumbs_count')
+               );
+               $json_data = json_encode($data);
+               
+               // Send request to CIS.
+               $r = new HttpRequest($url, HttpRequest::METH_POST);
+               $r->setBody($json_data);
+               try
+               {
+                       $response = $r->send()->getBody();
+               }
+               catch (HttpException $ex) 
+               {
+                       return FALSE;
+               }
+               
+               return $response;
+       }
+       
        public function _is_user_loggedin($param)
        {
                if (! $this->session->userdata('user_id'))
@@ -351,16 +349,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'])
@@ -373,14 +361,21 @@ class Video extends CI_Controller {
 
                        if ($this->upload->do_upload('video-upload-file'))
                        {
+                               $this->uploaded_file = $this->upload->data();
+                               $this->uploaded_file = $this->uploaded_file['file_name'];
                                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;
        }