From eca7b761e10fbd8b0b067bce6a745fac2cd28084 Mon Sep 17 00:00:00 2001
From: Frans kuipers <franskuipers@gmail.com>
Date: Fri, 4 Mar 2011 11:55:32 +0100
Subject: [PATCH] Feature #916922 by franskuipers: Add views support

---
 annotate.module                       |   10 ++
 views/annotate.views.inc              |  113 +++++++++++++++++++++++
 views/annotate.views_default.inc      |  158 +++++++++++++++++++++++++++++++++
 views/annotate_handler_field_note.inc |   55 ++++++++++++
 4 files changed, 336 insertions(+), 0 deletions(-)
 create mode 100644 views/annotate.views.inc
 create mode 100644 views/annotate.views_default.inc
 create mode 100644 views/annotate_handler_field_note.inc

diff --git a/annotate.module b/annotate.module
index 364935e..a930322 100644
--- a/annotate.module
+++ b/annotate.module
@@ -831,3 +831,13 @@ function annotate_delete_submit($form, &$form_state) {
   $form_state['redirect'] = 'node/' . $form_state['values']['nid'];
   return;
 }
+
+/**
+ * Implementation of hook_views_api.
+ */
+function annotate_views_api() {
+  return array(
+    'api' => 2.0,
+    'path' => drupal_get_path('module', 'annotate') . '/views',
+  );
+}
diff --git a/views/annotate.views.inc b/views/annotate.views.inc
new file mode 100644
index 0000000..c0946d1
--- /dev/null
+++ b/views/annotate.views.inc
@@ -0,0 +1,113 @@
+<?php
+// $Id $
+
+/**
+ * @file
+ * Annotate Views support. Declares all the main tables to Views
+ * @author Frans Kuipers http://drupal.org/user/103267
+ **/
+
+/**
+ * hook_views_data()
+ */
+function annotate_views_data() {
+  $tables['annotations'] = array(
+    'table' => array(
+      'group' => t('Annotations'),
+      'join' => array(
+        'users' => array(
+          'table' => 'annotations',
+          'left_field' => 'uid',
+          'field' => 'uid'
+         ),
+         'node' => array(
+           'table' => 'annotations',
+           'left_field' => 'nid',
+           'field' => 'nid'
+         ),
+       ),
+      'base' => array(
+        'field' => 'note',
+        'title' => t('Annotation'),
+        'help' => t('Notes added as annotations to node content.'),
+      ),
+    ),
+    // Fields definitions
+    'note' => array(
+      'title' => t('Note'),
+      'help' => t('The note text for this node'),
+      // Information for displaying the note
+      'field' => array(
+        'handler' => 'annotate_handler_field_note',
+        'format' => 'note_format',
+      ),
+      'extra' => array(
+        array('field' => 'uid', 'value' => '***CURRENT_USER***', 'numeric' => TRUE),
+      ),
+
+    ),
+    'timestamp' => array(
+      'title' => t('Post date'),
+      'help' => t('Date and time of when the comment was posted.'),
+      'field' => array(
+        'handler' => 'views_handler_field_date',
+        'click sortable' => TRUE,
+      ),
+      'sort' => array(
+        'handler' => 'views_handler_sort_date',
+      ),
+      'filter' => array(
+        'handler' => 'views_handler_filter_date',
+      ),
+    ),
+    'nid' => array(
+      'title' => t('Node'),
+      'help' => t('The node the note is annotated to.'),
+      'relationship' => array(
+        'base' => 'node',
+        'base field' => 'nid',
+        'handler' => 'views_handler_relationship',
+        'label' => t('node'),
+      ),
+    ),
+    'uid' => array(
+      'title' => t('User'),
+      'help' => t("The User ID of the note's author."),
+      'relationship' => array(
+        'base' => 'users',
+        'base field' => 'uid',
+        'handler' => 'views_handler_relationship',
+        'label' => t('user'),
+      ),
+    ),
+    // uid alias to filter on current user
+    'uid_current' => array(
+      'real field' => 'uid',
+      'title' => t('Current'),
+      'help' => t('Filter the view to the currently logged in user.'),
+      'filter' => array(
+        'handler' => 'views_handler_filter_user_current',
+        'type' => 'yes-no',
+      ),
+    ),
+  );
+
+  return $tables;
+}
+
+/**
+ *  Implementation of hook_views_handlers().
+ */
+function annotate_views_handlers() {
+  return array(
+    'info' => array(
+      'path' => drupal_get_path('module', 'annotate') . '/views',
+    ),
+    'handlers' => array(
+      // field handler
+      'annotate_handler_field_note' => array(
+        'parent' => 'views_handler_field'
+      ),
+    ),
+  );
+}
diff --git a/views/annotate.views_default.inc b/views/annotate.views_default.inc
new file mode 100644
index 0000000..1ecf985
--- /dev/null
+++ b/views/annotate.views_default.inc
@@ -0,0 +1,158 @@
+<?php
+// $Id: $
+/**
+ * @file revisiontags.views_default.inc
+ * Definition of revisiontag views.
+ */
+
+/**
+ * Implementation of hook_default_view_views().
+ */
+function annotate_views_default_views() {
+
+	$view = new view;
+	$view->name = 'annotate_block';
+	$view->description = 'annotations block in node context';
+	$view->tag = 'annotations';
+	$view->view_php = '';
+	$view->base_table = 'annotations';
+	$view->is_cacheable = FALSE;
+	$view->api_version = 2;
+	$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
+	$handler = $view->new_display('default', 'Defaults', 'default');
+	$handler->override_option('relationships', array(
+	  'nid' => array(
+	    'label' => 'node',
+	    'required' => 0,
+	    'id' => 'nid',
+	    'table' => 'annotations',
+	    'field' => 'nid',
+	    'relationship' => 'none',
+	  ),
+	));
+	$handler->override_option('fields', array(
+	  'note' => array(
+	    'label' => 'Note',
+	    'alter' => array(
+	      'alter_text' => 0,
+	      'text' => '',
+	      'make_link' => 0,
+	      'path' => '',
+	      'link_class' => '',
+	      'alt' => '',
+	      'prefix' => '',
+	      'suffix' => '',
+	      'target' => '',
+	      'help' => '',
+	      'trim' => 0,
+	      'max_length' => '',
+	      'word_boundary' => 1,
+	      'ellipsis' => 1,
+	      'html' => 0,
+	      'strip_tags' => 0,
+	    ),
+	    'empty' => '',
+	    'hide_empty' => 0,
+	    'empty_zero' => 0,
+	    'exclude' => 0,
+	    'id' => 'note',
+	    'table' => 'annotations',
+	    'field' => 'note',
+	    'relationship' => 'none',
+	  ),
+	  'timestamp' => array(
+	    'label' => 'Post date',
+	    'alter' => array(
+	      'alter_text' => 0,
+	      'text' => '',
+	      'make_link' => 0,
+	      'path' => '',
+	      'link_class' => '',
+	      'alt' => '',
+	      'prefix' => '',
+	      'suffix' => '',
+	      'target' => '',
+	      'help' => '',
+	      'trim' => 0,
+	      'max_length' => '',
+	      'word_boundary' => 1,
+	      'ellipsis' => 1,
+	      'html' => 0,
+	      'strip_tags' => 0,
+	    ),
+	    'empty' => '',
+	    'hide_empty' => 0,
+	    'empty_zero' => 0,
+	    'date_format' => 'small',
+	    'custom_date_format' => '',
+	    'exclude' => 0,
+	    'id' => 'timestamp',
+	    'table' => 'annotations',
+	    'field' => 'timestamp',
+	    'relationship' => 'none',
+	  ),
+	));
+	$handler->override_option('arguments', array(
+	  'nid' => array(
+	    'default_action' => 'default',
+	    'style_plugin' => 'default_summary',
+	    'style_options' => array(),
+	    'wildcard' => 'all',
+	    'wildcard_substitution' => 'All',
+	    'title' => '',
+	    'breadcrumb' => '',
+	    'default_argument_type' => 'node',
+	    'default_argument' => '',
+	    'validate_type' => 'node',
+	    'validate_fail' => 'not found',
+	    'break_phrase' => 0,
+	    'not' => 0,
+	    'id' => 'nid',
+	    'table' => 'node',
+	    'field' => 'nid',
+	    'relationship' => 'nid',
+	    'validate_user_argument_type' => 'uid',
+	    'validate_user_roles' => array(
+	      '2' => 0,
+	      '3' => 0,
+	    ),
+	    'default_options_div_prefix' => '',
+	    'default_argument_fixed' => '',
+	    'default_argument_user' => 0,
+	    'default_argument_php' => '',
+	    'validate_argument_node_type' => array(
+	      'page' => 0,
+	      'story' => 0,
+	    ),
+	    'validate_argument_node_access' => 1,
+	    'validate_argument_nid_type' => 'nid',
+	    'validate_argument_vocabulary' => array(),
+	    'validate_argument_type' => 'tid',
+	    'validate_argument_transform' => 0,
+	    'validate_user_restrict_roles' => 0,
+	    'validate_argument_node_flag_name' => '*relationship*',
+	    'validate_argument_node_flag_test' => 'flaggable',
+	    'validate_argument_node_flag_id_type' => 'id',
+	    'validate_argument_user_flag_name' => '*relationship*',
+	    'validate_argument_user_flag_test' => 'flaggable',
+	    'validate_argument_user_flag_id_type' => 'id',
+	    'validate_argument_php' => '',
+	    'override' => array(
+	      'button' => 'Override',
+	    ),
+	  ),
+	));
+	$handler->override_option('access', array(
+	  'type' => 'none',
+	));
+	$handler->override_option('cache', array(
+	  'type' => 'none',
+	));
+	$handler->override_option('style_plugin', 'table');
+	$handler = $view->new_display('block', 'Block', 'block_1');
+	$handler->override_option('block_description', '');
+	$handler->override_option('block_caching', -1);
+
+	$views[$view->name] = $view;
+  return $views;
+}
\ No newline at end of file
diff --git a/views/annotate_handler_field_note.inc b/views/annotate_handler_field_note.inc
new file mode 100644
index 0000000..4374803
--- /dev/null
+++ b/views/annotate_handler_field_note.inc
@@ -0,0 +1,55 @@
+<?php
+// $Id: views_handler_field_node_link_translate.inc,v 1.1.4.2 2010/03/19 23:09:54 merlinofchaos Exp $
+/**
+ * Field handler to present a link node translate.
+ */
+class annotate_handler_field_note extends views_handler_field {
+  function construct() {
+    parent::construct();
+    $this->additional_fields['uid'] = 'uid';
+    $this->additional_fields['format'] = 'note_format';
+    $this->additional_fields['visibility'] = 'visibility';
+  }
+
+
+   /**
+   * Called to implement a relationship in a query.
+   */
+
+  function query() {
+    $this->ensure_my_table();
+    // Add the field.
+    $this->field_alias = $this->query->add_field($this->table_alias, $this->real_field);
+    $this->add_additional_fields();
+    // Join the node table. Needed for the visibility WHERE query
+    $join = new views_join();
+    $join->construct('node', $this->table_alias, 'nid', 'nid');
+    $join->type = 'INNER';
+    $this->node_table = $this->query->ensure_table('note_node', $this->relationship, $join);
+
+    $access = user_access( ANNOTATE_READ_PUBLISHED_ANNOTATION) ? '1' : '0';
+    // Add where clause to query.
+    $where = '((annotations.visibility = 3 AND annotations.nid IN (SELECT nid FROM annotations WHERE uid = ***CURRENT_USER***))
+                OR (annotations.visibility = 1 AND note_node.uid=***CURRENT_USER***)
+                OR (annotations.visibility = 2)
+                OR (annotations.uid = ***CURRENT_USER***)
+                OR  ***CURRENT_USER*** = 1)
+                AND '. $access;
+    $this->query->add_where($this->options['group'], $where, $values);
+
+  }
+
+
+   /**
+   * Called to render this field.
+   */
+
+  function render($values) {
+    // render the annotation note
+    $value = $values->{$this->field_alias};
+    $format = $values->{$this->aliases['note_format']};
+    if ($value) {
+      return check_markup($value, $format, FALSE);
+    }
+  }
+}
-- 
1.7.4.1

