source: trunk/includes/nestedsets.php @ 751

Revision 751, 30.0 KB checked in by r2, 14 months ago (diff)

merge trunk + fuze

  • Property svn:executable set to *
Line 
1<?php
2/*
3        CCelkoNastedSet class
4
5    Written by Setec Astronomy
6
7        Thanks to Kirill Hryapin <kx@chl.ru> for finding and solving bugs into the MoveNode () method.
8
9    Modified by Vladimir Obukhov <r2@instantsoft.ru> for InstantCMS project.
10
11        Members
12        $TableName => Table name that contains nasted sets
13        $FieldID => Field name for table ID
14        $FieldIDParent => Field name for table IDParent
15        $FieldLeft => Field name for table nasted set left field
16        $FieldRight => Field name for table nasted set right field 
17    $FieldDiffer => Field name used to manage more than one type of nasted set in the same table
18        $FieldLevel => Field name for table nasted set level field  (0 = root node)
19        $FieldOrder => Field name for table nasted set order field   
20        $FieldIgnore => Field name for tablr nested set ignore field
21       
22        $TransactionTable => Name for table used to manage transactions
23
24        $MyLink => MySQL database link resource
25
26        Methods
27        SelectSubNodes () => Returns all the sub node of IDNode fo Level number of level.
28        SelectPath () => Returns the path from the IDNode to the root node.
29        MoveNode () => Method used to move the node IDNode to IDParent.
30        AddRootNode () => Method used to add the root node for a nested set
31        AddNode () => Method used to add a node to IDParent. Return the new IDNode.
32        DeleteNode () => Method used to delete the node identified by IDNode and all his children.
33        ClearNodes () => Method used to clear the nasted set table
34
35        BeginTransaction () => Method that start the transaction. Returns true or false.
36        EndTransaction () => Method that stop the transaction.
37        IsInTransaction () => Method to check the transaction state. Returns true or false.
38
39*/
40
41class CCelkoNastedSet { 
42
43   public  $TableName;
44   private $FieldID;
45   private $FieldIDParent;
46   private $FieldLeft;
47   private $FieldRight;
48   private $FieldDiffer;
49   private $FieldLevel;
50   private $FieldOrder;
51   private $FieldIgnore;
52   
53   public  $TransactionTable;
54   private $_IsInTransaction;
55   private $_TransactionTStamp;
56
57   public $MyLink;
58
59   public function __construct ()
60        { 
61           $this->TableName = "tblCelkoTree"; 
62           $this->FieldID = "id"; 
63           $this->FieldIDParent = "parent_id"; 
64           $this->FieldLeft = "NSLeft"; 
65           $this->FieldRight = "NSRight"; 
66           $this->FieldDiffer = "NSDiffer";
67           $this->FieldLevel = "NSLevel"; 
68           $this->FieldOrder = "ordering";
69           $this->FieldIgnore = "NSIgnore"; 
70       
71           $this->TransactionTable = "cms_ns_transactions";
72           $this->_IsInTransaction = false;
73           $this->_TransactionTStamp = 0;
74
75           $this->MyLink = null; 
76        } 
77
78        // Begin private functions //
79        private function _safe_set (&$var_true, $var_false = "")
80        {
81                if (!isset ($var_true)) 
82                { $var_true = $var_false; }
83        }
84
85        private function _safe_query ($query, $link)
86        {
87                if (empty($query)) { return false; }
88        $inDB   = cmsDatabase::getInstance();
89                $result = $inDB->query($query) or die("Nested sets error: $query");
90                return $result;
91        }
92        // End private functions //
93       
94        // Begin transaction functions //
95        private function InitializeTransaction ($Differ = "")
96        {
97                $sql_verify = "SELECT * FROM " . $this->TransactionTable .
98                                          " WHERE TableName = '" . $this->TableName . "' " .
99                                          " AND Differ = '" . $Differ . "'";
100                $rs_verify = $this->_safe_query ($sql_verify, $this->MyLink);
101               
102                if (($rs_verify) && (mysql_num_rows ($rs_verify) == 0))
103                {
104                        mysql_free_result ($rs_verify);
105                       
106                        $sql_insert = "INSERT INTO " . $this->TransactionTable . 
107                                                  " (TableName, Differ, InTransaction) " .
108                                                  " VALUES ('" . $this->TableName . "', '" . $Differ . "', 0)";
109                        $this->_safe_query ($sql_insert, $this->MyLink);
110                       
111                        return true;
112                }
113                else
114                { return false; }
115        }
116
117        private function BeginTransaction ($Differ = "")
118        {
119        //r2: çäåñü íóæíî áóäåò ðàçáèðàòüñÿ ñ òðàíçàêöèÿìè
120        $this->_IsInTransaction = true; return true;
121
122        $this->InitializeTransaction($Differ);
123
124                $TStamp = date ("YmdHis");
125
126                $sql_update = "UPDATE " . $this->TransactionTable . 
127                                          " SET TStamp = " . $TStamp . ", " .
128                                          " InTransaction = 1 " .
129                                          " WHERE InTransaction = 0 " .
130                                          " AND TableName = '" . $this->TableName . "' " .
131                                          " AND Differ = '" . $Differ . "'";
132                $this->_safe_query ($sql_update, $this->MyLink);
133               
134                $sql_verify = "SELECT * FROM " . $this->TransactionTable .
135                                          " WHERE TableName = '" . $this->TableName . "' " .
136                                          " AND InTransaction = 1 " .
137                                          " AND TStamp = " . $TStamp . 
138                                          " AND Differ = '" . $Differ . "'";
139                $rs_verify = $this->_safe_query ($sql_verify, $this->MyLink);
140
141                if (($rs_verify) && (mysql_num_rows ($rs_verify) == 1))
142                {
143                        mysql_free_result ($rs_verify);
144                        $this->_IsInTransaction = true;
145                        $this->_TransactionTStamp = $TStamp;
146                        return true;
147                }
148                else
149                { 
150                        $this->_IsInTransaction = false;
151                        $this->_TransactionTStamp = 0;
152                        return false; 
153                }
154        }
155       
156        public function EndTransaction ($Differ = "")
157        {
158                $sql_update = "UPDATE " . $this->TransactionTable . 
159                                          " SET InTransaction = 0 " .
160                                          " WHERE TableName = '" . $this->TableName . "' " .
161                                          " AND TStamp = " . $this->_TransactionTStamp . 
162                                          " AND Differ = '" . $Differ . "'";
163                $this->_safe_query ($sql_update, $this->MyLink);
164                $this->_IsInTransaction = false;
165        }
166
167        public function IsInTransaction ()
168        { return $this->_IsInTransaction; }
169        // End transaction  functions //
170       
171        // Begin nasted set functions //
172        public function ClearNodes ($Differ = "")
173        {
174        $this->BeginTransaction($Differ);
175                $sql_delete = "DELETE FROM " . $this->TableName . " WHERE " . $this->FieldDiffer . " = '" . $Differ . "'";
176                $this->_safe_query ($sql_delete, $this->MyLink);
177        $this->EndTransaction($Differ);
178        }
179
180        public function DeleteNode ($IDNode = -1, $Differ = "")
181        {
182        $this->BeginTransaction($Differ);
183
184                if (!$this->_IsInTransaction)
185                { return false; }
186               
187                $sql_select = "SELECT * FROM " . $this->TableName . " WHERE " . $this->FieldID . " = " . $IDNode . " AND " . $this->FieldDiffer . " = '" . $Differ . "'"; 
188                $rs_select = $this->_safe_query ($sql_select, $this->MyLink);
189                if (($rs_select) && ($row_select = mysql_fetch_assoc ($rs_select)))
190                {
191                        $this->_safe_set ($row_select[$this->FieldID], -1);
192                        $this->_safe_set ($row_select[$this->FieldLeft], -1);
193                        $this->_safe_set ($row_select[$this->FieldRight], -1);
194                        $delete_offset = $row_select[$this->FieldRight] - $row_select[$this->FieldLeft];
195
196                        // Delete sub nodes
197                        $sql_delete = "DELETE FROM " . $this->TableName . 
198                                                  " WHERE " . $this->FieldLeft . " >= " . $row_select[$this->FieldLeft] . 
199                                                  " AND " . $this->FieldLeft . " <= " . $row_select[$this->FieldRight] .
200                                                  " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
201                        $this->_safe_query ($sql_delete, $this->MyLink);
202
203                        // Update FieldLeft
204                        $sql_update = "UPDATE " . $this->TableName . 
205                                                  " SET " . $this->FieldLeft . " = " . $this->FieldLeft . " - " .  ($delete_offset + 1) .
206                                                  " WHERE " . $this->FieldLeft . " > " . $row_select[$this->FieldRight] .
207                                                  " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
208                        $this->_safe_query ($sql_update, $this->MyLink);
209
210                        // Update FieldRight
211                        $sql_update = "UPDATE " . $this->TableName . 
212                                                  " SET " . $this->FieldRight . " = " . $this->FieldRight . " - " .  ($delete_offset + 1) .
213                                                  " WHERE " . $this->FieldRight . " > " . $row_select[$this->FieldRight] . 
214                                                  " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
215                        $this->_safe_query ($sql_update, $this->MyLink);
216               
217                        mysql_free_result ($rs_select);
218
219            $this->EndTransaction($Differ);
220                        return true;
221
222
223        } else {
224
225            $this->EndTransaction($Differ);
226            return false;
227
228        }
229        }
230
231        public function AddRootNode ($Differ = "")
232        {
233        $this->BeginTransaction($Differ);
234
235                if (!$this->_IsInTransaction)
236                { return false; }
237
238                $sql_insert = "INSERT INTO " . $this->TableName . 
239                                          " (" . $this->FieldIDParent . ", " . $this->FieldLeft . ", " . $this->FieldRight . 
240                                          ", " . $this->FieldLevel . ", " . $this->FieldOrder . ", " . $this->FieldDiffer . ") " .
241                                          " VALUES (0, 1, 2, 0, 1, '" . $Differ . "')";
242                $this->_safe_query ($sql_insert, $this->MyLink);
243
244        $new_id = mysql_insert_id ($this->MyLink);
245
246        $this->EndTransaction($Differ);
247
248                return $new_id;
249        }
250
251        public function AddNode ($IDParent = -1, $Order = -1, $Differ = "")
252        {
253        $this->BeginTransaction($Differ);
254
255                if (!$this->_IsInTransaction)
256                { return false; }
257
258                $sql_select = "SELECT * FROM " . $this->TableName . " WHERE " . $this->FieldID . " = " . $IDParent . " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
259                $rs_select = $this->_safe_query ($sql_select, $this->MyLink);
260                if (($rs_select) && ($row_select = mysql_fetch_assoc ($rs_select)))
261                {
262                        $this->_safe_set ($row_select[$this->FieldID], -1);
263                        $this->_safe_set ($row_select[$this->FieldLeft], -1);
264                        $this->_safe_set ($row_select[$this->FieldRight], -1);
265                        $this->_safe_set ($row_select[$this->FieldLevel], -1);
266                       
267                        $left = $row_select[$this->FieldLeft] + 1;
268               
269                        // Update Order (set order = order +1 where order>$Order)
270                        if ($Order == -1)
271                        {
272                                $sql_order = "SELECT * FROM " . $this->TableName . 
273                                                         " WHERE " . $this->FieldIDParent . " = " . $IDParent .
274                                                         " AND " . $this->FieldDiffer . " = '" . $Differ . "'" .
275                                                         " ORDER BY " . $this->FieldOrder . " DESC " .
276                                                         " LIMIT 0,1";
277                                $rs_order = $this->_safe_query ($sql_order, $this->MyLink);
278                                if (($rs_order) && ($row_order = mysql_fetch_assoc ($rs_order)))
279                                {
280                                        $this->_safe_set ($row_order[$this->FieldOrder], 0);
281                                        $Order = $row_order[$this->FieldOrder] + 1;
282                                        mysql_free_result ($rs_order);
283                                }
284                                else
285                                { $Order = 1; }
286                        }
287
288                        $sql_update = "UPDATE " . $this->TableName . 
289                                                  " SET " . $this->FieldOrder . " = " . $this->FieldOrder . " + 1" .
290                                                  " WHERE " . $this->FieldIDParent . " = " . $IDParent .
291                                                  " AND " . $this->FieldOrder . " >= " . $Order . 
292                                                  " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
293                        $this->_safe_query ($sql_update, $this->MyLink);
294                       
295                        $sql_order = "SELECT * FROM " . $this->TableName . 
296                                                 " WHERE " . $this->FieldIDParent . " = " . $IDParent .
297                                                 " AND " . $this->FieldOrder  . " <= " . $Order .
298                                                 " AND " . $this->FieldDiffer . " = '" . $Differ . "'" .
299                                                 " ORDER BY " . $this->FieldOrder . " DESC " .
300                                                 " LIMIT 0,1";
301                        $rs_order = $this->_safe_query ($sql_order, $this->MyLink);
302                        if (($rs_order) && ($row_order = mysql_fetch_assoc ($rs_order)))
303                        {
304                                $this->_safe_set ($row_order[$this->FieldRight], -1);
305                                $left = $row_order[$this->FieldRight] + 1;
306                                mysql_free_result ($rs_order);
307                        }
308                       
309                        $right = $left + 1;
310                       
311                        // Update FieldLeft
312                        $sql_update = "UPDATE " . $this->TableName . 
313                                                  " SET " . $this->FieldLeft . " = " . $this->FieldLeft . " + 2" .
314                                                  " WHERE " . $this->FieldLeft . " >= " . $left .
315                                                  " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
316                        $this->_safe_query ($sql_update, $this->MyLink);
317                       
318                        // Update FieldRight
319                        $sql_update = "UPDATE " . $this->TableName . 
320                                                  " SET " . $this->FieldRight . " = " . $this->FieldRight . " + 2" .
321                                                  " WHERE " . $this->FieldRight . " >= " . $left .
322                                                  " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
323                        $this->_safe_query ($sql_update, $this->MyLink);
324
325                        // Insert
326                        $sql_insert = "INSERT INTO " . $this->TableName . 
327                                                  " (" . $this->FieldIDParent . ", " . $this->FieldLeft . ", " . $this->FieldRight . 
328                                                  ", " . $this->FieldLevel . ", " . $this->FieldOrder . ", " . $this->FieldDiffer . ") " .
329                                                  " VALUES (" . $IDParent . ", " . $left . ", " . $right . 
330                                                  ", " . ($row_select[$this->FieldLevel] + 1) . ", " . $Order . ", '" . $Differ . "')";
331                        $this->_safe_query ($sql_insert, $this->MyLink);
332
333                        mysql_free_result ($rs_select);
334
335            $new_id = mysql_insert_id ($this->MyLink);
336
337            $this->EndTransaction($Differ);
338
339                        return $new_id;
340                }
341                else
342                { return false; }
343        }
344       
345    public function MoveOrdering($IDNode, $dir = 1){
346
347        $this->BeginTransaction();
348
349        if (!$this->_IsInTransaction)
350                { return false; }
351       
352        $sql = "SELECT * FROM {$this->TableName} WHERE {$this->FieldID}='{$IDNode}'";
353        $res = $this->_safe_query ($sql, $this->MyLink);
354        $move_row = mysql_fetch_assoc($res);
355        mysql_free_result($res);
356       
357        if ($move_row[$this->FieldDiffer]) $Differ = 'AND ' . $this->FieldDiffer . ' = ' . $move_row[$this->FieldDiffer];
358        else $Differ = '';
359       
360                // ìàêñèìàëüíîå çíà÷åíèå ñîðòèðîâêè
361        $sql = "SELECT MAX({$this->FieldOrder}) FROM {$this->TableName} WHERE {$this->FieldIDParent}={$move_row[$this->FieldIDParent]}";
362        $res = $this->_safe_query ($sql, $this->MyLink);
363        list($maxordering) = mysql_fetch_row($res);
364        if (!$maxordering) $maxordering = 1;
365                // ìèíèìàëüíîå çíà÷åíèå ñîðòèðîâêè
366        $sql_min = "SELECT MIN({$this->FieldOrder}) FROM {$this->TableName} WHERE {$this->FieldIDParent}={$move_row[$this->FieldIDParent]}";
367        $res_min = $this->_safe_query ($sql_min, $this->MyLink);
368        list($minordering) = mysql_fetch_row($res_min);
369        if (!$minordering) $minordering = 1;
370
371        mysql_free_result($res);
372       
373        if ($dir == -1 && $move_row[$this->FieldOrder] == $minordering) return;
374        if ($dir == 1 && $move_row[$this->FieldOrder] == $maxordering) return;
375       
376        if ($dir == -1){
377           
378            $sql = "UPDATE {$this->TableName} SET {$this->FieldIgnore} = 1
379                    WHERE {$this->FieldLeft} >= {$move_row[$this->FieldLeft]} AND {$this->FieldRight} <= {$move_row[$this->FieldRight]} {$Differ}";
380            $this->_safe_query ($sql, $this->MyLink);
381            $count = mysql_affected_rows($this->MyLink) * 2;
382           
383            $sql = "SELECT * FROM {$this->TableName} 
384                    WHERE {$this->FieldIDParent} = {$move_row[$this->FieldIDParent]} AND {$this->FieldOrder} = ".($move_row[$this->FieldOrder]-1);
385            $res = $this->_safe_query ($sql, $this->MyLink);
386            $near = mysql_fetch_assoc($res);
387            mysql_free_result($res);
388           
389            $sql = "UPDATE {$this->TableName} 
390                    SET {$this->FieldLeft} = {$this->FieldLeft} + {$count},
391                        {$this->FieldRight} = {$this->FieldRight} + {$count}
392                    WHERE {$this->FieldLeft} >= {$near[$this->FieldLeft]} AND {$this->FieldRight} <= {$near[$this->FieldRight]}
393                    {$Differ}";
394                   
395            $this->_safe_query ($sql, $this->MyLink);
396            $count2 = mysql_affected_rows($this->MyLink) * 2;
397           
398            $sql = "UPDATE {$this->TableName} 
399                    SET {$this->FieldLeft} = {$this->FieldLeft} - {$count2},
400                        {$this->FieldRight} = {$this->FieldRight} - {$count2},
401                        {$this->FieldIgnore} = 0
402                    WHERE {$this->FieldIgnore} = 1
403                    {$Differ}";
404            $this->_safe_query ($sql, $this->MyLink);
405           
406            $sql = "UPDATE {$this->TableName} SET {$this->FieldOrder} = {$this->FieldOrder} - 1
407                    WHERE {$this->FieldID} = {$IDNode}";
408            $this->_safe_query ($sql, $this->MyLink);
409           
410            $sql = "UPDATE {$this->TableName} SET {$this->FieldOrder} = {$this->FieldOrder} + 1
411                    WHERE {$this->FieldID} = {$near[$this->FieldID]}";
412            $this->_safe_query ($sql, $this->MyLink);
413        }
414       
415        if ($dir == 1){
416           
417            $sql = "UPDATE {$this->TableName} SET {$this->FieldIgnore} = 1
418                    WHERE {$this->FieldLeft} >= {$move_row[$this->FieldLeft]} AND {$this->FieldRight} <= {$move_row[$this->FieldRight]} {$Differ}";
419            $this->_safe_query ($sql, $this->MyLink);
420            $count = mysql_affected_rows($this->MyLink) * 2;
421           
422            $sql = "SELECT * FROM {$this->TableName} 
423                    WHERE {$this->FieldIDParent} = {$move_row[$this->FieldIDParent]} AND {$this->FieldOrder} = ".($move_row[$this->FieldOrder]+1);
424            $res = $this->_safe_query ($sql, $this->MyLink);
425            $near = mysql_fetch_assoc($res);
426            mysql_free_result($res);
427           
428            $sql = "UPDATE {$this->TableName} 
429                    SET {$this->FieldLeft} = {$this->FieldLeft} - {$count},
430                        {$this->FieldRight} = {$this->FieldRight} - {$count}
431                    WHERE {$this->FieldLeft} >= {$near[$this->FieldLeft]} AND {$this->FieldRight} <= {$near[$this->FieldRight]}
432                    {$Differ}";
433                   
434            $this->_safe_query ($sql, $this->MyLink);
435            $count2 = mysql_affected_rows($this->MyLink) * 2;
436           
437            $sql = "UPDATE {$this->TableName} 
438                    SET {$this->FieldLeft} = {$this->FieldLeft} + {$count2},
439                        {$this->FieldRight} = {$this->FieldRight} + {$count2},
440                        {$this->FieldIgnore} = 0
441                    WHERE {$this->FieldIgnore} = 1
442                    {$Differ}";
443            $this->_safe_query ($sql, $this->MyLink);
444           
445            $sql = "UPDATE {$this->TableName} SET {$this->FieldOrder} = {$this->FieldOrder} + 1
446                    WHERE {$this->FieldID} = {$IDNode}";
447            $this->_safe_query ($sql, $this->MyLink);
448           
449            $sql = "UPDATE {$this->TableName} SET {$this->FieldOrder} = {$this->FieldOrder} - 1
450                    WHERE {$this->FieldID} = {$near[$this->FieldID]}";
451            $this->_safe_query ($sql, $this->MyLink);
452        }
453
454        $this->EndTransaction($Differ);
455
456        return true;
457    }   
458
459        public function MoveNode ($IDNode = -1, $IDParent = -1, $Order = -1, $Differ = "")
460        {
461        $this->BeginTransaction($Differ);
462
463        if (!$this->_IsInTransaction)
464                { return false; }
465
466                $sql_select = "SELECT * FROM " . $this->TableName .
467                              " WHERE " . $this->FieldID . " = " . $IDNode .
468                              " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
469
470                $rs_select = $this->_safe_query ($sql_select, $this->MyLink);
471                if (($rs_select) && ($row_select = mysql_fetch_assoc ($rs_select)))
472                {
473                        $this->_safe_set ($row_select[$this->FieldID], -1);
474                        $this->_safe_set ($row_select[$this->FieldLeft], -1);
475                        $this->_safe_set ($row_select[$this->FieldRight], -1);
476                        $this->_safe_set ($row_select[$this->FieldLevel], -1);
477                        $delete_offset = $row_select[$this->FieldRight] - $row_select[$this->FieldLeft];
478
479
480                        $sql_select_parent = "SELECT * FROM " . $this->TableName .
481                                             " WHERE " . $this->FieldID . " = " . $IDParent .
482                                             " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
483
484                        $rs_select_parent = $this->_safe_query ($sql_select_parent, $this->MyLink);
485                        if (($rs_select_parent) && ($row_select_parent = mysql_fetch_assoc ($rs_select_parent)))
486                        {
487                                $this->_safe_set ($row_select_parent[$this->FieldID], -1);
488                                $this->_safe_set ($row_select_parent[$this->FieldLeft], -1);
489                                $this->_safe_set ($row_select_parent[$this->FieldRight], -1);
490                                $this->_safe_set ($row_select_parent[$this->FieldLevel], -1);
491
492                                $left = $row_select_parent[$this->FieldLeft] + 1;
493
494                                //Set node tree as ignore
495                                $sql_ignore = "UPDATE " . $this->TableName .
496                                                          " SET " . $this->FieldIgnore . " = 1" .
497                                                          " WHERE " . $this->FieldLeft . " >= " . $row_select[$this->FieldLeft] .
498                                                          " AND " . $this->FieldRight . " <= " . $row_select[$this->FieldRight] .
499                                                          " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
500                                $this->_safe_query ($sql_ignore, $this->MyLink);
501
502                                // Update Order (set order = order +1 where order>$Order)
503                                if ($Order == -1)
504                                {
505                                        $sql_order = "SELECT * FROM " . $this->TableName .
506                                                                 " WHERE " . $this->FieldIDParent . " = " . $IDParent .
507                                                                 " AND " . $this->FieldDiffer . " = '" . $Differ . "'" .
508                                                                 " ORDER BY " . $this->FieldOrder . " DESC " .
509                                                                 " LIMIT 0,1";
510                                        $rs_order = $this->_safe_query ($sql_order, $this->MyLink);
511                                        if (($rs_order) && ($row_order = mysql_fetch_assoc ($rs_order)))
512                                        {
513                                                $this->_safe_set ($row_order[$this->FieldOrder], 0);
514                                                $Order = $row_order[$this->FieldOrder] + 1;
515                                                mysql_free_result ($rs_order);
516                                        }
517                                        else
518                                        { $Order = 1; }
519                                }
520
521                                $sql_update = "UPDATE " . $this->TableName .
522                                                          " SET " . $this->FieldOrder . " = " . $this->FieldOrder . " + 1" .
523                                                          " WHERE " . $this->FieldIDParent . " = " . $IDParent .
524                                                          " AND " . $this->FieldOrder . " >= " . $Order .
525                                                          " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
526                                $this->_safe_query ($sql_update, $this->MyLink);
527
528                                $sql_order = "SELECT * FROM " . $this->TableName .
529                                                         " WHERE " . $this->FieldIDParent . " = " . $IDParent .
530                                                         " AND " . $this->FieldOrder  . " <= " . $Order .
531                                                         " AND " . $this->FieldDiffer . " = '" . $Differ . "'" .
532                                                         " ORDER BY " . $this->FieldOrder . " DESC " .
533                                                         " LIMIT 0,1";
534                                $rs_order = $this->_safe_query ($sql_order, $this->MyLink);
535                                if (($rs_order) && ($row_order = mysql_fetch_assoc ($rs_order)))
536                                {
537                                        $this->_safe_set ($row_order[$this->FieldRight], -1);
538                                        $left = $row_order[$this->FieldRight] + 1;
539                                        mysql_free_result ($rs_order);
540                                }
541
542                                $child_offset = $row_select[$this->FieldRight] - $row_select[$this->FieldLeft] + 1;
543
544                                // Update FieldLeft
545                                if ($left < $row_select[$this->FieldLeft]) // Move to left
546                                {
547                                        $sql_update = "UPDATE " . $this->TableName .
548                                                                  " SET " . $this->FieldLeft . " = " . $this->FieldLeft . " + (" . $child_offset . ")" .
549                                                                  " WHERE " . $this->FieldLeft . " >= " . $left .
550                                                                  " AND " . $this->FieldLeft . " <= " . $row_select[$this->FieldLeft] .
551                                                                  " AND " . $this->FieldIgnore . " = 0" .
552                                                                  " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
553                                }
554                                else // Move to right
555                                {
556                                        $sql_update = "UPDATE " . $this->TableName .
557                                                                  " SET " . $this->FieldLeft . " = " . $this->FieldLeft . " - " . $child_offset .
558                                                                  " WHERE " . $this->FieldLeft . " <= " . $left .
559                                                                  " AND " . $this->FieldLeft . " >= " . $row_select[$this->FieldLeft] .
560                                                                  " AND " . $this->FieldIgnore . " = 0" .
561                                                                  " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
562                                }
563                                $this->_safe_query ($sql_update, $this->MyLink);
564
565                                // Update FieldRight
566                                if ($left < $row_select[$this->FieldLeft]) // Move to left
567                                {
568                                        $sql_update = "UPDATE " . $this->TableName .
569                                                                  " SET " . $this->FieldRight . " = " . $this->FieldRight . " + (" . $child_offset . ")" .
570                                                                  " WHERE " . $this->FieldRight . " >= " . $left .
571                                                                  " AND " . $this->FieldRight . " <= " . $row_select[$this->FieldRight] .
572                                                                  " AND " . $this->FieldIgnore . " = 0" .
573                                                                  " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
574                                }
575                                else // Move to right
576                                {
577                                        $sql_update = "UPDATE " . $this->TableName .
578                                                                  " SET " . $this->FieldRight . " = " . $this->FieldRight . " - " . $child_offset .
579                                                                  " WHERE " . $this->FieldRight . " < " . $left .
580                                                                  " AND " . $this->FieldRight . " >= " . $row_select[$this->FieldRight] .
581                                                                  " AND " . $this->FieldIgnore . " = 0" .
582                                                                  " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
583                                }
584                                $this->_safe_query ($sql_update, $this->MyLink);
585
586                                $level_difference = $row_select_parent[$this->FieldLevel] - $row_select[$this->FieldLevel] + 1;
587                                $new_offset = $row_select[$this->FieldLeft] - $left;
588                if ($left > $row_select[$this->FieldLeft]) // i.e. move to right
589                { $new_offset += $child_offset; }
590
591                                //Update new tree left
592                                $sql_update = "UPDATE " . $this->TableName .
593                                                          " SET " . $this->FieldLeft . " = " . $this->FieldLeft . " - (" . $new_offset . "), " .
594                                                          $this->FieldRight . " = " . $this->FieldRight . " - (" . $new_offset . ")," .
595                                                          "$this->FieldLevel = $this->FieldLevel + $level_difference" .
596                                                          " WHERE " . $this->FieldLeft . " >= " . $row_select[$this->FieldLeft] .
597                                                          " AND " . $this->FieldRight . " <= " . $row_select[$this->FieldRight] .
598                                                          " AND " . $this->FieldIgnore . " = 1" .
599                                                          " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
600                                $this->_safe_query ($sql_update, $this->MyLink);
601
602                                //Remove ignore statis from node tree
603                                $sql_ignore = "UPDATE " . $this->TableName .
604                                                          " SET " . $this->FieldIgnore . " = 0" .
605                                                          " WHERE " . $this->FieldLeft . " >= " . ($row_select[$this->FieldLeft] - $new_offset) .
606                                                          " AND " . $this->FieldRight . " <= " . ($row_select[$this->FieldRight] - $new_offset) .
607                                                          " AND " . $this->FieldIgnore . " = 1" .
608                                                          " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
609                                $this->_safe_query ($sql_ignore, $this->MyLink);
610
611                                //Update insert root field
612                                $sql_update = "UPDATE " . $this->TableName . " SET " . $this->FieldIDParent . " = " . $IDParent . ", " .
613                                                          $this->FieldOrder . " = " . $Order . " WHERE " . $this->FieldID . " = " . $IDNode;
614                                $this->_safe_query ($sql_update, $this->MyLink);
615
616                                mysql_free_result ($rs_select_parent);
617                $this->EndTransaction($Differ);
618                                return true;
619                        }
620                        else
621                        { $this->EndTransaction($Differ);
622                return false; }
623
624                        mysql_free_result ($rs_select);
625            $this->EndTransaction($Differ);
626                        return true;
627                }
628                else
629                { $this->EndTransaction($Differ); return false; }
630        }
631
632        public function SelectPath ($IDNode = -1, $Differ = "")
633        {
634
635                $sql_select = "SELECT * FROM " . $this->TableName . " WHERE " . $this->FieldID . " = " . $IDNode . " AND " . $this->FieldDiffer . " = '" . $Differ . "'";       
636                $rs_select = $this->_safe_query ($sql_select, $this->MyLink);
637                if (($rs_select) && ($row_select = mysql_fetch_assoc ($rs_select)))
638                {
639                        $this->_safe_set ($row_select[$this->FieldID], -1);
640                        $this->_safe_set ($row_select[$this->FieldLeft], -1);
641                        $this->_safe_set ($row_select[$this->FieldRight], -1);
642                        $sql_result = "SELECT * FROM " . $this->TableName . 
643                                                  " WHERE " . $this->FieldLeft . " <= " . $row_select[$this->FieldLeft] .
644                                                  " AND " . $this->FieldRight . " >= " . $row_select[$this->FieldRight] . 
645                                                  " AND " . $this->FieldDiffer . " = '" . $Differ . "'" . 
646                                                  " ORDER BY " . $this->FieldLeft;
647                        mysql_free_result ($rs_select);
648                        return $this->_safe_query ($sql_result, $this->MyLink); // Remember to free result                       
649                }
650                else
651                { return false; }
652        }
653
654        public function SelectSubNodes ($IDNode = -1, $Level = -1, $Differ = "")
655        {
656
657                $sql_select = "SELECT * FROM " . $this->TableName . " WHERE " . $this->FieldID . " = " . $IDNode . " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
658                $rs_select = $this->_safe_query ($sql_select, $this->MyLink);
659                if (($rs_select) && ($row_select = mysql_fetch_assoc ($rs_select)))
660                {
661                        $this->_safe_set ($row_select[$this->FieldID], -1);
662                        $this->_safe_set ($row_select[$this->FieldLeft], -1);
663                        $this->_safe_set ($row_select[$this->FieldRight], -1);
664                        $this->_safe_set ($row_select[$this->FieldLevel], -1);
665                        if ($Level == -1) // All child nodes
666                        {
667                                $sql_result = "SELECT * FROM " . $this->TableName . 
668                                                          " WHERE " . $this->FieldLeft . " > " . $row_select[$this->FieldLeft] .
669                                                          " AND " . $this->FieldRight . " < " . $row_select[$this->FieldRight] . 
670                                                          " AND " . $this->FieldDiffer . " = '" . $Differ . "'" .
671                                                          " ORDER BY " . $this->FieldLeft . "," . $this->FieldOrder;
672                        }
673                        else // Only $Level child nodes
674                        {
675                                $sql_result = "SELECT * FROM " . $this->TableName . 
676                                                          " WHERE " . $this->FieldLeft . " >= " . $row_select[$this->FieldLeft] .
677                                                          " AND " . $this->FieldRight . " <= " . $row_select[$this->FieldRight] . 
678                                                          " AND " . $this->FieldLevel . " <= " . ($Level + $row_select[$this->FieldLevel]) . 
679                                                          " AND " . $this->FieldDiffer . " = '" . $Differ . "'" .
680                                                          " ORDER BY " . $this->FieldLeft . "," . $this->FieldOrder;
681                        }
682                        mysql_free_result ($rs_select);           
683                        return $this->_safe_query ($sql_result, $this->MyLink); // Remember to free result                       
684                }
685                else
686                { return false; }
687        }
688       
689        public function SelectCountSubNodes ($IDNode = -1, $Level = -1, $Differ = "")
690        {
691
692                $sql_select = "SELECT * FROM " . $this->TableName . " WHERE " . $this->FieldID . " = " . $IDNode . " AND " . $this->FieldDiffer . " = '" . $Differ . "'";
693                $rs_select = $this->_safe_query ($sql_select, $this->MyLink);
694                if (($rs_select) && ($row_select = mysql_fetch_assoc ($rs_select)))
695                {
696                        $this->_safe_set ($row_select[$this->FieldID], -1);
697                        $this->_safe_set ($row_select[$this->FieldLeft], -1);
698                        $this->_safe_set ($row_select[$this->FieldRight], -1);
699                        $this->_safe_set ($row_select[$this->FieldLevel], -1);
700                        if ($Level == -1) // All child nodes
701                        {
702                                $sql_result = "SELECT count(" . $this->FieldID . ") FROM " . $this->TableName . 
703                                                          " WHERE " . $this->FieldLeft . " > " . $row_select[$this->FieldLeft] .
704                                                          " AND " . $this->FieldRight . " < " . $row_select[$this->FieldRight] . 
705                                                          " AND " . $this->FieldDiffer . " = '" . $Differ . "'" .
706                                                          " ORDER BY " . $this->FieldLeft . "," . $this->FieldOrder;
707                        }
708                        else // Only $Level child nodes
709                        {
710                                $sql_result = "SELECT count(" . $this->FieldID . ") FROM " . $this->TableName . 
711                                                          " WHERE " . $this->FieldLeft . " > " . $row_select[$this->FieldLeft] .
712                                                          " AND " . $this->FieldRight . " < " . $row_select[$this->FieldRight] . 
713                                                          " AND " . $this->FieldLevel . " <= " . ($Level + $row_select[$this->FieldLevel]) . 
714                                                          " AND " . $this->FieldDiffer . " = '" . $Differ . "'" .
715                                                          " ORDER BY " . $this->FieldLeft . "," . $this->FieldOrder;
716                        }
717                        mysql_free_result ($rs_select);
718                        $res = $this->_safe_query ($sql_result, $this->MyLink); // Remember to free result
719                        list($count) = mysql_fetch_row($res);
720                        mysql_free_result ($res);
721                        return $count;
722                }
723                else
724                { return false; }
725        }
726        // End nasted set functions //
727} 
728
729?>
Note: See TracBrowser for help on using the repository browser.