Show Module.php syntax highlighted
<?php
/**
* Module object is loaded using ModulesManager.
*
* Module can be a widget,a component or just a set of classes with templates.
*
* @package ModulesManager
* @author Luke Satin <cyberluk@seznam.cz>
* @version 0.9.6
*/
class Module {
public $object = null;
public $default_pos = 0;
public $admin_pos = 0;
public $display_admin = false;
private $accessRights = array ();
private $locales = true;
private $printed = false;
private $xml = false;
private $i18n_xml = false;
private $templateVars = array ();
const MODE_NORMAL = 1;
const MODE_SEARCH = 2;
protected $_resources;
function __get($var) {
if (strpos($var, 'i18n_') === 0) {
$array = explode("i18n_", $var);
if (!$this->i18n_xml) {
exit ('ERROR: LOCALES NOT LOADED! MODULE:' . $this->getInstance());
}
return i18n :: Translate($array[1], $this->i18n_xml);
} else {
$array = explode("__", $var);
if (sizeof($array) == 2) {
return $this->localeString($array[0], $array[1]);
}
}
}
function __construct($dbModule = false) {
if ($dbModule) {
$this->ECPMODULE($dbModule);
}
}
function getLanguageXML() {
return $this->i18n_xml;
}
function DrawSettings() {
$url = Location :: Rewrite('index.php?op=Settings');
return '<a class="settingsButton" onclick="ajaxify(this)" href="' . $url . '">' . Icon :: Insert('settings', 'icon') . ' Settings</a>';
}
function onSettings() {
try {
$XHTML = $this->InsertForm('settings.xml',$this->getSettings());
$code = 'var XHTML="' . String :: Escape($XHTML) . '";';
$code .= 'net.elitemedia.Settings.setWindow(net.elitemedia.Windows.Create({width:430,content:XHTML}));';
Ajax :: Action(TabEval :: getBehavior($code));
Renderer :: FlushAjax();
} catch (NoAjaxException $e) {
}
}
function onSaveSettings() {
$DataArray = Ajax :: getPostData();
UserCache :: Set('M' . $this->getID() . '_Settings', $DataArray);
try {
Ajax :: Action(TabCallFunction :: getBehavior('net.elitemedia.Settings.closeWindow'));
Renderer :: FlushAjax();
} catch (NoAjaxException $e) {
}
}
function getSettings() {
try {
$settings = UserCache :: Get('M' . $this->getID() . '_Settings');
return new ArrayToObject($settings);
} catch (Exception $e) {
return new ArrayToObject;
}
}
function InsertForm($XMLSource,$DefaultValues=false) {
$form = $this->getPath() . 'forms/' . $XMLSource;
if (@ file_exists($form)) {
$XHTML = XMLForms :: getXHTML($form,$DefaultValues);
return $XHTML;
} else {
return false;
}
}
function addAccess($Rights) {
$this->accessRights = $Rights;
}
function ECPMODULE($dbModule) {
global $ECP;
$this->addDBInfo($dbModule);
// Current site theme can rewrite module's template
if (Engine :: file_exists("styles/" . $ECP->getDefaultTheme() . "/modules/" . $this->getName() . "/" . $this->getTemplatePath())) {
$this->theme_path = "styles/" . $ECP->getDefaultTheme() . "/modules/" . $this->getName() . "/";
} else
if (Engine :: file_exists($this->path . $this->getTemplatePath())) {
$this->theme_path = $this->path;
} else
if (Engine :: file_exists("styles/" . $ECP->getDefaultTheme(2) . "/modules/" . $this->getName() . "/" . $this->getTemplatePath())) {
$this->theme_path = "styles/" . $ECP->getDefaultTheme(2) . "/modules/" . $this->getName() . "/";
} else
if (Engine :: file_exists("styles/" . $ECP->getDefaultTheme(1) . "/modules/" . $this->getName() . "/" . $this->getTemplatePath())) {
$this->theme_path = "styles/" . $ECP->getDefaultTheme(1) . "/modules/" . $this->getName() . "/";
} else {
// FATAL ERROR - TEMPLATE NOT FOUND
$this->theme_path = $this->path;
}
$this->CSS = $this->theme_path . $this->getTemplatePath() . "main.css";
//if (!Engine::file_exists($this->CSS) && Engine::file_exists($this->theme_path.$this->getTemplatePath()."main.css.php"))
// $this->CSS = $this->theme_path.$this->getTemplatePath()."main.css.php?template=".$this->getTemplate();
$this->loadLocaleFile();
$this->loadXML();
}
protected function _createResource($resource) {
$this->_resources[(string) $resource] = array ();
}
protected function _addResource($resource, $name, $href, $image_src = false, $search_data = false, $info = false) {
$object = new module_class_resource;
$object->name = $name;
$object->href = $href;
$object->image_src = $image_src;
$object->info = $info;
if (is_array($search_data) === false && $search_data !== false)
exit ("search_data must be an array! in " . $name);
$object->search_data = $search_data;
$this->_resources[(string) $resource][] = $object;
}
protected function _returnResources() {
return $this->_resources;
}
public function _getResources($mode = MODE_NORMAL) {
return false;
}
public function _getSEO($source, $name) {
return false;
}
function getThemePath() {
return $this->theme_path;
}
/**
* This method sets some of module's properties to it's initial values. You can pass an object which has these basic properties defined to set them in current module.
*
* @param object $module
*/
private function addDBInfo($dbModule) {
$this->height = $dbModule->height;
$this->language = $dbModule->language;
$this->template_name = $dbModule->template;
$this->template = "templates/" . $dbModule->template . "/";
$this->block = $dbModule->block;
$this->accessRights = $dbModule->accessRights;
$this->base = $dbModule->base;
$this->height = $dbModule->height;
$this->status = $dbModule->status;
$this->id = $dbModule->id;
if (isset ($dbModule->object))
$this->object = $dbModule->object;
$this->instance_name = $dbModule->instance_name;
$this->nick = $dbModule->name;
if ($_GET['module'] == $this->getInstance() && User :: inAdmin()) {
$this->display_admin = true;
}
if ((User :: inAdmin() && !$this->display_admin) || ($this->default_pos == -1 && User :: inAdmin() && $this->display_admin)) {
$this->default_pos = $dbModule->admin_position;
} else {
$this->default_pos = $dbModule->position;
}
$this->admin_pos = $dbModule->admin_position;
$this->resource = $dbModule->resource;
$this->path = "modules/" . $this->base . "/";
if ($this->display_admin)
$this->default_pos = 2;
}
/**
* @return boolean Returns current status - if the module is active.
*/
function getStatus() {
if ($_GET['module'] == $this->getInstance() && Location :: currentScript() == "openWindow.php")
return 1;
return $this->status;
}
/**
*
*
* @return string This method returns a path to images folder under current template in module.
*/
function getImagesPath($image = false) {
global $XCS;
if ($image === false) {
return $this->theme_path . $this->template . "images/";
} else {
if (Engine :: file_exists("styles/" . $XCS->getDefaultTheme() . "/modules/" . $this->getName() . "/" . $this->getTemplatePath() . "images/" . $image)) {
$returnPath = "styles/" . $XCS->getDefaultTheme() . "/modules/" . $this->getName() . "/" . $this->getTemplatePath() . "images/";
} else
if (Engine :: file_exists("modules/" . $this->getName() . "/" . $this->getTemplatePath() . "images/" . $image)) {
$returnPath = "modules/" . $this->getName() . "/" . $this->getTemplatePath() . "images/";
} else {
$returnPath = "modules/articles/templates/default/images/";
}
return $returnPath . $image;
}
}
function getTemplatesDir() {
return "modules/" . $this->getName() . "/" . $this->getTemplatePath();
}
/**
* @return int Returns ID stored in database for current module or -1 if it
* doesn't exist (this module was created manualy).
*/
function getID() {
return $this->id;
}
/**
* @return int This method returns module's height (=place number in
* current content position)
*/
function getHeight() {
return $this->height;
}
/**
*
*
* @return string Returns the name of current module. (=name of module,
* directory) - This information is stored in base column in xcs_modules
* table.
*/
function getBase() {
return $this->base;
}
function getName() {
return $this->getBase();
}
/**
*
*
* @return string Returns the root name of table from database,where this
* module stores it's informations. ($prefix."_module_"."$name)
*/
function getInstance() {
if ($this->instance_name != "none") {
$retval = $this->instance_name;
} else {
$retval = $this->base;
}
return $retval;
}
/**
*
*
* @return string Returns the localized name(=nick) of current module (user
* can specify this name in administration - it's stored in name column in
* modules table)
*/
function getNick() {
return $this->nick;
}
/**
*
*
* @return string Returns module's position
*/
function getPosition() {
return $this->default_pos;
}
/**
* @return string Returns module's own language
*/
function getLanguage() {
return $this->language;
}
/**
*
*
* @return string Returns module's position in administration
*/
function getAdminPosition() {
return $this->admin_pos;
}
/**
*
*
* @return string Returns the block of current module or "none" value.
*/
function getBlock() {
return $this->block;
}
/**
*
*
* @return string Returns template of current module.
*/
function getTemplate() {
return $this->template_name;
}
/**
*
*
* @return string Returns template path of current module.
*/
function getTemplatePath() {
return "templates/" . $this->template_name . "/";
}
/**
*
*
* @return string Returns path to module's main directory (including module's name).
*/
function getPath() {
return $this->path;
}
/**
*
*
* @param int $group
* @return object|false Returns object which have rights for given group as properties or FALSE if this module has no rights setted in DB.
*/
function getRights($group) {
global $DB;
$retval = false;
if (!sizeof($this->accessRights)) {
$TA = new TA();
$TA->addQuery(TA :: SELECT, "xcs_modules_access");
$TA->WHERE("module='" . $this->getInstance() . "'");
$TA->Execute();
while ($access = $TA->Result()->FetchObject()) {
$this->accessRights[] = $access;
}
$TA->End();
}
foreach ($this->accessRights as $rights) {
if ($rights->group == $group)
$retval = $rights;
}
if ($retval == false && $group != 0) {
foreach ($this->accessRights as $rights) {
if ($rights->group == 0)
$retval = $rights;
}
}
return $retval;
}
/**
* This method does the same as the {@link Engine} equivalent object with one difference: it checks if current module has rights to publish anything (=safe).
*/
function registerVariable($value, $variable, $type = false) {
global $XCS;
$rights = $this->getRights(User :: $profile->group);
if ($rights->read && $this->getStatus() == 1 || User :: inAdmin()) {
if ($type == 'array' && $value == false)
$value = array ();
$file = $this->content_file;
$this->templateVars[$variable] = $value;
$XCS->registerVariable($value, $variable, $file);
}
}
/**
* This method does the same as the {@link Engine} equivalent with one difference: it checks if current module has rights to publish anything (=safe).
*/
function addContent($text, $customPosition = false, $plain_text = false, $noscroll = false) {
global $ECP;
$rights = $this->getRights(User :: $profile->group);
if ($rights->read && ($this->default_pos != -1 || $customPosition !== false) && $this->getStatus() == 1 || User :: inAdmin()) {
if ($plain_text === true) {
$ECP->addContent($text, $customPosition);
return;
}
if ($customPosition === false)
$customPosition = $this->default_pos;
$theme_path_exists = Engine :: file_exists($this->theme_path . $this->template . $text);
$path_exists = Engine :: file_exists($this->path . $this->template . $text);
$default_exists = Engine :: file_exists($this->path . "templates/default/" . $text);
if ($theme_path_exists || $path_exists || $default_exists) {
if ($theme_path_exists) {
$text = $this->theme_path . $this->template . $text;
} else
if ($path_exists) {
$text = $this->path . $this->template . $text;
} else
if ($default_exists) {
$text = $this->path . "templates/default/" . $text;
}
$this->content_file = $text;
$ECP->addContent($this->content_file, (int)$customPosition);
$ECP->registerVariable($this, "language", $this->content_file); // variable $language is automatically accessible in every template file
$ECP->registerVariable($this, "currentModule", $this->content_file);
$ECP->registerVariable("_" . $this->getTemplate(), "cssid", $this->content_file);
} else {
$ECP->addContent($text, (int)$customPosition);
}
}
}
function getContent() {
if (empty ($this->content_file))
return false;
/*ModulesManager :: disableAll();
ModulesManager :: enableModule($this);
$content2 = ob_get_contents();
ob_end_clean();
ob_start();
$ECP = Engine :: getInstance();
$ECP->printContent($this->default_pos, $this);
$content = ob_get_contents();
ob_end_clean();
ob_start();
echo $content2;
return $content;*/
$currentModule=Engine::getFlag('currentModule');
$parentModule=Engine::getFlag('parentModule');
$ECP = Engine :: getInstance();
foreach ($this->templateVars as $name => $value) {
${ $name } = $value;
}
$content2 = ob_get_contents();
ob_end_clean();
ob_start();
require ($this->content_file);
$content = ob_get_contents();
ob_end_clean();
ob_start();
echo $content2;
return $content;
}
/**
* Loads XML configuration file.
*/
function loadXML() {
$this->xml = Engine :: loadXML("modules/" . $this->getName() . "/config.xml");
}
/**
* Returns XML object of XML configuration file.
*/
function getXML() {
return $this->xml;
}
/**
* This method does the same as the {@link Engine} equivalent.
*/
private function loadLocaleFile() {
global $ECP;
if (User :: isAuthor()) {
$this->language = User :: $profile->language;
}
$this->locales = i18n :: loadLocaleFile($this->language, $this->path);
$this->i18n_xml = Engine :: loadXML($this->path . "language/" . $this->language . ".xml");
}
/**
* This method does the same as the {@link Engine} equivalent.
*/
function localeString() {
global $ECP;
$args = func_get_args();
return i18n :: TranslateDEPRECATED($args, $this->locales);
}
/**
* This method outputs an href link(anchor) which (after you click on it) will open new window(popup) and display given content.
*
* @param string $link_name Text which will be displayed as a hypertext link.
* @param string $file File in current module directory which will be used for displaying the content in new window.
* @param string $params You can pass your own $_GET parameters here.
* @param int $width Window's width
* @param int $height Window's height
* @param bool $closeTag Add </a> tag?
* @return string Returns HTML code which you should insert if you want something to happen :-)
*/
function newWindow($link_name, $file, $params = false, $width = 500, $height = 400, $closeTag = true) {
if ($params)
$params = "&" . $params;
if ($closeTag)
$retval = "<a href=\"openWindow.php?module=" . $this->getInstance() . "$params\" onClick=\"openWindow(this.href,'$file','$width','$height');\" target=\"$file\">$link_name</a>";
else
$retval = "<a href=\"openWindow.php?module=" . $this->getInstance() . "$params\" onClick=\"openWindow(this.href,'$file','$width','$height');\" target=\"$file\">$link_name";
return $retval;
}
/**
* This method outputs a button which (after you click on it) will open new window(popup) and display given content.
*
* @param string $link_name Text which will be displayed as a hypertext link.
* @param string $file File in current module directory which will be used for displaying the content in new window.
* @param string $params You can pass your own $_GET parameters here.
* @param int $width Window's width
* @param int $height Window's height
* @return string Returns HTML code which you should insert if you want something to happen :-)
*/
function newWindowButton($link_name, $file, $params = false, $width = 500, $height = 400) {
if ($params)
$params = "&" . $params;
$retval = "<input type=\"button\" onClick=\"openWindow('openWindow.php?module=" . $this->getInstance() . "&file=$file$params','$file','$width','$height');\" value=\"$link_name\" />";
return $retval;
}
/**
* @return string Returns current module's resource (if any)
*/
function getResource() {
return $this->resource;
}
/**
* @return array Returns module's public resources
*/
function getResources($mode = module_class :: MODE_NORMAL) {
if (isset ($this->object) && $this->object instanceof module_class) {
$retval = $this->object->_getResources($mode);
} else {
$retval = false;
}
return $retval;
}
/**
* Activate current resource for current module.
*/
function activateResource() {
if (!empty ($this->resource) && $this->getStatus() == 1 && !User :: inAdmin() && $_GET['module'] != $this->getInstance()) {
$link = explode("?", $this->resource);
if ($this->default_pos != -1 && $link[0] == "index.php" && ($_GET['module'] == $this->getInstance() && $_GET['block'] == "" && $this->getBlock() == "none" || $_GET['module'] == $this->getInstance() && $this->getBlock() == $_GET['block'] || empty ($_GET['module']) && $this->hide == 0)) {
$this->oldGET = $_GET;
$gets = explode("&", $link[1]);
foreach ($gets as $get) {
$value = explode("=", $get);
$_GET[(string) $value[0]] = $value[1];
}
}
}
}
/**
* Unset GET variables defined by current resource for current module.
*/
function unsetResource() {
if (!empty ($this->resource) && $this->getStatus() == 1 && !User :: inAdmin() && $_GET['module'] != $this->getInstance()) {
$link = explode("?", $this->resource);
if ($this->default_pos != -1 && $link[0] == "index.php" && ($_GET['module'] == $this->getInstance() && $_GET['block'] == "" && $this->getBlock() == "none" || $_GET['module'] == $this->getInstance() && $this->getBlock() == $_GET['block'] || empty ($_GET['module']) && $this->hide == 0)) {
$_GET = $this->oldGET;
}
}
}
function formAction($url) {
// ? (check Survey module)
$url = Engine :: Rewrite('index.php?op=vote');
$url .= '" onsubmit="return net.elitemedia.events.onSubmit();';
return $url;
}
}
?>
See more files for this project here