00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
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
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.'&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
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
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 ?>