Skip to content

Instantly share code, notes, and snippets.

@bryanjswift
Created September 23, 2012 06:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanjswift/3769141 to your computer and use it in GitHub Desktop.
Save bryanjswift/3769141 to your computer and use it in GitHub Desktop.
Perch2 field type for vimeo embeds. Heavily based on youtube.class.php by Drew McLellan.
<?php
/**
* A field type for Vimeo videos
*
* @package default
* @author Bryan Swift
*/
class PerchFieldType_vimeo extends PerchAPI_FieldType
{
private $api_url = 'http://vimeo.com/api/v2/video';
public function render_inputs($details=array())
{
$id = $this->Tag->input_id();
$val = '';
if (isset($details[$id]) && $details[$id]!='') {
$json = $details[$id];
$val = $json['path'];
}else{
$json = array();
}
$s = $this->Form->text($this->Tag->input_id(), $val);
if (isset($json['vimeoID'])) {
$s.= '<div class="preview"><iframe src="http://player.vimeo.com/video'.$json['vimeoID'].'" width="320" height="240" frameborder="0"></iframe></div>';
}
return $s;
}
public function get_raw($post=false, $Item=false)
{
$store = array();
$id = $this->Tag->id();
if ($post===false) {
$post = $_POST;
}
if (isset($post[$id])) {
$this->raw_item = trim($post[$id]);
$url = $this->raw_item;
}
if ($url) {
$store['path'] = $url;
$store['vimeoID'] = $this->get_id($url);
$details = $this->get_details($store['vimeoID']);
if ($details) {
$store = array_merge($store, $details);
$store['_title'] = $store['title'];
}
}
return $store;
}
public function get_processed($raw=false)
{
if (is_array($raw)) {
$item = $raw;
if ($this->Tag->output() && isset($item[$this->Tag->output()])) {
return $item[$this->Tag->output()];
}
return $item['path'];
}
return $raw;
}
public function get_search_text($raw=false)
{
if ($raw===false) $raw = $this->get_raw();
if (!PerchUtil::count($raw)) return false;
if (isset($raw['title'])) return $raw['title'];
return false;
}
private function get_id($url)
{
$path = parse_url($url, PHP_URL_PATH);
if ($path) {
return $path;
}
return false;
}
private function get_details($videoID)
{
$url = $this->api_url . $videoID . '.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$php_data = curl_exec($ch);
curl_close($ch);
if ($php_data) {
$a = unserialize($php_data);
if (count($a)) {
return $a[0];
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment