5 * Copyright 2009, Fabien Dubois - Capgemini
6 * Copyright 2009-2011, Franck Villaume - Capgemini
7 * Copyright 2011, Franck Villaume - TrivialDev
8 * http://fusionforge.org
10 * This file is part of FusionForge.
12 * FusionForge is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * FusionForge is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 class MantisBTPlugin extends Plugin {
29 function MantisBTPlugin() {
31 $this->name = "mantisbt" ;
32 $this->text = "MantisBT" ; // To show in the tabs, use...
33 $this->_addHook('user_personal_links'); //to make a link to the user's personal part of the plugin
34 $this->_addHook('usermenu');
35 $this->_addHook('groupmenu'); // To put into the project tabs
36 $this->_addHook('groupisactivecheckbox'); // The "use ..." checkbox in editgroupinfo
37 $this->_addHook('groupisactivecheckboxpost'); //
38 $this->_addHook('userisactivecheckbox'); // The "use ..." checkbox in user account
39 $this->_addHook('userisactivecheckboxpost'); //
40 $this->_addHook('project_admin_plugins'); // to show up in the admin page fro group
41 $this->_addHook('group_delete');
42 $this->_addHook('group_update');
43 $this->_addHook('site_admin_option_hook');
46 function CallHook($hookname, &$params) {
47 global $G_SESSION, $HTML;
51 if ($G_SESSION->usesPlugin($this->name)) {
52 $param = '?type=user&user_id=' . $G_SESSION->getId() . '&pluginname=' . $this->name; // we indicate the part we're calling is the user one
53 echo $HTML->PrintSubMenu(array($this->text), array('/plugins/mantisbt/index.php' . $param), array(array('title' => _('Personal MantisBT page'))));
59 $group_id = $params['group'];
60 $project = group_get_object($group_id);
61 if (!$project || !is_object($project) || $project->isError() || !$project->isProject()) {
64 if ($project->usesPlugin($this->name)) {
65 $params['TITLES'][] = $this->text;
66 $params['DIRS'][] = '/plugins/'.$this->name.'/?type=group&group_id=' . $group_id . '&pluginname=' . $this->name;
67 $params['TOOLTIPS'][] = _('Tickets Management');
68 if (session_loggedin()) {
69 $user = session_get_user();
70 $userperm = $project->getPermission($user);
71 if ($userperm->isAdmin()) {
72 $params['TITLES'][] = $this->text . _('Admin');
73 $params['ADMIN'][] = '/plugins/'.$this->name.'/?type=admin&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)) {
92 if ($user->usesTooltips()) {
93 $arr_t[] = array('title' => _('Manage your mantisbt account and follow your tickets'));
97 echo util_make_link('/plugins/'.$this->name.'/?user_id=$userid&type=user&pluginname='.$this->name, _('View Personal MantisBT'), $arr_t);
103 case "project_admin_plugins": {
104 // this displays the link in the project admin options page to it's MantisBT administration
105 $group_id = $params['group_id'];
106 $group = group_get_object($group_id);
107 if ($group->usesPlugin($this->name)) {
109 echo util_make_link("/plugins/mantisbt/?group_id=$group_id&type=admin&pluginname=".$this->name, _('View Admin MantisBT')
116 case "site_admin_option_hook": {
117 echo '<li>'.util_make_link("/plugins/mantisbt/?type=globaladmin&pluginname=".$this->name,_('Global MantisBT admin')).'</li>';
121 case "group_delete": {
122 $group_id = $params['group_id'];
123 $group = group_get_object($group_id);
124 if ($group->usesPlugin($this->name)) {
125 if ($this->isProjectMantisCreated($group_id)) {
126 if ($this->removeProjectMantis($group_id)) {
137 case "group_update": {
138 $group_id = $params['group_id'];
139 $group_name =$params['group_name'];
140 $group_ispublic = $params['group_ispublic'];
141 $group = group_get_object($group_id);
142 if ($group->usesPlugin($this->name)) {
143 if ($this->isProjectMantisCreated($group_id)) {
144 if ($this->updateProjectMantis($group_id, $group_name, $group_ispublic)) {
160 * addProjectMantis - inject the Group into Mantisbt thru SOAP
162 * @param array Configuration Array (url, soap_user, soap_password, sync_roles)
163 * @return bool success or not
165 function addProjectMantis($groupId, $confArr) {
166 $groupObject = group_get_object($groupId);
168 $project['name'] = $groupObject->getPublicName();
169 $project['status'] = "development";
171 if ($groupObject->isPublic()) {
172 $project['view_state'] = 10;
174 $project['view_state'] = 50;
177 $project['description'] = $groupObject->getDescription();
180 $clientSOAP = new SoapClient($confArr['url']."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
181 $idProjetMantis = $clientSOAP->__soapCall('mc_project_add', array("username" => $confArr['soap_user'], "password" => $confArr['soap_password'], "project" => $project));
182 } catch (SoapFault $soapFault) {
183 $groupObject->setError('addProjectMantis::Error: ' . $soapFault->faultstring);
186 if (!isset($idProjetMantis) || !is_int($idProjetMantis)){
187 $groupObject->setError('addProjectMantis::Error: ' . _('Unable to create project in Mantisbt'));
190 return $idProjetMantis;
193 function removeProjectMantis($idProjet) {
194 $groupObject = group_get_object($idProjet);
195 $localMantisbtConf = $this->getMantisBTConf($groupObject->getID());
197 if (!$localMantisbtConf) {
198 $groupObject->setError('removeProjetMantis::Error' . ' '. _('No project found'));
202 $clientSOAP = new SoapClient($localMantisbtConf['url']."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
203 $delete = $clientSOAP->__soapCall('mc_project_delete', array("username" => $localMantisbtConf['soap_user'], "password" => $localMantisbtConf['soap_password'], "project_id" => $localMantisbtConf['id_mantisbt']));
204 } catch (SoapFault $soapFault) {
205 $groupObject->setError('removeProjetMantis::Error' . ' '.$soapFault->faultstring);
208 if (!isset($delete)) {
209 $groupObject->setError('removeProjetMantis:: ' . _('No project found in MantisBT') . ' ' .$localMantisbtConf['id_mantisbt']);
212 $res = db_query_params('DELETE FROM plugin_mantisbt WHERE id_mantisbt = $1',
213 array($localMantisbtConf['id_mantisbt']));
215 $groupObject->setError('removeProjetMantis:: ' . _('Cannot delete in database') . ' ' .$localMantisbtConf['id_mantisbt']);
224 * updateProjectMantis - update the Group informations into Mantisbt
225 * @param int id of the Group
226 * @param string group name
227 * @param int public or private
228 * @return bool success or not
230 function updateProjectMantis($groupId, $groupName, $groupIspublic) {
231 $groupObject = group_get_object($groupId);
233 $localMantisbtConf = $this->getMantisBTConf($groupObject->getID());
234 $project['name'] = $groupName;
235 $project['status'] = "development";
237 // should check the config on mantisbt side and not used hard coded values
238 if ($groupIspublic) {
239 $project['view_state'] = 10;
241 $project['view_state'] = 50;
244 if ($localMantisbtConf['id_mantisbt'] != 0) {
246 $clientSOAP = new SoapClient($localMantisbtConf['url']."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
247 $update = $clientSOAP->__soapCall('mc_project_update', array("username" => $localMantisbtConf['soap_user'], "password" => $localMantisbtConf['soap_password'], "project_id" => $localMantisbtConf['id_mantisbt'], "project" => $project));
248 } catch (SoapFault $soapFault) {
249 $groupObject->setError('updateProjectMantis::Error' . ' '. $soapFault->faultstring);
252 if (!isset($update)) {
253 $groupObject->setError('updateProjectMantis::Error' . ' ' . _('Update MantisBT project'));
257 $groupObject->setError('updateProjectMantis::Error ' . _('ID MantisBT project not found'));
264 * isProjectMantisCreated - check if the Project is already created
266 * @param int the Group Id
267 * @return boolean created or not
269 function isProjectMantisCreated($idProjet){
270 $resIdProjetMantis = db_query_params('SELECT id_mantisbt FROM plugin_mantisbt WHERE id_group = $1',
272 if (!$resIdProjetMantis)
275 if (db_numrows($resIdProjetMantis) > 0) {
283 * getMantisBTConf - get the mantisbt configuration id for a specific group_id
285 * @param integer the group_id
286 * @return array the mantisbt configuration array
289 function getMantisBTConf($group_id) {
290 $group = group_get_object($group_id);
291 $mantisbtConfArray = array();
292 $resIdProjetMantis = db_query_params('SELECT * FROM plugin_mantisbt WHERE id_group = $1', array($group_id));
293 if (!$resIdProjetMantis) {
294 $group->setError('getMantisBTId::error '.db_error());
295 return $mantisbtConfArray;
298 $row = db_numrows($resIdProjetMantis);
300 if ($row == null || count($row) > 2) {
301 $mantisbtConfArray['id_mantisbt'] = 0;
302 return $mantisbtConfArray;
305 $row = db_fetch_array($resIdProjetMantis);
306 $mantisbtConfArray['id_mantisbt'] = $row['id_mantisbt'];
307 $mantisbtConfArray['sync_roles'] = $row['sync_roles'];
308 $mantisbtConfArray['use_global'] = $row['use_global'];
309 if ($mantisbtConfArray['use_global']) {
310 $mantisbtGlobConf = $this->getGlobalconf();
311 $mantisbtConfArray['url'] = $mantisbtGlobConf['url'];
312 $mantisbtConfArray['soap_user'] = $mantisbtGlobConf['soap_user'];
313 $mantisbtConfArray['soap_password'] = $mantisbtGlobConf['soap_password'];
315 $mantisbtConfArray['url'] = $row['url'];
316 $mantisbtConfArray['soap_user'] = $row['soap_user'];
317 $mantisbtConfArray['soap_password'] = $row['soap_password'];
320 return $mantisbtConfArray;
324 * getInitDisplay - display the init page
325 * @return bool true only currently
327 function getInitDisplay() {
329 require_once $gfplugins.$this->name.'/view/init.php';
334 * getAdminView - display the admin page
335 * @return bool true only currently
337 function getAdminView() {
339 require_once $gfplugins.$this->name.'/www/admin/index.php';
344 * getSubMenu - display the submenu
346 * @param int enable tooltips : default NO
347 * @return bool true only currently
349 function getSubMenu($enable_tooltips = 0) {
353 $group = group_get_object($group_id);
355 $labelTitle = array();
356 $labelTitle[] = _('Roadmap');
357 $labelTitle[] = _('Tickets');
358 $labelPage = array();
359 $labelPage[] = "/plugins/".$this->name."/?type=group&group_id=".$group_id."&pluginname=".$this->name."&view=roadmap";
360 $labelPage[] = "/plugins/".$this->name."/?type=group&group_id=".$group_id."&pluginname=".$this->name;
361 $labelAttr = array();
362 if ($enable_tooltips) {
363 $labelAttr[] = array('title' => _('View the roadmap, per version tickets'), 'id' => 'roadmapView');
364 $labelAttr[] = array('title' => _('View all tickets.'), 'id' => 'ticketView');
366 $labelAttr[] = array();
367 $labelAttr[] = array();
369 $userperm = $group->getPermission($user);
370 if ( $userperm->isAdmin() ) {
371 $labelTitle[] = _('Administration');
372 $labelPage[] = "/plugins/".$this->name."/?type=admin&group_id=".$group_id."&pluginname=".$this->name;
373 $labelTitle[] = _('Statistics');
374 $labelPage[] = "/plugins/".$this->name."/?type=admin&group_id=".$group_id."&pluginname=".$this->name."&view=stat";
375 if ($enable_tooltips) {
376 $labelAttr[] = array('title' => _('Manage versions, categories and general configuration.'), 'id' => 'adminView');
377 $labelAttr[] = array('title' => _('View global statistics.'), 'id' => 'statView');
379 $labelAttr[] = array();
380 $labelAttr[] = array();
384 echo $HTML->subMenu($labelTitle, $labelPage, $labelAttr);
388 * getHeader - initialize header and js
389 * @param string type : user, project (aka group)
390 * @return bool success or not
392 function getHeader($type) {
396 use_javascript('/plugins/'.$this->name.'/scripts/MantisBTController.js');
397 use_stylesheet('/plugins/'.$this->name.'/style.css');
401 $params['toptab'] = $this->name;
402 $params['group'] = $group_id;
403 $params['title'] = $this->name.' Project Plugin!';
404 $params['pagename'] = $this->name;
405 $params['sectionvals'] = array(group_getname($group_id));
406 site_project_header($params);
412 $params['user_id'] = $user_id;
413 site_user_header($params);
417 case 'globaladmin': {
418 session_require_global_perm('forge_admin');
420 require_once($gfwww.'admin/admin_utils.php');
421 site_admin_header(array('title'=>_('Site Global MantisBT Admin'), 'toptab' => ''));
433 * initialize - initialize the mantisbt plugin
434 * create mantisbt project if needed
436 * @param int the group id
437 * @param array configuration array
438 * @return bool success or not
440 function initialize($group_id, $confArr) {
441 if ($confArr['mantisbtcreate']) {
442 $idProjectMantis = $this->addProjectMantis($group_id, $confArr);
444 $idProjectMantis = $this->getProjectMantisByName($group_id, $confArr);
446 if ($idProjectMantis) {
447 $result = db_query_params('insert into plugin_mantisbt (id_group, id_mantisbt, url, soap_user, soap_password, sync_roles)
448 values ($1, $2, $3, $4, $5, $6)',
452 $confArr['soap_user'],
453 $confArr['soap_password'],
454 $confArr['sync_roles']));
464 * initialize - initialize the mantisbt user
466 * @param array configuration array
467 * @return bool success or not
469 function initializeUser($confArr) {
471 $result = db_query_params('insert into plugin_mantisbt_users (id_user, mantisbt_user, mantisbt_password)
472 values ($1, $2, $3)',
473 array($user->getID(),
474 $confArr['mantisbt_user'],
475 $confArr['mantisbt_password']));
483 * updateConf - update the MantisBT plugin configuration
485 * @param int the group_id
486 * @param array configuration array
487 * @return bool success or not
489 function updateConf($group_id, $confArr) {
490 $result = db_query_params('update plugin_mantisbt set url = $1 , soap_user = $2, soap_password = $3, use_global = $4
491 where id_group = $5',
492 array($confArr['url'],
493 $confArr['soap_user'],
494 $confArr['soap_password'],
495 $confArr['global_conf'],
504 * updateUserConf - update the MantisBT User configuration
506 * @param array configuration array
507 * @return bool success or not
509 function updateUserConf($confArr) {
511 $result = db_query_params('update plugin_mantisbt_users set mantisbt_user = $1 , mantisbt_password = $2
513 array($confArr['mantisbt_user'],
514 $confArr['mantisbt_password'],
523 * getProjectMantisByName - find the project to link with
525 * @param array configuration array
526 * @return int the mantisbt id
528 function getProjectMantisByName($group_id, $confArr) {
529 $groupObject = group_get_object($group_id);
531 $clientSOAP = new SoapClient($confArr['url']."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
532 $mantisbtProjectArr = $clientSOAP->__soapCall('mc_projects_get_user_accessible', array("username" => $confArr['soap_user'], "password" => $confArr['soap_password']));
533 } catch (SoapFault $soapFault) {
534 $groupObject->setError('getProjectMantisByName::Error: ' . $soapFault->faultstring);
537 foreach ($mantisbtProjectArr as $mantisbtProject) {
538 if ($mantisbtProject->name == $confArr['mantisbtname']) {
539 return $mantisbtProject->id;
542 $groupObject->setError('getProjectMantisByName::Error: mantisbt project not found');
547 * getUserConf - return the user / password for the user id mantisbt account
549 * @return array the user configuration
551 function getUserConf() {
554 $resIdUser = db_query_params('SELECT mantisbt_user, mantisbt_password FROM plugin_mantisbt_users WHERE id_user = $1', array($user->getID()));
556 $user->setError('getUserConf::error '.db_error());
560 $row = db_numrows($resIdUser);
562 if ($row == null || count($row) > 2) {
566 $row = db_fetch_array($resIdUser);
567 $userConf['user'] = $row['mantisbt_user'];
568 $userConf['password'] = $row['mantisbt_password'];
569 $userConf['url'] = array();
570 foreach ($user->getGroups() as $groupObject) {
571 if ($groupObject->usesPlugin($this->name)) {
572 $mantisbtGroupConf = $this->getMantisBTConf($groupObject->getID());
573 $userConf['url'][] = $mantisbtGroupConf['url'];
580 * getGlobalconf - return the global configuration defined at forge level
582 * @return array the global configuration array
584 function getGlobalconf() {
585 $resGlobConf = db_query_params('SELECT * from plugin_mantisbt_global',array());
590 $row = db_numrows($resGlobConf);
592 if ($row == null || count($row) > 2) {
596 return db_fetch_array($resGlobConf);
600 * updateGlobalConf - update the global configuration in database
602 * @param array configuration array (url, soap_user, soap_password)
603 * @return bool true on success
605 function updateGlobalConf($confArr) {
606 if (!isset($confArr['url']) || !isset($confArr['soap_user']) || !isset($confArr['soap_password']))
609 $res = db_query_params('truncate plugin_mantisbt_global',array());
613 $res = db_query_params('insert into plugin_mantisbt_global (url, soap_user, soap_password)
614 values ($1, $2, $3)',
617 $confArr['soap_user'],
618 $confArr['soap_password'],
627 * getGlobalAdminView - display the Global Admin View
631 function getGlobalAdminView() {
633 $user = session_get_user();
634 $use_tooltips = $user->usesTooltips();
635 include $gfplugins.$this->name.'/view/admin/viewGlobalConfiguration.php';
641 // c-file-style: "bsd"