PageRenderTime 88ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/content/code/trunk/administrator/components/com_artofcontent/libraries/joomla/application/component/controller.php

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