Blame view

libraries/cms/html/rules.php 8.06 KB
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
<?php
/**
 * @package     Joomla.Libraries
 * @subpackage  HTML
 *
 * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('JPATH_PLATFORM') or die;

JLog::add('JHtmlRules is deprecated.', JLog::WARNING, 'deprecated');

/**
 * Extended Utility class for all HTML drawing classes.
 *
 * @since       1.6
 * @deprecated  4.0
 */
abstract class JHtmlRules
{
	/**
	 * Creates the HTML for the permissions widget
	 *
	 * @param   array    $actions   Array of action objects
	 * @param   integer  $assetId   Id of a specific asset to  create a widget for.
	 * @param   integer  $parent    Id of the parent of the asset
	 * @param   string   $control   The form control
	 * @param   string   $idPrefix  Prefix for the ids assigned to specific action-group pairs
	 *
	 * @return  string   HTML for the permissions widget
	 *
	 * @see     JAccess
	 * @see     JFormFieldRules
	 * @since   1.6
	 * @deprecated  4.0
	 */
	public static function assetFormWidget($actions, $assetId = null, $parent = null, $control = 'jform[rules]', $idPrefix = 'jform_rules')
	{
		$images = static::_getImagesArray();

		// Get the user groups.
		$groups = static::_getUserGroups();

		// Get the incoming inherited rules as well as the asset specific rules.
		$inheriting = JAccess::getAssetRules($parent ?: static::_getParentAssetId($assetId), true);
		$inherited = JAccess::getAssetRules($assetId, true);
		$rules = JAccess::getAssetRules($assetId);

		$html = array();

		$html[] = '<div class="acl-options">';
		$html[] = JHtml::_('tabs.start', 'acl-rules-' . $assetId, array('useCookie' => 1));
		$html[] = JHtml::_('tabs.panel', JText::_('JLIB_HTML_ACCESS_SUMMARY'), 'summary');
		$html[] = '			<p>' . JText::_('JLIB_HTML_ACCESS_SUMMARY_DESC') . '</p>';
		$html[] = '			<table class="aclsummary-table" summary="' . JText::_('JLIB_HTML_ACCESS_SUMMARY_DESC') . '">';
		$html[] = '			<caption>' . JText::_('JLIB_HTML_ACCESS_SUMMARY_DESC_CAPTION') . '</caption>';
		$html[] = '			<tr>';
		$html[] = '				<th class="col1 hidelabeltxt">' . JText::_('JLIB_RULES_GROUPS') . '</th>';

		foreach ($actions as $i => $action)
		{
			$html[] = '				<th class="col' . ($i + 2) . '">' . JText::_($action->title) . '</th>';
		}

		$html[] = '			</tr>';

		foreach ($groups as $i => $group)
		{
			$html[] = '			<tr class="row' . ($i % 2) . '">';
			$html[] = '				<td class="col1">' . $group->text . '</td>';

			foreach ($actions as $j => $action)
			{
				$html[] = '				<td class="col' . ($j + 2) . '">'
					. ($assetId ? ($inherited->allow($action->name, $group->identities) ? $images['allow'] : $images['deny'])
					: ($inheriting->allow($action->name, $group->identities) ? $images['allow'] : $images['deny'])) . '</td>';
			}

			$html[] = '			</tr>';
		}

		$html[] = ' 		</table>';

		foreach ($actions as $action)
		{
			$actionTitle = JText::_($action->title);
			$actionDesc = JText::_($action->description);
			$html[] = JHtml::_('tabs.panel', $actionTitle, $action->name);
			$html[] = '			<p>' . $actionDesc . '</p>';
			$html[] = '			<table class="aclmodify-table" summary="' . strip_tags($actionDesc) . '">';
			$html[] = '			<caption>' . JText::_('JLIB_HTML_ACCESS_MODIFY_DESC_CAPTION_ACL') . ' ' . $actionTitle . ' '
				. JText::_('JLIB_HTML_ACCESS_MODIFY_DESC_CAPTION_TABLE') . '</caption>';
			$html[] = '			<tr>';
			$html[] = '				<th class="col1 hidelabeltxt">' . JText::_('JLIB_RULES_GROUP') . '</th>';
			$html[] = '				<th class="col2">' . JText::_('JLIB_RULES_INHERIT') . '</th>';
			$html[] = '				<th class="col3 hidelabeltxt">' . JText::_('JMODIFY') . '</th>';
			$html[] = '				<th class="col4">' . JText::_('JCURRENT') . '</th>';
			$html[] = '			</tr>';

			foreach ($groups as $i => $group)
			{
				$selected = $rules->allow($action->name, $group->value);

				$html[] = '			<tr class="row' . ($i % 2) . '">';
				$html[] = '				<td class="col1">' . $group->text . '</td>';
				$html[] = '				<td class="col2">'
					. ($inheriting->allow($action->name, $group->identities) ? $images['allow-i'] : $images['deny-i']) . '</td>';
				$html[] = '				<td class="col3">';
				$html[] = '					<select id="' . $idPrefix . '_' . $action->name . '_' . $group->value
					. '" class="inputbox" size="1" name="' . $control . '[' . $action->name . '][' . $group->value . ']" title="'
					. JText::sprintf('JLIB_RULES_SELECT_ALLOW_DENY_GROUP', $actionTitle, $group->text) . '">';
				$html[] = '						<option value=""' . ($selected === null ? ' selected="selected"' : '') . '>'
					. JText::_('JLIB_RULES_INHERIT') . '</option>';
				$html[] = '						<option value="1"' . ($selected === true ? ' selected="selected"' : '') . '>'
					. JText::_('JLIB_RULES_ALLOWED') . '</option>';
				$html[] = '						<option value="0"' . ($selected === false ? ' selected="selected"' : '') . '>'
					. JText::_('JLIB_RULES_DENIED') . '</option>';
				$html[] = '					</select>';
				$html[] = '				</td>';
				$html[] = '				<td class="col4">'
					. ($assetId ? ($inherited->allow($action->name, $group->identities) ? $images['allow'] : $images['deny'])
					: ($inheriting->allow($action->name, $group->identities) ? $images['allow'] : $images['deny'])) . '</td>';
				$html[] = '			</tr>';
			}

			$html[] = '			</table>';
		}

		$html[] = JHtml::_('tabs.end');

		// Build the footer with legend and special purpose buttons.
		$html[] = '	<div class="clr"></div>';
		$html[] = '	<ul class="acllegend fltlft">';
		$html[] = '		<li class="acl-allowed">' . JText::_('JLIB_RULES_ALLOWED') . '</li>';
		$html[] = '		<li class="acl-denied">' . JText::_('JLIB_RULES_DENIED') . '</li>';
		$html[] = '	</ul>';
		$html[] = '</div>';

		return implode("\n", $html);
	}

	/**
	 * Get the id of the parent asset
	 *
	 * @param   integer  $assetId  The asset for which the parentid will be returned
	 *
	 * @return  integer  The id of the parent asset
	 *
	 * @since   1.6
	 * @deprecated  4.0
	 */
	protected static function _getParentAssetId($assetId)
	{
		// Get a database object.
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);

		// Get the user groups from the database.
		$query->select($db->quoteName('parent_id'))
			->from($db->quoteName('#__assets'))
			->where($db->quoteName('id') . ' = ' . (int) $assetId);
		$db->setQuery($query);

		return (int) $db->loadResult();
	}

	/**
	 * Get the user groups
	 *
	 * @return  array  Array of user groups
	 *
	 * @since   1.6
	 * @deprecated  4.0
	 */
	protected static function _getUserGroups()
	{
		// Get a database object.
		$db = JFactory::getDbo();

		// Get the user groups from the database.
		$db->setQuery(
			'SELECT a.id AS value, a.title AS text, b.id as parent'
			. ' FROM #__usergroups AS a LEFT JOIN #__usergroups AS b ON a.lft >= b.lft AND a.rgt <= b.rgt'
			. ' ORDER BY a.lft ASC, b.lft ASC'
		);
		$result = $db->loadObjectList();
		$options = array();

		// Pre-compute additional values.
		foreach ($result as $option)
		{
			$end = end($options);

			if ($end === false || $end->value != $option->value)
			{
				$end = $option;
				$end->level = 0;
				$options[] = $end;
			}
			else
			{
				$end->level++;
			}

			$end->identities[] = $option->parent;
		}

		return $options;
	}

	/**
	 * Get the array of images associate with specific permissions
	 *
	 * @return  array  An associative  array of permissions and images
	 *
	 * @since   1.6
	 * @deprecated  4.0
	 */
	protected static function _getImagesArray()
	{
		$images['allow-l'] = '<label class="icon-16-allow" title="' . JText::_('JLIB_RULES_ALLOWED') . '">' . JText::_('JLIB_RULES_ALLOWED')
			. '</label>';
		$images['deny-l'] = '<label class="icon-16-deny" title="' . JText::_('JLIB_RULES_DENIED') . '">' . JText::_('JLIB_RULES_DENIED') . '</label>';
		$images['allow'] = '<a class="icon-16-allow" title="' . JText::_('JLIB_RULES_ALLOWED') . '"> </a>';
		$images['deny'] = '<a class="icon-16-deny" title="' . JText::_('JLIB_RULES_DENIED') . '"> </a>';
		$images['allow-i'] = '<a class="icon-16-allowinactive" title="' . JText::_('JRULE_ALLOWED_INHERITED') . '"> </a>';
		$images['deny-i'] = '<a class="icon-16-denyinactive" title="' . JText::_('JRULE_DENIED_INHERITED') . '"> </a>';

		return $images;
	}
}