MODX Evolution Snippet: Vimeo

Post by Salvatore Tedde, 09/02/2012, 0. Category: MODX Evolution

With this Snippet you can resize a Vimeo video (and also get any parameter of the video!). 

Snippet developed by: Lorenzo Stanco.

1. Installation

To install Vimeo Snippet, go to  "Elements > Manage Elements > Snippets > New Snippet" and fill in the fields as follows:
Snippet Name: vimeo
Snippet Description: Resize video and thumb from a embed video.
Snippet code (php):

<?php
/**
 * Snippet Name: vimeo
 * Short Desc: Resize video and thumb from a embed video
 * Author: Lorenzo Stanco <lorenzo.stanco@gmail.com>
 * Version: 0.0.1
 * Last Edited: 17-10-2011
 */

// ---------------------------------------------------
// CONFIG
// ---------------------------------------------------
// $type:     [string], you can use these values: 
//                    - 'id': Video ID 
//                    - 'title': Video Title
//                    - 'description': Video Description
//                    - 'url': Video URL
//                    - 'upload_date': Date of upload
//                    - 'mobile_url': Video URL for mobile devices
//                    - 'thumbnail_small': Video Thumb URL (Small)
//                    - 'thumbnail_medium': Video Thumb URL (Medium) 
//                    - 'thumbnail_large': Video Thumb URL (Large)
//                    - 'user_name': User Name.
//                    - 'user_url': User URL
//                    - 'user_portrait_small': User Thumb URL (Small) 
//                    - 'user_portrait_medium': User Thumb URL (Medium)
//                    - 'user_portrait_large': User Thumb URL (Large)
//                    - 'user_portrait_huge': User Thumb URL (Huge)
//                    - 'stats_number_of_likes': Number of Likes
//                    - 'stats_number_of_plays': Number of Plays
//                    - 'stats_number_of_comments': Number of Comments
//                    - 'duration': Video Duration
//                    - 'width': Video Width
//                    - 'height': Video Height
// $tvname:   [string] Vimeo share code (embed)
// $width:    [int]    width dimension in pixel
// $height:   [int]    height dimension in pixel
//
// ---------------------------------------------------
// EXAMPLES
// ---------------------------------------------------
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`video` &width=`640` &height=`360`]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`id` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`description` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`url` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`upload_date` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`mobile_url` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`thmbnail_small` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`thumbnail_medium` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`thumbnail_large` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`user_name` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`user_url` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`user_portrait_small` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`user_portrait_medium` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`user_portrait_large` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`user_portrait_huge` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`stats_number_of_likes` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`stats_number_of_plays` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`stats_number_of_comments` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`duration` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`width` ]]
// [[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`height` ]]
//-----------------------------------------------------

//$video = $modx->getTemplateVarOutput($tvname);
//$video = $video[$tvname];

$video = str_replace(
    array('##w#', '##e#', '##a#'),
    array('?'   , '=',    '&'   ), 
    $tvname
);

// Chiedo il player?
if ($type == 'video') {

    // Ho già l'embed, cambio solo i parametri di dimensione
    if (!empty($width) && !empty($height)) {
        $pattern_w = '/ width=["]?([0-9]+)["]?/i';
        $pattern_h = '/ height=["]?([0-9]+)["]?/i';
        $video = preg_replace($pattern_w, ' width="'.intval($width).'"', $video);
        $video = preg_replace($pattern_h, ' height="'.intval($height).'"', $video);
    }
        
        // Tolgo il paragrafo descrittivo
        $video = preg_replace('/<p>.*?<\\/p>/', '', $video);
        
    return $video;
    
}

// Chiedo dati API?
if ($type != 'video') {

    // Se chiedo "url", chiedo in realtà la thumb
    if ($type == 'thumb') $type = 'thumbnail_large';

    // Prendo l'ID del video
    $id = null;
    $pattern = '/^<(iframe|embed).*"http.*?\\/video\\/([a-z0-9]*)/i';
    $target_share_code = trim($video);
    if (preg_match($pattern, $target_share_code, $id)) {
        $id = $id[2];
    } else {
        return 'Vimeo error.';
    }

    // Chiedo tutti i dati in formato JSON
        $data_url = 'http://vimeo.com/api/v2/video/' . $id . '.php';
    $data = @file_get_contents($data_url);
    if (!$data) return 'Vimeo error.';
    $data = @unserialize($data);
    if (!$data) return 'Vimeo error.';
    $data = array_pop($data);
    if (empty($data)) return 'Vimeo error.';
    
    // Ritorno quello che serve
    return $data[$type];

}

return 'Vimeo error.';
?>

Now we have to install a PHx Extender:
Snippet Name: phx:escapemodx
Snippet Description: Escape special embed characters
Snippet code (php):

<?php

return str_replace(
    array('?'   , '=',    '&'   ), 
    array('##w#', '##e#', '##a#'),
    $output
);

?>

2. Parameter &type=`video`

You can use this parameter in this way:
1) Create a Textarea TV and assign it to your Template.
2) Open a Resource and insert the Vimeo embedded code in the Textarea TV.
3) Add the Vimeo Snippet Call on your Template:

[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`video` &width=`500` &height=`400`]]

3. Parameter (others)

With this snippet, you can also get other video information, for example the thumbnail, the id, the description, etc etc. Here are some examples:

[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`video` &width=`640` &height=`360`]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`id` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`description` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`url` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`upload_date` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`mobile_url` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`thmbnail_small` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`thumbnail_medium` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`thumbnail_large` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`user_name` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`user_url` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`user_portrait_small` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`user_portrait_medium` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`user_portrait_large` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`user_portrait_huge` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`stats_number_of_likes` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`stats_number_of_plays` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`stats_number_of_comments` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`duration` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`width` ]]
[[vimeo? &tvname=`[*video-1:escapemodx*]` &type=`height` ]]

Write a comment