Skip to content

Instantly share code, notes, and snippets.

@davidDuymelinck
Created March 5, 2014 10:32
Show Gist options
  • Save davidDuymelinck/9364838 to your computer and use it in GitHub Desktop.
Save davidDuymelinck/9364838 to your computer and use it in GitHub Desktop.
<?php
class MixedGallery extends WireData implements Module {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
'title' => 'Mixed gallery',
'version' => 001,
'summary' => 'Add functions to show gallery with regular images and video previews.',
'requires' => "ProcessGetVideoThumbs",
);
}
/**
* Initialize the module
*
* ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
* when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
*
*/
public function init() {
}
const LINK = 1;
const EMBED = 2;
protected $gallery = array();
public function create($images, $videoUrls){
$lines = explode("\n", $videoUrls);
$ids = array();
foreach($lines as $line){
$id = trim(substr(strrchr($line, "v="), 2));
$ids[strtolower($id)] = $id;
}
foreach($images as $image){
if(strpos($image->basename, 'video') === false){
$this->gallery[] = array('image' => $image, 'type' => 'regular');
}else{
$image_id = str_replace(array('video-','.jpg'), '', $image->basename);
if(array_key_exists($image_id, $ids)){
$this->gallery[] = array('image' => $image, 'type' => 'video', 'id' => $ids[$image_id]);
}else{
$this->gallery[] = array('image' => $image, 'type' => 'regular');
}
}
}
}
public function all(){
return $this->gallery;
}
public function link($id, $type = self::LINK){
$segment = '';
switch($type){
case self::LINK: $segment = 'v'; break;
case self::EMBED: $segment = 'embed'; break;
}
return 'http://www.youtube.com/'.$segment.'/'.$id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment