PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

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