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');
39 function CallHook($hookname, &$params) {
40 global $G_SESSION, $HTML;
43 case "display_hierarchy": {
44 if ($this->displayHierarchy()) {
49 case "hierarchy_views": {
51 require_once $gfplugins.$this->name.'/include/hierarchy_utils.php';
52 $group_id = $params[0];
53 $project = group_get_object($group_id);
54 if ($project->usesPlugin($this->name)) {
59 include($gfplugins.$this->name.'/view/'.$params[1].'_project_link.php');
70 case "group_delete": {
71 if ($params['group']->usesPlugins($this->name)) {
72 if ($this->remove($params['group_id'])) {
80 case "site_admin_option_hook": {
81 echo '<li>'.$this->getAdminOptionLink().'</li>';
85 case "project_admin_plugins": {
86 // this displays the link in the project admin options page to it's administration
87 $group_id = $params['group_id'];
88 $group = group_get_object($group_id);
89 if ($group->usesPlugin($this->name)) {
91 echo util_make_link('/plugins/'.$this->name.'/?group_id='.$group_id.'&type=admin&pluginname='.$this->name, _('Hierarchy Admin'));
101 function displayHierarchy() {
102 $tree = $this->buildTree();
103 $displayTree = $this->affTree($tree, 0);
110 echo '<link rel="StyleSheet" href="/plugins/projects_hierarchy/dtree.css" type="text/css" />
111 <script type="text/javascript" src="/plugins/projects_hierarchy/dtree.js"></script>';
114 function buildTree() {
115 global $project_name;
116 $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
117 from groups as p1,groups as p2,plugin_projects_hierarchy_relationship
118 where p1.group_id=plugin_projects_hierarchy_relationship.project_id
119 and p2.group_id=plugin_projects_hierarchy_relationship.sub_project_id
120 and plugin_projects_hierarchy_relationship.status=$1
121 order by father_name, son_name',
124 // construction du tableau associatif
125 // key = name of the father
126 // value = list of sons
128 while ($row = db_fetch_array($res)) {
129 if (forge_check_perm('project_read', $row['father_id'])) {
130 $tree[$row['father_id']][] = $row['son_id'];
131 //get the unix name of the project
132 $project_name[$row['father_id']][0] = $row['father_name'];
133 $project_name[$row['son_id']][0] = $row['son_name'];
134 $project_name[$row['father_id']][1] = $row['father_unix_name'];
135 $project_name[$row['son_id']][1] = $row['son_unix_name'];
141 function affTree($tree, $lvl) {
142 global $project_name;
148 while (list($key, $sons) = each($tree)) {
149 // Really don't know why there is a warning there, and added @
150 if(@!$arbre[$key] != 0){
154 foreach ($sons as $son) {
155 $arbre[$son] = $cpt_pere;
159 if (sizeof($arbre)) {
160 $returnTree .= '<table ><tr><td>';
161 $returnTree .= '<script type="text/javascript">';
162 $returnTree .= 'd = new dTree(\'d\');';
163 $returnTree .= 'd.add(0,-1,\'Project Tree\');';
165 //construction automatique de l'arbre format : (num_fils, num_pere,nom,nom_unix)
166 while (list($key2, $sons2) = each($arbre)) {
167 $returnTree .= "d.add('".$key2."','".$sons2."','".$project_name[$key2][0]."','".util_make_url( '/projects/'.$project_name[$key2][1] .'/', $project_name[$key2][1] ) ."');";
170 $returnTree .= 'document.write(d);';
171 $returnTree .= '</script>';
172 $returnTree .= '</td></tr></table>';
178 * getFamily - find the children or parent group_id of this project.
180 * @param integer group_id to search for
181 * @param string parent or child ?
182 * @param boolean recurcive or not ?
183 * @param string validated or pending or any relation ?
184 * @return array array of arrays with group_id of parent or childs
187 function getFamily($group_id, $order, $deep = false, $status = 'any') {
188 $localFamily = array();
191 $statusCondition = 't';
196 $statusCondition = 'f';
206 $qpa = db_construct_qpa(false, 'SELECT project_id as id FROM plugin_projects_hierarchy_relationship
207 WHERE sub_project_id = $1', array($group_id));
208 if (isset($statusCondition))
209 $qpa = db_construct_qpa($qpa, ' AND status = $1', array($statusCondition));
211 $res = db_query_qpa($qpa);
215 $qpa = db_construct_qpa(false, 'SELECT sub_project_id as id FROM plugin_projects_hierarchy_relationship
216 WHERE project_id = $1', array($group_id));
217 if (isset($statusCondition))
218 $qpa = db_construct_qpa($qpa, ' AND status = $1', array($statusCondition));
220 $res = db_query_qpa($qpa);
228 if ($res && db_numrows($res) > 0) {
229 while ($arr = db_fetch_array($res)) {
230 $localFamily[] = $arr['id'];
235 $nextFamily = array();
236 for ( $i = 0; $i < count($localFamily); $i++) {
237 $nextFamily = $this->getFamily($localFamily[$i], $order, $deep, $status);
240 if (isset($nextFamily) && sizeof($nextFamily))
241 $localFamily = array_merge($localFamily, $nextFamily);
247 * getDocmanStatus - returns the docman status for this project
249 * @param integer group_id
250 * @return boolean true/false
253 function getDocmanStatus($group_id) {
254 $res = db_query_params('SELECT docman FROM plugin_projects_hierarchy WHERE project_id = $1 limit 1',
259 $resArr = db_fetch_array($res);
260 if ($resArr['docman'] == 't')
267 * setDocmanStatus - allow parent to browse your document manager and allow yourself to select your childs to be browsed.
269 * @param integer your groud_id
270 * @param boolean the status to set
271 * @return boolean success or not
273 function setDocmanStatus($group_id, $status = 0) {
274 $res = db_query_params('UPDATE plugin_projects_hierarchy set docman = $1 WHERE project_id = $2',
275 array($status, $group_id));
284 * redirect - encapsulate session_redirect to handle correctly the redirection URL
286 * @param string usually http_referer from $_SERVER
287 * @param string type of feedback : error, warning, feedback
288 * @param string the message of feedback
291 function redirect($http_referer, $type, $message) {
302 $url = util_find_relative_referer($http_referer);
303 if (strpos($url,'?')) {
304 session_redirect($url.'&'.$type.'='.urlencode($message));
306 session_redirect($url.'?'.$type.'='.urlencode($message));
310 * override default groupisactivecheckboxpost function for init value in db
312 function groupisactivecheckboxpost(&$params) {
313 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
314 $group = group_get_object($params['group']);
315 $flag = strtolower('use_'.$this->name);
316 if ( getIntFromRequest($flag) == 1 ) {
317 if ($this->add($group->getID())) {
318 $group->setPluginUse($this->name);
322 if ($this->remove($group->getID())) {
323 $group->setPluginUse($this->name, false);
331 * add - add a new group_id using the plugin
333 * @param integer group_id
334 * @return boolean true on success
337 function add($group_id) {
338 if (!$this->exists($group_id)) {
339 $globalConf = $this->getGlobalconf();
340 $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']));
348 * remove - remove group_id using the plugin
350 * @param integer group_id
351 * @return boolean true on success
354 function remove($group_id) {
355 if ($this->exists($group_id)) {
357 $res = db_query_params('DELETE FROM plugin_projects_hierarchy where project_id = $1', array($group_id));
363 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship where project_id = $1 OR sub_project_id = $1',
375 * addChild - add a new child to this project
377 * @param integer group_id
378 * @param integer sub_group_id
379 * @return boolean true on success
382 function addChild($project_id, $sub_project_id) {
383 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
384 if (!$this->hasRelation($project_id, $sub_project_id)) {
385 $res = db_query_params('INSERT INTO plugin_projects_hierarchy_relationship (project_id, sub_project_id)
386 VALUES ($1, $2)', array($project_id, $sub_project_id));
397 * removeChild - remove a child to this project
399 * @param integer group_id
400 * @param integer sub_group_id
401 * @return boolean true on success
404 function removeChild($project_id, $sub_project_id) {
405 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
406 if ($this->hasRelation($project_id, $sub_project_id)) {
407 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
408 WHERE project_id = $1 AND sub_project_id = $2',
409 array($project_id, $sub_project_id));
420 * removeParent - remove a parent to this project
422 * @param integer group_id
423 * @param integer sub_group_id
424 * @return boolean true on success
427 function removeParent($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('DELETE FROM plugin_projects_hierarchy_relationship
431 WHERE project_id = $1 AND sub_project_id = $2',
432 array($sub_project_id, $project_id));
443 * hasRelation - check if there is a relation child->parent or parent->child between 2 projects
445 * @param integer group_id
446 * @param integer sub_group_id
447 * @return boolean true on success
450 function hasRelation($project_id, $sub_project_id) {
451 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
452 $res = db_query_params('SELECT * FROM plugin_projects_hierarchy_relationship
453 WHERE ( project_id = $1 AND sub_project_id = $2 )
454 OR ( project_id = $2 AND sub_project_id = $1 )',
455 array($project_id, $sub_project_id));
456 if ($res && db_numrows($res) == 1 )
463 * validateRelationship - validate or reject a relation between 2 projects
465 * @param integer group_id
466 * @param integer sub_group_id
467 * @param string type of relation
468 * @param integer status of the relation
469 * @return boolean true on success
472 function validateRelationship($project_id, $sub_project_id, $relation, $status) {
473 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
474 if ($this->hasRelation($project_id, $sub_project_id)) {
476 $qpa = db_construct_qpa(false, 'UPDATE plugin_projects_hierarchy_relationship SET status = $1
477 WHERE ', array($status));
480 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
481 array($sub_project_id, $project_id));
485 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
486 array($project_id, $sub_project_id));
494 $res = db_query_qpa($qpa);
498 if (db_affected_rows($res))
501 $qpa = db_construct_qpa(false, 'DELETE FROM plugin_projects_hierarchy_relationship WHERE ',
505 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
506 array($project_id, $sub_project_id));
510 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
511 array($sub_project_id, $project_id));
519 $res = db_query_qpa($qpa);
523 if (db_affected_rows($res))
532 * exists - if this project use the plugin
534 * @param integer group_id
535 * @return boolean true on success
538 function exists($group_id) {
539 $res = db_query_params('SELECT project_id FROM plugin_projects_hierarchy WHERE project_id = $1', array($group_id));
543 if (db_numrows($res))
550 * getAdminOptionLink - return the admin link url
552 * @return string html url
555 function getAdminOptionLink() {
556 return util_make_link('/plugins/'.$this->name.'/?type=globaladmin&pluginname='.$this->name,_('Global Hierarchy admin'));
560 * getHeader - initialize header and js
561 * @param string type : user, project (aka group)
562 * @return bool success or not
564 function getHeader($type) {
568 case 'globaladmin': {
569 session_require_global_perm('forge_admin');
571 require_once($gfwww.'admin/admin_utils.php');
572 site_admin_header(array('title'=>_('Site Global Hierarchy Admin'), 'toptab'=>''));
578 site_project_header();
587 * getGlobalAdminView - display the global configuration view
589 * @return boolean True
592 function getGlobalAdminView() {
593 global $gfplugins, $use_tooltips;
594 include $gfplugins.$this->name.'/view/admin/viewGlobalConfiguration.php';
599 * getGlobalconf - return the global configuration defined at forge level
601 * @return array the global configuration array
603 function getGlobalconf() {
604 $resGlobConf = db_query_params('SELECT * from plugin_projects_hierarchy_global',array());
609 $row = db_numrows($resGlobConf);
611 if ($row == null || count($row) > 2) {
615 $resArr = db_fetch_array($resGlobConf);
616 $returnArr = array();
618 foreach($resArr as $column => $value) {
620 $returnArr[$column] = true;
622 $returnArr[$column] = false;
629 function getProjectAdminView() {
630 global $gfplugins, $use_tooltips;
631 include $gfplugins.$this->name.'/view/admin/viewProjectConfiguration.php';
636 * getFooter - display footer
638 function getFooter($type) {
642 case 'globaladmin': {
643 session_require_global_perm('forge_admin');
644 site_admin_footer(array());
649 site_project_footer(array());
657 * updateGlobalConf - update the global configuration in database
659 * @param array configuration array (tree, docman, delegate)
660 * @return bool true on success
662 function updateGlobalConf($confArr) {
663 if (!isset($confArr['tree']) || !isset($confArr['docman']) || !isset($confArr['delegate']))
666 $res = db_query_params('truncate plugin_projects_hierarchy_global',array());
670 $res = db_query_params('insert into plugin_projects_hierarchy_global (tree, docman, delegate)
671 values ($1, $2, $3)',
675 $confArr['delegate'],
684 * son_box - display a select box for son selection
686 * @param integer group_id
688 * @param string selected value
689 * @return string html box
692 function son_box($group_id, $name, $selected = 'xzxzxz') {
693 $sons = $this->getFamily($group_id, 'child', true, 'any');
694 $parent = $this->getFamily($group_id, 'parent', true, 'any');
695 $family = array_merge($parent, $sons);
696 $son = db_query_params('SELECT group_id, group_name FROM groups
699 AND group_id <> ALL ($3)
700 AND group_id IN (select group_id from group_plugin,plugins where group_plugin.plugin_id = plugins.plugin_id and plugins.plugin_name = $4);',
703 db_int_array_to_any_clause($family),
705 return html_build_select_box($son, $name, $selected, false);
710 // c-file-style: "bsd"