/**
  * Import a single video in real time (not in the background).
  *
  * This function relies on the import form output being in the
  * $_POST variable. The form should be validated before calling this.
  *
  * @return bool $success true if no error, false otherwise
  */
 private static function _importSingle()
 {
     require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'import.php';
     require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'Google' . DIRECTORY_SEPARATOR . 'Client.php';
     require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'Google' . DIRECTORY_SEPARATOR . 'Service' . DIRECTORY_SEPARATOR . 'YouTube.php';
     $client = new Google_Client();
     $client->setApplicationName("Omeka_Youtube_Import");
     $client->setDeveloperKey(YoutubeImport_ImportHelper::$youtube_api_key);
     try {
         $service = new Google_Service_YouTube($client);
     } catch (Exception $e) {
         throw $e;
     }
     if (isset($_REQUEST['youtubeurl'])) {
         $url = $_REQUEST['youtubeurl'];
     } else {
         throw new UnexpectedValueException('URL of Youtube video was not set');
     }
     if (isset($_REQUEST['youtubecollection'])) {
         $collection = $_REQUEST['youtubecollection'];
     } else {
         $collection = 0;
     }
     if (isset($_REQUEST['youtubeuserrole'])) {
         $ownerRole = $_REQUEST['youtubeuserrole'];
     } else {
         $ownerRole = 0;
     }
     if (isset($_REQUEST['youtubepublic'])) {
         $public = $_REQUEST['youtubepublic'];
     } else {
         $public = false;
     }
     try {
         $videoID = YoutubeImport_ImportHelper::ParseURL($url);
         $response = YoutubeImport_ImportHelper::GetVideo($videoID, $service, $collection, $ownerRole, $public);
         $post = $response['post'];
         $files = $response['files'];
     } catch (Exception $e) {
         throw $e;
     }
     $record = new Item();
     $record->setPostData($post);
     if (!$record->save(false)) {
         throw new Exception($record->getErrors());
     }
     if (!empty($files) && !empty($record)) {
         if (!insert_files_for_item($record, 'Url', $files)) {
             throw new Exception("Error attaching files");
         }
     }
     return true;
 }
 /**
  * Import a youtube video as submitted by a user through the contribution form
  *
  * This function relies on the import form output being in the
  * $_POST variable. The form should be validated before calling this.
  *
  * @return bool $success true if no error, false otherwise
  */
 private static function _importContributedSingle($item)
 {
     require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'import.php';
     require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'Google' . DIRECTORY_SEPARATOR . 'Client.php';
     require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'Google' . DIRECTORY_SEPARATOR . 'Service' . DIRECTORY_SEPARATOR . 'YouTube.php';
     $client = new Google_Client();
     $client->setApplicationName("Omeka_Youtube_Import");
     $client->setDeveloperKey(YoutubeImport_ImportHelper::$youtube_api_key);
     try {
         $service = new Google_Service_YouTube($client);
     } catch (Exception $e) {
         throw $e;
     }
     if (isset($_REQUEST['youtubeurl'])) {
         $url = $_REQUEST['youtubeurl'];
     } else {
         throw new UnexpectedValueException('URL of Youtube video was not set');
     }
     if (!isset($item)) {
         $item = Item();
     }
     try {
         $videoID = YoutubeImport_ImportHelper::ParseURL($url);
         $response = YoutubeImport_ImportHelper::GetVideo($videoID, $service, $item->collection_id, 'Contributor', get_option('contributedItemPublic') && $item->public == 1);
         $post = $response['post'];
         $files = $response['files'];
     } catch (Exception $e) {
         $item->delete();
         throw $e;
     }
     $post['collection_id'] = $item['collection_id'];
     $item->setPostData($post);
     if (!$item->save(false)) {
         throw new Exception($item->getErrors());
     }
     if (!empty($files) && !empty($item)) {
         if (!insert_files_for_item($item, 'Url', $files)) {
             throw new Exception("Error attaching files");
         }
     }
     return true;
 }