View source
<?php
class OSMTemplate {
private $version = '';
private $prefix = '';
public $settings = array();
private $css_dir = '';
protected $playerSettings = array();
public $defaultIds = array(
'loading' => '#mediaplayerloading',
'player' => '#mediaplayer',
'menu' => '#mediamenu',
'titleBar' => '#mediatitlebar',
'node' => '#medianode',
'playlist' => '#mediaplaylist',
'currentTime' => '#mediacurrenttime',
'totalTime' => '#mediatotaltime',
'playPause' => '#mediaplaypause',
'seekUpdate' => '#mediaseekupdate',
'seekProgress' => '#mediaseekprogress',
'seekBar' => '#mediaseekbar',
'seekHandle' => '#mediaseekhandle',
'volumeUpdate' => '#mediavolumeupdate',
'volumeBar' => '#mediavolumebar',
'volumeHandle' => '#mediavolumehandle',
'mute' => '#mediamute',
'linkText' => '#medialinktext',
'linkScroll' => '#medialinkscroll',
'close' => '#mediamenuclose',
'embed' => '#mediaembed',
'elink' => '#mediaelink',
'email' => '#mediaemail',
'busy' => '#mediabusy',
'preview' => '#mediapreview',
'play' => '#mediaplay',
'media' => '#mediadisplay',
'control' => '#mediacontrol',
'voter' => '#mediavoter',
'uservoter' => '#mediauservoter',
'mediaRegion' => '#mediaregion',
'field' => '.mediafield',
'prev' => '#mediaprev',
'next' => '#medianext',
'loadPrev' => '#medialoadprev',
'loadNext' => '#medialoadnext',
'prevPage' => '#mediaprevpage',
'nextPage' => '#medianextpage',
'pager' => '#mediapager',
'scroll' => '#mediascroll',
'busy' => '#mediabusy',
'links' => '#medialinks',
'listMask' => '#medialistmask',
'list' => '#medialist',
'scrollWrapper' => '#mediascrollbarwrapper',
'scrollBar' => '#mediascrollbar',
'scrollTrack' => '#mediascrolltrack',
'scrollHandle' => '#mediascrollhandle',
'scrollUp' => '#mediascrollup',
'scrollDown' => '#mediascrolldown',
'titleLinks' => '#mediatitlelinks',
);
public function __construct($playerSettings) {
$this->playerSettings = $playerSettings;
$this->settings = $this
->getSettings();
}
public function getVersion() {
$this->version = $this->version ? $this->version : file_get_contents(dirname(__FILE__) . '/version.txt');
return $this->version;
}
public function getSettings() {
return array();
}
public function getThemeRollerCSS() {
$theme_css = '';
if ($this->playerSettings['theme']) {
$theme_folder = 'jquery-ui/css/' . $this->playerSettings['theme'];
if ($contents = opendir(dirname(__FILE__) . '/' . $theme_folder)) {
while (($node = readdir($contents)) !== false) {
if (preg_match('/\\.css$/', $node)) {
$theme_css = $node;
break;
}
}
}
}
return $theme_css ? $theme_folder . '/' . $theme_css : '';
}
public function setCSSDir($dir) {
if ($this->settings['generateCSS']) {
$this->css_dir = $dir;
}
}
public function resetCSS() {
$this
->deleteCSS();
$this
->createCSS();
}
public function getCSSHeader() {
$playerPath = $this->playerSettings['playerPath'];
$css_files = $this
->getCSSFiles();
$header = '<link rel="stylesheet" type="text/css" href="' . $playerPath . $css_files['template'] . '" />';
$header .= "\n";
if (isset($css_files['template_ie'])) {
$header .= '<!--[if IE]><link rel="stylesheet" type="text/css" href="' . $playerPath . $css_files['template_ie'] . '" /><![endif]-->';
$header .= "\n";
}
return $header;
}
public function getCSSFiles() {
$files = array();
if ($this->settings['generateCSS']) {
$css_path = $this->css_dir ? $this->css_dir : dirname(__FILE__);
$css_local_path = $this->css_dir ? '' : 'css/';
$id = $this->playerSettings['id'];
$files['template'] = $css_local_path . "{$id}.css";
$files['template_ie'] = $css_local_path . "{$id}_ie.css";
if (!is_file($css_path . '/' . $files['template'])) {
$this
->createCSS();
}
}
else {
$files = $this->settings['cssFiles'];
}
return $files;
}
private function writeCSS($css, $handle) {
$contents = file_get_contents(dirname(__FILE__) . '/' . $css);
$playerPath = $this->playerSettings['playerPath'] ? $this->playerSettings['playerPath'] : '../';
$contents = str_replace('images/', $playerPath . str_replace(basename($css), '', $css) . 'images/', $contents);
$contents = preg_replace_callback('/z-index\\s*:\\s*([0-9]+)/', create_function('$match', '$zIndex = intval($match[1]);
return $zIndex ? "z-index:" . (' . $this->playerSettings['zIndex'] . ' + $zIndex) : $zIndex;'), $contents);
$len = strlen($contents);
$match = 0;
$matches = array();
preg_match_all('/\\{.*\\}/sU', $contents, $matches, PREG_OFFSET_CAPTURE);
if (count($matches[0]) > 0) {
$match_len = strlen($matches[0][$match][0]);
}
for ($i = 0; $i < $len; $i++) {
if (isset($matches[0][$match + 1]) && $i > $matches[0][$match + 1][1]) {
$match++;
$match_len = strlen($matches[0][$match][0]);
}
$char = $contents[$i];
if (($char == '#' || $char == '.') && !($i > $matches[0][$match][1] && $i <= $matches[0][$match][1] + $match_len)) {
fwrite($handle, $char . $this->playerSettings['id'] . '_');
}
else {
fwrite($handle, $char);
}
}
}
public function createCSS() {
if ($this->settings['generateCSS']) {
$dir = $this->css_dir ? $this->css_dir : dirname(__FILE__) . '/css';
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
@chmod($dir, 0777);
$to_path = $dir . '/' . $this->playerSettings['id'];
$to_css = $to_path . '.css';
$files = array();
$files[$to_css] = array();
if ($this->settings['cssFiles']['theme']) {
$files[$to_css][] = $this->settings['cssFiles']['theme'];
}
if ($this->settings['cssFiles']['template']) {
$files[$to_css][] = $this->settings['cssFiles']['template'];
}
if ($this->settings['cssFiles']['template_ie']) {
$files[$to_path . '_ie.css'] = array(
$this->settings['cssFiles']['template_ie'],
);
}
foreach ($files as $file => $contents) {
$handle = fopen($file, 'a+');
if ($handle) {
foreach ($contents as $content) {
if ($content) {
$this
->writeCSS($content, $handle);
}
}
fclose($handle);
}
@chmod($file, 0777);
}
}
}
public function deleteCSS() {
if ($this->settings['generateCSS']) {
$css_path = $this->css_dir ? $this->css_dir : dirname(__FILE__) . '/css';
$css_path .= '/' . $this->playerSettings['id'];
$css = $css_path . '.css';
if (is_file($css)) {
@chmod($css, 0775);
unlink($css);
}
$css = $css_path . '_ie.css';
if (is_file($css)) {
@chmod($css, 0775);
unlink($css);
}
}
}
public function getIds() {
$ids = array();
if ($this->settings['generateCSS']) {
foreach ($this->settings['ids'] as $index => $id) {
$ids[$index] = $id[0] . $this->prefix . substr($id, 1);
}
}
else {
$ids = $this->settings['ids'];
}
return $ids;
}
public function setPrefix($newPrefix) {
$this->prefix = $newPrefix;
}
public function theme($variables, $subtemplate = '') {
$template = $variables['params']['template'];
$preprocess = 'theme_preprocess' . $subtemplate;
if (method_exists($this, $preprocess)) {
$this
->{$preprocess}($variables);
}
extract($variables, EXTR_SKIP);
ob_start();
include "templates/{$template}/osmplayer_{$template}{$subtemplate}.tpl.php";
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
}