PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

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