1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
/**
|
3 |
|
|
* @file
|
4 |
|
|
* Variable API module. Form library.
|
5 |
|
|
*/
|
6 |
|
|
|
7 |
|
|
/**
|
8 |
|
|
* Build form element for a variable
|
9 |
|
|
*/
|
10 |
|
|
function variable_form_element($variable, $options = array()) {
|
11 |
|
|
$variable = variable_build($variable);
|
12 |
|
|
$variable = variable_build_options($variable, $options);
|
13 |
|
|
if (!empty($variable['element callback'])) {
|
14 |
|
|
$element = call_user_func($variable['element callback'], $variable, $options);
|
15 |
|
|
}
|
16 |
|
|
elseif (isset($variable['options'])) {
|
17 |
|
|
$element = variable_form_element_options($variable, $options);
|
18 |
|
|
}
|
19 |
|
|
else {
|
20 |
|
|
$element = variable_form_element_default($variable, $options);
|
21 |
|
|
}
|
22 |
|
|
if (!empty($variable['validate callback'])) {
|
23 |
|
|
$element['#element_validate'][] = 'variable_form_element_validate';
|
24 |
|
|
}
|
25 |
|
|
if (!empty($options['form parents'])) {
|
26 |
|
|
$element['#parents'] = $options['form parents'];
|
27 |
|
|
$element['#parents'][] = $variable['name'];
|
28 |
|
|
}
|
29 |
|
|
$element += array('#access' => variable_access($variable));
|
30 |
|
|
if (!empty($variable['required'])) {
|
31 |
|
|
$element += array('#required' => TRUE);
|
32 |
|
|
}
|
33 |
|
|
// Add variable data to element so we can use it later fo validation, etc..
|
34 |
|
|
$element['#variable'] = $variable;
|
35 |
|
|
return $element;
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
/**
|
39 |
|
|
* Build array form element
|
40 |
|
|
*/
|
41 |
|
|
function variable_form_element_array($variable, $options = array()) {
|
42 |
|
|
// This will be possibly a fieldset with tree value
|
43 |
|
|
$element = variable_form_element_default($variable, $options);
|
44 |
|
|
// We may have a multiple element base that will default to plain textfield
|
45 |
|
|
$item = $variable['repeat'];
|
46 |
|
|
$value = variable_get_value($variable, $options);
|
47 |
|
|
// Compile names and defaults for all elements
|
48 |
|
|
$names = $defaults = array();
|
49 |
|
|
if (!empty($variable['multiple'])) {
|
50 |
|
|
// If we've got a multiple value, it will be an array with known elements
|
51 |
|
|
$names = $variable['multiple'];
|
52 |
|
|
}
|
53 |
|
|
else {
|
54 |
|
|
// Array with unknown elements, we add an element for each existing one
|
55 |
|
|
$names = $value ? array_combine(array_keys($value), array_keys($value)) : array();
|
56 |
|
|
}
|
57 |
|
|
// Now build elements with the right names
|
58 |
|
|
foreach ($names as $key => $title) {
|
59 |
|
|
if (isset($value[$key]) && is_array($value[$key])) {
|
60 |
|
|
// This element is an array, we cannot edit it but we need to add it to the form
|
61 |
|
|
$element[$key] = array('#type' => 'value', '#value' => $value[$key]);
|
62 |
|
|
$element['variable_element_array_' . $key] = array('#type' => 'item', '#title' => $title, '#markup' => variable_format_array($value[$key]));
|
63 |
|
|
}
|
64 |
|
|
else {
|
65 |
|
|
$element[$key] = $item['element'] + array('#title' => $title, '#default_value' => isset($value[$key]) ? $value[$key] : '');
|
66 |
|
|
}
|
67 |
|
|
}
|
68 |
|
|
return $element;
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
/**
|
72 |
|
|
* Build multiple form element
|
73 |
|
|
*/
|
74 |
|
|
function variable_form_element_multiple($variable, $options = array()) {
|
75 |
|
|
$variable += array('element' => array(), 'title' => '', 'description' => '');
|
76 |
|
|
$element = $variable['element'] + array(
|
77 |
|
|
'#type' => 'fieldset',
|
78 |
|
|
'#title' => $variable['title'],
|
79 |
|
|
'#description' => $variable['description'],
|
80 |
|
|
);
|
81 |
|
|
foreach ($variable['children'] as $name => $item) {
|
82 |
|
|
$element[$name] = variable_form_element($item, $options);
|
83 |
|
|
}
|
84 |
|
|
return $element;
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
/**
|
88 |
|
|
* Build default form element
|
89 |
|
|
*/
|
90 |
|
|
function variable_form_element_default($variable, $options = array()) {
|
91 |
|
|
$variable += array('element' => array(), 'title' => '', 'description' => '');
|
92 |
|
|
$type = variable_get_type($variable['type']) + array('element' => array());
|
93 |
|
|
$element = $variable['element'] + array(
|
94 |
|
|
'#title' => $variable['title'],
|
95 |
|
|
'#description' => $variable['description'],
|
96 |
|
|
) + $type['element'];
|
97 |
|
|
$value = variable_get_value($variable, $options);
|
98 |
|
|
if (isset($value)) {
|
99 |
|
|
$element['#default_value'] = $value;
|
100 |
|
|
}
|
101 |
|
|
return $element;
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
/**
|
105 |
|
|
* Build form element for unknown variable.
|
106 |
|
|
*
|
107 |
|
|
* This is not an editable form element but a form item.
|
108 |
|
|
*/
|
109 |
|
|
function variable_form_element_unknown($variable, $options = array()) {
|
110 |
|
|
$variable += array('element' => array(), 'title' => '', 'description' => '');
|
111 |
|
|
$type = variable_get_type($variable['type']) + array('element' => array());
|
112 |
|
|
$element = $variable['element'] + array(
|
113 |
|
|
'#title' => $variable['title'],
|
114 |
|
|
'#description' => $variable['description'],
|
115 |
|
|
'#markup' => variable_format_value($variable, $options),
|
116 |
|
|
) + $type['element'];
|
117 |
|
|
return $element;
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
/**
|
121 |
|
|
* Build form element for text_format variable.
|
122 |
|
|
*/
|
123 |
|
|
function variable_form_element_text_format($variable, $options = array()) {
|
124 |
|
|
$variable += array('element' => array(), 'title' => '', 'description' => '');
|
125 |
|
|
$type = variable_get_type($variable['type']) + array('element' => array());
|
126 |
|
|
$element = $variable['element'] + array(
|
127 |
|
|
'#title' => $variable['title'],
|
128 |
|
|
'#description' => $variable['description'],
|
129 |
|
|
) + $type['element'];
|
130 |
|
|
$value = variable_get_value($variable, $options);
|
131 |
|
|
if (isset($value) && is_array($value)) {
|
132 |
|
|
if (isset($value['value'])) {
|
133 |
|
|
$element['#default_value'] = $value['value'];
|
134 |
|
|
}
|
135 |
|
|
if (isset($value['format'])) {
|
136 |
|
|
$element['#format'] = $value['format'];
|
137 |
|
|
}
|
138 |
|
|
}
|
139 |
|
|
return $element;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/**
|
143 |
|
|
* Build options variables
|
144 |
|
|
*/
|
145 |
|
|
function variable_form_element_options($variable, $options = array()) {
|
146 |
|
|
$element = variable_form_element_default($variable, $options);
|
147 |
|
|
$element['#options'] = $variable['options'];
|
148 |
|
|
// Depending on the number of options this may be radios or a drop-down.
|
149 |
|
|
// However if there are nested options (an option is an array) it should be always a drop-down.
|
150 |
|
|
if (empty($element['#type'])) {
|
151 |
|
|
$element['#type'] = count($variable['options']) > 4 || array_filter(array_map('is_array', $variable['options'])) ? 'select' : 'radios';
|
152 |
|
|
}
|
153 |
|
|
return $element;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
/**
|
157 |
c12e7e6a
|
Assos Assos
|
* Execute submit callbacks for variables in form.
|
158 |
85ad3d82
|
Assos Assos
|
*/
|
159 |
c12e7e6a
|
Assos Assos
|
function variable_form_submit_callback($form, &$form_state) {
|
160 |
|
|
if (isset($form['#variable_edit_form'])) {
|
161 |
|
|
// This may contain some realm options.
|
162 |
|
|
$options = isset($form['#variable_options']) ? $form['#variable_options'] : array();
|
163 |
|
|
foreach ($form['#variable_edit_form'] as $name) {
|
164 |
|
|
$variable = variable_get_info($name);
|
165 |
|
|
if ($variable && isset($variable['submit callback'])) {
|
166 |
|
|
variable_include($variable);
|
167 |
|
|
$variable['submit callback']($variable, $options, $form, $form_state);
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
85ad3d82
|
Assos Assos
|
}
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
/**
|
174 |
|
|
* Form to select variables
|
175 |
|
|
*/
|
176 |
|
|
function theme_variable_table_select($variables) {
|
177 |
|
|
$element = $variables['element'];
|
178 |
|
|
$header = isset($element['#header']) ? $element['#header'] : array('element' => '', 'title' => t('Name'), 'description' => t('Description'));
|
179 |
|
|
$fields = array_keys($header);
|
180 |
|
|
$rows = array();
|
181 |
|
|
foreach (element_children($element) as $name) {
|
182 |
|
|
if (isset($element[$name]['#variable_name'])) {
|
183 |
|
|
$variable_name = $element[$name]['#variable_name'];
|
184 |
|
|
}
|
185 |
|
|
else {
|
186 |
|
|
$variable_name = str_replace(array('<', '>'), array('[', ']'), $name);
|
187 |
|
|
}
|
188 |
|
|
$variable = _variable_variable($variable_name);
|
189 |
|
|
$row = array();
|
190 |
|
|
foreach ($fields as $field) {
|
191 |
|
|
if ($field == 'element') {
|
192 |
|
|
$row[] = drupal_render($element[$name]);
|
193 |
|
|
}
|
194 |
|
|
else {
|
195 |
|
|
$row[] = isset($variable[$field]) ? $variable[$field] : '';
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
|
|
$rows[] = $row;
|
199 |
|
|
}
|
200 |
|
|
// Add a "Select all" checkbox.
|
201 |
|
|
drupal_add_js('misc/tableselect.js');
|
202 |
|
|
$header['element'] = array('class' => array('select-all'));
|
203 |
|
|
|
204 |
|
|
return theme('table', array('header' => array_values($header), 'rows' => $rows));
|
205 |
|
|
} |