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 "project_admin_plugins": {
81 // this displays the link in the project admin options page to it's MantisBT administration
82 $group_id = $params['group_id'];
83 $group = group_get_object($group_id);
84 if ($group->usesPlugin($this->name)) {
86 echo util_make_link('/plugins/'.$this->name.'/?group_id='.$group_id.'&type=admin&pluginname='.$this->name, _('Hierarchy Admin'));
92 case "site_admin_option_hook": {
93 echo '<li>'.$this->getAdminOptionLink().'</li>';
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 $tree[$row['father_id']][] = $row['son_id'];
130 //get the unix name of the project
131 $project_name[$row['father_id']][0] = $row['father_name'];
132 $project_name[$row['son_id']][0] = $row['son_name'];
133 $project_name[$row['father_id']][1] = $row['father_unix_name'];
134 $project_name[$row['son_id']][1] = $row['son_unix_name'];
139 function affTree($tree, $lvl) {
140 global $project_name;
146 while (list($key, $sons) = each($tree)) {
147 // Really don't know why there is a warning there, and added @
148 if(@!$arbre[$key] != 0){
152 foreach ($sons as $son) {
153 $arbre[$son] = $cpt_pere;
157 $returnTree .= '<table ><tr><td>';
158 $returnTree .= '<script type="text/javascript">';
159 $returnTree .= 'd = new dTree(\'d\');';
160 $returnTree .= 'd.add(0,-1,\'Project Tree\');';
162 //construction automatique de l'arbre format : (num_fils, num_pere,nom,nom_unix)
163 while (list($key2, $sons2) = each($arbre)) {
164 $returnTree .= "d.add('".$key2."','".$sons2."','".$project_name[$key2][0]."','".util_make_url( '/projects/'.$project_name[$key2][1] .'/', $project_name[$key2][1] ) ."');";
167 $returnTree .= 'document.write(d);';
168 $returnTree .= '</script>';
169 $returnTree .= '</td></tr></table>';
174 * getFamily - find the children or parent group_id of this project.
176 * @param integer group_id to search for
177 * @param string parent or child ?
178 * @param boolean recurcive or not ?
179 * @param string validated or pending or any relation ?
180 * @return array array of arrays with group_id of parent or childs
183 function getFamily($group_id, $order, $deep = false, $status = 'any') {
184 $localFamily = array();
187 $statusCondition = 't';
192 $statusCondition = 'f';
202 $qpa = db_construct_qpa(false, 'SELECT project_id as id FROM plugin_projects_hierarchy_relationship
203 WHERE sub_project_id = $1', array($group_id));
204 if (isset($statusCondition))
205 $qpa = db_construct_qpa($qpa, ' AND status = $1', array($statusCondition));
207 $res = db_query_qpa($qpa);
211 $qpa = db_construct_qpa(false, 'SELECT sub_project_id as id FROM plugin_projects_hierarchy_relationship
212 WHERE project_id = $1', array($group_id));
213 if (isset($statusCondition))
214 $qpa = db_construct_qpa($qpa, ' AND status = $1', array($statusCondition));
216 $res = db_query_qpa($qpa);
224 if ($res && db_numrows($res) > 0) {
225 while ($arr = db_fetch_array($res)) {
226 $localFamily[] = array($arr['id']);
231 for ( $i = 0; $i < count($localFamily); $i++) {
232 $localFamily[$i][] = $this->getFamily($localFamily[$i], $order, $deep, $status);
238 function getDocmanStatus($group_id) {
239 $res = db_query_params('SELECT docman FROM plugin_projects_hierarchy WHERE project_id = $1 or sub_project_id = $1 limit 1',
244 $resArr = db_fetch_array($res);
245 if ($resArr['docman'] == 't')
252 * setDocmanStatus - allow parent to browse your document manager and allow yourself to select your childs to be browsed.
254 * @param integer your groud_id
255 * @param boolean the status to set
256 * @return boolean success or not
258 function setDocmanStatus($group_id, $status = 0) {
259 $res = db_query_params('UPDATE plugin_projects_hierarchy set docman = $1 WHERE sub_project_id = $2 OR project_id = $3',
260 array($status, $group_id, $group_id));
269 * redirect - encapsulate session_redirect to handle correctly the redirection URL
271 * @param string usually http_referer from $_SERVER
272 * @param string type of feedback : error, warning, feedback
273 * @param string the message of feedback
276 function redirect($http_referer, $type, $message) {
287 $url = util_find_relative_referer($http_referer);
288 if (strpos($url,'?')) {
289 session_redirect($url.'&'.$type.'='.urlencode($message));
291 session_redirect($url.'?'.$type.'='.urlencode($message));
295 * override default groupisactivecheckboxpost function for init value in db
297 function groupisactivecheckboxpost(&$params) {
298 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
299 $group = group_get_object($params['group']);
300 $flag = strtolower('use_'.$this->name);
301 if ( getIntFromRequest($flag) == 1 ) {
302 $group->setPluginUse($this->name);
303 $this->add($group->getID());
305 $group->setPluginUse($this->name, false);
306 $this->remove($group->getID());
311 function add($group_id) {
312 if (!$this->exists($group_id)) {
313 $res = db_query_params('INSERT INTO plugin_projects_hierarchy (project_id) VALUES ($1)', array($group_id));
321 function remove($group_id) {
322 if ($this->exists($group_id)) {
324 $res = db_query_params('DELETE FROM plugin_projects_hierarchy where project_id = $1', array($group_id));
330 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship where project_id = $1 OR sub_project_id = $1',
341 function addChild($project_id, $sub_project_id) {
342 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
343 if (!$this->hasRelation($project_id, $sub_project_id)) {
344 $res = db_query_params('INSERT INTO plugin_projects_hierarchy_relationship (project_id, sub_project_id)
345 VALUES ($1, $2)', array($project_id, $sub_project_id));
355 function removeChild($project_id, $sub_project_id) {
356 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
357 if ($this->hasRelation($project_id, $sub_project_id)) {
358 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
359 WHERE project_id = $1 AND sub_project_id = $2',
360 array($project_id, $sub_project_id));
370 function removeParent($project_id, $sub_project_id) {
371 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
372 if ($this->hasRelation($project_id, $sub_project_id)) {
373 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
374 WHERE project_id = $1 AND sub_project_id = $2',
375 array($sub_project_id, $project_id));
385 function hasRelation($project_id, $sub_project_id) {
386 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
387 $res = db_query_params('SELECT * FROM plugin_projects_hierarchy_relationship
388 WHERE ( project_id = $1 AND sub_project_id = $2 )
389 OR ( project_id = $2 AND sub_project_id = $1 )',
390 array($project_id, $sub_project_id));
391 if ($res && db_numrows($res) == 1 )
397 function validateRelationship($project_id, $sub_project_id, $status) {
398 if ($this->exists($project_id) && $this->exists($sub_project_id)) {
399 if ($this->hasRelation($project_id, $sub_project_id)) {
401 $res = db_query_params('UPDATE plugin_projects_hierarchy_relationship
403 WHERE project_id = $2 AND sub_project_id = $3',
404 array($status, $project_id, $sub_project_id));
408 if (db_affected_rows($res))
411 $res = db_query_params('DELETE FROM plugin_projects_hierarchy_relationship
412 WHERE project_id = $1 AND sub_project_id = $2',
413 array($project_id, $sub_project_id));
418 if (db_affected_rows($res))
426 function exists($group_id) {
427 $res = db_query_params('SELECT project_id FROM plugin_projects_hierarchy WHERE project_id = $1', array($group_id));
431 if (db_numrows($res))
437 function getAdminOptionLink() {
438 return util_make_link('/plugins/'.$this->name.'/?type=globaladmin&pluginname='.$this->name,_('Global Hierarchy admin'));
442 * getHeader - initialize header and js
443 * @param string type : user, project (aka group)
444 * @return bool success or not
446 function getHeader($type) {
450 case 'globaladmin': {
451 session_require_global_perm('forge_admin');
453 require_once($gfwww.'admin/admin_utils.php');
454 site_admin_header(array('title'=>_('Site Global Hierarchy Admin'), 'toptab' => ''));
465 function getGlobalAdminView() {
466 global $gfplugins, $use_tooltips;
467 include $gfplugins.$this->name.'/view/admin/viewGlobalConfiguration.php';
472 * getGlobalconf - return the global configuration defined at forge level
474 * @return array the global configuration array
476 function getGlobalconf() {
477 $resGlobConf = db_query_params('SELECT * from plugin_projects_hierarchy_global',array());
482 $row = db_numrows($resGlobConf);
484 if ($row == null || count($row) > 2) {
488 $resArr = db_fetch_array($resGlobConf);
489 $returnArr = array();
491 foreach($resArr as $column => $value) {
493 $returnArr[$column] = true;
495 $returnArr[$column] = false;
502 function getFooter() {
503 site_admin_footer(array());
507 * updateGlobalConf - update the global configuration in database
509 * @param array configuration array (tree, docman, delegate)
510 * @return bool true on success
512 function updateGlobalConf($confArr) {
513 if (!isset($confArr['tree']) || !isset($confArr['docman']) || !isset($confArr['delegate']))
516 $res = db_query_params('truncate plugin_projects_hierarchy_global',array());
520 $res = db_query_params('insert into plugin_projects_hierarchy_global (tree, docman, delegate)
521 values ($1, $2, $3)',
525 $confArr['delegate'],
533 function son_box($group_id, $name, $selected = 'xzxzxz') {
534 $sons = $this->getFamily($group_id, 'child', true, 'any');
535 $parent = $this->getFamily($group_id, 'parent', true, 'any');
537 $family = array_merge($parent, $sons);
538 if (sizeof($family)) {
539 //TODO : need to fix this. We only get parent here....
540 foreach ($family as $element) {
541 $skipped[] = $element[0];
544 $son = db_query_params('SELECT group_id, group_name FROM groups
547 AND group_id <> ALL ($3)
548 AND group_id NOT IN (SELECT sub_project_id FROM plugin_projects_hierarchy_relationship)
549 AND group_id IN (select group_id from group_plugin,plugins where group_plugin.plugin_id = plugins.plugin_id and plugins.plugin_name = $4);',
552 db_int_array_to_any_clause($skipped),
554 return html_build_select_box($son, $name, $selected, false);
559 // c-file-style: "bsd"