| 1 |
<?php |
<?php |
| 2 |
// $Id: flexiconvert.module,v 1.7 2008/05/07 21:26:47 aj045 Exp $ |
// $Id: flexiconvert.module,v 1.8 2008/08/19 02:34:40 aj045 Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 23 |
function flexiconvert_help($section) { |
function flexiconvert_help($section) { |
| 24 |
switch ($section) { |
switch ($section) { |
| 25 |
case 'admin/help#flexiconvert': |
case 'admin/help#flexiconvert': |
| 26 |
return t('Flexiconvert will convert some kinds of flexinode types into cck types.<br>' . |
return t('Flexiconvert will convert some kinds of flexinode types into cck types.<br>' . |
| 27 |
'<strong>Limitations and notices:</strong>' . |
'<strong>Limitations and notices:</strong>' . |
| 28 |
'<ul><li> May have trouble on highly customized fields and content types.</li>' . |
'<ul><li> May have trouble on highly customized fields and content types.</li>' . |
| 29 |
'<li> The customized settings for fields will not be carried over-- you will need to reset those yourself.</li>' . |
'<li> The customized settings for fields will not be carried over-- you will need to reset those yourself.</li>' . |
| 30 |
'<li> For "image" content types, the image source path and the gallery will not show up.</li>' . |
'<li> For "image" content types, the image source path and the gallery will not show up.</li>' . |
| 31 |
'<li> Content types from Flexinode are converted by their Flexinode name. If one or more of your Flexinode types has white space in it, the white space will be converted to underscores when the conversion finishes. For example the type "my example type" will be converted to "my_example_type". All amounts of white space between words will be converted to a single underscore, however, the content name will still show the spaces. For more details, read the README.txt file that came with the Flexiconvert module.' . |
'<li> Content types from Flexinode are converted by their Flexinode name. If one or more of your Flexinode types has white space in it, the white space will be converted to underscores when the conversion finishes. For example the type "my example type" will be converted to "my_example_type". All amounts of white space between words will be converted to a single underscore, however, the content name will still show the spaces. For more details, read the README.txt file that came with the Flexiconvert module.' . |
| 32 |
'</ul>' . |
'</ul>' . |
| 33 |
'<strong>Steps for use:</strong> <ol>' . |
'<strong>Steps for use:</strong> <ol>' . |
| 34 |
'<li> Make a complete back-up of your database before continuing.</li>' . |
'<li> Make a complete back-up of your database before continuing.</li>' . |
| 35 |
'<li> Make sure that the users who created the content still exist or set the UID for all nodes to the UID of a user that does exist. If you have content that was created by a user that no longer exists, the content will be converted but will not appear correctly or at all.' . |
'<li> Make sure that the users who created the content still exist or set the UID for all nodes to the UID of a user that does exist. If you have content that was created by a user that no longer exists, the content will be converted but will not appear correctly or at all.' . |
| 36 |
'<li> Make sure all your flexinode fields are unique.</li>' . |
'<li> Make sure all your flexinode fields are unique.</li>' . |
| 37 |
'<li> Turn off the Flexinode module-- you\'re probably not going to need it any more.</li>' . |
'<li> Turn off the Flexinode module-- you\'re probably not going to need it any more.</li>' . |
| 38 |
'<li> Turn on the CCK & all of the content type modules that came with it, like Text, Select, et cetera.</li>' . |
'<li> Turn on the CCK & all of the content type modules that came with it, like Text, Select, et cetera.</li>' . |
| 39 |
'<li> Go to admin/flexiconvert/convert and select all the node types you\'d like to convert and then click "Convert". Alternatively, if you\'d like to convert them all, check "Check all" and all of the types listed will be check marked for you.</li>' . |
'<li> Go to admin/flexiconvert/convert and select all the node types you\'d like to convert and then click "Convert". Alternatively, if you\'d like to convert them all, check "Check all" and all of the types listed will be check marked for you.</li>' . |
| 40 |
'<li> The "type" field in the {node} table will be updated to reflect the new types. This is necessary because Flexinode uses an non-traditional method of type-naming it\'s content in the {node} table.' . |
'<li> The "type" field in the {node} table will be updated to reflect the new types. This is necessary because Flexinode uses an non-traditional method of type-naming it\'s content in the {node} table.' . |
| 41 |
'</ol>' |
'</ol>' |
| 42 |
); |
); |
| 43 |
case 'admin/flexiconvert/convert': |
case 'admin/flexiconvert/convert': |
| 44 |
$msg .= t('Below is a list of Flexinode types found. Mark the ones you desire to convert and click submit.') . ' '; |
$msg .= t('Below is a list of Flexinode types found. Mark the ones you desire to convert and click submit.') . ' '; |
| 45 |
$msg .= t('Whitespace will cause types to be converted incorrectly.') . ' '; |
$msg .= t('Whitespace will cause types to be converted incorrectly.') . ' '; |
| 46 |
$msg .= '<strong>' . t('Please also note that if the user who created one or more pieces of content no longer exists, this conversion may execute incorrectly. You may want to convert the author of all content to UID 1.') . '</strong>'; |
$msg .= '<strong>' . t('Please also note that if the user who created one or more pieces of content no longer exists, this conversion may execute incorrectly. You may want to convert the author of all content to UID 1.') . '</strong>'; |
| 47 |
$msg .= '<br /><br />' . t('It is HIGHLY ADVISED that you make a complete back-up of your database BEFORE continuing.'); |
$msg .= '<br /><br />' . t('It is HIGHLY ADVISED that you make a complete back-up of your database BEFORE continuing.'); |
| 48 |
return $msg; |
return $msg; |
| 49 |
} |
} |
| 50 |
} |
} |
| 51 |
|
|
| 57 |
$items = array(); |
$items = array(); |
| 58 |
if (!$may_cache) { |
if (!$may_cache) { |
| 59 |
$items[] = array( |
$items[] = array( |
| 60 |
'path' => 'admin/flexiconvert/convert', |
'path' => 'admin/flexiconvert/convert', |
| 61 |
'title' => t('Flexiconvert'), |
'title' => t('Flexiconvert'), |
| 62 |
'callback' => 'flexiconvert_page', |
'callback' => 'flexiconvert_page', |
| 63 |
'access' => user_access('convert flexinode types'), |
'access' => user_access('convert flexinode types'), |
| 64 |
); |
); |
| 65 |
} |
} |
| 66 |
return $items; |
return $items; |
| 70 |
* Implementation of hook_perm(). |
* Implementation of hook_perm(). |
| 71 |
*/ |
*/ |
| 72 |
function flexiconvert_perm() { |
function flexiconvert_perm() { |
| 73 |
return array('convert flexinode types'); |
return array('convert flexinode types'); |
| 74 |
} |
} |
| 75 |
|
|
| 76 |
/** |
/** |
| 84 |
* -description : Description of this type. |
* -description : Description of this type. |
| 85 |
*/ |
*/ |
| 86 |
function flexiconvert_get_flexinode_types() { |
function flexiconvert_get_flexinode_types() { |
| 87 |
$sql = 'SELECT * FROM {flexinode_type} ORDER BY ctype_id ASC'; |
$sql = 'SELECT * FROM {flexinode_type} ORDER BY ctype_id ASC'; |
| 88 |
$q = db_query($sql); |
$q = db_query($sql); |
| 89 |
|
|
| 90 |
$types = array(); |
$types = array(); |
| 91 |
while ($type = db_fetch_object($q)) { |
while ($type = db_fetch_object($q)) { |
| 92 |
$types[$type->name] = $type; |
$types[$type->name] = $type; |
| 93 |
} |
} |
| 94 |
|
|
| 95 |
return $types; |
return $types; |
| 96 |
} |
} |
| 97 |
|
|
| 98 |
/** |
/** |
| 137 |
* data. You may need to skim over the code for Flexinode to figure out what each piece of data really means. |
* data. You may need to skim over the code for Flexinode to figure out what each piece of data really means. |
| 138 |
*/ |
*/ |
| 139 |
function flexiconvert_get_type($type) { |
function flexiconvert_get_type($type) { |
| 140 |
$type_obj = FALSE; |
$type_obj = FALSE; |
| 141 |
|
|
| 142 |
if (isset($type) && is_string($type)) { |
if (isset($type) && is_string($type)) { |
| 143 |
/* Get the type-specific data. */ |
/* Get the type-specific data. */ |
| 144 |
$db_q_type = db_query_range("SELECT * FROM {flexinode_type} WHERE LOWER(name) = '%s'", strtolower($type), 0, 1); |
$db_q_type = db_query_range("SELECT * FROM {flexinode_type} WHERE LOWER(name) = '%s'", strtolower($type), 0, 1); |
| 145 |
|
|
| 146 |
if (db_affected_rows()) { |
if (db_affected_rows()) { |
| 147 |
$type_obj = db_fetch_object($db_q_type); |
$type_obj = db_fetch_object($db_q_type); |
| 148 |
$type_obj->id = $type_obj->ctype_id; // So one doesn't have to type "ctype_id" all the time... |
$type_obj->id = $type_obj->ctype_id; // So one doesn't have to type "ctype_id" all the time... |
| 149 |
|
|
| 150 |
/* Get the fields for this type. A second query is required here because both the flexinode_field |
/* Get the fields for this type. A second query is required here because both the flexinode_field |
| 151 |
and flexinode_type tables in the database have at least one similar table field, "description". */ |
and flexinode_type tables in the database have at least one similar table field, "description". */ |
| 152 |
$type_obj->fields = array(); |
$type_obj->fields = array(); |
| 153 |
$db_q_fields = db_query("SELECT * FROM {flexinode_field} WHERE ctype_id=%d", $type_obj->id); |
$db_q_fields = db_query("SELECT * FROM {flexinode_field} WHERE ctype_id=%d", $type_obj->id); |
| 154 |
|
|
| 155 |
while ($type_field = db_fetch_array($db_q_fields)) { |
while ($type_field = db_fetch_array($db_q_fields)) { |
| 156 |
//$type_obj->fields[$type_field->field_id] = $type_field; |
//$type_obj->fields[$type_field->field_id] = $type_field; |
| 157 |
$type_obj->fields[] = $type_field; |
$type_obj->fields[] = $type_field; |
| 158 |
} |
} |
| 159 |
} |
} |
| 160 |
} |
} |
| 161 |
|
|
| 162 |
return $type_obj; |
return $type_obj; |
| 163 |
} |
} |
| 164 |
|
|
| 165 |
/** |
/** |
| 174 |
* modules is disabled. |
* modules is disabled. |
| 175 |
*/ |
*/ |
| 176 |
function flexiconvert_cck_running() { |
function flexiconvert_cck_running() { |
| 177 |
if (!module_exists('content')) { |
if (!module_exists('content')) { |
| 178 |
return FLEXICONVERT_CCK_DISABLED; |
return FLEXICONVERT_CCK_DISABLED; |
| 179 |
} |
} |
| 180 |
else if ( |
else if ( |
| 181 |
!module_exists('nodereference') || |
!module_exists('nodereference') || |
| 182 |
!module_exists('text') || |
!module_exists('text') || |
| 183 |
!module_exists('content_copy') || |
!module_exists('content_copy') || |
| 184 |
!module_exists('number') || |
!module_exists('number') || |
| 185 |
!module_exists('userreference') || |
!module_exists('userreference') || |
| 186 |
!module_exists('fieldgroup') || |
!module_exists('fieldgroup') || |
| 187 |
!module_exists('optionwidgets') |
!module_exists('optionwidgets') |
| 188 |
) { |
) { |
| 189 |
return FLEXICONVERT_CCK_ASSOCIATED_MODULES_DISABLED; |
return FLEXICONVERT_CCK_ASSOCIATED_MODULES_DISABLED; |
| 190 |
} |
} |
| 191 |
|
|
| 192 |
return FLEXCONVERT_CCK_IS_RUNNING; |
return FLEXCONVERT_CCK_IS_RUNNING; |
| 193 |
} |
} |
| 194 |
|
|
| 195 |
/** |
/** |
| 198 |
* If CCK is not running correctly, an error message is set instead of a form. |
* If CCK is not running correctly, an error message is set instead of a form. |
| 199 |
*/ |
*/ |
| 200 |
function flexiconvert_page() { |
function flexiconvert_page() { |
| 201 |
$cck_is_init = flexiconvert_cck_running(); |
$cck_is_init = flexiconvert_cck_running(); |
| 202 |
|
|
| 203 |
if ($cck_is_init == FLEXICONVERT_CCK_DISABLED) { |
if ($cck_is_init == FLEXICONVERT_CCK_DISABLED) { |
| 204 |
drupal_set_message(t('CCK module disabled or does not exist!'), 'error'); |
drupal_set_message(t('CCK module disabled or does not exist!'), 'error'); |
| 205 |
} |
} |
| 206 |
else if ($cck_is_init == FLEXICONVERT_CCK_ASSOCIATED_MODULES_DISABLED) { |
else if ($cck_is_init == FLEXICONVERT_CCK_ASSOCIATED_MODULES_DISABLED) { |
| 207 |
drupal_set_message(t('One or more of the packaged CCK field type modules is disabled!'), 'error'); |
drupal_set_message(t('One or more of the packaged CCK field type modules is disabled!'), 'error'); |
| 208 |
} |
} |
| 209 |
else { // CCK is running happily! Yay! |
else { // CCK is running happily! Yay! |
| 210 |
return drupal_get_form('flexiconvert_form'); |
return drupal_get_form('flexiconvert_form'); |
| 211 |
} |
} |
| 212 |
} |
} |
| 213 |
|
|
| 214 |
/** |
/** |
| 215 |
* Implementation of hook_form(). |
* Implementation of hook_form(). |
| 216 |
*/ |
*/ |
| 217 |
function flexiconvert_form() { |
function flexiconvert_form() { |
| 218 |
// Include a stylesheet because we need a pretty table-like appearance of the form. |
// Include a stylesheet because we need a pretty table-like appearance of the form. |
| 219 |
drupal_add_css('sites/all/modules/flexiconvert/flexiconvert_styles.css', 'module', 'all', TRUE); |
drupal_add_css('sites/all/modules/flexiconvert/flexiconvert_styles.css', 'module', 'all', TRUE); |
| 220 |
// Add jQuery code to select all the elements in the form. |
// Add jQuery code to select all the elements in the form. |
| 221 |
drupal_add_js(' |
drupal_add_js(' |
| 222 |
$(document).ready(function() { |
$(document).ready(function() { |
| 223 |
$("#edit-check-all").click(function() { |
$("#edit-check-all").click(function() { |
| 224 |
for (i = 0; i < this.form.elements.length; i++) { |
for (i = 0; i < this.form.elements.length; i++) { |
| 228 |
} |
} |
| 229 |
}); |
}); |
| 230 |
});', 'inline'); |
});', 'inline'); |
| 231 |
$form = array(); |
$form = array(); |
| 232 |
$types = flexiconvert_get_flexinode_types(); |
$types = flexiconvert_get_flexinode_types(); |
| 233 |
$types_header = array(t('Flexinode Types'), t('ID')); |
$types_header = array(t('Flexinode Types'), t('ID')); |
| 234 |
$types_rows = array(); |
$types_rows = array(); |
| 235 |
$form['flexiconvert_type']['#prefix'] = '<div id="flexiconvert_container">'; |
$form['flexiconvert_type']['#prefix'] = '<div id="flexiconvert_container">'; |
| 236 |
$form['flexiconvert_type']['#suffix'] = '</div>'; |
$form['flexiconvert_type']['#suffix'] = '</div>'; |
| 237 |
$form['flexiconvert_type']['#tree'] = TRUE; // Make this the parent of a tree so we can group these items together. |
$form['flexiconvert_type']['#tree'] = TRUE; // Make this the parent of a tree so we can group these items together. |
| 238 |
|
|
| 239 |
$i = 0; // Counter variable is used to determine the color pattern for the table. |
$i = 0; // Counter variable is used to determine the color pattern for the table. |
| 240 |
foreach ($types as $type) { |
foreach ($types as $type) { |
| 241 |
//$types_rows[] = array($type->name, $type->ctype_id); |
//$types_rows[] = array($type->name, $type->ctype_id); |
| 242 |
$form['flexiconvert_type'][$type->name] = array( |
$form['flexiconvert_type'][$type->name] = array( |
| 243 |
'#prefix' => '<div id="flexiconvert_row_container" class="' . ($i % 2 == 0 ? 'odd' : 'even') . '"><div id="flexiconvert_checkme">', |
'#prefix' => '<div id="flexiconvert_row_container" class="' . ($i % 2 == 0 ? 'odd' : 'even') . '"><div id="flexiconvert_checkme">', |
| 244 |
'#suffix' => '</div>', |
'#suffix' => '</div>', |
| 245 |
'#tree' => TRUE, |
'#tree' => TRUE, |
| 246 |
); |
); |
| 247 |
|
|
| 248 |
$form['flexiconvert_type'][$type->name]['checked'] = array( |
$form['flexiconvert_type'][$type->name]['checked'] = array( |
| 249 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 250 |
); |
); |
| 251 |
|
|
| 252 |
// Append to the suffix the name of this type. For display purposes. |
// Append to the suffix the name of this type. For display purposes. |
| 253 |
$form['flexiconvert_type'][$type->name]['#suffix'] .= '<div id="flexiconvert_title"><strong>' . $type->name . '</strong>'; |
$form['flexiconvert_type'][$type->name]['#suffix'] .= '<div id="flexiconvert_title"><strong>' . $type->name . '</strong>'; |
| 254 |
|
|
| 255 |
if (strpos($type->name, ' ') != FALSE) { |
if (strpos($type->name, ' ') != FALSE) { |
| 256 |
$form['flexiconvert_type'][$type->name]['#suffix'] .= ' (will be converted to ' . preg_replace('/[\s]+/', '_', $type->name) . ')'; |
$form['flexiconvert_type'][$type->name]['#suffix'] .= ' (will be converted to ' . preg_replace('/[\s]+/', '_', $type->name) . ')'; |
| 257 |
} |
} |
| 258 |
$form['flexiconvert_type'][$type->name]['#suffix'] .= '</div></div>'; |
$form['flexiconvert_type'][$type->name]['#suffix'] .= '</div></div>'; |
| 259 |
|
|
| 260 |
$form['flexiconvert_type'][$type->name]['title'] = array( |
$form['flexiconvert_type'][$type->name]['title'] = array( |
| 261 |
'#type' => 'value', |
'#type' => 'value', |
| 262 |
'#value' => $type->name, |
'#value' => $type->name, |
| 263 |
); |
); |
| 264 |
|
|
| 265 |
$form['flexiconvert_type'][$type->name]['description'] = array( |
$form['flexiconvert_type'][$type->name]['description'] = array( |
| 266 |
'#type' => 'value', |
'#type' => 'value', |
| 267 |
'#value' => $type->description, |
'#value' => $type->description, |
| 268 |
); |
); |
| 269 |
|
|
| 270 |
$i += 1; |
$i += 1; |
| 271 |
} |
} |
| 272 |
|
|
| 273 |
$form['check_all'] = array( |
$form['check_all'] = array( |
| 274 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 275 |
'#prefix' => '<div id="flexiconvert_row_container" class="' . ($i % 2 == 0 ? 'odd' : 'even') . '"><div id="flexiconvert_checkme">', |
'#prefix' => '<div id="flexiconvert_row_container" class="' . ($i % 2 == 0 ? 'odd' : 'even') . '"><div id="flexiconvert_checkme">', |
| 276 |
'#suffix' => '</div><div id="flexiconvert_title"><em>Check all</em></div></div>', |
'#suffix' => '</div><div id="flexiconvert_title"><em>Check all</em></div></div>', |
| 277 |
); |
); |
| 278 |
|
|
| 279 |
$form['submit'] = array( |
$form['submit'] = array( |
| 280 |
'#type' => 'submit', |
'#type' => 'submit', |
| 281 |
'#value' => 'Convert', |
'#value' => 'Convert', |
| 282 |
'#prefix' => '<div id="flexiconvert_container">', |
'#prefix' => '<div id="flexiconvert_container">', |
| 283 |
'#suffix' => '</div>', |
'#suffix' => '</div>', |
| 284 |
); |
); |
| 285 |
|
|
| 286 |
return $form; |
return $form; |
| 287 |
} |
} |
| 288 |
|
|
| 289 |
/** |
/** |
| 290 |
* Implementation of hook_form_validate(). |
* Implementation of hook_form_validate(). |
| 291 |
*/ |
*/ |
| 292 |
function flexiconvert_form_validate($form_id, $form_values) { |
function flexiconvert_form_validate($form_id, $form_values) { |
| 293 |
// No real need to do any validation. The form is nothing more than a |
// No real need to do any validation. The form is nothing more than a |
| 294 |
// bunch of check boxes. And we DO allow whitespace so no need to |
// bunch of check boxes. And we DO allow whitespace so no need to |
| 295 |
// check for that either. |
// check for that either. |
| 296 |
} |
} |
| 297 |
|
|
| 298 |
/** |
/** |
| 299 |
* Implementation of hook_form_submit(). |
* Implementation of hook_form_submit(). |
| 300 |
*/ |
*/ |
| 301 |
function flexiconvert_form_submit($form_id, $form_values) { |
function flexiconvert_form_submit($form_id, $form_values) { |
| 302 |
// Keep track of created fields so we don't get SQL duplication errors. |
// Keep track of created fields so we don't get SQL duplication errors. |
| 303 |
$fields_submitted = array(); |
$fields_submitted = array(); |
| 304 |
|
|
| 305 |
// If there are pieces of content that are of type |
// If there are pieces of content that are of type |
| 306 |
// "image", we need to ensure that it will be handled |
// "image", we need to ensure that it will be handled |
| 307 |
// properly. See the end of this function. |
// properly. See the end of this function. |
| 308 |
$image_type_exists = flexiconvert_get_type('image'); |
$image_type_exists = flexiconvert_get_type('image'); |
| 309 |
$image_module_exists = module_exists('image'); |
$image_module_exists = module_exists('image'); |
| 310 |
|
|
| 311 |
// Type conversion... |
// Type conversion... |
| 312 |
foreach ($form_values['flexiconvert_type'] as $ftype) { |
foreach ($form_values['flexiconvert_type'] as $ftype) { |
| 313 |
if ($ftype['checked']) { |
if ($ftype['checked']) { |
| 314 |
$type_obj = new stdClass(); |
$type_obj = new stdClass(); |
| 315 |
$type_obj->type = _flexiconvert_fix_single_type(strtolower($ftype['title'])); // Fix name... |
$type_obj->type = _flexiconvert_fix_single_type(strtolower($ftype['title'])); // Fix name... |
| 316 |
$type_obj->name = $ftype['title']; |
$type_obj->name = $ftype['title']; |
| 317 |
$type_obj->orig_type = ''; |
$type_obj->orig_type = ''; |
| 318 |
$type_obj->old_type = ''; |
$type_obj->old_type = ''; |
| 319 |
$type_obj->description = $ftype['description']; |
$type_obj->description = $ftype['description']; |
| 320 |
$type_obj->help = ''; |
$type_obj->help = ''; |
| 321 |
$type_obj->min_word_count = 0; |
$type_obj->min_word_count = 0; |
| 322 |
$type_obj->title_label = t('Title'); |
$type_obj->title_label = t('Title'); |
| 323 |
$type_obj->body_label = t('Body'); |
$type_obj->body_label = t('Body'); |
| 324 |
$type_obj->has_title = TRUE; |
$type_obj->has_title = TRUE; |
| 325 |
$type_obj->has_body = TRUE; |
$type_obj->has_body = TRUE; |
| 326 |
$type_obj->module = 'node'; |
$type_obj->module = 'node'; |
| 327 |
$type_obj->custom = TRUE; |
$type_obj->custom = TRUE; |
| 328 |
$type_obj->modified = FALSE; |
$type_obj->modified = FALSE; |
| 329 |
$type_obj->locked = FALSE; |
$type_obj->locked = FALSE; |
| 330 |
|
|
| 331 |
node_type_save($type_obj); |
node_type_save($type_obj); |
| 332 |
} |
} |
| 333 |
} // End type conversion loop... |
} // End type conversion loop... |
| 334 |
|
|
| 335 |
//Field conversion. |
//Field conversion. |
| 336 |
foreach ($form_values['flexiconvert_type'] as $ffield) { |
foreach ($form_values['flexiconvert_type'] as $ffield) { |
| 337 |
if ($ffield['checked']) { |
if ($ffield['checked']) { |
| 338 |
$type = flexiconvert_get_type($ffield['title']); |
$type = flexiconvert_get_type($ffield['title']); |
| 339 |
foreach($type->fields as $fd) { |
foreach($type->fields as $fd) { |
| 340 |
// Much of the code that appears below is straight from the CCK module. |
// Much of the code that appears below is straight from the CCK module. |
| 341 |
// The code is quite messy and a bit hard to read but it gets the job done. |
// The code is quite messy and a bit hard to read but it gets the job done. |
| 342 |
$field_name = trim($fd['label']); |
$field_name = trim($fd['label']); |
| 343 |
$field_name = drupal_strtolower($field_name); |
$field_name = drupal_strtolower($field_name); |
| 344 |
$field_name = str_replace(array(' ', '-'), '_', $field_name); |
$field_name = str_replace(array(' ', '-'), '_', $field_name); |
| 345 |
$field_name = preg_replace('/[^a-z0-9_]/', '', $field_name); |
$field_name = preg_replace('/[^a-z0-9_]/', '', $field_name); |
| 346 |
$field_name = 'field_'. $field_name; |
$field_name = 'field_'. $field_name; |
| 347 |
$field_name = substr($field_name, 0, 31); |
$field_name = substr($field_name, 0, 31); |
| 348 |
|
|
| 349 |
if (in_array($field_name, $fields_submitted)) { |
if (in_array($field_name, $fields_submitted)) { |
| 350 |
// Prevent unnecessary duplication of fields. |
// Prevent unnecessary duplication of fields. |
| 351 |
break; |
break; |
| 352 |
} |
} |
| 353 |
|
|
| 354 |
$cck_type = _flexiconvert_field_to_cck($fd['field_type']); |
$cck_type = _flexiconvert_field_to_cck($fd['field_type']); |
| 355 |
|
|
| 356 |
db_query("INSERT INTO {node_field} (field_name, type, global_settings, required, multiple, db_storage) VALUES ('%s', '%s', '%s', %d, %d, %d)", |
db_query("INSERT INTO {node_field} (field_name, type, global_settings, required, multiple, db_storage) VALUES ('%s', '%s', '%s', %d, %d, %d)", |
| 357 |
$field_name, |
$field_name, |
| 358 |
$cck_type, |
$cck_type, |
| 359 |
serialize(array( |
serialize(array( |
| 360 |
'text_processing' => '1', |
'text_processing' => '1', |
| 361 |
'max_length' => '', |
'max_length' => '', |
| 362 |
'allowed_values' => '', |
'allowed_values' => '', |
| 363 |
'allowed_values_php' => '' |
'allowed_values_php' => '' |
| 364 |
)), |
)), |
| 365 |
$fd['required'], |
$fd['required'], |
| 366 |
0, |
0, |
| 367 |
1 |
1 |
| 368 |
); |
); |
| 369 |
db_query("INSERT INTO {node_field_instance} (field_name, type_name, weight, label, widget_type, widget_settings, display_settings, description) VALUES ('%s', '%s', %d, '%s', '%s', '%s', '%s', '%s')", |
db_query("INSERT INTO {node_field_instance} (field_name, type_name, weight, label, widget_type, widget_settings, display_settings, description) VALUES ('%s', '%s', %d, '%s', '%s', '%s', '%s', '%s')", |
| 370 |
$field_name, |
$field_name, |
| 371 |
_flexiconvert_fix_single_type(strtolower($ffield['title'])), |
_flexiconvert_fix_single_type(strtolower($ffield['title'])), |
| 372 |
$fd['weight'], |
$fd['weight'], |
| 373 |
$fd['label'], |
$fd['label'], |
| 374 |
$cck_type, |
$cck_type, |
| 375 |
serialize(array()), |
serialize(array()), |
| 376 |
serialize(array()), |
serialize(array()), |
| 377 |
'' |
'' |
| 378 |
); |
); |
| 379 |
|
|
| 380 |
content_clear_type_cache(); |
content_clear_type_cache(); |
| 381 |
|
|
| 382 |
$field_types = _content_field_types(); |
$field_types = _content_field_types(); |
| 383 |
$field_type = $field_types[$cck_type]; |
$field_type = $field_types[$cck_type]; |
| 384 |
$field = content_fields($field_name); |
$field = content_fields($field_name); |
| 385 |
|
|
| 386 |
$columns = module_invoke($field_type['module'], 'field_settings', 'database columns', $field); |
$columns = module_invoke($field_type['module'], 'field_settings', 'database columns', $field); |
| 387 |
|
|
| 388 |
if (is_array($columns) && count($columns)) { |
if (is_array($columns) && count($columns)) { |
| 389 |
// Note that the first two parameters are empty arrays. |
// Note that the first two parameters are empty arrays. |
| 390 |
// These are the global settings and widget settings arrays. |
// These are the global settings and widget settings arrays. |
| 391 |
// The user will need to deal with handling these settings |
// The user will need to deal with handling these settings |
| 392 |
// on his or her own until an implementation comes along |
// on his or her own until an implementation comes along |
| 393 |
// that will convert Flexinode field settings to CCK field settings. |
// that will convert Flexinode field settings to CCK field settings. |
| 394 |
content_alter_db_field(array(), array(), $field, $columns); |
content_alter_db_field(array(), array(), $field, $columns); |
| 395 |
} |
} |
| 396 |
|
|
| 397 |
$fields_submitted[] = $field_name; // Push onto the array stack so we don't get duplicate fields. |
$fields_submitted[] = $field_name; // Push onto the array stack so we don't get duplicate fields. |
| 398 |
} |
} |
| 399 |
} |
} |
| 400 |
} // End field conversion loop... |
} // End field conversion loop... |
| 401 |
|
|
| 402 |
// Data conversion |
// Data conversion |
| 403 |
foreach ($form_values['flexiconvert_type'] as $fdata) { |
foreach ($form_values['flexiconvert_type'] as $fdata) { |
| 404 |
if ($fdata['checked']) { |
if ($fdata['checked']) { |
| 405 |
$type = flexiconvert_get_type($fdata['title']); |
$type = flexiconvert_get_type($fdata['title']); |
| 406 |
$q = db_query("SELECT r.*, n.created, n.status, n.changed, n.comment, n.promote, n.moderate, n.sticky FROM {node_revisions} r, {node} n WHERE n.nid=r.nid AND n.type='%s'", ('flexinode-' . $type->ctype_id)); |
$q = db_query("SELECT r.*, n.created, n.status, n.changed, n.comment, n.promote, n.moderate, n.sticky FROM {node_revisions} r, {node} n WHERE n.nid=r.nid AND n.type='%s'", ('flexinode-' . $type->ctype_id)); |
| 407 |
|
|
| 408 |
while ($node = db_fetch_object($q)) { |
while ($node = db_fetch_object($q)) { |
| 409 |
$resave = new stdClass(); |
$resave = new stdClass(); |
| 410 |
$resave->nid = $node->nid; |
$resave->nid = $node->nid; |
| 411 |
$resave->vid = $node->vid; |
$resave->vid = $node->vid; |
| 412 |
$resave->type = _flexiconvert_fix_single_type(strtolower($type->name)); |
$resave->type = _flexiconvert_fix_single_type(strtolower($type->name)); |
| 413 |
$resave->title = $node->title; |
$resave->title = $node->title; |
| 414 |
$resave->body = $node->body; |
$resave->body = $node->body; |
| 415 |
$resave->teaser = $node->teaser; |
$resave->teaser = $node->teaser; |
| 416 |
$resave->uid = $node->uid; |
$resave->uid = $node->uid; |
| 417 |
|
|
| 418 |
$resave->status = $node->status; |
$resave->status = $node->status; |
| 419 |
$resave->created = $node->created; |
$resave->created = $node->created; |
| 420 |
$resave->changed = time(); |
$resave->changed = time(); |
| 421 |
$resave->comment = $node->comment; |
$resave->comment = $node->comment; |
| 422 |
$resave->promote = $node->promote; |
$resave->promote = $node->promote; |
| 423 |
$resave->moderate = $node->moderate; |
$resave->moderate = $node->moderate; |
| 424 |
$resave->sticky = $node->sticky; |
$resave->sticky = $node->sticky; |
| 425 |
|
|
| 426 |
$fq = db_query('SELECT * FROM {flexinode_data} WHERE nid=%d', $resave->nid); |
$fq = db_query('SELECT * FROM {flexinode_data} WHERE nid=%d', $resave->nid); |
| 427 |
// Get the flexinode field data. |
// Get the flexinode field data. |
| 428 |
|
|
| 429 |
while ($fnode = db_fetch_object($fq)) { |
while ($fnode = db_fetch_object($fq)) { |
| 430 |
foreach ($type->fields as $fd) { |
foreach ($type->fields as $fd) { |
| 431 |
if ($fd['field_id'] == $fnode->field_id) { |
if ($fd['field_id'] == $fnode->field_id) { |
| 432 |
$field_name = trim($fd['label']); |
$field_name = trim($fd['label']); |
| 433 |
$field_name = drupal_strtolower($field_name); |
$field_name = drupal_strtolower($field_name); |
| 434 |
$field_name = str_replace(array(' ', '-'), '_', $field_name); |
$field_name = str_replace(array(' ', '-'), '_', $field_name); |
| 435 |
$field_name = preg_replace('/[^a-z0-9_]/', '', $field_name); |
$field_name = preg_replace('/[^a-z0-9_]/', '', $field_name); |
| 436 |
$field_name = 'field_'. $field_name; |
$field_name = 'field_'. $field_name; |
| 437 |
$field_name = substr($field_name, 0, 31); |
$field_name = substr($field_name, 0, 31); |
| 438 |
|
|
| 439 |
$resave->$field_name = array(array('value' => $fnode->textual_data)); |
$resave->$field_name = array(array('value' => $fnode->textual_data)); |
| 440 |
} |
} |
| 441 |
} |
} |
| 442 |
} |
} |
| 443 |
|
|
| 444 |
content_submit($resave); |
content_submit($resave); |
| 445 |
// No need to resave the node again-- it already exists! |
// No need to resave the node again-- it already exists! |
| 446 |
// So instead we go straight to CCK because it will need |
// So instead we go straight to CCK because it will need |
| 447 |
// to populate the content type tables. |
// to populate the content type tables. |
| 448 |
} |
} |
| 449 |
} |
} |
| 450 |
db_query("UPDATE {node} SET type='%s' WHERE type='%s'", _flexiconvert_fix_single_type(strtolower($type->name)), ('flexinode-' . $type->ctype_id)); |
db_query("UPDATE {node} SET type='%s' WHERE type='%s'", _flexiconvert_fix_single_type(strtolower($type->name)), ('flexinode-' . $type->ctype_id)); |
| 451 |
} // End data conversion loop... |
} // End data conversion loop... |
| 452 |
|
|
| 453 |
menu_rebuild(); |
menu_rebuild(); |
| 454 |
|
|
| 455 |
// If the type "image" exists but the image.module |
// If the type "image" exists but the image.module |
| 456 |
// file does not, we've got problems. |
// file does not, we've got problems. |
| 457 |
if ($image_type_exists && !$image_module_exists) { |
if ($image_type_exists && !$image_module_exists) { |
| 458 |
drupal_set_message(t('All content was converted (including image types) but it was found that your installation contains the content type "image" but the image.module file was not enabled. If this is true or you don\'t have alternate "image" type, then content of type "image" may cause errors upon viewing and/or editing. It is strongly advised that you enable the image module and upload all of the respective image files to your files directory.'), 'error'); |
drupal_set_message(t('All content was converted (including image types) but it was found that your installation contains the content type "image" but the image.module file was not enabled. If this is true or you don\'t have alternate "image" type, then content of type "image" may cause errors upon viewing and/or editing. It is strongly advised that you enable the image module and upload all of the respective image files to your files directory.'), 'error'); |
| 459 |
} |
} |
| 460 |
} |
} |
| 461 |
|
|
| 462 |
/** |
/** |
| 474 |
* function argument. |
* function argument. |
| 475 |
*/ |
*/ |
| 476 |
function _flexiconvert_field_to_cck($type) { |
function _flexiconvert_field_to_cck($type) { |
| 477 |
if (is_string($type)) { |
if (is_string($type)) { |
| 478 |
switch (strtolower($type)) { |
switch (strtolower($type)) { |
| 479 |
case 'url': |
case 'url': |
| 480 |
case 'email': |
case 'email': |
| 481 |
case 'text': |
case 'text': |
| 482 |
case 'textarea': |
case 'textarea': |
| 483 |
case 'textfield': |
case 'textfield': |
| 484 |
return 'text'; |
return 'text'; |
| 485 |
|
|
| 486 |
default: |
default: |
| 487 |
return strtolower($type); |
return strtolower($type); |
| 488 |
} |
} |
| 489 |
} |
} |
| 490 |
} |
} |
| 491 |
|
|
| 492 |
/** |
/** |
| 502 |
* each word converted to a single underscore. |
* each word converted to a single underscore. |
| 503 |
*/ |
*/ |
| 504 |
function _flexiconvert_fix_single_type($type = '') { |
function _flexiconvert_fix_single_type($type = '') { |
| 505 |
if (is_string($type)) { |
if (is_string($type)) { |
| 506 |
return preg_replace('/[\s]+/', '_', $type); |
return preg_replace('/[\s]+/', '_', $type); |
| 507 |
} |
} |
| 508 |
} |
} |