6 * Copyright 2009 - 2010 (c) : Franck Villaume - Capgemini
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with FusionForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 require_once 'include/database-pgsql.php';
27 class MantisBTPlugin extends Plugin {
28 function MantisBTPlugin () {
30 $this->name = "mantisbt" ;
31 $this->text = "MantisBT" ; // To show in the tabs, use...
32 $this->hooks[] = "user_personal_links";//to make a link to the user's personal part of the plugin
33 $this->hooks[] = "usermenu" ;
34 $this->hooks[] = "groupmenu" ; // To put into the project tabs
35 $this->hooks[] = "groupisactivecheckbox" ; // The "use ..." checkbox in editgroupinfo
36 $this->hooks[] = "groupisactivecheckboxpost" ; //
37 $this->hooks[] = "userisactivecheckbox" ; // The "use ..." checkbox in user account
38 $this->hooks[] = "userisactivecheckboxpost" ; //
39 $this->hooks[] = "project_admin_plugins"; // to show up in the admin page fro group
40 $this->hooks[] = "change_cal_permission";
41 $this->hooks[] = "change_cal_mail";
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) || $project->isError() || !$project->isProject()) {
68 if ($project->usesPlugin($this->name)) {
69 $params['TITLES'][]=$this->text;
70 $params['DIRS'][]='/plugins/' . $this->name . '/?type=group&id=' . $group_id . "&pluginname=" . $this->name;
72 if ($params['toptab'] == $this->name) {
73 $params['selected']=(count($params['TITLES'])-1);
77 case "groupisactivecheckbox":
78 //Check if the group is active
79 // this code creates the checkbox in the project edit public info page to activate/deactivate the plugin
80 $group_id=$params['group'];
81 $group = &group_get_object($group_id);
84 echo ' <input type="CHECKBOX" name="use_mantisbtplugin" value="1" ';
85 // CHECKED OR UNCHECKED?
86 if ( $group->usesPlugin ( $this->name ) ) {
87 echo 'checked="checked"';
92 echo "<strong>Use ".$this->text." Plugin</strong>";
97 case "groupisactivecheckboxpost":
98 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
99 $group_id=$params['group'];
100 $group = &group_get_object($group_id);
101 $use_mantisbtplugin = getIntFromRequest('use_mantisbtplugin');
102 if ( $use_mantisbtplugin == "1" ) {
103 if (! $group->usesPlugin($this->name)) {
105 $group->setPluginUse($this->name, true);
107 // ajout du projet mantis s'il n'existe pas
108 if (!isProjetMantisCreated($group->data_array['group_id'])){
109 addProjetMantis($group->data_array['group_id'],$group->data_array['group_name'],$group->data_array['is_public'], $group->data_array['short_description']);
111 // mise a jour des utilisateurs avec les roles
113 foreach($group->getMembers() as $member){
114 $members[] = $member->data_array['user_name'];
116 //updateUsersProjetMantis($group->data_array['group_id'],$members);
118 } else if ( $use_mantisbtplugin == "0" ) {
119 $group->setPluginUse ( $this->name, false );
123 case "userisactivecheckbox":
124 //check if user is active
125 // this code creates the checkbox in the user account manteinance page to activate/deactivate the plugin
126 $user = $params['user'];
129 echo ' <input type="CHECKBOX" name="use_mantisbtplugin" value="1" ';
130 // CHECKED OR UNCHECKED?
131 if ( $user->usesPlugin ( $this->name ) ) {
132 echo 'checked="CHECKED"';
134 echo "> Use ".$this->text." Plugin";
139 case "userisactivecheckboxpost":
140 // this code actually activates/deactivates the plugin after the form was submitted in the user account manteinance page
141 $user = $params['user'];
142 $use_mantisbtplugin = getIntFromRequest('use_mantisbtplugin');
143 if ( $use_mantisbtplugin == 1 ) {
144 $user->setPluginUse ( $this->name );
146 $user->setPluginUse ( $this->name, false );
150 case "user_personal_links":
151 // 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
152 $userid = $params['user_id'];
153 $user = user_get_object($userid);
154 $text = $params['text'];
155 //check if the user has the plugin activated
156 if ($user->usesPlugin($this->name)) {
158 echo util_make_link ("/plugins/mantisbt/index.php?id=$userid&type=user&pluginname=".$this->name,
159 _('View Personal MantisBT')
165 case "project_admin_plugins":
166 // this displays the link in the project admin options page to it's MantisBT administration
167 $group_id = $params['group_id'];
168 $group = &group_get_object($group_id);
169 if ( $group->usesPlugin ( $this->name ) ) {
170 echo util_make_link ("/plugins/mantisbt/index.php?id=$group_id&type=admin&pluginname=".$this->name,
171 _('View Admin MantisBT')
177 case "group_approved":
178 $group_id=$params['group_id'];
179 $group = &group_get_object($group_id);
180 if ($group->usesPlugin($this->name)) {
181 // ajout du projet mantis s'il n'existe pas
182 if (!isProjetMantisCreated($group->data_array['group_id'])){
183 addProjetMantis($group->data_array['group_id'],$group->data_array['group_name'],$group->data_array['is_public'], $group->data_array['short_description']);
186 // mise a jour des utilisateurs avec les roles
188 foreach($group->getMembers() as $member){
189 $members[] = $member->data_array['user_name'];
191 //updateUsersProjetMantis($group->data_array['group_id'],$members);
195 case "change_cal_permission":
196 // mise a jour des utilisateurs avec les roles
197 $group_id=$params[1];
198 $group = &group_get_object($group_id);
200 foreach($group->getMembers() as $member){
201 $members[] = $member->data_array['user_name'];
203 //updateUsersProjetMantis($group->data_array['group_id'],$members);
206 // mise a jour de l'adresse mail utilisateur
207 case "change_cal_mail":
209 updateUserInMantis($user_id);
212 case "add_cal_link_father":
213 case "del_cal_link_father":
214 $sub_group_id = $params[0];
215 $group_id = $params[1];
216 refreshHierarchyMantisBt();
220 $group_id=$params['group_id'];
221 $group = &group_get_object($group_id);
222 if ($group->usesPlugin($this->name)) {
223 if (isProjetMantisCreated($group->data_array['group_id'])) {
224 removeProjetMantis($group->data_array['group_id']);
230 $group_id=$params['group_id'];
231 $group = &group_get_object($group_id);
232 if ($group->usesPlugin($this->name)) {
233 if (isProjetMantisCreated($group_id)) {
234 updateProjetMantis($group_id, $params['group_name'], $group->data_array['is_public'], $group->data_array['short_description']);
242 function addProjetMantis($idProjet, $nomProjet, $isPublic, $description) {
245 $project['name'] = $nomProjet;
246 $project['status'] = "development";
248 if ($isPublic == "1"){
249 $project['view_state'] = 10;
251 $project['view_state'] = 50;
254 $project['description'] = $description;
257 $clientSOAP = new SoapClient("http://".forge_get_config('server','mantisbt')."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
258 $idProjetMantis = $clientSOAP->__soapCall('mc_project_add', array("username" => forge_get_config('adminsoap_user','mantisbt'), "password" => forge_get_config('adminsoap_password','mantisbt'), "project" => $project));
259 }catch (SoapFault $soapFault) {
260 echo $soapFault->faultstring;
263 if (!isset($idProjetMantis) || !is_int($idProjetMantis)){
264 echo "Error : Impossible de créer le projet dans mantis";
267 db_query_params('INSERT INTO group_mantisbt (id_group, id_mantisbt) VALUES ($1,$2)',
268 array($idProjet, $idProjetMantis));
273 function removeProjetMantis($idProjet) {
275 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
279 $row = db_fetch_array($resIdProjetMantis);
281 if ($row == null || count($row)>2) {
282 echo "Erreur : impossible de retrouver le projet au sein de mantisbt";
284 $idMantisbt = $row['id_mantisbt'];
286 $clientSOAP = new SoapClient("http://".forge_get_config('server','mantisbt')."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
287 $delete = $clientSOAP->__soapCall('mc_project_delete', array("username" => forge_get_config('adminsoap_user','mantisbt'), "password" => forge_get_config('adminsoap_password','mantisbt'), "project_id" => $idMantisbt));
288 }catch (SoapFault $soapFault) {
289 echo $soapFault->faultstring;
291 if (!isset($delete)){
292 echo "Error : Impossible de supprimer le projet dans mantis : ".$idProjet;
295 db_query_params('DELETE FROM group_mantisbt WHERE group_mantisbt.id_mantisbt = $1',
302 function updateProjetMantis($idProjet,$nomProjet,$isPublic, $description) {
305 $project['name'] = $nomProjet;
306 $project['status'] = "development";
308 if ($isPublic == "1"){
309 $project['view_state'] = 10;
311 $project['view_state'] = 50;
314 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
317 $row = db_fetch_array($resIdProjetMantis);
318 if ($row == null || count($row)>2) {
319 echo "Erreur : impossible de retrouver le projet au sein de mantisbt";
321 $idMantisbt = $row['id_mantisbt'];
323 $clientSOAP = new SoapClient("http://".forge_get_config('server','mantisbt')."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
324 $update = $clientSOAP->__soapCall('mc_project_update', array("username" => forge_get_config('adminsoap_user','mantisbt'), "password" => forge_get_config('adminsoap_password','mantisbt'), "project_id" => $idMantisbt, "project" => $project));;
325 } catch (SoapFault $soapFault) {
326 echo $soapFault->faultstring;
329 echo "Error : update MantisBT projet";
334 function isProjetMantisCreated($idProjet){
336 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
339 $row = db_fetch_array($resIdProjetMantis);
348 function getIdProjetMantis($idProjet){
350 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
353 $row = db_fetch_array($resIdProjetMantis);
358 return $row['id_mantisbt'];
363 function updateUserInMantis($user_id) {
364 global $sys_mantisbt_host, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_db_port, $sys_mantisbt_db_name;
365 // recuperation du nouveau mail
366 $resUser = db_query_params ('SELECT user_name, email FROM users WHERE user_id = $1',array($user_id));
368 $row = db_fetch_array($resUser);
369 $dbConnection = db_connect_host($sys_mantisbt_db_name, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_host, $sys_mantisbt_db_port);
371 $errMantis1 = "Error : Could not open connection" . db_error($dbConnection);
373 db_rollback($dbConnection);
375 db_query_params('UPDATE mantis_user_table set email = $1 where username = $2',array($row['email'],$row['user_name']),'-1','0',$dbConnection);
380 function updateUsersProjetMantis($idProjet, $members) {
383 global $sys_mantisbt_host, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_db_port, $sys_mantisbt_db_name;
385 // recuperation de id mantis
386 $idMantis = getIdProjetMantis($idProjet);
388 // TODO corriger inclusion bug
390 $role['Manager'] = 70;
391 $role['Concepteur'] = 55;
392 $role['Collaborateur'] = 55;
393 $role['Rapporteur'] = 55;
397 $stateForge = array ();
398 foreach ($members as $key => $member){
399 $resUserRole = db_query_params('SELECT role.role_name
400 FROM role, user_group, users
401 WHERE users.user_name = $1
402 AND ( user_group.user_id = users.user_id AND user_group.group_id = $2 )
403 AND user_group.role_id = role.role_id',
404 array($member,$idProjet));
406 $row = db_fetch_array($resUserRole);
407 $stateForge[$member]['name'] = $member;
408 $stateForge[$member]['role'] = $row['role_name'];
410 // on supprime les precedentes relations dans mantis
411 $stateMantis = array ();
412 $dbConnection = db_connect_host($sys_mantisbt_db_name, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_host, $sys_mantisbt_db_port);
414 $errMantis1 = "Error : Could not open connection" . db_error($dbConnection);
416 db_rollback($dbConnection);
418 $result = pg_delete($dbConnection,"mantis_project_user_list_table",array("project_id"=>$idMantis));
420 echo "Error : Impossible de nettoyer les roles dans mantisbt";
422 foreach($stateForge as $member => $array){
424 // recuperation de l'id user dans mantis
425 $resultIdUser = db_query_params('SELECT mantis_user_table.id FROM mantis_user_table WHERE mantis_user_table.username = $1',
426 array($member),'-1',0,$dbConnection);
428 $rowIdUser = db_fetch_array($resultIdUser);
429 $idUser = $rowIdUser['id'];
430 // insertion de la relation
431 $resultInsert = pg_insert($dbConnection,
432 "mantis_project_user_list_table",
433 array ("project_id" => $idMantis, "user_id" => $idUser, "access_level" => $role[$array['role']])
435 if (!isset($resultInsert))
436 echo "Error : Impossible de mettre à jour les roles dans mantisbt";
443 function refreshHierarchyMantisBt(){
444 global $sys_mantisbt_host, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_db_port, $sys_mantisbt_db_name;
446 $hierarchies=db_query_params('SELECT project_id, sub_project_id FROM plugin_projects_hierarchy WHERE activated=true',array());
448 $dbConnection = db_connect_host($sys_mantisbt_db_name, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_host, $sys_mantisbt_db_port);
450 db_rollback($dbConnection);
454 db_begin($dbConnection);
455 db_query_params('TRUNCATE TABLE mantis_project_hierarchy_table',array() , '-1', 0, $dbConnection);
456 while ($hierarchy = db_fetch_array($hierarchies)) {
457 $result = db_query_params ('INSERT INTO mantis_project_hierarchy_table (child_id, parent_id, inherit_parent) VALUES ($1, $2, $3)',
458 array (getIdProjetMantis($hierarchy['sub_project_id']), getIdProjetMantis($hierarchy['project_id']), 1),
464 $this->setError(_('Insert Failed') . db_error($dbConnection));
470 db_commit($dbConnection);
471 pg_close($dbConnection);
475 function getGroupIdByName($name){
477 $child_query = db_query_params('select group_id from groups where group_name = $1',array($name));
479 $row = db_fetch_array($child_query);
484 return $row['group_id'];
490 // c-file-style: "bsd"