Changeset 2566:9bf417837888 for inc/core/class.dc.categories.php
- Timestamp:
- 11/17/13 20:25:53 (12 years ago)
- Branch:
- 2.6
- Children:
- 2567:6c11245cbf04, 2568:61c67a7d17fa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.categories.php
r1532 r2566 22 22 protected $f_right = 'cat_rgt'; 23 23 protected $f_id = 'cat_id'; 24 24 25 25 protected $core; 26 26 protected $blog_id; 27 27 28 28 public function __construct($core) 29 29 { … … 34 34 $this->add_condition = array('blog_id' => "'".$this->con->escape($this->blog_id)."'"); 35 35 } 36 36 37 37 public function getChildren($start=0,$id=null,$sort='asc',$fields=array()) 38 38 { … … 40 40 return parent::getChildren($start,$id,$sort,$fields); 41 41 } 42 42 43 43 public function getParents($id,$fields=array()) 44 44 { … … 46 46 return parent::getParents($id,$fields); 47 47 } 48 48 49 49 public function getParent($id,$fields=array()) 50 50 { … … 57 57 { 58 58 protected $con; 59 59 60 60 protected $table; 61 61 protected $f_left; 62 62 protected $f_right; 63 63 protected $f_id; 64 64 65 65 protected $add_condition = array(); 66 66 67 67 protected $parents; 68 68 69 69 public function __construct($con) 70 70 { 71 71 $this->con =& $con; 72 72 } 73 73 74 74 public function getChildren($start=0,$id=null,$sort='asc',$fields=array()) 75 75 { 76 76 $fields = count($fields) > 0 ? ', C2.'.implode(', C2.',$fields) : ''; 77 77 78 78 $sql = 'SELECT C2.'.$this->f_id.', C2.'.$this->f_left.', C2.'.$this->f_right.', COUNT(C1.'.$this->f_id.') AS level ' 79 . $fields.' ' 79 . $fields.' ' 80 80 . 'FROM '.$this->table.' AS C1, '.$this->table.' AS C2 %s ' 81 81 . 'WHERE C2.'.$this->f_left.' BETWEEN C1.'.$this->f_left.' AND C1.'.$this->f_right.' ' … … 86 86 . ' %s ' 87 87 . 'ORDER BY C2.'.$this->f_left.' '.($sort == 'asc' ? 'ASC' : 'DESC').' '; 88 88 89 89 $from = $where = ''; 90 90 if ($start > 0) { … … 93 93 $where .= $this->getCondition('AND','C3.'); 94 94 } 95 95 96 96 $having = ''; 97 97 if ($id !== null) { 98 98 $having = ' HAVING C2.'.$this->f_id.' = '.(integer) $id; 99 99 } 100 100 101 101 $sql = sprintf($sql,$from,$where,$having); 102 102 103 103 return $this->con->select($sql); 104 104 } 105 105 106 106 public function getParents($id,$fields=array()) 107 107 { 108 108 $fields = count($fields) > 0 ? ', C1.'.implode(', C1.',$fields) : ''; 109 109 110 110 return $this->con->select( 111 111 'SELECT C1.'.$this->f_id.' '.$fields.' ' … … 119 119 ); 120 120 } 121 121 122 122 public function getParent($id,$fields=array()) 123 123 { 124 124 $fields = count($fields) > 0 ? ', C1.'.implode(', C1.',$fields) : ''; 125 125 126 126 return $this->con->select( 127 127 'SELECT C1.'.$this->f_id.' '.$fields.' ' … … 136 136 ); 137 137 } 138 138 139 139 /* ------------------------------------------------ 140 140 * Tree manipulations … … 145 145 throw new Exception('Invalid data block'); 146 146 } 147 147 148 148 if (is_array($data)) 149 149 { … … 155 155 unset($D); 156 156 } 157 157 158 158 # We want to put it at the end 159 159 $this->con->writeLock($this->table); … … 162 162 $rs = $this->con->select('SELECT MAX('.$this->f_id.') as n_id FROM '.$this->table); 163 163 $id = $rs->n_id; 164 164 165 165 $rs = $this->con->select( 166 166 'SELECT MAX('.$this->f_right.') as n_r '. … … 169 169 ); 170 170 $last = $rs->n_r == 0 ? 1 : $rs->n_r; 171 171 172 172 $data->{$this->f_id} = $id+1; 173 173 $data->{$this->f_left} = $last+1; 174 174 $data->{$this->f_right} = $last+2; 175 175 176 176 $data->insert(); 177 177 $this->con->unlock(); … … 187 187 } 188 188 } 189 189 190 190 public function updatePosition($id,$left,$right) 191 191 { … … 209 209 { 210 210 $node = (integer) $node; 211 211 212 212 $rs = $this->getChildren(0,$node); 213 213 if ($rs->isEmpty()) { … … 216 216 $node_left = (integer) $rs->{$this->f_left}; 217 217 $node_right = (integer) $rs->{$this->f_right}; 218 218 219 219 try 220 220 { 221 221 $this->con->begin(); 222 222 223 223 if ($keep_children) 224 224 { 225 225 $this->con->execute('DELETE FROM '.$this->table.' WHERE '.$this->f_id.' = '.$node); 226 226 227 227 $sql = 'UPDATE '.$this->table.' SET ' 228 228 . $this->f_right.' = CASE ' … … 242 242 . 'WHERE '.$this->f_right.' > '.$node_left 243 243 . $this->getCondition(); 244 244 245 245 $this->con->execute($sql); 246 246 } … … 248 248 { 249 249 $this->con->execute('DELETE FROM '.$this->table.' WHERE '.$this->f_left.' BETWEEN '.$node_left.' AND '.$node_right); 250 250 251 251 $node_delta = $node_right - $node_left + 1; 252 252 $sql = 'UPDATE '.$this->table.' SET ' … … 264 264 . $this->getCondition(); 265 265 } 266 266 267 267 $this->con->commit(); 268 268 } … … 273 273 } 274 274 } 275 275 276 276 public function resetOrder() 277 277 { … … 282 282 .'ORDER BY '.$this->f_left.' ASC ' 283 283 ); 284 284 285 285 $lft = 2; 286 286 $this->con->begin(); … … 304 304 } 305 305 } 306 306 307 307 public function setNodeParent($node,$target=0) 308 308 { … … 312 312 $node = (integer) $node; 313 313 $target = (integer) $target; 314 314 315 315 $rs = $this->getChildren(0,$node); 316 316 if ($rs->isEmpty()) { … … 320 320 $node_right = (integer) $rs->{$this->f_right}; 321 321 $node_level = (integer) $rs->level; 322 322 323 323 if ($target > 0) 324 324 { … … 336 336 $target_right = (integer) $rs->{$this->f_right}; 337 337 $target_level = (integer) $rs->level; 338 338 339 339 if ($node_left == $target_left 340 340 || ($target_left >= $node_left && $target_left <= $node_right) … … 344 344 throw new Exception('Cannot move tree'); 345 345 } 346 346 347 347 if ($target_left < $node_left && $target_right > $node_right && $target_level < $node_level -1) 348 348 { … … 405 405 . 'OR '.$this->f_right.' BETWEEN '.$node_left.' AND '.$target_right.')'; 406 406 } 407 407 408 408 $sql .= ' '.$this->getCondition(); 409 409 410 410 $this->con->execute($sql); 411 411 } 412 412 413 413 public function setNodePosition($nodeA,$nodeB,$position='after') 414 414 { 415 415 $nodeA = (integer) $nodeA; 416 416 $nodeB = (integer) $nodeB; 417 417 418 418 $rs = $this->getChildren(0,$nodeA); 419 419 if ($rs->isEmpty()) { … … 423 423 $A_right = $rs->{$this->f_right}; 424 424 $A_level = $rs->level; 425 425 426 426 $rs = $this->getChildren(0,$nodeB); 427 427 if ($rs->isEmpty()) { … … 431 431 $B_right = $rs->{$this->f_right}; 432 432 $B_level = $rs->level; 433 433 434 434 if ($A_level != $B_level) { 435 435 throw new Exception('Cannot change position'); 436 436 } 437 437 438 438 $rs = $this->getParents($nodeA); 439 439 $parentA = $rs->isEmpty() ? 0 : $rs->{$this->f_id}; 440 440 $rs = $this->getParents($nodeB); 441 441 $parentB = $rs->isEmpty() ? 0 : $rs->{$this->f_id}; 442 442 443 443 if ($parentA != $parentB) { 444 444 throw new Exception('Cannot change position'); 445 445 } 446 446 447 447 if ($position == 'before') 448 448 { … … 481 481 } 482 482 } 483 483 484 484 $sql .= $this->getCondition(); 485 485 $this->con->execute($sql); 486 486 } 487 487 488 488 protected function getCondition($start='AND',$prefix='') 489 489 { … … 491 491 return ''; 492 492 } 493 493 494 494 $w = array(); 495 495 foreach ($this->add_condition as $c => $n) { … … 499 499 } 500 500 } 501 ?>
Note: See TracChangeset
for help on using the changeset viewer.