This tree stuff took me a while... was very hard
This tree stuff took me a while... was very hard Posted by Alexander Shatalov PHP public function add($data) { $this->set($this->_params); $this->offset = 0; if (!empty($data[$this->parent_id_col])) { DB::insert($this->table, $data); $this->get(); $this->resort(); } else { $data[$this->sort_col] = intval(DB::row('SELECT MAX(`sort`) AS max_sort FROM '.$this->prefix.$this->table.' WHERE TRUE'.$this->where,'max_sort')) + 1; DB::insert($this->table, $data, $this->id_col); } $this->set($this->_params); } public function edit($id, $data) { DB::update($this->table, $data, $id, $this->id_col); } public function delete($id) { $arr = $this->tree_array($id, true); foreach ($arr as $i => $rs) { DB::delete($this->table, $rs[$this->id_col], $this->id_col); } } public function move($id, $parent_id, $after_id = 0) { if (!$id || $id==$after_id) return false; $id = (int)$id; $after_id = (int)$after_id; $parent_id = (int)$parent_id; $this->set($this->_params); if ($after_id) { $sql = 'SELECT `'.$this->id_col.'`, `'.$this->sort_col.'` FROM `'.$this->prefix.$this->table.'` WHERE `'.$this->parent_id_col.'`='.$parent_id.' AND `'.$this->id_col.'`!='.$id.' ORDER BY `'.$this->sort_col.'`, `'.$this->id_col.'`'; $qry = DB::qry($sql,0,0); $increase = $increased = false; $i = 1; while ($rs = DB::fetch($qry)) { if ($increase) { if ($increased) { $sort++; } else { $sort+=2; $increased = true; } } else { $sort = $i; } if ($rs[$this->id_col]==$after_id) { $increase = true; } $sql = 'UPDATE `'.$this->prefix.$this->table.'` SET `'.$this->sort_col.'`='.$sort.' WHERE id='.$rs[$this->id_col]; DB::run($sql); $i++; } $after_sort = DB::row('SELECT `'.$this->sort_col.'` FROM `'.$this->prefix.$this->table.'` WHERE `'.$this->id_col.'`='.$after_id, $this->sort_col); $sql = 'UPDATE `'.$this->prefix.$this->table.'` SET `'.$this->sort_col.'`='.($after_sort+1).', `'.$this->parent_id_col.'`='.$parent_id.' WHERE id='.$id; DB::run($sql); } else { $max = DB::row('SELECT `'.$this->sort_col.'` FROM `'.$this->prefix.$this->table.'` WHERE `'.$this->parent_id_col.'`='.$parent_id.$this->where.' ORDER BY `'.$this->sort_col.'` DESC',$this->sort_col); $sql = 'UPDATE `'.$this->prefix.$this->table.'` SET `'.$this->parent_id_col.'`='.$parent_id.', `'.$this->sort_col.'`='.($max+1).' WHERE id='.$id; DB::run($sql); } if ($this->resort_min) { $this->where = ' AND '.$this->parent_id_col.'='.$parent_id; $this->offset = 0; $this->get(); $this->resort($parent_id); } else { $this->offset = 0; $this->get(); $this->resort(); } $this->set($this->_params); } public function resort($parent_id = 0, $level = 0) { if ($this->offset) return false; foreach ($this->_tree as $i => $a) { if ($a[$this->parent_id_col]==$parent_id) { if (!$this->resort_reverse) { $sort = $this->getSort($level); DB::run('UPDATE `'.$this->prefix.$this->table.'` SET `'.$this->sort_col.'`='.$sort.' WHERE `'.$this->id_col.'`='.$a[$this->id_col]); $this->resort($a[$this->id_col], $level + 1); } else { $this->resort($a[$this->id_col], $level + 1); $sort = $this->getSort($level); DB::run('UPDATE `'.$this->prefix.$this->table.'` SET `'.$this->sort_col.'`='.$sort.' WHERE `'.$this->id_col.'`='.$a[$this->id_col]); } } } } private function getSort($level) { if ($this->resort_smart) { if ($level) { $this->_sort[1]++; $sort = $this->_sort[1]; } else { $this->_sort[0]++; $sort = $this->_sort[0]; } } else { $this->_sort++; $sort = $this->_sort; } return $sort; } |