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
38 $this->_addHook('docmansearch_has_hierarchy'); // used by the search menu in docman
41 function CallHook($hookname, &$params) {
42 global $G_SESSION, $HTML;
45 case "display_hierarchy": {
46 if ($this->displayHierarchy()) {
51 case "hierarchy_views": {
53 require_once $gfplugins.$this->name.'/include/hierarchy_utils.php';
54 $group_id = $params[0];
55 $project = group_get_object($group_id);
56 if ($project->usesPlugin($this->name)) {
61 include($gfplugins.$this->name.'/view/'.$params[1].'_project_link.php');
72 case "group_delete": {
73 if ($params['group']->usesPlugins($this->name)) {
74 if ($this->remove($params['group_id'])) {
82 case "site_admin_option_hook": {
83 // Use this to provide a link to the site wide administrative pages for your plugin
84 echo '<li>'.$this->getAdminOptionLink().'</li>';
88 case "project_admin_plugins": {
89 // this displays the link in the project admin options page to it's administration
90 $group_id = $params['group_id'];
91 $group = group_get_object($group_id);
92 if ($group->usesPlugin($this->name)) {
94 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)')));
100 case "display_hierarchy_submenu": {
101 // Use to display a submenu in software map page if at least one project has a valid relationship
102 $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',
105 if (db_numrows($res1) > 0) {
106 $res2 = db_query_params('SELECT count(*) as used FROM plugin_projects_hierarchy_relationship where status = $1',
109 $row = db_fetch_array($res2);
111 $hierarchy_used = true;
115 if (isset($hierarchy_used)) {
116 $hierarMenuTitle[] = _('Per Category');
117 $hierarMenuTitle[] = _('Per Hierarchy');
118 $hierarMenuAttr[] = array('title' => _('Browse per category the available projects. Some projects might not appear here they do not choose any categories'), 'class' => 'tabtitle-nw');
119 $hierarMenuAttr[] = array('title' => _('Browse per hierarchy. Projects can share relationship between projects, as father and sons'), 'class' => 'tabtitle');
120 $hierarMenuUrl[] = '/softwaremap/trove_list.php?cat=c';
121 $hierarMenuUrl[] = '/softwaremap/trove_list.php?cat=h';
122 echo ($HTML->subMenu($hierarMenuTitle, $hierarMenuUrl, $hierarMenuAttr));
127 case "docmansearch_has_hierarchy": {
128 if ($params['includesubprojects']) {
129 $group_id = $params['group_id'];
130 $group = group_get_object($group_id);
131 if ($group->usesPlugin($this->name)) {
132 $arrayChilds= $this->getFamily($group_id, 'child', true, 'validated');
133 foreach ( $arrayChilds as $childId ) {
134 $params['qpa'] = db_construct_qpa($params['qpa'], ' OR group_id = $1', array($childId));
145 function displayHierarchy() {
146 $tree = $this->buildTree();
147 $displayTree = $this->affTree($tree, 0);
154 echo '<link rel="StyleSheet" href="/plugins/projects-hierarchy/dtree.css" type="text/css" />
155 <script type="text/javascript" src="/plugins/projects-hierarchy/dtree.js"></script>';
158 function buildTree() {
159 global $project_name;
160 $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
161 from groups as p1,groups as p2,plugin_projects_hierarchy_relationship
162 where p1.group_id=plugin_projects_hierarchy_relationship.project_id
163 and p2.group_id=plugin_projects_hierarchy_relationship.sub_project_id
164 and plugin_projects_hierarchy_relationship.status=$1
165 and (select tree from plugin_projects_hierarchy where project_id = p2.group_id)
166 order by father_name, son_name',
169 // construction du tableau associatif
170 // key = name of the father
171 // value = list of sons
173 while ($row = db_fetch_array($res)) {
174 if (forge_check_perm('project_read', $row['father_id'])) {
175 $tree[$row['father_id']][] = $row['son_id'];
176 //get the unix name of the project
177 $project_name[$row['father_id']][0] = $row['father_name'];
178 $project_name[$row['son_id']][0] = $row['son_name'];
179 $project_name[$row['father_id']][1] = $row['father_unix_name'];
180 $project_name[$row['son_id']][1] = $row['son_unix_name'];
186 function affTree($tree, $lvl) {
187 global $project_name;
193 while (list($key, $sons) = each($tree)) {
194 // Really don't know why there is a warning there, and added @
195 if(@!$arbre[$key] != 0){
199 foreach ($sons as $son) {
200 $arbre[$son] = $cpt_pere;
204 if (sizeof($arbre)) {
205 $returnTree .= '<table ><tr><td>';
206 $returnTree .= '<script type="text/javascript">';
207 $returnTree .= 'd = new dTree(\'d\');';
208 $returnTree .= 'd.add(0,-1,\'Project Hierarchy Tree\');';
210 //construction automatique de l'arbre format : (num_fils, num_pere,nom,nom_unix)
211 while (list($key2, $sons2) = each($arbre)) {
212 $returnTree .= "d.add('".$key2."','".$sons2."','".$project_name[$key2][0]."','".util_make_url( '/projects/'.$project_name[$key2][1] .'/', $project_name[$key2][1] ) ."');";
215 $returnTree .= 'document.write(d);';
216 $returnTree .= '</script>';
217 $returnTree .= '</td></tr></table>';
223 * getFamily - find the children or parent group_id of this project.
225 * @param integer group_id to search for
226 * @param string parent or child ?
227 * @param boolean recurcive or not ?
228 * @param string validated or pending or any relation ?
229 * @return array array of arrays with group_id of parent or childs
232 function getFamily($group_id, $order, $deep = false, $status = 'any') {
233 $localFamily = array();
236 $statusCondition = 't';
241 $statusCondition = 'f';
251 $qpa = db_construct_qpa(false, 'SELECT project_id as id FROM plugin_projects_hierarchy_relationship
252 WHERE sub_project_id = $1', array($group_id));
253 if (isset($statusCondition))
254 $qpa = db_construct_qpa($qpa, ' AND status = $1', array($statusCondition));
256 $res = db_query_qpa($qpa);
260 $qpa = db_construct_qpa(false, 'SELECT sub_project_id as id FROM plugin_projects_hierarchy_relationship
261 WHERE project_id = $1', array($group_id));
262 if (isset($statusCondition))
263 $qpa = db_construct_qpa($qpa, ' AND status = $1', array($statusCondition));
265 $res = db_query_qpa($qpa);
273 if ($res && db_numrows($res) > 0) {
274 while ($arr = db_fetch_array($res)) {
275 $localFamily[] = $arr['id'];
280 $nextFamily = array();
281 for ( $i = 0; $i < count($localFamily); $i++) {
282 $nextFamily = $this->getFamily($localFamily[$i], $order, $deep, $status);
285 if (isset($nextFamily) && sizeof($nextFamily))
286 $localFamily = array_merge($localFamily, $nextFamily);
292 * getDocmanStatus - returns the docman status for this project
294 * @param integer group_id
295 * @return boolean true/false
298 function getDocmanStatus($group_id) {
299 $res = db_query_params('SELECT docman FROM plugin_projects_hierarchy WHERE project_id = $1 limit 1',
304 $resArr = db_fetch_array($res);
305 if ($resArr['docman'] == 't')
312 * setDocmanStatus - allow parent to browse your document manager and allow yourself to select your childs to be browsed.
314 * @param integer your groud_id
315 * @param boolean the status to set
316 * @return boolean success or not
318 function setDocmanStatus($group_id, $status = 0) {
319 $res = db_query_params('UPDATE plugin_projects_hierarchy set docman = $1 WHERE project_id = $2',
320 array($status, $group_id));
329 * redirect - encapsulate session_redirect to handle correctly the redirection URL
331 * @param string usually http_referer from $_SERVER
332 * @param string type of feedback : error, warning, feedback
333 * @param string the message of feedback
336 function redirect($http_referer, $type, $message) {
347 $url = util_find_relative_referer($http_referer);
348 if (strpos($url,'?')) {
349 session_redirect($url.'&'.$type.'='.urlencode($message));
351 session_redirect($url.'?'.$type.'='.urlencode($message));
355 * override default groupisactivecheckboxpost function for init value in db
357 function groupisactivecheckboxpost(&$params) {
358 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
359 $group = group_get_object($params['group']);
360 $flag = strtolower('use_'.$this->name);
361 if ( getIntFromRequest($flag) == 1 ) {
362 if ($this->add($group->getID())) {
363 $group->setPluginUse($this->name);
367 if ($this->remove($group->getID())) {
368 $group->setPluginUse($this->name, false);
376 * add - add a new group_id using the plugin
378 * @param integer group_id
379 * @return boolean true on success
382 function add($group_id) {
383 if (!$this->exists($group_id)) {
384 $globalConf = $this->getGlobalconf();
385 $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']));
393 * remove - remove group_id using the plugin
395 * @param integer group_id
396 * @return boolean true on success
399 function remove($group_id) {
400 if ($this->exists($group_id)) {
402 $res = db_query_params('DELETE FROM plugin_projects_hierarchy where project_id = $1', array($group_id));
408 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship where project_id = $1 OR sub_project_id = $1',
420 * addChild - add a new child to this project
422 * @param integer group_id
423 * @param integer sub_group_id
424 * @return boolean true on success
427 function addChild($project_id, $sub_project_id) {
428 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
429 if (!$this->hasRelation($project_id, $sub_project_id)) {
430 $res = db_query_params('INSERT INTO plugin_projects_hierarchy_relationship (project_id, sub_project_id)
431 VALUES ($1, $2)', array($project_id, $sub_project_id));
442 * removeChild - remove a child to this project
444 * @param integer group_id
445 * @param integer sub_group_id
446 * @return boolean true on success
449 function removeChild($project_id, $sub_project_id) {
450 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
451 if ($this->hasRelation($project_id, $sub_project_id)) {
452 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
453 WHERE project_id = $1 AND sub_project_id = $2',
454 array($project_id, $sub_project_id));
465 * removeParent - remove a parent to this project
467 * @param integer group_id
468 * @param integer sub_group_id
469 * @return boolean true on success
472 function removeParent($project_id, $sub_project_id) {
473 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
474 if ($this->hasRelation($project_id, $sub_project_id)) {
475 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
476 WHERE project_id = $1 AND sub_project_id = $2',
477 array($sub_project_id, $project_id));
488 * hasRelation - check if there is a relation child->parent or parent->child between 2 projects
490 * @param integer group_id
491 * @param integer sub_group_id
492 * @return boolean true on success
495 function hasRelation($project_id, $sub_project_id) {
496 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
497 $res = db_query_params('SELECT * FROM plugin_projects_hierarchy_relationship
498 WHERE ( project_id = $1 AND sub_project_id = $2 )
499 OR ( project_id = $2 AND sub_project_id = $1 )',
500 array($project_id, $sub_project_id));
501 if ($res && db_numrows($res) == 1 )
508 * validateRelationship - validate or reject a relation between 2 projects
510 * @param integer group_id
511 * @param integer sub_group_id
512 * @param string type of relation
513 * @param integer status of the relation
514 * @return boolean true on success
517 function validateRelationship($project_id, $sub_project_id, $relation, $status) {
518 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
519 if ($this->hasRelation($project_id, $sub_project_id)) {
521 $qpa = db_construct_qpa(false, 'UPDATE plugin_projects_hierarchy_relationship SET status = $1
522 WHERE ', array($status));
525 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
526 array($sub_project_id, $project_id));
530 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
531 array($project_id, $sub_project_id));
539 $res = db_query_qpa($qpa);
543 if (db_affected_rows($res))
546 $qpa = db_construct_qpa(false, 'DELETE FROM plugin_projects_hierarchy_relationship WHERE ');
549 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
550 array($project_id, $sub_project_id));
554 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
555 array($sub_project_id, $project_id));
563 $res = db_query_qpa($qpa);
567 if (db_affected_rows($res))
576 * exists - if this project use the plugin
578 * @param integer group_id
579 * @return boolean true on success
582 function exists($group_id) {
583 $res = db_query_params('SELECT project_id FROM plugin_projects_hierarchy WHERE project_id = $1', array($group_id));
587 if (db_numrows($res))
594 * getAdminOptionLink - return the admin link url
596 * @return string html url
599 function getAdminOptionLink() {
600 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)')));
604 * getHeader - initialize header and js
605 * @param string type : user, project (aka group)
606 * @param array params
607 * @return bool success or not
609 function getHeader($type, $params=NULL) {
613 case 'globaladmin': {
614 session_require_global_perm('forge_admin');
616 require_once($gfwww.'admin/admin_utils.php');
617 site_admin_header(array('title'=>_('Site Global Hierarchy Admin'), 'toptab'=>''));
623 site_project_header($params);
632 * getGlobalAdminView - display the global configuration view
634 * @return boolean True
637 function getGlobalAdminView() {
638 global $gfplugins, $use_tooltips;
639 include $gfplugins.$this->name.'/view/admin/viewGlobalConfiguration.php';
644 * getGlobalconf - return the global configuration defined at forge level
646 * @return array the global configuration array
648 function getGlobalconf() {
649 $resGlobConf = db_query_params('SELECT * from plugin_projects_hierarchy_global',array());
654 $row = db_numrows($resGlobConf);
656 if ($row == null || count($row) > 2) {
660 $resArr = db_fetch_array($resGlobConf);
661 $returnArr = array();
663 foreach($resArr as $column => $value) {
665 $returnArr[$column] = true;
667 $returnArr[$column] = false;
675 * getConf - return the configuration defined at project level
677 * @param integer the group id
678 * @return array the configuration array
680 function getConf($project_id) {
681 $resConf = db_query_params('SELECT * from plugin_projects_hierarchy where project_id=$1',array($project_id));
686 $row = db_numrows($resConf);
688 if ($row == null || count($row) > 2) {
692 $resArr = db_fetch_array($resConf);
693 $returnArr = array();
695 foreach($resArr as $column => $value) {
697 $returnArr[$column] = true;
699 $returnArr[$column] = false;
707 function getProjectAdminView() {
708 global $gfplugins, $use_tooltips;
709 include $gfplugins.$this->name.'/view/admin/viewProjectConfiguration.php';
714 * getFooter - display footer
716 function getFooter($type) {
720 case 'globaladmin': {
721 session_require_global_perm('forge_admin');
722 site_admin_footer(array());
727 site_project_footer(array());
735 * updateGlobalConf - update the global configuration in database
737 * @param array configuration array (tree, docman, delegate)
738 * @return bool true on success
740 function updateGlobalConf($confArr) {
741 if (!isset($confArr['tree']) || !isset($confArr['docman']) || !isset($confArr['delegate']))
744 $res = db_query_params('truncate plugin_projects_hierarchy_global',array());
748 $res = db_query_params('insert into plugin_projects_hierarchy_global (tree, docman, delegate)
749 values ($1, $2, $3)',
762 * updateConf - update the configuration in database for this project
764 * @param array configuration array (tree, docman, delegate)
765 * @return bool true on success
767 function updateConf($project_id, $confArr) {
768 if (!isset($confArr['tree']) || !isset($confArr['docman']) || !isset($confArr['delegate']) || !isset($confArr['globalconf']))
771 $res = db_query_params('update plugin_projects_hierarchy
772 set (tree, docman, delegate, globalconf) = ($1, $2, $3, $4)
773 where project_id = $5',
777 $confArr['delegate'],
778 $confArr['globalconf'],
788 * son_box - display a select box for son selection
790 * @param integer group_id
792 * @param string selected value
793 * @return string html box
796 function son_box($group_id, $name, $selected = 'xzxzxz') {
797 $sons = $this->getFamily($group_id, 'child', true, 'any');
798 $parent = $this->getFamily($group_id, 'parent', true, 'any');
799 $family = array_merge($parent, $sons);
800 $son = db_query_params('SELECT group_id, group_name FROM groups
803 AND group_id <> ALL ($3)
804 AND group_id IN (select group_id from group_plugin,plugins where group_plugin.plugin_id = plugins.plugin_id and plugins.plugin_name = $4);',
807 db_int_array_to_any_clause($family),
809 return html_build_select_box($son, $name, $selected, false);
814 // c-file-style: "bsd"