Skip to content

Commit

Permalink
Fix #10596: Implemented pluggable issue columns.
Browse files Browse the repository at this point in the history
Added new MantisColumn class archetype to define the standard format
and behavior for custom columns and how they display to the user.

Currently, plugin columns are limited to viewing data only, and can
not be used for sorting, although the sorting data is currently
passed for potential future implementation.
  • Loading branch information
amyreese committed Jun 17, 2009
1 parent f4ebcfa commit 991e5b6
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 2 deletions.
44 changes: 44 additions & 0 deletions core/classes/MantisColumn.class.php
@@ -0,0 +1,44 @@
<?php
# MantisBT - a php based bugtracking system

# Copyright (C) 2002 - 2009 MantisBT Team - mantisbt-dev@lists.sourceforge.

# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.

/**
* Base class that implements basic column functionality
* and integration with MantisBT.
* @package MantisBT
* @subpackage classes
*/
abstract class MantisColumn {

/**
* Column title, as displayed to the user.
*/
public $title = null;

/**
* Column name, as selected in the manage columns interfaces.
*/
public $column = null;

/**
* Function to display column data for a given bug row.
* @param object Bug object
* @param int Column display target
*/
abstract function display( $p_bug, $p_columns_target );
}

70 changes: 68 additions & 2 deletions core/columns_api.php
Expand Up @@ -41,6 +41,36 @@ function columns_get_standard() {
return array_keys($t_columns);
}

/**
* Allow plugins to define a set of class-based columns, and register/load
* them here to be used by columns_api.
* @return array Mapping of column name to column object
*/
function columns_get_plugin_columns() {
static $s_column_array = null;

if ( is_null( $s_column_array ) ) {
$s_column_array = array();

$t_all_plugin_columns = event_signal( 'EVENT_FILTER_COLUMNS' );
foreach( $t_all_plugin_columns as $t_plugin => $t_plugin_columns ) {
foreach( $t_plugin_columns as $t_callback => $t_plugin_column_array ) {
if ( is_array( $t_plugin_column_array ) ) {
foreach( $t_plugin_column_array as $t_column_class ) {
if ( class_exists( $t_column_class ) && is_subclass_of( $t_column_class, 'MantisColumn' ) ) {
$t_column_object = new $t_column_class();
$t_column_name = utf8_strtolower( $t_plugin . '_' . $t_column_object->column );
$s_column_array[ $t_column_name ] = $t_column_object;
}
}
}
}
}
}

return $s_column_array;
}

/**
* Get all accessible columns for the current project / current user..
* @param int $p_project_id project id
Expand All @@ -50,6 +80,9 @@ function columns_get_standard() {
function columns_get_all( $p_project_id = null ) {
$t_columns = columns_get_standard();

# add plugin columns
$t_columns = array_merge( $t_columns, array_keys( columns_get_plugin_columns() ) );

# Add project custom fields to the array. Only add the ones for which the current user has at least read access.
if( $p_project_id === null ) {
$t_project_id = helper_get_current_project();
Expand All @@ -67,8 +100,6 @@ function columns_get_all( $p_project_id = null ) {
$t_columns[] = 'custom_' . $t_def['name'];
}

# foreach

return $t_columns;
}

Expand Down Expand Up @@ -148,6 +179,12 @@ function column_get_title( $p_column ) {
return $t_custom_field;
}

$t_plugin_columns = columns_get_plugin_columns();
if ( isset( $t_plugin_columns[ $p_column ] ) ) {
$t_column_object = $t_plugin_columns[ $p_column ];
return $t_column_object->title;
}

switch( $p_column ) {
case 'attachment':
return lang_get( 'attachments' );
Expand Down Expand Up @@ -719,6 +756,35 @@ function print_column_selection( $p_bug, $p_columns_target = COLUMNS_TARGET_VIEW
echo '</td>';
}

/**
* Print column title for a specific custom column.
* @param object Column object
* @param string sort
* @param string direction
* @param int $p_columns_target: see COLUMNS_TARGET_* in constant_inc.php
* @access public
*/
function print_column_title_plugin( $p_column_object, $p_sort, $p_dir, $p_columns_target=COLUMNS_TARGET_VIEW_PAGE ) {
echo '<td>', string_display_line( $p_column_object->title ), '</td>';
}

/**
* Print custom column content for a specific bug.
* @param object Column object
* @param array $p_row bug row
* @param int $p_columns_target: see COLUMNS_TARGET_* in constant_inc.php
* @access public
*/
function print_column_plugin( $p_column_object, $p_bug, $p_columns_target = COLUMNS_TARGET_VIEW_PAGE ) {
if ( $p_columns_target != COLUMNS_TARGET_CSV_PAGE ) {
echo '<td>';
$p_column_object->display( $p_bug, $p_columns_target );
echo '</td>';
} else {
$p_column_object->display( $p_bug, $p_columns_target );
}
}

/**
*
* @param array $p_row bug row
Expand Down
14 changes: 14 additions & 0 deletions core/custom_function_api.php
Expand Up @@ -263,9 +263,16 @@ function custom_function_default_print_column_title( $p_column, $p_columns_targe
echo '</td>';
}
} else {
$t_plugin_columns = columns_get_plugin_columns();

$t_function = 'print_column_title_' . $p_column;
if( function_exists( $t_function ) ) {
$t_function( $t_sort, $t_dir, $p_columns_target );

} else if ( isset( $t_plugin_columns[ $p_column ] ) ) {
$t_column_object = $t_plugin_columns[ $p_column ];
print_column_title_plugin( $t_column_object, $p_sort, $p_dir, $p_columns_target );

} else {
echo '<td>';
print_view_bug_sort_link( column_get_title( $p_column ), $p_column, $t_sort, $t_dir, $p_columns_target );
Expand Down Expand Up @@ -313,6 +320,8 @@ function custom_function_default_print_column_value( $p_column, $p_issue_row, $p
}
echo $t_column_end;
} else {
$t_plugin_columns = columns_get_plugin_columns();

if( $p_columns_target != COLUMNS_TARGET_CSV_PAGE ) {
$t_function = 'print_column_' . $p_column;
} else {
Expand All @@ -325,6 +334,11 @@ function custom_function_default_print_column_value( $p_column, $p_issue_row, $p
} else {
$t_function( $p_issue_row[$p_column] );
}

} else if ( isset( $t_plugin_columns[ $p_column ] ) ) {
$t_column_object = $t_plugin_columns[ $p_column ];
print_column_plugin( $t_column_object, $p_issue_row, $p_columns_target );

} else {
if( isset( $p_issue_row[$p_column] ) ) {
echo $t_column_start . $p_issue_row[$p_column] . $t_column_end;
Expand Down
1 change: 1 addition & 0 deletions core/events_inc.php
Expand Up @@ -71,6 +71,7 @@

# Bug filter events
'EVENT_FILTER_FIELDS' => EVENT_TYPE_DEFAULT,
'EVENT_FILTER_COLUMNS' => EVENT_TYPE_DEFAULT,

# Bug report event
'EVENT_REPORT_BUG_FORM_TOP' => EVENT_TYPE_EXECUTE,
Expand Down

0 comments on commit 991e5b6

Please sign in to comment.