From 4016b8d32a9deeb998ec84003cee1311e6ce1230 Mon Sep 17 00:00:00 2001
From: alanmackenzie <alanmackenzie@1045032.no-reply.drupal.org>
Date: Wed, 8 Aug 2012 21:32:36 +0100
Subject: [PATCH] Fixed bad updatehook

---
 simpleads.updates.inc |  121 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 120 insertions(+), 1 deletion(-)

diff --git a/simpleads.updates.inc b/simpleads.updates.inc
index 71b5b68..b2fb780 100644
--- a/simpleads.updates.inc
+++ b/simpleads.updates.inc
@@ -521,4 +521,123 @@ function simpleads_update_7004(&$sandbox) {
     ),
   );
   field_update_instance($instance);
-}
\ No newline at end of file
+}
+
+
+/**
+ * Create a new field for Ad status. Disable comments on SimpleAds content type.
+  */
+function simpleads_update_7005(&$sandbox) {
+
+  $t = get_t();
+
+  // Cleanup from previous failed update attempts.
+  // see: http://drupal.org/node/1594798
+  if (field_info_field("field_ad_status") !== NULL) {
+    // Find all ads that have been set inactive so we can
+    // reset this value later.
+    $query = db_select('node', 'n');
+    $query->join('field_data_field_ad_status', 's', 'n.nid = s.entity_id');
+    $results = $query->fields('n', array('nid'))
+      ->condition('n.type', 'simpleads')
+      ->condition('s.field_ad_status_value', 0)
+      ->execute();
+
+    $inactive_ad_nids = array();
+    foreach ($results as $row) {
+      $inactive_ad_nids[$row->nid] = $row->nid;
+    }
+
+    // Delete the field so we can properly complete the update
+    // as a normal execution would.
+    field_delete_field("field_ad_status");
+    field_purge_batch(100);
+  }
+
+  var_dump($inactive_ad_nids);
+  $fields = array(
+    array(
+      'field_name' => 'field_ad_status',
+      'type' => 'list_boolean',
+      'cardinality' => 1,
+      'translatable' => TRUE,
+      'settings' => array(
+        'allowed_values' => array(
+          0 => $t('This ad is inactive'),
+          1 => $t('This ad is active'),
+        ),
+        'allowed_values_function' => '',
+      ),
+      'module' => 'list',
+      'translatable' => TRUE,
+      'entity_types' => array(),
+    ),
+  );
+  foreach ($fields as $field) {
+    field_create_field($field);
+  }
+
+  $instance = array(
+    'field_name' => 'field_ad_status',
+    'entity_type' => 'node',
+    'bundle' => 'simpleads',
+    'label' => $t('Status'),
+    'description' => $t('Please use this field to show or hide ads even if the ad is active and published.'),
+    'required' => FALSE,
+    'widget' => array(
+      'type' => 'options_onoff',
+      'settings' => array(
+        'display_label' => 0,
+      ),
+      'weight' => 5,
+      'module' => 'options',
+    ),
+    'default_value' => array(
+      0 => array(
+        'value' => 1,
+      ),
+    ),
+    'settings' => array(
+      'user_register_form' => FALSE,
+    ),
+    'display' => array(
+      'default' => array(
+        'label' => 'above',
+        'type' => 'list_default',
+        'module' => 'list',
+        'settings' => array(),
+      ),
+      'teaser' => array(
+        'label' => 'above',
+        'settings' => array(),
+        'type' => 'hidden',
+        'weight' => 0,
+      ),
+    ),
+  );
+  field_create_instance($instance);
+
+  // Set all published ads to be active if they were not set inactive in a
+  // previous failed run.
+  $result = db_select('node', 'n')
+    ->fields('n', array('nid'))
+    ->condition('n.type', 'simpleads')
+    ->condition('n.status', 1)
+    ->execute();
+  foreach ($result as $row) {
+    $node = node_load($row->nid);
+    if (is_array($inactive_ad_nids) && array_key_exists($row->nid, $inactive_ad_nids)) {
+      $ad_status = 0;
+    }
+    else {
+      $ad_status = 1;
+    }
+    $node->field_ad_status[$node->language][0]['value'] = $ad_status;
+    node_save($node);
+  }
+
+  // Reset menu, comments and other default options.
+  variable_set('menu_options_simpleads', array());
+  variable_set('node_options_simpleads', array());
+  variable_set('comment_simpleads', 1);
+}
--
1.7.9.5

