You are here

recipe.install in Recipe 7

Same filename and directory in other branches
  1. 5 recipe.install
  2. 6 recipe.install
  3. 7.2 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_uninstall().
 *
 * Automatically drops tables in hook_schema.
 */
function recipe_uninstall() {

  // 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_node_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,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'unit_key' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Untranslated unit key from the units array.',
      ),
      'quantity' => array(
        'type' => 'float',
        'not null' => FALSE,
      ),
      'ingredient_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Ingredient weight within recipe.',
      ),
      'note' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Ingredient processing or notes related to recipe.',
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  $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;
}

/**
 * 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()));
  }
}

Functions

Namesort descending Description
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_get_default_units Returns an array of the default unit values.
_recipe_populate_units Loads the recipe_units table.