/** * TC12: Create operation on Albums * Test to verify that the database can save new albums * The test is performed by creating an album object with the required input fields * Once the test is complete no errors will appear and the album will be in the database */ public function testCreateAlbum() { //Create the album $album = new Album(); $album['artist_id'] = $this->artist['artist_id']; $album['name'] = 'testalbum2'; $album['added_by_user_id'] = 1; //SYSTEM user $album->save(); //Verify it exist now $this->assertTrue($album->exists()); }
function build_autos($items, $data, $user) { foreach ($items as $index => &$item) { if (isset($item['auto'])) { if (isset($data['urls'][$item['auto']])) { $item['path'] = $data['urls'][$item['auto']]; } else { if ($item['auto'] === 'set') { $item['path'] = ''; } } if ($item['auto'] === 'profile') { switch ($item['id']) { case 'twitter': $item['path'] = 'https://twitter.com/' . $user->twitter; break; default: $item['path'] = $user->{$item['id']}; if (empty($item['path'])) { unset($items[$index]); continue; } break; } if (!isset($item['label']) || empty($item['label'])) { $item['label'] = ucwords($item['id']) . ($item['id'] === 'google' ? '+' : ''); } } else { if ($item['auto'] === 'rss') { $item['path'] = '/feed/' . $item['id'] . ($item['id'] === 'essay' ? 's' : '') . '/recent.rss'; if (!isset($item['label'])) { $item['label'] = $data['url_data'][$item['id']]['plural'] . ' RSS'; } } else { if (preg_match('/s$/', $item['auto']) || $item['auto'] === 'timeline') { if ($item['auto'] === 'timeline' && isset($item['year'])) { $item['path'] .= $item['year'] . '/'; if (isset($item['month']) && $item['month'] !== false && $item['month'] !== 'any') { $m = str_pad($item['month'], 2, '0', STR_PAD_LEFT); $item['path'] .= $m . '/'; } } if (strpos($item['auto'], '_') !== false) { foreach (array('id', 'slug', 'month', 'year', 'day') as $id) { if ($id === 'month') { if (!isset($item['month']) || $item['month'] === 'any' || $item['month'] === false) { $item['month'] = ''; } else { $item['month'] = str_pad($item['month'], 2, '0', STR_PAD_LEFT); } } if ($id === 'day' && !isset($item['day'])) { $item['day'] = ''; } if ($id === 'slug' && !isset($item['slug']) && isset($item['id'])) { if (strpos($item['auto'], 'tag_') === 0) { $item['slug'] = $item['id']; } else { $c = new Category(); if (is_numeric($item['id'])) { $c->select('slug')->get_by_id($item['id']); $item['slug'] = $c->slug; } else { $item['slug'] = $item['id']; } } } if (isset($item[$id])) { $item['path'] = str_replace(":{$id}", $item[$id], $item['path']); } } } else { if (!isset($item['label'])) { $item['label'] = $data['url_data'][$item['auto'] === 'categories' ? 'category' : rtrim($item['auto'], 's')]['plural']; } } } else { if ($item['auto'] === 'home') { if (!isset($item['label'])) { $item['label'] = $data['url_data']['home']; } $item['path'] = '/home/'; } else { if ($item['auto'] === 'album' || $item['auto'] === 'set') { $a = new Album(); $a->select('id,slug,created_on,title'); if (is_numeric($item['id'])) { $a->where('id', $item['id']); } else { $a->where('slug', $item['id'])->or_where('internal_id', $item['id']); } $a->get(); if (!$a->exists()) { unset($items[$index]); continue; } $item['path'] = str_replace(':id', $a->id, $item['path']); $item['path'] = str_replace(':slug', $a->slug, $item['path']); $item['path'] = str_replace(':year', date('Y', $a->created_on), $item['path']); $item['path'] = str_replace(':month', date('m', $a->created_on), $item['path']); $item['path'] = str_replace(':day', date('d', $a->created_on), $item['path']); if (!isset($item['label'])) { $item['label'] = $a->title; } } else { if ($item['auto'] === 'page' || $item['auto'] === 'essay') { $t = new Text(); $t->select('id,slug,published_on,title'); if (is_numeric($item['id'])) { $t->where('id', $item['id']); } else { $t->where('slug', $item['id']); } $t->get(); if (!$t->exists()) { unset($items[$index]); continue; } $item['path'] = str_replace(':id', $t->id, $item['path']); $item['path'] = str_replace(':slug', $t->slug, $item['path']); $item['path'] = str_replace(':year', date('Y', $t->published_on), $item['path']); $item['path'] = str_replace(':month', date('m', $t->published_on), $item['path']); $item['path'] = str_replace(':day', date('d', $t->published_on), $item['path']); if (!isset($item['label'])) { $item['label'] = $t->title; } } else { if ($item['auto'] === 'content') { $c = new Content(); $c->select('id,slug,captured_on,title'); if (isset($item['album_id'])) { $item['path'] = preg_replace('/:(id|slug)/', ':album_$1', $data['urls']['album']) . substr(str_replace(':year/:month/', '', $data['urls']['content']), 1); $a = new Album(); $a->select('id,slug,created_on,title'); if (is_numeric($item['album_id'])) { $a->where('id', $item['album_id']); } else { $a->where('slug', $item['album_id'])->or_where('internal_id', $item['album_id']); } $a->get(); if (!$a->exists()) { unset($items[$index]); continue; } $item['path'] = str_replace(':album_id', $a->id, $item['path']); $item['path'] = str_replace(':album_slug', $a->slug, $item['path']); $date = $a->created_on; } else { $date = $c->captured_on; } if (is_numeric($item['id'])) { $c->where('id', $item['id']); } else { $c->where('slug', $item['id'])->or_where('internal_id', $item['id']); } $c->get(); if (!$c->exists()) { unset($items[$index]); continue; } $item['path'] = str_replace(':id', $c->id, $item['path']); $item['path'] = str_replace(':slug', $c->slug, $item['path']); $item['path'] = str_replace(':year', date('Y', $date), $item['path']); $item['path'] = str_replace(':month', date('m', $date), $item['path']); $item['path'] = str_replace(':day', date('d', $date), $item['path']); if (!isset($item['label'])) { $item['label'] = $c->title; } if (isset($item['lightbox']) && $item['lightbox']) { $item['path'] .= 'lightbox/'; } } else { if ($item['auto'] === 'tag') { $item['path'] = str_replace(':slug', $item['id'], $item['path']); } } } } } } } } if ($item['auto'] !== 'profile') { $item['path'] = str_replace(array(':year', ':month'), '', $item['path']); $item['path'] = preg_replace('/[\\(\\)\\?\\:]/', '', $item['path']); $item['path'] = preg_replace('~[/]+~', '/', $item['path']); } } } return $items; }
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 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 context($params, $auth) { if (!$params['neighbors']) { $single_neighbors = true; $n = 1; } else { $single_neighbors = false; $n = $params['neighbors'] / 2; } $to_arr_options = array('auth' => $auth); if (!isset($params['context_order'])) { $params['context_order'] = 'left_id'; $params['context_order_direction'] = 'ASC'; } if ($params['context_order'] === 'manual') { $params['context_order'] = 'left_id'; } $next_operator = strtolower($params['context_order_direction']) === 'asc' ? '>' : '<'; $prev_operator = $next_operator === '>' ? '<' : '>'; $arr = array(); $next = new Album(); $prev = new Album(); if (isset($params['context']) && strpos($params['context'], 'tag-') === 0) { $tag = str_replace('tag-', '', urldecode($params['context'])); $t = new Tag(); $t->where('name', $tag)->get(); if ($t->exists()) { $prev->where('deleted', 0)->where('id !=', $this->id)->where('title <', $this->title)->where_related_tag('id', $t->id)->order_by('title DESC, id DESC'); $next->where('deleted', 0)->where('id !=', $this->id)->where('title >', $this->title)->where_related_tag('id', $t->id)->order_by('title ASC, id ASC'); $arr['type'] = 'tag'; $arr['title'] = $tag; $arr['slug'] = $tag; $to_arr_options['context'] = "tag-{$tag}"; $t->model = 'tag_albums'; $t->slug = $t->name; $url = $t->url(); if ($url) { list($arr['__koken_url'], $arr['url']) = $url; } } } else { if (isset($params['context']) && strpos($params['context'], 'category-') === 0) { $category = str_replace('category-', '', $params['context']); $cat = new Category(); $cat->where('slug', $category)->get(); if ($cat->exists()) { $prev->where('deleted', 0)->where('id !=', $this->id)->where('title <', $this->title)->where_related_category('id', $cat->id)->order_by('title DESC, id DESC'); $next->where('deleted', 0)->where('id !=', $this->id)->where('title >', $this->title)->where_related_category('id', $cat->id)->order_by('title ASC, id ASC'); $arr['type'] = 'category'; $arr['title'] = $cat->title; $arr['slug'] = $cat->slug; $to_arr_options['context'] = "category-{$cat->id}"; $cat->model = 'category_albums'; $url = $cat->url(); if ($url) { list($arr['__koken_url'], $arr['url']) = $url; } } } else { $prev->where('deleted', 0)->where("{$params['context_order']} {$prev_operator}", $this->{$params['context_order']})->where('level', $this->level)->order_by("{$params['context_order']} " . ($prev_operator === '<' ? 'DESC' : 'ASC')); $next->where('deleted', 0)->where("{$params['context_order']} {$next_operator}", $this->{$params['context_order']})->where('level', $this->level)->order_by("{$params['context_order']} {$params['context_order_direction']}"); if ($this->level > 1) { $parent = new Album(); $parent->select('left_id,right_id')->where('left_id <', $this->left_id)->where('level <', $this->level)->where('visibility', $this->visibility)->where('deleted', 0)->order_by('left_id DESC')->limit(1)->get(); if ($parent->exists()) { $next->where('left_id >', $parent->left_id); $next->where('right_id <', $parent->right_id); $prev->where('left_id >', $parent->left_id); $prev->where('right_id <', $parent->right_id); } } } } if (!$auth) { $next->where('visibility', 0); $prev->where('visibility', 0); } if (!$params['include_empty_neighbors']) { $next->where('total_count >', 0); $prev->where('total_count >', 0); } $max = $next->get_clone()->count(); $min = $prev->get_clone()->count(); $arr['total'] = $max + $min + 1; $arr['position'] = $min + 1; $pre_limit = $next_limit = $n; if ($min < $pre_limit) { $next_limit += $pre_limit - $min; $pre_limit = $min; } if ($max < $next_limit) { $pre_limit = min($min, $pre_limit + ($next_limit - $max)); $next_limit = $max; } $arr['previous'] = array(); $arr['next'] = array(); if ($next_limit > 0) { $next->limit($next_limit)->get_iterated(); foreach ($next as $a) { $arr['next'][] = $a->to_array($to_arr_options); } } if ($pre_limit > 0) { $prev->limit($pre_limit)->get_iterated(); foreach ($prev as $a) { $arr['previous'][] = $a->to_array($to_arr_options); } $arr['previous'] = array_reverse($arr['previous']); } return $arr; }