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

/libraries/joomla/application/component/helper.php

https://github.com/cosmocommerce/joomla
PHP | 182 lines | 92 code | 26 blank | 64 comment | 15 complexity | 9321f2886a85550824e03cae23cad4d6 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id$
  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. * Component helper class
  13. *
  14. * @package Joomla.Framework
  15. * @subpackage Application
  16. * @since 1.5
  17. */
  18. class JComponentHelper
  19. {
  20. /**
  21. * The component list cache
  22. *
  23. * @var array
  24. */
  25. protected static $_components = array();
  26. /**
  27. * Get the component information.
  28. *
  29. * @param string $option The component option.
  30. * @param boolean $string If set and a component does not exist, the enabled attribue will be set to false
  31. * @return object An object with the fields for the component.
  32. */
  33. public static function getComponent($option, $strict = false)
  34. {
  35. if (!isset(self::$_components[$option])) {
  36. if (self::_load($option)){
  37. $result = &self::$_components[$option];
  38. } else {
  39. $result = new stdClass;
  40. $result->enabled = $strict ? false : true;
  41. $result->params = new JParameter;
  42. }
  43. } else {
  44. $result = &self::$_components[$option];
  45. }
  46. return $result;
  47. }
  48. /**
  49. * Checks if the component is enabled
  50. *
  51. * @param string $option The component option.
  52. * @param boolean $string If set and a component does not exist, false will be returned
  53. * @return boolean
  54. */
  55. public static function isEnabled($option, $strict = false)
  56. {
  57. $result = &self::getComponent($option, $strict);
  58. return ($result->enabled | JFactory::getApplication()->isAdmin());
  59. }
  60. /**
  61. * Gets the parameter object for the component
  62. *
  63. * @param string The option for the component.
  64. * @param boolean If set and a component does not exist, false will be returned
  65. * @return JRegistry As of 1.6, this method returns a JRegistry (previous versions returned JParameter).
  66. */
  67. public static function getParams($option, $strict = false)
  68. {
  69. $component = &self::getComponent($option, $strict);
  70. return $component->params;
  71. }
  72. /**
  73. * Render the component.
  74. * @param string The component option.
  75. */
  76. public static function renderComponent($option, $params = array())
  77. {
  78. // Initialise variables.
  79. $app = JFactory::getApplication();
  80. if (empty($option)) {
  81. // Throw 404 if no component
  82. JError::raiseError(404, JText::_("COMPONENT_NOT_FOUND"));
  83. return;
  84. }
  85. $scope = $app->scope; //record the scope
  86. $app->scope = $option; //set scope to component name
  87. // Build the component path.
  88. $option = preg_replace('/[^A-Z0-9_\.-]/i', '', $option);
  89. $file = substr($option, 4);
  90. // Define component path.
  91. define('JPATH_COMPONENT', JPATH_BASE.DS.'components'.DS.$option);
  92. define('JPATH_COMPONENT_SITE', JPATH_SITE.DS.'components'.DS.$option);
  93. define('JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR.DS.'components'.DS.$option);
  94. // get component path
  95. if ($app->isAdmin() && file_exists(JPATH_COMPONENT.DS.'admin.'.$file.'.php')) {
  96. $path = JPATH_COMPONENT.DS.'admin.'.$file.'.php';
  97. } else {
  98. $path = JPATH_COMPONENT.DS.$file.'.php';
  99. }
  100. // If component disabled throw error
  101. if (!self::isEnabled($option) || !file_exists($path)) {
  102. JError::raiseError(404, JText::_('COMPONENT_NOT_FOUND'));
  103. }
  104. $task = JRequest::getString('task');
  105. // Load common and local language files.
  106. $lang = &JFactory::getLanguage();
  107. $lang->load($option, JPATH_BASE, null, false, false)
  108. || $lang->load($option, JPATH_COMPONENT, null, false, false)
  109. || $lang->load($option, JPATH_BASE, $lang->getDefault(), false, false)
  110. || $lang->load($option, JPATH_COMPONENT, $lang->getDefault(), false, false);
  111. // Handle template preview outlining.
  112. $contents = null;
  113. // Execute the component.
  114. ob_start();
  115. require_once $path;
  116. $contents = ob_get_contents();
  117. ob_end_clean();
  118. // Build the component toolbar
  119. jimport('joomla.application.helper');
  120. if (($path = JApplicationHelper::getPath('toolbar')) && $app->isAdmin()) {
  121. // Get the task again, in case it has changed
  122. $task = JRequest::getString('task');
  123. // Make the toolbar
  124. include_once $path;
  125. }
  126. $app->scope = $scope; //revert the scope
  127. return $contents;
  128. }
  129. /**
  130. * Load the installed components into the _components property.
  131. *
  132. * @return boolean
  133. */
  134. protected static function _load($option)
  135. {
  136. $db = JFactory::getDbo();
  137. $query = $db->getQuery(true);
  138. $query->select('extension_id AS "id", element AS "option", params, enabled');
  139. $query->from('#__extensions');
  140. $query->where('`type` = "component"');
  141. $query->where('`element` = "'.$option.'"');
  142. $db->setQuery($query);
  143. self::$_components[$option] = $db->loadObject();
  144. if ($error = $db->getErrorMsg() || empty(self::$_components[$option])) {
  145. // Fatal error.
  146. JError::raiseWarning(500, 'Error loading component: "'.$option.'" '.$error);
  147. return false;
  148. }
  149. // Convert the params to an object.
  150. if (is_string(self::$_components[$option]->params)) {
  151. $temp = new JParameter(self::$_components[$option]->params);
  152. self::$_components[$option]->params = $temp;
  153. }
  154. return true;
  155. }
  156. }