From d5e7426c25c8aa33bdcdb0aee7ae65e5970ee6c7 Mon Sep 17 00:00:00 2001
From: Henrique Recidive <recidive@gmail.com>
Date: Wed, 11 Apr 2012 11:49:57 -0300
Subject: [PATCH] Issue #1357058: Follow up. Fixing some notices with UUID
 integration stuff. Making scheduling first revision work
 again. Making default values work again. Making delete
 revision operation show up only if there are more then one
 revision.

---
 ers.module                                |    3 ++-
 plugins/entity/ERSEntityDefault.class.php |   24 +++++++++++++++++-------
 plugins/entity/ERSEntityNode.class.php    |   20 +++++++++++++-------
 3 files changed, 32 insertions(+), 15 deletions(-)

diff --git a/ers.module b/ers.module
index 1293b94..8852c49 100644
--- a/ers.module
+++ b/ers.module
@@ -445,12 +445,13 @@ function ers_set_on_edit_path($status = TRUE) {
  */
 function ers_entity_schedule_form($entity_type, $entity, $options = array()) {
   list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
-  $handler = ers_entity_plugin_get_handler($entity_type);
 
   if (!user_access("publish $entity_type $bundle content")) {
     return array();
   }
 
+  $handler = ers_entity_plugin_get_handler($entity_type);
+
   // Set default options.
   $options += array(
     'checkbox' => TRUE,
diff --git a/plugins/entity/ERSEntityDefault.class.php b/plugins/entity/ERSEntityDefault.class.php
index 413eb02..b012b2b 100644
--- a/plugins/entity/ERSEntityDefault.class.php
+++ b/plugins/entity/ERSEntityDefault.class.php
@@ -395,18 +395,22 @@ class ERSEntityDefault implements ERSEntityInterface {
 
     // Do we have to schedule publishing this revision?
     if (!empty($entity->ers_new_schedule)) {
+      // Set the schedule to current revision id, or 0 for unpublishing. We need
+      // this again here since ers_schedule_revision_id doesn't survive entity
+      // saving stuff.
+      $handler = ers_entity_plugin_get_handler($this->entity_type);
+      if ($handler->supports_publishing_flag) {
+        $entity->ers_schedule_revision_id = ($entity->ers_schedule_selector == 'publish' ? $revision_id : 0);
+      }
+      else {
+        $entity->ers_schedule_revision_id = $revision_id;
+      }
+
       $this->update_entity_schedule($entity, $entity->ers_new_schedule, $entity->ers_schedule_revision_id);
     }
 
     // Store state information.
     $this->update_entity_state($entity);
-
-    // When publishing a revision immediately after inserting the entity
-    // set_published_revision_current() doesn't get called, so we need to call
-    // it here to make sure entity types do what they need to do in this method.
-    if (!empty($entity->published_revision_id)) {
-      $this->set_published_revision_current($entity_id, $entity);
-    }
   }
 
   public function hook_entity_update($entity) {
@@ -513,6 +517,12 @@ class ERSEntityDefault implements ERSEntityInterface {
           ->condition('schedule_id', $entity->ers_schedule[$revision_id]->schedule_id)
           ->execute();
       }
+
+      // When publishing a revision immediately after inserting the entity
+      // set_published_revision_current() doesn't get called, so we need to call
+      // it here to make sure entity types do what they need to do in this method.
+      list($entity_id, $old_revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
+      $this->set_published_revision_current($entity_id, $entity);
     }
   }
 
diff --git a/plugins/entity/ERSEntityNode.class.php b/plugins/entity/ERSEntityNode.class.php
index 18fcda2..5bf4498 100644
--- a/plugins/entity/ERSEntityNode.class.php
+++ b/plugins/entity/ERSEntityNode.class.php
@@ -51,7 +51,7 @@ class ERSEntityNode extends ERSEntityDefault {
     $uid = $published_revision->revision_uid;
 
     // If node is unpublished, publish it.
-    $published_revision->status = !empty($entity->published_revision_id);
+    $published_revision->status = empty($entity->published_revision_id) ? NODE_NOT_PUBLISHED : NODE_PUBLISHED;
 
     node_save($published_revision);
     if ($uid != $GLOBALS['user']->uid) {
@@ -105,6 +105,7 @@ class ERSEntityNode extends ERSEntityDefault {
           $form['revision_information']['revision']['#default_value'] = FALSE;
           $form['revision_information']['revision']['#description'] = t('You are currently editing the draft revision. This draft will not be published until it is published from the revisions tab.');
         }
+        // @todo: maybe we should get default value from revision being edited?
         $publish_default = variable_get('ers_publish_draft_' . $node->type, TRUE);
       }
       else {
@@ -120,7 +121,9 @@ class ERSEntityNode extends ERSEntityDefault {
         '#group' => 'additional_settings',
       );
       $form['ers'] += ers_entity_schedule_form('node', $node);
-      $form['ers']['ers_publish']['#default_value'] = $publish_default;
+      // Set default values.
+      $form['ers']['ers_schedule_add']['#default_value'] = $publish_default;
+      $form['ers']['ers']['ers_schedule_selector']['#default_value'] = $publish_default ? 'publish' : NULL;
 
       $form['#submit'][] = 'ers_node_form_submit';
     }
@@ -206,7 +209,8 @@ class ERSEntityNode extends ERSEntityDefault {
           $operations['data'][] = '<strong>' . t('draft') . '</strong>';
         }
 
-        if ($delete_permission) {
+        // Only show up delete operation if there are more then one revision.
+        if (count($revisions) > 1 && $delete_permission) {
           $operations['data'][] = l(t('delete'), "node/$node->nid/revisions/$revision->vid/delete");
         }
         $schedule = array('data' => array());
@@ -348,12 +352,14 @@ class ERSEntityNode extends ERSEntityDefault {
           $node->draft_revision_vuuid = $vuuids[$node->draft_revision_id];
         }
 
-        foreach ($node->ers_schedule as $vid => &$item) {
-          if (is_numeric($item->revision_id)) {
-            $item->revision_id = $vuuids[$item->revision_id];
+        if (!empty($node->ers_schedule)) {
+          foreach ($node->ers_schedule as $vid => &$item) {
+            if (is_numeric($item->revision_id)) {
+              $item->revision_id = $vuuids[$item->revision_id];
+            }
           }
+          unset($item);
         }
-        unset($item);
 
         $revisions = array();
         foreach ($vuuids as $vid => $vuuid) {
-- 
1.7.9.1

