5 * Copyright 2009, Fabien Dubois - Capgemini
6 * Copyright 2009-2011, 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 class MantisBTPlugin extends Plugin {
34 function MantisBTPlugin() {
36 $this->name = "mantisbt" ;
37 $this->text = "MantisBT" ; // To show in the tabs, use...
38 $this->_addHook('user_personal_links'); //to make a link to the user's personal part of the plugin
39 $this->_addHook('usermenu');
40 $this->_addHook('groupmenu'); // To put into the project tabs
41 $this->_addHook('groupisactivecheckbox'); // The "use ..." checkbox in editgroupinfo
42 $this->_addHook('groupisactivecheckboxpost'); //
43 $this->_addHook('userisactivecheckbox'); // The "use ..." checkbox in user account
44 $this->_addHook('userisactivecheckboxpost'); //
45 $this->_addHook('project_admin_plugins'); // to show up in the admin page fro group
46 $this->_addHook('change_cal_permission');
47 $this->_addHook('change_cal_mail');
48 $this->_addHook('add_cal_link_father');
49 $this->_addHook('del_cal_link_father');
50 $this->_addHook('group_approved');
51 $this->_addHook('group_delete');
52 $this->_addHook('group_update');
55 function CallHook($hookname, &$params) {
56 global $G_SESSION, $HTML;
60 if ($G_SESSION->usesPlugin($this->name)) {
61 $param = '?type=user&user_id=' . $G_SESSION->getId() . '&pluginname=' . $this->name; // we indicate the part we're calling is the user one
62 echo $HTML->PrintSubMenu(array($this->text), array('/plugins/mantisbt/index.php' . $param), array(_('Personnal MantisBT page')));
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&group_id=' . $group_id . '&pluginname=' . $this->name;
77 if ($params['toptab'] == $this->name) {
78 $params['selected']=(count($params['TITLES'])-1);
83 case "user_personal_links": {
84 // 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
85 $userid = $params['user_id'];
86 $user = user_get_object($userid);
87 $text = $params['text'];
88 //check if the user has the plugin activated
89 if ($user->usesPlugin($this->name)) {
91 echo util_make_link("/plugins/mantisbt/?user_id=$userid&type=user&pluginname=".$this->name,
92 _('View Personal MantisBT')
99 case "project_admin_plugins": {
100 // this displays the link in the project admin options page to it's MantisBT administration
101 $group_id = $params['group_id'];
102 $group = group_get_object($group_id);
103 if ($group->usesPlugin($this->name)) {
105 echo util_make_link("/plugins/mantisbt/?group_id=$group_id&type=admin&pluginname=".$this->name,
106 _('View Admin MantisBT')
113 case "change_cal_permission": {
114 // mise a jour des utilisateurs avec les roles
115 $group_id = $params[1];
116 $group = group_get_object($group_id);
118 foreach($group->getMembers() as $member){
119 $members[] = $member->data_array['user_name'];
121 $this->updateUsersProjetMantis($group->data_array['group_id'],$members);
124 // mise a jour de l'adresse mail utilisateur
125 case "change_cal_mail": {
126 $user_id = $params[1];
127 // before activate this, please fix updateUserInMantis
128 //$this->updateUserInMantis($user_id);
132 case "add_cal_link_father":
133 case "del_cal_link_father": {
134 $sub_group_id = $params[0];
135 $group_id = $params[1];
136 // before activate this, please fix refreshHierarchyMantisBt
137 //$this->refreshHierarchyMantisBt();
141 case "group_delete": {
142 $group_id = $params['group_id'];
143 $group = group_get_object($group_id);
144 if ($group->usesPlugin($this->name)) {
145 if ($this->isProjectMantisCreated($group_id)) {
146 if ($this->removeProjectMantis($group_id)) {
157 case "group_update": {
158 $group_id = $params['group_id'];
159 $group_name =$params['group_name'];
160 $group_ispublic = $params['group_ispublic'];
161 $group = group_get_object($group_id);
162 if ($group->usesPlugin($this->name)) {
163 if ($this->isProjectMantisCreated($group_id)) {
164 if ($this->updateProjectMantis($group_id, $group_name, $group_ispublic)) {
180 * addProjectMantis - inject the Group into Mantisbt thru SOAP
182 * @param array Configuration Array (url, soap_user, soap_password, sync_roles, sync_users)
183 * @return bool success or not
185 function addProjectMantis($groupId, $confArr) {
186 $groupObject = group_get_object($groupId);
188 $project['name'] = $groupObject->getPublicName();
189 $project['status'] = "development";
191 if ($groupObject->isPublic()) {
192 $project['view_state'] = 10;
194 $project['view_state'] = 50;
197 $project['description'] = $groupObject->getDescription();
200 $clientSOAP = new SoapClient($confArr['url']."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
201 $idProjetMantis = $clientSOAP->__soapCall('mc_project_add', array("username" => $confArr['soap_user'], "password" => $confArr['soap_password'], "project" => $project));
202 } catch (SoapFault $soapFault) {
203 $groupObject->setError('addProjectMantis::Error: ' . $soapFault->faultstring);
206 if (!isset($idProjetMantis) || !is_int($idProjetMantis)){
207 $groupObject->setError('addProjectMantis::Error: ' . _('Unable to create project in Mantisbt'));
210 return $idProjetMantis;
213 function removeProjectMantis($idProjet) {
214 $resIdProjetMantis = db_query_params('SELECT id_mantisbt FROM plugin_mantisbt WHERE id_group = $1',
218 $row = db_fetch_array($resIdProjetMantis);
220 if ($row == null || count($row)>2) {
221 echo 'removeProjetMantis:: ' . _('No project found');
223 $idMantisbt = $row['id_mantisbt'];
225 $clientSOAP = new SoapClient(forge_get_config('server_url','mantisbt')."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
226 $delete = $clientSOAP->__soapCall('mc_project_delete', array("username" => forge_get_config('adminsoap_user','mantisbt'), "password" => forge_get_config('adminsoap_password','mantisbt'), "project_id" => $idMantisbt));
227 } catch (SoapFault $soapFault) {
228 echo $soapFault->faultstring;
230 if (!isset($delete)){
231 echo 'removeProjetMantis:: ' . _('No project found in MantisBT') . ' ' .$idProjet;
233 db_query_params('DELETE FROM plugin_mantisbt WHERE id_mantisbt = $1',
241 * updateProjectMantis - update the Group informations into Mantisbt
242 * @param int id of the Group
243 * @param string group name
244 * @param int public or private
245 * @return bool success or not
247 function updateProjectMantis($groupId, $groupName, $groupIspublic) {
248 $groupObject = group_get_object($groupId);
250 $project['name'] = $groupName;
251 $project['status'] = "development";
253 // should check the config on mantisbt side and not used hard coded values
254 if ($groupIspublic) {
255 $project['view_state'] = 10;
257 $project['view_state'] = 50;
261 $idMantisbt = getIdProjetMantis($groupId);
265 $clientSOAP = new SoapClient(forge_get_config('server_url','mantisbt')."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
266 $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));;
267 } catch (SoapFault $soapFault) {
268 $groupObject->setError('updateProjectMantis::Error' . ' '. $soapFault->faultstring);
271 if (!isset($update)) {
272 $groupObject->setError('updateProjectMantis::Error' . ' ' . _('Update MantisBT project'));
276 $groupObject->setError('updateProjectMantis::Error ' . _('ID MantisBT project not found'));
283 * isProjectMantisCreated - check if the Project is already created
285 * @param int the Group Id
286 * @return boolean created or not
288 function isProjectMantisCreated($idProjet){
290 $resIdProjetMantis = db_query_params('SELECT id_mantisbt FROM plugin_mantisbt WHERE id_group = $1',
292 if (!$resIdProjetMantis)
295 if (db_numrows($resIdProjetMantis) > 0) {
302 function updateUserInMantis($user_id) {
303 global $sys_mantisbt_host, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_db_port, $sys_mantisbt_db_name;
304 // recuperation du nouveau mail
305 $resUser = db_query_params('SELECT user_name, email FROM users WHERE user_id = $1',array($user_id));
307 $row = db_fetch_array($resUser);
309 // WONT WORK : db_connect_host is not in any fusionforge api
310 $dbConnection = db_connect_host($sys_mantisbt_db_name, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_host, $sys_mantisbt_db_port);
312 $errMantis1 = "Error : Could not open connection" . db_error($dbConnection);
314 db_rollback($dbConnection);
316 db_query_params('UPDATE mantis_user_table set email = $1 where username = $2',array($row['email'],$row['user_name']),'-1','0',$dbConnection);
322 * updateUsersProjectMantis - inject Username in mantisbt for specific project
324 * @param int Group Id
325 * @param array Unix username array
326 * @return boolean success or not
328 function updateUsersProjectMantis($groupId, $members) {
329 $groupObject = group_get_object($groupId);
333 // @TODO put that in config file ?
335 $role['Manager'] = 70;
336 $role['Concepteur'] = 55;
337 $role['Collaborateur'] = 55;
338 $role['Rapporteur'] = 55;
341 // @TODO : make a robust function there based on RBAC ?
342 $stateForge = array();
343 foreach ($members as $key => $member){
344 $resUserRole = db_query_params('SELECT role.role_name
345 FROM role, user_group, users
346 WHERE users.user_name = $1
347 AND ( user_group.user_id = users.user_id AND user_group.group_id = $2 )
348 AND user_group.role_id = role.role_id',
349 array($member, $groupObject->getID()));
351 $groupObject->setError('updateUsersProjectMantis::'. _('Error : Cannot retrieve information about role') . ' ' .db_error());
354 $row = db_fetch_array($resUserRole);
355 $stateForge[$member]['name'] = $member;
356 $stateForge[$member]['role'] = $row['role_name'];
360 if ($this->__getDBType() === "pgsql") {
361 if ($this->__updateUsersProjectMantisPgsql($groupObject->getID(), $stateForge)) {
370 function refreshHierarchyMantisBt(){
371 global $sys_mantisbt_host, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_db_port, $sys_mantisbt_db_name;
373 $hierarchies=db_query_params('SELECT project_id, sub_project_id FROM plugin_projects_hierarchy WHERE activated=true',array());
375 // WONT WORK : db_connect_host is not in any fusionforge api
376 $dbConnection = db_connect_host($sys_mantisbt_db_name, $sys_mantisbt_db_user, $sys_mantisbt_db_password, $sys_mantisbt_host, $sys_mantisbt_db_port);
378 db_rollback($dbConnection);
382 db_begin($dbConnection);
383 db_query_params('TRUNCATE TABLE mantis_project_hierarchy_table', array() , '-1', 0, $dbConnection);
384 while ($hierarchy = db_fetch_array($hierarchies)) {
385 $result = db_query_params ('INSERT INTO mantis_project_hierarchy_table (child_id, parent_id, inherit_parent) VALUES ($1, $2, $3)',
386 array (getIdProjetMantis($hierarchy['sub_project_id']), getIdProjetMantis($hierarchy['project_id']), 1),
392 $this->setError(_('Insert Failed') . db_error($dbConnection));
398 db_commit($dbConnection);
399 pg_close($dbConnection);
404 * __updateUsersProjectMantisPgsql - update Users for this project in PostgreSQL DB
406 * @param int this Group Id
407 * @param array the role of this forge
408 * @return boolean success or not
411 function __updateUsersProjectMantisPgsql($groupId, $stateForge) {
412 $groupObject = group_get_object($groupId);
414 // WONT WORK : db_connect_host is not in any fusionforge api
415 $dbConnection = db_connect_host(forge_get_config('db_name','mantisbt'), forge_get_config('db_user','mantisbt'), forge_get_config('db_password','mantisbt'), forge_get_config('db_host','mantisbt'), forge_get_config('db_port','mantisbt'));
417 $groupObject->setError('updateUsersProjectMantis::'. _('Error : Could not open connection') . db_error($dbConnection));
418 db_rollback($dbConnection);
420 $idMantis = getIdProjetMantis($groupId);
421 $result = pg_delete($dbConnection,"mantis_project_user_list_table",array("project_id"=>$idMantis));
423 echo 'updateUsersProjectMantis::Error '. _('Unable to clean roles in Mantisbt');
425 foreach($stateForge as $member => $array){
427 $resultIdUser = db_query_params('SELECT mantis_user_table.id FROM mantis_user_table WHERE mantis_user_table.username = $1',
428 array($member), '-1', 0, $dbConnection);
430 $rowIdUser = db_fetch_array($resultIdUser);
431 $idUser = $rowIdUser['id'];
433 $resultInsert = pg_insert($dbConnection,
434 "mantis_project_user_list_table",
435 array("project_id" => $idMantis, "user_id" => $idUser, "access_level" => $role[$array['role']])
437 if (!isset($resultInsert)) {
438 echo 'updateUsersProjectMantis::Error '. _('Unable to update roles in mantisbt');
449 * __getDBType - return the type of DB used for mantisbt
451 * @return string type of the DB
454 function __getDBType() {
455 switch (forge_get_config('db_name','mantisbt')) {
468 * getMantisBTConf - get the mantisbt configuration id for a specific group_id
470 * @return array the mantisbt configuration array
473 function getMantisBTConf() {
475 $group = group_get_object($group_id);
476 $mantisbtConfArray = array();
477 $resIdProjetMantis = db_query_params('SELECT * FROM plugin_mantisbt WHERE id_group = $1', array($group_id));
478 if (!$resIdProjetMantis) {
479 $group->setError('getMantisBTId::error '.db_error());
480 return $mantisbtConfArray;
483 $row = db_numrows($resIdProjetMantis);
485 if ($row == null || count($row) > 2) {
486 $mantisbtConfArray['id_mantisbt'] = 0;
487 return $mantisbtConfArray;
490 $row = db_fetch_array($resIdProjetMantis);
491 $mantisbtConfArray['id_mantisbt'] = $row['id_mantisbt'];
492 $mantisbtConfArray['url'] = $row['url'];
493 $mantisbtConfArray['soap_user'] = $row['soap_user'];
494 $mantisbtConfArray['soap_password'] = $row['soap_password'];
495 $mantisbtConfArray['sync_users'] = $row['sync_users'];
496 $mantisbtConfArray['sync_roles'] = $row['sync_roles'];
497 return $mantisbtConfArray;
501 * getInitDisplay - display the init page
502 * @return bool true only currently
504 function getInitDisplay() {
506 require_once $gfplugins.$this->name.'/view/init.php';
511 * getAdminView - display the admin page
512 * @return bool true only currently
514 function getAdminView() {
516 require_once $gfplugins.$this->name.'/www/admin/index.php';
521 * getSubMenu - display the submenu
523 * @param int enable tooltips : default NO
524 * @return bool true only currently
526 function getSubMenu($enable_tooltips = 0) {
530 $group = group_get_object($group_id);
532 $labelTitle = array();
533 $labelTitle[] = _('Roadmap');
534 $labelTitle[] = _('Tickets');
535 $labelPage = array();
536 $labelPage[] = "/plugins/mantisbt/?type=group&group_id=".$group_id."&pluginname=".$this->name."&view=roadmap";
537 $labelPage[] = "/plugins/mantisbt/?type=group&group_id=".$group_id."&pluginname=".$this->name;
538 $labelAttr = array();
539 if ($enable_tooltips) {
540 $labelAttr[] = array('title' => _('View the roadmap, per version tickets'), 'id' => 'roadmapView');
541 $labelAttr[] = array('title' => _('View all tickets.'), 'id' => 'ticketView');
543 $labelAttr[] = array();
544 $labelAttr[] = array();
546 $userperm = $group->getPermission($user);
547 if ( $userperm->isAdmin() ) {
548 $labelTitle[] = _('Administration');
549 $labelPage[] = "/plugins/mantisbt/?type=admin&group_id=".$group_id."&pluginname=".$this->name;
550 $labelTitle[] = _('Statistics');
551 $labelPage[] = "/plugins/mantisbt/?type=admin&group_id=".$group_id."&pluginname=".$this->name."&view=stat";
552 if ($enable_tooltips) {
553 $labelAttr[] = array('title' => _('Manage versions, categories and general configuration.'), 'id' => 'adminView');
554 $labelAttr[] = array('title' => _('View global statistics.'), 'id' => 'statView');
556 $labelAttr[] = array();
557 $labelAttr[] = array();
561 echo $HTML->subMenu($labelTitle, $labelPage, $labelAttr);
565 * getHeader - initialize header and js
566 * @param string type : user, project (aka group)
567 * @return bool success or not
569 function getHeader($type) {
571 $params['toptab'] = $this->name;
573 use_javascript('scripts/MantisBTController.js');
577 $params['group'] = $group_id;
578 $params['title'] = $this->name.' Project Plugin!';
579 $params['pagename'] = $this->name;
580 $params['sectionvals'] = array(group_getname($group_id));
581 site_project_header($params);
587 $params['user'] = $user_id;
588 site_user_header($params);
600 * initialize - initialize the mantisbt plugin
601 * create mantisbt project if needed
603 * @param int the group id
604 * @param array configuration array
605 * @return bool success or not
607 function initialize($group_id, $confArr) {
608 if ($confArr['mantisbtcreate']) {
609 $idProjectMantis = $this->addProjectMantis($group_id, $confArr);
611 $idProjectMantis = $this->getProjectMantisByName($group_id, $confArr);
613 if ($idProjectMantis) {
614 $result = db_query_params('insert into plugin_mantisbt (id_group, id_mantisbt, url, soap_user, soap_password, sync_roles, sync_users)
615 values ($1, $2, $3, $4, $5, $6, $7)',
619 $confArr['soap_user'],
620 $confArr['soap_password'],
621 $confArr['sync_roles'],
622 $confArr['sync_users']));
633 // c-file-style: "bsd"