user gets notified interatively or by email in case of a CIS error
[living-lab-site.git] / application / controllers / video.php
index eb4ed80..320a94c 100644 (file)
@@ -9,6 +9,9 @@
  */
 class Video extends CI_Controller {
 
+       protected $uploaded_file;
+       protected $av_info;
+       
        public function __construct()
        {
                parent::__construct();
@@ -18,38 +21,15 @@ class Video extends CI_Controller {
        
        public function index()
        {
-               
+               //phpinfo();
        }
        
-       public function test($video_id)
+       public function test($param)
        {
-               // 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->model('videos_model');
                
-               $this->load->view('footer');
-               $this->load->view('html_end');
+               echo $this->videos_model->send_upload_error_email($param,
+                               CIS_RESP_UNREACHABLE) ? 's-a trimis' : 'nu s-a trimis';
        }
        
        /**
@@ -65,17 +45,67 @@ class Video extends CI_Controller {
                // **
                // ** LOADING MODEL
                // **
-               // Retrieve video information.
                $this->load->model('videos_model');
-               $this->videos_model->inc_views($id);
+               
+               $data['user_id'] = $this->session->userdata('user_id');
+               if ($data['user_id'] === FALSE)
+                       $data['user_id'] = '';
+               else
+                       $data['user_id'] = intval($data['user_id']);
+               $user_roles = intval($this->session->userdata('roles'));
+//             echo USER_ROLE_ADMIN . ' / ';
+//             var_dump($user_roles);
+//             var_dump($user_roles | USER_ROLE_ADMIN);
+//             die();
+               
+               // Retrieve video information.
                $data['video'] = $this->videos_model->get_video($id, $name);
+               if ($data['video'] === FALSE)
+               {       
+                       $this->load->helper('message');
+                       show_error_msg_page($this, 
+                               $this->lang->line('video_msg_no_video'));
+                       return;
+               }
+               
+               // Video is being processed by CIS.
+               if ($data['video']['activation_code']
+                               && $data['video']['cis_response'] == CIS_RESP_NONE)
+               {
+                       $this->load->helper('message');
+                       show_error_msg_page($this, 
+                               $this->lang->line('video_msg_video_not_ready'));
+                       return;
+               }
+               
+               // Unlogged in user can't see unactivated videos.
+               if (empty($data['user_id']))
+                       $allow_unactivated = FALSE;
+               else
+               {
+                       if (($user_roles & USER_ROLE_ADMIN) == 0
+                                       && $data['user_id'] != $data['video']['user_id'])
+                               $allow_unactivated = FALSE;
+                       else
+                               $allow_unactivated = TRUE;
+               }
+               
+               // Video is not activated; can be seen by owner and admin.
+               if ($data['video']['activation_code'] && !$allow_unactivated)
+               {
+                       $this->load->helper('message');
+                       show_error_msg_page($this, 
+                               $this->lang->line('video_msg_video_unactivated'));
+                       return;
+               }                       
+               
                $categories = $this->config->item('categories');
                $data['video']['category_name'] = 
                        $categories[ $data['video']['category_id'] ];
                $data['plugin_type'] = ($plugin === NULL ? 'auto' : $plugin);
-               $data['user_id'] = $this->session->userdata('user_id');
-               if ($data['user_id'] === FALSE)
-                       $data['user_id'] = '';
+               
+               // Increment the number of views for the video.
+               $this->videos_model->inc_views($id);
                
                // Display page.
                $params = array(        'title' => $data['video']['title'] . ' – '
@@ -117,77 +147,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 +184,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,10 +193,118 @@ class Video extends CI_Controller {
                }
                else
                {
-
+                       $this->load->model('videos_model');
+                       $this->load->helper('video');
+                       $this->config->load('content_ingestion');
+                       
+                       $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, $this->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'),
+                                       $this->av_info['duration'],
+                                       $prepared_formats['db_formats'], $category_id, $user_id,
+                                       $this->uploaded_file);
+                       if ($activation_code == FALSE)
+                       {
+                               $this->load->helper('message');
+                               show_error_msg_page($this, 
+                                       $this->lang->line('video_msg_add_video_db_error'));
+                               return;
+                       }
+                       
+                       // Send a content ingestion request to
+                       // CIS (Content Ingestion Server).
+                       $r = $this->videos_model->send_content_ingestion($activation_code,
+                                       $this->uploaded_file,
+                                       $name, $this->av_info['size'],
+                                       $prepared_formats['transcode_configs']);
+                       if ($r == FALSE)
+                       {
+                               $this->videos_model->set_cis_response($activation_code,
+                                               CIS_RESP_UNREACHABLE);
+                               
+                               $this->load->helper('message');
+                               show_error_msg_page($this, 
+                                               $this->lang->line(
+                                                               'video_msg_send_content_ingestion_error'));
+                               return;
+                       }
+                       
+                       $this->load->helper('message');
+                       show_info_msg_page($this, 
+                               $this->lang->line('video_msg_video_uploaded'));
                }
        }
        
+       /**
+        * URL used by CIS service to announce its content ingestion completion.
+        * 
+        * @param string $activation_code 
+        */
+       public function cis_completion($activation_code)
+       {
+               $this->load->model('videos_model');
+               
+               if ($this->config->item('require_moderation'))
+                       $this->videos_model->set_cis_response($activation_code,
+                                       CIS_RESP_COMPLETION);
+               else
+                       $this->videos_model->activate_video($activation_code);
+               
+//             log_message('info', "cis_completion $activation_code");
+       }
+       
+       /**
+        * URL used by CIS service to annouce an error which occured while
+        * ingesting content.
+        * 
+        * @param string $activation_code
+        * @param string $error_name 'internal_error' corresponds to constant
+        * CIS_RESP_INTERNAL_ERROR and 'unreachable' corresponds to constant
+        * CIS_RESP_UNREACHABLE
+        */
+       public function cis_error($activation_code, $error_name = 'internal_error')
+       {
+               $this->load->model('videos_model');
+               
+               if ($error_name == 'internal_error')
+               {
+                       $this->videos_model->set_cis_response ($activation_code,
+                                       CIS_RESP_INTERNAL_ERROR);
+                       log_message('error',
+                                       "Internal CIS error for activation code $activation_code");
+                       $this->videos_model->send_upload_error_email($activation_code,
+                                       CIS_RESP_INTERNAL_ERROR);
+               }
+               // Unreachable error is announced by a CIS-LB which was unable to
+               // contact an CIS.
+               else if ($error_name == 'unreachable')
+               {
+                       $this->videos_model->set_cis_response($activation_code,
+                                       CIS_RESP_UNREACHABLE);
+                       log_message('error',
+                                       "CIS-LB could not reach any CIS for activation code $activation_code");
+                       $this->videos_model->send_upload_error_email($activation_code,
+                                       CIS_RESP_UNREACHABLE);
+               }
+               else
+               {
+                       log_message('error',
+                                       "Invalid error name received from CIS / CIS-LB for activation code $activation_code");
+               }
+               
+       }
+       
        /**
        * Increments (dis)likes count for video with the specified id and returns to
        * the client as plain text the number if likes.
@@ -351,16 +435,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 +447,27 @@ class Video extends CI_Controller {
 
                        if ($this->upload->do_upload('video-upload-file'))
                        {
+                               $upload_data = $this->upload->data();
+                               $this->uploaded_file = $upload_data['file_name'];
+                               
+                               $this->load->helper('video');
+                               $this->av_info = get_av_info($upload_data['full_path']);
+                               if (!$this->av_info)
+                                       return FALSE;
+                               
                                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;
        }