| 1 |
<?php |
<?php |
| 2 |
// $Id: formupdater.module,v 1.4 2005/11/23 01:15:45 webchick Exp $ |
// $Id: formupdater.module,v 1.5 2005/11/26 01:53:15 jjeff Exp $ |
| 3 |
|
|
| 4 |
function formupdater_help($section){ |
function formupdater_help($section){ |
| 5 |
switch($section){ |
switch($section){ |
| 25 |
$form['input'] = array('#type' => 'textarea', '#title'=> t('Drupal PHP code containing old form function calls'), '#rows' => 20); |
$form['input'] = array('#type' => 'textarea', '#title'=> t('Drupal PHP code containing old form function calls'), '#rows' => 20); |
| 26 |
$form['submit'] = array('#type' => 'submit', '#value' => 'submit'); |
$form['submit'] = array('#type' => 'submit', '#value' => 'submit'); |
| 27 |
$output = drupal_get_form('formupdater_input', $form); |
$output = drupal_get_form('formupdater_input', $form); |
| 28 |
$output .= str_replace(array('$RCSf'.'ile:', ',v', ',', '$Re'.'vision: ', '$Da'.'te: ', '$'), '', '<p style="font-size:x-small">$RCSfile: formupdater.module,v $ version: <b>$Revision: 1.4 $</b>, $Date: 2005/11/23 01:15:45 $</p>'); |
$output .= str_replace(array('$RCSf'.'ile:', ',v', ',', '$Re'.'vision: ', '$Da'.'te: ', '$'), '', '<p style="font-size:x-small">$RCSfile: formupdater.module,v $ version: <b>$Revision: 1.5 $</b>, $Date: 2005/11/26 01:53:15 $</p>'); |
| 29 |
return $output; |
return $output; |
| 30 |
} |
} |
| 31 |
|
|
| 32 |
function formupdater_process($edit){ |
function formupdater_process($edit){ |
| 33 |
//var_dump($form_values); |
//var_dump($form_values); |
| 34 |
$code = $edit['input']; |
$code = $edit['input']; |
| 35 |
$regex = '/form_(.*?)\(([\w\W]*?)\);/i'; |
$regex = '/\s(form_(.*?)|form())\(([\w\W]*?)\);/i'; |
| 36 |
preg_match_all($regex, $code, $matches, PREG_SET_ORDER); |
preg_match_all($regex, $code, $matches, PREG_SET_ORDER); |
| 37 |
$i = 1; |
$i = 1; |
| 38 |
if (is_array($matches)){ |
if (is_array($matches)){ |
| 39 |
foreach ($matches as $match){ |
foreach ($matches as $match){ |
| 40 |
//print($match[1]); |
//print($match[1]); |
| 41 |
if($array = formupdater_convert($match[1], $match[2])) { |
if($array = formupdater_convert($match[2], $match[4])) { |
| 42 |
$form['x_'.$i] = array('#type' => 'fieldset', '#title' => $match[1] ? 'form_'. $match[1].'()' : 'form()'); |
$note = $array['note']; |
| 43 |
|
unset($array['note']); |
| 44 |
|
$form['x_'.$i] = array( |
| 45 |
|
'#type' => 'fieldset', |
| 46 |
|
'#title' => $match[2] ? 'form_'. $match[2].'()' : 'form()', |
| 47 |
|
'#collapsible' => true |
| 48 |
|
); |
| 49 |
$form['x_'.$i]['before'] = array('#type' => 'item', '#title' => t('Drupal 4.6'), '#value' => htmlentities($match[0])); |
$form['x_'.$i]['before'] = array('#type' => 'item', '#title' => t('Drupal 4.6'), '#value' => htmlentities($match[0])); |
| 50 |
foreach ($array as $name => $args){ |
foreach ($array as $name => $args){ |
| 51 |
if(strlen($match[1])){ |
if(strlen($match[2])){ |
| 52 |
$code = "\$form[".str_replace('][', "']['", $name)."] = array(\n "; |
$code = "\$form[".str_replace('][', "']['", $name)."] = array(\n "; |
| 53 |
$argsout = array(); |
$argsout = array(); |
| 54 |
foreach($args as $key => $val){ |
foreach($args as $key => $val){ |
| 55 |
$argsout[] = "'$key' => $val"; |
$argsout[] = "'$key' => $val"; |
| 56 |
} |
} |
| 57 |
$code .= implode(",\n ", $argsout); |
$code .= implode(",\n ", $argsout); |
| 58 |
$code .= ",\n);"; |
$code .= "\n);"; |
| 59 |
} |
} |
| 60 |
else { |
else { |
| 61 |
// this is a form() call |
// this is a form() call |
| 70 |
$output .= $code; |
$output .= $code; |
| 71 |
|
|
| 72 |
} |
} |
| 73 |
$form['x_'.$i]['after'] = array('#type' => 'textarea', '#title' => t('Drupal 4.7'), '#default_value' => $code); |
$lines = substr_count($code, "\n") + 1; |
| 74 |
|
$form['x_'.$i]['after'] = array('#type' => 'textarea', '#title' => t('Drupal 4.7'), '#default_value' => $code, '#rows' => $lines); |
| 75 |
|
/* |
| 76 |
//notes for different types |
//notes for different types |
| 77 |
$d = ''; |
$d = ''; |
| 78 |
switch($match[1]){ |
switch($match[1]){ |
| 81 |
$d = t('**This code may need some extra work.'); |
$d = t('**This code may need some extra work.'); |
| 82 |
break; |
break; |
| 83 |
} |
} |
| 84 |
$form['x_'.$i]['after']['#description'] = $d; |
*/ |
| 85 |
|
$form['x_'.$i]['after']['#description'] = $note; |
| 86 |
$i++; |
$i++; |
| 87 |
} |
} |
| 88 |
} |
} |
| 127 |
} |
} |
| 128 |
$args = $trimedargs; |
$args = $trimedargs; |
| 129 |
|
|
| 130 |
$a = array(); |
$a = formupdater_get_argnames($type); |
| 131 |
|
$note = $a['note']; |
| 132 |
|
unset($a['note']); |
| 133 |
|
//var_dump($a); |
| 134 |
|
|
| 135 |
|
if (!$a['na']){ |
| 136 |
|
$form['#type'] = "'". $a['#type'] ."'"; |
| 137 |
|
unset($a['na']); |
| 138 |
|
unset($a['#type']); |
| 139 |
|
ksort($a); |
| 140 |
|
foreach($a as $key => $value) { |
| 141 |
|
if (isset($args[$key])){ |
| 142 |
|
$form[$value] = $args[$key]; |
| 143 |
|
} |
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
$name = $form['#name']; |
| 147 |
|
unset($form['#name']); |
| 148 |
|
$return[$name] = $form; |
| 149 |
|
$return['note'] = $note; |
| 150 |
|
} |
| 151 |
|
else { |
| 152 |
|
$return = FALSE; |
| 153 |
|
} |
| 154 |
|
return $return; |
| 155 |
|
} |
| 156 |
|
|
| 157 |
|
function formupdater_get_argnames($type) { |
| 158 |
|
$a = array(); |
| 159 |
switch($type){ |
switch($type){ |
| 160 |
case '': |
case '': |
| 161 |
// no type = form(); |
// no type = form(); |
| 162 |
$a = array(1 => '#method', 2 => '#action', 3 => '#attributes'); |
$a = array(1 => '#method', 2 => '#action', 3 => '#attributes'); |
| 163 |
|
$a['note'] = t('Replace your_form_id with an appropriate id.'); |
| 164 |
break; |
break; |
| 165 |
case 'autocomplete': |
case 'autocomplete': |
| 166 |
// form_autocomplete($title, $name, $value, $size, $maxlength, $callback_path, $description = NULL, $attributes = NULL, $required = FALSE) |
// form_autocomplete($title, $name, $value, $size, $maxlength, $callback_path, $description = NULL, $attributes = NULL, $required = FALSE) |
| 167 |
$a = array(2 => '#default_value', 3 => '#size', 4 => '#maxlength', 5 => '#autocomplete_path', 6 => '#description', 7 => '#attributes', 8 => '#required'); |
$a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#size', 4 => '#maxlength', 5 => '#autocomplete_path', 6 => '#description', 7 => '#attributes', 8 => '#required'); |
| 168 |
$type = 'textfield'; |
$type = 'textfield'; |
| 169 |
break; |
break; |
| 170 |
case 'button': |
case 'button': |
| 171 |
// form_button($value, $name = 'op', $type = 'submit', $attributes = NULL) |
// form_button($value, $name = 'op', $type = 'submit', $attributes = NULL) |
| 172 |
$a = array(2 => '#button_type', 3 => '#attributes'); |
$a = array(0 => '#title', 1 => '#name', 2 => '#button_type', 3 => '#attributes'); |
| 173 |
break; |
break; |
| 174 |
case 'checkbox': |
case 'checkbox': |
| 175 |
// form_checkbox($title, $name, $value = 1, $checked = FALSE, $description = NULL, $attributes = NULL, $required = FALSE) |
// form_checkbox($title, $name, $value = 1, $checked = FALSE, $description = NULL, $attributes = NULL, $required = FALSE) |
| 176 |
$a = array(2 => '#return_value', 3 => '#default_value', 4 => '#description', 5 => '#attributes', 6 => '#required'); |
$a = array(0 => '#title', 1 => '#name', 2 => '#return_value', 3 => '#default_value', 4 => '#description', 5 => '#attributes', 6 => '#required'); |
| 177 |
break; |
break; |
| 178 |
case 'checkboxes': |
case 'checkboxes': |
| 179 |
// form_checkboxes($title, $name, $values, $options, $description = NULL, $attributes = NULL, $required = FALSE) |
// form_checkboxes($title, $name, $values, $options, $description = NULL, $attributes = NULL, $required = FALSE) |
| 180 |
$a = array(2 => '#default_value', 3 => '#options', 4 => '#description', 5 => '#attributes', 6 => '#required'); |
$a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#options', 4 => '#description', 5 => '#attributes', 6 => '#required'); |
| 181 |
|
$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.'); |
| 182 |
break; |
break; |
| 183 |
case 'date': |
case 'date': |
| 184 |
// DOES NOT EXIST IN 4.6 |
// DOES NOT EXIST IN 4.6 |
| 185 |
break; |
break; |
| 186 |
case 'file': |
case 'file': |
| 187 |
// form_file($title, $name, $size, $description = NULL, $required = FALSE) |
// form_file($title, $name, $size, $description = NULL, $required = FALSE) |
| 188 |
$a = array(2 => '#size', 3 => '#description', 4 => '#attributes', 5 => '#required'); |
$a = array(0 => '#title', 1 => '#name', 2 => '#size', 3 => '#description', 4 => '#attributes', 5 => '#required'); |
| 189 |
break; |
break; |
| 190 |
case 'get_error': |
case 'get_error': |
| 191 |
// NOT A FORM FUNCTION ***************************** |
// NOT A FORM FUNCTION ***************************** |
| 192 |
$na = true; |
$a['na'] = true; |
| 193 |
break; |
break; |
| 194 |
case 'get_errors': |
case 'get_errors': |
| 195 |
// NOT A FORM FUNCTION ***************************** |
// NOT A FORM FUNCTION ***************************** |
| 196 |
$na = true; |
$a['na'] = true; |
| 197 |
break; |
break; |
| 198 |
case 'group': |
case 'group': |
| 199 |
// form_group($legend, $group, $description = NULL, $attributes = NULL) |
// form_group($legend, $group, $description = NULL, $attributes = NULL) |
| 200 |
$a = array(2 => '#description', 3 => '#attributes'); |
// we're ignoring $group, because this info is now stored in one of the subarrays |
| 201 |
$type = 'fieldset'; |
$a = array(0 => '#title', 2 => '#description', 3 => '#attributes'); |
| 202 |
|
$a['#type'] = 'fieldset'; |
| 203 |
|
$a['note'] = t('Fieldsets work very differently in 4.7. You will probably need to wrangle with this code some.'); |
| 204 |
// SPECIAL CASE ***************************** |
// SPECIAL CASE ***************************** |
| 205 |
break; |
break; |
| 206 |
case 'hidden': |
case 'hidden': |
| 207 |
// form_hidden($name, $value) |
// form_hidden($name, $value) |
| 208 |
// we're going to do some trickery here |
$a = array(0 => '#name', 1 => '#value'); |
|
$args[2] = $args[1]; |
|
|
$args[1] = $args[0]; |
|
|
unset($args[0]); |
|
|
$a = array(2 => '#value'); |
|
| 209 |
break; |
break; |
| 210 |
case 'item': |
case 'item': |
| 211 |
// form_item($title, $value, $description = NULL, $id = NULL, $required = FALSE, $na = FALSE) |
// form_item($title, $value, $description = NULL, $id = NULL, $required = FALSE, $na = FALSE) |
| 212 |
$a = array(1 => '#default_value', 2 => '#description', 3 => '#id', 4 => '#required'); |
$a = array(0 => '#title', 1 => '#default_value', 2 => '#description', 3 => '#id', 4 => '#required'); |
| 213 |
break; |
break; |
| 214 |
case 'password': |
case 'password': |
| 215 |
// form_password($title, $name, $value, $size, $maxlength, $description = NULL, $attributes = NULL, $required = FALSE) |
// form_password($title, $name, $value, $size, $maxlength, $description = NULL, $attributes = NULL, $required = FALSE) |
| 216 |
$a = array(2 => '#default_value', 3 => '#size', 4 => '#maxlength', 5 => '#description'); |
$a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#size', 4 => '#maxlength', 5 => '#description'); |
| 217 |
break; |
break; |
| 218 |
case 'radio': |
case 'radio': |
| 219 |
// form_radio($title, $name, $value = 1, $checked = FALSE, $description = NULL, $attributes = NULL, $required = FALSE) |
// form_radio($title, $name, $value = 1, $checked = FALSE, $description = NULL, $attributes = NULL, $required = FALSE) |
| 220 |
$a = array(2 => '#return_value', 3 => '#default_value', 4 => '#description', 5 => '#attributes', 6 => '#required'); |
$a = array(0 => '#title', 1 => '#name', 2 => '#return_value', 3 => '#default_value', 4 => '#description', 5 => '#attributes', 6 => '#required'); |
| 221 |
// note: #return_value ignored? |
// note: #return_value ignored? |
| 222 |
break; |
break; |
| 223 |
case 'radios': |
case 'radios': |
| 224 |
// form_radios($title, $name, $value, $options, $description = NULL, $required = FALSE, $attributes = NULL) |
// form_radios($title, $name, $value, $options, $description = NULL, $required = FALSE, $attributes = NULL) |
| 225 |
$a = array(2 => '#default_value', 3 => '#options', 4 => '#description', 5 => '#required', 6 => '#attributes'); |
$a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#options', 4 => '#description', 5 => '#required', 6 => '#attributes'); |
| 226 |
break; |
break; |
| 227 |
case 'select': |
case 'select': |
| 228 |
// function form_select($title, $name, $value, $options, $description = NULL, $extra = 0, $multiple = FALSE, $required = FALSE) |
// function form_select($title, $name, $value, $options, $description = NULL, $extra = 0, $multiple = FALSE, $required = FALSE) |
| 229 |
$a = array(2 => '#default_value', 3 => '#options', 4 => '#description', 5 => '#extra', 6 => '#multiple', 7 => '#required'); |
$a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#options', 4 => '#description', 5 => '#extra', 6 => '#multiple', 7 => '#required'); |
| 230 |
// NOTE: #extra ignored |
// NOTE: #extra ignored |
| 231 |
break; |
break; |
| 232 |
case 'submit': |
case 'submit': |
| 233 |
// form_submit($value, $name = 'op', $attributes = NULL) |
// form_submit($value, $name = 'op', $attributes = NULL) |
| 234 |
$a = array(0 => '#value', 1 => '#name', 2 => '#attributes'); |
$a = array(0 => '#value', 1 => '#name', 2 => '#attributes'); |
| 235 |
if (!isset($args[1])){ |
//if (!isset($args[1])){ |
| 236 |
$args[1] = "'op'"; |
// $args[1] = "'op'"; |
| 237 |
} |
//} |
| 238 |
break; |
break; |
| 239 |
case 'textarea': |
case 'textarea': |
| 240 |
// form_textarea($title, $name, $value, $cols, $rows, $description = NULL, $attributes = NULL, $required = FALSE) |
// form_textarea($title, $name, $value, $cols, $rows, $description = NULL, $attributes = NULL, $required = FALSE) |
| 241 |
$a = array(2 => '#default_value', 3 => '#cols', 4 => '#rows', 5 => '#description', 6 => '#attributes', 7 => '#required'); |
$a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#cols', 4 => '#rows', 5 => '#description', 6 => '#attributes', 7 => '#required'); |
| 242 |
break; |
break; |
| 243 |
case 'textfield': |
case 'textfield': |
| 244 |
// form_textfield($title, $name, $value, $size, $maxlength, $description = NULL, $attributes = NULL, $required = FALSE) |
// form_textfield($title, $name, $value, $size, $maxlength, $description = NULL, $attributes = NULL, $required = FALSE) |
| 245 |
$a = array(2 => '#default_value', 3 => '#size', 4 => '#maxlength', 5 => '#description', 6 => '#attributes', 7 => '#required'); |
$a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#size', 4 => '#maxlength', 5 => '#description', 6 => '#attributes', 7 => '#required'); |
| 246 |
break; |
break; |
| 247 |
case 'weight': |
case 'weight': |
| 248 |
// form_weight($title = NULL, $name = 'weight', $value = 0, $delta = 10, $description = NULL, $extra = 0) |
// form_weight($title = NULL, $name = 'weight', $value = 0, $delta = 10, $description = NULL, $extra = 0) |
| 249 |
$a = array(2 => '#default_value', 3 => '#delta', 4 => '#description', 5 => '#extra'); |
$a = array(0 => '#title', 1 => '#name', 2 => '#default_value', 3 => '#delta', 4 => '#description', 5 => '#extra'); |
| 250 |
// NOTE: #extra is ignored |
// NOTE: #extra is ignored |
| 251 |
break; |
break; |
| 252 |
default: |
default: |
| 253 |
$na = true; |
$a['na'] = true; |
|
} |
|
|
|
|
|
// put together the array |
|
|
if (!isset($a[0])){ |
|
|
$a[0] = '#title'; |
|
|
} |
|
|
ksort($a); |
|
|
|
|
|
if (!$na){ |
|
|
$form[$args[1]]['#type'] = "'$type'"; |
|
|
foreach($a as $key => $value) { |
|
|
if (isset($args[$key])){ |
|
|
$form[$args[1]][$value] = $args[$key]; |
|
|
} |
|
|
} |
|
|
$return = $form; |
|
| 254 |
} |
} |
| 255 |
else { |
if (!isset($a['#type'])){ |
| 256 |
$return = FALSE; |
$a['#type'] = $type; |
| 257 |
} |
} |
| 258 |
return $return; |
$a['na'] = $a['na'] ? TRUE : FALSE; |
| 259 |
|
return $a; |
| 260 |
} |
} |
| 261 |
|
|
| 262 |
|
|