3 * projects_hierarchyPlugin Class
5 * Copyright 2006 (c) Fabien Regnier - Sogeti
6 * Copyright 2010-2011, Franck Villaume - Capgemini
7 * Copyright 2012, 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 projects_hierarchyPlugin extends Plugin {
27 function __construct() {
29 $this->name = 'projects-hierarchy';
30 $this->text = _('Project Hierarchy'); // To show in the tabs, use...
31 $this->_addHook('groupisactivecheckbox'); // The "use ..." checkbox in editgroupinfo
32 $this->_addHook('groupisactivecheckboxpost');
33 $this->_addHook('hierarchy_views'); // include specific views
34 $this->_addHook('display_hierarchy'); // to see the tree of projects
35 $this->_addHook('group_delete'); // clean tables on delete
36 $this->_addHook('project_admin_plugins'); // to show up in the admin page fro group
37 $this->_addHook('site_admin_option_hook'); // to provide a link to the site wide administrative pages of plugin
38 $this->_addHook('display_hierarchy_submenu'); // to display a submenu in software map if projects-hierarchy plugin is used
39 $this->_addHook('docmansearch_has_hierarchy'); // used by the search menu in docman
42 function CallHook($hookname, &$params) {
43 global $G_SESSION, $HTML;
46 case "display_hierarchy": {
47 if ($this->displayHierarchy()) {
52 case "hierarchy_views": {
54 require_once $gfplugins.$this->name.'/include/hierarchy_utils.php';
55 $group_id = $params[0];
56 $project = group_get_object($group_id);
57 if ($project->usesPlugin($this->name)) {
62 include($gfplugins.$this->name.'/view/'.$params[1].'_project_link.php');
73 case "group_delete": {
74 if ($params['group']->usesPlugin($this->name)) {
75 if ($this->remove($params['group_id'])) {
83 case "site_admin_option_hook": {
84 // Use this to provide a link to the site wide administrative pages for your plugin
85 echo '<li>'.$this->getAdminOptionLink().'</li>';
89 case "project_admin_plugins": {
90 // this displays the link in the project admin options page to it's administration
91 $group_id = $params['group_id'];
92 $group = group_get_object($group_id);
93 if ($group->usesPlugin($this->name)) {
95 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)')));
101 case "display_hierarchy_submenu": {
102 $globalConf = $this->getGlobalconf();
103 if ($globalConf['tree']) {
104 // Use to display a submenu in software map page if at least one project has a valid relationship
105 $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',
108 if (db_numrows($res1) > 0) {
109 $res2 = db_query_params('SELECT count(*) as used FROM plugin_projects_hierarchy_relationship where status = $1',
112 $row = db_fetch_array($res2);
114 $hierarchy_used = true;
118 if (isset($hierarchy_used)) {
119 $hierarMenuTitle[] = _('Per Category');
120 $hierarMenuTitle[] = _('Per Hierarchy');
121 $hierarMenuAttr[] = array('title' => _('Browse per category the available projects. Some projects might not appear here they do not choose any categories'), 'class' => 'tabtitle-nw');
122 $hierarMenuAttr[] = array('title' => _('Browse per hierarchy. Projects can share relationship between projects, as father and sons'), 'class' => 'tabtitle');
123 $hierarMenuUrl[] = '/softwaremap/trove_list.php?cat=c';
124 $hierarMenuUrl[] = '/softwaremap/trove_list.php?cat=h';
125 echo ($HTML->subMenu($hierarMenuTitle, $hierarMenuUrl, $hierarMenuAttr));
131 case "docmansearch_has_hierarchy": {
132 if ($params['includesubprojects']) {
133 $group_id = $params['group_id'];
134 $group = group_get_object($group_id);
135 if ($group->usesPlugin($this->name)) {
136 $arrayChilds= $this->getFamily($group_id, 'child', true, 'validated');
137 foreach ( $arrayChilds as $childId ) {
138 $params['qpa'] = db_construct_qpa($params['qpa'], ' OR group_id = $1', array($childId));
149 function displayHierarchy() {
150 $tree = $this->buildTree();
151 $displayTree = $this->affTree($tree, 0);
158 echo '<link rel="StyleSheet" href="/plugins/projects-hierarchy/dtree.css" type="text/css" />
159 <script type="text/javascript" src="/plugins/projects-hierarchy/dtree.js"></script>';
162 function buildTree() {
163 global $project_name;
164 $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
165 from groups as p1,groups as p2,plugin_projects_hierarchy_relationship
166 where p1.group_id=plugin_projects_hierarchy_relationship.project_id
167 and p2.group_id=plugin_projects_hierarchy_relationship.sub_project_id
168 and plugin_projects_hierarchy_relationship.status=$1
169 and (select tree from plugin_projects_hierarchy where project_id = p2.group_id)
170 order by father_name, son_name',
173 // construction du tableau associatif
174 // key = name of the father
175 // value = list of sons
177 while ($row = db_fetch_array($res)) {
178 if (forge_check_perm('project_read', $row['father_id']) && forge_check_perm('project_read', $row['son_id'])) {
179 $tree[$row['father_id']][] = $row['son_id'];
180 //get the unix name of the project
181 $project_name[$row['father_id']][0] = $row['father_name'];
182 $project_name[$row['son_id']][0] = $row['son_name'];
183 $project_name[$row['father_id']][1] = $row['father_unix_name'];
184 $project_name[$row['son_id']][1] = $row['son_unix_name'];
190 function affTree($tree, $lvl) {
191 global $project_name;
197 while (list($key, $sons) = each($tree)) {
198 // Really don't know why there is a warning there, and added @
199 if(@!$arbre[$key] != 0){
203 foreach ($sons as $son) {
204 $arbre[$son] = $cpt_pere;
208 if (sizeof($arbre)) {
209 $returnTree .= '<table ><tr><td>';
210 $returnTree .= '<script type="text/javascript">';
211 $returnTree .= 'd = new dTree(\'d\');';
212 $returnTree .= 'd.add(0,-1,\'Project Hierarchy Tree\');';
214 //construction automatique de l'arbre format : (num_fils, num_pere,nom,nom_unix)
215 while (list($key2, $sons2) = each($arbre)) {
216 $returnTree .= "d.add('".$key2."','".$sons2."','".$project_name[$key2][0]."','".util_make_url( '/projects/'.$project_name[$key2][1] .'/', $project_name[$key2][1] ) ."');";
219 $returnTree .= 'document.write(d);';
220 $returnTree .= '</script>';
221 $returnTree .= '</td></tr></table>';
227 * getFamily - find the children or parent group_id of this project.
229 * @param integer group_id to search for
230 * @param string parent or child ?
231 * @param boolean recurcive or not ?
232 * @param string validated or pending or any relation ?
233 * @return array array of arrays with group_id of parent or childs
236 function getFamily($group_id, $order, $deep = false, $status = 'any') {
237 $localFamily = array();
240 $statusCondition = 't';
245 $statusCondition = 'f';
255 $qpa = db_construct_qpa(false, 'SELECT project_id as id FROM plugin_projects_hierarchy_relationship
256 WHERE sub_project_id = $1', array($group_id));
257 if (isset($statusCondition))
258 $qpa = db_construct_qpa($qpa, ' AND status = $1', array($statusCondition));
260 $res = db_query_qpa($qpa);
264 $qpa = db_construct_qpa(false, 'SELECT sub_project_id as id FROM plugin_projects_hierarchy_relationship
265 WHERE project_id = $1', array($group_id));
266 if (isset($statusCondition))
267 $qpa = db_construct_qpa($qpa, ' AND status = $1', array($statusCondition));
269 $res = db_query_qpa($qpa);
277 if ($res && db_numrows($res) > 0) {
278 while ($arr = db_fetch_array($res)) {
279 $localFamily[] = $arr['id'];
284 $nextFamily = array();
285 for ( $i = 0; $i < count($localFamily); $i++) {
286 $nextFamily = $this->getFamily($localFamily[$i], $order, $deep, $status);
289 if (isset($nextFamily) && sizeof($nextFamily))
290 $localFamily = array_merge($localFamily, $nextFamily);
296 * getDocmanStatus - returns the docman status for this project
298 * @param integer group_id
299 * @return boolean true/false
302 function getDocmanStatus($group_id) {
303 $res = db_query_params('SELECT docman FROM plugin_projects_hierarchy WHERE project_id = $1 limit 1',
308 $resArr = db_fetch_array($res);
309 if ($resArr['docman'] == 't')
316 * setDocmanStatus - allow parent to browse your document manager and allow yourself to select your childs to be browsed.
318 * @param integer your groud_id
319 * @param boolean the status to set
320 * @return boolean success or not
322 function setDocmanStatus($group_id, $status = 0) {
323 $res = db_query_params('UPDATE plugin_projects_hierarchy set docman = $1 WHERE project_id = $2',
324 array($status, $group_id));
333 * redirect - encapsulate session_redirect to handle correctly the redirection URL
335 * @param string usually http_referer from $_SERVER
336 * @param string type of feedback : error, warning, feedback
337 * @param string the message of feedback
340 function redirect($http_referer, $type, $message) {
351 $url = util_find_relative_referer($http_referer);
352 if (strpos($url,'?')) {
353 session_redirect($url.'&'.$type.'='.urlencode($message));
355 session_redirect($url.'?'.$type.'='.urlencode($message));
359 * override default groupisactivecheckboxpost function for init value in db
361 function groupisactivecheckboxpost(&$params) {
362 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
363 $group = group_get_object($params['group']);
364 $flag = strtolower('use_'.$this->name);
365 if ( getIntFromRequest($flag) == 1 ) {
366 if ($this->add($group->getID())) {
367 $group->setPluginUse($this->name);
371 if ($this->remove($group->getID())) {
372 $group->setPluginUse($this->name, false);
380 * add - add a new group_id using the plugin
382 * @param integer group_id
383 * @return boolean true on success
386 function add($group_id) {
387 if (!$this->exists($group_id)) {
388 $globalConf = $this->getGlobalconf();
389 $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']));
397 * remove - remove group_id using the plugin
399 * @param integer group_id
400 * @return boolean true on success
403 function remove($group_id) {
404 if ($this->exists($group_id)) {
406 $res = db_query_params('DELETE FROM plugin_projects_hierarchy where project_id = $1', array($group_id));
412 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship where project_id = $1 OR sub_project_id = $1',
424 * addChild - add a new child to this project
426 * @param integer group_id
427 * @param integer sub_group_id
428 * @return boolean true on success
431 function addChild($project_id, $sub_project_id) {
432 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
433 if (!$this->hasRelation($project_id, $sub_project_id)) {
434 $res = db_query_params('INSERT INTO plugin_projects_hierarchy_relationship (project_id, sub_project_id)
435 VALUES ($1, $2)', array($project_id, $sub_project_id));
446 * removeChild - remove a child to this project
448 * @param integer group_id
449 * @param integer sub_group_id
450 * @return boolean true on success
453 function removeChild($project_id, $sub_project_id) {
454 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
455 if ($this->hasRelation($project_id, $sub_project_id)) {
456 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
457 WHERE project_id = $1 AND sub_project_id = $2',
458 array($project_id, $sub_project_id));
469 * removeParent - remove a parent to this project
471 * @param integer group_id
472 * @param integer sub_group_id
473 * @return boolean true on success
476 function removeParent($project_id, $sub_project_id) {
477 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
478 if ($this->hasRelation($project_id, $sub_project_id)) {
479 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
480 WHERE project_id = $1 AND sub_project_id = $2',
481 array($sub_project_id, $project_id));
492 * hasRelation - check if there is a relation child->parent or parent->child between 2 projects
494 * @param integer group_id
495 * @param integer sub_group_id
496 * @return boolean true on success
499 function hasRelation($project_id, $sub_project_id) {
500 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
501 $res = db_query_params('SELECT * FROM plugin_projects_hierarchy_relationship
502 WHERE ( project_id = $1 AND sub_project_id = $2 )
503 OR ( project_id = $2 AND sub_project_id = $1 )',
504 array($project_id, $sub_project_id));
505 if ($res && db_numrows($res) == 1 )
512 * validateRelationship - validate or reject a relation between 2 projects
514 * @param integer group_id
515 * @param integer sub_group_id
516 * @param string type of relation
517 * @param integer status of the relation
518 * @return boolean true on success
521 function validateRelationship($project_id, $sub_project_id, $relation, $status) {
522 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
523 if ($this->hasRelation($project_id, $sub_project_id)) {
525 $qpa = db_construct_qpa(false, 'UPDATE plugin_projects_hierarchy_relationship SET status = $1
526 WHERE ', array($status));
529 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
530 array($sub_project_id, $project_id));
534 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
535 array($project_id, $sub_project_id));
543 $res = db_query_qpa($qpa);
547 if (db_affected_rows($res))
550 $qpa = db_construct_qpa(false, 'DELETE FROM plugin_projects_hierarchy_relationship WHERE ');
553 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
554 array($project_id, $sub_project_id));
558 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
559 array($sub_project_id, $project_id));
567 $res = db_query_qpa($qpa);
571 if (db_affected_rows($res))
580 * exists - if this project use the plugin
582 * @param integer group_id
583 * @return boolean true on success
586 function exists($group_id) {
587 $res = db_query_params('SELECT project_id FROM plugin_projects_hierarchy WHERE project_id = $1', array($group_id));
591 if (db_numrows($res))
598 * getAdminOptionLink - return the admin link url
600 * @return string html url
603 function getAdminOptionLink() {
604 return util_make_link('/plugins/'.$this->name.'/?type=globaladmin',_('Global Hierarchy admin'), array('class'=>'tabtitle', 'title'=>_('Configure the projets-hierarchy plugin (docman, tree, delegate, globalconf features)')));
608 * getHeader - initialize header and js
609 * @param string type : user, project (aka group)
610 * @param array params
611 * @return bool success or not
613 function getHeader($type, $params=NULL) {
617 case 'globaladmin': {
618 session_require_global_perm('forge_admin');
620 require_once($gfwww.'admin/admin_utils.php');
621 site_admin_header(array('title'=>_('Site Global Hierarchy Admin'), 'toptab'=>''));
627 site_project_header($params);
636 * getGlobalAdminView - display the global configuration view
638 * @return boolean True
641 function getGlobalAdminView() {
642 global $gfplugins, $use_tooltips;
643 include $gfplugins.$this->name.'/view/admin/viewGlobalConfiguration.php';
648 * getGlobalconf - return the global configuration defined at forge level
650 * @return array the global configuration array
652 function getGlobalconf() {
653 $resGlobConf = db_query_params('SELECT * from plugin_projects_hierarchy_global',array());
658 $row = db_numrows($resGlobConf);
660 if ($row == null || count($row) > 2) {
664 $resArr = db_fetch_array($resGlobConf);
665 $returnArr = array();
667 foreach($resArr as $column => $value) {
669 $returnArr[$column] = true;
671 $returnArr[$column] = false;
679 * getConf - return the configuration defined at project level
681 * @param integer the group id
682 * @return array the configuration array
684 function getConf($project_id) {
685 $resConf = db_query_params('SELECT * from plugin_projects_hierarchy where project_id=$1',array($project_id));
690 $row = db_numrows($resConf);
692 if ($row == null || count($row) > 2) {
696 $resArr = db_fetch_array($resConf);
697 $returnArr = array();
699 foreach($resArr as $column => $value) {
701 $returnArr[$column] = true;
703 $returnArr[$column] = false;
711 function getProjectAdminView() {
712 global $gfplugins, $use_tooltips;
713 include $gfplugins.$this->name.'/view/admin/viewProjectConfiguration.php';
718 * getFooter - display footer
720 function getFooter($type) {
724 case 'globaladmin': {
725 session_require_global_perm('forge_admin');
726 site_admin_footer(array());
731 site_project_footer(array());
739 * updateGlobalConf - update the global configuration in database
741 * @param array configuration array (tree, docman, delegate)
742 * @return bool true on success
744 function updateGlobalConf($confArr) {
745 if (!isset($confArr['tree']) || !isset($confArr['docman']) || !isset($confArr['delegate']))
748 $res = db_query_params('truncate plugin_projects_hierarchy_global',array());
752 $res = db_query_params('insert into plugin_projects_hierarchy_global (tree, docman, delegate)
753 values ($1, $2, $3)',
766 * updateConf - update the configuration in database for this project
768 * @param array configuration array (tree, docman, delegate)
769 * @return bool true on success
771 function updateConf($project_id, $confArr) {
772 if (!isset($confArr['tree']) || !isset($confArr['docman']) || !isset($confArr['delegate']) || !isset($confArr['globalconf']))
775 $res = db_query_params('update plugin_projects_hierarchy
776 set (tree, docman, delegate, globalconf) = ($1, $2, $3, $4)
777 where project_id = $5',
781 $confArr['delegate'],
782 $confArr['globalconf'],
792 * son_box - display a select box for son selection
794 * @param integer group_id
796 * @param string selected value
797 * @return string html box
800 function son_box($group_id, $name, $selected = 'xzxzxz') {
801 $sons = $this->getFamily($group_id, 'child', true, 'any');
802 $parent = $this->getFamily($group_id, 'parent', true, 'any');
803 $family = array_merge($parent, $sons);
804 $son = db_query_params('SELECT group_id, group_name FROM groups
807 AND group_id <> ALL ($3)
808 AND group_id IN (select group_id from group_plugin,plugins where group_plugin.plugin_id = plugins.plugin_id and plugins.plugin_name = $4);',
811 db_int_array_to_any_clause($family),
813 return html_build_select_box($son, $name, $selected, false);
818 // c-file-style: "bsd"