function index() { // TODO: Make sure user is admin over content they trash list($params, $id) = $this->parse_params(func_get_args()); if ($this->method != 'get') { $c = new Content(); $a = new Album(); $t = new Trash(); $tag = new Tag(); $options = array('content' => array(), 'albums' => array()); $params = array_merge($options, $params); if (!empty($params['content'])) { $params['content'] = explode(',', $params['content']); } if (!empty($params['albums'])) { $params['albums'] = explode(',', $params['albums']); } switch ($this->method) { case 'post': $q = array(); $content_ids = array(); $album_ids = array(); $now = time(); if (!empty($params['content'])) { $content = $c->where_in('id', $params['content'])->get_iterated(); foreach ($content as $c) { $q[] = "('content-{$c->id}', '" . $this->db->escape_str(utf8_encode(serialize($c->to_array(array('auth' => $this->auth))))) . "', {$now})"; } } if (!empty($params['albums'])) { foreach ($params['albums'] as $album_id) { $al = new Album(); $al->get_by_id($album_id); if ($al->exists()) { $q[] = "('album-{$al->id}', '" . $this->db->escape_str(utf8_encode(serialize($al->to_array()))) . "', {$now})"; $al->tree_trash(); foreach ($al->categories->get_iterated() as $category) { $category->update_counts('album'); } foreach ($al->tags->get_iterated() as $tag) { $tag->update_counts('album'); } } } $a->update_set_counts(); } if (!empty($q)) { $q = join(',', $q); $this->db->query("INSERT INTO {$t->table} VALUES {$q} ON DUPLICATE KEY UPDATE data = VALUES(data)"); } if (!empty($params['content'])) { $c->where_in('id', $params['content'])->update('deleted', 1); $albums = $a->where_in_related('content', 'id', $params['content'])->get_iterated(); foreach ($albums as $a) { $a->update_counts(); } $previews = $a->where_in_related('cover', 'id', $params['content'])->distinct()->get_iterated(); $prefix = preg_replace('/trash$/', '', $t->table); $this->db->query("DELETE FROM {$prefix}join_albums_covers WHERE cover_id IN(" . join(',', $params['content']) . ")"); foreach ($previews as $a) { $a->reset_covers(); } foreach ($c->where_in('id', $params['content'])->get_iterated() as $content) { foreach ($content->categories->get_iterated() as $category) { $category->update_counts('content'); } foreach ($content->tags->get_iterated() as $tag) { $tag->update_counts('content'); } } } $this->redirect('/trash'); break; case 'delete': $ids = array(); foreach ($params['content'] as $id) { $ids[] = "'content-{$id}'"; } foreach ($params['albums'] as $id) { $ids[] = "'album-{$id}'"; } if (!empty($ids)) { $ids = join(',', $ids); $this->db->query("DELETE FROM {$t->table} WHERE id IN ({$ids})"); } if (!empty($params['albums'])) { foreach ($params['albums'] as $album_id) { $al = new Album(); $al->get_by_id($album_id); if ($al->exists()) { $al->tree_trash_restore(); foreach ($al->categories->get_iterated() as $category) { $category->update_counts('album'); } foreach ($al->tags->get_iterated() as $tag) { $tag->update_counts('album'); } } } $a->update_set_counts(); } if (!empty($params['content'])) { $c->where_in('id', $params['content'])->update('deleted', 0); $covers = $a->where_in_related('cover', 'id', $params['content'])->distinct()->get_iterated(); foreach ($covers as $a) { $a->reset_covers(); } $albums = $a->where_in_related('content', 'id', $params['content'])->get_iterated(); foreach ($albums as $a) { $a->update_counts(); } foreach ($c->where_in('id', $params['content'])->get_iterated() as $content) { foreach ($content->categories->get_iterated() as $category) { $category->update_counts('content'); } foreach ($content->tags->get_iterated() as $tag) { $tag->update_counts('content'); } } } exit; break; } } $options = array('page' => 1, 'limit' => 100); $options = array_merge($options, $params); if (is_numeric($options['limit']) && $options['limit'] > 0) { $options['limit'] = min($options['limit'], 100); } else { $options['limit'] = 100; } $t = new Trash(); $final = $t->paginate($options); $data = $t->order_by('created_on DESC')->get_iterated(); $final['trash'] = array(); foreach ($data as $member) { $content = unserialize(utf8_decode($member->data)); if (!$content) { $content = unserialize($member->data); } if (isset($content['description'])) { $type = 'album'; } else { $type = 'content'; } if ($content) { $final['trash'][] = array('type' => $type, 'data' => $content); } else { $final['total']--; } } $this->set_response_data($final); }
function covers() { list($params, $id) = $this->parse_params(func_get_args()); $params['auth'] = $this->auth; // Standard add/delete cover list($id, $content_id) = $id; if ($this->method === 'get') { $this->redirect("/albums/{$id}"); } $a = new Album($id); $c = new Content(); if (!$a->exists()) { $this->error('404', 'Album not found.'); return; } $cover_count = $a->covers->count(); if ($cover_count > 50) { $this->error('403', 'Only 50 covers can be added to any one album.'); return; } if ($a->album_type == 2 && $cover_count == 0) { $subs = new Album(); $subs->select('id')->where('right_id <', $a->right_id)->where('left_id >', $a->left_id)->where('visibility', $a->visibility)->get_iterated(); $id_arr = array(); foreach ($subs as $sub) { $id_arr[] = $sub->id; } if (!empty($id_arr)) { $subc = new Content(); $covers = $subc->query("SELECT DISTINCT cover_id FROM {$a->db_join_prefix}albums_covers WHERE album_id IN (" . join(',', $id_arr) . ") GROUP BY album_id LIMIT " . (3 - $cover_count)); $f_ids = array(); foreach ($covers as $f) { $f_ids[] = $f->cover_id; } if (!empty($f_ids)) { $subc->query("SELECT id FROM {$subc->table} WHERE id IN(" . join(',', $f_ids) . ") ORDER BY FIELD(id, " . join(',', array_reverse($f_ids)) . ")"); foreach ($subc as $content) { $a->save_cover($content); } } } } if (is_numeric($content_id)) { if ($this->method == 'delete') { $c->where_related('covers', 'id', $id)->get_by_id($content_id); } else { if ($a->album_type == 2) { $c->get_by_id($content_id); } else { $c->where_related('album', 'id', $id)->get_by_id($content_id); } } if (!$c->exists()) { $this->error('404', 'Content not found.'); return; } if ($this->method == 'delete') { $a->delete_cover($c); $a->reset_covers(); } else { $a->delete_cover($c); $a->save_cover($c); } } else { $content_id = explode(',', $content_id); if ($this->method == 'delete') { $c->where_related('covers', 'id', $id)->where_in('id', $content_id)->get_iterated(); } else { if ($a->album_type == 2) { $c->where_in('id', $content_id)->get_iterated(); } else { $c->where_related('album', 'id', $id)->where_in('id', $content_id)->get_iterated(); } } if (!$c->result_count()) { $this->error('404', 'Content not found.'); return; } if ($this->method == 'delete') { foreach ($c as $cover) { $a->delete_cover($cover); } $a->reset_covers(); } else { foreach ($c as $cover) { $a->delete_cover($cover); } foreach ($content_id as $cid) { $a->save_cover($c->get_by_id($cid)); } } } $this->redirect("/albums/{$id}"); }
function do_delete() { $a = new Album(); $previews = $a->where_related('cover', 'id', $this->id)->get_iterated(); foreach ($previews as $a) { $a->reset_covers(); } $albums = $a->where_related('content', 'id', $this->id)->get_iterated(); foreach ($albums as $a) { $a->update_counts(); } $this->clear_cache(); if (empty($this->storage_url)) { $original = $this->path_to_original(); $info = pathinfo($original); $mid = preg_replace('/\\.' . $info['extension'] . '$/', '.1600.' . $info['extension'], $original); unlink($original); if (file_exists($mid)) { unlink($mid); } if ($this->file_type > 0 && is_dir($original . '_previews')) { delete_files($original . '_previews', true, 1); } if (@rmdir(dirname($original))) { @rmdir(dirname(dirname($original))); } } else { Shutter::delete_original($this->storage_url); if (!empty($this->storage_url_midsize)) { Shutter::delete_original($this->storage_url_midsize); } } Shutter::hook('content.delete', $this->to_array(array('auth' => true))); $s = new Slug(); $this->db->query("DELETE FROM {$s->table} WHERE id = 'content.{$this->slug}'"); $this->delete(); }