aliroComponentAdminManager.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  * These classes provide basic management functions for the admin side of components
00038  *
00039  */
00040 
00046 class aliroComponentAdminManager extends aliroComponentManager {
00047     public $act = '';
00048     public $task = '';
00049     public $cid = 0;
00050     public $currid = 0;
00051     protected $name = '';
00052     protected $controller = null;
00053 
00054     public function __construct ($component, $system, $version) {
00055         parent::__construct($component, $system, $version);
00056         $this->act = $this->getParam ($_REQUEST, 'act', $this->barename);
00057         $this->task = $this->getParam($_REQUEST, 'task');
00058         // Problem - e.g. change number of items per page, null task returned - so no good using getParam default alone
00059         $this->cid = $this->getParam($_REQUEST, 'cid', array(0));
00060         if (is_array( $this->cid )) {
00061             foreach ($this->cid as &$value) $value = intval($value);
00062             $this->currid=$this->cid[0];
00063         }
00064         else $this->currid = intval($this->cid);
00065         $this->name = $this->getAction();
00066         if (class_exists($this->name)) {
00067             if (!$this->task) {
00068                 $this->task = $this->getParam($_REQUEST, 'toolbarbutton');
00069                 $this->task = $this->unTranslateTask($this->task);
00070             }
00071             if (!$this->task) $this->task = 'list';
00072             $this->controller = call_user_func(array($this->name, 'getInstance'), $this);
00073         }
00074         else trigger_error(sprintf(T_('Aliro error in %s: class not found %s'), $this->formalname, $this->name));
00075     }
00076 
00077     protected function unTranslateTask ($translated) {
00078         if (method_exists($this->name, 'taskTranslator')) {
00079             $translator = call_user_func(array($this->name, 'taskTranslator'));
00080             $result = array_search ($translated, $translator);
00081             return $result ? $result : $translated;
00082         }
00083         return $translated;
00084     }
00085     
00086     private function getAction () {
00087         $actname = strtoupper(substr($this->act,0,1)).strtolower(substr($this->act,1));
00088         return strtolower($this->barename).'Admin'.$actname;
00089     }
00090 
00091     public function activate () {
00092         if (empty($this->controller->ignoreMagicQuotes)) $this->noMagicQuotes();
00093         $task = $this->task.'Task';
00094         if (method_exists($this->controller, 'getRequestData')) $this->controller->getRequestData();
00095         if (method_exists($this->controller, 'checkPermission')) {
00096             if (!$this->controller->checkPermission()) {
00097                 $this->redirect('index.php', T_('You are not authorized to view this resource.'), _ALIRO_ERROR_FATAL);
00098             }
00099         }
00100         if (method_exists($this->controller,$task)) {
00101             $this->controller->$task();
00102         }
00103         else trigger_error(sprintf(T_('Aliro error in %s: method %s not found in class %s'), $this->formalname, $task, $this->name));
00104     }
00105 
00106     public function toolbar () {
00107         if (method_exists($this->controller,'toolbar')) $this->controller->toolbar();
00108         else trigger_error(sprintf(T_('Aliro error in %s: method %s not found in class %s'), $this->formalname, 'toolbar', $this->name));
00109     }
00110 
00111     public function check_selection ($text) {
00112         if (!is_array($this->cid) OR count( $this->cid ) < 1) {
00113             $this->setErrorMessage($text);
00114             return false;
00115         }
00116         return true;
00117     }
00118 
00119 }
00124 abstract class aliroComponentAdminControllers extends aliroComponentControllers {
00125     public $optionurl = '';
00126     public $fulloptionurl = '';
00127     public $act = '';
00128     public $task = '';
00129     protected $cid = array(0);
00130     protected $translator = array();
00131 
00132     protected function __construct ($manager) {
00133         parent::__construct($manager);
00134         if (!$this->option) {
00135             $this->option = $this->getParam ($_REQUEST, 'core');
00136             $this->optionurl = 'index.php?core=';
00137         }
00138         else $this->optionurl = 'index.php?option=';
00139         $this->act = $manager->act;
00140         $this->task = $manager->task;
00141         $this->cid = $manager->cid;
00142         $this->optionurl .= $this->option.'&amp;act='.$this->act;
00143         $this->fulloptionurl = $this->getCfg('admin_site').'/'.$this->optionurl;
00144         $classname = get_class($this);
00145         if (method_exists($classname, 'taskTranslator')) {
00146             $this->translator = call_user_func(array($classname, 'taskTranslator'));
00147         }
00148     }
00149 
00150     protected function __clone () {
00151         // Protected to enforce singleton
00152     }
00153 
00154     protected function checkExclusion ($task, $showError=true) {
00155         if (isset($this->function_exclude) AND in_array($task, $this->function_exclude)) {
00156             if ($showError) $this->setErrorMessage(T_('Invalid operation attempted'), _ALIRO_ERROR_FATAL);
00157             return true;
00158         }
00159         return false;
00160     }
00161 
00162     // This is the basic default - may be overridden
00163     public function toolbar () {
00164         $toolbar = aliroAdminToolbar::getInstance();
00165         switch ($this->task) {
00166             case 'new':
00167             case 'edit':
00168                 $toolbar->save();
00169                 $toolbar->apply();
00170                 $toolbar->cancel();
00171                 break;
00172 
00173             case 'list':
00174             default:
00175                 if (!$this->checkExclusion('new', false)) {
00176                     $toolbar->addNew();
00177                 }
00178                 if (!$this->checkExclusion('remove', false)) {
00179                     $toolbar->deleteList();
00180                 }
00181                 if (!$this->checkExclusion('edit', false)) $toolbar->editList();
00182                 break;
00183         }
00184     }
00185 
00186     protected function toolBarButton ($task, $requireSelect=false) {
00187         $template = $this->getTemplateObject();
00188         $translated = isset($this->translator[$task]) ? $this->translator[$task] : $task;
00189         $template->toolBarButton($translated, $requireSelect);
00190     }
00191 
00192     protected function basicInsert ($tablename) {
00193         $database = aliroDatabase::getInstance();
00194         $query = "INSERT INTO $tablename (";
00195         $fields = $this->getTableInfo($tablename);
00196         foreach ($fields as $field) {
00197             $fieldname = $field->Field;
00198             if ($value = $this->handleField($fieldname, $field->Type)) {
00199                 $fieldset[] = "`$fieldname`";
00200                 $valueset[] = "'$value'";
00201             }
00202         }
00203         if (isset($fieldset)) {
00204             $query .= implode(',', $fieldset).') VALUES ('.implode(',', $valueset).')';
00205             $database->doSQL($query);
00206             $newid = $database->insertid();
00207         }
00208         else $newid = 0;
00209         return $newid;
00210     }
00211 
00212     protected function basicUpdate ($tablename, $keyname, $id) {
00213         $database = aliroDatabase::getInstance();
00214         $query = "UPDATE $tablename SET ";
00215         $fields = $this->getTableInfo($tablename);
00216         foreach ($fields as $field) {
00217             $fieldname = $field->Field;
00218             if ($fieldname == $keyname) continue;
00219             $value = $this->handleField($fieldname, $field->Type);
00220             $setters[] = "`$fieldname` = '$value'";
00221         }
00222         if (isset($setters)) {
00223             $query .= implode (',', $setters)." WHERE `$keyname` = $id";
00224             $database->doSQL($query);
00225         }
00226     }
00227 
00228     private function handleField ($fieldname, $type) {
00229         $fieldname[0] = strtoupper($fieldname[0]);
00230         if (false === strpos($type, 'text')) $mask = 0;
00231         else $mask = _MOS_ALLOWHTML;
00232         $value = $this->getParam($_POST, $fieldname, null, $mask);
00233         $database = aliroDatabase::getInstance();
00234         $value = $database->getEscaped($value);
00235         $method = 'validate'.$fieldname;
00236         if (method_exists($this, $method)) $this->$method($value);
00237         return $value;
00238     }
00239 
00240 }
00241 
00242 ?>

Generated on Wed May 14 13:01:55 2008 for ALIRO by  doxygen 1.5.5