3 * projects_hierarchyPlugin Class
5 * Copyright 2006 (c) Fabien Regnier - Sogeti
6 * Copyright 2010-2011, Franck Villaume - Capgemini
7 * http://fusionforge.org
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 class projects_hierarchyPlugin extends Plugin {
26 function projects_hierarchyPlugin() {
28 $this->name = 'projects-hierarchy';
29 $this->text = _('Project Hierarchy'); // To show in the tabs, use...
30 $this->_addHook('groupisactivecheckbox'); // The "use ..." checkbox in editgroupinfo
31 $this->_addHook('groupisactivecheckboxpost');
32 $this->_addHook('hierarchy_views'); // include specific views
33 $this->_addHook('display_hierarchy'); // to see the tree of projects
34 $this->_addHook('group_delete'); // clean tables on delete
35 $this->_addHook('project_admin_plugins'); // to show up in the admin page fro group
36 $this->_addHook('site_admin_option_hook'); // to provide a link to the site wide administrative pages of plugin
37 $this->_addHook('display_hierarchy_submenu'); // to display a submenu in software map if projects-hierarchy plugin is used
40 function CallHook($hookname, &$params) {
41 global $G_SESSION, $HTML;
44 case "display_hierarchy": {
45 if ($this->displayHierarchy()) {
50 case "hierarchy_views": {
52 require_once $gfplugins.$this->name.'/include/hierarchy_utils.php';
53 $group_id = $params[0];
54 $project = group_get_object($group_id);
55 if ($project->usesPlugin($this->name)) {
60 include($gfplugins.$this->name.'/view/'.$params[1].'_project_link.php');
71 case "group_delete": {
72 if ($params['group']->usesPlugins($this->name)) {
73 if ($this->remove($params['group_id'])) {
81 case "site_admin_option_hook": {
82 // Use this to provide a link to the site wide administrative pages for your plugin
83 echo '<li>'.$this->getAdminOptionLink().'</li>';
87 case "project_admin_plugins": {
88 // this displays the link in the project admin options page to it's administration
89 $group_id = $params['group_id'];
90 $group = group_get_object($group_id);
91 if ($group->usesPlugin($this->name)) {
93 echo util_make_link('/plugins/'.$this->name.'/?group_id='.$group_id.'&type=admin&pluginname='.$this->name, _('Hierarchy Admin'), array('class'=>'tabtitle', 'title'=>_('Configure the projets-hierarchy plugin (docman, tree, delegate, globalconf features)')));
99 case "display_hierarchy_submenu": {
100 // Use to display a submenu in software map page if at least one project has a valid relationship
101 $res1 = db_query_params('SELECT g.group_name FROM plugins p, group_plugin gp, groups g WHERE plugin_name = $1 and gp.group_id = g.group_id and p.plugin_id = gp.plugin_id',
104 if (db_numrows($res1) > 0) {
105 $res2 = db_query_params('SELECT count(*) as used FROM plugin_projects_hierarchy_relationship where status = $1',
108 $row = db_fetch_array($res2);
110 $hierarchy_used = true;
114 if (isset($hierarchy_used)) {
115 $hierarMenuTitle[] = _('Per Category');
116 $hierarMenuTitle[] = _('Per Hierarchy');
117 $hierarMenuAttr[] = array('title' => _('Browse per category the available projects. Some projects might not appear here they do not choose any categories'), 'class' => 'tabtitle-nw');
118 $hierarMenuAttr[] = array('title' => _('Browse per hierarchy. Projects can share relationship between projects, as father and sons'), 'class' => 'tabtitle');
119 $hierarMenuUrl[] = '/softwaremap/trove_list.php?cat=c';
120 $hierarMenuUrl[] = '/softwaremap/trove_list.php?cat=h';
121 echo ($HTML->subMenu($hierarMenuTitle, $hierarMenuUrl, $hierarMenuAttr));
130 function displayHierarchy() {
131 $tree = $this->buildTree();
132 $displayTree = $this->affTree($tree, 0);
139 echo '<link rel="StyleSheet" href="/plugins/projects-hierarchy/dtree.css" type="text/css" />
140 <script type="text/javascript" src="/plugins/projects-hierarchy/dtree.js"></script>';
143 function buildTree() {
144 global $project_name;
145 $res = db_query_params('select p1.group_id as father_id,p1.unix_group_name as father_unix_name,p1.group_name as father_name,p2.group_id as son_id,p2.unix_group_name as son_unix_name,p2.group_name as son_name
146 from groups as p1,groups as p2,plugin_projects_hierarchy_relationship
147 where p1.group_id=plugin_projects_hierarchy_relationship.project_id
148 and p2.group_id=plugin_projects_hierarchy_relationship.sub_project_id
149 and plugin_projects_hierarchy_relationship.status=$1
150 and (select tree from plugin_projects_hierarchy where project_id = p2.group_id)
151 order by father_name, son_name',
154 // construction du tableau associatif
155 // key = name of the father
156 // value = list of sons
158 while ($row = db_fetch_array($res)) {
159 if (forge_check_perm('project_read', $row['father_id'])) {
160 $tree[$row['father_id']][] = $row['son_id'];
161 //get the unix name of the project
162 $project_name[$row['father_id']][0] = $row['father_name'];
163 $project_name[$row['son_id']][0] = $row['son_name'];
164 $project_name[$row['father_id']][1] = $row['father_unix_name'];
165 $project_name[$row['son_id']][1] = $row['son_unix_name'];
171 function affTree($tree, $lvl) {
172 global $project_name;
178 while (list($key, $sons) = each($tree)) {
179 // Really don't know why there is a warning there, and added @
180 if(@!$arbre[$key] != 0){
184 foreach ($sons as $son) {
185 $arbre[$son] = $cpt_pere;
189 if (sizeof($arbre)) {
190 $returnTree .= '<table ><tr><td>';
191 $returnTree .= '<script type="text/javascript">';
192 $returnTree .= 'd = new dTree(\'d\');';
193 $returnTree .= 'd.add(0,-1,\'Project Hierarchy Tree\');';
195 //construction automatique de l'arbre format : (num_fils, num_pere,nom,nom_unix)
196 while (list($key2, $sons2) = each($arbre)) {
197 $returnTree .= "d.add('".$key2."','".$sons2."','".$project_name[$key2][0]."','".util_make_url( '/projects/'.$project_name[$key2][1] .'/', $project_name[$key2][1] ) ."');";
200 $returnTree .= 'document.write(d);';
201 $returnTree .= '</script>';
202 $returnTree .= '</td></tr></table>';
208 * getFamily - find the children or parent group_id of this project.
210 * @param integer group_id to search for
211 * @param string parent or child ?
212 * @param boolean recurcive or not ?
213 * @param string validated or pending or any relation ?
214 * @return array array of arrays with group_id of parent or childs
217 function getFamily($group_id, $order, $deep = false, $status = 'any') {
218 $localFamily = array();
221 $statusCondition = 't';
226 $statusCondition = 'f';
236 $qpa = db_construct_qpa(false, 'SELECT project_id as id FROM plugin_projects_hierarchy_relationship
237 WHERE sub_project_id = $1', array($group_id));
238 if (isset($statusCondition))
239 $qpa = db_construct_qpa($qpa, ' AND status = $1', array($statusCondition));
241 $res = db_query_qpa($qpa);
245 $qpa = db_construct_qpa(false, 'SELECT sub_project_id as id FROM plugin_projects_hierarchy_relationship
246 WHERE project_id = $1', array($group_id));
247 if (isset($statusCondition))
248 $qpa = db_construct_qpa($qpa, ' AND status = $1', array($statusCondition));
250 $res = db_query_qpa($qpa);
258 if ($res && db_numrows($res) > 0) {
259 while ($arr = db_fetch_array($res)) {
260 $localFamily[] = $arr['id'];
265 $nextFamily = array();
266 for ( $i = 0; $i < count($localFamily); $i++) {
267 $nextFamily = $this->getFamily($localFamily[$i], $order, $deep, $status);
270 if (isset($nextFamily) && sizeof($nextFamily))
271 $localFamily = array_merge($localFamily, $nextFamily);
277 * getDocmanStatus - returns the docman status for this project
279 * @param integer group_id
280 * @return boolean true/false
283 function getDocmanStatus($group_id) {
284 $res = db_query_params('SELECT docman FROM plugin_projects_hierarchy WHERE project_id = $1 limit 1',
289 $resArr = db_fetch_array($res);
290 if ($resArr['docman'] == 't')
297 * setDocmanStatus - allow parent to browse your document manager and allow yourself to select your childs to be browsed.
299 * @param integer your groud_id
300 * @param boolean the status to set
301 * @return boolean success or not
303 function setDocmanStatus($group_id, $status = 0) {
304 $res = db_query_params('UPDATE plugin_projects_hierarchy set docman = $1 WHERE project_id = $2',
305 array($status, $group_id));
314 * redirect - encapsulate session_redirect to handle correctly the redirection URL
316 * @param string usually http_referer from $_SERVER
317 * @param string type of feedback : error, warning, feedback
318 * @param string the message of feedback
321 function redirect($http_referer, $type, $message) {
332 $url = util_find_relative_referer($http_referer);
333 if (strpos($url,'?')) {
334 session_redirect($url.'&'.$type.'='.urlencode($message));
336 session_redirect($url.'?'.$type.'='.urlencode($message));
340 * override default groupisactivecheckboxpost function for init value in db
342 function groupisactivecheckboxpost(&$params) {
343 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
344 $group = group_get_object($params['group']);
345 $flag = strtolower('use_'.$this->name);
346 if ( getIntFromRequest($flag) == 1 ) {
347 if ($this->add($group->getID())) {
348 $group->setPluginUse($this->name);
352 if ($this->remove($group->getID())) {
353 $group->setPluginUse($this->name, false);
361 * add - add a new group_id using the plugin
363 * @param integer group_id
364 * @return boolean true on success
367 function add($group_id) {
368 if (!$this->exists($group_id)) {
369 $globalConf = $this->getGlobalconf();
370 $res = db_query_params('INSERT INTO plugin_projects_hierarchy (project_id, tree, docman, delegate) VALUES ($1, $2, $3, $4)', array($group_id, (int)$globalConf['tree'], (int)$globalConf['docman'], (int)$globalConf['delegate']));
378 * remove - remove group_id using the plugin
380 * @param integer group_id
381 * @return boolean true on success
384 function remove($group_id) {
385 if ($this->exists($group_id)) {
387 $res = db_query_params('DELETE FROM plugin_projects_hierarchy where project_id = $1', array($group_id));
393 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship where project_id = $1 OR sub_project_id = $1',
405 * addChild - add a new child to this project
407 * @param integer group_id
408 * @param integer sub_group_id
409 * @return boolean true on success
412 function addChild($project_id, $sub_project_id) {
413 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
414 if (!$this->hasRelation($project_id, $sub_project_id)) {
415 $res = db_query_params('INSERT INTO plugin_projects_hierarchy_relationship (project_id, sub_project_id)
416 VALUES ($1, $2)', array($project_id, $sub_project_id));
427 * removeChild - remove a child to this project
429 * @param integer group_id
430 * @param integer sub_group_id
431 * @return boolean true on success
434 function removeChild($project_id, $sub_project_id) {
435 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
436 if ($this->hasRelation($project_id, $sub_project_id)) {
437 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
438 WHERE project_id = $1 AND sub_project_id = $2',
439 array($project_id, $sub_project_id));
450 * removeParent - remove a parent to this project
452 * @param integer group_id
453 * @param integer sub_group_id
454 * @return boolean true on success
457 function removeParent($project_id, $sub_project_id) {
458 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
459 if ($this->hasRelation($project_id, $sub_project_id)) {
460 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
461 WHERE project_id = $1 AND sub_project_id = $2',
462 array($sub_project_id, $project_id));
473 * hasRelation - check if there is a relation child->parent or parent->child between 2 projects
475 * @param integer group_id
476 * @param integer sub_group_id
477 * @return boolean true on success
480 function hasRelation($project_id, $sub_project_id) {
481 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
482 $res = db_query_params('SELECT * FROM plugin_projects_hierarchy_relationship
483 WHERE ( project_id = $1 AND sub_project_id = $2 )
484 OR ( project_id = $2 AND sub_project_id = $1 )',
485 array($project_id, $sub_project_id));
486 if ($res && db_numrows($res) == 1 )
493 * validateRelationship - validate or reject a relation between 2 projects
495 * @param integer group_id
496 * @param integer sub_group_id
497 * @param string type of relation
498 * @param integer status of the relation
499 * @return boolean true on success
502 function validateRelationship($project_id, $sub_project_id, $relation, $status) {
503 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
504 if ($this->hasRelation($project_id, $sub_project_id)) {
506 $qpa = db_construct_qpa(false, 'UPDATE plugin_projects_hierarchy_relationship SET status = $1
507 WHERE ', array($status));
510 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
511 array($sub_project_id, $project_id));
515 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
516 array($project_id, $sub_project_id));
524 $res = db_query_qpa($qpa);
528 if (db_affected_rows($res))
531 $qpa = db_construct_qpa(false, 'DELETE FROM plugin_projects_hierarchy_relationship WHERE ');
534 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
535 array($project_id, $sub_project_id));
539 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
540 array($sub_project_id, $project_id));
548 $res = db_query_qpa($qpa);
552 if (db_affected_rows($res))
561 * exists - if this project use the plugin
563 * @param integer group_id
564 * @return boolean true on success
567 function exists($group_id) {
568 $res = db_query_params('SELECT project_id FROM plugin_projects_hierarchy WHERE project_id = $1', array($group_id));
572 if (db_numrows($res))
579 * getAdminOptionLink - return the admin link url
581 * @return string html url
584 function getAdminOptionLink() {
585 return util_make_link('/plugins/'.$this->name.'/?type=globaladmin&pluginname='.$this->name,_('Global Hierarchy admin'), array('class'=>'tabtitle', 'title'=>_('Configure the projets-hierarchy plugin (docman, tree, delegate, globalconf features)')));
589 * getHeader - initialize header and js
590 * @param string type : user, project (aka group)
591 * @param array params
592 * @return bool success or not
594 function getHeader($type, $params=NULL) {
598 case 'globaladmin': {
599 session_require_global_perm('forge_admin');
601 require_once($gfwww.'admin/admin_utils.php');
602 site_admin_header(array('title'=>_('Site Global Hierarchy Admin'), 'toptab'=>''));
608 site_project_header($params);
617 * getGlobalAdminView - display the global configuration view
619 * @return boolean True
622 function getGlobalAdminView() {
623 global $gfplugins, $use_tooltips;
624 include $gfplugins.$this->name.'/view/admin/viewGlobalConfiguration.php';
629 * getGlobalconf - return the global configuration defined at forge level
631 * @return array the global configuration array
633 function getGlobalconf() {
634 $resGlobConf = db_query_params('SELECT * from plugin_projects_hierarchy_global',array());
639 $row = db_numrows($resGlobConf);
641 if ($row == null || count($row) > 2) {
645 $resArr = db_fetch_array($resGlobConf);
646 $returnArr = array();
648 foreach($resArr as $column => $value) {
650 $returnArr[$column] = true;
652 $returnArr[$column] = false;
660 * getConf - return the configuration defined at project level
662 * @param integer the group id
663 * @return array the configuration array
665 function getConf($project_id) {
666 $resConf = db_query_params('SELECT * from plugin_projects_hierarchy where project_id=$1',array($project_id));
671 $row = db_numrows($resConf);
673 if ($row == null || count($row) > 2) {
677 $resArr = db_fetch_array($resConf);
678 $returnArr = array();
680 foreach($resArr as $column => $value) {
682 $returnArr[$column] = true;
684 $returnArr[$column] = false;
692 function getProjectAdminView() {
693 global $gfplugins, $use_tooltips;
694 include $gfplugins.$this->name.'/view/admin/viewProjectConfiguration.php';
699 * getFooter - display footer
701 function getFooter($type) {
705 case 'globaladmin': {
706 session_require_global_perm('forge_admin');
707 site_admin_footer(array());
712 site_project_footer(array());
720 * updateGlobalConf - update the global configuration in database
722 * @param array configuration array (tree, docman, delegate)
723 * @return bool true on success
725 function updateGlobalConf($confArr) {
726 if (!isset($confArr['tree']) || !isset($confArr['docman']) || !isset($confArr['delegate']))
729 $res = db_query_params('truncate plugin_projects_hierarchy_global',array());
733 $res = db_query_params('insert into plugin_projects_hierarchy_global (tree, docman, delegate)
734 values ($1, $2, $3)',
747 * updateConf - update the configuration in database for this project
749 * @param array configuration array (tree, docman, delegate)
750 * @return bool true on success
752 function updateConf($project_id, $confArr) {
753 if (!isset($confArr['tree']) || !isset($confArr['docman']) || !isset($confArr['delegate']) || !isset($confArr['globalconf']))
756 $res = db_query_params('update plugin_projects_hierarchy
757 set (tree, docman, delegate, globalconf) = ($1, $2, $3, $4)
758 where project_id = $5',
762 $confArr['delegate'],
763 $confArr['globalconf'],
773 * son_box - display a select box for son selection
775 * @param integer group_id
777 * @param string selected value
778 * @return string html box
781 function son_box($group_id, $name, $selected = 'xzxzxz') {
782 $sons = $this->getFamily($group_id, 'child', true, 'any');
783 $parent = $this->getFamily($group_id, 'parent', true, 'any');
784 $family = array_merge($parent, $sons);
785 $son = db_query_params('SELECT group_id, group_name FROM groups
788 AND group_id <> ALL ($3)
789 AND group_id IN (select group_id from group_plugin,plugins where group_plugin.plugin_id = plugins.plugin_id and plugins.plugin_name = $4);',
792 db_int_array_to_any_clause($family),
794 return html_build_select_box($son, $name, $selected, false);
799 // c-file-style: "bsd"