3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /************************************************************************** * API VIMEO * **************************************************************************/ function _msmvimeo_api_request($uri, $options = array()) { module_load_include('php', 'msmvimeo', 'VimeoApi/src/Vimeo'); $instance = field_info_instance('node', 'field_vimeo', 'video'); $widget = $instance['widget']; $settings = $widget['settings']; $access_token = 'c605e81f4d865b952eb9842da0380f6b'; $vimeo = new Vimeo($settings['vimeo_consumer_key'], $settings['vimeo_consumer_secret'], $access_token); $result = array(); try { $result = $vimeo->request($uri, $options); } catch (VimeoRequestException $e) { drupal_set_message("Encountered an API error -- code {$e->getCode()} - {$e->getMessage()}", 'error'); watchdog('msmvimeo', "Vimeo : La requête via l'api vimeo a échoué - user: {$settings["vimeo_username"]}.", array(), WATCHDOG_ERROR); } return $result; } function _msmvimeo_list_videos_with_formatted_info($settings, $reset = FALSE){ $list = _msmvimeo_list_videos($settings, $reset); $list = _msmvimeo_unset_from_list_not_public_video($list); $list_with_formatted_info = _msmvimeo_list_videos_add_formatted_info_list_videos($list); return $list_with_formatted_info; } /** * List videos in account associated to the current widget */ function _msmvimeo_list_videos($settings, $reset = FALSE) { $cid = 'vimeo_list_'.$settings["vimeo_username"]; if ($reset === FALSE) { // On cherche les informations en cache $cache = cache_get($cid); if (!empty($cache)) { return $cache->data; } } $list = array(); $videos = _msmvimeo_api_request('/users/'.$settings["vimeo_username"].'/videos', array('perpage' => 50, 'sort' => 'date', 'page' => 1)); foreach($videos['body']['data'] as $entry) { $list[] = $entry; } variable_set($cid, $list); cache_set($cid, $list, 'cache', time()+3600); watchdog('msmvimeo', "Vimeo : La mise à jour de la liste des vidéos de {$settings["vimeo_username"]} a été effectuée avec succès."); return $list; } function _msmvimeo_unset_from_list_not_public_video($list) { $new_list = array(); foreach($list as $entry) { if ($entry['privacy']['view'] == 'anybody') { $new_list[] = $entry; } } return $new_list; } function _msmvimeo_list_videos_add_formatted_info_list_videos($videos_list){ foreach($videos_list as $key => $video) { $videos_list[$key] = _msmvimeo_video_details_add_formatted_info($video); } return $videos_list; } function _msmvimeo_video_details_add_formatted_info($video) { $video['formatted_info']['title'] = $video['name']; $video['formatted_info']['description'] = $video['description']; $vimeo_id = explode('/',$video['uri']); $vimeo_id = $vimeo_id[2]; $video['formatted_info']['vimeo_id'] = $vimeo_id; $video['formatted_info']['date'] = date('d/m/Y H:m:i', strtotime($video['modified_time'])); $video['formatted_info']['link'] = $video['link']; $video['formatted_info']['duration'] = $video['duration']; $video['formatted_info']['stats_number_of_plays'] = $video['stats']['plays']; return $video; } function _msmvimeo_video_details_with_formatted_info($vimeo_id){ $video = _msmvimeo_api_request('/videos/' . $vimeo_id); $video_details_with_formatted_info = _msmvimeo_video_details_add_formatted_info($video['body']); return $video_details_with_formatted_info; } /** * Retrieve details from Vimeo about a video **/ function _msmvimeo_video_details_with_formatted_info_and_local_thumbnail($vimeo_id, $instance) { $data = _msmvimeo_api_request('/videos/' . $vimeo_id); $data = _msmvimeo_video_details_add_formatted_info($data['body']); //Download large thumbnail to our server (for use with ImageCache) if (!empty($data['pictures']['sizes'])) { $thumbnail_large = end($data['pictures']['sizes'])['link']; $result = _msmvimeo_download_thumbnail($vimeo_id, $thumbnail_large, $instance['field_name']); if ($result === TRUE) { //Add local thumbnail to data array $data['thumbnail_local'] = 'public://'.$instance['field_name'].'/' . $vimeo_id . '.jpg'; } } return $data; } /** * Download thumbnail from Vimeo **/ function _msmvimeo_download_thumbnail($id, $url, $directory) { //Check if the containing folder exists -- if not, create it. $dir = 'public://'.$directory; file_prepare_directory($dir, FILE_CREATE_DIRECTORY); file_prepare_directory($dir, FILE_MODIFY_PERMISSIONS); //Setup local filename $destination = $dir.'/'.$id . '.jpg'; $result = drupal_http_request($url); $code = floor($result->code / 100) * 100; $types = array('image/jpeg'); if ($result->data && $code != 400 && $code != 500) { $src = file_save_data($result->data, $destination, FILE_EXISTS_REPLACE); image_path_flush($destination); return TRUE; } } /************************************************************************** * VIMEO CACHE * **************************************************************************/ /** * Implements hook_permission(). */ function msmvimeo_permission() { return array( 'flush vimeo cache' => array( 'title' => 'Gestion des vidéos', 'description' => 'Vider le cache de la liste des vidéos', ), ); } /** * Implements hook_menu(). */ function msmvimeo_menu() { $items['vimeo/cache/flush/%/%/%'] = array( 'page callback' => 'msmvimeo_flush_cache', 'page arguments' => array(3,4,5), 'access arguments' => array('flush vimeo cache'), 'type' => MENU_CALLBACK, ); return $items; } function msmvimeo_flush_cache($type, $name, $bundle) { $instance = field_info_instance($type, $name, $bundle); if (!empty($instance)) { $widget = $instance['widget']; $vimeo_settings = $widget['settings']; $list = _msmvimeo_list_videos($vimeo_settings, TRUE); drupal_set_message("La liste des vidéos a été mise à jour"); } else { drupal_set_message("La liste des vidéos n'a pas pu être mise à jour", 'error'); } if (!empty($_GET['destination'])) { drupal_goto($_GET['destination']); } elseif (!empty($list)) { return $list; } } /************************************************************************** * VIMEO STATS * **************************************************************************/ function msmvimeo_refresh_plays($type, $name, $bundle) { $instance = field_info_instance($type, $name, $bundle); if (!empty($instance)) { $widget = $instance['widget']; $settings = $widget['settings']; $videos = _msmvimeo_api_request('/users/'.$settings["vimeo_username"].'/videos', array('perpage' => 1, 'sort' => 'date')); $total = !empty($videos['body']['total']) ? $videos['body']['total'] : 0; $pages = ceil($total/50); for ($i=1; $i <=$pages; $i++) { $videos = _msmvimeo_api_request('/users/'.$settings["vimeo_username"].'/videos', array('perpage' => 50, 'sort' => 'date', 'page' => $i)); $videos = _msmvimeo_unset_from_list_not_public_video($videos['body']['data']); $videos = _msmvimeo_list_videos_add_formatted_info_list_videos($videos); foreach($videos as $video) { $video_id = $video['formatted_info']['vimeo_id']; $nid = db_query('SELECT entity_id FROM {field_data_field_vimeo} WHERE field_vimeo_vimeo_id = :vid', array(':vid' => $video_id))->fetchColumn(0); if (!empty($nid)) { $node = node_load($nid); $node->field_plays[LANGUAGE_NONE][0]['value'] = $video['formatted_info']['stats_number_of_plays']; field_attach_update('node', $node); } } } } } /************************************************************************** * FIELD VIMEO * **************************************************************************/ /** * Implements hook_field_info(). * Defines the field for admin/structure/types/manage/dummy/fields */ function msmvimeo_field_info() { return array( 'msmvimeo' => array( 'label' => 'Champ Vimeo', 'description' => 'Champ de référence aux vidéos d\'un compte Vimeo', 'settings' => array('num_dimensions' => 2), // DO NOT REMOVE => Define the number of values exposed by the field... 'default_widget' => 'msmvimeo_vimeo_field', 'default_formatter' => 'msmvimeo_default' ) ); } /** * Implements hook_field_is_empty(). * Defines when the result returned by the field should be regarded as empty */ function msmvimeo_field_is_empty($item, $field) { if ($field['type'] == 'msmvimeo') { if (empty($item['vimeo_id'])) { return TRUE; } return FALSE; } } /** * Implements hook_field_widget_info(). * Defines the widget settings */ function msmvimeo_field_widget_info() { return array( 'msmvimeo_vimeo_form' => array( 'label' => 'Vidéo associée', 'field types' => array('msmvimeo'), 'behaviors' => array( 'multiple values' => FIELD_BEHAVIOR_DEFAULT, // DO NOT REMOVE => Define the number of values exposed by the field... 'default value' => FIELD_BEHAVIOR_NONE, ), ), ); } /** * Implements hook_field_widget_settings_form(). * Defines the form to define settings for the widget called in a content type */ function msmvimeo_field_widget_settings_form($field, $instance) { $widget = $instance['widget']; $settings = $widget['settings']; $form['vimeo_username'] = array( '#type' => 'textfield', '#size' => 60, '#maxlenght' => 255, '#title' => 'Nom d\'utilisateur du compte Vimeo', '#default_value' => isset($settings['vimeo_username']) ? $settings['vimeo_username'] : null, '#description' => 'Merci de bien vouloir indiquer le nom d\'utilisateur du compte Vimeo', '#required' => TRUE, ); $form['vimeo_consumer_key'] = array( '#type' => 'textfield', '#size' => 60, '#maxlenght' => 255, '#title' => 'Clef client pour l\'API', '#default_value' => isset($settings['vimeo_consumer_key']) ? $settings['vimeo_consumer_key'] : null, '#description' => 'Merci de bien vouloir indiquer la clef client pour l\'API', '#required' => TRUE, ); $form['vimeo_consumer_secret'] = array( '#type' => 'textfield', '#size' => 60, '#maxlenght' => 255, '#title' => 'Mot de passe pour l\'API', '#default_value' => isset($settings['vimeo_consumer_secret']) ? $settings['vimeo_consumer_secret'] : null, '#description' => 'Merci de bien vouloir indiquer le mot de passe client pour l\'API', '#required' => TRUE, ); return $form; } /** * Implements hook_field_formatter_info(). * Defines the formatter for the widget viewed in content */ function msmvimeo_field_formatter_info() { return array( 'embed_video' => array( 'label' => 'Vidéo intégrée', 'field types' => array('msmvimeo'), 'settings' => array('vimeo_player_width' => 536, 'vimeo_player_height' => 302) ), ); } /** * Implements hook_field_formatter_settings_form(). * Defines the form to to define settings for the formatter */ function msmvimeo_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) { $display = $instance['display'][$view_mode]; $settings = $display['settings']; $element['vimeo_player_width'] = array( '#type' => 'textfield', '#size' => 3, '#maxlenght' => 3, '#title' => 'Largeur du player', '#default_value' => $settings['vimeo_player_width'], '#description' => 'Valeur en pixels', '#required' => TRUE, ); $element['vimeo_player_height'] = array( '#type' => 'textfield', '#size' => 3, '#maxlenght' => 3, '#title' => 'Hauteur du player', '#default_value' => $settings['vimeo_player_height'], '#description' => 'Valeur en pixels', '#required' => TRUE, ); return $element; } /** * Implements hook_field_formatter_settings_summary(). * Defines the summary returned by the formatter */ function msmvimeo_field_formatter_settings_summary($field, $instance, $view_mode) { $display = $instance['display'][$view_mode]; $settings = $display['settings']; $summary = array(); if ($display['type'] == 'embed_video') { if (empty($settings) || !isset($settings['vimeo_player_width']) || !isset($settings['vimeo_player_height'])) { $summary[] = '<div style="color:red">Vous devez préciser la largeur et la hauteur du player Vimeo.</div>'; } else { $summary[] = 'Hauteur : '.$settings['vimeo_player_height'].'px'; $summary[] = 'Largeur : '.$settings['vimeo_player_width'].'px'; } } return implode('<br />', $summary); } /** * Implements hook_field_widget_form(). * Defines the form for the widget in entities creation form */ function msmvimeo_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { $base = $element; $widget = $instance['widget']; $vimeo_settings = $widget['settings']; // Define default value $options = array(); if (!empty($items[$delta]['vimeo_id'])) { $data = unserialize($items[$delta]['vimeo_data']); $options[$items[$delta]['vimeo_id']] = $data['formatted_info']['title']; } // videos waiting on Vimeo $list = _msmvimeo_list_videos_with_formatted_info($vimeo_settings); foreach ($list as $entry) { $entry_id = $entry['formatted_info']['vimeo_id']; $options[$entry_id] = $entry['formatted_info']['title']; } // online videos $query = new EntityFieldQuery; $query->entityCondition('entity_type', $instance['entity_type']); if ($instance['entity_type'] == 'node') { $query->propertyCondition('type', $instance['bundle']); } if (!empty($items[$delta]['vimeo_id'])) { $query->fieldCondition($instance['field_name'], 'vimeo_id', $items[$delta]['vimeo_id'], '<>'); } $query->fieldOrderBy($instance['field_name'], 'vimeo_id', 'ASC'); $result = $query->execute(); if (!empty($result[$instance['entity_type']])) { $entities = entity_load($instance['entity_type'], array_keys($result[$instance['entity_type']])); foreach ($entities as $entity) { foreach ($entity->{$instance['field_name']} as $lines) { foreach ($lines as $entry) { unset($options[$entry["vimeo_id"]]); } } } } if (isset($_GET['vimeo_id']) && !isset($items[$delta]['vimeo_id'])) { $items[$delta]['vimeo_id'] = (int)$_GET['vimeo_id']; } // Form $element['vimeo_id']= array( '#title' => !empty($instance['label']) ? $instance['label'] : 'Vidéo associée', '#required' => $instance['required'], '#type' => 'select', '#options' => $options, '#default_value' => isset($items[$delta]['vimeo_id']) ? $items[$delta]['vimeo_id'] : NULL, '#description' => "Pour mettre à jour la liste des vidéos, utilisez <strong>". l('ce lien', 'vimeo/cache/flush/'.$instance['entity_type'].'/'.$instance['field_name'].'/'.$instance['bundle'], array('query' => array('destination' => current_path())))."</strong>." ); $element['vimeo_data']= array( '#type' => 'hidden' ); return $element; } /** * Implements hook_field_presave(). * Presave operations handled on the field => cf. Vimeo API */ function msmvimeo_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) { if ($entity_type == 'node' && isset($entity->is_imported) && $entity->is_imported === TRUE) { return; } if ($field['type'] == 'msmvimeo') { foreach ($items as $delta => $item) { $data = _msmvimeo_video_details_with_formatted_info_and_local_thumbnail($item['vimeo_id'], $instance); if (!empty($data)) { $items[$delta]['vimeo_data'] = serialize($data); } } } } /** * Implements hook_field_formatter_view(). * Defines the output of the formatter */ function msmvimeo_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { $element = array(); $settings = $display['settings']; switch ($display['type']) { case 'embed_video': foreach ($items as $delta => $item) { $player = '<iframe src="https://player.vimeo.com/video/'.$item['vimeo_id'].'?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=0" width="'.$settings['vimeo_player_width'].'" height="'.$settings['vimeo_player_height'].'" frameborder="0"></iframe>'; $element[$delta] = array( '#markup' => $player, ); } break; } return $element; }
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.3.0beta1