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. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
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 along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 class MantisBTPlugin extends Plugin {
28 function MantisBTPlugin() {
30 $this->name = "mantisbt" ;
31 $this->text = "MantisBT" ; // To show in the tabs, use...
32 $this->_addHook('user_personal_links'); //to make a link to the user's personal part of the plugin
33 $this->_addHook('usermenu');
34 $this->_addHook('groupmenu'); // To put into the project tabs
35 $this->_addHook('groupisactivecheckbox'); // The "use ..." checkbox in editgroupinfo
36 $this->_addHook('groupisactivecheckboxpost'); //
37 $this->_addHook('userisactivecheckbox'); // The "use ..." checkbox in user account
38 $this->_addHook('userisactivecheckboxpost'); //
39 $this->_addHook('project_admin_plugins'); // to show up in the admin page fro group
40 $this->_addHook('group_delete');
41 $this->_addHook('group_update');
42 $this->_addHook('site_admin_option_hook');
45 function CallHook($hookname, &$params) {
46 global $G_SESSION, $HTML;
50 if ($G_SESSION->usesPlugin($this->name)) {
51 $param = '?type=user&user_id=' . $G_SESSION->getId() . '&pluginname=' . $this->name; // we indicate the part we're calling is the user one
52 echo $HTML->PrintSubMenu(array($this->text), array('/plugins/mantisbt/index.php' . $param), array(array('title' => _('Personal MantisBT page'))));
58 $group_id = $params['group'];
59 $project = group_get_object($group_id);
60 if (!$project || !is_object($project) || $project->isError() || !$project->isProject()) {
63 if ($project->usesPlugin($this->name)) {
64 $params['TITLES'][] = $this->text;
65 $params['DIRS'][] = '/plugins/'.$this->name.'/?type=group&group_id=' . $group_id . '&pluginname=' . $this->name;
66 $params['TOOLTIPS'][] = _('Tickets Management');
67 if (session_loggedin()) {
68 $user = session_get_user();
69 $userperm = $project->getPermission($user);
70 if ($userperm->isAdmin()) {
71 $params['ADMIN'][] = '/plugins/'.$this->name.'/?type=admin&group_id=' . $group_id . '&pluginname=' . $this->name;
74 if (isset($params['toptab'])) {
75 (($params['toptab'] == $this->name) ? $params['selected'] = (count($params['TITLES'])-1) : '' );
81 case "user_personal_links": {
82 // 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
83 $userid = $params['user_id'];
84 $user = user_get_object($userid);
85 $text = $params['text'];
86 //check if the user has the plugin activated
87 if ($user->usesPlugin($this->name)) {
90 if ($user->usesTooltips()) {
91 $arr_t[] = array('title' => _('Manage your mantisbt account and follow your tickets'));
95 echo util_make_link('/plugins/'.$this->name.'/?user_id=$userid&type=user&pluginname='.$this->name, _('View Personal MantisBT'), $arr_t);
101 case "project_admin_plugins": {
102 // this displays the link in the project admin options page to it's MantisBT administration
103 $group_id = $params['group_id'];
104 $group = group_get_object($group_id);
105 if ($group->usesPlugin($this->name)) {
107 echo util_make_link("/plugins/mantisbt/?group_id=$group_id&type=admin&pluginname=".$this->name, _('View Admin MantisBT')
114 case "site_admin_option_hook": {
115 echo '<li>'.util_make_link("/plugins/mantisbt/?type=globaladmin&pluginname=".$this->name,_('Global MantisBT admin')).'</li>';
119 case "group_delete": {
120 $group_id = $params['group_id'];
121 $group = group_get_object($group_id);
122 if ($group->usesPlugin($this->name)) {
123 if ($this->isProjectMantisCreated($group_id)) {
124 if ($this->removeProjectMantis($group_id)) {
135 case "group_update": {
136 $group_id = $params['group_id'];
137 $group_name =$params['group_name'];
138 $group_ispublic = $params['group_ispublic'];
139 $group = group_get_object($group_id);
140 if ($group->usesPlugin($this->name)) {
141 if ($this->isProjectMantisCreated($group_id)) {
142 if ($this->updateProjectMantis($group_id, $group_name, $group_ispublic)) {
158 * addProjectMantis - inject the Group into Mantisbt thru SOAP
160 * @param array Configuration Array (url, soap_user, soap_password, sync_roles)
161 * @return bool success or not
163 function addProjectMantis($groupId, $confArr) {
164 $groupObject = group_get_object($groupId);
166 $project['name'] = $groupObject->getPublicName();
167 $project['status'] = "development";
169 if ($groupObject->isPublic()) {
170 $project['view_state'] = 10;
172 $project['view_state'] = 50;
175 $project['description'] = $groupObject->getDescription();
178 $clientSOAP = new SoapClient($confArr['url']."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
179 $idProjetMantis = $clientSOAP->__soapCall('mc_project_add', array("username" => $confArr['soap_user'], "password" => $confArr['soap_password'], "project" => $project));
180 } catch (SoapFault $soapFault) {
181 $groupObject->setError('addProjectMantis::Error: ' . $soapFault->faultstring);
184 if (!isset($idProjetMantis) || !is_int($idProjetMantis)){
185 $groupObject->setError('addProjectMantis::Error: ' . _('Unable to create project in Mantisbt'));
188 return $idProjetMantis;
191 function removeProjectMantis($idProjet) {
192 $groupObject = group_get_object($idProjet);
193 $localMantisbtConf = $this->getMantisBTConf($groupObject->getID());
195 if (!$localMantisbtConf) {
196 $groupObject->setError('removeProjetMantis::Error' . ' '. _('No project found'));
200 $clientSOAP = new SoapClient($localMantisbtConf['url']."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
201 $delete = $clientSOAP->__soapCall('mc_project_delete', array("username" => $localMantisbtConf['soap_user'], "password" => $localMantisbtConf['soap_password'], "project_id" => $localMantisbtConf['id_mantisbt']));
202 } catch (SoapFault $soapFault) {
203 $groupObject->setError('removeProjetMantis::Error' . ' '.$soapFault->faultstring);
206 if (!isset($delete)) {
207 $groupObject->setError('removeProjetMantis:: ' . _('No project found in MantisBT') . ' ' .$localMantisbtConf['id_mantisbt']);
210 $res = db_query_params('DELETE FROM plugin_mantisbt WHERE id_mantisbt = $1',
211 array($localMantisbtConf['id_mantisbt']));
213 $groupObject->setError('removeProjetMantis:: ' . _('Cannot delete in database') . ' ' .$localMantisbtConf['id_mantisbt']);
222 * updateProjectMantis - update the Group informations into Mantisbt
223 * @param int id of the Group
224 * @param string group name
225 * @param int public or private
226 * @return bool success or not
228 function updateProjectMantis($groupId, $groupName, $groupIspublic) {
229 $groupObject = group_get_object($groupId);
231 $localMantisbtConf = $this->getMantisBTConf($groupObject->getID());
232 $project['name'] = $groupName;
233 $project['status'] = "development";
235 // should check the config on mantisbt side and not used hard coded values
236 if ($groupIspublic) {
237 $project['view_state'] = 10;
239 $project['view_state'] = 50;
242 if ($localMantisbtConf['id_mantisbt'] != 0) {
244 $clientSOAP = new SoapClient($localMantisbtConf['url']."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
245 $update = $clientSOAP->__soapCall('mc_project_update', array("username" => $localMantisbtConf['soap_user'], "password" => $localMantisbtConf['soap_password'], "project_id" => $localMantisbtConf['id_mantisbt'], "project" => $project));
246 } catch (SoapFault $soapFault) {
247 $groupObject->setError('updateProjectMantis::Error' . ' '. $soapFault->faultstring);
250 if (!isset($update)) {
251 $groupObject->setError('updateProjectMantis::Error' . ' ' . _('Update MantisBT project'));
255 $groupObject->setError('updateProjectMantis::Error ' . _('ID MantisBT project not found'));
262 * isProjectMantisCreated - check if the Project is already created
264 * @param int the Group Id
265 * @return boolean created or not
267 function isProjectMantisCreated($idProjet){
268 $resIdProjetMantis = db_query_params('SELECT id_mantisbt FROM plugin_mantisbt WHERE id_group = $1',
270 if (!$resIdProjetMantis)
273 if (db_numrows($resIdProjetMantis) > 0) {
281 * getMantisBTConf - get the mantisbt configuration id for a specific group_id
283 * @param integer the group_id
284 * @return array the mantisbt configuration array
287 function getMantisBTConf($group_id) {
288 $group = group_get_object($group_id);
289 $mantisbtConfArray = array();
290 $resIdProjetMantis = db_query_params('SELECT * FROM plugin_mantisbt WHERE id_group = $1', array($group_id));
291 if (!$resIdProjetMantis) {
292 $group->setError('getMantisBTId::error '.db_error());
293 return $mantisbtConfArray;
296 $row = db_numrows($resIdProjetMantis);
298 if ($row == null || count($row) > 2) {
299 $mantisbtConfArray['id_mantisbt'] = 0;
300 return $mantisbtConfArray;
303 $row = db_fetch_array($resIdProjetMantis);
304 $mantisbtConfArray['id_mantisbt'] = $row['id_mantisbt'];
305 $mantisbtConfArray['sync_roles'] = $row['sync_roles'];
306 $mantisbtConfArray['use_global'] = $row['use_global'];
307 if ($mantisbtConfArray['use_global']) {
308 $mantisbtGlobConf = $this->getGlobalconf();
309 $mantisbtConfArray['url'] = $mantisbtGlobConf['url'];
310 $mantisbtConfArray['soap_user'] = $mantisbtGlobConf['soap_user'];
311 $mantisbtConfArray['soap_password'] = $mantisbtGlobConf['soap_password'];
313 $mantisbtConfArray['url'] = $row['url'];
314 $mantisbtConfArray['soap_user'] = $row['soap_user'];
315 $mantisbtConfArray['soap_password'] = $row['soap_password'];
318 return $mantisbtConfArray;
322 * getInitDisplay - display the init page
323 * @return bool true only currently
325 function getInitDisplay() {
327 require_once $gfplugins.$this->name.'/view/init.php';
332 * getAdminView - display the admin page
333 * @return bool true only currently
335 function getAdminView() {
337 require_once $gfplugins.$this->name.'/www/admin/index.php';
342 * getSubMenu - display the submenu
344 * @param int enable tooltips : default NO
345 * @return bool true only currently
347 function getSubMenu($enable_tooltips = 0) {
351 $group = group_get_object($group_id);
353 $labelTitle = array();
354 $labelTitle[] = _('Roadmap');
355 $labelTitle[] = _('Tickets');
356 $labelPage = array();
357 $labelPage[] = "/plugins/".$this->name."/?type=group&group_id=".$group_id."&pluginname=".$this->name."&view=roadmap";
358 $labelPage[] = "/plugins/".$this->name."/?type=group&group_id=".$group_id."&pluginname=".$this->name;
359 $labelAttr = array();
360 if ($enable_tooltips) {
361 $labelAttr[] = array('title' => _('View the roadmap, per version tickets'), 'id' => 'roadmapView');
362 $labelAttr[] = array('title' => _('View all tickets.'), 'id' => 'ticketView');
364 $labelAttr[] = array();
365 $labelAttr[] = array();
367 $userperm = $group->getPermission($user);
368 if ( $userperm->isAdmin() ) {
369 $labelTitle[] = _('Administration');
370 $labelPage[] = "/plugins/".$this->name."/?type=admin&group_id=".$group_id."&pluginname=".$this->name;
371 $labelTitle[] = _('Statistics');
372 $labelPage[] = "/plugins/".$this->name."/?type=admin&group_id=".$group_id."&pluginname=".$this->name."&view=stat";
373 if ($enable_tooltips) {
374 $labelAttr[] = array('title' => _('Manage versions, categories and general configuration.'), 'id' => 'adminView');
375 $labelAttr[] = array('title' => _('View global statistics.'), 'id' => 'statView');
377 $labelAttr[] = array();
378 $labelAttr[] = array();
382 echo $HTML->subMenu($labelTitle, $labelPage, $labelAttr);
386 * getHeader - initialize header and js
387 * @param string type : user, project (aka group)
388 * @return bool success or not
390 function getHeader($type) {
394 use_javascript('/plugins/'.$this->name.'/scripts/MantisBTController.js');
395 use_stylesheet('/plugins/'.$this->name.'/style.css');
399 $params['toptab'] = $this->name;
400 $params['group'] = $group_id;
401 $params['title'] = $this->name.' Project Plugin!';
402 $params['pagename'] = $this->name;
403 $params['sectionvals'] = array(group_getname($group_id));
404 site_project_header($params);
410 $params['user_id'] = $user_id;
411 site_user_header($params);
415 case 'globaladmin': {
416 session_require_global_perm('forge_admin');
418 require_once($gfwww.'admin/admin_utils.php');
419 site_admin_header(array('title'=>_('Site Global MantisBT Admin'), 'toptab' => ''));
431 * initialize - initialize the mantisbt plugin
432 * create mantisbt project if needed
434 * @param int the group id
435 * @param array configuration array
436 * @return bool success or not
438 function initialize($group_id, $confArr) {
439 if ($confArr['mantisbtcreate']) {
440 $idProjectMantis = $this->addProjectMantis($group_id, $confArr);
442 $idProjectMantis = $this->getProjectMantisByName($group_id, $confArr);
444 if ($idProjectMantis) {
445 $result = db_query_params('insert into plugin_mantisbt (id_group, id_mantisbt, url, soap_user, soap_password, sync_roles)
446 values ($1, $2, $3, $4, $5, $6)',
450 $confArr['soap_user'],
451 $confArr['soap_password'],
452 $confArr['sync_roles']));
462 * initialize - initialize the mantisbt user
464 * @param array configuration array
465 * @return bool success or not
467 function initializeUser($confArr) {
469 $result = db_query_params('insert into plugin_mantisbt_users (id_user, mantisbt_user, mantisbt_password)
470 values ($1, $2, $3)',
471 array($user->getID(),
472 $confArr['mantisbt_user'],
473 $confArr['mantisbt_password']));
481 * updateConf - update the MantisBT plugin configuration
483 * @param int the group_id
484 * @param array configuration array
485 * @return bool success or not
487 function updateConf($group_id, $confArr) {
488 $result = db_query_params('update plugin_mantisbt set url = $1 , soap_user = $2, soap_password = $3, use_global = $4
489 where id_group = $5',
490 array($confArr['url'],
491 $confArr['soap_user'],
492 $confArr['soap_password'],
493 $confArr['global_conf'],
502 * updateUserConf - update the MantisBT User configuration
504 * @param array configuration array
505 * @return bool success or not
507 function updateUserConf($confArr) {
509 $result = db_query_params('update plugin_mantisbt_users set mantisbt_user = $1 , mantisbt_password = $2
511 array($confArr['mantisbt_user'],
512 $confArr['mantisbt_password'],
521 * getProjectMantisByName - find the project to link with
523 * @param array configuration array
524 * @return int the mantisbt id
526 function getProjectMantisByName($group_id, $confArr) {
527 $groupObject = group_get_object($group_id);
529 $clientSOAP = new SoapClient($confArr['url']."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
530 $mantisbtProjectArr = $clientSOAP->__soapCall('mc_projects_get_user_accessible', array("username" => $confArr['soap_user'], "password" => $confArr['soap_password']));
531 } catch (SoapFault $soapFault) {
532 $groupObject->setError('getProjectMantisByName::Error: ' . $soapFault->faultstring);
535 foreach ($mantisbtProjectArr as $mantisbtProject) {
536 if ($mantisbtProject->name == $confArr['mantisbtname']) {
537 return $mantisbtProject->id;
540 $groupObject->setError('getProjectMantisByName::Error: mantisbt project not found');
545 * getUserConf - return the user / password for the user id mantisbt account
547 * @return array the user configuration
549 function getUserConf() {
552 $resIdUser = db_query_params('SELECT mantisbt_user, mantisbt_password FROM plugin_mantisbt_users WHERE id_user = $1', array($user->getID()));
554 $user->setError('getUserConf::error '.db_error());
558 $row = db_numrows($resIdUser);
560 if ($row == null || count($row) > 2) {
564 $row = db_fetch_array($resIdUser);
565 $userConf['user'] = $row['mantisbt_user'];
566 $userConf['password'] = $row['mantisbt_password'];
567 $userConf['url'] = array();
568 foreach ($user->getGroups() as $groupObject) {
569 if ($groupObject->usesPlugin($this->name)) {
570 $mantisbtGroupConf = $this->getMantisBTConf($groupObject->getID());
571 $userConf['url'][] = $mantisbtGroupConf['url'];
578 * getGlobalconf - return the global configuration defined at forge level
580 * @return array the global configuration array
582 function getGlobalconf() {
583 $resGlobConf = db_query_params('SELECT * from plugin_mantisbt_global',array());
588 $row = db_numrows($resGlobConf);
590 if ($row == null || count($row) > 2) {
594 return db_fetch_array($resGlobConf);
598 * updateGlobalConf - update the global configuration in database
600 * @param array configuration array (url, soap_user, soap_password)
601 * @return bool true on success
603 function updateGlobalConf($confArr) {
604 if (!isset($confArr['url']) || !isset($confArr['soap_user']) || !isset($confArr['soap_password']))
607 $res = db_query_params('truncate plugin_mantisbt_global',array());
611 $res = db_query_params('insert into plugin_mantisbt_global (url, soap_user, soap_password)
612 values ($1, $2, $3)',
615 $confArr['soap_user'],
616 $confArr['soap_password'],
625 * getGlobalAdminView - display the Global Admin View
629 function getGlobalAdminView() {
631 $user = session_get_user();
632 $use_tooltips = $user->usesTooltips();
633 include $gfplugins.$this->name.'/view/admin/viewGlobalConfiguration.php';
639 // c-file-style: "bsd"