| 1 |
<?php |
<?php |
| 2 |
// $Id: formupdater.module,v 1.6 2005/11/28 14:18:10 jjeff Exp $ |
// $Id$ |
| 3 |
|
|
| 4 |
function formupdater_help($section){ |
/** |
| 5 |
switch($section){ |
* @file |
| 6 |
case 'admin/modules#description': |
* Regex engine to help with module <4.6 upgrades. |
| 7 |
return t('Regex engine to help with module conversion.'); |
*/ |
| 8 |
break; |
|
| 9 |
|
/** |
| 10 |
|
* Implementation of hook_help(). |
| 11 |
|
*/ |
| 12 |
|
function formupdater_help($section) { |
| 13 |
|
switch ($section) { |
| 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 |
function formupdater_menu($may_cache){ |
/** |
| 20 |
|
* Implementation of hook_menu(). |
| 21 |
|
*/ |
| 22 |
|
function formupdater_menu($may_cache) { |
| 23 |
$items = array(); |
$items = array(); |
| 24 |
if ($may_cache){ |
if ($may_cache) { |
| 25 |
$items[] = array('path'=>'formupdater', 'title'=> t('form updater'), 'access' => user_access('access content'), 'callback' => 'formupdater_page'); |
$items[] = array( |
| 26 |
|
'path' => 'formupdater', |
| 27 |
|
'title' => t('Form updater'), |
| 28 |
|
'access' => user_access('access content'), |
| 29 |
|
'callback' => 'drupal_get_form', |
| 30 |
|
'callback arguments' => array('formupdater_form') |
| 31 |
|
); |
| 32 |
} |
} |
| 33 |
return $items; |
return $items; |
| 34 |
} |
} |
| 35 |
|
|
| 36 |
function formupdater_page(){ |
function formupdater_form() { |
| 37 |
if($edit = $_REQUEST['edit']){ |
$form = array(); |
| 38 |
return formupdater_process($edit); |
$form['code'] = array( |
| 39 |
} |
'#type' => 'textarea', |
| 40 |
$form['help'] = array('#type' => 'markup', '#value' => 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 Drupal 4.7 formapi.</p>')); |
'#title' => t('Drupal PHP code containing old form function calls'), |
| 41 |
$form['input'] = array('#type' => 'textarea', '#title'=> t('Drupal PHP code containing old form function calls'), '#rows' => 20); |
'#rows' => 20 |
| 42 |
$form['submit'] = array('#type' => 'submit', '#value' => 'submit'); |
); |
| 43 |
$output = drupal_get_form('formupdater_input', $form); |
$form['submit'] = array( |
| 44 |
$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.6 $</b>, $Date: 2005/11/28 14:18:10 $</p>'); |
'#type' => 'submit', |
| 45 |
return $output; |
'#value' => t('Process code') |
| 46 |
|
); |
| 47 |
|
return $form; |
| 48 |
} |
} |
| 49 |
|
|
| 50 |
function formupdater_process($edit){ |
function formupdater_form_submit($form_id, $form_values) { |
| 51 |
//var_dump($form_values); |
$code = $form_values['code']; |
|
$code = $edit['input']; |
|
| 52 |
$regex = '/\s(form_(.*?)|form())\(([\w\W]*?)\);/i'; |
$regex = '/\s(form_(.*?)|form())\(([\w\W]*?)\);/i'; |
| 53 |
preg_match_all($regex, $code, $matches, PREG_SET_ORDER); |
preg_match_all($regex, $code, $matches, PREG_SET_ORDER); |
| 54 |
$i = 1; |
$i = 1; |
| 55 |
if (is_array($matches)){ |
if (is_array($matches)) { |
| 56 |
foreach ($matches as $match){ |
foreach ($matches as $match) { |
| 57 |
//print($match[1]); |
//print($match[1]); |
| 58 |
if($array = formupdater_convert($match[2], $match[4])) { |
if ($array = formupdater_convert($match[2], $match[4])) { |
| 59 |
$note = $array['note']; |
$note = $array['note']; |
| 60 |
unset($array['note']); |
unset($array['note']); |
| 61 |
$form['x_'.$i] = array( |
$form['x_'. $i] = array( |
| 62 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 63 |
'#title' => $match[2] ? 'form_'. $match[2].'()' : 'form()', |
'#title' => $match[2] ? 'form_'. $match[2] .'()' : 'form()', |
| 64 |
'#collapsible' => true |
'#collapsible' => true |
| 65 |
); |
); |
| 66 |
$form['x_'.$i]['before'] = array('#type' => 'item', '#title' => t('Drupal 4.6'), '#value' => htmlentities($match[0])); |
$form['x_'. $i]['before'] = array( |
| 67 |
foreach ($array as $name => $args){ |
'#type' => 'item', |
| 68 |
if(strlen($match[2])){ |
'#title' => t('Drupal 4.6'), |
| 69 |
$code = "\$form[".str_replace('][', "']['", $name)."] = array(\n "; |
'#value' => htmlentities($match[0]), |
| 70 |
|
'#weight' => 0 |
| 71 |
|
); |
| 72 |
|
foreach ($array as $name => $args) { |
| 73 |
|
if (drupal_strlen($match[2])) { |
| 74 |
|
$code = "\$form[". str_replace('][', "']['", $name) ."] = array(\n "; |
| 75 |
$argsout = array(); |
$argsout = array(); |
| 76 |
foreach($args as $key => $val){ |
foreach ($args as $key => $val) { |
| 77 |
$argsout[] = "'$key' => $val"; |
$argsout[] = "'$key' => $val"; |
| 78 |
} |
} |
| 79 |
$code .= implode(",\n ", $argsout); |
$code .= implode(",\n ", $argsout); |
| 82 |
else { |
else { |
| 83 |
// this is a form() call |
// this is a form() call |
| 84 |
$code = ''; |
$code = ''; |
| 85 |
foreach($args as $key => $val){ |
foreach ($args as $key => $val) { |
| 86 |
if ($key != '#type' && $key != '#title'){ |
if ($key != '#type' && $key != '#title') { |
| 87 |
$code .= "\$form['$key'] = $val;\n"; |
$code .= "\$form['$key'] = $val;\n"; |
| 88 |
} |
} |
| 89 |
} |
} |
| 90 |
$code .= "\$output = drupal_get_form('your_form_id', \$form);"; |
$code .= "\$output = drupal_render(\$form);"; |
| 91 |
} |
} |
|
$output .= $code; |
|
|
|
|
| 92 |
} |
} |
| 93 |
$lines = substr_count($code, "\n") + 1; |
$lines = substr_count($code, "\n") + 1; |
| 94 |
$form['x_'.$i]['after'] = array('#type' => 'textarea', '#title' => t('Drupal 4.7'), '#default_value' => $code, '#rows' => $lines); |
$form['x_'. $i]['after'] = array( |
| 95 |
|
'#type' => 'textarea', |
| 96 |
|
'#title' => t('Forms API'), |
| 97 |
|
'#rows' => $lines, |
| 98 |
|
'#value' => $code, |
| 99 |
|
'#weight' => 1 |
| 100 |
|
); |
| 101 |
/* |
/* |
| 102 |
//notes for different types |
//notes for different types |
| 103 |
$d = ''; |
$d = ''; |
| 108 |
break; |
break; |
| 109 |
} |
} |
| 110 |
*/ |
*/ |
| 111 |
$form['x_'.$i]['after']['#description'] = $note; |
if (!empty($note)) { |
| 112 |
|
$form['x_'. $i]['after']['#description'] = $note; |
| 113 |
|
} |
| 114 |
$i++; |
$i++; |
| 115 |
} |
} |
| 116 |
} |
} |
| 117 |
} |
} |
| 118 |
$output = drupal_get_form('formupdater', $form); |
$output = drupal_render($form); |
| 119 |
return $output; |
print theme('page', $output); |
| 120 |
|
exit; |
| 121 |
} |
} |
| 122 |
|
|
| 123 |
function formupdater_convert($type, $args){ |
function formupdater_convert($type, $args) { |
| 124 |
$type = trim($type); |
$type = trim($type); |
| 125 |
|
|
| 126 |
//temporarily replace commas in sub-arguments |
//temporarily replace commas in sub-arguments |
| 127 |
//split by parenthasis |
//split by parenthasis |
| 128 |
$parensplit = preg_split('/([\(\)])/', $args, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); |
$parensplit = preg_split('/([\(\)])/', $args, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); |
| 129 |
$open = 0; |
$open = 0; |
| 130 |
foreach((array)$parensplit as $parenchunk){ |
foreach ((array)$parensplit as $parenchunk) { |
| 131 |
switch($parenchunk){ |
switch ($parenchunk) { |
| 132 |
case '(': |
case '(': |
| 133 |
$open++; |
$open++; |
| 134 |
$reargs .= $parenchunk; |
$reargs .= $parenchunk; |
| 138 |
$reargs .= $parenchunk; |
$reargs .= $parenchunk; |
| 139 |
break; |
break; |
| 140 |
default: |
default: |
| 141 |
if ($open > 0){ |
if ($open > 0) { |
| 142 |
// if we're inside a set of parenthasis, "protect" the commas |
// if we're inside a set of parenthasis, "protect" the commas |
| 143 |
$parenchunk = str_replace(',', '^%^%^', $parenchunk); |
$parenchunk = str_replace(',', '^%^%^', $parenchunk); |
| 144 |
} |
} |
| 149 |
// make args into an array |
// make args into an array |
| 150 |
$args = explode(',', $reargs); |
$args = explode(',', $reargs); |
| 151 |
$trimedargs = array(); |
$trimedargs = array(); |
| 152 |
foreach((array)$args as $arg){ |
foreach ((array)$args as $arg) { |
| 153 |
// trim out white space |
// trim out white space |
| 154 |
// and put commas back in |
// and put commas back in |
| 155 |
$trimedargs[] = trim(str_replace('^%^%^', ',', $arg)); |
$trimedargs[] = trim(str_replace('^%^%^', ',', $arg)); |
| 161 |
unset($a['note']); |
unset($a['note']); |
| 162 |
//var_dump($a); |
//var_dump($a); |
| 163 |
|
|
| 164 |
if (!$a['na']){ |
if (!$a['na']) { |
| 165 |
$form['#type'] = "'". $a['#type'] ."'"; |
$form['#type'] = "'". $a['#type'] ."'"; |
| 166 |
unset($a['na']); |
unset($a['na']); |
| 167 |
unset($a['#type']); |
unset($a['#type']); |
| 168 |
ksort($a); |
ksort($a); |
| 169 |
foreach($a as $key => $value) { |
foreach ($a as $key => $value) { |
| 170 |
if (isset($args[$key])){ |
if (isset($args[$key])) { |
| 171 |
$form[$value] = $args[$key]; |
$form[$value] = $args[$key]; |
| 172 |
} |
} |
| 173 |
} |
} |
| 185 |
|
|
| 186 |
function formupdater_get_argnames($type) { |
function formupdater_get_argnames($type) { |
| 187 |
$a = array(); |
$a = array(); |
| 188 |
switch($type){ |
switch ($type) { |
| 189 |
case '': |
case '': |
| 190 |
// no type = form(); |
// no type = form(); |
| 191 |
$a = array(1 => '#method', 2 => '#action', 3 => '#attributes'); |
$a = array(1 => '#method', 2 => '#action', 3 => '#attributes'); |
| 281 |
default: |
default: |
| 282 |
$a['na'] = true; |
$a['na'] = true; |
| 283 |
} |
} |
| 284 |
if (!isset($a['#type'])){ |
if (!isset($a['#type'])) { |
| 285 |
$a['#type'] = $type; |
$a['#type'] = $type; |
| 286 |
} |
} |
| 287 |
$a['na'] = $a['na'] ? TRUE : FALSE; |
$a['na'] = $a['na'] ? TRUE : FALSE; |
| 288 |
return $a; |
return $a; |
| 289 |
} |
} |
|
|
|
|
|
|
|
?> |
|