Commit 972758e6982b14fc8d83c08b6a9d52a7b468ff97

Authored by Michael Joseph
1 parent b2213ff7

added code to update unit root folder on update and create


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@1586 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 234 additions and 241 deletions
lib/unitmanagement/Unit.inc
... ... @@ -9,268 +9,261 @@
9 9 */
10 10  
11 11 class Unit {
12   -
13   - /** object's primary key */
14   - var $iId;
15   - /** unit's name */
16   - var $sName;
17   -
18   -
19   - function Unit($sNewName)
20   - {
21   - //object has not been created in database yet
22   - $this->iId = -1;
23   - $this->sName = $sNewName;
24   -
25   - }
26   -
27   - /**
28   - * Get the object's primary key
29   - *
30   - * @return int object's primary key
31   - *
32   - */
33   - function getID()
34   - {
35   - return $this->iId;
36   - }
37   -
38   - /**
39   - * Get the unit's name
40   - *
41   - * @return String unit's name
42   - *
43   - */
44   - function getName()
45   - {
46   - return $this->sName;
47   - }
48   -
49   - /**
50   - * Set the unit's name
51   - *
52   - * @param String Unit's name
53   - *
54   - */
55   - function setName($sNewValue)
56   - {
57   - $this->sName = $sNewValue;
58   - }
59   -
60   -
61   - /**
62   - * Create the current object in the database
63   - *
64   - * @return boolean on successful store, false otherwise and set $_SESSION["errorMessage"]
65   - *
66   - */
67   - function create()
68   - {
69   - global $default, $lang_err_database, $lang_err_object_exists;
70   - //if the object hasn't been created
71   - if ($this->iId < 0)
72   - { //check to see if name exsits
73   - $sql = $default->db;
74   - $query = "SELECT name FROM ". $default->owl_units_table ." WHERE name = '" . $this->sName . "'";
75   - $sql->query($query);
76   - $rows = $sql->num_rows($sql);
77   -
78   - if ($rows > 0)
79   - {
80   - // duplicate username
81   - $_SESSION["errorMessage"] = "Unit::The name " . $this->sName . " is already in use!";
82   - return false;
83   - }
84   - else
85   - {
86   - $result = $sql->query("INSERT INTO " . $default->owl_units_table . " (name) VALUES ('" . addslashes($this->sName) . "')");
87   - if ($result)
88   - {
89   - $this->iId = $sql->insert_id();
90   - return true;
91   - }
92   - $_SESSION["errorMessage"] = $lang_err_database;
93   - return false;
94   - }
95   - }
96   - $_SESSION["errorMessage"] = $lang_err_object_exists . "id = " . $this->iId . " table = $default->owl_units_table";
97   - return false;
98   - }
99   -
100   - /**
101   - * Update the values in the database table with the object's current values
102   - *
103   - * @return boolean true on successful update, false otherwise and set $_SESSION["errorMessage"]
104   - *
105   - */
106   - function update()
107   - {
108   - global $default, $lang_err_database, $lang_err_object_key;
109   - //only update if the object has been stored
110   - if ($this->iId > 0)
111   - {
112   - $sql = $default->db;
113   - $result = $sql->query("UPDATE " . $default->owl_units_table . " SET name = '" . addslashes($this->sName) . "' WHERE id = $this->iId");
114   - if ($result)
115   - {
116   - return true;
117   - }
118   - $_SESSION["errorMessage"] = $lang_err_database;
119   - return false;
120   - }
121   - $_SESSION["errorMessage"] = $lang_err_object_key;
122   - return false;
123   - }
124   -
125   - /**
126   - * Delete the current object from the database
127   - *
128   - * @return boolean true on successful deletion, false otherwise and set $_SESSION["errorMessage"]
129   - *
130   - */
131   - function delete()
132   - {
133   - global $default, $lang_err_database, $lang_err_object_key;
134   - //only delete the object if it exists in the database
135   -
136   - if ($this->iId >= 0) {
137   - //check to see if group is linked to a unit
138   - $sql = $default->db;
139   - $query = "SELECT unit_id FROM ". $default->owl_groups_units_table ." WHERE unit_id = '" . $this->iId . "'";
140   - $sql->query($query);
141   - $rows = $sql->num_rows($sql);
142   -
143   -
144   -
145   - if ($rows > 0){
146   - // duplicate link exists
147   - $_SESSION["errorMessage"] = "Group::The Group " . $this->sName . " exits!";
148   - return false;
149   -
150   - }else{
151   - $sql = $default->db;
152   - $result = $sql->query("DELETE FROM $default->owl_units_table WHERE id = $this->iId");
153   - if ($result)
154   - {
155   - return true;
156   - }
157   - $_SESSION["errorMessage"] = $lang_err_database;
158   - return false;
159   - }
160   - }
161   - $_SESSION["errorMessage"] = $lang_err_object_key;
162   - return false;
163   - }
164   -
165   - /**
166   - * Static function.
167   - * Given a web_documents primary key it will create a
168   - * Unit object and populate it with the
169   - * corresponding database values
170   - *
171   - * @return Unit populated Unit object on successful query, false otherwise and set $_SESSION["errorMessage"]
172   - */
173   - function & get($iUnitID)
174   - {
175   - global $default;
176   - $sql = $default->db;
177   - $result = $sql->query("SELECT * FROM $default->owl_units_table WHERE id = $iUnitID");
178   - if ($result)
179   - {
180   - if ($sql->next_record())
181   - {
182   - $oUnit = & new Unit(stripslashes($sql->f("name")));
183   - $oUnit->iId = $iUnitID;
184   - return $oUnit;
185   - }
186   - $_SESSION["errorMessage"] = $lang_err_object_not_exist . "id = " . $iUnitID . " table = $default->owl_units_table";
187   - return false;
188   - }
189   - $_SESSION["errorMessage"] = $lang_err_database;
190   - return false;
191   - }
192 12  
193   - /*
  13 + /** object's primary key */
  14 + var $iId;
  15 + /** unit's name */
  16 + var $sName;
  17 +
  18 +
  19 + function Unit($sNewName) {
  20 + //object has not been created in database yet
  21 + $this->iId = -1;
  22 + $this->sName = $sNewName;
  23 + }
  24 +
  25 + /**
  26 + * Get the object's primary key
  27 + *
  28 + * @return int object's primary key
  29 + *
  30 + */
  31 + function getID() {
  32 + return $this->iId;
  33 + }
  34 +
  35 + /**
  36 + * Get the unit's name
  37 + *
  38 + * @return String unit's name
  39 + *
  40 + */
  41 + function getName() {
  42 + return $this->sName;
  43 + }
  44 +
  45 + /**
  46 + * Set the unit's name
  47 + *
  48 + * @param String Unit's name
  49 + *
  50 + */
  51 + function setName($sNewValue) {
  52 + $this->sName = $sNewValue;
  53 + }
  54 +
  55 +
  56 + /**
  57 + * Create the current object in the database
  58 + *
  59 + * @return boolean on successful store, false otherwise and set $_SESSION["errorMessage"]
  60 + *
  61 + */
  62 + function create() {
  63 + global $default, $lang_err_database, $lang_err_object_exists;
  64 + //if the object hasn't been created
  65 + if ($this->iId < 0) { //check to see if name exsits
  66 + $sql = $default->db;
  67 + $query = "SELECT name FROM ". $default->owl_units_table ." WHERE name = '" . $this->sName . "'";
  68 + $sql->query($query);
  69 + $rows = $sql->num_rows($sql);
  70 +
  71 + if ($rows > 0) {
  72 + // duplicate unit name
  73 + $_SESSION["errorMessage"] = "Unit::The name " . $this->sName . " is already in use!";
  74 + return false;
  75 + } else {
  76 + $result = $sql->query("INSERT INTO " . $default->owl_units_table . " (name) VALUES ('" . addslashes($this->sName) . "')");
  77 + if ($result) {
  78 + $this->iId = $sql->insert_id();
  79 + // create a new unit root folder
  80 + // FIXME: lookup the organisation for this unit and use the appropriate folder id for the org root folder
  81 + $oFolder = new Folder($this->sName, $this->sName . " Unit Root Folder", 1, $_SESSION["userID"], $this->iId);
  82 + if ($oFolder->create()) {
  83 + return true;
  84 + } else {
  85 + return false;
  86 + }
  87 + }
  88 + $_SESSION["errorMessage"] = $lang_err_database;
  89 + return false;
  90 + }
  91 + }
  92 + $_SESSION["errorMessage"] = $lang_err_object_exists . "id = " . $this->iId . " table = $default->owl_units_table";
  93 + return false;
  94 + }
  95 +
  96 + /**
  97 + * Update the values in the database table with the object's current values
  98 + *
  99 + * @return boolean true on successful update, false otherwise and set $_SESSION["errorMessage"]
  100 + *
  101 + */
  102 + function update() {
  103 + global $default, $lang_err_database, $lang_err_object_key;
  104 + //only update if the object has been stored
  105 + if ($this->iId > 0) {
  106 + $sql = $default->db;
  107 + // lookup current name before updating
  108 + $sOldName = lookupField($default->owl_units_table, "name", "id", $this->iId);
  109 + $result = $sql->query("UPDATE " . $default->owl_units_table . " SET name = '" . addslashes($this->sName) . "' WHERE id = $this->iId");
  110 + if ($result) {
  111 + // need to update the units root folder also
  112 + $iFolderID = Folder::getFolderID($sOldName);
  113 + // lookup the folder with the old unit name
  114 + $oFolder = Folder::get($iFolderID);
  115 + // folder name has changed, update the full_path
  116 + $sOldPath = $default->documentRoot . "/" . $oFolder->getFullPath() . "/" . $oFolder->getName();
  117 + // update the folder name
  118 + $oFolder->setName($this->sName);
  119 + $oFolder->setDescription($this->sName . " Unit Root Folder");
  120 + $sNewPath = $default->documentRoot . "/" . $oFolder->getFullPath() . "/" . $oFolder->getName();
  121 + if ($oFolder->update(true)) {
  122 + if (!PhysicalFolderManagement::renameFolder($sOldPath, $sNewPath)) {
  123 + //reverse the database changes if the physical rename failed
  124 + $oFolder->setName($sOldName);
  125 + $oFolder->update(true);
  126 + return false;
  127 + }
  128 + }
  129 + return true;
  130 + }
  131 + $_SESSION["errorMessage"] = $lang_err_database;
  132 + return false;
  133 + }
  134 + $_SESSION["errorMessage"] = $lang_err_object_key;
  135 + return false;
  136 + }
  137 +
  138 + /**
  139 + * Delete the current object from the database
  140 + *
  141 + * @return boolean true on successful deletion, false otherwise and set $_SESSION["errorMessage"]
  142 + *
  143 + */
  144 + function delete() {
  145 + global $default, $lang_err_database, $lang_err_object_key;
  146 + //only delete the object if it exists in the database
  147 +
  148 + if ($this->iId >= 0) {
  149 + //check to see if group is linked to a unit
  150 + $sql = $default->db;
  151 + $query = "SELECT unit_id FROM ". $default->owl_groups_units_table ." WHERE unit_id = '" . $this->iId . "'";
  152 + $sql->query($query);
  153 + $rows = $sql->num_rows($sql);
  154 +
  155 +
  156 +
  157 + if ($rows > 0) {
  158 + // duplicate link exists
  159 + $_SESSION["errorMessage"] = "Group::The Group " . $this->sName . " exits!";
  160 + return false;
  161 +
  162 + } else {
  163 + $sql = $default->db;
  164 + $result = $sql->query("DELETE FROM $default->owl_units_table WHERE id = $this->iId");
  165 + if ($result) {
  166 + return true;
  167 + }
  168 + $_SESSION["errorMessage"] = $lang_err_database;
  169 + return false;
  170 + }
  171 + }
  172 + $_SESSION["errorMessage"] = $lang_err_object_key;
  173 + return false;
  174 + }
  175 +
  176 + /**
  177 + * Static function.
  178 + * Given a web_documents primary key it will create a
  179 + * Unit object and populate it with the
  180 + * corresponding database values
  181 + *
  182 + * @return Unit populated Unit object on successful query, false otherwise and set $_SESSION["errorMessage"]
  183 + */
  184 + function & get($iUnitID) {
  185 + global $default;
  186 + $sql = $default->db;
  187 + $result = $sql->query("SELECT * FROM $default->owl_units_table WHERE id = $iUnitID");
  188 + if ($result) {
  189 + if ($sql->next_record()) {
  190 + $oUnit = & new Unit(stripslashes($sql->f("name")));
  191 + $oUnit->iId = $iUnitID;
  192 + return $oUnit;
  193 + }
  194 + $_SESSION["errorMessage"] = $lang_err_object_not_exist . "id = " . $iUnitID . " table = $default->owl_units_table";
  195 + return false;
  196 + }
  197 + $_SESSION["errorMessage"] = $lang_err_database;
  198 + return false;
  199 + }
  200 +
  201 + /**
194 202 * static function
195 203 *
196 204 * gets the id of a unit using their name
197 205 *
198   - * @param String
199   - * The unit_ID
200   - *
  206 + * @param String The unit name
201 207 */
202   -
203   - function getUnitID($name)
204   - {
205   - global $default;
206   -
207   - $id = lookupID($default->owl_units_table, "name", $name);
208   -
209   - $this->iId= $id;
210   - }
211   -
212   -
213   - /*
  208 + function getUnitID($name) {
  209 + global $default;
  210 + $id = lookupID($default->owl_units_table, "name", $name);
  211 + $this->iId= $id;
  212 + }
  213 +
  214 + /**
214 215 * static function
215 216 *
216 217 * gets the name of a unit using their id
217 218 *
218   - * @param String
219   - * The name
  219 + * @param String The name
220 220 *
221 221 */
222   -
223   - function getUnitName($id)
224   - {
225   - global $default;
226   -
227   - $name = lookupField("$default->owl_units_table", "name", "id", $id );
228   -
229   - $this->sName= $name;
230   - }
231   -
232   - /**
233   - * Static function
234   - * Get a list of web documents
235   - *
236   - * @param String Where clause (not required)
237   - *
238   - * @return Array array of Unit objects, false otherwise and set $_SESSION["errorMessage"]
239   - */
240   - function getList($sWhereClause = null)
241   - {
242   - global $default, $lang_err_database;
243   - $aUnitArray;
244   - settype($aUnitArray, "array");
245   - $sql = $default->db;
246   - $result = $sql->query("SELECT * FROM " . $default->owl_units_table . (isset($sWhereClause) ? " " . $sWhereClause : ""));
247   - if ($result)
248   - {
249   - $iCount = 0;
250   - while ($sql->next_record())
251   - {
252   - $oUnit = & Unit::get($sql->f("id"));
253   - $aUnitArray[$iCount] = $oUnit;
254   - $iCount++;
255   - }
256   - return $aUnitArray;
257   - }
258   - $_SESSION["errorMessage"] = $lang_err_database;
259   - return false;
260   - }
261   -
  222 + function getUnitName($id) {
  223 + global $default;
  224 + $name = lookupField("$default->owl_units_table", "name", "id", $id );
  225 + $this->sName= $name;
  226 + }
  227 +
  228 + /**
  229 + * Static function
  230 + * Get a list of web documents
  231 + *
  232 + * @param String Where clause (not required)
  233 + *
  234 + * @return Array array of Unit objects, false otherwise and set $_SESSION["errorMessage"]
  235 + */
  236 + function getList($sWhereClause = null) {
  237 + global $default, $lang_err_database;
  238 + $aUnitArray;
  239 + settype($aUnitArray, "array");
  240 + $sql = $default->db;
  241 + $result = $sql->query("SELECT * FROM " . $default->owl_units_table . (isset($sWhereClause) ? " " . $sWhereClause : ""));
  242 + if ($result) {
  243 + $iCount = 0;
  244 + while ($sql->next_record()) {
  245 + $oUnit = & Unit::get($sql->f("id"));
  246 + $aUnitArray[$iCount] = $oUnit;
  247 + $iCount++;
  248 + }
  249 + return $aUnitArray;
  250 + }
  251 + $_SESSION["errorMessage"] = $lang_err_database;
  252 + return false;
  253 + }
  254 +
262 255 }
263 256 /**
264 257 * Static function
265 258 *
266 259 * Creates a unit object from an array
267 260 *
268   -* @param Array Array of parameters. Must match order of parameters in constructor
  261 +* @param Array Array of parameters. Must match order of parameters in constructor
269 262 *
270 263 * @return User user object
271 264 */
272 265 function & unitCreateFromArray($aParameters) {
273   - $oUnit = & new Unit($aParameters[0], $aParameters[1], $aParameters[2], $aParameters[3], $aParameters[4], $aParameters[5], $aParameters[6], $aParameters[7], $aParameters[8], $aParameters[9], $aParameters[10]);
274   - return $oUnit;
  266 + $oUnit = & new Unit($aParameters[0], $aParameters[1], $aParameters[2], $aParameters[3], $aParameters[4], $aParameters[5], $aParameters[6], $aParameters[7], $aParameters[8], $aParameters[9], $aParameters[10]);
  267 + return $oUnit;
275 268 }
276 269 ?>
... ...