You are here

function recipe_mastercook4_import_form_submit in Recipe 7

Same name and namespace in other branches
  1. 6 plugins/recipe_mastercook4.module \recipe_mastercook4_import_form_submit()
  2. 7.2 modules/recipe_mastercook4.module \recipe_mastercook4_import_form_submit()

File

includes/recipe_mastercook4.module, line 214
recipe_mastercook4.module - Enables importing and exporting of MasterCook4 format recipes.

Code

function recipe_mastercook4_import_form_submit($form, &$form_state) {

  // save to a temp files
  $data = array();
  $validators = array(
    'file_validate_extensions' => array(
      'mxp xml',
    ),
  );
  if ($file = file_save_upload('recipe_import_file', $validators, FALSE, FILE_EXISTS_RENAME)) {
    $data = file($file->uri);
    drupal_set_message(t('The attached file was successfully uploaded'));
  }
  else {
    drupal_set_message(t('The attched file failed to upload.'), 'error');
    return;
  }
  $recipe_txt = '';
  foreach ($data as $line) {
    if (preg_match("/\\* +Exported +from/i", $line)) {
      $recipe_txt = trim($recipe_txt);

      // Save recipe
      if (strlen($recipe_txt) > 0) {
        $parsed_recipe_object = recipe_mastercook4_import_single($recipe_txt);
        if (strlen($parsed_recipe_object['title']) > 0) {
          if (($node = recipe_import_get_node($parsed_recipe_object)) != FALSE) {

            // Save the recipe.
            node_save($node);
          }
        }
      }

      // Clear recipe buffer.
      $recipe_txt = '';
    }
    $recipe_txt .= $line;
  }

  // Handle the last one needed.
  $parsed_recipe_object = recipe_mastercook4_import_single($recipe_txt);
  if (strlen($parsed_recipe_object['title']) > 0) {
    if (($node = recipe_import_get_node($parsed_recipe_object)) != FALSE) {
      node_save($node);
    }
  }
}