From 7738939917cf0df05f9f2da8f11b4770c310dc6d Mon Sep 17 00:00:00 2001
From: root <root@d7-232fmeinero.drupal.uk.sift.com>
Date: Tue, 6 Mar 2012 17:54:59 +0000
Subject: [PATCH] Issue #1418906: Added time units support.

---
 edit_limit.admin.inc |   26 ++++++++--
 edit_limit.js        |  137 +++++++++++++++++++++++++++++++++++---------------
 edit_limit.module    |   49 ++++++++++++++----
 3 files changed, 155 insertions(+), 57 deletions(-)

diff --git a/edit_limit.admin.inc b/edit_limit.admin.inc
index 85d4004..363e808 100644
--- a/edit_limit.admin.inc
+++ b/edit_limit.admin.inc
@@ -49,10 +49,20 @@ function edit_limit_settings($form, $form_state) {
     '#collapsible' => TRUE,
     '#collapsed' => FALSE,
   );
+  $time_units = array();
+  foreach (explode(',', EDIT_LIMIT_TIME_UNITS) as $unit){
+    $time_units[$unit] = $unit;
+  }
+  $form['node_limits']['edit_limit_node_time_unit'] = array(
+      '#type' => 'select',
+      '#title' => t('Node time unit'),
+      '#options' => $time_units,
+      '#default_value' => variable_get('edit_limit_node_time_unit', EDIT_LIMIT_TIME_UNIT_DEFAULT),
+  );
   $form['node_limits']['edit_limit_node_time_default'] = array(
     '#type' => 'textfield',
-    '#title' => t('Node time limit (seconds)'),
-    '#description' => t('The amount of time after a node has been saved that it can be edited (in seconds). 86400 seconds is 1 day.'),
+    '#title' => t('Node time limit'),
+    '#description' => t('The amount of time after a node has been saved that it can be edited (in unit selected above).'),
     '#size' => 4,
     '#default_value' => variable_get('edit_limit_node_time_default', EDIT_LIMIT_NODE_TIME_DEFAULT),
     '#maxlength' => 10,
@@ -74,7 +84,7 @@ function edit_limit_settings($form, $form_state) {
   $form['node_limits']['edit_limit_node_types'] = array(
     '#type' => 'select',
     '#title' => t('Content types'),
-    '#description' => t('Select the content types to apply these node limits to. Only content types selected here will have any limits applied.'),
+    '#description' => t('Select the content types to apply these node liits to. Only content types selected here will have any limits applied.'),
     '#multiple' => TRUE,
     '#options' => $types,
     '#default_value' => variable_get('edit_limit_node_types', array()),
@@ -87,10 +97,16 @@ function edit_limit_settings($form, $form_state) {
     '#collapsible' => TRUE,
     '#collapsed' => FALSE,
   );
+  $form['comment_limits']['edit_limit_comment_time_unit'] = array(
+        '#type' => 'select',
+        '#title' => t('Node time unit'),
+        '#options' => $time_units,
+        '#default_value' => variable_get('edit_limit_comment_time_unit', EDIT_LIMIT_TIME_UNIT_DEFAULT),
+  );
   $form['comment_limits']['edit_limit_comment_time'] = array(
     '#type' => 'textfield',
-    '#title' => t('Comment time limit (seconds)'),
-    '#description' => t('The amount of time after a comment has been saved that it can be edited (in seconds).'),
+    '#title' => t('Comment time limit'),
+    '#description' => t('The amount of time after a comment has been saved that it can be edited (in unit selected above).'),
     '#size' => 4,
     '#default_value' => variable_get('edit_limit_comment_time', EDIT_LIMIT_COMMENT_TIME),
     '#maxlength' => 10,
diff --git a/edit_limit.js b/edit_limit.js
index 2a02e86..011a832 100644
--- a/edit_limit.js
+++ b/edit_limit.js
@@ -1,15 +1,20 @@
 /*global Drupal: false */
 (function ($, undefined) {
 
-var comment_timer_update = function(node, seconds) {
-  if (seconds > 0) {
+var first_downgrade = true;
+
+var comment_timer_update = function(node, time_left) {
+  if(time_left > 0) {
     /* update counter */
-    setTimeout(function(){
-      seconds -= 1;
-      node.countdown.html(seconds);
-      comment_timer_update(node, seconds);
+    setTimeout(function() {
+      time_left -= 1;
+      if(seconds_to_unit(time_left, node)==1){
+        node = donwgrade_unit(node);
+      }
+      node.countdown.html(seconds_to_unit(time_left, node));
+      comment_timer_update(node, time_left);
     }, 1000);
-  }
+  } 
 
   else {
     /* Destroy counter; Remove the edit link from the DOM */
@@ -17,53 +22,103 @@ var comment_timer_update = function(node, seconds) {
   }
 };
 
-var timer_update_message = function(node, seconds) {
-  if (seconds > 0) {
+var timer_update_message = function(node, time_left) {
+  if(time_left > 0) {
     /* update counter */
-    setTimeout(function(){
-      seconds -= 1;
-      node.countdown.html(seconds);
-      timer_update_message(node, seconds);
+    setTimeout(function() {
+      time_left -= 1;
+      if(seconds_to_unit(time_left, node)==1){
+        node = donwgrade_unit(node);
+      }
+      node.countdown.html(seconds_to_unit(time_left, node));
+      timer_update_message(node, time_left);
     }, 1000);
-  }
+  } 
 
   else {
     /* destroy counter */
-    node.el
-      .removeClass('status')
-      .html(Drupal.t('This can no longer be edited.'))
-      .addClass('error')
-    ;
+    if(node.el.html().indexOf("left to edit this")!=-1){
+      node.el
+        .removeClass('status')
+        .html(Drupal.t('This can no longer be edited.'))
+        .addClass('error')
+      ;
+    }
   }
 };
 
+var donwgrade_unit = function(node) {
+  switch (node.unit.text().substring(0,3)) {
+    case 'min':
+      node.unit.text('seconds');
+      node.countdown.text('60');
+      break;
+    case 'hou':
+      node.unit.text('minutes');
+      node.countdown.text('60');
+      break;
+    case 'day':
+      node.unit.text('hours');
+      node.countdown.text('24');
+      break;
+  }
+  return node;
+}
+
+
+var seconds_to_unit = function(time, node) {
+  switch (node.unit.text().substring(0,3)) {
+    case 'min':
+      time = Math.ceil(time / 60);
+      break;
+    case 'hou':
+      time = Math.ceil(time / 60 / 60);
+      break;
+    case 'day':
+      time = Math.ceil(time / 60 / 60 / 24);
+      break;
+  }
+  
+  return time;
+}
+
+var set_time_unit = function(el) {
+  try{
+    var time_unit = $(el).find('span[class*="edit-limit-time-unit-"]').attr('class').replace(/edit-limit-time-unit-/, '');
+    $(el).find('em.placeholder').first().next().text(time_unit);
+  }
+  catch(e){}
+}
 
 Drupal.behaviors.tableSelect = {
-  attach: function(context) {
-    /* initialize for the comment entries owned by the user */
-    $("li.comment-edit a").each(function(){
-      var $this = $(this),
-      node =
-        { 'el': $this,
-        'countdown': $this.find('em.placeholder').first()
-        },
-      seconds = parseInt(node.countdown.text(), 10)
-      ;
-      comment_timer_update(node, seconds);
+  attach : function(context) {
+    $("li.comment-edit a").each(function() {
+      set_time_unit($(this));
+      var $this = $(this), node = {
+        'el' : $this,
+        'countdown' : $this.find('em.placeholder').first(),
+        'unit' : $this.find('em.placeholder').first().next()
+      }, time_left = parseInt(node.countdown.text());
+      if(seconds_to_unit(time_left, node)==1){
+        node = donwgrade_unit(node);
+      }
+      node.countdown.html(seconds_to_unit(time_left, node));
+      comment_timer_update(node, time_left);
     });
-
     /* initialize countdown for the message area */
-    $(".messages").each(function(){
-      var $this = $(this),
-      node =
-        { 'el': $this,
-        'countdown': $this.find('em.placeholder').first()
-        },
-      seconds = parseInt(node.countdown.text(), 10)
-      ;
-      if (node.countdown.length) {
-        timer_update_message(node, seconds);
+    $(".messages").each(function() {
+      set_time_unit($(this));
+      var $this = $(this), node = {
+        'el' : $this,
+        'countdown' : $this.find('em.placeholder').first(),
+        'unit' : $this.find('em.placeholder').first().next()
+      }, time_left = parseInt(node.countdown.text());
+      if(seconds_to_unit(time_left, node)==1){
+        node = donwgrade_unit(node);
       }
+      if(node.el.html().indexOf("left to edit this")!=-1)
+        node.countdown.html(seconds_to_unit(time_left, node));
+      timer_update_message(node, time_left);
     });
   }
 };
diff --git a/edit_limit.module b/edit_limit.module
index 54b18ef..979474b 100644
--- a/edit_limit.module
+++ b/edit_limit.module
@@ -9,6 +9,8 @@ define('EDIT_LIMIT_NODE_COUNT_DEFAULT', 3);
 define('EDIT_LIMIT_COMMENT_COUNT', 3);
 define('EDIT_LIMIT_NODE_TIME_DEFAULT', 86400); // in seconds, 1 day
 define('EDIT_LIMIT_COMMENT_TIME', 300); // in seconds, 5 minutes
+define('EDIT_LIMIT_TIME_UNITS', 'seconds,minutes,hours,days');
+define('EDIT_LIMIT_TIME_UNIT_DEFAULT', 'seconds');
 
 
 /**
@@ -125,7 +127,10 @@ function _edit_limit_user_acccess($node) {
 
   // Test that the node was not created longer ago than the allowed time limit.
   if ($edit_limit_node_time_enabled && $return) {
-    $timeleft = intval($node->created) + intval(variable_get('edit_limit_node_time_default', EDIT_LIMIT_NODE_TIME_DEFAULT)) - REQUEST_TIME;
+    $time_unit = variable_get('edit_limit_node_time_unit', EDIT_LIMIT_TIME_UNIT_DEFAULT);
+    $timeleft = intval($node->created) + intval(_unit_to_seconds(variable_get('edit_limit_node_time_default', EDIT_LIMIT_NODE_TIME_DEFAULT), $time_unit)) - REQUEST_TIME;
+
+    drupal_add_js(drupal_get_path('module', 'edit_limit') . '/edit_limit.js');
 
     if ($timeleft <= 0) {
       $time_limit_reached = TRUE;
@@ -135,9 +140,8 @@ function _edit_limit_user_acccess($node) {
       static $type;
       if (empty($type)) {
         $type = 'status';
-        drupal_add_js(drupal_get_path('module', 'edit_limit') . '/edit_limit.js');
 
-        drupal_set_message(t('You have %seconds seconds left to edit this content.', array('%seconds' => $timeleft)), $type, FALSE);
+        drupal_set_message(t('<span class="edit-limit-time-unit-'.$time_unit.'">You have %seconds %time_unit left to edit this content.</span>', array('%seconds' => $timeleft, '%time_unit' => 'seconds')), $type, FALSE);
       }
     }
   }
@@ -145,6 +149,27 @@ function _edit_limit_user_acccess($node) {
   return $return;
 }
 
+/**
+ * Helper function to convert a time unit to seconds
+ */
+function _unit_to_seconds($time=0, $unit='seconds'){
+  switch ($unit){
+    case 'minutes':
+      $time_in_seconds = $time * 60;
+      break;
+    case 'hours':
+      $time_in_seconds = $time * 60 * 60;
+      break;
+    case 'days':
+      $time_in_seconds = $time * 60 * 60 * 24;
+      break;
+    default:
+      $time_in_seconds = $time;
+    break;
+  }
+  return $time_in_seconds;
+}
+
 
 /**
  * Access check function to see if the user can still edit the comment or not.
@@ -164,18 +189,19 @@ function edit_limit_comment_access($edit = NULL, $comment = NULL) {
     }
 
     if (variable_get('edit_limit_comment_time_enabled', FALSE)) {
+      $time_unit = variable_get('edit_limit_comment_time_unit', EDIT_LIMIT_TIME_UNIT_DEFAULT);
       // By this point, we want to return false if the time has passed.
-      if ($comment->created + intval(variable_get('edit_limit_comment_time', EDIT_LIMIT_COMMENT_TIME)) < REQUEST_TIME) {
+      if ($comment->created + intval(_unit_to_seconds(variable_get('edit_limit_comment_time', EDIT_LIMIT_COMMENT_TIME), $time_unit)) < REQUEST_TIME) {
         $return = FALSE;
       }
       else {
-        static $seconds;
-        if (empty($seconds)) {
+        static $time_left;
+        if (empty($time_left)) {
           drupal_add_js(drupal_get_path('module', 'edit_limit') . '/edit_limit.js');
-          $seconds = $comment->created + intval(variable_get('edit_limit_comment_time', EDIT_LIMIT_COMMENT_TIME)) - REQUEST_TIME;
+          $time_left = $comment->created + intval(_unit_to_seconds(variable_get('edit_limit_comment_time', EDIT_LIMIT_COMMENT_TIME), $time_unit)) - REQUEST_TIME;
         }
         $type = 'status';
-        drupal_set_message(t('You have %seconds seconds left to edit this comment.', array('%seconds' => $seconds)), $type, FALSE);
+        drupal_set_message(t('<span class="edit-limit-time-unit-'.$time_unit.'">You have %time_left %time_unit left to edit this comment.</span>', array('%time_left' => $time_left, '%time_unit' => 'seconds')), $type, FALSE);
       }
     }
     return $return;
@@ -265,8 +291,9 @@ function edit_limit_node_view_alter(&$build) {
       
       // We don't have to do much user access checking, since this link will only exist if the user passed
       // the comment_access() check for this given comment.
-      if (!user_access('bypass edit limits') && variable_get('edit_limit_comment_time_enabled', FALSE) && !empty($build['comments']['comments'][$key]['links']['comment']['#links']['comment-edit']) && in_array($comment['#node']->type, variable_get('edit_limit_comments', array()))) {
-        $time_limit = $comment['#comment']->created + intval(variable_get('edit_limit_comment_time', EDIT_LIMIT_COMMENT_TIME));
+      if (!user_access('bypass edit limits') && variable_get('edit_limit_comment_time_enabled', FALSE) && !empty($build['comments']['comments'][$key]['links']['comment']['#links']['comment-edit'])) {
+        $time_unit = variable_get('edit_limit_comment_time_unit', EDIT_LIMIT_TIME_UNIT_DEFAULT);
+        $time_limit = $comment['#comment']->created + intval(_unit_to_seconds(variable_get('edit_limit_comment_time', EDIT_LIMIT_COMMENT_TIME), $time_unit));
 
         // If too much time has passed, destroy the link.
         if ($time_limit < REQUEST_TIME) {
@@ -277,7 +304,7 @@ function edit_limit_node_view_alter(&$build) {
           drupal_add_js(drupal_get_path('module', 'edit_limit') . '/edit_limit.js');
   
           // Modify the link to add the remaining seconds (only users that can't pass 'bypass edit limits' get this far).
-          $build['comments']['comments'][$key]['links']['comment']['#links']['comment-edit']['title'] .= ' '.t('(%seconds seconds left)', array('%seconds' => $time_limit - REQUEST_TIME));
+          $build['comments']['comments'][$key]['links']['comment']['#links']['comment-edit']['title'] .= ' '.t('<span class="edit-limit-time-unit-'.$time_unit.'">(%time_limit %time_unit left)'.'</span>', array('%time_limit' => ($time_limit - REQUEST_TIME), '%time_unit' => 'seconds'));
 //' (' . ($time_limit - REQUEST_TIME) . ' seconds left)';
         }
       }
-- 
1.7.4.1

