PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/application/component/controller.php

https://bitbucket.org/joomla/joomla-platform/
PHP | 992 lines | 459 code | 129 blank | 404 comment | 83 complexity | c622efdb5c93be447b2e78070e27b26d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Application
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Base class for a Joomla Controller
  12. *
  13. * Controller (Controllers are where you put all the actual code.) Provides basic
  14. * functionality, such as rendering views (aka displaying templates).
  15. *
  16. * @abstract
  17. * @package Joomla.Platform
  18. * @subpackage Application
  19. * @since 11.1
  20. */
  21. class JController extends JObject
  22. {
  23. /**
  24. * ACO Section for the controller.
  25. *
  26. * @var string
  27. * @deprecated 1.6 - Apr 5, 2010
  28. */
  29. protected $_acoSection;
  30. /**
  31. * Default ACO Section value for the controller.
  32. *
  33. * @var string
  34. * @deprecated 1.6 - Apr 5, 2010
  35. */
  36. protected $_acoSectionValue;
  37. /**
  38. * The base path of the controller
  39. *
  40. * @var string
  41. * @since 11.1 Replaces _basePath.
  42. */
  43. protected $basePath;
  44. /**
  45. * @var string The default view for the display method.
  46. * @since 11.1
  47. */
  48. protected $default_view;
  49. /**
  50. * The mapped task that was performed.
  51. *
  52. * @var string
  53. * @since 11.1 Replaces _doTask.
  54. */
  55. protected $doTask;
  56. /**
  57. * Redirect message.
  58. *
  59. * @var string
  60. * @since 11.1 Replaces _message.
  61. */
  62. protected $message;
  63. /**
  64. * Redirect message type.
  65. *
  66. * @var string
  67. * @since 11.1 Replaces _messageType.
  68. */
  69. protected $messageType;
  70. /**
  71. * Array of class methods
  72. *
  73. * @var array
  74. * @since 11.1 Replaces _methods.
  75. */
  76. protected $methods;
  77. /**
  78. * The name of the controller
  79. *
  80. * @var array
  81. * @since 11.1 Replaces _name.
  82. */
  83. protected $name;
  84. /**
  85. * The prefix of the models
  86. *
  87. * @var string
  88. * @since 11.1
  89. */
  90. protected $model_prefix;
  91. /**
  92. * The set of search directories for resources (views).
  93. *
  94. * @var array
  95. * @since 11.1 Replaces _path.
  96. */
  97. protected $paths;
  98. /**
  99. * URL for redirection.
  100. *
  101. * @var string
  102. * @since 11.1 Replaces _redirect.
  103. */
  104. protected $redirect;
  105. /**
  106. * Current or most recently performed task.
  107. *
  108. * @var string
  109. * @since 11.1 Replaces _task.
  110. */
  111. protected $task;
  112. /**
  113. * Array of class methods to call for a given task.
  114. *
  115. * @var array
  116. * @since 11.1 Replaces _taskMap.
  117. */
  118. protected $taskMap;
  119. /**
  120. * Adds to the stack of model paths in LIFO order.
  121. *
  122. * @param mixed $path The directory (string), or list of directories (array) to add.
  123. * @param string $prefix A prefix for models
  124. *
  125. * @return void
  126. */
  127. public static function addModelPath($path, $prefix='')
  128. {
  129. jimport('joomla.application.component.model');
  130. JModel::addIncludePath($path, $prefix);
  131. }
  132. /**
  133. * Create the filename for a resource.
  134. *
  135. * @param string $type The resource type to create the filename for.
  136. * @param array $parts An associative array of filename information. Optional.
  137. *
  138. * @return string The filename.
  139. * @since 11.1 Replaced _createFileName.
  140. */
  141. protected static function createFileName($type, $parts = array())
  142. {
  143. $filename = '';
  144. switch ($type) {
  145. case 'controller':
  146. if (!empty($parts['format'])) {
  147. if ($parts['format'] == 'html') {
  148. $parts['format'] = '';
  149. } else {
  150. $parts['format'] = '.'.$parts['format'];
  151. }
  152. } else {
  153. $parts['format'] = '';
  154. }
  155. $filename = strtolower($parts['name']).$parts['format'].'.php';
  156. break;
  157. case 'view':
  158. if (!empty($parts['type'])) {
  159. $parts['type'] = '.'.$parts['type'];
  160. }
  161. $filename = strtolower($parts['name']).'/view'.$parts['type'].'.php';
  162. break;
  163. }
  164. return $filename;
  165. }
  166. /**
  167. * Method to get a singleton controller instance.
  168. *
  169. * @param string $prefix The prefix for the controller.
  170. * @param array $config An array of optional constructor options.
  171. *
  172. * @return mixed JController derivative class or JException on error.
  173. * @since 11.1
  174. */
  175. public static function getInstance($prefix, $config = array())
  176. {
  177. static $instance;
  178. if (!empty($instance)) {
  179. return $instance;
  180. }
  181. // Get the environment configuration.
  182. $basePath = array_key_exists('base_path', $config) ? $config['base_path'] : JPATH_COMPONENT;
  183. $format = JRequest::getWord('format');
  184. $command = JRequest::getVar('task', 'display');
  185. // Check for array format.
  186. $filter = JFilterInput::getInstance();
  187. if (is_array($command)) {
  188. $command = $filter->clean(array_pop(array_keys($command)), 'cmd');
  189. }
  190. else {
  191. $command = $filter->clean($command, 'cmd');
  192. }
  193. // Check for a controller.task command.
  194. if (strpos($command, '.') !== false) {
  195. // Explode the controller.task command.
  196. list($type, $task) = explode('.', $command);
  197. // Define the controller filename and path.
  198. $file = self::createFileName('controller', array('name' => $type, 'format' => $format));
  199. $path = $basePath.'/controllers/'.$file;
  200. // Reset the task without the contoller context.
  201. JRequest::setVar('task', $task);
  202. }
  203. else {
  204. // Base controller.
  205. $type = null;
  206. $task = $command;
  207. // Define the controller filename and path.
  208. $file = self::createFileName('controller', array('name' => 'controller'));
  209. $path = $basePath.'/'.$file;
  210. }
  211. // Get the controller class name.
  212. $class = ucfirst($prefix).'Controller'.ucfirst($type);
  213. // Include the class if not present.
  214. if (!class_exists($class)) {
  215. // If the controller file path exists, include it.
  216. if (file_exists($path)) {
  217. require_once $path;
  218. }
  219. else {
  220. throw new JException(JText::sprintf('JLIB_APPLICATION_ERROR_INVALID_CONTROLLER', $type, $format), 1056, E_ERROR, $type, true);
  221. }
  222. }
  223. // Instantiate the class.
  224. if (class_exists($class)) {
  225. $instance = new $class($config);
  226. }
  227. else {
  228. throw new JException(JText::sprintf('JLIB_APPLICATION_ERROR_INVALID_CONTROLLER_CLASS', $class), 1057, E_ERROR, $class, true);
  229. }
  230. return $instance;
  231. }
  232. /**
  233. * Constructor.
  234. *
  235. * @param array $config An optional associative array of configuration settings.
  236. * Recognized key values include 'name', 'default_task', 'model_path', and
  237. * 'view_path' (this list is not meant to be comprehensive).
  238. *
  239. * @return JController
  240. * @since 11.1
  241. */
  242. public function __construct($config = array())
  243. {
  244. // Initialise variables.
  245. $this->methods = array();
  246. $this->message = null;
  247. $this->messageType = 'message';
  248. $this->paths = array();
  249. $this->redirect = null;
  250. $this->taskMap = array();
  251. // Determine the methods to exclude from the base class.
  252. $xMethods = get_class_methods('JController');
  253. // Get the public methods in this class using reflection.
  254. $r = new ReflectionClass($this);
  255. $rName = $r->getName();
  256. $rMethods = $r->getMethods(ReflectionMethod::IS_PUBLIC);
  257. $methods = array();
  258. foreach ($rMethods as $rMethod)
  259. {
  260. $mName = $rMethod->getName();
  261. // Add default display method if not explicitly declared.
  262. if (!in_array($mName, $xMethods) || $mName == 'display') {
  263. $this->methods[] = strtolower($mName);
  264. // Auto register the methods as tasks.
  265. $this->taskMap[strtolower($mName)] = $mName;
  266. }
  267. }
  268. //set the view name
  269. if (empty($this->name)) {
  270. if (array_key_exists('name', $config)) {
  271. $this->name = $config['name'];
  272. }
  273. else {
  274. $this->name = $this->getName();
  275. }
  276. }
  277. // Set a base path for use by the controller
  278. if (array_key_exists('base_path', $config)) {
  279. $this->basePath = $config['base_path'];
  280. }
  281. else {
  282. $this->basePath = JPATH_COMPONENT;
  283. }
  284. // If the default task is set, register it as such
  285. if (array_key_exists('default_task', $config)) {
  286. $this->registerDefaultTask($config['default_task']);
  287. }
  288. else {
  289. $this->registerDefaultTask('display');
  290. }
  291. // Set the models prefix
  292. if (empty($this->model_prefix)) {
  293. if (array_key_exists('model_prefix', $config)) {
  294. // User-defined prefix
  295. $this->model_prefix = $config['model_prefix'];
  296. }
  297. else {
  298. $this->model_prefix = $this->name . 'Model';
  299. }
  300. }
  301. // Set the default model search path
  302. if (array_key_exists('model_path', $config)) {
  303. // user-defined dirs
  304. $this->addModelPath($config['model_path'], $this->model_prefix);
  305. }
  306. else {
  307. $this->addModelPath($this->basePath.'/models', $this->model_prefix);
  308. }
  309. // Set the default view search path
  310. if (array_key_exists('view_path', $config)) {
  311. // User-defined dirs
  312. $this->setPath('view', $config['view_path']);
  313. }
  314. else {
  315. $this->setPath('view', $this->basePath.'/views');
  316. }
  317. // Set the default view.
  318. if (array_key_exists('default_view', $config)) {
  319. $this->default_view = $config['default_view'];
  320. }
  321. else if (empty($this->default_view)) {
  322. $this->default_view = $this->getName();
  323. }
  324. }
  325. /**
  326. * Adds to the search path for templates and resources.
  327. *
  328. * @param string $type The path type (e.g. 'model', 'view').
  329. * @param string|array $path The directory or stream to search.
  330. *
  331. * @return JController This object to support chaining.
  332. * @since 11.1 Replaces _addPath.
  333. */
  334. protected function addPath($type, $path)
  335. {
  336. // Just force path to array
  337. settype($path, 'array');
  338. if (!isset($this->paths[$type])) {
  339. $this->paths[$type] = array();
  340. }
  341. // Loop through the path directories
  342. foreach ($path as $dir)
  343. {
  344. // No surrounding spaces allowed!
  345. $dir = rtrim(JPath::check($dir, '/'), '/').'/';
  346. // Add to the top of the search dirs
  347. array_unshift($this->paths[$type], $dir);
  348. }
  349. return $this;
  350. }
  351. /**
  352. * Add one or more view paths to the controller's stack, in LIFO order.
  353. *
  354. * @param string|array $path The directory (string) or list of directories (array) to add.
  355. *
  356. * @return JController This object to support chaining.
  357. */
  358. public function addViewPath($path)
  359. {
  360. $this->addPath('view', $path);
  361. return $this;
  362. }
  363. /**
  364. * Authorisation check
  365. *
  366. * @param string $task The ACO Section Value to check access on
  367. *
  368. * @return boolean True if authorised
  369. * @since 11.1
  370. * @deprecated 1.6 - Apr 5, 2010
  371. */
  372. public function authorize($task)
  373. {
  374. $this->authorise($task);
  375. }
  376. /**
  377. * Authorisation check
  378. *
  379. * @param string $task The ACO Section Value to check access on.
  380. *
  381. * @return boolean True if authorised
  382. * @since 11.1
  383. */
  384. public function authorise($task)
  385. {
  386. // Only do access check if the aco section is set
  387. if ($this->_acoSection) {
  388. // If we have a section value set that trumps the passed task ???
  389. if ($this->_acoSectionValue) {
  390. // We have one, so set it and lets do the check
  391. $task = $this->_acoSectionValue;
  392. }
  393. // Get the JUser object for the current user and return the authorization boolean
  394. $user = JFactory::getUser();
  395. return $user->authorise($this->_acoSection, $task);
  396. }
  397. else {
  398. // Nothing set, nothing to check... so obviously its ok :)
  399. return true;
  400. }
  401. }
  402. /**
  403. * Method to check whether an ID is in the edit list.
  404. *
  405. * @param string $context The context for the session storage.
  406. * @param int $id The ID of the record to add to the edit list.
  407. *
  408. * @return boolean True if the ID is in the edit list.
  409. * @since 11.1
  410. */
  411. protected function checkEditId($context, $id)
  412. {
  413. if ($id) {
  414. $app = JFactory::getApplication();
  415. $values = (array) $app->getUserState($context.'.id');
  416. $result = in_array((int) $id, $values);
  417. if (JDEBUG) {
  418. jimport('joomla.error.log');
  419. $log = JLog::getInstance('jcontroller.log.php')->addEntry(
  420. array('comment' => sprintf('Checking edit ID %s.%s: %d %s', $context, $id, (int) $result, str_replace("\n", ' ', print_r($values, 1))))
  421. );
  422. }
  423. return $result;
  424. }
  425. else {
  426. // No id for a new item.
  427. return true;
  428. }
  429. }
  430. /**
  431. * Method to load and return a model object.
  432. *
  433. * @param string $name The name of the model.
  434. * @param string $prefix Optional model prefix.
  435. * @param array $config Configuration array for the model. Optional.
  436. *
  437. * @return mixed Model object on success; otherwise null failure.
  438. * @since 11.1 Replaces _createModel.
  439. */
  440. protected function createModel($name, $prefix = '', $config = array())
  441. {
  442. // Clean the model name
  443. $modelName = preg_replace('/[^A-Z0-9_]/i', '', $name);
  444. $classPrefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix);
  445. $result = JModel::getInstance($modelName, $classPrefix, $config);
  446. return $result;
  447. }
  448. /**
  449. * Method to load and return a view object. This method first looks in the
  450. * current template directory for a match and, failing that, uses a default
  451. * set path to load the view class file.
  452. *
  453. * Note the "name, prefix, type" order of parameters, which differs from the
  454. * "name, type, prefix" order used in related public methods.
  455. *
  456. * @param string $name The name of the view.
  457. * @param string $prefix Optional prefix for the view class name.
  458. * @param string $type The type of view.
  459. * @param array $config Configuration array for the view. Optional.
  460. *
  461. * @return mixed View object on success; null or error result on failure.
  462. * @since 11.1 Replaces _createView.
  463. */
  464. protected function createView($name, $prefix = '', $type = '', $config = array())
  465. {
  466. // Clean the view name
  467. $viewName = preg_replace('/[^A-Z0-9_]/i', '', $name);
  468. $classPrefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix);
  469. $viewType = preg_replace('/[^A-Z0-9_]/i', '', $type);
  470. // Build the view class name
  471. $viewClass = $classPrefix . $viewName;
  472. if (!class_exists($viewClass)) {
  473. jimport('joomla.filesystem.path');
  474. $path = JPath::find(
  475. $this->paths['view'],
  476. $this->createFileName('view', array('name' => $viewName, 'type' => $viewType))
  477. );
  478. if ($path) {
  479. require_once $path;
  480. if (!class_exists($viewClass)) {
  481. $result = JError::raiseError(
  482. 500,
  483. JText::sprintf('JLIB_APPLICATION_ERROR_VIEW_CLASS_NOT_FOUND', $viewClass, $path)
  484. );
  485. return null;
  486. }
  487. }
  488. else {
  489. return null;
  490. }
  491. }
  492. return new $viewClass($config);
  493. }
  494. /**
  495. * Typical view method for MVC based architecture
  496. *
  497. * This function is provide as a default implementation, in most cases
  498. * you will need to override it in your own controllers.
  499. *
  500. * @param boolean $cachable If true, the view output will be cached
  501. * @param array $urlparams An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
  502. *
  503. * @return JController This method to support chaining.
  504. * @since 11.1
  505. */
  506. public function display($cachable = false, $urlparams = false)
  507. {
  508. $document = JFactory::getDocument();
  509. $viewType = $document->getType();
  510. $viewName = JRequest::getCmd('view', $this->default_view);
  511. $viewLayout = JRequest::getCmd('layout', 'default');
  512. $view = $this->getView($viewName, $viewType, '', array('base_path' => $this->basePath));
  513. // Get/Create the model
  514. if ($model = $this->getModel($viewName)) {
  515. // Push the model into the view (as default)
  516. $view->setModel($model, true);
  517. }
  518. // Set the layout
  519. $view->setLayout($viewLayout);
  520. $view->assignRef('document', $document);
  521. $conf = JFactory::getConfig();
  522. // Display the view
  523. if ($cachable && $viewType != 'feed' && $conf->get('caching') >= 1) {
  524. $option = JRequest::getCmd('option');
  525. $cache = JFactory::getCache($option, 'view');
  526. if (is_array($urlparams)) {
  527. $app = JFactory::getApplication();
  528. $registeredurlparams = $app->get('registeredurlparams');
  529. if (empty($registeredurlparams)) {
  530. $registeredurlparams = new stdClass();
  531. }
  532. foreach ($urlparams AS $key => $value)
  533. {
  534. // Add your safe url parameters with variable type as value {@see JFilterInput::clean()}.
  535. $registeredurlparams->$key = $value;
  536. }
  537. $app->set('registeredurlparams', $registeredurlparams);
  538. }
  539. $cache->get($view, 'display');
  540. }
  541. else {
  542. $view->display();
  543. }
  544. return $this;
  545. }
  546. /**
  547. * Execute a task by triggering a method in the derived class.
  548. *
  549. * @param string $task The task to perform. If no matching task is found, the '__default' task is executed, if defined.
  550. *
  551. * @return mixed|false The value returned by the called method, false in error case.
  552. * @since 11.1
  553. */
  554. public function execute($task)
  555. {
  556. $this->task = $task;
  557. $task = strtolower($task);
  558. if (isset($this->taskMap[$task])) {
  559. $doTask = $this->taskMap[$task];
  560. }
  561. elseif (isset($this->taskMap['__default'])) {
  562. $doTask = $this->taskMap['__default'];
  563. }
  564. else {
  565. return JError::raiseError(404, JText::sprintf('JLIB_APPLICATION_ERROR_TASK_NOT_FOUND', $task));
  566. }
  567. // Record the actual task being fired
  568. $this->doTask = $doTask;
  569. // Make sure we have access
  570. if ($this->authorise($doTask)) {
  571. $retval = $this->$doTask();
  572. return $retval;
  573. }
  574. else {
  575. return JError::raiseError(403, JText::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'));
  576. }
  577. }
  578. /**
  579. * Method to get a model object, loading it if required.
  580. *
  581. * @param string $name The model name. Optional.
  582. * @param string $prefix The class prefix. Optional.
  583. * @param array $config Configuration array for model. Optional.
  584. *
  585. * @return object The model.
  586. * @since 11.1
  587. */
  588. public function getModel($name = '', $prefix = '', $config = array())
  589. {
  590. if (empty($name)) {
  591. $name = $this->getName();
  592. }
  593. if (empty($prefix)) {
  594. $prefix = $this->model_prefix;
  595. }
  596. if ($model = $this->createModel($name, $prefix, $config)) {
  597. // Task is a reserved state
  598. $model->setState('task', $this->task);
  599. // Let's get the application object and set menu information if it's available
  600. $app = JFactory::getApplication();
  601. $menu = $app->getMenu();
  602. if (is_object($menu)) {
  603. if ($item = $menu->getActive()) {
  604. $params = $menu->getParams($item->id);
  605. // Set default state data
  606. $model->setState('parameters.menu', $params);
  607. }
  608. }
  609. }
  610. return $model;
  611. }
  612. /**
  613. * Method to get the controller name
  614. *
  615. * The dispatcher name is set by default parsed using the classname, or it can be set
  616. * by passing a $config['name'] in the class constructor
  617. *
  618. * @return string The name of the dispatcher
  619. * @since 11.1
  620. */
  621. public function getName()
  622. {
  623. $name = $this->name;
  624. if (empty($name)) {
  625. $r = null;
  626. if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
  627. JError::raiseError(500, JText::_('JLIB_APPLICATION_ERROR_CONTROLLER_GET_NAME'));
  628. }
  629. $name = strtolower($r[1]);
  630. }
  631. return $name;
  632. }
  633. /**
  634. * Get the last task that is being performed or was most recently performed.
  635. *
  636. * @return string The task that is being performed or was most recently performed.
  637. * @since 11.1
  638. */
  639. public function getTask()
  640. {
  641. return $this->task;
  642. }
  643. /**
  644. * Gets the available tasks in the controller.
  645. *
  646. * @return array Array[i] of task names.
  647. * @since 11.1
  648. */
  649. public function getTasks()
  650. {
  651. return $this->methods;
  652. }
  653. /**
  654. * Method to get a reference to the current view and load it if necessary.
  655. *
  656. * @param string $name The view name. Optional, defaults to the controller name.
  657. * @param string $type The view type. Optional.
  658. * @param string $prefix The class prefix. Optional.
  659. * @param array $config Configuration array for view. Optional.
  660. *
  661. * @return object Reference to the view or an error.
  662. * @since 11.1
  663. */
  664. public function getView($name = '', $type = '', $prefix = '', $config = array())
  665. {
  666. static $views;
  667. if (!isset($views)) {
  668. $views = array();
  669. }
  670. if (empty($name)) {
  671. $name = $this->getName();
  672. }
  673. if (empty($prefix)) {
  674. $prefix = $this->getName() . 'View';
  675. }
  676. if (empty($views[$name])) {
  677. if ($view = $this->createView($name, $prefix, $type, $config)) {
  678. $views[$name] = & $view;
  679. }
  680. else {
  681. $result = JError::raiseError(
  682. 500,
  683. JText::sprintf('JLIB_APPLICATION_ERROR_VIEW_NOT_FOUND', $name, $type, $prefix)
  684. );
  685. return $result;
  686. }
  687. }
  688. return $views[$name];
  689. }
  690. /**
  691. * Method to add a record ID to the edit list.
  692. *
  693. * @param string $context The context for the session storage.
  694. * @param int $id The ID of the record to add to the edit list.
  695. *
  696. * @return void
  697. * @since 11.1
  698. */
  699. protected function holdEditId($context, $id)
  700. {
  701. // Initialise variables.
  702. $app = JFactory::getApplication();
  703. $values = (array) $app->getUserState($context.'.id');
  704. // Add the id to the list if non-zero.
  705. if (!empty($id)) {
  706. array_push($values, (int) $id);
  707. $values = array_unique($values);
  708. $app->setUserState($context.'.id', $values);
  709. if (JDEBUG) {
  710. jimport('joomla.error.log');
  711. $log = JLog::getInstance('jcontroller.log.php')->addEntry(
  712. array('comment' => sprintf('Holding edit ID %s.%s %s', $context, $id, str_replace("\n", ' ', print_r($values, 1))))
  713. );
  714. }
  715. }
  716. }
  717. /**
  718. * Redirects the browser or returns false if no redirect is set.
  719. *
  720. * @return boolean False if no redirect exists.
  721. * @since 11.1
  722. */
  723. public function redirect()
  724. {
  725. if ($this->redirect) {
  726. $app = JFactory::getApplication();
  727. $app->redirect($this->redirect, $this->message, $this->messageType);
  728. }
  729. return false;
  730. }
  731. /**
  732. * Register the default task to perform if a mapping is not found.
  733. *
  734. * @param string $method The name of the method in the derived class to perform if a named task is not found.
  735. *
  736. * @return JController This object to support chaining.
  737. * @since 11.1
  738. */
  739. public function registerDefaultTask($method)
  740. {
  741. $this->registerTask('__default', $method);
  742. return $this;
  743. }
  744. /**
  745. * Register (map) a task to a method in the class.
  746. *
  747. * @param string $task The task.
  748. * @param string $method The name of the method in the derived class to perform for this task.
  749. *
  750. * @return JController This object to support chaining.
  751. * @since 11.1
  752. */
  753. public function registerTask($task, $method)
  754. {
  755. if (in_array(strtolower($method), $this->methods)) {
  756. $this->taskMap[strtolower($task)] = $method;
  757. }
  758. return $this;
  759. }
  760. /**
  761. * Method to check whether an ID is in the edit list.
  762. *
  763. * @param string $context The context for the session storage.
  764. * @param int $id The ID of the record to add to the edit list.
  765. *
  766. * @return void
  767. * @since 11.1
  768. */
  769. protected function releaseEditId($context, $id)
  770. {
  771. $app = JFactory::getApplication();
  772. $values = (array) $app->getUserState($context.'.id');
  773. // Do a strict search of the edit list values.
  774. $index = array_search((int) $id, $values, true);
  775. if (is_int($index)) {
  776. unset($values[$index]);
  777. $app->setUserState($context.'.id', $values);
  778. if (JDEBUG) {
  779. jimport('joomla.error.log');
  780. $log = JLog::getInstance('jcontroller.log.php')->addEntry(
  781. array('comment' => sprintf('Releasing edit ID %s.%s %s', $context, $id, str_replace("\n", ' ', print_r($values, 1))))
  782. );
  783. }
  784. }
  785. }
  786. /**
  787. * Sets the access control levels.
  788. *
  789. * @param string $section The ACO section (eg, the component).
  790. * @param string $value The ACO section value (if using a constant value).
  791. *
  792. * @return void
  793. * @since 11.1
  794. * @deprecated 1.6 - Apr 5, 2010
  795. */
  796. public function setAccessControl($section, $value = null)
  797. {
  798. $this->_acoSection = $section;
  799. $this->_acoSectionValue = $value;
  800. }
  801. /**
  802. * Sets the internal message that is passed with a redirect
  803. *
  804. * @param string $text Message to display on redirect.
  805. * @param string $type Message type (since 11.1). Optional, defaults to 'message'.
  806. *
  807. * @return string Previous message
  808. * @since 11.1
  809. */
  810. public function setMessage($text, $type = 'message')
  811. {
  812. $previous = $this->message;
  813. $this->message = $text;
  814. $this->messageType = $type;
  815. return $previous;
  816. }
  817. /**
  818. * Sets an entire array of search paths for resources.
  819. *
  820. * @param string $type The type of path to set, typically 'view' or 'model'.
  821. * @param string|array $path The new set of search paths. If null or false, resets to the current directory only.
  822. *
  823. * @return void
  824. * @since 11.1 Replaces _setPath.
  825. */
  826. protected function setPath($type, $path)
  827. {
  828. // clear out the prior search dirs
  829. $this->paths[$type] = array();
  830. // actually add the user-specified directories
  831. $this->addPath($type, $path);
  832. }
  833. /**
  834. * Set a URL for browser redirection.
  835. *
  836. * @param string $url URL to redirect to.
  837. * @param string $msg Message to display on redirect. Optional, defaults to value set internally by controller, if any.
  838. * @param string $type Message type. Optional, defaults to 'message' or the type set by a previous call to setMessage.
  839. *
  840. * @return JController This object to support chaining.
  841. *
  842. * @since 11.1
  843. */
  844. public function setRedirect($url, $msg = null, $type = null)
  845. {
  846. $this->redirect = $url;
  847. if ($msg !== null) {
  848. // Controller may have set this directly
  849. $this->message = $msg;
  850. }
  851. // Ensure the type is not overwritten by a previous call to setMessage.
  852. if (empty($type)) {
  853. if (empty($this->messageType)) {
  854. $this->messageType = 'message';
  855. }
  856. }
  857. // If the type is explicitly set, set it.
  858. else {
  859. $this->messageType = $type;
  860. }
  861. return $this;
  862. }
  863. }