5 * Copyright 2009, Fabien Dubois - Capgemini
6 * Copyright 2009-2010, Franck Villaume - Capgemini
7 * http://fusionforge.org
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
28 * @todo : need a massive cleanup
29 * deal correctly with password (might need direct db access ?)
30 * limit non SOAP call aka direct db access to mantisbt
33 require_once 'include/database-pgsql.php';
35 class MantisBTPlugin extends Plugin {
36 function MantisBTPlugin () {
38 $this->name = "mantisbt" ;
39 $this->text = "MantisBT" ; // To show in the tabs, use...
40 $this->hooks[] = "user_personal_links"; //to make a link to the user's personal part of the plugin
41 $this->hooks[] = "usermenu" ;
42 $this->hooks[] = "groupmenu" ; // To put into the project tabs
43 $this->hooks[] = "groupisactivecheckbox" ; // The "use ..." checkbox in editgroupinfo
44 $this->hooks[] = "groupisactivecheckboxpost" ; //
45 $this->hooks[] = "userisactivecheckbox" ; // The "use ..." checkbox in user account
46 $this->hooks[] = "userisactivecheckboxpost" ; //
47 $this->hooks[] = "project_admin_plugins"; // to show up in the admin page fro group
48 $this->hooks[] = "change_cal_permission";
49 $this->hooks[] = "change_cal_mail";
50 $this->hooks[] = "add_cal_link_father";
51 $this->hooks[] = "del_cal_link_father";
52 $this->hooks[] = "group_approved";
53 $this->hooks[] = "group_delete";
54 $this->hooks[] = "group_update";
57 function CallHook ($hookname, $params) {
58 global $use_mantisbtplugin,$G_SESSION,$HTML;
61 $text = $this->text; // this is what shows in the tab
62 if ($G_SESSION->usesPlugin($this->name)) {
63 $param = '?type=user&id=' . $G_SESSION->getId() . "&pluginname=" . $this->name; // we indicate the part we're calling is the user one
64 echo ' | ' . $HTML->PrintSubMenu (array ($text),
65 array ('/plugins/mantisbt/index.php' . $param ));
70 $group_id=$params['group'];
71 $project = group_get_object($group_id);
72 if (!$project || !is_object($project) || $project->isError() || !$project->isProject()) {
75 if ($project->usesPlugin($this->name)) {
76 $params['TITLES'][]=$this->text;
77 $params['DIRS'][]='/plugins/' . $this->name . '/?type=group&id=' . $group_id . "&pluginname=" . $this->name;
79 if ($params['toptab'] == $this->name) {
80 $params['selected']=(count($params['TITLES'])-1);
84 case "groupisactivecheckbox": {
85 //Check if the group is active
86 // this code creates the checkbox in the project edit public info page to activate/deactivate the plugin
87 $group_id = $params['group'];
88 $group = group_get_object($group_id);
91 echo ' <input type="CHECKBOX" name="use_mantisbtplugin" value="1" ';
92 // CHECKED OR UNCHECKED?
93 if ( $group->usesPlugin ( $this->name ) ) {
94 echo 'checked="checked"';
99 echo "<strong>Use ".$this->text." Plugin</strong>";
104 case "groupisactivecheckboxpost": {
105 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
106 $group_id = $params['group'];
107 $group = group_get_object($group_id);
108 $use_mantisbtplugin = getIntFromRequest('use_mantisbtplugin');
109 if ( $use_mantisbtplugin == "1" ) {
110 if (! $group->usesPlugin($this->name)) {
112 $group->setPluginUse($this->name, true);
114 // add project in mantis if does not exists
115 if (!isProjetMantisCreated($group->data_array['group_id'])){
116 addProjetMantis($group->data_array['group_id'],$group->data_array['group_name'],$group->data_array['is_public'], $group->data_array['short_description']);
118 // update users and roles in mantis
120 foreach($group->getMembers() as $member){
121 $members[] = $member->data_array['user_name'];
123 updateUsersProjetMantis($group->data_array['group_id'],$members);
125 } else if ( $use_mantisbtplugin == "0" ) {
126 $group->setPluginUse ( $this->name, false );
130 case "userisactivecheckbox": {
131 //check if user is active
132 // this code creates the checkbox in the user account maintenance page to activate/deactivate the plugin
133 $user = $params['user'];
136 echo ' <input type="CHECKBOX" name="use_mantisbtplugin" value="1" ';
137 // CHECKED OR UNCHECKED?
138 if ( $user->usesPlugin ( $this->name ) ) {
139 echo 'checked="CHECKED"';
141 echo "> Use ".$this->text." Plugin";
146 case "userisactivecheckboxpost": {
147 // this code actually activates/deactivates the plugin after the form was submitted in the user account manteinance page
148 $user = $params['user'];
149 $use_mantisbtplugin = getIntFromRequest('use_mantisbtplugin');
150 if ( $use_mantisbtplugin == 1 ) {
151 $user->setPluginUse ( $this->name );
153 $user->setPluginUse ( $this->name, false );
157 case "user_personal_links": {
158 // 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
159 $userid = $params['user_id'];
160 $user = user_get_object($userid);
161 $text = $params['text'];
162 //check if the user has the plugin activated
163 if ($user->usesPlugin($this->name)) {
165 echo util_make_link ("/plugins/mantisbt/index.php?id=$userid&type=user&pluginname=".$this->name,
166 _('View Personal MantisBT')
172 case "project_admin_plugins": {
173 // this displays the link in the project admin options page to it's MantisBT administration
174 $group_id = $params['group_id'];
175 $group = &group_get_object($group_id);
176 if ( $group->usesPlugin ( $this->name ) ) {
177 echo util_make_link ("/plugins/mantisbt/index.php?id=$group_id&type=admin&pluginname=".$this->name,
178 _('View Admin MantisBT')
184 case "group_approved": {
185 $group_id=$params['group_id'];
186 $group = &group_get_object($group_id);
187 if ($group->usesPlugin($this->name)) {
188 // ajout du projet mantis s'il n'existe pas
189 if (!isProjetMantisCreated($group->data_array['group_id'])){
190 addProjetMantis($group->data_array['group_id'],$group->data_array['group_name'],$group->data_array['is_public'], $group->data_array['short_description']);
192 // mise a jour des utilisateurs avec les roles
194 foreach($group->getMembers() as $member){
195 $members[] = $member->data_array['user_name'];
197 updateUsersProjetMantis($group->data_array['group_id'],$members);
201 case "change_cal_permission": {
202 // mise a jour des utilisateurs avec les roles
203 $group_id=$params[1];
204 $group = &group_get_object($group_id);
206 foreach($group->getMembers() as $member){
207 $members[] = $member->data_array['user_name'];
209 //updateUsersProjetMantis($group->data_array['group_id'],$members);
212 // mise a jour de l'adresse mail utilisateur
213 case "change_cal_mail": {
215 updateUserInMantis($user_id);
218 case "add_cal_link_father":
219 case "del_cal_link_father": {
220 $sub_group_id = $params[0];
221 $group_id = $params[1];
222 refreshHierarchyMantisBt();
225 case "group_delete": {
226 $group_id=$params['group_id'];
227 $group = &group_get_object($group_id);
228 if ($group->usesPlugin($this->name)) {
229 if (isProjetMantisCreated($group->data_array['group_id'])) {
230 removeProjetMantis($group->data_array['group_id']);
235 case "group_update": {
236 $group_id=$params['group_id'];
237 $group = &group_get_object($group_id);
238 if ($group->usesPlugin($this->name)) {
239 if (isProjetMantisCreated($group_id)) {
240 updateProjetMantis($group_id, $params['group_name'], $group->data_array['is_public'], $group->data_array['short_description']);
249 function addProjetMantis($idProjet, $nomProjet, $isPublic, $description) {
252 $project['name'] = $nomProjet;
253 $project['status'] = "development";
255 if ($isPublic == "1"){
256 $project['view_state'] = 10;
258 $project['view_state'] = 50;
261 $project['description'] = $description;
264 $clientSOAP = new SoapClient("http://".forge_get_config('server','mantisbt')."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
265 $idProjetMantis = $clientSOAP->__soapCall('mc_project_add', array("username" => forge_get_config('adminsoap_user', 'mantisbt'), "password" => forge_get_config('adminsoap_password', 'mantisbt'), "project" => $project));
266 } catch (SoapFault $soapFault) {
267 echo $soapFault->faultstring;
270 if (!isset($idProjetMantis) || !is_int($idProjetMantis)){
271 echo "Error : Impossible de créer le projet dans mantis";
274 db_query_params('INSERT INTO group_mantisbt (id_group, id_mantisbt) VALUES ($1,$2)',
275 array($idProjet, $idProjetMantis));
280 function removeProjetMantis($idProjet) {
282 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
286 $row = db_fetch_array($resIdProjetMantis);
288 if ($row == null || count($row)>2) {
289 echo 'removeProjetMantis:: ' . _('No project found');
291 $idMantisbt = $row['id_mantisbt'];
293 $clientSOAP = new SoapClient("http://".forge_get_config('server','mantisbt')."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
294 $delete = $clientSOAP->__soapCall('mc_project_delete', array("username" => forge_get_config('adminsoap_user','mantisbt'), "password" => forge_get_config('adminsoap_password','mantisbt'), "project_id" => $idMantisbt));
295 } catch (SoapFault $soapFault) {
296 echo $soapFault->faultstring;
298 if (!isset($delete)){
299 echo 'removeProjetMantis:: ' . _('No project found in MantisBT') . ' ' .$idProjet;
301 db_query_params('DELETE FROM group_mantisbt WHERE group_mantisbt.id_mantisbt = $1',
308 function updateProjetMantis($idProjet,$nomProjet,$isPublic, $description) {
311 $project['name'] = $nomProjet;
312 $project['status'] = "development";
314 if ($isPublic == "1"){
315 $project['view_state'] = 10;
317 $project['view_state'] = 50;
320 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
323 $row = db_fetch_array($resIdProjetMantis);
324 if ($row == null || count($row)>2) {
325 echo 'updateProjetMantis:: ' . _('No project found');
327 $idMantisbt = $row['id_mantisbt'];
329 $clientSOAP = new SoapClient("http://".forge_get_config('server','mantisbt')."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
330 $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));;
331 } catch (SoapFault $soapFault) {
332 echo $soapFault->faultstring;
335 echo 'updateProjetMantis::Error ' . _('Update MantisBT project');
339 function isProjetMantisCreated($idProjet){
341 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
344 $row = db_fetch_array($resIdProjetMantis);
353 function getIdProjetMantis($idProjet){
355 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
358 $row = db_fetch_array($resIdProjetMantis);
363 return $row['id_mantisbt'];
368 function updateUserInMantis($user_id) {
369 global $sys_mantisbt_host, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_db_port, $sys_mantisbt_db_name;
370 // recuperation du nouveau mail
371 $resUser = db_query_params ('SELECT user_name, email FROM users WHERE user_id = $1',array($user_id));
373 $row = db_fetch_array($resUser);
374 $dbConnection = db_connect_host($sys_mantisbt_db_name, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_host, $sys_mantisbt_db_port);
376 $errMantis1 = "Error : Could not open connection" . db_error($dbConnection);
378 db_rollback($dbConnection);
380 db_query_params('UPDATE mantis_user_table set email = $1 where username = $2',array($row['email'],$row['user_name']),'-1','0',$dbConnection);
385 function updateUsersProjetMantis($idProjet, $members) {
388 global $sys_mantisbt_host, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_db_port, $sys_mantisbt_db_name;
390 // recuperation de id mantis
391 $idMantis = getIdProjetMantis($idProjet);
393 // TODO corriger inclusion bug
395 $role['Manager'] = 70;
396 $role['Concepteur'] = 55;
397 $role['Collaborateur'] = 55;
398 $role['Rapporteur'] = 55;
402 $stateForge = array ();
403 foreach ($members as $key => $member){
404 $resUserRole = db_query_params('SELECT role.role_name
405 FROM role, user_group, users
406 WHERE users.user_name = $1
407 AND ( user_group.user_id = users.user_id AND user_group.group_id = $2 )
408 AND user_group.role_id = role.role_id',
409 array($member, $idProjet));
411 $row = db_fetch_array($resUserRole);
412 $stateForge[$member]['name'] = $member;
413 $stateForge[$member]['role'] = $row['role_name'];
415 // on supprime les precedentes relations dans mantis
416 $stateMantis = array ();
417 $dbConnection = db_connect_host($sys_mantisbt_db_name, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_host, $sys_mantisbt_db_port);
419 $errMantis1 = _('Error : Could not open connection') . db_error($dbConnection);
421 db_rollback($dbConnection);
423 $result = pg_delete($dbConnection,"mantis_project_user_list_table",array("project_id"=>$idMantis));
425 echo 'updateUsersProjetMantis::Error '. _('Unable to clean roles in Mantisbt');
427 foreach($stateForge as $member => $array){
429 // recuperation de l'id user dans mantis
430 $resultIdUser = db_query_params('SELECT mantis_user_table.id FROM mantis_user_table WHERE mantis_user_table.username = $1',
431 array($member), '-1', 0, $dbConnection);
433 $rowIdUser = db_fetch_array($resultIdUser);
434 $idUser = $rowIdUser['id'];
435 // insertion de la relation
436 $resultInsert = pg_insert($dbConnection,
437 "mantis_project_user_list_table",
438 array("project_id" => $idMantis, "user_id" => $idUser, "access_level" => $role[$array['role']])
440 if (!isset($resultInsert))
441 echo 'updateUsersProjetMantis::Error '. _('Unable to update roles in mantisbt');
448 function refreshHierarchyMantisBt(){
449 global $sys_mantisbt_host, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_db_port, $sys_mantisbt_db_name;
451 $hierarchies=db_query_params('SELECT project_id, sub_project_id FROM plugin_projects_hierarchy WHERE activated=true',array());
453 $dbConnection = db_connect_host($sys_mantisbt_db_name, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_host, $sys_mantisbt_db_port);
455 db_rollback($dbConnection);
459 db_begin($dbConnection);
460 db_query_params('TRUNCATE TABLE mantis_project_hierarchy_table', array() , '-1', 0, $dbConnection);
461 while ($hierarchy = db_fetch_array($hierarchies)) {
462 $result = db_query_params ('INSERT INTO mantis_project_hierarchy_table (child_id, parent_id, inherit_parent) VALUES ($1, $2, $3)',
463 array (getIdProjetMantis($hierarchy['sub_project_id']), getIdProjetMantis($hierarchy['project_id']), 1),
469 $this->setError(_('Insert Failed') . db_error($dbConnection));
475 db_commit($dbConnection);
476 pg_close($dbConnection);
480 function getGroupIdByName($name){
482 $child_query = db_query_params('select group_id from groups where group_name = $1', array($name));
484 $row = db_fetch_array($child_query);
489 return $row['group_id'];
495 // c-file-style: "bsd"