aliroModule.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*******************************************************************************
00004  * Aliro - the modern, accessible content management system
00005  *
00006  * Aliro is open source software, free to use, and licensed under GPL.
00007  * You can find the full licence at http://www.gnu.org/copyleft/gpl.html GNU/GPL
00008  *
00009  * The author freely draws attention to the fact that Aliro derives from Mambo,
00010  * software that is controlled by the Mambo Foundation.  However, this section
00011  * of code is totally new.  If it should contain any fragments that are similar
00012  * to Mambo, please bear in mind (1) there are only so many ways to do things
00013  * and (2) the author of Aliro is also the author and copyright owner for large
00014  * parts of Mambo 4.6.
00015  *
00016  * Tribute should be paid to all the developers who took Mambo to the stage
00017  * it had reached at the time Aliro was created.  It is a feature rich system
00018  * that contains a good deal of innovation.
00019  *
00020  * Your attention is also drawn to the fact that Aliro relies on other items of
00021  * open source software, which is very much in the spirit of open source.  Aliro
00022  * wishes to give credit to those items of code.  Please refer to
00023  * http://aliro.org/credits for details.  The credits are not included within
00024  * the Aliro package simply to avoid providing a marker that allows hackers to
00025  * identify the system.
00026  *
00027  * Copyright in this code is strictly reserved by its author, Martin Brampton.
00028  * If it seems appropriate, the copyright will be vested in the Aliro Organisation
00029  * at a suitable time.
00030  *
00031  * Copyright (c) 2007 Martin Brampton
00032  *
00033  * http://aliro.org
00034  *
00035  * counterpoint@aliro.org
00036  *
00037  * aliroLoginDetails is a simple data class used to create an object to carry the
00038  * information from a user login - user ID, password and the flag for whether the
00039  * system is to "remember" the user and automatically log them in.  The main use
00040  * for objects of this class is to pass data to mambots related to the authentication
00041  * process.
00042  *
00043  * aliroExtensionHandler knows all about the various installed extensions in
00044  * the system.  Anything not integral to the core - components, modules, mambots,
00045  * templates - are counted as extensions.  It is a cached singleton class and
00046  * uses common code the implement the object cache.
00047  *
00048  * aliroAuthenticator is the abstract class that contains common code for use
00049  * on both the user and admin sides of Aliro.
00050  *
00051  * aliroUserAuthenticator is the class that is instantiated to handle user side
00052  * authentication - basically login and logout.  On the user side, the actual
00053  * authentication is done by mambots.  The default Aliro authentication mambot
00054  * checks the credentials against the database, although it calls back to the
00055  * aliroUserAuthenticator class to perform the actual validation.  It is possible
00056  * to supplement the default processing with other mambots, or replace it
00057  * completely.  Uses for such an approach might include use of an LDAP system.
00058  * There are several mambot "hooks" and the other purpose for this is to be able
00059  * to integrate extensions that elaborate the handling of users with additional
00060  * properties and such like.
00061  *
00062  * aliroModuleHandler is a cached singleton class that looks after all the data
00063  * for modules within Aliro.  It is optimised towards creating useful data
00064  * structures in the constructor, which are then cached.  The access methods
00065  * are as simple as possible, so as to give the best run time performance.
00066  *
00067  * aliroModule is the object that corresponds to an entry in the module table.
00068  * In addition, it has methods to assist in the rendering of modules on the
00069  * browser screen.  Details of format are referred to the template, so that
00070  * control of XHTML is kept out of the core.
00071  *
00072  */
00073 
00074 class aliroModuleHandler extends aliroCommonExtHandler {
00075     protected static $instance = __CLASS__;
00076 
00077     private $allModules = array();
00078     private $keyToSubscript = array();
00079     private $user_area_links = array();
00080     private $admin_area_links = array();
00081     private $allMenusByModule = array();
00082     private $allModulesByMenu = array();
00083     private $distinct_user_side = array();
00084     private $visibleKeys = array();
00085     private $modulesByFormalName = array();
00086 
00087     protected $extensiondir = '/modules/';
00088 
00089     protected function __construct () {
00090         $query = "SELECT m1.*, (CASE WHEN m2.menuid = 0 THEN 'All' WHEN m2.menuid IS NULL THEN 'None' ELSE 'Varies' END) pages"
00091         ." FROM `#__modules` m1 LEFT JOIN `#__modules_menu` m2 ON m1.id = m2.moduleid"
00092         ." GROUP BY m1.id ORDER BY m1.position, m1.ordering";
00093         $database = aliroCoreDatabase::getInstance();
00094         if ($result = $database->doSQLget($query, 'aliroModule')) $this->allModules = $result;
00095         $translatePages = array ('All' => T_('All'), 'None' => T_('None'), 'Varies' => T_('Varies'));
00096         foreach ($this->allModules as $sub=>&$module) {
00097             $this->keyToSubscript[$module->id] = $sub;
00098             $this->modulesByFormalName[$module->module] = $sub;
00099             $module->pages = $translatePages[$module->pages];
00100             if ($module->published) {
00101                 if ($module->admin & 1) $this->user_area_links[$module->position][] = $module->id;
00102                 if ($module->admin & 2) $this->admin_area_links[$module->position][] = $module->id;
00103             }
00104             if ($module->admin & 1) $distinct_user_side[$module->module] = 1;
00105         }
00106         if (isset($distinct_user_side)) {
00107             $this->distinct_user_side = array_keys($distinct_user_side);
00108             sort($this->distinct_user_side);
00109         }
00110         $database->setQuery ("SELECT * FROM #__modules_menu");
00111         if ($menus = $database->loadObjectList()) foreach ($menus as $menu) {
00112             $this->allMenusByModule[$menu->moduleid][] = $menu->menuid;
00113         }
00114         $database->setQuery("SELECT m1.menuid, m2.moduleid FROM `#__modules_menu` m1"
00115         ." INNER JOIN `#__modules_menu` m2 ON m1.menuid = m2.menuid OR m2.menuid =0"
00116         ." GROUP BY m1.menuid, m2.moduleid");
00117         if ($menus = $database->loadObjectList()) foreach ($menus as $menu) {
00118             $this->allModulesByMenu[$menu->menuid][] = $menu->moduleid;
00119         }
00120     }
00121 
00122     // Singleton accessor with cache
00123     public static function getInstance () {
00124         return is_object(self::$instance) ? self::$instance : (self::$instance = parent::getCachedSingleton(self::$instance));
00125     }
00126 
00127     public function makeModuleFromExtension ($extension) {
00128         $newmodule = new aliroModule();
00129         $newmodule->title = T_('Please select a title');
00130         // Can't set ordering until we know position
00131         $newmodule->published = 1;
00132         $newmodule->module = $extension->formalname;
00133         $newmodule->showtitle = 1;
00134         $newmodule->admin = $extension->admin;
00135         $newmodule->class = $extension->class;
00136         $newmodule->adminclass = $extension->adminclass;
00137         return $newmodule;
00138     }
00139 
00140     private function getVisibleKeys ($position, $isAdmin) {
00141         if (isset($this->visibleKeys[$position][$isAdmin])) return $this->visibleKeys[$position][$isAdmin];
00142         $result = array();
00143         if ($isAdmin) $elements = isset($this->admin_area_links[$position]) ? $this->admin_area_links[$position] : array();
00144         else $elements = isset($this->user_area_links[$position]) ? $this->user_area_links[$position] : array();
00145         $currentmenu = aliroRequest::getInstance()->getItemid();
00146         if (!isset($this->allModulesByMenu[$currentmenu])) $currentmenu = 0;
00147         if (isset($this->allModulesByMenu[$currentmenu])) {
00148             $elements = array_intersect($elements, $this->allModulesByMenu[$currentmenu]);
00149             $authoriser = aliroAuthoriser::getInstance();
00150             foreach ($elements as $element) if ($authoriser->checkUserPermission ('view', 'aliroModule', $element)) $result[] = $element;
00151         }
00152         $this->visibleKeys[$position][$isAdmin] = $result;
00153         return $result;
00154     }
00155 
00156     public function countModules ($position, $isAdmin) {
00157         return count($this->getVisibleKeys ($position, $isAdmin));
00158     }
00159 
00160     public function getModules ($position, $isAdmin) {
00161         $result = array();
00162         $keys = $this->getVisibleKeys ($position, $isAdmin);
00163         foreach ($keys as $key) $result[] = $this->allModules[$this->keyToSubscript[$key]];
00164         return $result;
00165     }
00166 
00167     public function getModuleByID ($id) {
00168         return isset($this->allModules[$this->keyToSubscript[$id]]) ? $this->allModules[$this->keyToSubscript[$id]] : null;
00169     }
00170     
00171     public function getModuleByFormalName ($formalname) {
00172         return isset($this->modulesByFormalName[$formalname]) ? $this->allModules[$this->modulesByFormalName[$formalname]] : null;
00173     }
00174 
00175     public function getSelectedModules ($position='', $formalname='', $search='', $admin=false) {
00176         $results = array();
00177         foreach ($this->allModules as $module) {
00178             if ($admin) {
00179                 if (!($module->admin & 2)) continue;
00180             }
00181             elseif (!($module->admin & 1)) continue;
00182             if ($position AND $module->position != $position) continue;
00183             if ($formalname AND $module->module != $formalname) continue;
00184             if ($search AND strpos(strtolower($module->title), $search) === false) continue;
00185             $results[] = $module;
00186         }
00187         return $results;
00188     }
00189 
00190     public function getModulesByPosition ($admin) {
00191         $results = array();
00192         $check = $admin ? 2 : 1;
00193         foreach ($this->allModules as $module) {
00194             if ($module->admin & $check) $results[$module->position][] = $module;
00195         }
00196         return $results;
00197     }
00198 
00199     public function getMenus ($module_id) {
00200         return isset($this->allMenusByModule[$module_id]) ? $this->allMenusByModule[$module_id] : array();
00201     }
00202 
00203     public function getDistinctNames () {
00204         return $this->distinct_user_side;
00205     }
00206 
00207     public function deleteModules ($ids) {
00208         foreach ($ids as &$id) $id = intval($id);
00209         $idlist = implode (',', $ids);
00210         $database = aliroCoreDatabase::getInstance();
00211         $database->doSQL ("DELETE FROM #__modules WHERE id IN ('$idlist')");
00212         $database->doSQL ("DELETE FROM #__modules_menu WHERE moduleid IN ('$idlist')");
00213         $this->clearCache();
00214     }
00215 
00216     public function publishModules ($ids, $new_publish) {
00217         foreach ($ids as &$id) $id = intval($id);
00218         $new_publish = intval($new_publish);
00219         $idlist = implode (',', $ids);
00220         $database = aliroCoreDatabase::getInstance();
00221         $database->doSQL ("UPDATE #__modules SET published = $new_publish WHERE id IN ($idlist)");
00222         $this->clearCache();
00223     }
00224 
00225     public function changeOrder ($id, $direction) {
00226         $module = $this->allModules[$this->keyToSubscript[$id]];
00227         $movement = 'down' == $direction ? 15 : -15;
00228         $this->updateOrdering (array($id => $module->ordering + $movement));
00229     }
00230 
00231     public function updateOrdering ($orders) {
00232         foreach ($orders as $id=>$order) {
00233             $module =  $this->allModules[$this->keyToSubscript[$id]];
00234             if ($module->ordering != $order) $changes[$id] = $order;
00235         }
00236         foreach ($this->allModules as $module) {
00237             $ordering = isset($changes[$module->id]) ? $changes[$module->id] : $module->ordering;
00238             $allmodules[$module->position][$ordering] = $module->id;
00239         }
00240         $changed = false;
00241         $query = "UPDATE #__modules SET ordering = CASE ";
00242         foreach ($allmodules as $position=>$orderings) {
00243             $order = 10;
00244             ksort($orderings);
00245             foreach ($orderings as $ordering=>$id) {
00246                 $module = $this->allModules[$this->keyToSubscript[$id]];
00247                 if ($order != $module->ordering) {
00248                     $query .= "WHEN id = $id THEN $order ";
00249                     $changed = true;
00250                 }
00251                 $order += 10;
00252             }
00253         }
00254         if ($changed) {
00255             $query .= 'ELSE ordering END';
00256             aliroCoreDatabase::getInstance()->doSQL ($query);
00257             $this->clearCache();
00258         }
00259     }
00260 
00261 }
00262 
00267 class aliroModule extends aliroDatabaseRow {
00268     protected $DBclass = 'aliroCoreDatabase';
00269     protected $tableName = '#__modules';
00270     protected $rowKey = 'id';
00271     protected $handler = 'aliroModuleHandler';
00272     protected $formalfield = 'module';
00273 
00274     // overloaded check function
00275     public function check() {
00276         // check for presence of a name
00277         if (trim( $this->title ) == '') {
00278             $this->_error = T_('Your Module must contain a title.');
00279             return false;
00280         }
00281         return true;
00282     }
00283 
00284     public function getParams () {
00285         $params = new aliroParameters ($this->params);
00286         return $params;
00287     }
00288 
00289     public function loadLanguage () {
00290         // check for custom language file
00291         $basepath = criticalInfo::getInstance()->absolute_path.'/modules/'.$this->module;
00292         $path = $basepath.aliroCore::get('mosConfig_lang').'.php';
00293         if (file_exists( $path )) include( $path );
00294         else {
00295             $path = $basepath.'.en.php';
00296             if (file_exists( $path )) include( $path );
00297         }
00298     }
00299 
00300     public function renderModule ($area, $template) {
00301         $this->loadLanguage();
00302         $params = $this->getParams();
00303         $moduleclass_sfx = $params->get( 'moduleclass_sfx' );
00304         $title = $this->showtitle ? $this->title : '';
00305         $moduleclass = ($this->admin & 2) ? $this->adminclass : $this->class;
00306         $modobject = new $moduleclass;
00307         $modobject->activate($this, $content, $area, $params);
00308         $method = 'moduleStyle'.$area->style;
00309         return $template->$method($moduleclass_sfx, $title, $content);
00310     }
00311 
00312     public function renderModuleTitle ($area, $template) {
00313         $params = $this->getParams();
00314         $moduleclass_sfx = $params->get( 'moduleclass_sfx' );
00315         $title = $this->showtitle ? $this->title : '';
00316         $method = 'moduleStyle'.$area->style;
00317         return $template->$method($moduleclass_sfx, $title, '');
00318     }
00319 
00320 }

Generated on Thu Apr 17 13:03:26 2008 for ALIRO by  doxygen 1.5.5