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');
43 $this->_addHook('widget_instance', 'myPageBox', false);
44 $this->_addHook('widgets', 'widgets', false);
47 function CallHook($hookname, &$params) {
48 global $G_SESSION, $HTML;
52 if ($G_SESSION->usesPlugin($this->name)) {
53 $param = '?type=user&user_id=' . $G_SESSION->getId() . '&pluginname=' . $this->name; // we indicate the part we're calling is the user one
54 echo $HTML->PrintSubMenu(array($this->text), array('/plugins/mantisbt/index.php' . $param), array(array('title' => _('Personal MantisBT page'))));
60 $group_id = $params['group'];
61 $project = group_get_object($group_id);
62 if (!$project || !is_object($project) || $project->isError() || !$project->isProject()) {
65 if ($project->usesPlugin($this->name)) {
66 $params['TITLES'][] = $this->text;
67 $params['DIRS'][] = '/plugins/'.$this->name.'/?type=group&group_id=' . $group_id . '&pluginname=' . $this->name;
68 $params['TOOLTIPS'][] = _('Tickets Management');
69 if (session_loggedin()) {
70 $user = session_get_user();
71 $userperm = $project->getPermission($user);
72 if ($userperm->isAdmin()) {
73 $params['ADMIN'][] = '/plugins/'.$this->name.'/?type=admin&group_id=' . $group_id . '&pluginname=' . $this->name;
76 if (isset($params['toptab'])) {
77 (($params['toptab'] == $this->name) ? $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 //check if the user has the plugin activated
88 if ($user->usesPlugin($this->name)) {
91 $arr_t[] = array('title' => _('Manage your mantisbt account and follow your tickets'), 'class' => 'tabtitle');
92 echo util_make_link('/plugins/'.$this->name.'/?user_id=$userid&type=user&pluginname='.$this->name, _('View Personal MantisBT'), $arr_t);
98 case "project_admin_plugins": {
99 // this displays the link in the project admin options page to it's MantisBT administration
100 $group_id = $params['group_id'];
101 $group = group_get_object($group_id);
102 if ($group->usesPlugin($this->name)) {
104 echo util_make_link("/plugins/mantisbt/?group_id=$group_id&type=admin&pluginname=".$this->name, _('View Admin MantisBT')
111 case "site_admin_option_hook": {
112 echo '<li>'.$this->getAdminOptionLink().'</li>';
116 case "group_delete": {
117 $group_id = $params['group_id'];
118 $group = group_get_object($group_id);
119 if ($group->usesPlugin($this->name)) {
120 if ($this->isProjectMantisCreated($group_id)) {
121 if ($this->removeProjectMantis($group_id)) {
132 case "group_update": {
133 $group_id = $params['group_id'];
134 $group_name =$params['group_name'];
135 $group_ispublic = $params['group_ispublic'];
136 $group = group_get_object($group_id);
137 if ($group->usesPlugin($this->name)) {
138 if ($this->isProjectMantisCreated($group_id)) {
139 if ($this->updateProjectMantis($group_id, $group_name, $group_ispublic)) {
151 $group = group_get_object($GLOBALS['group_id']);
152 if ($group->usesPlugin($this->name)) {
153 return $this->widgets($params);
157 case "widget_instance": {
158 $group = group_get_object($GLOBALS['group_id']);
159 if ($group->usesPlugin($this->name)) {
160 return $this->myPageBox($params);
169 * addProjectMantis - inject the Group into Mantisbt thru SOAP
171 * @param array Configuration Array (url, soap_user, soap_password, sync_roles)
172 * @return bool success or not
174 function addProjectMantis($groupId, $confArr) {
175 $groupObject = group_get_object($groupId);
177 $project['name'] = $groupObject->getPublicName();
178 $project['status'] = "development";
180 if ($groupObject->isPublic()) {
181 $project['view_state'] = 10;
183 $project['view_state'] = 50;
186 $project['description'] = $groupObject->getDescription();
189 $clientSOAP = new SoapClient($confArr['url']."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
190 $idProjetMantis = $clientSOAP->__soapCall('mc_project_add', array("username" => $confArr['soap_user'], "password" => $confArr['soap_password'], "project" => $project));
191 } catch (SoapFault $soapFault) {
192 $groupObject->setError('addProjectMantis::Error: ' . $soapFault->faultstring);
195 if (!isset($idProjetMantis) || !is_int($idProjetMantis)){
196 $groupObject->setError('addProjectMantis::Error: ' . _('Unable to create project in Mantisbt'));
199 return $idProjetMantis;
202 function removeProjectMantis($idProjet) {
203 $groupObject = group_get_object($idProjet);
204 $localMantisbtConf = $this->getMantisBTConf($groupObject->getID());
206 if (!$localMantisbtConf) {
207 $groupObject->setError('removeProjetMantis::Error' . ' '. _('No project found'));
211 $clientSOAP = new SoapClient($localMantisbtConf['url']."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
212 $delete = $clientSOAP->__soapCall('mc_project_delete', array("username" => $localMantisbtConf['soap_user'], "password" => $localMantisbtConf['soap_password'], "project_id" => $localMantisbtConf['id_mantisbt']));
213 } catch (SoapFault $soapFault) {
214 $groupObject->setError('removeProjetMantis::Error' . ' '.$soapFault->faultstring);
217 if (!isset($delete)) {
218 $groupObject->setError('removeProjetMantis:: ' . _('No project found in MantisBT') . ' ' .$localMantisbtConf['id_mantisbt']);
221 $res = db_query_params('DELETE FROM plugin_mantisbt WHERE id_mantisbt = $1',
222 array($localMantisbtConf['id_mantisbt']));
224 $groupObject->setError('removeProjetMantis:: ' . _('Cannot delete in database') . ' ' .$localMantisbtConf['id_mantisbt']);
233 * updateProjectMantis - update the Group informations into Mantisbt
234 * @param int id of the Group
235 * @param string group name
236 * @param int public or private
237 * @return bool success or not
239 function updateProjectMantis($groupId, $groupName, $groupIspublic) {
240 $groupObject = group_get_object($groupId);
242 $localMantisbtConf = $this->getMantisBTConf($groupObject->getID());
243 $project['name'] = $groupName;
244 $project['status'] = "development";
246 // should check the config on mantisbt side and not used hard coded values
247 if ($groupIspublic) {
248 $project['view_state'] = 10;
250 $project['view_state'] = 50;
253 if ($localMantisbtConf['id_mantisbt'] != 0) {
255 $clientSOAP = new SoapClient($localMantisbtConf['url']."/api/soap/mantisconnect.php?wsdl", array('trace'=>true, 'exceptions'=>true));
256 $update = $clientSOAP->__soapCall('mc_project_update', array("username" => $localMantisbtConf['soap_user'], "password" => $localMantisbtConf['soap_password'], "project_id" => $localMantisbtConf['id_mantisbt'], "project" => $project));
257 } catch (SoapFault $soapFault) {
258 $groupObject->setError('updateProjectMantis::Error' . ' '. $soapFault->faultstring);
261 if (!isset($update)) {
262 $groupObject->setError('updateProjectMantis::Error' . ' ' . _('Update MantisBT project'));
266 $groupObject->setError('updateProjectMantis::Error ' . _('ID MantisBT project not found'));
273 * isProjectMantisCreated - check if the Project is already created
275 * @param int the Group Id
276 * @return boolean created or not
278 function isProjectMantisCreated($idProjet){
279 $resIdProjetMantis = db_query_params('SELECT id_mantisbt FROM plugin_mantisbt WHERE id_group = $1',
281 if (!$resIdProjetMantis)
284 if (db_numrows($resIdProjetMantis) > 0) {
292 * getMantisBTConf - get the mantisbt configuration id for a specific group_id
294 * @param integer the group_id
295 * @return array the mantisbt configuration array
298 function getMantisBTConf($group_id) {
299 $group = group_get_object($group_id);
300 $mantisbtConfArray = array();
301 $resIdProjetMantis = db_query_params('SELECT * FROM plugin_mantisbt WHERE id_group = $1', array($group_id));
302 if (!$resIdProjetMantis) {
303 $group->setError('getMantisBTId::error '.db_error());
304 return $mantisbtConfArray;
307 $row = db_numrows($resIdProjetMantis);
309 if ($row == null || count($row) > 2) {
310 $mantisbtConfArray['id_mantisbt'] = 0;
311 return $mantisbtConfArray;
314 $row = db_fetch_array($resIdProjetMantis);
315 $mantisbtConfArray['id_mantisbt'] = $row['id_mantisbt'];
316 $mantisbtConfArray['sync_roles'] = $row['sync_roles'];
317 $mantisbtConfArray['use_global'] = $row['use_global'];
318 if ($mantisbtConfArray['use_global']) {
319 $mantisbtGlobConf = $this->getGlobalconf();
320 $mantisbtConfArray['url'] = $mantisbtGlobConf['url'];
321 $mantisbtConfArray['soap_user'] = $mantisbtGlobConf['soap_user'];
322 $mantisbtConfArray['soap_password'] = $mantisbtGlobConf['soap_password'];
324 $mantisbtConfArray['url'] = $row['url'];
325 $mantisbtConfArray['soap_user'] = $row['soap_user'];
326 $mantisbtConfArray['soap_password'] = $row['soap_password'];
329 return $mantisbtConfArray;
333 * getInitDisplay - display the init page
334 * @return bool true only currently
336 function getInitDisplay() {
338 require_once $gfplugins.$this->name.'/view/init.php';
343 * getAdminView - display the admin page
344 * @return bool true only currently
346 function getAdminView() {
348 require_once $gfplugins.$this->name.'/www/admin/index.php';
353 * getSubMenu - display the submenu
355 * @param int enable tooltips : default NO
356 * @return bool true only currently
358 function getSubMenu() {
362 $group = group_get_object($group_id);
364 $labelTitle = array();
365 $labelTitle[] = _('Roadmap');
366 $labelTitle[] = _('Tickets');
367 $labelPage = array();
368 $labelPage[] = "/plugins/".$this->name."/?type=group&group_id=".$group_id."&pluginname=".$this->name."&view=roadmap";
369 $labelPage[] = "/plugins/".$this->name."/?type=group&group_id=".$group_id."&pluginname=".$this->name;
370 $labelAttr = array();
371 $labelAttr[] = array('title' => _('View the roadmap, per version tickets'), 'id' => 'roadmapView', 'class' => 'tabtitle-nw');
372 $labelAttr[] = array('title' => _('View all tickets.'), 'id' => 'ticketView', 'class' => 'tabtitle');
373 $userperm = $group->getPermission($user);
374 if ( $userperm->isAdmin() ) {
375 $labelTitle[] = _('Administration');
376 $labelPage[] = "/plugins/".$this->name."/?type=admin&group_id=".$group_id."&pluginname=".$this->name;
377 $labelTitle[] = _('Statistics');
378 $labelPage[] = "/plugins/".$this->name."/?type=admin&group_id=".$group_id."&pluginname=".$this->name."&view=stat";
379 $labelAttr[] = array('title' => _('Manage versions, categories and general configuration.'), 'id' => 'adminView', 'class' => 'tabtitle');
380 $labelAttr[] = array('title' => _('View global statistics.'), 'id' => 'statView', 'class' => 'tabtitle');
383 echo $HTML->subMenu($labelTitle, $labelPage, $labelAttr);
387 * getHeader - initialize header and js
388 * @param string type : user, project (aka group)
389 * @return bool success or not
391 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 include $gfplugins.$this->name.'/view/admin/viewGlobalConfiguration.php';
636 function getAdminOptionLink() {
637 return util_make_link('/plugins/'.$this->name.'/?type=globaladmin&pluginname='.$this->name, _('Global MantisBT admin'), array('class' => 'tabtitle', 'title' => _('Direct link to global configuration of this plugin')));
640 function widgets($params) {
641 require_once('common/widget/WidgetLayoutManager.class.php');
642 if ($params['owner_type'] == WidgetLayoutManager::OWNER_TYPE_GROUP) {
643 $params['fusionforge_widgets'][] = 'plugin_mantisbt_project_latestissues';
648 function myPageBox($params) {
650 require_once('common/widget/WidgetLayoutManager.class.php');
651 if ($params['widget'] == 'plugin_mantisbt_project_latestissues') {
652 require_once $gfplugins.$this->name.'/common/mantisbt_Widget_ProjectLastIssues.class.php';
653 $params['instance'] = new mantisbt_Widget_ProjectLastIssues($this);
659 // c-file-style: "bsd"