Sources for file plugins/class/bubbles.php in version 4.0 Beta 1
Click on a comment to hide it. Click here to show all comments.
/**
* Project: Xnyo 4: Bubbles
* File: plugins/class/bubbles.php
*
* Version: 4.0-dev
* SVN Id: $Id: bubbles.php 5 2007-05-18 03:49:07Z bok $
* SVN URL: $HeadURL:
http://svn.syd.wholesalebroadband.com.au/xnyo/trunk/plugins/class/bubbles.php $
* Authors: Robert Amos <bok[at]odynia.org>
*
* Copyright (c) 2001-2007 Robert Amos <bok[at]odynia.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
class XnyoBubbles implements XnyoClassPlugin
{
// Plugin Information
public $_plugin_info = array
(
'name' => 'Xnyo Bubbles Plugin',
'description' => 'Communicate with your bubbles',
'action' => XNYO_PLUGIN_ACTION_GLOBAL,
'varname' => 'bubbles'
);
// storage
private $xnyo;
/**
* Constructor
**/
public function __construct ()
{
$this->xnyo = $GLOBALS['xnyo_parent'];
$this->xnyo->smartyvar->bubbles = array();
}
/**
* Is this actually a bubble?
**/
public function isBubble ($name)
{
if (!defined('BUBBLES_DIR'))
{
if (XNYO_DEBUG) $this->xnyo->d('Trying to verify bubble <b>%s</b> but no BUBBLES_DIR defined.',
$name);
return false;
}
if (!is_dir(BUBBLES_DIR) || !is_readable(BUBBLES_DIR))
{
if (XNYO_DEBUG) $this->xnyo->d('BUBBLES_DIR <b>%s</b> does not exist or cannot be read.',
BUBBLE_DIR);
return false;
}
if (!is_dir(BUBBLES_DIR.$name))
{
if (XNYO_DEBUG) $this->xnyo->d('Bubble <b>%s</b> does not exist.', $name);
return false;
}
return true;
}
/**
* This will return the HTML for the bubble
**/
public function fetchHTML ($name, $tpl=null)
{
global $smarty;
if (!$this->isBubble($name))
return '';
// have we loaded this bubble before?
if (is_null($tpl))
$tpl = 'default.tpl';
// we have, just return the template
if (isset($this->xnyo->smartyvar->bubbles[$name]))
$this->xnyo->smartyvar->bubble = $this->xnyo->smartyvar->bubbles[$name];
// not in existance?
if (!$this->isHTML($name, $tpl))
throw new XnyoError('Unable to locate template <b>%s</b> for bubble <b>%s</b>. File not found or
not readable.', BUBBLES_DIR.$name.DIRSEP.$tpl, $name);
// change the template dir to allow {include} etc to work
$o = $smarty->template_dir;
$smarty->template_dir = BUBBLES_DIR.$name;
$f = $smarty->fetch($tpl);
$smarty->template_dir = $o;
return $f;
}
/**
* Get header and footer HTML for bubbles
**/
public function fetchHeader($name)
{
global $smarty;
$this->xnyo->smartyvar->bubbles[$name] = array
(
'name' => $name,
'javascript' => $this->isScript($name, 'default.js'),
'stylesheet' => $this->isStylesheet($name, 'default.css')
);
$this->xnyo->smartyvar->bubble = $this->xnyo->smartyvar->bubbles[$name];
if (file_exists($smarty->template_dir.DIRSEP.'bubbleheader.tpl'))
return $smarty->fetch('bubbleheader.tpl');
return '';
}
public function fetchFooter()
{
global $smarty;
if (file_exists($smarty->template_dir.DIRSEP.'bubblefooter.tpl'))
return $smarty->fetch('bubblefooter.tpl');
return '';
}
public function fetchEditHeader($name)
{
global $smarty;
$this->xnyo->smartyvar->bubbles[$name] = array
(
'name' => $name,
'javascript' => $this->isScript($name, 'default.js'),
'stylesheet' => $this->isStylesheet($name, 'default.css')
);
if (isset($this->xnyo->smartyvar->bubbles[$name]))
$this->xnyo->smartyvar->bubble = $this->xnyo->smartyvar->bubbles[$name];
if (file_exists($smarty->template_dir.DIRSEP.'editbubbleheader.tpl'))
return $smarty->fetch('editbubbleheader.tpl');
return '';
}
public function fetchEditFooter()
{
global $smarty;
if (file_exists($smarty->template_dir.DIRSEP.'editbubblefooter.tpl'))
return $smarty->fetch('editbubblefooter.tpl');
return '';
}
/**
* Does a template exist?
**/
public function isHTML ($name, $tpl)
{
if (!file_exists(BUBBLES_DIR.$name.DIRSEP.$tpl) || !is_readable(BUBBLES_DIR.$name.DIRSEP.$tpl))
return false;
return true;
}
/**
* Does a script exist
**/
public function isScript($name, $script)
{
if (!file_exists(BUBBLES_DIR.$name.DIRSEP.$script) ||
!is_readable(BUBBLES_DIR.$name.DIRSEP.$script))
return false;
return true;
}
/**
* Return the javascript for a bubble
**/
public function fetchScript ($name, $script=null, $id=0)
{
if (is_null($script))
$script = 'default.js';
if (!$this->isBubble($name) || !$this->isScript($name, $script))
return '';
$f = file_get_contents(BUBBLES_DIR.$name.DIRSEP.$script);
if ($id != 0)
return str_replace('<!id!>', $id, $f);
return $f;
}
/**
* Does a stylesheet exist
**/
public function isStylesheet($name, $css)
{
if (!file_exists(BUBBLES_DIR.$name.DIRSEP.$css) || !is_readable(BUBBLES_DIR.$name.DIRSEP.$css))
return false;
return true;
}
/**
* Return the stylesheet for a bubble
**/
public function fetchStylesheet ($name, $css=null, $id=0)
{
if (is_null($css))
$css = 'default.css';
if (!$this->isBubble($name) || !$this->isStylesheet($name, $css))
return '';
$f = file_get_contents(BUBBLES_DIR.$name.DIRSEP.$css);
if ($id != 0)
return str_replace('<!id!>', $id, $f);
return $f;
}
/**
* Load a list of bubbles
*
* returns an array of the bubble HTML
**/
public function load ($arr)
{
global $smarty;
if (!is_array($arr))
$arr = array($arr);
// lets go
$o = array();
$smarty->assign('bulk', true);
foreach ($arr as $v)
{
if (is_array($v))
{
$name = array_shift($v);
if (!$this->isBubble($name))
throw new XnyoError('Unable to bulk load bubble <b>%s</b> as it is not valid.', $name);
$p = $this->xnyo->load($name, XNYO_PLUGIN_BUBBLE);
$o[] = call_user_func_array(array($p, 'fetchBubble'), $v);
} else
{
if (!$this->isBubble($v))
throw new XnyoError('Unable to bulk load bubble <b>%s</b> as it is not valid.', $v);
$p = $this->xnyo->load($v, XNYO_PLUGIN_BUBBLE);
$o[] = $p->fetchBubble();
}
}
$smarty->assign('bulk', false);
return $o;
}
}
class XnyoBubble implements XnyoClassPlugin
{
// Plugin Information
public $_plugin_info = array
(
'name' => 'Xnyo Bubble Base Class',
'description' => 'Extend this to implement your own bubbles',
'action' => XNYO_PLUGIN_ACTION_NONE
);
// Storage
protected $bubble;
protected $xnyo;
/**
* Constructor
**/
public function __construct ()
{
$this->bubbles = $GLOBALS['bubbles'];
$this->xnyo = $GLOBALS['xnyo_parent'];
// get our class name, strip off the bubble if necessary
$this->bubble = strtolower(get_class($this));
if (strlen($this->bubble) > 7 && substr($this->bubble, 0, 6) == 'bubble')
$this->bubble = substr($this->bubble, 6);
}
/**
* Is this a bubble? Well duh!
**/
public function isBubble ()
{
return true;
}
/**
* Fetch the output of a template
**/
public function fetchHTML ($tpl=null, $headers=false)
{
if (is_null($tpl))
{
$tpl = 'default.tpl';
$headers = true;
}
if ($headers)
return $this->fetchHeader() . $this->fetchHTML($tpl) . $this->fetchFooter();
return $this->bubbles->fetchHTML($this->bubble, $tpl);
}
public function fetchHeader ()
{
return $this->bubbles->fetchHeader($this->bubble);
}
public function fetchFooter ()
{
return $this->bubbles->fetchFooter();
}
public function fetchEditHeader ()
{
return $this->bubbles->fetchEditHeader($this->bubble);
}
public function fetchEditFooter ()
{
return $this->bubbles->fetchEditFooter();
}
public function isHTML ($tpl)
{
return $this->bubbles->isHTML($this->bubble, $tpl);
}
public function fetchBubble ()
{
return $this->fetchHTML();
}
/**
* Shortcut to $smary->assign()
**/
public function assign ($var, $value)
{
global $smarty;
return $smarty->assign($var, $value);
}
/**
* Fetch javascript
**/
public function fetchScript ($script=null)
{
return $this->bubbles->fetchScript($this->bubble, $script);
}
public function isScript($script)
{
return $this->bubbles->isScript($this->bubble, $script);
}
/**
* Fetch stylesheet
**/
public function fetchStylesheet ($css=null)
{
return $this->bubbles->fetchStylesheet($this->bubble, $css);
}
public function isStylesheet ($css)
{
return $this->bubbles->isStylesheet($this->bubble, $css);
}
/**
* Dump the contents of a variable into the error log
**/
public function dump ()
{
$args = func_get_args();
ob_start();
var_dump($args);
error_log(ob_get_contents(), 3, ini_get('error_log'));
ob_end_clean();
}
}
