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

/admin/libraries/legacy/controller/legacy.php

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