Source for file legacy.php
Documentation is available at legacy.php
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Base class for a Joomla Controller
* Controller (Controllers are where you put all the actual code.) Provides basic
* functionality, such as rendering views (aka displaying templates).
* The base path of the controller
* @note Replaces _basePath.
* The default view for the display method.
* The mapped task that was performed.
* @note Replaces _doTask.
* @note Replaces _message.
* @note Replaces _messageType.
* @note Replaces _methods.
* The name of the controller
* The prefix of the models
* The set of search directories for resources (views).
* @note Replaces _redirect.
* Current or most recently performed task.
* Array of class methods to call for a given task.
* @note Replaces _taskMap.
* Hold a JInput object for easier access to the input variables.
protected static $instance;
* Adds to the stack of model paths in LIFO order.
* @param mixed $path The directory (string), or list of directories (array) to add.
* @param string $prefix A prefix for models
* Create the filename for a resource.
* @param string $type The resource type to create the filename for.
* @param array $parts An associative array of filename information. Optional.
* @return string The filename.
* @note Replaced _createFileName.
if (!empty($parts['format']))
if ($parts['format'] ==
'html')
$parts['format'] =
'.' .
$parts['format'];
$filename =
strtolower($parts['name'] .
$parts['format'] .
'.php');
if (!empty($parts['type']))
$parts['type'] =
'.' .
$parts['type'];
$filename =
strtolower($parts['name'] .
'/view' .
$parts['type'] .
'.php');
* Method to get a singleton controller instance.
* @param string $prefix The prefix for the controller.
* @param array $config An array of optional constructor options.
* @return JControllerLegacy
* @throws Exception if the controller cannot be loaded.
public static function getInstance($prefix, $config =
array())
$input =
JFactory::getApplication()->input;
// Get the environment configuration.
$basePath =
array_key_exists('base_path', $config) ?
$config['base_path'] :
JPATH_COMPONENT;
$format =
$input->getWord('format');
$command =
$input->get('task', 'display');
// Check for array format.
$command =
$filter->clean($command, 'cmd');
// Check for a controller.task command.
if (strpos($command, '.') !==
false)
// Explode the controller.task command.
list
($type, $task) =
explode('.', $command);
// Define the controller filename and path.
$file =
self::createFileName('controller', array('name' =>
$type, 'format' =>
$format));
$path =
$basePath .
'/controllers/' .
$file;
$backuppath =
$basePath .
'/controller/' .
$file;
// Reset the task without the controller context.
$input->set('task', $task);
// Define the controller filename and path.
$file =
self::createFileName('controller', array('name' =>
'controller', 'format' =>
$format));
$path =
$basePath .
'/' .
$file;
$backupfile =
self::createFileName('controller', array('name' =>
'controller'));
$backuppath =
$basePath .
'/' .
$backupfile;
// Get the controller class name.
// Include the class if not present.
// If the controller file path exists, include it.
elseif (isset
($backuppath) &&
file_exists($backuppath))
require_once $backuppath;
throw
new InvalidArgumentException(JText::sprintf('JLIB_APPLICATION_ERROR_INVALID_CONTROLLER', $type, $format));
// Instantiate the class.
self::$instance =
new $class($config);
throw
new InvalidArgumentException(JText::sprintf('JLIB_APPLICATION_ERROR_INVALID_CONTROLLER_CLASS', $class));
* @param array $config An optional associative array of configuration settings.
* Recognized key values include 'name', 'default_task', 'model_path', and
* 'view_path' (this list is not meant to be comprehensive).
JLog::addLogger(array('text_file' =>
'jcontroller.log.php'), JLog::ALL, array('controller'));
// Determine the methods to exclude from the base class.
// Get the public methods in this class using reflection.
$r =
new ReflectionClass($this);
$rMethods =
$r->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($rMethods as $rMethod)
$mName =
$rMethod->getName();
// Add default display method if not explicitly declared.
if (!in_array($mName, $xMethods) ||
$mName ==
'display')
// Auto register the methods as tasks.
$this->name =
$config['name'];
// Set a base path for use by the controller
// If the default task is set, register it as such
// Set the default model search path
// Set the default view search path
$this->setPath('view', $config['view_path']);
* Adds to the search path for templates and resources.
* @param string $type The path type (e.g. 'model', 'view').
* @param mixed $path The directory string or stream array to search.
* @return JControllerLegacy A JControllerLegacy object to support chaining.
* @note Replaces _addPath.
protected function addPath($type, $path)
// Just force path to array
if (!isset
($this->paths[$type]))
$this->paths[$type] =
array();
// Loop through the path directories
// No surrounding spaces allowed!
// Add to the top of the search dirs
* Add one or more view paths to the controller's stack, in LIFO order.
* @param mixed $path The directory (string) or list of directories (array) to add.
* @return JControllerLegacy This object to support chaining.
* @param string $task The ACO Section Value to check access on.
* @return boolean True if authorised
* @deprecated 13.3 Use JAccess instead.
JLog::add(__METHOD__ .
' is deprecated. Use JAccess instead.', JLog::WARNING, 'deprecated');
* Method to check whether an ID is in the edit list.
* @param string $context The context for the session storage.
* @param integer $id The ID of the record to add to the edit list.
* @return boolean True if the ID is in the edit list.
$values = (array)
$app->getUserState($context .
'.id');
'Checking edit ID %s.%s: %d %s',
* Method to load and return a model object.
* @param string $name The name of the model.
* @param string $prefix Optional model prefix.
* @param array $config Configuration array for the model. Optional.
* @return mixed Model object on success; otherwise null failure.
* @note Replaces _createModel.
protected function createModel($name, $prefix =
'', $config =
array())
* Method to load and return a view object. This method first looks in the
* current template directory for a match and, failing that, uses a default
* set path to load the view class file.
* Note the "name, prefix, type" order of parameters, which differs from the
* "name, type, prefix" order used in related public methods.
* @param string $name The name of the view.
* @param string $prefix Optional prefix for the view class name.
* @param string $type The type of view.
* @param array $config Configuration array for the view. Optional.
* @return mixed View object on success; null or error result on failure.
* @note Replaces _createView.
protected function createView($name, $prefix =
'', $type =
'', $config =
array())
// Build the view class name
$viewClass =
$classPrefix .
$viewName;
throw
new Exception(JText::sprintf('JLIB_APPLICATION_ERROR_VIEW_CLASS_NOT_FOUND', $viewClass, $path), 500);
return new $viewClass($config);
* Typical view method for MVC based architecture
* This function is provide as a default implementation, in most cases
* you will need to override it in your own controllers.
* @param boolean $cachable If true, the view output will be cached
* @param array $urlparams An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
* @return JControllerLegacy A JControllerLegacy object to support chaining.
public function display($cachable =
false, $urlparams =
array())
$viewType =
$document->getType();
$viewLayout =
$this->input->get('layout', 'default');
$view =
$this->getView($viewName, $viewType, '', array('base_path' =>
$this->basePath, 'layout' =>
$viewLayout));
if ($model =
$this->getModel($viewName))
// Push the model into the view (as default)
$view->setModel($model, true);
$view->document =
$document;
if ($cachable &&
$viewType !=
'feed' &&
$conf->get('caching') >=
1)
$option =
$this->input->get('option');
if (!empty($app->registeredurlparams))
$registeredurlparams =
$app->registeredurlparams;
$registeredurlparams =
new stdClass;
foreach ($urlparams as $key =>
$value)
// Add your safe url parameters with variable type as value {@see JFilterInput::clean()}.
$registeredurlparams->$key =
$value;
$app->registeredurlparams =
$registeredurlparams;
$cache->get($view, 'display');
* Execute a task by triggering a method in the derived class.
* @param string $task The task to perform. If no matching task is found, the '__default' task is executed, if defined.
* @return mixed The value returned by the called method, false in error case.
elseif (isset
($this->taskMap['__default']))
$doTask =
$this->taskMap['__default'];
throw
new Exception(JText::sprintf('JLIB_APPLICATION_ERROR_TASK_NOT_FOUND', $task), 404);
// Record the actual task being fired
* Method to get a model object, loading it if required.
* @param string $name The model name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for model. Optional.
* @return object The model.
public function getModel($name =
'', $prefix =
'', $config =
array())
if ($model =
$this->createModel($name, $prefix, $config))
// Task is a reserved state
$model->setState('task', $this->task);
// Let's get the application object and set menu information if it's available
if ($item =
$menu->getActive())
$params =
$menu->getParams($item->id);
// Set default state data
$model->setState('parameters.menu', $params);
* Method to get the controller name
* The dispatcher name is set by default parsed using the classname, or it can be set
* by passing a $config['name'] in the class constructor
* @return string The name of the dispatcher
throw
new Exception(JText::_('JLIB_APPLICATION_ERROR_CONTROLLER_GET_NAME'), 500);
* Get the last task that is being performed or was most recently performed.
* @return string The task that is being performed or was most recently performed.
* Gets the available tasks in the controller.
* @return array Array[i] of task names.
* Method to get a reference to the current view and load it if necessary.
* @param string $name The view name. Optional, defaults to the controller name.
* @param string $type The view type. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for view. Optional.
* @return JViewLegacy Reference to the view or an error.
public function getView($name =
'', $type =
'', $prefix =
'', $config =
array())
$prefix =
$this->getName() .
'View';
if (empty($views[$name]))
if ($view =
$this->createView($name, $prefix, $type, $config))
throw
new Exception(JText::sprintf('JLIB_APPLICATION_ERROR_VIEW_NOT_FOUND', $name, $type, $prefix), 500);
* Method to add a record ID to the edit list.
* @param string $context The context for the session storage.
* @param integer $id The ID of the record to add to the edit list.
$values = (array)
$app->getUserState($context .
'.id');
// Add the id to the list if non-zero.
$app->setUserState($context .
'.id', $values);
'Holding edit ID %s.%s %s',
* Redirects the browser or returns false if no redirect is set.
* @return boolean False if no redirect exists.
// Enqueue the redirect message
* Register the default task to perform if a mapping is not found.
* @param string $method The name of the method in the derived class to perform if a named task is not found.
* @return JControllerLegacy A JControllerLegacy object to support chaining.
* Register (map) a task to a method in the class.
* @param string $task The task.
* @param string $method The name of the method in the derived class to perform for this task.
* @return JControllerLegacy A JControllerLegacy object to support chaining.
* Unregister (unmap) a task in the class.
* @param string $task The task.
* @return JControllerLegacy This object to support chaining.
* Method to check whether an ID is in the edit list.
* @param string $context The context for the session storage.
* @param integer $id The ID of the record to add to the edit list.
$values = (array)
$app->getUserState($context .
'.id');
// Do a strict search of the edit list values.
$app->setUserState($context .
'.id', $values);
'Releasing edit ID %s.%s %s',
* Sets the internal message that is passed with a redirect
* @param string $text Message to display on redirect.
* @param string $type Message type. Optional, defaults to 'message'.
* @return string Previous message
public function setMessage($text, $type =
'message')
* Sets an entire array of search paths for resources.
* @param string $type The type of path to set, typically 'view' or 'model'.
* @param string $path The new set of search paths. If null or false, resets to the current directory only.
* @note Replaces _setPath.
protected function setPath($type, $path)
// Clear out the prior search dirs
$this->paths[$type] =
array();
// Actually add the user-specified directories
* Set a URL for browser redirection.
* @param string $url URL to redirect to.
* @param string $msg Message to display on redirect. Optional, defaults to value set internally by controller, if any.
* @param string $type Message type. Optional, defaults to 'message' or the type set by a previous call to setMessage.
* @return JControllerLegacy This object to support chaining.
public function setRedirect($url, $msg =
null, $type =
null)
// Controller may have set this directly
// Ensure the type is not overwritten by a previous call to setMessage.
// If the type is explicitly set, set it.
Documentation generated on Tue, 19 Nov 2013 15:06:47 +0100 by phpDocumentor 1.4.3