/[drupal]/contributions/modules/formupdater/formupdater.module
ViewVC logotype

Contents of /contributions/modules/formupdater/formupdater.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.11 - (show annotations) (download) (as text)
Sun Dec 28 05:06:21 2008 UTC (11 months ago) by deekayen
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +1 -1 lines
File MIME type: text/x-php
fix another undefined var PHP notice
1 <?php
2 // $Id: formupdater.module,v 1.9 2008/07/23 02:21:21 deekayen Exp $
3
4 /**
5 * @file
6 * Regex engine to help with module <=4.6 upgrades.
7 */
8
9 /**
10 * Implementation of hook_help().
11 */
12 function formupdater_help($path, $arg) {
13 switch ($path) {
14 case 'formupdater':
15 return t('<p>Form Updater will search through your old Drupal code to find calls to old form functions (such as form_textfield, form_radios, etc). It will then reformat these functions as arrays for use with the new <a href="http://api.drupal.org/api/file/developer/topics/forms_api_reference.html">Drupal Forms API</a>.</p>');
16 }
17 }
18
19 /**
20 * Implementation of hook_menu().
21 */
22 function formupdater_menu() {
23 $items = array();
24 $items['formupdater'] = array(
25 'title' => 'Form updater',
26 'access callback' => 'user_access',
27 'access arguments' => array('access content'),
28 'page callback' => 'drupal_get_form',
29 'page arguments' => array('formupdater_form')
30 );
31 return $items;
32 }
33
34 function formupdater_form() {
35 $form = array();
36 $form['code'] = array(
37 '#type' => 'textarea',
38 '#title' => t('Drupal PHP code containing old form function calls'),
39 '#rows' => 20
40 );
41 $form['submit'] = array(
42 '#type' => 'submit',
43 '#value' => t('Process code')
44 );
45 return $form;
46 }
47
48 function formupdater_form_submit($form, &$form_state) {
49 $code = $form_state['values']['code'];
50 $regex = '/\s(form_(.*?)|form())\(([\w\W]*?)\);/i';
51 preg_match_all($regex, $code, $matches, PREG_SET_ORDER);
52 $i = 1;
53 if (is_array($matches)) {
54 foreach ($matches as $match) {
55 //print($match[1]);
56 if ($array = formupdater_convert($match[2], $match[4])) {
57 $note = $array['note'];
58 unset($array['note']);
59 $form['x_'. $i] = array(
60 '#type' => 'fieldset',
61 '#title' => $match[2] ? 'form_'. $match[2] .'()' : 'form()',
62 '#collapsible' => true
63 );
64 $form['x_'. $i]['before'] = array(
65 '#type' => 'item',
66 '#title' => t('Drupal 4.6'),
67 '#value' => htmlentities($match[0]),
68 '#weight' => 0
69 );
70 foreach ($array as $name => $args) {
71 if (drupal_strlen($match[2])) {
72 $code = "\$form[". str_replace('][', "']['", $name) ."] = array(\n ";
73 $argsout = array();
74 foreach ($args as $key => $val) {
75 $argsout[] = "'$key' => $val";
76 }
77 $code .= implode(",\n ", $argsout);
78 $code .= ",\n);";
79 }
80 else {
81 // this is a form() call
82 $code = '';
83 foreach ($args as $key => $val) {
84 if ($key != '#type' && $key != '#title') {
85 $code .= "\$form['$key'] = $val;\n";
86 }
87 }
88 $code .= "\$output = drupal_render(\$form);";
89 }
90 }
91 $lines = substr_count($code, "\n") + 1;
92 $form['x_'. $i]['after'] = array(
93 '#type' => 'textarea',
94 '#title' => t('Forms API'),
95 '#rows' => $lines,
96 '#value' => $code,
97 '#weight' => 1
98 );
99 /*
100 //notes for different types
101 $d = '';
102 switch($match[1]){
103 case '':
104 case 'group':
105 $d = t('**This code may need some extra work.');
106 break;
107 }
108 */
109 if (!empty($note)) {
110 $form['x_'. $i]['after']['#description'] = $note;
111 }
112 $i++;
113 }
114 }
115 }
116 $output = drupal_render($form);
117 print theme('page', $output);
118 exit;
119 }
120
121 function formupdater_convert($type, $args) {
122 $type = trim($type);
123
124 //temporarily replace commas in sub-arguments
125 //split by parenthasis
126 $parensplit = preg_split('/([\(\)])/', $args, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
127 $open = 0;
128 $reargs = '';
129 foreach ((array)$parensplit as $parenchunk) {
130 switch ($parenchunk) {
131 case '(':
132 $open++;
133 $reargs .= $parenchunk;
134 break;
135 case ')':
136 $open--;
137 $reargs .= $parenchunk;
138 break;
139 default:
140 if ($open > 0) {
141 // if we're inside a set of parenthasis, "protect" the commas
142 $parenchunk = str_replace(',', '^%^%^', $parenchunk);
143 }
144 $reargs .= $parenchunk;
145 }
146 }
147
148 // make args into an array
149 $args = explode(',', $reargs);
150 $trimedargs = array();
151 foreach ((array)$args as $arg) {
152 // trim out white space
153 // and put commas back in
154 $trimedargs[] = trim(str_replace('^%^%^', ',', $arg));
155 }
156 $args = $trimedargs;
157
158 $a = formupdater_get_argnames($type);
159 $note = isset($a['note']) ? $a['note'] : '';
160 unset($a['note']);
161 //var_dump($a);
162
163 if (!$a['na']) {
164 $form['#type'] = "'". $a['#type'] ."'";
165 unset($a['na']);
166 unset($a['#type']);
167 ksort($a);
168 foreach ($a as $key => $value) {
169 if (isset($args[$key])) {
170 $form[$value] = $args[$key];
171 }
172 }
173
174 $name = isset($form['#name']) ? $form['#name'] : '';
175 unset($form['#name']);
176 $return[$name] = $form;
177 $return['note'] = $note;
178 }
179 else {
180 $return = FALSE;
181 }
182 return $return;
183 }
184
185 function formupdater_get_argnames($type) {
186 $a = array();
187 switch ($type) {
188 case '':
189 // no type = form();
190 $a = array(1 => '#method', 2 => '#action', 3 => '#attributes');
191 $a['note'] = t('Replace your_form_id with an appropriate id.');
192 break;
193 case 'autocomplete':
194 // form_autocomplete($title, $name, $value, $size, $maxlength, $callback_path, $description = NULL, $attributes = NULL, $required = FALSE)
195 $a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#size', 4 => '#maxlength', 5 => '#autocomplete_path', 6 => '#description', 7 => '#attributes', 8 => '#required');
196 $type = 'textfield';
197 break;
198 case 'button':
199 // form_button($value, $name = 'op', $type = 'submit', $attributes = NULL)
200 $a = array(0 => '#title', 1 => '#name', 2 => '#button_type', 3 => '#attributes');
201 break;
202 case 'checkbox':
203 // form_checkbox($title, $name, $value = 1, $checked = FALSE, $description = NULL, $attributes = NULL, $required = FALSE)
204 $a = array(0 => '#title', 1 => '#name', 2 => '#return_value', 3 => '#default_value', 4 => '#description', 5 => '#attributes', 6 => '#required');
205 break;
206 case 'checkboxes':
207 // form_checkboxes($title, $name, $values, $options, $description = NULL, $attributes = NULL, $required = FALSE)
208 $a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#options', 4 => '#description', 5 => '#attributes', 6 => '#required');
209 $a['note'] = t('4.7 deals with the return values of checkboxes differently. 4.6 returned an array where the values were the checkbox keys. 4.7 returns an array where the key is the checkbox key and the value is the value (by default: checked = 1, unchecked = 0). You will need to adjust your form handler appropriately. You can use array_keys() on the checkbox array to get old-style values.');
210 break;
211 case 'date':
212 // DOES NOT EXIST IN 4.6
213 break;
214 case 'file':
215 // form_file($title, $name, $size, $description = NULL, $required = FALSE)
216 $a = array(0 => '#title', 1 => '#name', 2 => '#size', 3 => '#description', 4 => '#attributes', 5 => '#required');
217 break;
218 case 'get_error':
219 // NOT A FORM FUNCTION *****************************
220 $a['na'] = TRUE;
221 break;
222 case 'get_errors':
223 // NOT A FORM FUNCTION *****************************
224 $a['na'] = TRUE;
225 break;
226 case 'group':
227 // form_group($legend, $group, $description = NULL, $attributes = NULL)
228 // we're ignoring $group, because this info is now stored in one of the subarrays
229 $a = array(0 => '#title', 2 => '#description', 3 => '#attributes');
230 $a['#type'] = 'fieldset';
231 $a['note'] = t('Fieldsets work very differently in 4.7. You will probably need to wrangle with this code some.');
232 // SPECIAL CASE *****************************
233 break;
234 case 'hidden':
235 // form_hidden($name, $value)
236 $a = array(0 => '#name', 1 => '#value');
237 break;
238 case 'item':
239 // form_item($title, $value, $description = NULL, $id = NULL, $required = FALSE, $na = FALSE)
240 $a = array(0 => '#title', 1 => '#default_value', 2 => '#description', 3 => '#id', 4 => '#required');
241 break;
242 case 'password':
243 // form_password($title, $name, $value, $size, $maxlength, $description = NULL, $attributes = NULL, $required = FALSE)
244 $a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#size', 4 => '#maxlength', 5 => '#description');
245 break;
246 case 'radio':
247 // form_radio($title, $name, $value = 1, $checked = FALSE, $description = NULL, $attributes = NULL, $required = FALSE)
248 $a = array(0 => '#title', 1 => '#name', 2 => '#return_value', 3 => '#default_value', 4 => '#description', 5 => '#attributes', 6 => '#required');
249 // note: #return_value ignored?
250 break;
251 case 'radios':
252 // form_radios($title, $name, $value, $options, $description = NULL, $required = FALSE, $attributes = NULL)
253 $a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#options', 4 => '#description', 5 => '#required', 6 => '#attributes');
254 break;
255 case 'select':
256 // function form_select($title, $name, $value, $options, $description = NULL, $extra = 0, $multiple = FALSE, $required = FALSE)
257 $a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#options', 4 => '#description', 5 => '#extra', 6 => '#multiple', 7 => '#required');
258 // NOTE: #extra ignored
259 break;
260 case 'submit':
261 // form_submit($value, $name = 'op', $attributes = NULL)
262 $a = array(0 => '#value', 1 => '#name', 2 => '#attributes');
263 //if (!isset($args[1])){
264 // $args[1] = "'op'";
265 //}
266 break;
267 case 'textarea':
268 // form_textarea($title, $name, $value, $cols, $rows, $description = NULL, $attributes = NULL, $required = FALSE)
269 $a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#cols', 4 => '#rows', 5 => '#description', 6 => '#attributes', 7 => '#required');
270 break;
271 case 'textfield':
272 // form_textfield($title, $name, $value, $size, $maxlength, $description = NULL, $attributes = NULL, $required = FALSE)
273 $a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#size', 4 => '#maxlength', 5 => '#description', 6 => '#attributes', 7 => '#required');
274 break;
275 case 'weight':
276 // form_weight($title = NULL, $name = 'weight', $value = 0, $delta = 10, $description = NULL, $extra = 0)
277 $a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#delta', 4 => '#description', 5 => '#extra');
278 // NOTE: #extra is ignored
279 break;
280 default:
281 $a['na'] = TRUE;
282 }
283 if (!isset($a['#type'])) {
284 $a['#type'] = $type;
285 }
286 $a['na'] = isset($a['na']) && $a['na'] ? TRUE : FALSE;
287 return $a;
288 }

  ViewVC Help
Powered by ViewVC 1.1.2