commonimplicittreedropdown.class.php

webmyster, 08/19/2011 11:15 AM

Download (5.67 KB)

 
1
<?php
2
/*
3
 * @version $Id: commonimplicittreedropdown.class.php 14684 2011-06-11 06:32:40Z remi $
4
 -------------------------------------------------------------------------
5
 GLPI - Gestionnaire Libre de Parc Informatique
6
 Copyright (C) 2003-2011 by the INDEPNET Development Team.
7

8
 http://indepnet.net/   http://glpi-project.org
9
 -------------------------------------------------------------------------
10

11
 LICENSE
12

13
 This file is part of GLPI.
14

15
 GLPI is free software; you can redistribute it and/or modify
16
 it under the terms of the GNU General Public License as published by
17
 the Free Software Foundation; either version 2 of the License, or
18
 (at your option) any later version.
19

20
 GLPI is distributed in the hope that it will be useful,
21
 but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 GNU General Public License for more details.
24

25
 You should have received a copy of the GNU General Public License
26
 along with GLPI; if not, write to the Free Software Foundation, Inc.,
27
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28
 --------------------------------------------------------------------------
29
*/
30

    
31
// ----------------------------------------------------------------------
32
// Original Author of file: Damien Touraine
33
// Purpose of file:
34
// ----------------------------------------------------------------------
35

    
36
if (!defined('GLPI_ROOT')) {
37
  die("Sorry. You can't access directly to this file");
38
}
39

    
40
class CommonImplicitTreeDropdown extends CommonTreeDropdown {
41
  // Methods to be overloaded
42
  function getMyNewAncestor($add) {
43
    return 0; // By default, we rattach to the root element
44
  }
45

    
46
  function getPotentialSons($add) {
47
    return array(); // By default, we don't have any son
48
  }
49

    
50
  // Methods that overloads parents to update the hierarchy
51
  function prepareInputForAdd($input) {
52
    $input[$this->getForeignKeyField()] = $this->getMyNewAncestor(true);
53
    // We call the parent to manage hierarchy
54
    return parent::prepareInputForAdd($input);
55
  }
56

    
57
  function prepareInputForUpdate($input) {
58
    $input[$this->getForeignKeyField()] = $this->getMyNewAncestor(false);
59
    // We call the parent to manage hierarchy
60
    return parent::prepareInputForUpdate($input);
61
  }
62

    
63
  // After adding or updating an item, we adapt old childrens and new childrens to point to the correct ancestor
64
  function post_addItem() {
65
    $this->alterElementInsideTree("add");
66
    parent::post_addItem();
67
  }
68

    
69
  function post_updateItem($history=1) {
70
    $this->alterElementInsideTree("update");
71
    parent::post_updateItem($history);
72
  }
73

    
74
  // Before deleting an item, we make all children to point to the old parent of the current node.
75
  function pre_deleteItem() {
76
    $this->alterElementInsideTree("delete");
77
    return parent::pre_deleteItem();
78
  }
79

    
80
  // The haveChildren=false must be define to be sure that CommonDropdown allows the deletion of a node of the hierarchy
81
  function haveChildren() {
82
    return false;
83
  }
84

    
85
  // Key function to manage the children of the node
86
  private function alterElementInsideTree($step) {
87
    global $DB;
88

    
89
    switch ($step) {
90
    case 'add' :
91
      $newParent = $this->input[$this->getForeignKeyField()];
92
      $potentialSons = $this->getPotentialSons(true);
93
      break;
94
    case 'update' :
95
      $oldParent = $this->fields[$this->getForeignKeyField()];
96
      $newParent = $this->input[$this->getForeignKeyField()];
97
      $potentialSons = $this->getPotentialSons(false);
98
      break;
99
    case 'delete' :
100
      $oldParent = $this->fields[$this->getForeignKeyField()];
101
      $potentialSons = array(); // Because there is no future sons !
102
      break;
103
    };
104
    // $oldParent used to attach old childrens
105
    // $newParent used to find new children that must be attached to me
106
    // $potentialSons list ALL childrens (sons as well as grandsons)
107

    
108
    if ($step != "add") { // Because there is no old sons of new node
109
      // First, get all my current direct sons (old ones) that are not new potential sons
110
      $query = "SELECT `id`
111
                FROM `".$this->getTable()."`
112
                WHERE `".$this->getForeignKeyField()."`='".$this->getID()."'
113
                AND `id` NOT IN ('".implode("', '", $potentialSons)."')";
114
      $oldSons = array();
115
      foreach ($DB->request($query) as $oldSon) {
116
        $oldSons[] = $oldSon["id"];
117
      }
118
      if (count($oldSons) > 0) { // Then make them pointing to old parent
119
        $query = "UPDATE `".$this->getTable()."`
120
                  SET `".$this->getForeignKeyField()."`='$oldParent'
121
                  WHERE `id` IN ('".implode("', '",$oldSons)."')";
122
        $DB->query($query);
123
        // Then, regenerate the old sons to reflect there new ancestors
124
        $this->regenerateTreeUnderID($oldParent, true, true);
125
        $this->recursiveCleanSonsAboveID($oldParent);
126
      }
127
    }
128

    
129
    if ($step != "delete") { // Because ther is no new sons for deleted nodes
130
      // And, get all direct sons of my new Father that must be attached to me (ie : that are potential sons
131
      $query = "SELECT `id`
132
                FROM `".$this->getTable()."`
133
                WHERE `".$this->getForeignKeyField()."`='$newParent'
134
                AND `id` IN ('".implode("', '", $potentialSons)."')";
135
      $newSons = array();
136
      foreach ($DB->request($query) as $newSon) {
137
        $newSons[] = $newSon["id"];
138
      }
139
      if (count($newSons) > 0) { // Then make them pointing to me
140
        $query = "UPDATE `".$this->getTable()."`
141
                  SET `".$this->getForeignKeyField()."`='".$this->getID()."'
142
                  WHERE `id` IN ('".implode("', '",$newSons)."')";
143
        $DB->query($query);
144
        // Then, regenerate the new sons to reflect there new ancestors
145
        $this->regenerateTreeUnderID($this->getID(), true, true);
146
        $this->recursiveCleanSonsAboveID($this->getID());
147
      }
148
    }
149
  }
150
}
151

    
152
?>