MantisBT插件实践(查询Project)
MantisBT插件实践(查询Project)
MantisBT是一个口碑不错的缺陷管理系统,相比同样开源的Bugzilla,MantisBT最大的特点就是易部署和扩展,很适合规模不大的团队使用。
最近由于MantisBT系统的Project越来越多,作者花了些时间实现了一个可以根据关键字查询project的插件。这篇文章就以查询project为例,简单介绍下mantisBT插件的实现方式,由于作者也是初学,如有不正确的地方,请大家指正。
首先插件的功能很简单,就是在主菜单上条件Projects的link,然后link指向自己的php页面,页面上可以填写查询内容,下面列出满足条件的project列表:
一般插件的目录结构如下:
Projects.php定义了ProjectsPlugin类,该类需要继承MantisPlugin:
- /**
- * 引用 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;
- }
- }
/** * 引用 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:
- <?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.';
<?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目录就是插件具体显示的页面,由于内容较多这里就不贴所有代码了,仅罗列下重要的部分,下面是查询数据库的函数:
- # --------------------
- # 根据查询内容在数据库里查找符合条件的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列表 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详细信息的内容:
- //查询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 );
//查询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画出来。