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
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 // Use this to provide a link to the site wide administrative pages for your plugin
82 echo '<li>'.$this->getAdminOptionLink().'</li>';
86 case "project_admin_plugins": {
87 // this displays the link in the project admin options page to it's administration
88 $group_id = $params['group_id'];
89 $group = group_get_object($group_id);
90 if ($group->usesPlugin($this->name)) {
92 echo util_make_link('/plugins/'.$this->name.'/?group_id='.$group_id.'&type=admin&pluginname='.$this->name, _('Hierarchy Admin'));
102 function displayHierarchy() {
103 $tree = $this->buildTree();
104 $displayTree = $this->affTree($tree, 0);
111 echo '<link rel="StyleSheet" href="/plugins/projects-hierarchy/dtree.css" type="text/css" />
112 <script type="text/javascript" src="/plugins/projects-hierarchy/dtree.js"></script>';
115 function buildTree() {
116 global $project_name;
117 $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
118 from groups as p1,groups as p2,plugin_projects_hierarchy_relationship
119 where p1.group_id=plugin_projects_hierarchy_relationship.project_id
120 and p2.group_id=plugin_projects_hierarchy_relationship.sub_project_id
121 and plugin_projects_hierarchy_relationship.status=$1
122 order by father_name, son_name',
125 // construction du tableau associatif
126 // key = name of the father
127 // value = list of sons
129 while ($row = db_fetch_array($res)) {
130 if (forge_check_perm('project_read', $row['father_id'])) {
131 $tree[$row['father_id']][] = $row['son_id'];
132 //get the unix name of the project
133 $project_name[$row['father_id']][0] = $row['father_name'];
134 $project_name[$row['son_id']][0] = $row['son_name'];
135 $project_name[$row['father_id']][1] = $row['father_unix_name'];
136 $project_name[$row['son_id']][1] = $row['son_unix_name'];
142 function affTree($tree, $lvl) {
143 global $project_name;
149 while (list($key, $sons) = each($tree)) {
150 // Really don't know why there is a warning there, and added @
151 if(@!$arbre[$key] != 0){
155 foreach ($sons as $son) {
156 $arbre[$son] = $cpt_pere;
160 if (sizeof($arbre)) {
161 $returnTree .= '<table ><tr><td>';
162 $returnTree .= '<script type="text/javascript">';
163 $returnTree .= 'd = new dTree(\'d\');';
164 $returnTree .= 'd.add(0,-1,\'Project Tree\');';
166 //construction automatique de l'arbre format : (num_fils, num_pere,nom,nom_unix)
167 while (list($key2, $sons2) = each($arbre)) {
168 $returnTree .= "d.add('".$key2."','".$sons2."','".$project_name[$key2][0]."','".util_make_url( '/projects/'.$project_name[$key2][1] .'/', $project_name[$key2][1] ) ."');";
171 $returnTree .= 'document.write(d);';
172 $returnTree .= '</script>';
173 $returnTree .= '</td></tr></table>';
179 * getFamily - find the children or parent group_id of this project.
181 * @param integer group_id to search for
182 * @param string parent or child ?
183 * @param boolean recurcive or not ?
184 * @param string validated or pending or any relation ?
185 * @return array array of arrays with group_id of parent or childs
188 function getFamily($group_id, $order, $deep = false, $status = 'any') {
189 $localFamily = array();
192 $statusCondition = 't';
197 $statusCondition = 'f';
207 $qpa = db_construct_qpa(false, 'SELECT project_id as id FROM plugin_projects_hierarchy_relationship
208 WHERE sub_project_id = $1', array($group_id));
209 if (isset($statusCondition))
210 $qpa = db_construct_qpa($qpa, ' AND status = $1', array($statusCondition));
212 $res = db_query_qpa($qpa);
216 $qpa = db_construct_qpa(false, 'SELECT sub_project_id as id FROM plugin_projects_hierarchy_relationship
217 WHERE project_id = $1', array($group_id));
218 if (isset($statusCondition))
219 $qpa = db_construct_qpa($qpa, ' AND status = $1', array($statusCondition));
221 $res = db_query_qpa($qpa);
229 if ($res && db_numrows($res) > 0) {
230 while ($arr = db_fetch_array($res)) {
231 $localFamily[] = $arr['id'];
236 $nextFamily = array();
237 for ( $i = 0; $i < count($localFamily); $i++) {
238 $nextFamily = $this->getFamily($localFamily[$i], $order, $deep, $status);
241 if (isset($nextFamily) && sizeof($nextFamily))
242 $localFamily = array_merge($localFamily, $nextFamily);
248 * getDocmanStatus - returns the docman status for this project
250 * @param integer group_id
251 * @return boolean true/false
254 function getDocmanStatus($group_id) {
255 $res = db_query_params('SELECT docman FROM plugin_projects_hierarchy WHERE project_id = $1 limit 1',
260 $resArr = db_fetch_array($res);
261 if ($resArr['docman'] == 't')
268 * setDocmanStatus - allow parent to browse your document manager and allow yourself to select your childs to be browsed.
270 * @param integer your groud_id
271 * @param boolean the status to set
272 * @return boolean success or not
274 function setDocmanStatus($group_id, $status = 0) {
275 $res = db_query_params('UPDATE plugin_projects_hierarchy set docman = $1 WHERE project_id = $2',
276 array($status, $group_id));
285 * redirect - encapsulate session_redirect to handle correctly the redirection URL
287 * @param string usually http_referer from $_SERVER
288 * @param string type of feedback : error, warning, feedback
289 * @param string the message of feedback
292 function redirect($http_referer, $type, $message) {
303 $url = util_find_relative_referer($http_referer);
304 if (strpos($url,'?')) {
305 session_redirect($url.'&'.$type.'='.urlencode($message));
307 session_redirect($url.'?'.$type.'='.urlencode($message));
311 * override default groupisactivecheckboxpost function for init value in db
313 function groupisactivecheckboxpost(&$params) {
314 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
315 $group = group_get_object($params['group']);
316 $flag = strtolower('use_'.$this->name);
317 if ( getIntFromRequest($flag) == 1 ) {
318 if ($this->add($group->getID())) {
319 $group->setPluginUse($this->name);
323 if ($this->remove($group->getID())) {
324 $group->setPluginUse($this->name, false);
332 * add - add a new group_id using the plugin
334 * @param integer group_id
335 * @return boolean true on success
338 function add($group_id) {
339 if (!$this->exists($group_id)) {
340 $globalConf = $this->getGlobalconf();
341 $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']));
349 * remove - remove group_id using the plugin
351 * @param integer group_id
352 * @return boolean true on success
355 function remove($group_id) {
356 if ($this->exists($group_id)) {
358 $res = db_query_params('DELETE FROM plugin_projects_hierarchy where project_id = $1', array($group_id));
364 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship where project_id = $1 OR sub_project_id = $1',
376 * addChild - add a new child to this project
378 * @param integer group_id
379 * @param integer sub_group_id
380 * @return boolean true on success
383 function addChild($project_id, $sub_project_id) {
384 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
385 if (!$this->hasRelation($project_id, $sub_project_id)) {
386 $res = db_query_params('INSERT INTO plugin_projects_hierarchy_relationship (project_id, sub_project_id)
387 VALUES ($1, $2)', array($project_id, $sub_project_id));
398 * removeChild - remove a child to this project
400 * @param integer group_id
401 * @param integer sub_group_id
402 * @return boolean true on success
405 function removeChild($project_id, $sub_project_id) {
406 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
407 if ($this->hasRelation($project_id, $sub_project_id)) {
408 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
409 WHERE project_id = $1 AND sub_project_id = $2',
410 array($project_id, $sub_project_id));
421 * removeParent - remove a parent to this project
423 * @param integer group_id
424 * @param integer sub_group_id
425 * @return boolean true on success
428 function removeParent($project_id, $sub_project_id) {
429 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
430 if ($this->hasRelation($project_id, $sub_project_id)) {
431 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
432 WHERE project_id = $1 AND sub_project_id = $2',
433 array($sub_project_id, $project_id));
444 * hasRelation - check if there is a relation child->parent or parent->child between 2 projects
446 * @param integer group_id
447 * @param integer sub_group_id
448 * @return boolean true on success
451 function hasRelation($project_id, $sub_project_id) {
452 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
453 $res = db_query_params('SELECT * FROM plugin_projects_hierarchy_relationship
454 WHERE ( project_id = $1 AND sub_project_id = $2 )
455 OR ( project_id = $2 AND sub_project_id = $1 )',
456 array($project_id, $sub_project_id));
457 if ($res && db_numrows($res) == 1 )
464 * validateRelationship - validate or reject a relation between 2 projects
466 * @param integer group_id
467 * @param integer sub_group_id
468 * @param string type of relation
469 * @param integer status of the relation
470 * @return boolean true on success
473 function validateRelationship($project_id, $sub_project_id, $relation, $status) {
474 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
475 if ($this->hasRelation($project_id, $sub_project_id)) {
477 $qpa = db_construct_qpa(false, 'UPDATE plugin_projects_hierarchy_relationship SET status = $1
478 WHERE ', array($status));
481 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
482 array($sub_project_id, $project_id));
486 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
487 array($project_id, $sub_project_id));
495 $res = db_query_qpa($qpa);
499 if (db_affected_rows($res))
502 $qpa = db_construct_qpa(false, 'DELETE FROM plugin_projects_hierarchy_relationship WHERE ',
506 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
507 array($project_id, $sub_project_id));
511 $qpa = db_construct_qpa($qpa, 'project_id = $1 AND sub_project_id = $2',
512 array($sub_project_id, $project_id));
520 $res = db_query_qpa($qpa);
524 if (db_affected_rows($res))
533 * exists - if this project use the plugin
535 * @param integer group_id
536 * @return boolean true on success
539 function exists($group_id) {
540 $res = db_query_params('SELECT project_id FROM plugin_projects_hierarchy WHERE project_id = $1', array($group_id));
544 if (db_numrows($res))
551 * getAdminOptionLink - return the admin link url
553 * @return string html url
556 function getAdminOptionLink() {
557 return util_make_link('/plugins/'.$this->name.'/?type=globaladmin&pluginname='.$this->name,_('Global Hierarchy admin'));
561 * getHeader - initialize header and js
562 * @param string type : user, project (aka group)
563 * @param array params
564 * @return bool success or not
566 function getHeader($type, $params=NULL) {
570 case 'globaladmin': {
571 session_require_global_perm('forge_admin');
573 require_once($gfwww.'admin/admin_utils.php');
574 site_admin_header(array('title'=>_('Site Global Hierarchy Admin'), 'toptab'=>''));
580 site_project_header($params);
589 * getGlobalAdminView - display the global configuration view
591 * @return boolean True
594 function getGlobalAdminView() {
595 global $gfplugins, $use_tooltips;
596 include $gfplugins.$this->name.'/view/admin/viewGlobalConfiguration.php';
601 * getGlobalconf - return the global configuration defined at forge level
603 * @return array the global configuration array
605 function getGlobalconf() {
606 $resGlobConf = db_query_params('SELECT * from plugin_projects_hierarchy_global',array());
611 $row = db_numrows($resGlobConf);
613 if ($row == null || count($row) > 2) {
617 $resArr = db_fetch_array($resGlobConf);
618 $returnArr = array();
620 foreach($resArr as $column => $value) {
622 $returnArr[$column] = true;
624 $returnArr[$column] = false;
631 function getProjectAdminView() {
632 global $gfplugins, $use_tooltips;
633 include $gfplugins.$this->name.'/view/admin/viewProjectConfiguration.php';
638 * getFooter - display footer
640 function getFooter($type) {
644 case 'globaladmin': {
645 session_require_global_perm('forge_admin');
646 site_admin_footer(array());
651 site_project_footer(array());
659 * updateGlobalConf - update the global configuration in database
661 * @param array configuration array (tree, docman, delegate)
662 * @return bool true on success
664 function updateGlobalConf($confArr) {
665 if (!isset($confArr['tree']) || !isset($confArr['docman']) || !isset($confArr['delegate']))
668 $res = db_query_params('truncate plugin_projects_hierarchy_global',array());
672 $res = db_query_params('insert into plugin_projects_hierarchy_global (tree, docman, delegate)
673 values ($1, $2, $3)',
677 $confArr['delegate'],
686 * son_box - display a select box for son selection
688 * @param integer group_id
690 * @param string selected value
691 * @return string html box
694 function son_box($group_id, $name, $selected = 'xzxzxz') {
695 $sons = $this->getFamily($group_id, 'child', true, 'any');
696 $parent = $this->getFamily($group_id, 'parent', true, 'any');
697 $family = array_merge($parent, $sons);
698 $son = db_query_params('SELECT group_id, group_name FROM groups
701 AND group_id <> ALL ($3)
702 AND group_id IN (select group_id from group_plugin,plugins where group_plugin.plugin_id = plugins.plugin_id and plugins.plugin_name = $4);',
705 db_int_array_to_any_clause($family),
707 return html_build_select_box($son, $name, $selected, false);
712 // c-file-style: "bsd"