MantisBT插件实践(查询Project)

MantisBT插件实践(查询Project)                         

 

MantisBT是一个口碑不错的缺陷管理系统,相比同样开源的Bugzilla,MantisBT最大的特点就是易部署和扩展,很适合规模不大的团队使用。

最近由于MantisBT系统的Project越来越多,作者花了些时间实现了一个可以根据关键字查询project的插件。这篇文章就以查询project为例,简单介绍下mantisBT插件的实现方式,由于作者也是初学,如有不正确的地方,请大家指正。

首先插件的功能很简单,就是在主菜单上条件Projects的link,然后link指向自己的php页面,页面上可以填写查询内容,下面列出满足条件的project列表:

 

 一般插件的目录结构如下:

Projects.php定义了ProjectsPlugin类,该类需要继承MantisPlugin:

  1. /** 
  2.  *  引用 MantisPlugin.class.php 
  3.  */  
  4.   
  5. require_once( config_get( 'class_path' ) . 'MantisPlugin.class.php' );  
  6.   
  7. class ProjectsPlugin extends MantisPlugin {  
  8.   
  9.     /** 
  10.      *  定义插件的名称、描述、版本、依赖关系等信息 
  11.      */  
  12.     function register( ) {  
  13.         $this->name = plugin_lang_get( 'title' );  
  14.         $this->description = plugin_lang_get( 'description' );  
  15.         //$this->page = 'config';  
  16.   
  17.         $this->version = '0.0.1';  
  18.         $this->requires = array(  
  19.             'MantisCore' => '1.2.0',  
  20.         );  
  21.   
  22.         $this->author = 'Joseph';  
  23.         $this->contact = 'Joseph.Zhao2@gmail.com';  
  24.         $this->url = 'http://www.mantisbt.org';  
  25.     }  
  26.   
  27.     /** 
  28.      * 由于本插件简单,install函数直接返回true 
  29.      */  
  30.     function install() {  
  31.         return true;  
  32.     }  
  33.   
  34.     /* 
  35.      * 添加Event 
  36.      */  
  37.     function hooks( ) {  
  38.         $t_hooks = array(  
  39.             'EVENT_MENU_MAIN'  => 'print_menu_projects',  
  40.         );  
  41.         return array_merge( parent::hooks(), $t_hooks );  
  42.     }  
  43.   
  44.     /* 
  45.      * 添加link 
  46.      */  
  47.     function print_menu_projects( ) {  
  48.         $t_links = array();  
  49.         // plugin_page函数用来拼接pages目录下面的php页面  
  50.         $t_page = plugin_page( 'list' );  
  51.         $t_lang = plugin_lang_get( 'search_link' );  
  52.         $t_links[] = "<a href=\"$t_page\">$t_lang</a>";  
  53.         return $t_links;  
  54.     }  
  55. }  
/**
 *  引用 MantisPlugin.class.php
 */

require_once( config_get( 'class_path' ) . 'MantisPlugin.class.php' );

class ProjectsPlugin extends MantisPlugin {

	/**
	 *  定义插件的名称、描述、版本、依赖关系等信息
	 */
	function register( ) {
		$this->name = plugin_lang_get( 'title' );
		$this->description = plugin_lang_get( 'description' );
		//$this->page = 'config';

		$this->version = '0.0.1';
		$this->requires = array(
			'MantisCore' => '1.2.0',
		);

		$this->author = 'Joseph';
		$this->contact = 'Joseph.Zhao2@gmail.com';
		$this->url = 'http://www.mantisbt.org';
	}

	/**
	 * 由于本插件简单,install函数直接返回true
	 */
	function install() {
		return true;
	}

	/*
	 * 添加Event
	 */
	function hooks( ) {
		$t_hooks = array(
			'EVENT_MENU_MAIN'  => 'print_menu_projects',
		);
		return array_merge( parent::hooks(), $t_hooks );
	}

	/*
	 * 添加link
	 */
	function print_menu_projects( ) {
		$t_links = array();
		// plugin_page函数用来拼接pages目录下面的php页面
		$t_page = plugin_page( 'list' );
		$t_lang = plugin_lang_get( 'search_link' );
		$t_links[] = "<a href=\"$t_page\">$t_lang</a>";
		return $t_links;
	}
}

lang目录下面存放参数配置文件,支持多语言,作者的只有strings_english.txt:

  1. <?php  
  2. /* 
  3.  Projects plugin help to search the project by project name 
  4. */  
  5.   
  6.     $s_plugin_Projects = '';  
  7.     $s_plugin_Projects_title = 'Search Projects';  
  8.     $s_plugin_Projects_description = 'Plugin add project searching to MantisBT';  
  9.     $s_plugin_Projects_search_link = 'Projects';  
  10.     $s_plugin_Projects_not_found = 'Sorry. No projects contain your search text.';  
<?php
/*
 Projects plugin help to search the project by project name
*/

	$s_plugin_Projects = '';
	$s_plugin_Projects_title = 'Search Projects';
	$s_plugin_Projects_description = 'Plugin add project searching to MantisBT';
	$s_plugin_Projects_search_link = 'Projects';
	$s_plugin_Projects_not_found = 'Sorry. No projects contain your search text.';

 pages目录就是插件具体显示的页面,由于内容较多这里就不贴所有代码了,仅罗列下重要的部分,下面是查询数据库的函数:

  1. # --------------------  
  2. # 根据查询内容在数据库里查找符合条件的project id列表  
  3. function user_search_accessible_projects( $p_user_id, $p_search, $p_show_disabled = false ) {  
  4.     // 获得数据库表名称  
  5.     $t_project_table = db_get_table( 'mantis_project_table' );  
  6.     $t_project_user_list_table = db_get_table( 'mantis_project_user_list_table' );  
  7.     $t_project_hierarchy_table = db_get_table( 'mantis_project_hierarchy_table' );  
  8.       
  9.     $t_public = VS_PUBLIC;  
  10.     $t_private = VS_PRIVATE;  
  11.   
  12.     $result = null;  
  13.     // 查询SQL  
  14.     $query = "SELECT p.id, p.name, ph.parent_id  
  15.               FROM $t_project_table p  
  16.               LEFT JOIN $t_project_user_list_table u  
  17.                     ON p.id=u.project_id AND u.user_id=" . db_param() . "  
  18.               LEFT JOIN $t_project_hierarchy_table ph  
  19.                     ON ph.child_id = p.id  
  20.               WHERE " . ( $p_show_disabled ? '' : ( 'p.enabled = ' . db_param() . ' AND ' ) ) . "  
  21.                     ( p.view_state=" . db_param() . "  
  22.                         OR (p.view_state=" . db_param() . "  
  23.                         AND  
  24.                         u.user_id=" . db_param() . " )  
  25.                     )   
  26.                     AND p.name like '%". $p_search ."%'  
  27.               ORDER BY p.name";  
  28.     // SQL执行  
  29.     $result = db_query_bound( $query, ( $p_show_disabled ? Array( $p_user_id, $t_public, $t_private, $p_user_id ) : Array( $p_user_id, true, $t_public, $t_private, $p_user_id ) ) );  
  30.     // 查询结果的条数  
  31.     $row_count = db_num_rows( $result );  
  32.   
  33.     $t_projects = array();  
  34.     // 遍历查询结果  
  35.     for( $i = 0;$i < $row_count;$i++ ) {  
  36.         $row = db_fetch_array( $result );  
  37.         $t_projects[(int)$row['id']] = ( $row['parent_id'] === NULL ) ? 0 : (int)$row['parent_id'];  
  38.     }  
  39.   
  40.     // 剔除重复的project id  
  41.     $t_prune = array();  
  42.     foreach( $t_projects as $t_id => $t_parent ) {  
  43.         if(( $t_parent !== 0 ) && isset( $t_projects[$t_parent] ) ) {  
  44.             $t_prune[] = $t_id;  
  45.         }  
  46.     }  
  47.     foreach( $t_prune as $t_id ) {  
  48.         unset( $t_projects[$t_id] );  
  49.     }  
  50.     // 获得所有project id  
  51.     $t_projects = array_keys( $t_projects );  
  52.     return $t_projects;  
  53. }  
# --------------------
# 根据查询内容在数据库里查找符合条件的project id列表
function user_search_accessible_projects( $p_user_id, $p_search, $p_show_disabled = false ) {
	// 获得数据库表名称
	$t_project_table = db_get_table( 'mantis_project_table' );
	$t_project_user_list_table = db_get_table( 'mantis_project_user_list_table' );
	$t_project_hierarchy_table = db_get_table( 'mantis_project_hierarchy_table' );
	
	$t_public = VS_PUBLIC;
	$t_private = VS_PRIVATE;

	$result = null;
	// 查询SQL
	$query = "SELECT p.id, p.name, ph.parent_id
			  FROM $t_project_table p
			  LEFT JOIN $t_project_user_list_table u
				    ON p.id=u.project_id AND u.user_id=" . db_param() . "
			  LEFT JOIN $t_project_hierarchy_table ph
				    ON ph.child_id = p.id
			  WHERE " . ( $p_show_disabled ? '' : ( 'p.enabled = ' . db_param() . ' AND ' ) ) . "
					( p.view_state=" . db_param() . "
					    OR (p.view_state=" . db_param() . "
					    AND
						u.user_id=" . db_param() . " )
					) 
					AND p.name like '%". $p_search ."%'
			  ORDER BY p.name";
	// SQL执行
	$result = db_query_bound( $query, ( $p_show_disabled ? Array( $p_user_id, $t_public, $t_private, $p_user_id ) : Array( $p_user_id, true, $t_public, $t_private, $p_user_id ) ) );
	// 查询结果的条数
	$row_count = db_num_rows( $result );

	$t_projects = array();
	// 遍历查询结果
	for( $i = 0;$i < $row_count;$i++ ) {
		$row = db_fetch_array( $result );
		$t_projects[(int)$row['id']] = ( $row['parent_id'] === NULL ) ? 0 : (int)$row['parent_id'];
	}

	// 剔除重复的project id
	$t_prune = array();
	foreach( $t_projects as $t_id => $t_parent ) {
		if(( $t_parent !== 0 ) && isset( $t_projects[$t_parent] ) ) {
			$t_prune[] = $t_id;
		}
	}
	foreach( $t_prune as $t_id ) {
		unset( $t_projects[$t_id] );
	}
	// 获得所有project id
	$t_projects = array_keys( $t_projects );
	return $t_projects;
}

 通过查询到的project id,获得project详细信息的内容:

  1. //查询project id  
  2. $t_projects = user_search_accessible_projects(auth_get_current_user_id(), REQUEST["search"] ,false);  
  3. $t_full_projects = array();  
  4. foreach ( $t_projects as $t_project_id ) {  
  5.     // 根据project id,获得project内容  
  6.     $t_full_projects[] = project_get_row( $t_project_id );  
  7. }  
  8. $f_sort = gpc_get_string( 'sort', 'name' );  
  9. $t_projects = multi_sort( $t_full_projects, $f_sort, $t_direction );  
  10. $t_stack    = array( $t_projects );  
	//查询project id
	$t_projects = user_search_accessible_projects(auth_get_current_user_id(), REQUEST["search"] ,false);
	$t_full_projects = array();
	foreach ( $t_projects as $t_project_id ) {
		// 根据project id,获得project内容
		$t_full_projects[] = project_get_row( $t_project_id );
	}
	$f_sort	= gpc_get_string( 'sort', 'name' );
	$t_projects = multi_sort( $t_full_projects, $f_sort, $t_direction );
	$t_stack 	= array( $t_projects );

后面的工作就是用php语言把project使用table画出来。

 

posted @ 2017-06-01 10:48  sky20080101  阅读(524)  评论(1编辑  收藏  举报