function repair_tree() { $query = <<<Q SELECT * FROM {$this->table} mto WHERE EXISTS ( SELECT 1 FROM {$this->table} mti WHERE mti.left_id = mto.left_id AND mti.deleted = mto.deleted AND mti.visibility = mto.visibility LIMIT 1, 1 ) ORDER BY left_id ASC, album_type ASC, id ASC LIMIT 1 Q; $fresh = new Album(); $dupes = $fresh->query($query); if (count($dupes->all) < 1) { return true; } $keep = $dupes->all[0]; $diff = $keep->right_id - $keep->left_id + 1; $this->where('visibility', $keep->visibility)->where('deleted', $keep->deleted)->where('right_id >=', $keep->right_id)->where('id !=', $keep->id)->update(array('right_id' => "right_id + {$diff}"), false); $this->where('visibility', $keep->visibility)->where('deleted', $keep->deleted)->where('left_id >=', $keep->left_id)->where('id !=', $keep->id)->update(array('left_id' => "left_id + {$diff}"), false); return $this->repair_tree(); }
function _order($order, $album = false) { $ids = explode(',', $order); $new_order_map = array(); foreach ($ids as $key => $val) { $pos = $key + 1; $new_order_map[$val] = $pos; } $contents = new Album(); $contents->where_in('id', $ids); $sql = $contents->get_sql() . ' ORDER BY FIELD(id, ' . join(',', $ids) . ')'; $contents->query($sql); $next_slot = $album ? $album->left_id + 1 : 1; $this->db->trans_begin(); $start = strtotime(gmdate("M d Y H:i:s", time())); foreach ($contents as $sub_album) { $size = $sub_album->right_id - $sub_album->left_id + 1; if ($sub_album->left_id != $next_slot) { $delta = $sub_album->left_id - $next_slot; $delta = $delta >= 0 ? '- ' . $delta : '+ ' . abs($delta); $_a = new Album(); $_a->where('left_id >=', $sub_album->left_id)->where('right_id <=', $sub_album->right_id)->where('level >=', $sub_album->level)->where('modified_on <', $start)->update(array('left_id' => "left_id {$delta}", 'right_id' => "right_id {$delta}", 'modified_on' => $start), false); } $next_slot += $size; } $this->db->trans_complete(); }