Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
rules.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Form
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 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Form Rule class for the Joomla Platform.
14  *
15  * @package Joomla.Platform
16  * @subpackage Form
17  * @since 11.1
18  */
20 {
21  /**
22  * Method to test the value.
23  *
24  * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
25  * @param mixed $value The form field value to validate.
26  * @param string $group The field name group control value. This acts as as an array container for the field.
27  * For example if the field has name="foo" and the group value is set to "bar" then the
28  * full field name would end up being "bar[foo]".
29  * @param JRegistry $input An optional JRegistry object with the entire data set to validate against the entire form.
30  * @param JForm $form The form object for which the field is being tested.
31  *
32  * @return boolean True if the value is valid, false otherwise.
33  *
34  * @since 11.1
35  */
36  public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
37  {
38  // Get the possible field actions and the ones posted to validate them.
39  $fieldActions = self::getFieldActions($element);
40  $valueActions = self::getValueActions($value);
41 
42  // Make sure that all posted actions are in the list of possible actions for the field.
43  foreach ($valueActions as $action)
44  {
45  if (!in_array($action, $fieldActions))
46  {
47  return false;
48  }
49  }
50 
51  return true;
52  }
53 
54  /**
55  * Method to get the list of permission action names from the form field value.
56  *
57  * @param mixed $value The form field value to validate.
58  *
59  * @return array A list of permission action names from the form field value.
60  *
61  * @since 11.1
62  */
63  protected function getValueActions($value)
64  {
65  $actions = array();
66 
67  // Iterate over the asset actions and add to the actions.
68  foreach ((array) $value as $name => $rules)
69  {
70  $actions[] = $name;
71  }
72 
73  return $actions;
74  }
75 
76  /**
77  * Method to get the list of possible permission action names for the form field.
78  *
79  * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the
80  * form field object.
81  *
82  * @return array A list of permission action names from the form field element definition.
83  *
84  * @since 11.1
85  */
86  protected function getFieldActions(SimpleXMLElement $element)
87  {
88  $actions = array();
89 
90  // Initialise some field attributes.
91  $section = $element['section'] ? (string) $element['section'] : '';
92  $component = $element['component'] ? (string) $element['component'] : '';
93 
94  // Get the asset actions for the element.
95  $elActions = JAccess::getActions($component, $section);
96 
97  // Iterate over the asset actions and add to the actions.
98  foreach ($elActions as $item)
99  {
100  $actions[] = $item->name;
101  }
102 
103  // Iterate over the children and add to the actions.
104  foreach ($element->children() as $el)
105  {
106  if ($el->getName() == 'action')
107  {
108  $actions[] = (string) $el['name'];
109  }
110  }
111 
112  return $actions;
113  }
114 }