You are here

public static function BotchaRecipebookModel::save in BOTCHA Spam Prevention 6.2

Same name and namespace in other branches
  1. 6.3 model/recipebook/botcha.recipebook.model.inc \BotchaRecipebookModel::save()
  2. 7.2 model/botcha_recipebook.model.inc \BotchaRecipebookModel::save()
  3. 7.3 model/recipebook/botcha.recipebook.model.inc \BotchaRecipebookModel::save()

Save recipe book to the database.

Parameters

BotchaRecipebook $recipebook:

1 call to BotchaRecipebookModel::save()
BotchaRecipebook::save in controller/botcha_recipebook.controller.inc

File

model/botcha_recipebook.model.inc, line 37
Contains BotchaRecipebookModel class.

Class

BotchaRecipebookModel
@file Contains BotchaRecipebookModel class.

Code

public static function save($recipebook) {
  db_query("DELETE FROM {botcha_recipebook_recipe} WHERE rbid = '%s'", array(
    $recipebook->id,
  ));
  db_query("DELETE FROM {botcha_recipebook_form} WHERE rbid = '%s'", array(
    $recipebook->id,
  ));
  if (!$recipebook instanceof BotchaRecipebookNone) {
    if (db_result(db_query("SELECT COUNT(*) FROM {botcha_recipebook} brb WHERE brb.id = '%s'", array(
      $recipebook->id,
    )))) {
      db_query("UPDATE {botcha_recipebook} SET title = '%s', description = '%s' WHERE id = '%s'", array(
        $recipebook->title,
        $recipebook->description,
        $recipebook->id,
      ));
    }
    else {
      db_query("INSERT INTO {botcha_recipebook} (id, title, description) VALUES('%s', '%s', '%s')", array(
        $recipebook->id,
        $recipebook->title,
        $recipebook->description,
      ));
    }

    // Save relationships between recipe book and recipes to DB.
    $recipes = $recipebook
      ->getRecipes();
    $query = "INSERT INTO {botcha_recipebook_recipe} (rbid, recipe_id) VALUES('%s', '%s')";
    foreach ($recipes as $recipe) {
      db_query($query, array(
        $recipebook->id,
        $recipe->id,
      ));
    }

    // Save relationships between recipe book and forms to DB.
    $forms = $recipebook
      ->getForms();
    foreach ($forms as $form) {
      if (db_result(db_query("SELECT COUNT(*) FROM {botcha_recipebook_form} brf WHERE brf.form_id = '%s'", array(
        $form->id,
      )))) {
        db_query("UPDATE {botcha_recipebook_form} SET rbid = '%s' WHERE form_id = '%s'", array(
          $recipebook->id,
          $form->id,
        ));
      }
      else {
        db_query("INSERT INTO {botcha_recipebook_form} (rbid, form_id) VALUES('%s', '%s')", array(
          $recipebook->id,
          $form->id,
        ));
      }
    }
  }
}