PageRenderTime 20ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

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