PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/FullService/joomla
PHP | 286 lines | 173 code | 46 blank | 67 comment | 28 complexity | 6e10abef413973e88112d4e642d43744 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Framework
  5. * @subpackage Form
  6. * @copyright Copyright (C) 2005 - 2010 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[] = '<ul id="rules">';
  89. // Start a row for each user group.
  90. foreach ($groups as $group)
  91. {
  92. $difLevel = $group->level - $curLevel;
  93. if ($difLevel > 0) {
  94. $html[] = '<ul>';
  95. }
  96. else if ($difLevel < 0) {
  97. $html[] = str_repeat('</li></ul>', -$difLevel);
  98. }
  99. $html[] = '<li>';
  100. $html[] = '<div class="panel">';
  101. $html[] = '<h3 class="jpane-toggler title" ><a href="javascript:void(0);"><span>';
  102. $html[] = str_repeat('<span class="level">|&ndash;</span> ', $curLevel = $group->level) . $group->text;
  103. $html[] = '</span></a></h3>';
  104. $html[] = '<div class="jpane-slider content">';
  105. $html[] = '<div class="mypanel">';
  106. $html[] = '<table class="group-rules">';
  107. $html[] = '<caption>' . JText::sprintf('JLIB_RULES_GROUP', $group->text) . '<br /><span>' .
  108. JText::_('JLIB_RULES_SETTINGS_DESC') . '</span></caption>';
  109. $html[] = '<thead>';
  110. $html[] = '<tr>';
  111. $html[] = '<th class="actions" id="actions-th' . $group->value . '">';
  112. $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_ACTION') . '</span>';
  113. $html[] = '</th>';
  114. $html[] = '<th class="settings" id="settings-th' . $group->value . '">';
  115. $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_SELECT_SETTING') . '</span>';
  116. $html[] = '</th>';
  117. // The calculated setting is not shown for the root group of global configuration.
  118. $canCalculateSettings = ($group->parent_id || !empty($component));
  119. if ($canCalculateSettings) {
  120. $html[] = '<th id="aclactionth' . $group->value . '">';
  121. $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_CALCULATED_SETTING') . '</span>';
  122. $html[] = '</th>';
  123. }
  124. $html[] = '</tr>';
  125. $html[] = '</thead>';
  126. $html[] = '<tbody >';
  127. foreach ($actions as $action)
  128. {
  129. $html[] = '<tr>';
  130. $html[] = '<td headers="actions-th' . $group->value . '">';
  131. $html[] = '<label class="hasTip" for="' . $this->id . '_' . $action->name . '_' . $group->value . '" title="'.htmlspecialchars(JText::_($action->title).'::'.JText::_($action->description), ENT_COMPAT, 'UTF-8').'">';
  132. $html[] = JText::_($action->title);
  133. $html[] = '</label>';
  134. $html[] = '</td>';
  135. $html[] = '<td headers="settings-th' . $group->value . '">';
  136. $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)) . '">';
  137. $inheritedRule = JAccess::checkGroup($group->value, $action->name, $assetId);
  138. // Get the actual setting for the action for this group.
  139. $assetRule = $assetRules->allow($action->name, $group->value);
  140. // Build the dropdowns for the permissions sliders
  141. // The parent group has "Not Set", all children can rightly "Inherit" from that.
  142. $html[] = '<option value=""' . ($assetRule === null ? ' selected="selected"' : '') . '>' .
  143. JText::_(empty($group->parent_id) && empty($component) ? 'JLIB_RULES_NOT_SET' : 'JLIB_RULES_INHERITED') . '</option>';
  144. $html[] = '<option value="1"' . ($assetRule === true ? ' selected="selected"' : '') . '>' .
  145. JText::_('JLIB_RULES_ALLOWED') . '</option>';
  146. $html[] = '<option value="0"' . ($assetRule === false ? ' selected="selected"' : '') . '>' .
  147. JText::_('JLIB_RULES_DENIED') . '</option>';
  148. $html[] = '</select>&nbsp; ';
  149. // If this asset's rule is allowed, but the inherited rule is deny, we have a conflict.
  150. if (($assetRule === true) && ($inheritedRule === false)) {
  151. $html[] = JText::_('JLIB_RULES_CONFLICT');
  152. }
  153. $html[] = '</td>';
  154. // Build the Calculated Settings column.
  155. // The inherited settings column is not displayed for the root group in global configuration.
  156. if ($canCalculateSettings) {
  157. $html[] = '<td headers="global_th' . $group->value . '">';
  158. // This is where we show the current effective settings considering currrent group, path and cascade.
  159. // Check whether this is a component or global. Change the text slightly.
  160. if (JAccess::checkGroup($group->value, 'core.admin') !== true)
  161. {
  162. if ($inheritedRule === null) {
  163. $html[] = '<span class="icon-16-unset">'.
  164. JText::_('JLIB_RULES_NOT_ALLOWED').'</span>';
  165. }
  166. else if ($inheritedRule === true)
  167. {
  168. $html[] = '<span class="icon-16-allowed">'.
  169. JText::_('JLIB_RULES_ALLOWED').'</span>';
  170. }
  171. else if ($inheritedRule === false) {
  172. if ($assetRule === false) {
  173. $html[] = '<span class="icon-16-denied">'.
  174. JText::_('JLIB_RULES_NOT_ALLOWED').'</span>';
  175. }
  176. else {
  177. $html[] = '<span class="icon-16-denied"><span class="icon-16-locked">'.
  178. JText::_('JLIB_RULES_NOT_ALLOWED_LOCKED').'</span></span>';
  179. }
  180. }
  181. //Now handle the groups with core.admin who always inherit an allow.
  182. }
  183. else if (!empty($component)) {
  184. $html[] = '<span class="icon-16-allowed"><span class="icon-16-locked">'.
  185. JText::_('JLIB_RULES_ALLOWED_ADMIN').'</span></span>';
  186. }
  187. else {
  188. // Special handling for groups that have global admin because they can't be denied.
  189. // The admin rights can be changed.
  190. if ($action->name === 'core.admin') {
  191. $html[] = '<span class="icon-16-allowed">'.
  192. JText::_('JLIB_RULES_ALLOWED').'</span>';
  193. }
  194. elseif ($inheritedRule === false) {
  195. // Other actions cannot be changed.
  196. $html[] = '<span class="icon-16-denied"><span class="icon-16-locked">'.
  197. JText::_('JLIB_RULES_NOT_ALLOWED_ADMIN_CONFLICT').'</span></span>';
  198. }
  199. else {
  200. $html[] = '<span class="icon-16-allowed"><span class="icon-16-locked">'.
  201. JText::_('JLIB_RULES_ALLOWED_ADMIN').'</span></span>';
  202. }
  203. }
  204. $html[] = '</td>';
  205. }
  206. $html[] = '</tr>';
  207. }
  208. $html[] = '</tbody>';
  209. $html[] = '</table></div>';
  210. $html[] = JText::_('JLIB_RULES_SETTING_NOTES');
  211. $html[] = '</div></div>';
  212. } // endforeach
  213. $html[] = str_repeat('</li></ul>', $curLevel);
  214. $html[] = '</ul>';
  215. $html[] = '</div>';
  216. $js = "window.addEvent('domready', function(){ new Accordion($$('div#permissions-sliders.pane-sliders .panel h3.jpane-toggler'), $$('div#permissions-sliders.pane-sliders .panel div.jpane-slider'), {onActive: function(toggler, i) {toggler.addClass('jpane-toggler-down');toggler.removeClass('jpane-toggler');Cookie.write('jpanesliders_permissions-sliders".$component."',$$('div#permissions-sliders.pane-sliders .panel h3').indexOf(toggler));},onBackground: function(toggler, i) {toggler.addClass('jpane-toggler');toggler.removeClass('jpane-toggler-down');},duration: 300,display: ".JRequest::getInt('jpanesliders_permissions-sliders'.$component, 0, 'cookie').",show: ".JRequest::getInt('jpanesliders_permissions-sliders'.$component, 0, 'cookie').",opacity: false}); });";
  217. JFactory::getDocument()->addScriptDeclaration($js);
  218. return implode("\n", $html);
  219. }
  220. /**
  221. * Get a list of the user groups.
  222. *
  223. * @return array
  224. * @since 1.6
  225. */
  226. protected function getUserGroups()
  227. {
  228. // Initialise variables.
  229. $db = JFactory::getDBO();
  230. $query = $db->getQuery(true)
  231. ->select('a.id AS value, a.title AS text, COUNT(DISTINCT b.id) AS level, a.parent_id')
  232. ->from('#__usergroups AS a')
  233. ->leftJoin('`#__usergroups` AS b ON a.lft > b.lft AND a.rgt < b.rgt')
  234. ->group('a.id')
  235. ->order('a.lft ASC');
  236. $db->setQuery($query);
  237. $options = $db->loadObjectList();
  238. return $options;
  239. }
  240. }