From a1dc0892f42614fff3b54c93a6fa1e1da1bb3349 Mon Sep 17 00:00:00 2001
From: Kalman Hosszu <hosszu.kalman@gmail.com>
Date: Sat, 5 Mar 2011 15:55:00 +0100
Subject: [PATCH] Multiquote feature

---
 multiquote/includes/multiquote.admin.inc |   44 ++++++++++++
 multiquote/includes/multiquote.menu.inc  |   57 +++++++++++++++
 multiquote/multiquote.info               |    7 ++
 multiquote/multiquote.module             |  111 ++++++++++++++++++++++++++++++
 multiquote/theme/img/loader.gif          |  Bin 0 -> 2196 bytes
 multiquote/theme/multiquote.css          |    7 ++
 multiquote/theme/multiquote.js           |   63 +++++++++++++++++
 quote.info                               |    1 +
 quote.module                             |   68 ++++++++++--------
 9 files changed, 327 insertions(+), 31 deletions(-)
 create mode 100644 multiquote/includes/multiquote.admin.inc
 create mode 100644 multiquote/includes/multiquote.menu.inc
 create mode 100644 multiquote/multiquote.info
 create mode 100644 multiquote/multiquote.module
 create mode 100644 multiquote/theme/img/loader.gif
 create mode 100644 multiquote/theme/multiquote.css
 create mode 100644 multiquote/theme/multiquote.js

diff --git a/multiquote/includes/multiquote.admin.inc b/multiquote/includes/multiquote.admin.inc
new file mode 100644
index 0000000..672f0ad
--- /dev/null
+++ b/multiquote/includes/multiquote.admin.inc
@@ -0,0 +1,44 @@
+<?php
+// $Id$
+/**
+ * @file
+ *
+ *
+ *
+ * @author Kálmán Hosszu - hosszu.kalman@gmail.com - http://www.kalman-hosszu.com
+ */
+
+/**
+ * Build multiquote_settings_page form.
+ *
+ * @param array $form_state
+ * @return array The created form.
+ */
+function multiquote_settings_page($form_state) {
+  $form = array();
+
+  /* == JS messages == */
+  $form['multiquote_check_link'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Check link text'),
+    '#default_value' => variable_get('multiquote_check_link', DEFAULT_MULTIQUOTE_CHECK_LINK),
+    '#description' => t('This string is translatable.'),
+  );
+
+  $form['multiquote_uncheck_link'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Uncheck link text'),
+    '#default_value' => variable_get('multiquote_uncheck_link', DEFAULT_MULTIQUOTE_UNCHECK_LINK),
+    '#description' => t('This string is translatable.'),
+  );
+
+  $form['multiquote_insert_link'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Insert link text'),
+    '#default_value' => variable_get('multiquote_insert_link', DEFAULT_MULTIQUOTE_INSERT_LINK),
+    '#description' => t('This string is translatable.'),
+  );
+
+  // Store to system variables
+  return system_settings_form($form);
+}
\ No newline at end of file
diff --git a/multiquote/includes/multiquote.menu.inc b/multiquote/includes/multiquote.menu.inc
new file mode 100644
index 0000000..9c974e6
--- /dev/null
+++ b/multiquote/includes/multiquote.menu.inc
@@ -0,0 +1,57 @@
+<?php
+// $Id$
+/**
+ * @file
+ *
+ *
+ *
+ * @author Kálmán Hosszu - hosszu.kalman@gmail.com - http://www.kalman-hosszu.com
+ */
+
+ /**
+ * Menu callback for multiquote/add/%
+ *
+ * @param int $cid
+ *   The comment ID.
+ */
+function multiquote_add_page($cid) {
+  // Ide jön a quote lekérdezése
+//  $comment = multiquote_add($cid);
+  $comment = array(
+    $cid => quote_generate_quoted_text($cid),
+  );
+
+  $param = array(
+    'success' => TRUE,
+    'linktext' => t(variable_get('multiquote_uncheck_link', 'unflag as multiquote')),
+    'linkurl' => url('multiquote/delete/' . $cid),
+    'comment' => array(
+      'cid' => key($comment),
+      'value' => current($comment),
+    ),
+    'event' => 'add',
+  );
+
+  drupal_json($param);
+}
+
+/**
+ * Menu callback for multiquote/delete/%
+ *
+ * @param int $cid
+ *   The comment ID.
+ */
+function multiquote_delete_page($cid) {
+  // Delete
+  $param = array(
+    'success' => TRUE,
+    'linktext' => t(variable_get('multiquote_check_link', 'flag as multiquote')),
+    'linkurl' => url('multiquote/add/' . $cid),
+    'comment' => array(
+      'cid' => $cid,
+    ),
+    'event' => 'delete',
+  );
+
+  drupal_json($param);
+}
\ No newline at end of file
diff --git a/multiquote/multiquote.info b/multiquote/multiquote.info
new file mode 100644
index 0000000..3fda286
--- /dev/null
+++ b/multiquote/multiquote.info
@@ -0,0 +1,7 @@
+; $Id$
+name = Multiquote
+description = Quote module's multiquote submodule
+package = Quote
+core = 6.x
+
+dependencies[] = quote
\ No newline at end of file
diff --git a/multiquote/multiquote.module b/multiquote/multiquote.module
new file mode 100644
index 0000000..692ba94
--- /dev/null
+++ b/multiquote/multiquote.module
@@ -0,0 +1,111 @@
+<?php
+// $Id$
+/**
+ * @file
+ * 
+ *
+ *
+ * @author Kálmán Hosszu - hosszu.kalman@gmail.com - http://www.kalman-hosszu.com
+ */
+
+define('DEFAULT_MULTIQUOTE_CHECK_LINK', 'flag as multiquote');
+define('DEFAULT_MULTIQUOTE_UNCHECK_LINK', 'unflag as multiquote');
+define('DEFAULT_MULTIQUOTE_INSERT_LINK', 'Insert quoted comments');
+
+/* ====================== */
+/* ==== DRUPAL HOOKS ==== */
+/* ====================== */
+
+/**
+ * Implementation of hook_menu().
+ *
+ * @return An array of menu items.
+ */
+function multiquote_menu() {
+  $items = array();
+
+  /* == Manage multiquote events == */
+  $items['multiquote/add/%'] = array(
+    'title' => 'Add multiquote',
+    'page callback' => 'multiquote_add_page',
+    'page arguments' => array(2),
+    'access arguments' => array('access multiquote'),
+    'file' => 'includes/multiquote.menu.inc',
+    'type' => MENU_CALLBACK,
+  );
+
+  $items['multiquote/delete/%'] = array(
+    'title' => 'Delete multiquote',
+    'page callback' => 'multiquote_delete_page',
+    'page arguments' => array(2),
+    'access arguments' => array('access multiquote'),
+    'file' => 'includes/multiquote.menu.inc',
+    'type' => MENU_CALLBACK,
+  );
+
+  /* Admin interface */
+  $items['admin/settings/quote/multiquote'] = array(
+    'title' => 'Multiquote',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('multiquote_settings_page'),
+    'access arguments' => array('administer multiquote'),
+    'file' => 'includes/multiquote.admin.inc',
+  );
+
+  return $items;
+}
+
+/**
+ * Implementation of hook_perm().
+ *
+ * @return array An array of valid permissions for the multiquote module
+ */
+function multiquote_perm() {
+  return array('create multiquote', 'administer multiquote');
+}
+
+/**
+ * Implementation of hook_link().
+ */
+function multiquote_link($type, $object, $teaser = FALSE) {
+  $links = array();
+
+  if ($type == 'comment' && user_access('create multiquote')) {
+    $links['multiquote'] = array(
+      'title' => t(variable_get('multiquote_check_link', DEFAULT_MULTIQUOTE_CHECK_LINK)),
+      'href' => 'multiquote/add/' . $object->cid,
+      'attributes' => array('class' => 'multiquote multiquote-add'),
+    );
+  }
+
+  return $links;
+}
+
+/**
+ * Implementation of hook_init().
+ */
+function multiquote_init() {
+  drupal_add_js(array('multiquote' => array()), 'setting');
+  drupal_add_js(drupal_get_path('module', 'multiquote') . '/theme/multiquote.js');
+  drupal_add_css(drupal_get_path('module', 'multiquote') . '/theme/multiquote.css');
+}
+
+/**
+ * Implementation of hook_form_alter().
+ */
+function multiquote_form_comment_form_alter(&$form, $form_state) {
+//  print_r($form);
+  $form['comment_filter']['comment']['#process'][] = 'multiquote_test_process';
+}
+
+
+/* ====================== */
+/* == MODULE FUNCTIONS == */
+/* ====================== */
+
+function multiquote_test_process($element) {
+//  print_r($element);
+  $element['#description'] .= l(t('Insert quoted comments'), $_GET['q'], array('attributes' => array('id' => 'multiquote-insert')));
+
+  return $element;
+}
\ No newline at end of file
diff --git a/multiquote/theme/img/loader.gif b/multiquote/theme/img/loader.gif
new file mode 100644
index 0000000000000000000000000000000000000000..207e95c3fa8cc31a89af150ad74059b1667922b6
GIT binary patch
literal 2196
zcmeH{Urbw79LLW&_xASmFQt@HSs}NhO2-Jh1BM+kb4v^B62vmdY!-sIxUi{Y-eKaZ
zd%2gEe>0H6h_VE=!^E(JL`)>X2jVS^Zcfvn5<-k2WvFCNG`ghG7rZ6$WnPVsKJ5GO
zJ8$RvJD>CY{Z5~0cApFxfB|GdPWa6w{k0|AwWK|oJQ2I!Je@i}^Zo0eZod2F_QJwK
zJhi*9v9}~^YcH8hrc$Ymjg76Xt@{st+t|%+?PYgm$u+ybCo8+3&1pZopVPthPs$Sl
z|6c-*eO+r)N4wV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I66TkFogokU>B}H#d-&TI?-^oT*
zRA10W&q#7unMxBUvZJU0H>48~K*CV@D7`H;MzIa8(QDnFN!<(7r69X=cc1~zTKw(l
Pm4SkyKc+`yv*-Q<rDLic

literal 0
HcmV?d00001

diff --git a/multiquote/theme/multiquote.css b/multiquote/theme/multiquote.css
new file mode 100644
index 0000000..921db92
--- /dev/null
+++ b/multiquote/theme/multiquote.css
@@ -0,0 +1,7 @@
+/* $Id$ */
+
+/* Load animation in the right of the text. */
+.multiquote-load {
+  background: url("img/loader.gif") no-repeat right center;
+  padding-right: 15px;
+}
\ No newline at end of file
diff --git a/multiquote/theme/multiquote.js b/multiquote/theme/multiquote.js
new file mode 100644
index 0000000..d177a1b
--- /dev/null
+++ b/multiquote/theme/multiquote.js
@@ -0,0 +1,63 @@
+// $Id$
+Drupal.behaviors.multiquote = function(context) {
+  // Check click event
+  $('a.multiquote', context).click(function() {
+
+    // Store element
+    var element = $(this);
+
+    // Double clicking
+    if (element.hasClass('multiquote-load')) {
+      return false;
+    }
+
+    // Add load animation
+    element.addClass('multiquote-load');
+
+    $.ajax({
+      type: 'GET',
+      url: element.attr('href'),
+      dataType: 'json',
+      success: function (data) {
+        // Create url and link text from the result
+        element.html(data.linktext).attr('href', data.linkurl);
+        element.removeClass('multiquote-load');
+
+        if (data.event == 'add') {
+          Drupal.settings.multiquote[data.comment.cid] = data.comment.value;
+          element.removeClass('multiquote-add');
+          element.addClass('multiquote-delete');
+        }
+
+        if (data.event == 'delete') {
+          delete Drupal.settings.multiquote[data.comment.cid];
+          element.removeClass('multiquote-delete');
+          element.addClass('multiquote-add');
+        }
+      },
+      error: function (xmlhttp) {
+        alert('An HTTP error '+ xmlhttp.status +' occurred.\n'+ element.attr('href'));
+        element.removeClass('multiquote-load');
+      }
+    });
+
+    return false;
+  });
+};
+
+Drupal.behaviors.multiquote_insert = function(context) {
+  
+  // Check click event
+  $('#multiquote-insert', context).click(function() {
+    var content = '';
+
+    // Fill the content variable with blockquote elements.
+    for (var i in Drupal.settings.multiquote) {
+      content += Drupal.settings.multiquote[i];
+    }
+
+    $('#edit-comment').val(content);
+
+    return false;
+  });
+}
\ No newline at end of file
diff --git a/quote.info b/quote.info
index 757b9fc..f9a5846 100644
--- a/quote.info
+++ b/quote.info
@@ -1,4 +1,5 @@
 name = Quote
 description = Allows users to quote posts or comments.
 core = 6.x
+package = Quote
 dependencies[] = comment
diff --git a/quote.module b/quote.module
index d445fcd..a2a9e91 100644
--- a/quote.module
+++ b/quote.module
@@ -90,45 +90,51 @@ function quote_form_alter(&$form, &$form_state, $form_id) {
     $nid = arg(2);
     $cid = arg(3);
 
-    if ($cid) {
-      $comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = 0', $cid));
+    if ($quoted_text = quote_generate_quoted_text($cid, $nid)) {
+      // Add quoted text and preserve existing content (signature etc.).
+      $form['comment_filter']['comment']['#default_value'] = $quoted_text . $form['comment_filter']['comment']['#default_value'];
 
-      if ($comment->uid) {
-        $author = $comment->registered_name;
+      if (_quote_variable_get('subject_required')) {
+        $form['subject']['#required'] = TRUE;
       }
-      else {
-        $author = (!empty($comment->name)) ? $comment->name : variable_get('anonymous', 'Anonymous');
-      }
-      $quote = $comment->comment;
+
+      // The Form API, by default, drops name-value pairs from the form's action
+      // URL (besides ?q=). Manually adding it back in as a hidden element.
+      $form['quote'] = array(
+        '#type' => 'hidden',
+        '#value' => 1
+      );
     }
-    elseif ($nid && _quote_variable_get('node_link_display')) {
-      $node = node_load(array('nid' => $nid));
-      if (in_array($node->type, _quote_variable_get('node_types'))) {
-        $quote = $node->body;
-        $author = !empty($node->name) ? $node->name : variable_get('anonymous', 'Anonymous');
-      }
-      else {
-        return;
-      }
+  }
+}
+
+function quote_generate_quoted_text($cid = NULL, $nid = NULL) {
+  if ($cid) {
+    $comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = 0', $cid));
+
+    if ($comment->uid) {
+      $author = $comment->registered_name;
     }
     else {
-      return;
+      $author = (!empty($comment->name)) ? $comment->name : variable_get('anonymous', 'Anonymous');
     }
-
-    // Add quoted text and preserve existing content (signature etc.).
-    $form['comment_filter']['comment']['#default_value'] = '[quote='. $author .']'. trim($quote) ."[/quote]\n". $form['comment_filter']['comment']['#default_value'];
-
-    if (_quote_variable_get('subject_required')) {
-      $form['subject']['#required'] = TRUE;
+    $quote = $comment->comment;
+  }
+  elseif ($nid && _quote_variable_get('node_link_display')) {
+    $node = node_load(array('nid' => $nid));
+    if (in_array($node->type, _quote_variable_get('node_types'))) {
+      $quote = $node->body;
+      $author = !empty($node->name) ? $node->name : variable_get('anonymous', 'Anonymous');
     }
-
-    // The Form API, by default, drops name-value pairs from the form's action
-    // URL (besides ?q=). Manually adding it back in as a hidden element.
-    $form['quote'] = array(
-      '#type' => 'hidden',
-      '#value' => 1
-    );
+    else {
+      return NULL;
+    }
+  }
+  else {
+    return NULL;
   }
+
+  return '[quote='. $author .']'. trim($quote) ."[/quote]\n";
 }
 
 /**
-- 
1.7.2

