PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/joomla/form/fields/rules.php

https://github.com/nikosdion/Akeeba-Example
PHP | 312 lines | 195 code | 48 blank | 69 comment | 28 complexity | c230f022c2423fcc5d187f08270f7bb5 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Form
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 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. jimport('joomla.html.html');
  11. jimport('joomla.access.access');
  12. jimport('joomla.form.formfield');
  13. /**
  14. * Form Field class for the Joomla Platform.
  15. * Field for assigning permissions to groups for a given asset
  16. *
  17. * @package Joomla.Platform
  18. * @subpackage Form
  19. * @see JAccess
  20. * @since 11.1
  21. */
  22. class JFormFieldRules extends JFormField
  23. {
  24. /**
  25. * The form field type.
  26. *
  27. * @var string
  28. * @since 11.1
  29. */
  30. public $type = 'Rules';
  31. /**
  32. * Method to get the field input markup for Access Control Lists.
  33. * Optionally can be associated with a specific component and section.
  34. *
  35. * TODO: Add access check.
  36. *
  37. * @return string The field input markup.
  38. *
  39. * @since 11.1
  40. */
  41. protected function getInput()
  42. {
  43. JHtml::_('behavior.tooltip');
  44. // Initialise some field attributes.
  45. $section = $this->element['section'] ? (string) $this->element['section'] : '';
  46. $component = $this->element['component'] ? (string) $this->element['component'] : '';
  47. $assetField = $this->element['asset_field'] ? (string) $this->element['asset_field'] : 'asset_id';
  48. // Get the actions for the asset.
  49. $actions = JAccess::getActions($component, $section);
  50. // Iterate over the children and add to the actions.
  51. foreach ($this->element->children() as $el)
  52. {
  53. if ($el->getName() == 'action')
  54. {
  55. $actions[] = (object) array('name' => (string) $el['name'], 'title' => (string) $el['title'],
  56. 'description' => (string) $el['description']);
  57. }
  58. }
  59. // Get the explicit rules for this asset.
  60. if ($section == 'component')
  61. {
  62. // Need to find the asset id by the name of the component.
  63. $db = JFactory::getDbo();
  64. $db->setQuery('SELECT id FROM #__assets WHERE name = ' . $db->quote($component));
  65. $assetId = (int) $db->loadResult();
  66. if ($error = $db->getErrorMsg())
  67. {
  68. JError::raiseNotice(500, $error);
  69. }
  70. }
  71. else
  72. {
  73. // Find the asset id of the content.
  74. // Note that for global configuration, com_config injects asset_id = 1 into the form.
  75. $assetId = $this->form->getValue($assetField);
  76. }
  77. // Use the compact form for the content rules (deprecated).
  78. //if (!empty($component) && $section != 'component') {
  79. // return JHtml::_('rules.assetFormWidget', $actions, $assetId, $assetId ? null : $component, $this->name, $this->id);
  80. //}
  81. // Full width format.
  82. // Get the rules for just this asset (non-recursive).
  83. $assetRules = JAccess::getAssetRules($assetId);
  84. // Get the available user groups.
  85. $groups = $this->getUserGroups();
  86. // Build the form control.
  87. $curLevel = 0;
  88. // Prepare output
  89. $html = array();
  90. $html[] = '<div id="permissions-sliders" class="pane-sliders">';
  91. $html[] = '<p class="rule-desc">' . JText::_('JLIB_RULES_SETTINGS_DESC') . '</p>';
  92. $html[] = '<ul id="rules">';
  93. // Start a row for each user group.
  94. foreach ($groups as $group)
  95. {
  96. $difLevel = $group->level - $curLevel;
  97. if ($difLevel > 0)
  98. {
  99. $html[] = '<li><ul>';
  100. }
  101. else if ($difLevel < 0)
  102. {
  103. $html[] = str_repeat('</ul></li>', -$difLevel);
  104. }
  105. $html[] = '<li>';
  106. $html[] = '<div class="panel">';
  107. $html[] = '<h3 class="pane-toggler title"><a href="javascript:void(0);"><span>';
  108. $html[] = str_repeat('<span class="level">|&ndash;</span> ', $curLevel = $group->level) . $group->text;
  109. $html[] = '</span></a></h3>';
  110. $html[] = '<div class="pane-slider content pane-hide">';
  111. $html[] = '<div class="mypanel">';
  112. $html[] = '<table class="group-rules">';
  113. $html[] = '<thead>';
  114. $html[] = '<tr>';
  115. $html[] = '<th class="actions" id="actions-th' . $group->value . '">';
  116. $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_ACTION') . '</span>';
  117. $html[] = '</th>';
  118. $html[] = '<th class="settings" id="settings-th' . $group->value . '">';
  119. $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_SELECT_SETTING') . '</span>';
  120. $html[] = '</th>';
  121. // The calculated setting is not shown for the root group of global configuration.
  122. $canCalculateSettings = ($group->parent_id || !empty($component));
  123. if ($canCalculateSettings)
  124. {
  125. $html[] = '<th id="aclactionth' . $group->value . '">';
  126. $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_CALCULATED_SETTING') . '</span>';
  127. $html[] = '</th>';
  128. }
  129. $html[] = '</tr>';
  130. $html[] = '</thead>';
  131. $html[] = '<tbody>';
  132. foreach ($actions as $action)
  133. {
  134. $html[] = '<tr>';
  135. $html[] = '<td headers="actions-th' . $group->value . '">';
  136. $html[] = '<label class="hasTip" for="' . $this->id . '_' . $action->name . '_' . $group->value . '" title="'
  137. . htmlspecialchars(JText::_($action->title) . '::' . JText::_($action->description), ENT_COMPAT, 'UTF-8') . '">';
  138. $html[] = JText::_($action->title);
  139. $html[] = '</label>';
  140. $html[] = '</td>';
  141. $html[] = '<td headers="settings-th' . $group->value . '">';
  142. $html[] = '<select name="' . $this->name . '[' . $action->name . '][' . $group->value . ']" id="' . $this->id . '_' . $action->name
  143. . '_' . $group->value . '" title="'
  144. . JText::sprintf('JLIB_RULES_SELECT_ALLOW_DENY_GROUP', JText::_($action->title), trim($group->text)) . '">';
  145. $inheritedRule = JAccess::checkGroup($group->value, $action->name, $assetId);
  146. // Get the actual setting for the action for this group.
  147. $assetRule = $assetRules->allow($action->name, $group->value);
  148. // Build the dropdowns for the permissions sliders
  149. // The parent group has "Not Set", all children can rightly "Inherit" from that.
  150. $html[] = '<option value=""' . ($assetRule === null ? ' selected="selected"' : '') . '>'
  151. . JText::_(empty($group->parent_id) && empty($component) ? 'JLIB_RULES_NOT_SET' : 'JLIB_RULES_INHERITED') . '</option>';
  152. $html[] = '<option value="1"' . ($assetRule === true ? ' selected="selected"' : '') . '>' . JText::_('JLIB_RULES_ALLOWED')
  153. . '</option>';
  154. $html[] = '<option value="0"' . ($assetRule === false ? ' selected="selected"' : '') . '>' . JText::_('JLIB_RULES_DENIED')
  155. . '</option>';
  156. $html[] = '</select>&#160; ';
  157. // If this asset's rule is allowed, but the inherited rule is deny, we have a conflict.
  158. if (($assetRule === true) && ($inheritedRule === false))
  159. {
  160. $html[] = JText::_('JLIB_RULES_CONFLICT');
  161. }
  162. $html[] = '</td>';
  163. // Build the Calculated Settings column.
  164. // The inherited settings column is not displayed for the root group in global configuration.
  165. if ($canCalculateSettings)
  166. {
  167. $html[] = '<td headers="aclactionth' . $group->value . '">';
  168. // This is where we show the current effective settings considering currrent group, path and cascade.
  169. // Check whether this is a component or global. Change the text slightly.
  170. if (JAccess::checkGroup($group->value, 'core.admin') !== true)
  171. {
  172. if ($inheritedRule === null)
  173. {
  174. $html[] = '<span class="icon-16-unset">' . JText::_('JLIB_RULES_NOT_ALLOWED') . '</span>';
  175. }
  176. else if ($inheritedRule === true)
  177. {
  178. $html[] = '<span class="icon-16-allowed">' . JText::_('JLIB_RULES_ALLOWED') . '</span>';
  179. }
  180. else if ($inheritedRule === false)
  181. {
  182. if ($assetRule === false)
  183. {
  184. $html[] = '<span class="icon-16-denied">' . JText::_('JLIB_RULES_NOT_ALLOWED') . '</span>';
  185. }
  186. else
  187. {
  188. $html[] = '<span class="icon-16-denied"><span class="icon-16-locked">' . JText::_('JLIB_RULES_NOT_ALLOWED_LOCKED')
  189. . '</span></span>';
  190. }
  191. }
  192. }
  193. else if (!empty($component))
  194. {
  195. $html[] = '<span class="icon-16-allowed"><span class="icon-16-locked">' . JText::_('JLIB_RULES_ALLOWED_ADMIN')
  196. . '</span></span>';
  197. }
  198. else
  199. {
  200. // Special handling for groups that have global admin because they can't be denied.
  201. // The admin rights can be changed.
  202. if ($action->name === 'core.admin')
  203. {
  204. $html[] = '<span class="icon-16-allowed">' . JText::_('JLIB_RULES_ALLOWED') . '</span>';
  205. }
  206. elseif ($inheritedRule === false)
  207. {
  208. // Other actions cannot be changed.
  209. $html[] = '<span class="icon-16-denied"><span class="icon-16-locked">'
  210. . JText::_('JLIB_RULES_NOT_ALLOWED_ADMIN_CONFLICT') . '</span></span>';
  211. }
  212. else
  213. {
  214. $html[] = '<span class="icon-16-allowed"><span class="icon-16-locked">' . JText::_('JLIB_RULES_ALLOWED_ADMIN')
  215. . '</span></span>';
  216. }
  217. }
  218. $html[] = '</td>';
  219. }
  220. $html[] = '</tr>';
  221. }
  222. $html[] = '</tbody>';
  223. $html[] = '</table></div>';
  224. $html[] = '</div></div>';
  225. $html[] = '</li>';
  226. }
  227. $html[] = str_repeat('</ul></li>', $curLevel);
  228. $html[] = '</ul><div class="rule-notes">';
  229. if ($section == 'component' || $section == null)
  230. {
  231. $html[] = JText::_('JLIB_RULES_SETTING_NOTES');
  232. }
  233. else
  234. {
  235. $html[] = JText::_('JLIB_RULES_SETTING_NOTES_ITEM');
  236. }
  237. $html[] = '</div></div>';
  238. $js = "window.addEvent('domready', function(){ new Fx.Accordion($$('div#permissions-sliders.pane-sliders .panel h3.pane-toggler'), $$('div#permissions-sliders.pane-sliders .panel div.pane-slider'), {onActive: function(toggler, i) {toggler.addClass('pane-toggler-down');toggler.removeClass('pane-toggler');i.addClass('pane-down');i.removeClass('pane-hide');Cookie.write('jpanesliders_permissions-sliders"
  239. . $component
  240. . "',$$('div#permissions-sliders.pane-sliders .panel h3').indexOf(toggler));},onBackground: function(toggler, i) {toggler.addClass('pane-toggler');toggler.removeClass('pane-toggler-down');i.addClass('pane-hide');i.removeClass('pane-down');},duration: 300,display: "
  241. . JRequest::getInt('jpanesliders_permissions-sliders' . $component, 0, 'cookie') . ",show: "
  242. . JRequest::getInt('jpanesliders_permissions-sliders' . $component, 0, 'cookie') . ", alwaysHide:true, opacity: false}); });";
  243. JFactory::getDocument()->addScriptDeclaration($js);
  244. return implode("\n", $html);
  245. }
  246. /**
  247. * Get a list of the user groups.
  248. *
  249. * @return array
  250. *
  251. * @since 11.1
  252. */
  253. protected function getUserGroups()
  254. {
  255. // Initialise variables.
  256. $db = JFactory::getDBO();
  257. $query = $db->getQuery(true)->select('a.id AS value, a.title AS text, COUNT(DISTINCT b.id) AS level, a.parent_id')
  258. ->from('#__usergroups AS a')->leftJoin($query->qn('#__usergroups') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt')->group('a.id')
  259. ->order('a.lft ASC');
  260. $db->setQuery($query);
  261. $options = $db->loadObjectList();
  262. return $options;
  263. }
  264. }