10 defined(
'JPATH_PLATFORM') or die;
154 public static function addModelPath($path, $prefix =
'')
170 protected static function createFileName($type, $parts = array())
177 if (!empty($parts[
'format']))
179 if ($parts[
'format'] ==
'html')
181 $parts[
'format'] =
'';
185 $parts[
'format'] =
'.' . $parts[
'format'];
190 $parts[
'format'] =
'';
193 $filename = strtolower($parts[
'name'] . $parts[
'format'] .
'.php');
197 if (!empty($parts[
'type']))
199 $parts[
'type'] =
'.' . $parts[
'type'];
206 $filename = strtolower($parts[
'name'] .
'/view' . $parts[
'type'] .
'.php');
224 public static function getInstance($prefix, $config = array())
226 if (is_object(self::$instance))
228 return self::$instance;
234 $basePath = array_key_exists(
'base_path', $config) ? $config[
'base_path'] : JPATH_COMPONENT;
235 $format = $input->getWord(
'format');
236 $command = $input->get(
'task',
'display');
241 if (is_array($command))
243 $command = $filter->clean(array_pop(array_keys($command)),
'cmd');
247 $command = $filter->clean($command,
'cmd');
251 if (strpos($command,
'.') !==
false)
254 list ($type, $task) = explode(
'.', $command);
257 $file = self::createFileName(
'controller', array(
'name' => $type,
'format' => $format));
258 $path = $basePath .
'/controllers/' . $file;
259 $backuppath = $basePath .
'/controller/' . $file;
262 $input->set(
'task', $task);
270 $file = self::createFileName(
'controller', array(
'name' =>
'controller',
'format' => $format));
271 $path = $basePath .
'/' . $file;
272 $backupfile = self::createFileName(
'controller', array(
'name' =>
'controller'));
273 $backuppath = $basePath .
'/' . $backupfile;
277 $class = ucfirst($prefix) .
'Controller' . ucfirst($type);
280 if (!class_exists($class))
283 if (file_exists($path))
287 elseif (isset($backuppath) && file_exists($backuppath))
289 require_once $backuppath;
293 throw new InvalidArgumentException(
JText::sprintf(
'JLIB_APPLICATION_ERROR_INVALID_CONTROLLER', $type, $format));
298 if (class_exists($class))
300 self::$instance =
new $class($config);
304 throw new InvalidArgumentException(
JText::sprintf(
'JLIB_APPLICATION_ERROR_INVALID_CONTROLLER_CLASS', $class));
307 return self::$instance;
319 public function __construct($config = array())
321 $this->methods = array();
322 $this->message = null;
323 $this->messageType =
'message';
324 $this->paths = array();
325 $this->redirect = null;
326 $this->taskMap = array();
328 if (defined(
'JDEBUG') && JDEBUG)
336 $xMethods = get_class_methods(
'JControllerLegacy');
339 $r =
new ReflectionClass($this);
340 $rMethods = $r->getMethods(ReflectionMethod::IS_PUBLIC);
342 foreach ($rMethods as $rMethod)
344 $mName = $rMethod->getName();
347 if (!in_array($mName, $xMethods) || $mName ==
'display')
349 $this->methods[] = strtolower($mName);
352 $this->taskMap[strtolower($mName)] = $mName;
357 if (empty($this->name))
359 if (array_key_exists(
'name', $config))
361 $this->name = $config[
'name'];
365 $this->name = $this->getName();
370 if (array_key_exists(
'base_path', $config))
372 $this->basePath = $config[
'base_path'];
376 $this->basePath = JPATH_COMPONENT;
380 if (array_key_exists(
'default_task', $config))
382 $this->registerDefaultTask($config[
'default_task']);
386 $this->registerDefaultTask(
'display');
390 if (empty($this->model_prefix))
392 if (array_key_exists(
'model_prefix', $config))
395 $this->model_prefix = $config[
'model_prefix'];
399 $this->model_prefix = $this->name .
'Model';
404 if (array_key_exists(
'model_path', $config))
407 $this->addModelPath($config[
'model_path'], $this->model_prefix);
411 $this->addModelPath($this->basePath .
'/models', $this->model_prefix);
415 if (array_key_exists(
'view_path', $config))
418 $this->setPath(
'view', $config[
'view_path']);
422 $this->setPath(
'view', $this->basePath .
'/views');
426 if (array_key_exists(
'default_view', $config))
428 $this->default_view = $config[
'default_view'];
430 elseif (empty($this->default_view))
432 $this->default_view = $this->getName();
448 protected function addPath($type, $path)
451 settype($path,
'array');
453 if (!isset($this->paths[$type]))
455 $this->paths[$type] = array();
459 foreach ($path as $dir)
465 array_unshift($this->paths[$type], $dir);
478 public function addViewPath($path)
480 $this->addPath(
'view', $path);
495 public function authorise($task)
512 protected function checkEditId($context, $id)
517 $values = (array) $app->getUserState($context .
'.id');
519 $result = in_array((
int) $id, $values);
521 if (defined(
'JDEBUG') && JDEBUG)
525 'Checking edit ID %s.%s: %d %s',
529 str_replace(
"\n",
' ', print_r($values, 1))
557 protected function createModel($name, $prefix =
'', $config = array())
560 $modelName = preg_replace(
'/[^A-Z0-9_]/i',
'', $name);
561 $classPrefix = preg_replace(
'/[^A-Z0-9_]/i',
'', $prefix);
587 protected function createView($name, $prefix =
'', $type =
'', $config = array())
590 $viewName = preg_replace(
'/[^A-Z0-9_]/i',
'', $name);
591 $classPrefix = preg_replace(
'/[^A-Z0-9_]/i',
'', $prefix);
592 $viewType = preg_replace(
'/[^A-Z0-9_]/i',
'', $type);
595 $viewClass = $classPrefix . $viewName;
597 if (!class_exists($viewClass))
599 jimport(
'joomla.filesystem.path');
600 $path =
JPath::find($this->paths[
'view'], $this->createFileName(
'view', array(
'name' => $viewName,
'type' => $viewType)));
606 if (!class_exists($viewClass))
608 throw new Exception(
JText::sprintf(
'JLIB_APPLICATION_ERROR_VIEW_CLASS_NOT_FOUND', $viewClass, $path), 500);
617 return new $viewClass($config);
633 public function display($cachable =
false, $urlparams = array())
636 $viewType = $document->getType();
637 $viewName = $this->input->get(
'view', $this->default_view);
638 $viewLayout = $this->input->get(
'layout',
'default');
640 $view = $this->getView($viewName, $viewType,
'', array(
'base_path' => $this->basePath,
'layout' => $viewLayout));
643 if ($model = $this->getModel($viewName))
646 $view->setModel($model,
true);
649 $view->document = $document;
654 if ($cachable && $viewType !=
'feed' && $conf->get(
'caching') >= 1)
656 $option = $this->input->get(
'option');
659 if (is_array($urlparams))
663 if (!empty($app->registeredurlparams))
665 $registeredurlparams = $app->registeredurlparams;
669 $registeredurlparams =
new stdClass;
672 foreach ($urlparams as $key => $value)
675 $registeredurlparams->$key = $value;
678 $app->registeredurlparams = $registeredurlparams;
681 $cache->get($view,
'display');
701 public function execute($task)
705 $task = strtolower($task);
706 if (isset($this->taskMap[$task]))
708 $doTask = $this->taskMap[$task];
710 elseif (isset($this->taskMap[
'__default']))
712 $doTask = $this->taskMap[
'__default'];
716 throw new Exception(
JText::sprintf(
'JLIB_APPLICATION_ERROR_TASK_NOT_FOUND', $task), 404);
720 $this->doTask = $doTask;
722 return $this->$doTask();
736 public function getModel($name =
'', $prefix =
'', $config = array())
740 $name = $this->getName();
745 $prefix = $this->model_prefix;
748 if ($model = $this->createModel($name, $prefix, $config))
751 $model->setState(
'task', $this->task);
755 $menu = $app->getMenu();
757 if (is_object($menu))
759 if ($item = $menu->getActive())
761 $params = $menu->getParams($item->id);
764 $model->setState(
'parameters.menu', $params);
782 public function getName()
784 if (empty($this->name))
787 if (!preg_match(
'/(.*)Controller/i', get_class($this), $r))
789 throw new Exception(
JText::_(
'JLIB_APPLICATION_ERROR_CONTROLLER_GET_NAME'), 500);
791 $this->name = strtolower($r[1]);
804 public function getTask()
816 public function getTasks()
818 return $this->methods;
834 public function getView($name =
'', $type =
'', $prefix =
'', $config = array())
845 $name = $this->getName();
850 $prefix = $this->getName() .
'View';
853 if (empty($views[$name]))
855 if ($view = $this->createView($name, $prefix, $type, $config))
857 $views[$name] = & $view;
861 throw new Exception(
JText::sprintf(
'JLIB_APPLICATION_ERROR_VIEW_NOT_FOUND', $name, $type, $prefix), 500);
865 return $views[$name];
878 protected function holdEditId($context, $id)
881 $values = (array) $app->getUserState($context .
'.id');
886 array_push($values, (
int) $id);
887 $values = array_unique($values);
888 $app->setUserState($context .
'.id', $values);
890 if (defined(
'JDEBUG') && JDEBUG)
894 'Holding edit ID %s.%s %s',
897 str_replace(
"\n",
' ', print_r($values, 1))
913 public function redirect()
920 $app->enqueueMessage($this->message, $this->messageType);
923 $app->redirect($this->redirect);
938 public function registerDefaultTask($method)
940 $this->registerTask(
'__default', $method);
955 public function registerTask($task, $method)
957 if (in_array(strtolower($method), $this->methods))
959 $this->taskMap[strtolower($task)] = $method;
974 public function unregisterTask($task)
976 unset($this->taskMap[strtolower($task)]);
991 protected function releaseEditId($context, $id)
994 $values = (array) $app->getUserState($context .
'.id');
997 $index = array_search((
int) $id, $values,
true);
1001 unset($values[$index]);
1002 $app->setUserState($context .
'.id', $values);
1004 if (defined(
'JDEBUG') && JDEBUG)
1008 'Releasing edit ID %s.%s %s',
1011 str_replace(
"\n",
' ', print_r($values, 1))
1030 public function setMessage($text, $type =
'message')
1032 $previous = $this->message;
1033 $this->message = $text;
1034 $this->messageType = $type;
1050 protected function setPath($type, $path)
1053 $this->paths[$type] = array();
1056 $this->addPath($type, $path);
1070 public function setRedirect($url, $msg = null, $type = null)
1072 $this->redirect = $url;
1076 $this->message = $msg;
1082 if (empty($this->messageType))
1084 $this->messageType =
'message';
1090 $this->messageType = $type;