You are here

recipe.install in Recipe 7.2

Same filename and directory in other branches
  1. 5 recipe.install
  2. 6 recipe.install
  3. 7 recipe.install

Install, update and uninstall functions for the recipe module.

File

recipe.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the recipe module.
 */

/**
 * Implements hook_install().
 */
function recipe_install() {

  // Set up the recipe node type.
  require_once dirname(__FILE__) . '/includes/recipe.node_type.inc';
  _recipe_install_node_type();
}

/**
 * Implements hook_uninstall().
 *
 * Automatically drops tables in hook_schema.
 */
function recipe_uninstall() {

  // Delete the recipe node fields.
  require_once dirname(__FILE__) . '/includes/recipe.node_type.inc';
  _recipe_uninstall_node_type();

  // remove node revisions
  db_query('DELETE FROM {node_revision} WHERE nid in (select nid from {node} WHERE type = :type)', array(
    ':type' => 'recipe',
  ));

  // remove recipe nodes
  db_query('DELETE FROM {node} WHERE type = :type', array(
    ':type' => 'recipe',
  ));

  // remove variables
  db_query('DELETE FROM {variable} WHERE name like :type', array(
    ':type' => 'recipe_%',
  ));
}

/**
 * Implements hook_schema().
 */
function recipe_schema() {
  $schema['recipe'] = array(
    'fields' => array(
      'nid' => array(
        'description' => 'The primary identifier for a recipe.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'source' => array(
        'type' => 'varchar',
        'not null' => FALSE,
        'length' => 255,
      ),
      'yield' => array(
        'type' => 'int',
        'unsigned' => FALSE,
        'not null' => TRUE,
      ),
      'yield_unit' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The units for the yield field(servings, people, cans, cookies, etc).',
      ),
      'description' => array(
        'type' => 'text',
      ),
      'instructions' => array(
        'type' => 'text',
      ),
      'notes' => array(
        'type' => 'text',
      ),
      'preptime' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'default' => 0,
      ),
      'cooktime' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'nid',
    ),
  );
  $schema['recipe_ingredient'] = array(
    'description' => 'The base table for recipe ingredients.',
    'fields' => array(
      'id' => array(
        'description' => 'The primary identifier for a recipe ingredient.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
      ),
      'link' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  return $schema;
}

/**
 * Implements hook_field_schema().
 */
function recipe_field_schema($field) {
  return array(
    'columns' => array(
      'iid' => array(
        'description' => 'The {recipe_ingredient}.id referenced by field.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ),
      'quantity' => array(
        'type' => 'float',
        'not null' => FALSE,
      ),
      'unit_key' => array(
        'description' => 'Untranslated unit key from the units array.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'note' => array(
        'description' => 'Ingredient processing or notes related to recipe.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'indexes' => array(
      'iid' => array(
        'iid',
      ),
    ),
    'foreign keys' => array(
      'iid' => array(
        'table' => 'recipe_ingredient',
        'columns' => array(
          'iid' => 'id',
        ),
      ),
    ),
  );
}

/**
 * Loads the recipe_units table.
 */
function _recipe_populate_units() {
  $units = _recipe_get_default_units();
  foreach ($units as $u) {
    db_insert('recipe_unit')
      ->fields(array(
      'name' => $u['name'],
      'abbreviation' => $u['abbreviation'],
      'metric' => $u['metric'],
      'type' => $u['type'],
      'aliases' => $u['aliases'],
    ))
      ->execute();
  }
  return TRUE;
}

/**
 * Returns an array of the default unit values.
 *
 * This is used to fill the recipe_unit table.
 */
function _recipe_get_default_units() {
  $units = array(
    array(
      'name' => 'Slice',
      'abbreviation' => 'sli',
      'metric' => 0,
      'type' => 'Unit',
      'aliases' => '',
    ),
    array(
      'name' => 'Unit',
      'abbreviation' => '',
      'metric' => 0,
      'type' => 'Unit',
      'aliases' => 'each,ea,whole',
    ),
    array(
      'name' => 'Clove',
      'abbreviation' => 'clv',
      'metric' => 0,
      'type' => 'Unit',
      'aliases' => '',
    ),
    array(
      'name' => 'Pinch',
      'abbreviation' => 'pn',
      'metric' => 0,
      'type' => 'Unit',
      'aliases' => '',
    ),
    array(
      'name' => 'Package',
      'abbreviation' => 'pk',
      'metric' => 0,
      'type' => 'Unit',
      'aliases' => 'pack',
    ),
    array(
      'name' => 'Can',
      'abbreviation' => 'cn',
      'metric' => 0,
      'type' => 'Unit',
      'aliases' => 'jar',
    ),
    array(
      'name' => 'Drop',
      'abbreviation' => 'dr',
      'metric' => 0,
      'type' => 'Unit',
      'aliases' => '',
    ),
    array(
      'name' => 'Bunch',
      'abbreviation' => 'bn',
      'metric' => 0,
      'type' => 'Unit',
      'aliases' => '',
    ),
    array(
      'name' => 'Dash',
      'abbreviation' => 'ds',
      'metric' => 0,
      'type' => 'Unit',
      'aliases' => '',
    ),
    array(
      'name' => 'Carton',
      'abbreviation' => 'ct',
      'metric' => 0,
      'type' => 'Unit',
      'aliases' => '',
    ),
    array(
      'name' => 'Cup',
      'abbreviation' => 'c',
      'metric' => 0,
      'type' => 'Volume',
      'aliases' => '',
    ),
    array(
      'name' => 'Tablespoon',
      'abbreviation' => 'T',
      'metric' => 0,
      'type' => 'Volume',
      'aliases' => 'tbsp,tb',
    ),
    array(
      'name' => 'Teaspoon',
      'abbreviation' => 't',
      'metric' => 0,
      'type' => 'Volume',
      'aliases' => 'tsp',
    ),
    array(
      'name' => 'Pound',
      'abbreviation' => 'lb',
      'metric' => 0,
      'type' => 'Mass',
      'aliases' => '',
    ),
    array(
      'name' => 'Ounce',
      'abbreviation' => 'oz',
      'metric' => 0,
      'type' => 'Mass',
      'aliases' => '',
    ),
    array(
      'name' => 'Pint',
      'abbreviation' => 'pt',
      'metric' => 0,
      'type' => 'Volume',
      'aliases' => '',
    ),
    array(
      'name' => 'Quart',
      'abbreviation' => 'q',
      'metric' => 0,
      'type' => 'Volume',
      'aliases' => '',
    ),
    array(
      'name' => 'Gallon',
      'abbreviation' => 'gal',
      'metric' => 0,
      'type' => 'Volume',
      'aliases' => '',
    ),
    array(
      'name' => 'Milligram',
      'abbreviation' => 'mg',
      'metric' => 1,
      'type' => 'Mass',
      'aliases' => '',
    ),
    array(
      'name' => 'Centigram',
      'abbreviation' => 'cg',
      'metric' => 1,
      'type' => 'Mass',
      'aliases' => '',
    ),
    array(
      'name' => 'Gram',
      'abbreviation' => 'g',
      'metric' => 1,
      'type' => 'Mass',
      'aliases' => '',
    ),
    array(
      'name' => 'Kilogram',
      'abbreviation' => 'kg',
      'metric' => 1,
      'type' => 'Mass',
      'aliases' => '',
    ),
    array(
      'name' => 'Millilitre',
      'abbreviation' => 'ml',
      'metric' => 1,
      'type' => 'Volume',
      'aliases' => '',
    ),
    array(
      'name' => 'Centilitre',
      'abbreviation' => 'cl',
      'metric' => 1,
      'type' => 'Volume',
      'aliases' => '',
    ),
    array(
      'name' => 'Litre',
      'abbreviation' => 'l',
      'metric' => 1,
      'type' => 'Volume',
      'aliases' => '',
    ),
    array(
      'name' => 'Decilitre',
      'abbreviation' => 'dl',
      'metric' => 1,
      'type' => 'Volume',
      'aliases' => '',
    ),
    array(
      'name' => 'Tablespoon (Metric)',
      'abbreviation' => 'tbsp',
      'metric' => 1,
      'type' => 'Volume',
      'aliases' => 'T',
    ),
    array(
      'name' => 'Teaspoon (Metric)',
      'abbreviation' => 'tsp',
      'metric' => 1,
      'type' => 'Volume',
      'aliases' => 't',
    ),
    array(
      'name' => 'Unknown',
      'abbreviation' => 'Unknown',
      'metric' => 0,
      'type' => 'Unit',
      'aliases' => '',
    ),
  );
  return $units;
}

/**
 * Takes schema from 5.x-1.0 version up to 6.x, pre 6100
 */
function recipe_update_6000() {
  $ret = array();
  db_change_field($ret, 'recipe', 'nid', 'nid', array(
    'type' => 'serial',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'size' => 'normal',
  ));
  db_change_field($ret, 'recipe', 'yield', 'yield', array(
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => FALSE,
  ));
  db_change_field($ret, 'recipe_node_ingredient', 'unit_id', 'unit_id', array(
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
  ));
  db_change_field($ret, 'recipe_node_ingredient', 'quantity', 'quantity', array(
    'type' => 'float',
    'not null' => FALSE,
  ));
  db_change_field($ret, 'recipe_ingredient', 'link', 'link', array(
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
  ));
  db_change_field($ret, 'recipe_unit', 'id', 'id', array(
    'type' => 'serial',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'size' => 'normal',
  ));
  db_change_field($ret, 'recipe_unit', 'name', 'name', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => FALSE,
  ));
  db_change_field($ret, 'recipe_unit', 'abbreviation', 'abbreviation', array(
    'type' => 'varchar',
    'length' => 8,
    'not null' => FALSE,
  ));
  db_change_field($ret, 'recipe_unit', 'metric', 'metric', array(
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
  ));
  db_change_field($ret, 'recipe_unit', 'type', 'type', array(
    'type' => 'varchar',
    'length' => 6,
    'not null' => TRUE,
    'default' => 'Mass',
  ));
  return $ret;
}

/**
 * Adds weight column to recipe_node_ingredient
 */
function recipe_update_6100() {
  db_add_field('recipe_node_ingredient', 'weight', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));
}

/**
 * Adds aliases column to recipe_unit
 */
function recipe_update_6101() {
  db_add_field('recipe_unit', 'aliases', array(
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
    'default' => '',
  ));
  $units = _recipe_get_default_units();
  foreach ($units as $u) {
    db_update('recipe_unit')
      ->condition('name', $u['name'])
      ->fields(array(
      'aliases' => $u['aliases'],
    ))
      ->execute();
  }
}

/**
 * Adds note column to the recipe_node_ingredient table.
 */
function recipe_update_6102() {
  db_add_field('recipe_node_ingredient', 'note', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));
}

/**
 * Adds yield_unit column to the recipe table.
 */
function recipe_update_6103() {
  db_add_field('recipe', 'yield_unit', array(
    'type' => 'varchar',
    'length' => 64,
    'not null' => TRUE,
    'default' => '',
  ));
}

/**
 * Fixes bad index depth setting value from recipe-6.x-1.1
 */
function recipe_update_6104() {
  $ret = array();
  $depth = variable_get('recipe_index_depth', 4);
  if ($depth <= 0) {

    // Let the stock default value handle this.
    variable_del('recipe_index_depth');
  }
  return $ret;
}

/**
 * Adds description column to the recipe table.
 */
function recipe_update_7000() {

  // Add the new description property to the recipe table.
  db_add_field('recipe', 'description', array(
    'type' => 'text',
  ));

  // Copy the value from the body field created by drupal7 conversion.
  $query = db_select('field_data_body', 'fdb');
  $query
    ->innerJoin('node', 'n', 'n.vid = fdb.revision_id');
  $query
    ->fields('fdb', array(
    'entity_type',
    'bundle',
    'entity_id',
    'revision_id',
    'body_value',
  ))
    ->fields('n', array(
    'nid',
    'type',
    'status',
    'comment',
    'promote',
    'sticky',
    'language',
  ))
    ->condition('n.type', 'recipe')
    ->orderBy('fdb.revision_id', 'ASC');
  $recipes = $query
    ->execute();
  foreach ($recipes as $recipe) {
    db_update('recipe')
      ->fields(array(
      'description' => $recipe->body_value,
    ))
      ->condition('nid', $recipe->nid)
      ->execute();
  }

  // Unbind body field from the recipe node type.
  $field = field_info_field('body');
  $instance = field_info_instance('node', 'body', 'recipe');
  if (!empty($field)) {
    field_delete_instance($instance, FALSE);
  }
}

/**
 * Rebuilds the rdf mapping table.
 */
function recipe_update_7001() {
  if (module_exists("rdf")) {
    $modules = module_implements('rdf_mapping');
    rdf_modules_installed($modules);
  }
}

/**
 * Adds cooktime column to the recipe table.
 */
function recipe_update_7002() {

  // Add the new cooktime field to the recipe table.
  db_add_field('recipe', 'cooktime', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'default' => 0,
  ));

  // Refresh RDF mapping to pick up new field.
  if (module_exists("rdf")) {
    $modules = module_implements('rdf_mapping');
    rdf_modules_installed($modules);
  }
}

/**
 * Adds unit_key column to the recipe_node_ingredient table.
 */
function recipe_update_7003() {

  // Add the new unit_key field to the recipe_node_ingredient table.
  db_add_field('recipe_node_ingredient', 'unit_key', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));

  // Update unit_key field from the old unit name field from the recipe_unit table.
  $data_errors = FALSE;
  $query = 'update {recipe_node_ingredient} rni set rni.unit_key = (select lower(max(ru.name)) from {recipe_unit} ru where ru.id=rni.unit_id)';
  if (db_query($query) == FALSE) {
    $data_errors = TRUE;
  }

  // For the unit keys that changed, update them to the new key.
  $changes = array(
    'pint' => 'us liquid pint',
    'quart' => 'us liquid quart',
    'gallon' => 'us gallon',
    'tablespoon (metric)' => 'tablespoon',
    'metric tablespoon' => 'tablespoon',
    'teaspoon (metric)' => 'teaspoon',
    'metric teaspoon' => 'teaspoon',
    'millilitre' => 'milliliter',
    'centilitre' => 'centiliter',
    'decilitre' => 'decilitre',
    'litre' => 'liter',
  );
  foreach ($changes as $old_key => $new_key) {
    db_update('recipe_node_ingredient')
      ->condition('unit_key', $old_key)
      ->fields(array(
      'unit_key' => $new_key,
    ))
      ->execute();
  }

  // Remove unit_id field from the recipe_node_ingredient table.
  db_drop_field('recipe_node_ingredient', 'unit_id');

  // Don't remove the recipe_unit table at this time.
  // People may have user-entered content in there.
}

/**
 * Update recipe RDF mapping to use the schema.org terms.
 */
function recipe_update_7004() {
  if (module_exists("rdf")) {
    rdf_mapping_save(current(recipe_rdf_mapping()));
  }
}

/**
 * Migrate the recipe_help variable to the recipe node type help setting.
 */
function recipe_update_7200() {
  $recipe_help = variable_get('recipe_help', '');
  if (!empty($recipe_help)) {
    $node_type = node_type_load('recipe');
    if (empty($node_type->help)) {
      $node_type->help = $recipe_help;
      node_type_save($node_type);
    }
  }
  variable_del('recipe_help');
}

/**
 * Installs the default recipe node type.
 */
function recipe_update_7201() {

  // Install the new Recipe node type and its default fields.
  require_once dirname(__FILE__) . '/includes/recipe.node_type.inc';
  _recipe_install_node_type();
}

/**
 * Move recipe descriptions in {recipe}.description into the new field.
 */
function recipe_update_7202(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_nid'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {recipe} WHERE description IS NOT NULL')
      ->fetchField();
  }
  $query = db_select('recipe', 'r');
  $query
    ->join('node', 'n', 'r.nid = n.nid');
  $query
    ->fields('r', array(
    'nid',
    'description',
  ))
    ->fields('n', array(
    'vid',
  ))
    ->condition('r.nid', $sandbox['current_nid'], '>')
    ->isNotNull('description')
    ->orderBy('nid', 'ASC')
    ->range(0, 100);
  $recipes = $query
    ->execute();
  foreach ($recipes as $recipe) {
    db_insert('field_data_recipe_description')
      ->fields(array(
      'entity_type' => 'node',
      'bundle' => 'recipe',
      'entity_id' => $recipe->nid,
      'revision_id' => $recipe->vid,
      'language' => LANGUAGE_NONE,
      'delta' => 0,
      'recipe_description_value' => $recipe->description,
      'recipe_description_summary' => '',
      'recipe_description_format' => 'filtered_html',
    ))
      ->execute();
    $sandbox['progress']++;
    $sandbox['current_nid'] = $recipe->nid;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}

/**
 * Move recipe instructions in {recipe}.instructions into new the field.
 */
function recipe_update_7203(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_nid'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {recipe} WHERE instructions IS NOT NULL')
      ->fetchField();
  }
  $query = db_select('recipe', 'r');
  $query
    ->join('node', 'n', 'r.nid = n.nid');
  $query
    ->fields('r', array(
    'nid',
    'instructions',
  ))
    ->fields('n', array(
    'vid',
  ))
    ->condition('r.nid', $sandbox['current_nid'], '>')
    ->isNotNull('instructions')
    ->orderBy('nid', 'ASC')
    ->range(0, 100);
  $recipes = $query
    ->execute();
  foreach ($recipes as $recipe) {
    db_insert('field_data_recipe_instructions')
      ->fields(array(
      'entity_type' => 'node',
      'bundle' => 'recipe',
      'entity_id' => $recipe->nid,
      'revision_id' => $recipe->vid,
      'language' => LANGUAGE_NONE,
      'delta' => 0,
      'recipe_instructions_value' => $recipe->instructions,
      'recipe_instructions_format' => 'filtered_html',
    ))
      ->execute();
    $sandbox['progress']++;
    $sandbox['current_nid'] = $recipe->nid;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}

/**
 * Move recipe notes in {recipe}.notes into the new field.
 */
function recipe_update_7204(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_nid'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {recipe} WHERE notes IS NOT NULL')
      ->fetchField();
  }
  $query = db_select('recipe', 'r');
  $query
    ->join('node', 'n', 'r.nid = n.nid');
  $query
    ->fields('r', array(
    'nid',
    'notes',
  ))
    ->fields('n', array(
    'vid',
  ))
    ->condition('r.nid', $sandbox['current_nid'], '>')
    ->isNotNull('notes')
    ->orderBy('nid', 'ASC')
    ->range(0, 100);
  $recipes = $query
    ->execute();
  foreach ($recipes as $recipe) {
    db_insert('field_data_recipe_notes')
      ->fields(array(
      'entity_type' => 'node',
      'bundle' => 'recipe',
      'entity_id' => $recipe->nid,
      'revision_id' => $recipe->vid,
      'language' => LANGUAGE_NONE,
      'delta' => 0,
      'recipe_notes_value' => $recipe->notes,
      'recipe_notes_format' => 'filtered_html',
    ))
      ->execute();
    $sandbox['progress']++;
    $sandbox['current_nid'] = $recipe->nid;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}

/**
 * Move recipe sources in {recipe}.source into the new field.
 */
function recipe_update_7205(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_nid'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {recipe} WHERE source IS NOT NULL')
      ->fetchField();
  }
  $query = db_select('recipe', 'r');
  $query
    ->join('node', 'n', 'r.nid = n.nid');
  $query
    ->fields('r', array(
    'nid',
    'source',
  ))
    ->fields('n', array(
    'vid',
  ))
    ->condition('r.nid', $sandbox['current_nid'], '>')
    ->isNotNull('source')
    ->orderBy('nid', 'ASC')
    ->range(0, 100);
  $recipes = $query
    ->execute();
  foreach ($recipes as $recipe) {
    db_insert('field_data_recipe_source')
      ->fields(array(
      'entity_type' => 'node',
      'bundle' => 'recipe',
      'entity_id' => $recipe->nid,
      'revision_id' => $recipe->vid,
      'language' => LANGUAGE_NONE,
      'delta' => 0,
      'recipe_source_value' => $recipe->source,
      'recipe_source_format' => 'filtered_html',
    ))
      ->execute();
    $sandbox['progress']++;
    $sandbox['current_nid'] = $recipe->nid;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}

/**
 * Move recipe preparation times in {recipe}.preptime into the new field.
 */
function recipe_update_7206(&$sandbox) {

  // Don't migrate prep times that are equal to 0. Version 7.x-1.x did not
  // display 0-value prep times, which should be replaced by an empty field.
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_nid'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {recipe} WHERE preptime IS NOT NULL && preptime != 0')
      ->fetchField();
  }
  $query = db_select('recipe', 'r');
  $query
    ->join('node', 'n', 'r.nid = n.nid');
  $query
    ->fields('r', array(
    'nid',
    'preptime',
  ))
    ->fields('n', array(
    'vid',
  ))
    ->condition('r.nid', $sandbox['current_nid'], '>')
    ->condition('r.preptime', 0, '!=')
    ->isNotNull('preptime')
    ->orderBy('nid', 'ASC')
    ->range(0, 100);
  $recipes = $query
    ->execute();
  foreach ($recipes as $recipe) {
    db_insert('field_data_recipe_prep_time')
      ->fields(array(
      'entity_type' => 'node',
      'bundle' => 'recipe',
      'entity_id' => $recipe->nid,
      'revision_id' => $recipe->vid,
      'language' => LANGUAGE_NONE,
      'delta' => 0,
      'recipe_prep_time_value' => $recipe->preptime,
    ))
      ->execute();
    $sandbox['progress']++;
    $sandbox['current_nid'] = $recipe->nid;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}

/**
 * Move recipe cooking times in {recipe}.cooktime into the new field.
 */
function recipe_update_7207(&$sandbox) {

  // Don't migrate cook times that are equal to 0. Version 7.x-1.x did not
  // display 0-value cook times, which should be replaced by an empty field.
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_nid'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {recipe} WHERE cooktime IS NOT NULL && cooktime != 0')
      ->fetchField();
  }
  $query = db_select('recipe', 'r');
  $query
    ->join('node', 'n', 'r.nid = n.nid');
  $query
    ->fields('r', array(
    'nid',
    'cooktime',
  ))
    ->fields('n', array(
    'vid',
  ))
    ->condition('r.nid', $sandbox['current_nid'], '>')
    ->condition('r.cooktime', 0, '!=')
    ->isNotNull('cooktime')
    ->orderBy('nid', 'ASC')
    ->range(0, 100);
  $recipes = $query
    ->execute();
  foreach ($recipes as $recipe) {
    db_insert('field_data_recipe_cook_time')
      ->fields(array(
      'entity_type' => 'node',
      'bundle' => 'recipe',
      'entity_id' => $recipe->nid,
      'revision_id' => $recipe->vid,
      'language' => LANGUAGE_NONE,
      'delta' => 0,
      'recipe_cook_time_value' => $recipe->cooktime,
    ))
      ->execute();
    $sandbox['progress']++;
    $sandbox['current_nid'] = $recipe->nid;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}

/**
 * Move ingredient references in {recipe_node_ingredient} into the new field.
 */
function recipe_update_7208(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_nid'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT id) FROM {recipe_node_ingredient} WHERE ingredient_id IS NOT NULL')
      ->fetchField();
  }
  $query = db_select('recipe_node_ingredient', 'r');
  $query
    ->join('node', 'n', 'r.nid = n.nid');
  $query
    ->fields('r', array(
    'nid',
    'unit_key',
    'quantity',
    'ingredient_id',
    'note',
  ))
    ->fields('n', array(
    'vid',
  ))
    ->condition('r.nid', $sandbox['current_nid'], '>')
    ->isNotNull('ingredient_id')
    ->orderBy('nid', 'ASC')
    ->range(0, 100);
  $ingredient_references = $query
    ->execute();

  // Track the last delta given to a node's field value.
  // {recipe_node_ingredient}.weight can't be copied over since it may have
  // duplicate values, which would violate the field's primary index unique
  // integrity.
  $last_deltas = array();
  foreach ($ingredient_references as $ingredient_reference) {
    $nid = $ingredient_reference->nid;
    if (isset($last_deltas[$nid])) {
      $last_deltas[$nid]++;
    }
    else {
      $last_deltas[$nid] = 0;
    }
    db_insert('field_data_recipe_ingredient')
      ->fields(array(
      'entity_type' => 'node',
      'bundle' => 'recipe',
      'entity_id' => $nid,
      'revision_id' => $ingredient_reference->vid,
      'language' => LANGUAGE_NONE,
      'delta' => $last_deltas[$nid],
      'recipe_ingredient_iid' => $ingredient_reference->ingredient_id,
      'recipe_ingredient_quantity' => $ingredient_reference->quantity,
      'recipe_ingredient_unit_key' => $ingredient_reference->unit_key,
      'recipe_ingredient_note' => $ingredient_reference->note,
    ))
      ->execute();
    $sandbox['progress']++;
    $sandbox['current_nid'] = $ingredient_reference->nid;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}

/**
 * Delete {recipe_node_ingredient} and remove old setting variables.
 */
function recipe_update_7209(&$sandbox) {
  db_drop_table('recipe_node_ingredient');
  variable_del('recipe_fraction_display');
  variable_del('recipe_unit_display');
  variable_del('recipe_default_unit');
  variable_del('recipe_add_more_count');
}

Functions

Namesort descending Description
recipe_field_schema Implements hook_field_schema().
recipe_install Implements hook_install().
recipe_schema Implements hook_schema().
recipe_uninstall Implements hook_uninstall().
recipe_update_6000 Takes schema from 5.x-1.0 version up to 6.x, pre 6100
recipe_update_6100 Adds weight column to recipe_node_ingredient
recipe_update_6101 Adds aliases column to recipe_unit
recipe_update_6102 Adds note column to the recipe_node_ingredient table.
recipe_update_6103 Adds yield_unit column to the recipe table.
recipe_update_6104 Fixes bad index depth setting value from recipe-6.x-1.1
recipe_update_7000 Adds description column to the recipe table.
recipe_update_7001 Rebuilds the rdf mapping table.
recipe_update_7002 Adds cooktime column to the recipe table.
recipe_update_7003 Adds unit_key column to the recipe_node_ingredient table.
recipe_update_7004 Update recipe RDF mapping to use the schema.org terms.
recipe_update_7200 Migrate the recipe_help variable to the recipe node type help setting.
recipe_update_7201 Installs the default recipe node type.
recipe_update_7202 Move recipe descriptions in {recipe}.description into the new field.
recipe_update_7203 Move recipe instructions in {recipe}.instructions into new the field.
recipe_update_7204 Move recipe notes in {recipe}.notes into the new field.
recipe_update_7205 Move recipe sources in {recipe}.source into the new field.
recipe_update_7206 Move recipe preparation times in {recipe}.preptime into the new field.
recipe_update_7207 Move recipe cooking times in {recipe}.cooktime into the new field.
recipe_update_7208 Move ingredient references in {recipe_node_ingredient} into the new field.
recipe_update_7209 Delete {recipe_node_ingredient} and remove old setting variables.
_recipe_get_default_units Returns an array of the default unit values.
_recipe_populate_units Loads the recipe_units table.