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

Language PHP Lines 183
MD5 Hash 9321f2886a85550824e03cae23cad4d6
Repository https://github.com/cosmocommerce/joomla.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
 * @version		$Id$
 * @package		Joomla.Framework
 * @subpackage	Application
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access
defined('JPATH_BASE') or die;

/**
 * Component helper class
 *
 * @package		Joomla.Framework
 * @subpackage	Application
 * @since		1.5
 */
class JComponentHelper
{
	/**
	 * The component list cache
	 *
	 * @var	array
	 */
	protected static $_components = array();

	/**
	 * Get the component information.
	 *
	 * @param	string $option	The component option.
	 * @param	boolean	$string	If set and a component does not exist, the enabled attribue will be set to false
	 * @return	object			An object with the fields for the component.
	 */
	public static function getComponent($option, $strict = false)
	{
		if (!isset(self::$_components[$option])) {
			if (self::_load($option)){
				$result = &self::$_components[$option];
			} else {
				$result				= new stdClass;
				$result->enabled	= $strict ? false : true;
				$result->params		= new JParameter;
			}
		} else {
			$result = &self::$_components[$option];
		}

		return $result;
	}

	/**
	 * Checks if the component is enabled
	 *
	 * @param	string	$option		The component option.
	 * @param	boolean	$string		If set and a component does not exist, false will be returned
	 * @return	boolean
	 */
	public static function isEnabled($option, $strict = false)
	{
		$result = &self::getComponent($option, $strict);
		return ($result->enabled | JFactory::getApplication()->isAdmin());
	}

	/**
	 * Gets the parameter object for the component
	 *
	 * @param	string		The option for the component.
	 * @param	boolean		If set and a component does not exist, false will be returned
	 * @return	JRegistry	As of 1.6, this method returns a JRegistry (previous versions returned JParameter).
	 */
	public static function getParams($option, $strict = false)
	{
		$component = &self::getComponent($option, $strict);
		return $component->params;
	}

	/**
	 * Render the component.
	 * @param	string	The component option.
	 */
	public static function renderComponent($option, $params = array())
	{
		// Initialise variables.
		$app	= JFactory::getApplication();

		if (empty($option)) {
			// Throw 404 if no component
			JError::raiseError(404, JText::_("COMPONENT_NOT_FOUND"));
			return;
		}

		$scope = $app->scope; //record the scope
		$app->scope = $option;  //set scope to component name

		// Build the component path.
		$option	= preg_replace('/[^A-Z0-9_\.-]/i', '', $option);
		$file	= substr($option, 4);

		// Define component path.
		define('JPATH_COMPONENT',				JPATH_BASE.DS.'components'.DS.$option);
		define('JPATH_COMPONENT_SITE',			JPATH_SITE.DS.'components'.DS.$option);
		define('JPATH_COMPONENT_ADMINISTRATOR',	JPATH_ADMINISTRATOR.DS.'components'.DS.$option);

		// get component path
		if ($app->isAdmin() && file_exists(JPATH_COMPONENT.DS.'admin.'.$file.'.php')) {
			$path = JPATH_COMPONENT.DS.'admin.'.$file.'.php';
		} else {
			$path = JPATH_COMPONENT.DS.$file.'.php';
		}

		// If component disabled throw error
		if (!self::isEnabled($option) || !file_exists($path)) {
			JError::raiseError(404, JText::_('COMPONENT_NOT_FOUND'));
		}

		$task = JRequest::getString('task');

		// Load common and local language files.
		$lang = &JFactory::getLanguage();
			$lang->load($option, JPATH_BASE, null, false, false)
		||	$lang->load($option, JPATH_COMPONENT, null, false, false)
		||	$lang->load($option, JPATH_BASE, $lang->getDefault(), false, false)
		||	$lang->load($option, JPATH_COMPONENT, $lang->getDefault(), false, false);

		// Handle template preview outlining.
		$contents = null;

		// Execute the component.
		ob_start();
		require_once $path;
		$contents = ob_get_contents();
		ob_end_clean();

		// Build the component toolbar
		jimport('joomla.application.helper');
		if (($path = JApplicationHelper::getPath('toolbar')) && $app->isAdmin()) {
			// Get the task again, in case it has changed
			$task = JRequest::getString('task');

			// Make the toolbar
			include_once $path;
		}

		$app->scope = $scope; //revert the scope

		return $contents;
	}

	/**
	 * Load the installed components into the _components property.
	 *
	 * @return	boolean
	 */
	protected static function _load($option)
	{
		$db		= JFactory::getDbo();
		$query	= $db->getQuery(true);
		$query->select('extension_id AS "id", element AS "option", params, enabled');
		$query->from('#__extensions');
		$query->where('`type` = "component"');
		$query->where('`element` = "'.$option.'"');
		$db->setQuery($query);

		self::$_components[$option] = $db->loadObject();

		if ($error = $db->getErrorMsg() || empty(self::$_components[$option])) {
			// Fatal error.
			JError::raiseWarning(500, 'Error loading component: "'.$option.'" '.$error);
			return false;
		}

		// Convert the params to an object.
		if (is_string(self::$_components[$option]->params)) {
			$temp = new JParameter(self::$_components[$option]->params);
			self::$_components[$option]->params = $temp;
		}

		return true;
	}
}
Back to Top