You are here

public function BotchaRecipebookModel::save in BOTCHA Spam Prevention 6.3

Same name and namespace in other branches
  1. 6.2 model/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:

Overrides IBotchaRecipebookModel::save

File

model/recipebook/botcha.recipebook.model.inc, line 51
Contains BotchaRecipebookModel class.

Class

BotchaRecipebookModel

Code

public function save($recipebook) {

  // Catching of PDOException helps to avoid WSOD during update use case. The
  // reason is in that form_alter is called before performing an update.
  // @see http://drupal.org/node/1828710
  try {
    db_delete('botcha_recipebook_recipe')
      ->condition('rbid', $recipebook->id)
      ->execute();
    db_delete('botcha_recipebook_form')
      ->condition('rbid', $recipebook->id)
      ->execute();
    if (!$recipebook instanceof BotchaRecipebookNone) {
      db_merge('botcha_recipebook')
        ->key(array(
        'id' => $recipebook->id,
      ))
        ->fields(array(
        'title' => $recipebook->title,
        'description' => $recipebook->description,
      ))
        ->execute();

      // Save relationships between recipe book and recipes to DB.
      $recipe_ids = $recipebook
        ->getRecipes();
      foreach ($recipe_ids as $recipe_id) {
        db_insert('botcha_recipebook_recipe')
          ->fields(array(
          'rbid' => $recipebook->id,
          'recipe_id' => $recipe_id,
        ))
          ->execute();
      }

      // Save relationships between recipe book and forms to DB.
      $form_ids = $recipebook
        ->getForms();
      foreach ($form_ids as $form_id) {
        $brf = db_select('botcha_recipebook_form', 'brf')
          ->fields('brf', array(
          'rbid',
        ))
          ->condition('form_id', $form_id)
          ->execute()
          ->fetchCol();
        if (count($brf)) {
          $query = db_update('botcha_recipebook_form')
            ->condition('form_id', $form_id)
            ->fields(array(
            'rbid' => $recipebook->id,
          ));
        }
        else {
          $query = db_insert('botcha_recipebook_form')
            ->fields(array(
            'form_id' => $form_id,
            'rbid' => $recipebook->id,
          ));
        }
        $query
          ->execute();

        // Strange but true: db_merge does not work here, fails with integrity constraint violation.

        /*
                  $query->key(array('form_id', 'rbid'), array($form->id, $recipebook->id))
         ->execute();
        *
        */
      }
    }
  } catch (Exception $e) {
    if ($e instanceof PDOException) {
      watchdog_exception('BOTCHA', $e, 'Please perform an update via update.php or reinstall the BOTCHA module to fix the reason of this warning! %type: !message in %function (line %line of %file).', array(), WATCHDOG_WARNING);
    }
  }
}