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
27 * @todo : need a massive cleanup
28 * deal correctly with password (might need direct db access ?)
29 * limit non SOAP call aka direct db access to mantisbt
32 require_once 'include/database-pgsql.php';
34 class MantisBTPlugin extends Plugin {
35 function MantisBTPlugin () {
37 $this->name = "mantisbt" ;
38 $this->text = "MantisBT" ; // To show in the tabs, use...
39 $this->hooks[] = "user_personal_links"; //to make a link to the user's personal part of the plugin
40 $this->hooks[] = "usermenu" ;
41 $this->hooks[] = "groupmenu" ; // To put into the project tabs
42 $this->hooks[] = "groupisactivecheckbox" ; // The "use ..." checkbox in editgroupinfo
43 $this->hooks[] = "groupisactivecheckboxpost" ; //
44 $this->hooks[] = "userisactivecheckbox" ; // The "use ..." checkbox in user account
45 $this->hooks[] = "userisactivecheckboxpost" ; //
46 $this->hooks[] = "project_admin_plugins"; // to show up in the admin page fro group
47 $this->hooks[] = "change_cal_permission";
48 $this->hooks[] = "change_cal_mail";
49 $this->hooks[] = "add_cal_link_father";
50 $this->hooks[] = "del_cal_link_father";
51 $this->hooks[] = "group_approved";
52 $this->hooks[] = "group_delete";
53 $this->hooks[] = "group_update";
56 function CallHook ($hookname, $params) {
57 global $G_SESSION, $HTML;
60 $text = $this->text; // this is what shows in the tab
61 if ($G_SESSION->usesPlugin($this->name)) {
62 $param = '?type=user&id=' . $G_SESSION->getId() . "&pluginname=" . $this->name; // we indicate the part we're calling is the user one
63 echo ' | ' . $HTML->PrintSubMenu (array ($text), array ('/plugins/mantisbt/index.php' . $param ));
68 $group_id=$params['group'];
69 $project = group_get_object($group_id);
70 if (!$project || !is_object($project) || $project->isError() || !$project->isProject()) {
73 if ($project->usesPlugin($this->name)) {
74 $params['TITLES'][]=$this->text;
75 $params['DIRS'][]='/plugins/' . $this->name . '/?type=group&id=' . $group_id . "&pluginname=" . $this->name;
77 if ($params['toptab'] == $this->name) {
78 $params['selected']=(count($params['TITLES'])-1);
82 case "groupisactivecheckboxpost": {
83 // update users and roles in mantis
85 foreach($group->getMembers() as $member){
86 $members[] = $member->data_array['user_name'];
88 $this->updateUsersProjectMantis($group->data_array['group_id'],$members);
91 case "user_personal_links": {
92 // 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
93 $userid = $params['user_id'];
94 $user = user_get_object($userid);
95 $text = $params['text'];
96 //check if the user has the plugin activated
97 if ($user->usesPlugin($this->name)) {
99 echo util_make_link ("/plugins/mantisbt/index.php?id=$userid&type=user&pluginname=".$this->name,
100 _('View Personal MantisBT')
106 case "project_admin_plugins": {
107 // this displays the link in the project admin options page to it's MantisBT administration
108 $group_id = $params['group_id'];
109 $group = group_get_object($group_id);
110 if ( $group->usesPlugin ( $this->name ) ) {
111 echo util_make_link ("/plugins/mantisbt/index.php?id=$group_id&type=admin&pluginname=".$this->name,
112 _('View Admin MantisBT')
118 case "group_approved": {
119 $group_id=$params['group_id'];
120 $group = group_get_object($group_id);
121 if ($group->usesPlugin($this->name)) {
122 // ajout du projet mantis s'il n'existe pas
123 if (!$this->isProjetMantisCreated($group->data_array['group_id'])){
124 $this->addProjetMantis($group->data_array['group_id'],$group->data_array['group_name'],$group->data_array['is_public'], $group->data_array['short_description']);
126 // mise a jour des utilisateurs avec les roles
128 foreach($group->getMembers() as $member){
129 $members[] = $member->data_array['user_name'];
131 $this->updateUsersProjetMantis($group->data_array['group_id'],$members);
135 case "change_cal_permission": {
136 // mise a jour des utilisateurs avec les roles
137 $group_id=$params[1];
138 $group = group_get_object($group_id);
140 foreach($group->getMembers() as $member){
141 $members[] = $member->data_array['user_name'];
143 $this->updateUsersProjetMantis($group->data_array['group_id'],$members);
146 // mise a jour de l'adresse mail utilisateur
147 case "change_cal_mail": {
149 $this->updateUserInMantis($user_id);
152 case "add_cal_link_father":
153 case "del_cal_link_father": {
154 $sub_group_id = $params[0];
155 $group_id = $params[1];
156 $this->refreshHierarchyMantisBt();
159 case "group_delete": {
160 $group_id=$params['group_id'];
161 $group = group_get_object($group_id);
162 if ($group->usesPlugin($this->name)) {
163 if ($this->isProjectMantisCreated($group->data_array['group_id'])) {
164 $this->removeProjectMantis($group->data_array['group_id']);
169 case "group_update": {
170 $group_id=$params['group_id'];
171 $group = group_get_object($group_id);
172 if ($group->usesPlugin($this->name)) {
173 if ($this->isProjectMantisCreated($group_id)) {
174 $this->updateProjectMantis($group_id, $params['group_name'], $group->data_array['is_public'], $group->data_array['short_description']);
182 function groupisactivecheckboxpost(&$params) {
183 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
184 $group = group_get_object($params['group']);
185 $flag = strtolower('use_'.$this->name);
186 if ( getStringFromRequest($flag) == 1 ) {
187 if (!$this->isProjectMantisCreated($group->data_array['group_id'])){
188 if($this->addProjectMantis($group)) {
189 $group->setPluginUse($this->name);
193 $group->setPluginUse($this->name, false);
198 * @param object The Group
199 * @return bool success or not
201 function addProjectMantis(&$groupObject) {
204 $project['name'] = $groupObject->getPublicName();
205 $project['status'] = "development";
207 if ($groupObject->isPublic()) {
208 $project['view_state'] = 10;
210 $project['view_state'] = 50;
213 $project['description'] = $groupObject->getDescription();
216 $clientSOAP = new SoapClient(forge_get_config('server_url','mantisbt')."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
217 $idProjetMantis = $clientSOAP->__soapCall('mc_project_add', array("username" => forge_get_config('adminsoap_user', 'mantisbt'), "password" => forge_get_config('adminsoap_passwd', 'mantisbt'), "project" => $project));
218 } catch (SoapFault $soapFault) {
219 $groupObject->setError('addProjectMantis::Error: ' . $soapFault->faultstring);
222 if (!isset($idProjetMantis) || !is_int($idProjetMantis)){
223 $groupObject->setError('addProjectMantis::Error: ' . _('Unable to create project in Mantisbt'));
226 $res = db_query_params('INSERT INTO group_mantisbt (id_group, id_mantisbt) VALUES ($1,$2)',
227 array($groupObject->getID(), $idProjetMantis));
229 $groupObject->setError('addProjectMantis::Error: ' . _('db_error') . ' ' .db_error());
236 function removeProjectMantis($idProjet) {
238 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
242 $row = db_fetch_array($resIdProjetMantis);
244 if ($row == null || count($row)>2) {
245 echo 'removeProjetMantis:: ' . _('No project found');
247 $idMantisbt = $row['id_mantisbt'];
249 $clientSOAP = new SoapClient(forge_get_config('server_url','mantisbt')."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
250 $delete = $clientSOAP->__soapCall('mc_project_delete', array("username" => forge_get_config('adminsoap_user','mantisbt'), "password" => forge_get_config('adminsoap_password','mantisbt'), "project_id" => $idMantisbt));
251 } catch (SoapFault $soapFault) {
252 echo $soapFault->faultstring;
254 if (!isset($delete)){
255 echo 'removeProjetMantis:: ' . _('No project found in MantisBT') . ' ' .$idProjet;
257 db_query_params('DELETE FROM group_mantisbt WHERE group_mantisbt.id_mantisbt = $1',
264 function updateProjectMantis($idProjet, $nomProjet, $isPublic, $description) {
267 $project['name'] = $nomProjet;
268 $project['status'] = "development";
270 if ($isPublic == "1"){
271 $project['view_state'] = 10;
273 $project['view_state'] = 50;
276 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
279 $row = db_fetch_array($resIdProjetMantis);
280 if ($row == null || count($row)>2) {
281 echo 'updateProjectMantis:: ' . _('No project found');
283 $idMantisbt = $row['id_mantisbt'];
285 $clientSOAP = new SoapClient(forge_get_config('server_url','mantisbt')."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
286 $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));;
287 } catch (SoapFault $soapFault) {
288 echo $soapFault->faultstring;
291 echo 'updateProjectMantis::Error ' . _('Update MantisBT project');
295 function isProjectMantisCreated($idProjet){
297 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
299 if (!$resIdProjetMantis)
302 if (db_numrows($resIdProjetMantis) > 0) {
309 function updateUserInMantis($user_id) {
310 global $sys_mantisbt_host, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_db_port, $sys_mantisbt_db_name;
311 // recuperation du nouveau mail
312 $resUser = db_query_params ('SELECT user_name, email FROM users WHERE user_id = $1',array($user_id));
314 $row = db_fetch_array($resUser);
315 $dbConnection = db_connect_host($sys_mantisbt_db_name, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_host, $sys_mantisbt_db_port);
317 $errMantis1 = "Error : Could not open connection" . db_error($dbConnection);
319 db_rollback($dbConnection);
321 db_query_params('UPDATE mantis_user_table set email = $1 where username = $2',array($row['email'],$row['user_name']),'-1','0',$dbConnection);
326 function updateUsersProjectMantis($idProjet, $members) {
329 global $sys_mantisbt_host, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_db_port, $sys_mantisbt_db_name;
331 // recuperation de id mantis
332 $idMantis = getIdProjetMantis($idProjet);
334 // TODO corriger inclusion bug
336 $role['Manager'] = 70;
337 $role['Concepteur'] = 55;
338 $role['Collaborateur'] = 55;
339 $role['Rapporteur'] = 55;
343 $stateForge = array ();
344 foreach ($members as $key => $member){
345 $resUserRole = db_query_params('SELECT role.role_name
346 FROM role, user_group, users
347 WHERE users.user_name = $1
348 AND ( user_group.user_id = users.user_id AND user_group.group_id = $2 )
349 AND user_group.role_id = role.role_id',
350 array($member, $idProjet));
352 $row = db_fetch_array($resUserRole);
353 $stateForge[$member]['name'] = $member;
354 $stateForge[$member]['role'] = $row['role_name'];
356 // on supprime les precedentes relations dans mantis
357 $stateMantis = array ();
358 $dbConnection = db_connect_host($sys_mantisbt_db_name, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_host, $sys_mantisbt_db_port);
360 $errMantis1 = _('Error : Could not open connection') . db_error($dbConnection);
362 db_rollback($dbConnection);
364 $result = pg_delete($dbConnection,"mantis_project_user_list_table",array("project_id"=>$idMantis));
366 echo 'updateUsersProjectMantis::Error '. _('Unable to clean roles in Mantisbt');
368 foreach($stateForge as $member => $array){
370 // recuperation de l'id user dans mantis
371 $resultIdUser = db_query_params('SELECT mantis_user_table.id FROM mantis_user_table WHERE mantis_user_table.username = $1',
372 array($member), '-1', 0, $dbConnection);
374 $rowIdUser = db_fetch_array($resultIdUser);
375 $idUser = $rowIdUser['id'];
376 // insertion de la relation
377 $resultInsert = pg_insert($dbConnection,
378 "mantis_project_user_list_table",
379 array("project_id" => $idMantis, "user_id" => $idUser, "access_level" => $role[$array['role']])
381 if (!isset($resultInsert))
382 echo 'updateUsersProjectMantis::Error '. _('Unable to update roles in mantisbt');
389 function refreshHierarchyMantisBt(){
390 global $sys_mantisbt_host, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_db_port, $sys_mantisbt_db_name;
392 $hierarchies=db_query_params('SELECT project_id, sub_project_id FROM plugin_projects_hierarchy WHERE activated=true',array());
394 $dbConnection = db_connect_host($sys_mantisbt_db_name, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_host, $sys_mantisbt_db_port);
396 db_rollback($dbConnection);
400 db_begin($dbConnection);
401 db_query_params('TRUNCATE TABLE mantis_project_hierarchy_table', array() , '-1', 0, $dbConnection);
402 while ($hierarchy = db_fetch_array($hierarchies)) {
403 $result = db_query_params ('INSERT INTO mantis_project_hierarchy_table (child_id, parent_id, inherit_parent) VALUES ($1, $2, $3)',
404 array (getIdProjetMantis($hierarchy['sub_project_id']), getIdProjetMantis($hierarchy['project_id']), 1),
410 $this->setError(_('Insert Failed') . db_error($dbConnection));
416 db_commit($dbConnection);
417 pg_close($dbConnection);
423 function getIdProjetMantis($idProjet){
425 $resIdProjetMantis = db_query_params('SELECT group_mantisbt.id_mantisbt FROM group_mantisbt WHERE group_mantisbt.id_group = $1',
428 $row = db_fetch_array($resIdProjetMantis);
432 return $row['id_mantisbt'];
438 // c-file-style: "bsd"