From b33124a152d8b13a859ac607016518409481f1ba Mon Sep 17 00:00:00 2001
From: Clemens Tolboom <clemens@build2be.com>
Date: Sat, 5 Mar 2011 16:32:45 +0100
Subject: [PATCH] Issue #916922 by franskuipers, jeffreyvddb, clemens.tolboom : views integration

---
 annotate.module                             |   10 +
 views/annotate.views.inc                    |  129 ++++++++
 views/annotate.views_default.inc            |  476 +++++++++++++++++++++++++++
 views/annotate_handler_field_note.inc       |   51 +++
 views/annotate_handler_field_visibility.inc |   15 +
 5 files changed, 681 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
 create mode 100644 views/annotate_handler_field_visibility.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..aa76ac7
--- /dev/null
+++ b/views/annotate.views.inc
@@ -0,0 +1,129 @@
+<?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',
+      ),
+    ),
+    'visibility' => array(
+      'title' => t('Visibility'),
+      'help' => t('Visibility of the note, 0=private, 1=editor, 2=other, 3=collaborators.'),
+      'field' => array(
+        'handler' => 'annotate_handler_field_visibility',
+        'click sortable' => TRUE,
+      ),
+      'filter' => array(
+        'handler' => 'views_handler_filter_in_operator',
+        'options callback' => '_annotate_get_visibility_list',
+      ),
+    ),
+    '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'
+      ),
+      'annotate_handler_field_visibility' => 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..f5f7df6
--- /dev/null
+++ b/views/annotate.views_default.inc
@@ -0,0 +1,476 @@
+<?php
+// $Id$
+
+/**
+ * @file annotate.views_default.inc
+ *
+ * Definition of annotate views.
+ */
+
+/**
+ * Implementation of hook_default_view_views().
+ */
+function annotate_views_default_views() {
+  $view = new view;
+  $view->name = 'annotate_recent';
+  $view->description = 'Recent annotations';
+  $view->tag = 'annotate';
+  $view->view_php = '';
+  $view->base_table = 'annotations';
+  $view->is_cacheable = FALSE;
+  $view->api_version = 2;
+  $view->disabled = TRUE; /* 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',
+  ),
+  'uid' => array(
+    'label' => 'user',
+    'required' => 0,
+    'id' => 'uid',
+    'table' => 'annotations',
+    'field' => 'uid',
+    'override' => array(
+      'button' => 'Override',
+  ),
+    'relationship' => 'none',
+  ),
+  ));
+  $handler->override_option('fields', array(
+  'nid' => array(
+    'label' => '',
+    '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,
+    'link_to_node' => 0,
+    'exclude' => 1,
+    'id' => 'nid',
+    'table' => 'node',
+    'field' => 'nid',
+    'relationship' => 'nid',
+    'override' => array(
+      'button' => 'Override',
+  ),
+  ),
+  'timestamp' => array(
+    'label' => '',
+    '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' => 'time ago',
+    'custom_date_format' => '',
+    'exclude' => 0,
+    'id' => 'timestamp',
+    'table' => 'annotations',
+    'field' => 'timestamp',
+    'relationship' => 'none',
+    'override' => array(
+      'button' => 'Override',
+  ),
+  ),
+  'name' => array(
+    'label' => '',
+    '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,
+    'link_to_user' => 1,
+    'overwrite_anonymous' => 0,
+    'anonymous_text' => '',
+    'exclude' => 0,
+    'id' => 'name',
+    'table' => 'users',
+    'field' => 'name',
+    'relationship' => 'uid',
+    'override' => array(
+      'button' => 'Override',
+  ),
+  ),
+  'note' => array(
+    'label' => '',
+    'alter' => array(
+      'alter_text' => 0,
+      'text' => '',
+      'make_link' => 1,
+      'path' => 'node/[nid]',
+      'link_class' => '',
+      'alt' => '',
+      'prefix' => '',
+      'suffix' => '',
+      'target' => '',
+      'help' => '',
+      'trim' => 1,
+      'max_length' => '100',
+      'word_boundary' => 1,
+      'ellipsis' => 1,
+      'html' => 1,
+      'strip_tags' => 1,
+  ),
+    'empty' => '',
+    'hide_empty' => 0,
+    'empty_zero' => 0,
+    'exclude' => 0,
+    'id' => 'note',
+    'table' => 'annotations',
+    'field' => 'note',
+    'relationship' => 'none',
+    'override' => array(
+      'button' => 'Override',
+  ),
+  ),
+  ));
+  $handler->override_option('sorts', array(
+  'timestamp' => array(
+    'order' => 'DESC',
+    'granularity' => 'second',
+    'id' => 'timestamp',
+    'table' => 'annotations',
+    'field' => 'timestamp',
+    'relationship' => 'none',
+  ),
+  ));
+  $handler->override_option('filters', array(
+  'visibility' => array(
+    'id' => 'visibility',
+    'table' => 'annotations',
+    'field' => 'visibility',
+  ),
+  ));
+  $handler->override_option('access', array(
+  'type' => 'none',
+  ));
+  $handler->override_option('cache', array(
+  'type' => 'none',
+  ));
+  $handler->override_option('title', 'Recent notes');
+  $handler->override_option('items_per_page', 5);
+  $handler->override_option('use_pager', 'mini');
+  $handler->override_option('style_plugin', 'list');
+  $handler->override_option('row_options', array(
+  'inline' => array(
+    'timestamp' => 'timestamp',
+    'name' => 'name',
+  ),
+  'separator' => 'said',
+  'hide_empty' => 0,
+  ));
+  $handler = $view->new_display('block', 'Recent annotations', 'block_1');
+  $handler->override_option('block_description', '');
+  $handler->override_option('block_caching', -1);
+
+  $views[$view->name] = $view;
+
+  $view = new view;
+  $view->name = 'annotate_by_node';
+  $view->description = 'Annotations by node';
+  $view->tag = 'annotate';
+  $view->view_php = '';
+  $view->base_table = 'annotations';
+  $view->is_cacheable = FALSE;
+  $view->api_version = 2;
+  $view->disabled = TRUE; /* 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' => 1,
+    'id' => 'nid',
+    'table' => 'annotations',
+    'field' => 'nid',
+    'relationship' => 'none',
+  ),
+  'uid' => array(
+    'label' => 'user',
+    'required' => 1,
+    'id' => 'uid',
+    'table' => 'annotations',
+    'field' => 'uid',
+    'relationship' => 'none',
+  ),
+  ));
+  $handler->override_option('fields', array(
+  'name' => array(
+    'label' => 'Name',
+    '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,
+    'link_to_user' => 1,
+    'overwrite_anonymous' => 0,
+    'anonymous_text' => '',
+    'exclude' => 0,
+    'id' => 'name',
+    'table' => 'users',
+    'field' => 'name',
+    'relationship' => 'uid',
+    'override' => array(
+      'button' => 'Override',
+  ),
+  ),
+  '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' => 'time ago',
+    'custom_date_format' => '',
+    'exclude' => 0,
+    'id' => 'timestamp',
+    'table' => 'annotations',
+    'field' => 'timestamp',
+    'relationship' => 'none',
+  ),
+  'visibility' => array(
+    'label' => 'Visibility',
+    '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' => 'visibility',
+    'table' => 'annotations',
+    'field' => 'visibility',
+    'relationship' => 'none',
+  ),
+  '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',
+  ),
+  ));
+  $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,
+  ),
+    '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_php' => '',
+  ),
+  ));
+  $handler->override_option('access', array(
+  'type' => 'none',
+  ));
+  $handler->override_option('cache', array(
+  'type' => 'none',
+  ));
+  $handler->override_option('title', 'Annotations');
+  $handler->override_option('items_per_page', 5);
+  $handler->override_option('use_pager', '1');
+  $handler->override_option('style_plugin', 'table');
+  $handler->override_option('style_options', array(
+  'grouping' => '',
+  'override' => 1,
+  'sticky' => 0,
+  'order' => 'asc',
+  'columns' => array(
+    'name' => 'name',
+    'timestamp' => 'timestamp',
+    'visibility' => 'visibility',
+    'note' => 'note',
+  ),
+  'info' => array(
+    'name' => array(
+      'sortable' => 1,
+      'separator' => '',
+  ),
+    'timestamp' => array(
+      'sortable' => 1,
+      'separator' => '',
+  ),
+    'visibility' => array(
+      'sortable' => 1,
+      'separator' => '',
+  ),
+    'note' => array(
+      'separator' => '',
+  ),
+  ),
+  'default' => 'timestamp',
+  ));
+  $handler = $view->new_display('page', 'Annotations', 'page_1');
+  $handler->override_option('path', 'node/%/annotate');
+  $handler->override_option('menu', array(
+  'type' => 'tab',
+  'title' => 'Annotations',
+  'description' => '',
+  'weight' => '0',
+  'name' => 'navigation',
+  ));
+  $handler->override_option('tab_options', array(
+  'type' => 'none',
+  'title' => '',
+  'description' => '',
+  'weight' => 0,
+  'name' => 'navigation',
+  ));
+
+  $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..a24e39b
--- /dev/null
+++ b/views/annotate_handler_field_note.inc
@@ -0,0 +1,51 @@
+<?php
+// $Id$
+
+/**
+ * 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);
+    }
+  }
+}
diff --git a/views/annotate_handler_field_visibility.inc b/views/annotate_handler_field_visibility.inc
new file mode 100644
index 0000000..efd8818
--- /dev/null
+++ b/views/annotate_handler_field_visibility.inc
@@ -0,0 +1,15 @@
+<?php
+// $Id$
+
+/**
+ * Field handler to present a link node translate.
+ */
+class annotate_handler_field_visibility extends views_handler_field {
+  /**
+   * Called to render this field.
+   */
+  function render($values) {
+  	$options = _annotate_get_visibility_list();
+    return $options[$values->annotations_visibility];
+  }
+}
-- 
1.7.1

