6 * Copyright 2010, Capgemini
7 * Author: Franck Villaume - Capgemini
9 * This file is part of FusionForge.
11 * FusionForge is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with FusionForge; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 require_once $gfcommon.'include/database-pgsql.php';
28 class MantisBTPlugin extends Plugin {
29 function MantisBTPlugin () {
31 $this->name = "mantisbt" ;
32 $this->text = "MantisBT" ; // To show in the tabs, use...
33 $this->hooks[] = "user_personal_links";//to make a link to the user's personal part of the plugin
34 $this->hooks[] = "usermenu" ;
35 $this->hooks[] = "groupmenu" ; // To put into the project tabs
36 $this->hooks[] = "groupisactivecheckbox" ; // The "use ..." checkbox in editgroupinfo
37 $this->hooks[] = "groupisactivecheckboxpost" ; //
38 $this->hooks[] = "userisactivecheckbox" ; // The "use ..." checkbox in user account
39 $this->hooks[] = "userisactivecheckboxpost" ; //
40 $this->hooks[] = "project_admin_plugins"; // to show up in the admin page fro group
41 $this->hooks[] = "change_cal_permission";
42 $this->hooks[] = "add_cal_link_father";
43 $this->hooks[] = "del_cal_link_father";
44 $this->hooks[] = "group_approved";
45 $this->hooks[] = "group_delete";
46 $this->hooks[] = "group_update";
49 function CallHook ($hookname, $params) {
50 global $use_mantisbtplugin,$G_SESSION,$HTML;
53 $text = $this->text; // this is what shows in the tab
54 if ($G_SESSION->usesPlugin($this->name)) {
55 $param = '?type=user&id=' . $G_SESSION->getId() . "&pluginname=" . $this->name; // we indicate the part we're calling is the user one
56 echo ' | ' . $HTML->PrintSubMenu (array ($text),
57 array ('/plugins/mantisbt/index.php' . $param ));
62 $group_id=$params['group'];
63 $project = &group_get_object($group_id);
64 if (!$project || !is_object($project)) {
67 if ($project->isError()) {
70 if (!$project->isProject()) {
74 if ($project->usesPlugin($this->name)) {
75 $params['TITLES'][]=$this->text;
76 $params['DIRS'][]='/plugins/' . $this->name . '/index.php?type=group&id=' . $group_id . "&pluginname=" . $this->name;
78 $params['TITLES'][]='<font color="#444444">'.$this->text.'</font>';
79 if ($G_SESSION && $project->getPermission(user_get_object($G_SESSION->getId()))->isAdmin()) {
80 $params['DIRS'][]='/project/admin/editgroupinfo.php?group_id=' . $group_id;
82 $params['DIRS'][]='#';
85 (($params['toptab'] == $this->name) ? $params['selected']=(count($params['TITLES'])-1) : '' );
88 case "groupisactivecheckbox":
89 //Check if the group is active
90 // this code creates the checkbox in the project edit public info page to activate/deactivate the plugin
91 $group_id=$params['group'];
92 $group = &group_get_object($group_id);
95 echo ' <input type="CHECKBOX" name="use_mantisbtplugin" value="1" ';
96 // CHECKED OR UNCHECKED?
97 if ( $group->usesPlugin ( $this->name ) ) {
98 echo "CHECKED disabled";
103 echo "<strong>Use ".$this->text." Plugin</strong>";
108 case "groupisactivecheckboxpost":
109 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
110 $group_id=$params['group'];
111 $group = &group_get_object($group_id);
112 $use_mantisbtplugin = getStringFromRequest('use_mantisbtplugin');
113 if ( $use_mantisbtplugin == "1" ) {
114 if (! $group->usesPlugin($this->name)) {
116 $group->setPluginUse($this->name, true);
118 // ajout du projet mantis s'il n'existe pas
119 if (!isProjetMantisCreated($group->data_array['group_id'])){
120 addProjetMantis($group->data_array['group_id'],$group->data_array['group_name'],$group->data_array['is_public'], $group->data_array['description']);
122 // mise a jour des utilisateurs avec les roles
124 foreach($group->getMembers() as $member){
125 $members[] = $member->data_array['user_name'];
127 updateUsersProjetMantis($group->data_array['group_id'],$members);
129 } else if ( $use_mantisbtplugin == "0" ) {
130 $group->setPluginUse ( $this->name, false );
134 case "userisactivecheckbox":
135 //check if user is active
136 // this code creates the checkbox in the user account manteinance page to activate/deactivate the plugin
137 $user = $params['user'];
140 echo ' <input type="CHECKBOX" name="use_mantisbtplugin" value="1" ';
141 // CHECKED OR UNCHECKED?
142 if ( $user->usesPlugin ( $this->name ) ) {
145 echo "> Use ".$this->text." Plugin";
150 case "userisactivecheckboxpost":
151 // this code actually activates/deactivates the plugin after the form was submitted in the user account manteinance page
152 $user = $params['user'];
153 $use_mantisbtplugin = getStringFromRequest('use_mantisbtplugin');
154 if ( $use_mantisbtplugin == 1 ) {
155 $user->setPluginUse ( $this->name );
157 $user->setPluginUse ( $this->name, false );
161 echo ' <input type="CHECKBOX" name="use_mantisbtplugin" value="1" ';
162 // CHECKED OR UNCHECKED?
163 if ( $user->usesPlugin ( $this->name ) ) {
166 echo "> Use ".$this->text." Plugin";
171 case "user_personal_links":
172 // this displays the link in the user's profile page to it's personal MantisBT (if you want other sto access it, youll have to change the permissions in the index.php
173 $userid = $params['user_id'];
174 $user = user_get_object($userid);
175 $text = $params['text'];
176 //check if the user has the plugin activated
177 if ($user->usesPlugin($this->name)) {
179 echo util_make_link ("/plugins/mantisbt/index.php?id=$userid&type=user&pluginname=".$this->name,
180 _('View Personal MantisBT')
186 case "project_admin_plugins":
187 // this displays the link in the project admin options page to it's MantisBT administration
188 $group_id = $params['group_id'];
189 $group = &group_get_object($group_id);
190 if ( $group->usesPlugin ( $this->name ) ) {
191 echo util_make_link ("/plugins/mantisbt/index.php?id=$group_id&type=admin&pluginname=".$this->name,
192 _('View Admin MantisBT')
198 case "group_approved":
199 $group_id=$params['group_id'];
200 $group = &group_get_object($group_id);
201 if ($group->usesPlugin($this->name)) {
202 // ajout du projet mantis s'il n'existe pas
203 if (!isProjetMantisCreated($group->data_array['group_id'])){
204 addProjetMantis($group->data_array['group_id'],$group->data_array['group_name'],$group->data_array['is_public'], $group->data_array['description']);
206 // mise a jour des utilisateurs avec les roles
208 foreach($group->getMembers() as $member){
209 $members[] = $member->data_array['user_name'];
211 updateUsersProjetMantis($group->data_array['group_id'],$members);
215 case "change_cal_permission":
216 // mise a jour des utilisateurs avec les roles
217 $group_id=$params[1];
218 $group = &group_get_object($group_id);
220 foreach($group->getMembers() as $member){
221 $members[] = $member->data_array['user_name'];
223 updateUsersProjetMantis($group->data_array['group_id'],$members);
226 case "add_cal_link_father":
227 case "del_cal_link_father":
228 $sub_group_id = $params[0];
229 $group_id = $params[1];
230 refreshHierarchyMantisBt();
234 $group_id=$params['group_id'];
235 $group = &group_get_object($group_id);
236 if ($group->usesPlugin($this->name)) {
237 if (isProjetMantisCreated($group->data_array['group_id'])) {
238 removeProjetMantis($group->data_array['group_id']);
244 $group_id=$params['group_id'];
245 $group = &group_get_object($group_id);
246 if ($group->usesPlugin($this->name)) {
247 if (isProjetMantisCreated($group_id)) {
248 updateProjetMantis($group_id, $params['group_name'], $group->data_array['is_public'], $group->data_array['short_description']);
256 function addProjetMantis($idProjet, $nomProjet, $isPublic, $description){
258 global $sys_mantisbt_host,$sys_mantisbt_user, $sys_mantisbt_password;
261 $project['name'] = $nomProjet;
262 $project['status'] = "development";
264 if ($isPublic == "1"){
265 $project['view_state'] = 10;
267 $project['view_state'] = 50;
270 $project['description'] = $description;
272 $clientSOAP = new SoapClient("http://$sys_mantisbt_host/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
275 $idProjetMantis = $clientSOAP->__soapCall('mc_project_add', array("username" => $sys_mantisbt_user, "password" => $sys_mantisbt_password, "project" => $project));
276 }catch (SoapFault $soapFault) {
277 echo $soapFault->faultstring;
280 if (!isset($idProjetMantis) || !is_int($idProjetMantis)){
281 echo "Error : Impossible de créer le projet dans mantis";
283 db_query_params('INSERT INTO group_mantisbt (id_group, id_mantisbt) VALUES ($1,$2)',
284 array($idProjet, $idProjetMantis));
289 function removeProjetMantis($idProjet){
291 global $sys_mantisbt_host,$sys_mantisbt_user, $sys_mantisbt_password;
293 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
297 $row =& db_fetch_array($resIdProjetMantis);
299 if ($row == null || count($row)>2) {
300 echo "Erreur : impossible de retrouver le projet au sein de mantisbt";
302 $idMantisbt = $row['id_mantisbt'];
303 $clientSOAP = new SoapClient("http://$sys_mantisbt_host/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
305 $delete = $clientSOAP->__soapCall('mc_project_delete', array("username" => $sys_mantisbt_user, "password" => $sys_mantisbt_password, "project_id" => $idMantisbt));
306 }catch (SoapFault $soapFault) {
307 echo $soapFault->faultstring;
311 echo "Error : Impossible de supprimer le projet dans mantis : ".$idProjet;
314 db_query_params('DELETE FROM group_mantisbt WHERE group_mantisbt.id_mantisbt = $1',
321 function updateProjetMantis($idProjet,$nomProjet,$isPublic, $description) {
323 global $sys_mantisbt_host,$sys_mantisbt_user, $sys_mantisbt_password;
326 $project['name'] = $nomProjet;
327 $project['status'] = "development";
329 if ($isPublic == "1"){
330 $project['view_state'] = 10;
332 $project['view_state'] = 50;
335 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
338 $row =& db_fetch_array($resIdProjetMantis);
339 if ($row == null || count($row)>2) {
340 echo "Erreur : impossible de retrouver le projet au sein de mantisbt";
342 $idMantisbt = $row['id_mantisbt'];
343 $clientSOAP = new SoapClient("http://$sys_mantisbt_host/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
345 $update = $clientSOAP->__soapCall('mc_project_update', array("username" => $sys_mantisbt_user, "password" => $sys_mantisbt_password, "project_id" => $idMantisbt, "project" => $project));;
346 } catch (SoapFault $soapFault) {
347 echo $soapFault->faultstring;
351 echo "Error : update MantisBT projet";
356 function isProjetMantisCreated($idProjet){
358 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
361 $row =& db_fetch_array($resIdProjetMantis);
371 function getIdProjetMantis($idProjet){
373 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
376 $row =& db_fetch_array($resIdProjetMantis);
381 return $row['id_mantisbt'];
386 function updateUsersProjetMantis($idProjet, $members){
389 global $sys_mantisbt_host, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_db_port, $sys_mantisbt_db_name;
391 // recuperation de id mantis
392 $idMantis = getIdProjetMantis($idProjet);
394 // TODO corriger inclusion bug
396 $role['Manager'] = 70;
397 $role['Concepteur'] = 55;
398 $role['Collaborateur'] = 55;
399 $role['Rapporteur'] = 55;
403 $stateForge = array ();
404 foreach ($members as $key => $member){
405 $resUserRole = db_query_params('SELECT role.role_name
406 FROM role, user_group, users
407 WHERE users.user_name = $1
408 AND ( user_group.user_id = users.user_id AND user_group.group_id = $2 )
409 AND user_group.role_id = role.role_id',
410 array($member,$idProjet));
412 $row =& db_fetch_array($resUserRole);
413 $stateForge[$member]['name'] = $member;
414 $stateForge[$member]['role'] = $row['role_name'];
416 // on supprime les precedentes relations dans mantis
417 $stateMantis = array ();
418 $dbConnection = db_connect_host($sys_mantisbt_db_name, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_host, $sys_mantisbt_db_port);
420 $errMantis1 = "Error : Could not open connection" . db_error($dbConnection);
422 db_rollback($dbConnection);
424 $result = pg_delete($dbConnection,"mantis_project_user_list_table",array("project_id"=>$idMantis));
426 echo "Error : Impossible de nettoyer les roles dans mantisbt";
428 foreach($stateForge as $member => $array){
430 // recuperation de l'id user dans mantis
431 $resultIdUser = db_query_params('SELECT mantis_user_table.id FROM mantis_user_table WHERE mantis_user_table.username = $1',
432 array($member),'-1',0,$dbConnection);
434 $rowIdUser =& db_fetch_array($resultIdUser);
435 $idUser = $rowIdUser['id'];
436 // insertion de la relation
437 $resultInsert = pg_insert($dbConnection,
438 "mantis_project_user_list_table",
439 array ("project_id" => $idMantis, "user_id" => $idUser, "access_level" => $role[$array['role']])
442 echo "Error : Impossible de mettre à jour les roles dans mantisbt";
449 function refreshHierarchyMantisBt(){
450 global $sys_mantisbt_host, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_db_port, $sys_mantisbt_db_name;
452 $hierarchies=db_query_params('SELECT project_id, sub_project_id FROM plugin_projects_hierarchy WHERE activated=true');
454 $dbConnection = db_connect_host($sys_mantisbt_db_name, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_host, $sys_mantisbt_db_port);
456 db_rollback($dbConnection);
460 db_begin($dbConnection);
461 db_query_params('TRUNCATE TABLE mantis_project_hierarchy_table',array() , '-1', 0, $dbConnection);
462 while ($hierarchy = db_fetch_array($hierarchies)) {
463 $result = db_query_params ('INSERT INTO mantis_project_hierarchy_table (child_id, parent_id, inherit_parent)
464 VALUES ($1, $2, $3)',
465 array (getIdProjetMantis($hierarchy['sub_project_id']),
466 getIdProjetMantis($hierarchy['project_id']),
474 $this->setError(_('Insert Failed') . db_error($dbConnection));
480 db_commit($dbConnection);
481 pg_close($dbConnection);
487 // c-file-style: "bsd"