You are here

recipe.install in Recipe 5

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

File

recipe.install
View source
<?php

// recipe.install
function recipe_install() {
  $s = false;
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $s = db_query("CREATE TABLE {recipe} (\n         nid int(10) unsigned NOT NULL,\n         source varchar(255),\n         yield int(2) unsigned NOT NULL,\n         instructions text,\n         notes text,\n         preptime int(10) unsigned DEFAULT '0',\n         PRIMARY KEY (nid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $s = $s && db_query("CREATE TABLE {recipe_node_ingredient} (\n         id int unsigned NOT NULL PRIMARY KEY auto_increment,\n         nid int(10) unsigned NOT NULL,\n         unit_id int(3) unsigned NOT NULL,\n         quantity double,\n         ingredient_id int(10) unsigned NOT NULL\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $s = $s && db_query("CREATE TABLE {recipe_ingredient} (\n         id int(10) unsigned NOT NULL PRIMARY KEY auto_increment,\n         name varchar(255),\n         link int(10) NOT NULL\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $s = $s && db_query("CREATE TABLE {recipe_unit} (\n         id int(3) unsigned NOT NULL PRIMARY KEY auto_increment,\n         name varchar(64) NOT NULL default '',\n         abbreviation varchar(8) NOT NULL default '',\n         metric int(1) unsigned NOT NULL default '0',\n         type enum('Mass','Volume','Unit') NOT NULL default 'Mass'\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $s = $s && recipe_populate_units();
      break;
    case 'pgsql':
      $s = db_query("CREATE TABLE {recipe} (\n         nid integer NOT NULL,\n         source varchar(255),\n         yield integer NOT NULL,\n         instructions text,\n         notes text,\n         preptime integer NOT NULL default '0',\n         PRIMARY KEY (nid)\n      );");
      $s = $s && db_query("CREATE TABLE {recipe_node_ingredient} (\n         id serial NOT NULL,\n         nid integer NOT NULL,\n         unit_id integer NOT NULL,\n         quantity real,\n         ingredient_id integer NOT NULL,\n         PRIMARY KEY(id)\n      );");
      $s = $s && db_query("CREATE TABLE {recipe_ingredient} (\n         id serial NOT NULL PRIMARY KEY,\n         name varchar(255),\n         link integer not null\n      );");
      $s = $s && db_query("CREATE TABLE {recipe_unit} (\n         id serial NOT NULL PRIMARY KEY,\n         name varchar(64) NOT NULL default '',\n         abbreviation varchar(8) NOT NULL default '',\n         metric integer NOT NULL default '0',\n         type varchar(6) NOT NULL default 'Mass'\n      );");
      $s = $s && recipe_populate_units();
      break;
  }
  if ($s) {
    drupal_set_message(t('Recipe module installed tables successfully.'));
  }
  else {
    drupal_set_message(t('The installation of the Recipe module was unsuccessful.'), 'error');
  }
}
function recipe_update_1() {
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {recipe_node_ingredient} DROP weight");
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_add_column($ret, 'recipe', 'preptime', 'integer', array(
        'not null' => TRUE,
        'default' => 0,
      ));
      break;
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {recipe} ADD COLUMN preptime int(10) NOT NULL DEFAULT 0");
      break;
  }
  return $ret;
}

/**
 * Adds a field to link ingredient to corresponding node.
 */
function recipe_update_2() {
  $ret = array();
  $ret[] = update_sql('ALTER TABLE {recipe_ingredient} ADD link INT NOT NULL');
  return $ret;
}
function recipe_populate_units() {
  $s = true;
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (1, 'Slice', 'sli', 0, 'Unit');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (2, 'Unit', '', 0, 'Unit');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (3, 'Clove', 'clv', 0, 'Unit');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (4, 'Pinch', 'pn', 0, 'Unit');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (5, 'Package', 'pk', 0, 'Unit');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (6, 'Can', 'cn', 0, 'Unit');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (7, 'Drop', 'dr', 0, 'Unit');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (8, 'Bunch', 'bn', 0, 'Unit');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (9, 'Dash', 'ds', 0, 'Unit');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (10, 'Carton', 'ct', 0, 'Unit');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (11, 'Cup', 'c', 0, 'Unit');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (12, 'Tablespoon', 'T', 0, 'Volume');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (13, 'Teaspoon', 't', 0, 'Volume');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (14, 'Pound', 'lb', 0, 'Mass');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (15, 'Ounce', 'oz', 0, 'Mass');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (16, 'Pint', 'pt', 0, 'Volume');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (17, 'Quart', 'q', 0, 'Volume');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (18, 'Gallon', 'gal', 0, 'Volume');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (19, 'Milligram', 'mg', 1, 'Mass');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (20, 'Centigram', 'cg', 1, 'Mass');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (21, 'Gram', 'g', 1, 'Mass');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (22, 'Kilogram', 'kg', 1, 'Mass');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (23, 'Millilitre', 'ml', 1, 'Volume');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (24, 'Centilitre', 'cl', 1, 'Volume');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (25, 'Litre', 'l', 1, 'Volume');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (26, 'Decilitre', 'dl', 1, 'Volume');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (27, 'Tablespoon (Metric)', 'tbsp', 1, 'Volume');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (28, 'Teaspoon (Metric)', 'tsp', 1, 'Volume');");
  $s = $s && db_query("INSERT INTO {recipe_unit} VALUES (29, 'Unknown', '', 0, 'Unit');");
  return $s;
}
function recipe_uninstall() {
  if (db_table_exists("recipe")) {
    db_query("DROP TABLE {recipe}");
  }
  if (db_table_exists("recipe_node_ingredient")) {
    db_query("DROP TABLE {recipe_node_ingredient}");
  }
  if (db_table_exists("recipe_ingredient")) {
    db_query("DROP TABLE {recipe_ingredient}");
  }
  if (db_table_exists("recipe_unit")) {
    db_query("DROP TABLE {recipe_unit}");
  }
}

Functions

Namesort descending Description
recipe_install
recipe_populate_units
recipe_uninstall
recipe_update_1
recipe_update_2 Adds a field to link ingredient to corresponding node.