From ae8b6f5892fca750666dbd681063a5e52a86159b Mon Sep 17 00:00:00 2001
From: Dominik Blunk <dominik@blunk.ch>
Date: Wed, 10 Nov 2010 13:28:26 +0100
Subject: [PATCH] This plugin allows the usage of "Tags" as column on pages like "view issues", "CSV export" or "Excel export".
 After installing the plugin the column "tags" may be added/removed as usual under "Configuration - Manage Columns"

---
 csv_export.php                          |   14 +++++-
 excel_xml_export.php                    |   14 +++++-
 plugins/ColumnTags/ColumnTags.class.php |   80 ++++++++++++++++++++++++++++++
 plugins/ColumnTags/ColumnTags.php       |   81 +++++++++++++++++++++++++++++++
 4 files changed, 187 insertions(+), 2 deletions(-)
 create mode 100644 plugins/ColumnTags/ColumnTags.class.php
 create mode 100644 plugins/ColumnTags/ColumnTags.php

diff --git a/csv_export.php b/csv_export.php
index 2116db2..0dbd2d1 100644
--- a/csv_export.php
+++ b/csv_export.php
@@ -113,7 +113,19 @@
 				echo csv_escape_string($t_value);
 			} else {
 				$t_function = 'csv_format_' . $t_column;
-				echo $t_function( $t_row->$t_column );
+				if( function_exists( $t_function ) ) {
+					echo $t_function( $t_row->$t_column );
+				}
+				else {
+					ob_start();
+					$t_plugin_columns = columns_get_plugin_columns();
+					if ( isset( $t_plugin_columns[ $t_column ] ) ) {
+						$t_column_object = $t_plugin_columns[ $t_column ];
+						print_column_plugin( $t_column_object, $t_row, COLUMNS_TARGET_CSV_PAGE );
+					}
+					$t_value = ob_get_clean();
+					echo csv_escape_string($t_value);
+				}
 			}
 		}
 
diff --git a/excel_xml_export.php b/excel_xml_export.php
index 13469e4..872ff70 100644
--- a/excel_xml_export.php
+++ b/excel_xml_export.php
@@ -94,7 +94,19 @@
 							echo excel_format_custom_field( $t_row->id, $t_row->project_id, $t_custom_field );
 						} else {
 							$t_function = 'excel_format_' . $t_column;
-							echo $t_function( $t_row->$t_column );
+							if( function_exists( $t_function ) ) {
+								echo $t_function( $t_row->$t_column );
+							}
+							else {
+								ob_start();
+								$t_plugin_columns = columns_get_plugin_columns();
+								if ( isset( $t_plugin_columns[ $t_column ] ) ) {
+									$t_column_object = $t_plugin_columns[ $t_column ];
+									print_column_plugin( $t_column_object, $t_row, COLUMNS_TARGET_CSV_PAGE );
+								}
+								$t_value = ob_get_clean();
+								echo excel_prepare_string( $t_value );
+							}
 						}
 					}
 				}
diff --git a/plugins/ColumnTags/ColumnTags.class.php b/plugins/ColumnTags/ColumnTags.class.php
new file mode 100644
index 0000000..5f2e6c1
--- /dev/null
+++ b/plugins/ColumnTags/ColumnTags.class.php
@@ -0,0 +1,80 @@
+<?php
+/**
+ * Class ColumnTags
+ *
+ * This model class represents the logic for the column "tags"
+ * @author Dominik Blunk <dominik.blunk@acc-solutions.ch>
+ * @copyright Copyright (c) 2010 - 2011, ACC Solutions AG
+ * @link http://www.acc-solutions.ch
+ * @package model
+ */
+
+/**
+ *	ColumnTags column class
+ */
+class ColumnTags extends MantisColumn {
+
+	/**
+	 * Column title, as displayed to the user.
+	 */
+	public $title = 'Tags';
+
+	/**
+	 * Column name, as selected in the manage columns interfaces.
+	 */
+	public $column = 'tags';
+
+	/**
+	 * Column is sortable by the user.  Setting this to true implies that
+	 * the column will properly implement the sortquery() method.
+	 */
+	public $sortable = true;
+
+	/**
+	 * Build the SQL query elements 'join' and 'order' as used by
+	 * core/filter_api.php to create the filter sorting query.
+	 * @param string Sorting order ('ASC' or 'DESC')
+	 * @return array Keyed-array with query elements; see developer guide
+	 */
+	public function sortquery( $p_dir ) {
+		$t_tags_table = db_get_table( 'mantis_tag_table' );
+		return array( 'join' => 'left join mantis_bug_tag_table on (mantis_bug_table.id = mantis_bug_tag_table.bug_id)
+								left join mantis_tag_table on (mantis_tag_table.id = mantis_bug_tag_table.tag_id)',
+					  'order' => 'mantis_tag_table.name ' . $p_dir );
+	}
+
+	/**
+	 * Allow plugin columns to pre-cache data for all issues
+	 * that will be shown in a given view.  This is preferable to
+	 * the alternative option of querying the database for each
+	 * issue as the display() method is called.
+	 * @param array Bug objects
+	 */
+	public function cache( $p_bugs ) {}
+
+	/**
+	 * Function to display column data for a given bug row.
+	 * @param object Bug object
+	 * @param int Column display target
+	 */
+	public function display( $p_bug, $p_columns_target ) {
+		$t_tags = tag_bug_get_attached($p_bug->id);
+		if ($p_columns_target == COLUMNS_TARGET_CSV_PAGE) {
+			$i = 0;
+			foreach( $t_tags as $t_tag ) {
+				print( $i > 0 ? config_get( 'tag_separator' ) . ' ' : '' );
+				print ( string_display( $t_tag['name'] ) );
+				$i++;
+			}
+		}
+		else {
+			$i = 0;
+			foreach( $t_tags as $t_tag ) {
+				print( $i > 0 ? config_get( 'tag_separator' ) . ' ' : '' );
+				tag_display_link( $t_tag );
+				$i++;
+			}
+		}
+	}
+}
+
diff --git a/plugins/ColumnTags/ColumnTags.php b/plugins/ColumnTags/ColumnTags.php
new file mode 100644
index 0000000..4ba6651
--- /dev/null
+++ b/plugins/ColumnTags/ColumnTags.php
@@ -0,0 +1,81 @@
+<?PHP
+/**
+ * Mantis plugin ColumnTags
+ *
+ * This model class represents the business logic associated with the ColumnTags plugin
+ * @author Dominik Blunk <dominik.blunk@acc-solutions.ch>
+ * @copyright Copyright (c) 2010 - 2011, ACC Solutions AG
+ * @link http://www.acc-solutions.ch
+ * @package model
+ */
+require_once ('ColumnTags.class.php');
+
+/**
+ * ColumnTags plugin class
+ */
+class ColumnTagsPlugin extends MantisPlugin {
+
+	/**
+	 * Define name, version etc. of custom plugin
+	 *
+	 * @return void
+	 */
+	public function register() {
+		$this->name = 'Column Tags';																# Proper name of plugin
+		$this->description = 'Allow usage of Tags as Column on View Issues Page, CSV export etc.';	# Short description of the plugin
+		$this->page = '';																			# Default plugin page (when clicking on manage plugin page)
+		$this->version = '1.0';																		# Plugin version string
+		$this->requires = array( 'MantisCore' => '1.2.0' );											# Plugin dependencies, array of basename => version pairs. Should always depend on an appropriate version of MantisBT
+		$this->author = 'ACC Solutions AG';															# Author/team name
+		$this->contact = 'dev@acc-solutions.ch';													# Author/team e-mail address
+		$this->url = 'http://www.acc-solutions.ch';													# Support webpage
+	}
+
+	/**
+	 * Plugin configuration options
+	 *
+	 * @return array
+	 */
+	function config( ) {
+		return array( );
+	}
+
+	/**
+	 * Declaration of events that this plugin will trigger
+	 *
+	 * @return void
+	 */
+	function events( ) {
+		return array( );
+	}
+
+	/**
+	 * Declaration (assignment) of hooks (which events triggers which function)
+	 *
+	 * @return void
+	 */
+	function hooks( ) {
+		return array( 'EVENT_FILTER_COLUMNS' => 'get_tags_column' );
+	}
+
+	/**
+	 * Plugin initialisation
+	 *
+	 * @return void
+	 */
+	function init( ) {
+	}
+
+	/**
+	 * Return tag column
+	 * Plugin specific function
+	 *
+	 * @param string $p_event event name triggering the method
+	 * @param string $p_bug_id bug id
+	 * @return string
+	 */
+	function get_tags_column( $p_event, $p_bug_id ) {
+		# must correspond to an existing class name
+		return array( 'ColumnTags' );
+	}
+}
-- 
1.7.3.1.msysgit.0

