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 $group_id = $params['group_id'];
129 $group = group_get_object($group_id);
130 if ($group->usesPlugin($this->name)) {
131 $qpa = $params['qpa'];
140 function displayHierarchy() {
141 $tree = $this->buildTree();
142 $displayTree = $this->affTree($tree, 0);
149 echo '<link rel="StyleSheet" href="/plugins/projects-hierarchy/dtree.css" type="text/css" />
150 <script type="text/javascript" src="/plugins/projects-hierarchy/dtree.js"></script>';
153 function buildTree() {
154 global $project_name;
155 $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
156 from groups as p1,groups as p2,plugin_projects_hierarchy_relationship
157 where p1.group_id=plugin_projects_hierarchy_relationship.project_id
158 and p2.group_id=plugin_projects_hierarchy_relationship.sub_project_id
159 and plugin_projects_hierarchy_relationship.status=$1
160 and (select tree from plugin_projects_hierarchy where project_id = p2.group_id)
161 order by father_name, son_name',
164 // construction du tableau associatif
165 // key = name of the father
166 // value = list of sons
168 while ($row = db_fetch_array($res)) {
169 if (forge_check_perm('project_read', $row['father_id'])) {
170 $tree[$row['father_id']][] = $row['son_id'];
171 //get the unix name of the project
172 $project_name[$row['father_id']][0] = $row['father_name'];
173 $project_name[$row['son_id']][0] = $row['son_name'];
174 $project_name[$row['father_id']][1] = $row['father_unix_name'];
175 $project_name[$row['son_id']][1] = $row['son_unix_name'];
181 function affTree($tree, $lvl) {
182 global $project_name;
188 while (list($key, $sons) = each($tree)) {
189 // Really don't know why there is a warning there, and added @
190 if(@!$arbre[$key] != 0){
194 foreach ($sons as $son) {
195 $arbre[$son] = $cpt_pere;
199 if (sizeof($arbre)) {
200 $returnTree .= '<table ><tr><td>';
201 $returnTree .= '<script type="text/javascript">';
202 $returnTree .= 'd = new dTree(\'d\');';
203 $returnTree .= 'd.add(0,-1,\'Project Hierarchy Tree\');';
205 //construction automatique de l'arbre format : (num_fils, num_pere,nom,nom_unix)
206 while (list($key2, $sons2) = each($arbre)) {
207 $returnTree .= "d.add('".$key2."','".$sons2."','".$project_name[$key2][0]."','".util_make_url( '/projects/'.$project_name[$key2][1] .'/', $project_name[$key2][1] ) ."');";
210 $returnTree .= 'document.write(d);';
211 $returnTree .= '</script>';
212 $returnTree .= '</td></tr></table>';
218 * getFamily - find the children or parent group_id of this project.
220 * @param integer group_id to search for
221 * @param string parent or child ?
222 * @param boolean recurcive or not ?
223 * @param string validated or pending or any relation ?
224 * @return array array of arrays with group_id of parent or childs
227 function getFamily($group_id, $order, $deep = false, $status = 'any') {
228 $localFamily = array();
231 $statusCondition = 't';
236 $statusCondition = 'f';
246 $qpa = db_construct_qpa(false, 'SELECT project_id as id FROM plugin_projects_hierarchy_relationship
247 WHERE sub_project_id = $1', array($group_id));
248 if (isset($statusCondition))
249 $qpa = db_construct_qpa($qpa, ' AND status = $1', array($statusCondition));
251 $res = db_query_qpa($qpa);
255 $qpa = db_construct_qpa(false, 'SELECT sub_project_id as id FROM plugin_projects_hierarchy_relationship
256 WHERE 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);
268 if ($res && db_numrows($res) > 0) {
269 while ($arr = db_fetch_array($res)) {
270 $localFamily[] = $arr['id'];
275 $nextFamily = array();
276 for ( $i = 0; $i < count($localFamily); $i++) {
277 $nextFamily = $this->getFamily($localFamily[$i], $order, $deep, $status);
280 if (isset($nextFamily) && sizeof($nextFamily))
281 $localFamily = array_merge($localFamily, $nextFamily);
287 * getDocmanStatus - returns the docman status for this project
289 * @param integer group_id
290 * @return boolean true/false
293 function getDocmanStatus($group_id) {
294 $res = db_query_params('SELECT docman FROM plugin_projects_hierarchy WHERE project_id = $1 limit 1',
299 $resArr = db_fetch_array($res);
300 if ($resArr['docman'] == 't')
307 * setDocmanStatus - allow parent to browse your document manager and allow yourself to select your childs to be browsed.
309 * @param integer your groud_id
310 * @param boolean the status to set
311 * @return boolean success or not
313 function setDocmanStatus($group_id, $status = 0) {
314 $res = db_query_params('UPDATE plugin_projects_hierarchy set docman = $1 WHERE project_id = $2',
315 array($status, $group_id));
324 * redirect - encapsulate session_redirect to handle correctly the redirection URL
326 * @param string usually http_referer from $_SERVER
327 * @param string type of feedback : error, warning, feedback
328 * @param string the message of feedback
331 function redirect($http_referer, $type, $message) {
342 $url = util_find_relative_referer($http_referer);
343 if (strpos($url,'?')) {
344 session_redirect($url.'&'.$type.'='.urlencode($message));
346 session_redirect($url.'?'.$type.'='.urlencode($message));
350 * override default groupisactivecheckboxpost function for init value in db
352 function groupisactivecheckboxpost(&$params) {
353 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
354 $group = group_get_object($params['group']);
355 $flag = strtolower('use_'.$this->name);
356 if ( getIntFromRequest($flag) == 1 ) {
357 if ($this->add($group->getID())) {
358 $group->setPluginUse($this->name);
362 if ($this->remove($group->getID())) {
363 $group->setPluginUse($this->name, false);
371 * add - add a new group_id using the plugin
373 * @param integer group_id
374 * @return boolean true on success
377 function add($group_id) {
378 if (!$this->exists($group_id)) {
379 $globalConf = $this->getGlobalconf();
380 $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']));
388 * remove - remove group_id using the plugin
390 * @param integer group_id
391 * @return boolean true on success
394 function remove($group_id) {
395 if ($this->exists($group_id)) {
397 $res = db_query_params('DELETE FROM plugin_projects_hierarchy where project_id = $1', array($group_id));
403 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship where project_id = $1 OR sub_project_id = $1',
415 * addChild - add a new child to this project
417 * @param integer group_id
418 * @param integer sub_group_id
419 * @return boolean true on success
422 function addChild($project_id, $sub_project_id) {
423 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
424 if (!$this->hasRelation($project_id, $sub_project_id)) {
425 $res = db_query_params('INSERT INTO plugin_projects_hierarchy_relationship (project_id, sub_project_id)
426 VALUES ($1, $2)', array($project_id, $sub_project_id));
437 * removeChild - remove a child to this project
439 * @param integer group_id
440 * @param integer sub_group_id
441 * @return boolean true on success
444 function removeChild($project_id, $sub_project_id) {
445 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
446 if ($this->hasRelation($project_id, $sub_project_id)) {
447 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
448 WHERE project_id = $1 AND sub_project_id = $2',
449 array($project_id, $sub_project_id));
460 * removeParent - remove a parent to this project
462 * @param integer group_id
463 * @param integer sub_group_id
464 * @return boolean true on success
467 function removeParent($project_id, $sub_project_id) {
468 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
469 if ($this->hasRelation($project_id, $sub_project_id)) {
470 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
471 WHERE project_id = $1 AND sub_project_id = $2',
472 array($sub_project_id, $project_id));
483 * hasRelation - check if there is a relation child->parent or parent->child between 2 projects
485 * @param integer group_id
486 * @param integer sub_group_id
487 * @return boolean true on success
490 function hasRelation($project_id, $sub_project_id) {
491 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
492 $res = db_query_params('SELECT * FROM plugin_projects_hierarchy_relationship
493 WHERE ( project_id = $1 AND sub_project_id = $2 )
494 OR ( project_id = $2 AND sub_project_id = $1 )',
495 array($project_id, $sub_project_id));
496 if ($res && db_numrows($res) == 1 )
503 * validateRelationship - validate or reject a relation between 2 projects
505 * @param integer group_id
506 * @param integer sub_group_id
507 * @param string type of relation
508 * @param integer status of the relation
509 * @return boolean true on success
512 function validateRelationship($project_id, $sub_project_id, $relation, $status) {
513 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
514 if ($this->hasRelation($project_id, $sub_project_id)) {
516 $qpa = db_construct_qpa(false, 'UPDATE plugin_projects_hierarchy_relationship SET status = $1
517 WHERE ', array($status));
520 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
521 array($sub_project_id, $project_id));
525 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
526 array($project_id, $sub_project_id));
534 $res = db_query_qpa($qpa);
538 if (db_affected_rows($res))
541 $qpa = db_construct_qpa(false, 'DELETE FROM plugin_projects_hierarchy_relationship WHERE ');
544 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
545 array($project_id, $sub_project_id));
549 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
550 array($sub_project_id, $project_id));
558 $res = db_query_qpa($qpa);
562 if (db_affected_rows($res))
571 * exists - if this project use the plugin
573 * @param integer group_id
574 * @return boolean true on success
577 function exists($group_id) {
578 $res = db_query_params('SELECT project_id FROM plugin_projects_hierarchy WHERE project_id = $1', array($group_id));
582 if (db_numrows($res))
589 * getAdminOptionLink - return the admin link url
591 * @return string html url
594 function getAdminOptionLink() {
595 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)')));
599 * getHeader - initialize header and js
600 * @param string type : user, project (aka group)
601 * @param array params
602 * @return bool success or not
604 function getHeader($type, $params=NULL) {
608 case 'globaladmin': {
609 session_require_global_perm('forge_admin');
611 require_once($gfwww.'admin/admin_utils.php');
612 site_admin_header(array('title'=>_('Site Global Hierarchy Admin'), 'toptab'=>''));
618 site_project_header($params);
627 * getGlobalAdminView - display the global configuration view
629 * @return boolean True
632 function getGlobalAdminView() {
633 global $gfplugins, $use_tooltips;
634 include $gfplugins.$this->name.'/view/admin/viewGlobalConfiguration.php';
639 * getGlobalconf - return the global configuration defined at forge level
641 * @return array the global configuration array
643 function getGlobalconf() {
644 $resGlobConf = db_query_params('SELECT * from plugin_projects_hierarchy_global',array());
649 $row = db_numrows($resGlobConf);
651 if ($row == null || count($row) > 2) {
655 $resArr = db_fetch_array($resGlobConf);
656 $returnArr = array();
658 foreach($resArr as $column => $value) {
660 $returnArr[$column] = true;
662 $returnArr[$column] = false;
670 * getConf - return the configuration defined at project level
672 * @param integer the group id
673 * @return array the configuration array
675 function getConf($project_id) {
676 $resConf = db_query_params('SELECT * from plugin_projects_hierarchy where project_id=$1',array($project_id));
681 $row = db_numrows($resConf);
683 if ($row == null || count($row) > 2) {
687 $resArr = db_fetch_array($resConf);
688 $returnArr = array();
690 foreach($resArr as $column => $value) {
692 $returnArr[$column] = true;
694 $returnArr[$column] = false;
702 function getProjectAdminView() {
703 global $gfplugins, $use_tooltips;
704 include $gfplugins.$this->name.'/view/admin/viewProjectConfiguration.php';
709 * getFooter - display footer
711 function getFooter($type) {
715 case 'globaladmin': {
716 session_require_global_perm('forge_admin');
717 site_admin_footer(array());
722 site_project_footer(array());
730 * updateGlobalConf - update the global configuration in database
732 * @param array configuration array (tree, docman, delegate)
733 * @return bool true on success
735 function updateGlobalConf($confArr) {
736 if (!isset($confArr['tree']) || !isset($confArr['docman']) || !isset($confArr['delegate']))
739 $res = db_query_params('truncate plugin_projects_hierarchy_global',array());
743 $res = db_query_params('insert into plugin_projects_hierarchy_global (tree, docman, delegate)
744 values ($1, $2, $3)',
757 * updateConf - update the configuration in database for this project
759 * @param array configuration array (tree, docman, delegate)
760 * @return bool true on success
762 function updateConf($project_id, $confArr) {
763 if (!isset($confArr['tree']) || !isset($confArr['docman']) || !isset($confArr['delegate']) || !isset($confArr['globalconf']))
766 $res = db_query_params('update plugin_projects_hierarchy
767 set (tree, docman, delegate, globalconf) = ($1, $2, $3, $4)
768 where project_id = $5',
772 $confArr['delegate'],
773 $confArr['globalconf'],
783 * son_box - display a select box for son selection
785 * @param integer group_id
787 * @param string selected value
788 * @return string html box
791 function son_box($group_id, $name, $selected = 'xzxzxz') {
792 $sons = $this->getFamily($group_id, 'child', true, 'any');
793 $parent = $this->getFamily($group_id, 'parent', true, 'any');
794 $family = array_merge($parent, $sons);
795 $son = db_query_params('SELECT group_id, group_name FROM groups
798 AND group_id <> ALL ($3)
799 AND group_id IN (select group_id from group_plugin,plugins where group_plugin.plugin_id = plugins.plugin_id and plugins.plugin_name = $4);',
802 db_int_array_to_any_clause($family),
804 return html_build_select_box($son, $name, $selected, false);
809 // c-file-style: "bsd"