PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/source/libraries/projectfork/form/rules/groupaccess.php

https://github.com/projectfork/Projectfork
PHP | 140 lines | 73 code | 18 blank | 49 comment | 6 complexity | 725538e1e494c24ce5c4b34842b34f33 MD5 | raw file
  1. <?php
  2. /**
  3. * @package pkg_projectfork
  4. * @subpackage lib_projectfork
  5. *
  6. * @author Tobias Kuhn (eaxs)
  7. * @copyright Copyright (C) 2006-2013 Tobias Kuhn. All rights reserved.
  8. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL, see LICENSE.txt
  9. */
  10. defined('_JEXEC') or die();
  11. if (version_compare(JVERSION, '3.0.0', '>=')) {
  12. class PFJFormRuleCompat extends JFormRule
  13. {
  14. public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
  15. {
  16. return $this->testCompat($element, $value, $group = null, $input = null, $form = null);
  17. }
  18. public function testCompat($element, $value, $group = null, $input = null, $form = null)
  19. {
  20. return false;
  21. }
  22. }
  23. }
  24. else {
  25. class PFJFormRuleCompat extends JFormRule
  26. {
  27. public function test(&$element, $value, $group = null, &$input = null, &$form = null)
  28. {
  29. return $this->testCompat($element, $value, $group = null, $input = null, $form = null);
  30. }
  31. public function testCompat($element, $value, $group = null, $input = null, $form = null)
  32. {
  33. return false;
  34. }
  35. }
  36. }
  37. /**
  38. * Form Rule class for the group access field.
  39. *
  40. */
  41. class JFormRuleGroupAccess extends PFJFormRuleCompat
  42. {
  43. /**
  44. * Method to test the value.
  45. *
  46. * @param simplexmlelement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
  47. * @param mixed $value The form field value to validate.
  48. * @param string $group The field name group control value.
  49. * @param object $input An optional JRegistry object with the entire data set to validate against the entire form.
  50. * @param object $form The form object for which the field is being tested.
  51. *
  52. * @return boolean True if the value is valid, false otherwise.
  53. *
  54. * @throws jexception on invalid rule.
  55. */
  56. public function testCompat($element, $value, $group = null, $input = null, $form = null)
  57. {
  58. // Get the possible field actions and the ones posted to validate them.
  59. $fieldActions = self::getFieldActions($element);
  60. $valueActions = self::getValueActions($value);
  61. // Make sure that all posted actions are in the list of possible actions for the field.
  62. foreach ($valueActions as $action)
  63. {
  64. if (!in_array($action, $fieldActions))
  65. {
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. /**
  72. * Method to get the list of permission action names from the form field value.
  73. *
  74. * @param mixed $value The form field value to validate.
  75. *
  76. * @return array A list of permission action names from the form field value.
  77. */
  78. protected function getValueActions($value)
  79. {
  80. // Initialise variables.
  81. $actions = array();
  82. // Iterate over the asset actions and add to the actions.
  83. foreach ((array) $value as $name => $rules)
  84. {
  85. if (is_numeric($name)) continue;
  86. $actions[] = $name;
  87. }
  88. return $actions;
  89. }
  90. /**
  91. * Method to get the list of possible permission action names for the form field.
  92. *
  93. * @param object $element The SimpleXMLElement object representing the <field /> tag for the
  94. * form field object.
  95. *
  96. * @return array A list of permission action names from the form field element definition.
  97. */
  98. protected function getFieldActions($element)
  99. {
  100. // Initialise variables.
  101. $actions = array();
  102. // Initialise some field attributes.
  103. $section = $element['section'] ? (string) $element['section'] : '';
  104. $component = $element['component'] ? (string) $element['component'] : 'com_projectfork';
  105. // Get the asset actions for the element.
  106. $elActions = JAccess::getActions($component, $section);
  107. // Iterate over the asset actions and add to the actions.
  108. foreach ($elActions as $item)
  109. {
  110. $actions[] = $item->name;
  111. }
  112. // Iterate over the children and add to the actions.
  113. foreach ($element->children() as $el)
  114. {
  115. if ($el->getName() == 'action')
  116. {
  117. $actions[] = (string) $el['name'];
  118. }
  119. }
  120. return $actions;
  121. }
  122. }