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

/libraries/joomla/html/rules.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 225 lines | 137 code | 27 blank | 61 comment | 3 complexity | 1b1994334daedae68c37671c48c22c60 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage HTML
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 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. * Extended Utility class for all HTML drawing classes.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage HTML
  15. * @since 11.1
  16. */
  17. abstract class JHtmlRules
  18. {
  19. /**
  20. * Creates the HTML for the permissions widget
  21. *
  22. * @param array $actions Array of action objects
  23. * @param integer $assetId Id of a specific asset to create a widget for.
  24. * @param integer $parent Id of the parent of the asset
  25. * @param string $control The form control
  26. * @param string $idPrefix Prefix for the ids assigned to specific action-group pairs
  27. *
  28. * @return string HTML for the permissions widget
  29. *
  30. * @since 11.1
  31. *
  32. * @see JAccess
  33. * @see JFormFieldRules
  34. */
  35. public static function assetFormWidget($actions, $assetId = null, $parent = null, $control = 'jform[rules]', $idPrefix = 'jform_rules')
  36. {
  37. $images = self::_getImagesArray();
  38. // Get the user groups.
  39. $groups = self::_getUserGroups();
  40. // Get the incoming inherited rules as well as the asset specific rules.
  41. $inheriting = JAccess::getAssetRules($parent ? $parent : self::_getParentAssetId($assetId), true);
  42. $inherited = JAccess::getAssetRules($assetId, true);
  43. $rules = JAccess::getAssetRules($assetId);
  44. $html = array();
  45. $html[] = '<div class="acl-options">';
  46. $html[] = JHtml::_('tabs.start', 'acl-rules-' . $assetId, array('useCookie' => 1));
  47. $html[] = JHtml::_('tabs.panel', JText::_('JLIB_HTML_ACCESS_SUMMARY'), 'summary');
  48. $html[] = ' <p>' . JText::_('JLIB_HTML_ACCESS_SUMMARY_DESC') . '</p>';
  49. $html[] = ' <table class="aclsummary-table" summary="' . JText::_('JLIB_HTML_ACCESS_SUMMARY_DESC') . '">';
  50. $html[] = ' <caption>' . JText::_('JLIB_HTML_ACCESS_SUMMARY_DESC_CAPTION') . '</caption>';
  51. $html[] = ' <tr>';
  52. $html[] = ' <th class="col1 hidelabeltxt">' . JText::_('JLIB_RULES_GROUPS') . '</th>';
  53. foreach ($actions as $i => $action)
  54. {
  55. $html[] = ' <th class="col' . ($i + 2) . '">' . JText::_($action->title) . '</th>';
  56. }
  57. $html[] = ' </tr>';
  58. foreach ($groups as $i => $group)
  59. {
  60. $html[] = ' <tr class="row' . ($i % 2) . '">';
  61. $html[] = ' <td class="col1">' . $group->text . '</td>';
  62. foreach ($actions as $j => $action)
  63. {
  64. $html[] = ' <td class="col' . ($j + 2) . '">'
  65. . ($assetId ? ($inherited->allow($action->name, $group->identities) ? $images['allow'] : $images['deny'])
  66. : ($inheriting->allow($action->name, $group->identities) ? $images['allow'] : $images['deny'])) . '</td>';
  67. }
  68. $html[] = ' </tr>';
  69. }
  70. $html[] = ' </table>';
  71. foreach ($actions as $action)
  72. {
  73. $actionTitle = JText::_($action->title);
  74. $actionDesc = JText::_($action->description);
  75. $html[] = JHtml::_('tabs.panel', $actionTitle, $action->name);
  76. $html[] = ' <p>' . $actionDesc . '</p>';
  77. $html[] = ' <table class="aclmodify-table" summary="' . strip_tags($actionDesc) . '">';
  78. $html[] = ' <caption>' . JText::_('JLIB_HTML_ACCESS_MODIFY_DESC_CAPTION_ACL') . ' ' . $actionTitle . ' '
  79. . JText::_('JLIB_HTML_ACCESS_MODIFY_DESC_CAPTION_TABLE') . '</caption>';
  80. $html[] = ' <tr>';
  81. $html[] = ' <th class="col1 hidelabeltxt">' . JText::_('JLIB_RULES_GROUP') . '</th>';
  82. $html[] = ' <th class="col2">' . JText::_('JLIB_RULES_INHERIT') . '</th>';
  83. $html[] = ' <th class="col3 hidelabeltxt">' . JText::_('JMODIFY') . '</th>';
  84. $html[] = ' <th class="col4">' . JText::_('JCURRENT') . '</th>';
  85. $html[] = ' </tr>';
  86. foreach ($groups as $i => $group)
  87. {
  88. $selected = $rules->allow($action->name, $group->value);
  89. $html[] = ' <tr class="row' . ($i % 2) . '">';
  90. $html[] = ' <td class="col1">' . $group->text . '</td>';
  91. $html[] = ' <td class="col2">'
  92. . ($inheriting->allow($action->name, $group->identities) ? $images['allow-i'] : $images['deny-i']) . '</td>';
  93. $html[] = ' <td class="col3">';
  94. $html[] = ' <select id="' . $idPrefix . '_' . $action->name . '_' . $group->value
  95. . '" class="inputbox" size="1" name="' . $control . '[' . $action->name . '][' . $group->value . ']" title="'
  96. . JText::sprintf('JLIB_RULES_SELECT_ALLOW_DENY_GROUP', $actionTitle, $group->text) . '">';
  97. $html[] = ' <option value=""' . ($selected === null ? ' selected="selected"' : '') . '>'
  98. . JText::_('JLIB_RULES_INHERIT') . '</option>';
  99. $html[] = ' <option value="1"' . ($selected === true ? ' selected="selected"' : '') . '>'
  100. . JText::_('JLIB_RULES_ALLOWED') . '</option>';
  101. $html[] = ' <option value="0"' . ($selected === false ? ' selected="selected"' : '') . '>'
  102. . JText::_('JLIB_RULES_DENIED') . '</option>';
  103. $html[] = ' </select>';
  104. $html[] = ' </td>';
  105. $html[] = ' <td class="col4">'
  106. . ($assetId ? ($inherited->allow($action->name, $group->identities) ? $images['allow'] : $images['deny'])
  107. : ($inheriting->allow($action->name, $group->identities) ? $images['allow'] : $images['deny'])) . '</td>';
  108. $html[] = ' </tr>';
  109. }
  110. $html[] = ' </table>';
  111. }
  112. $html[] = JHtml::_('tabs.end');
  113. // Build the footer with legend and special purpose buttons.
  114. $html[] = ' <div class="clr"></div>';
  115. $html[] = ' <ul class="acllegend fltlft">';
  116. $html[] = ' <li class="acl-allowed">' . JText::_('JLIB_RULES_ALLOWED') . '</li>';
  117. $html[] = ' <li class="acl-denied">' . JText::_('JLIB_RULES_DENIED') . '</li>';
  118. $html[] = ' </ul>';
  119. $html[] = '</div>';
  120. return implode("\n", $html);
  121. }
  122. /**
  123. * Get the id of the parent asset
  124. *
  125. * @param integer $assetId The asset for which the parentid will be returned
  126. *
  127. * @return integer The id of the parent asset
  128. *
  129. * @since 11.1
  130. */
  131. protected static function _getParentAssetId($assetId)
  132. {
  133. // Get a database object.
  134. $db = JFactory::getDbo();
  135. $query = $db->getQuery(true);
  136. // Get the user groups from the database.
  137. $query->select($db->quoteName('parent_id'))
  138. ->from($db->quoteName('#__assets'))
  139. ->where($db->quoteName('id') . ' = ' . (int) $assetId);
  140. $db->setQuery($query);
  141. return (int) $db->loadResult();
  142. }
  143. /**
  144. * Get the user groups
  145. *
  146. * @return array Array of user groups
  147. *
  148. * @since 11.1
  149. */
  150. protected static function _getUserGroups()
  151. {
  152. // Get a database object.
  153. $db = JFactory::getDbo();
  154. // Get the user groups from the database.
  155. $db->setQuery(
  156. 'SELECT a.id AS value, a.title AS text, b.id as parent'
  157. . ' FROM #__usergroups AS a LEFT JOIN #__usergroups AS b ON a.lft >= b.lft AND a.rgt <= b.rgt'
  158. . ' ORDER BY a.lft ASC, b.lft ASC'
  159. );
  160. $result = $db->loadObjectList();
  161. $options = array();
  162. // Pre-compute additional values.
  163. foreach ($result as $option)
  164. {
  165. $end = end($options);
  166. if ($end === false || $end->value != $option->value)
  167. {
  168. $end = $option;
  169. $end->level = 0;
  170. $options[] = $end;
  171. }
  172. else
  173. {
  174. $end->level++;
  175. }
  176. $end->identities[] = $option->parent;
  177. }
  178. return $options;
  179. }
  180. /**
  181. * Get the array of images associate with specific permissions
  182. *
  183. * @return array An associative array of permissions and images
  184. *
  185. * @since 11.1
  186. */
  187. protected static function _getImagesArray()
  188. {
  189. $images['allow-l'] = '<label class="icon-16-allow" title="' . JText::_('JLIB_RULES_ALLOWED') . '">' . JText::_('JLIB_RULES_ALLOWED')
  190. . '</label>';
  191. $images['deny-l'] = '<label class="icon-16-deny" title="' . JText::_('JLIB_RULES_DENIED') . '">' . JText::_('JLIB_RULES_DENIED') . '</label>';
  192. $images['allow'] = '<a class="icon-16-allow" title="' . JText::_('JLIB_RULES_ALLOWED') . '"> </a>';
  193. $images['deny'] = '<a class="icon-16-deny" title="' . JText::_('JLIB_RULES_DENIED') . '"> </a>';
  194. $images['allow-i'] = '<a class="icon-16-allowinactive" title="' . JText::_('JRULE_ALLOWED_INHERITED') . '"> </a>';
  195. $images['deny-i'] = '<a class="icon-16-denyinactive" title="' . JText::_('JRULE_DENIED_INHERITED') . '"> </a>';
  196. return $images;
  197. }
  198. }