public function index($tag_id, $offset = 0) { $columns = array(); $configs = array('work-tag', $this->tag->id, 'works', '%s'); $conditions = conditions($columns, $configs); WorkTagMapping::addConditions($conditions, 'work_tag_id = ?', $this->tag->id); $limit = 12; $total = WorkTagMapping::count(array('conditions' => $conditions)); $offset = $offset < $total ? $offset : 0; $this->load->library('pagination'); $pagination = $this->pagination->initialize(array_merge(array('total_rows' => $total, 'num_links' => 3, 'per_page' => $limit, 'uri_segment' => 0, 'base_url' => '', 'page_query_string' => false, 'first_link' => '第一頁', 'last_link' => '最後頁', 'prev_link' => '上一頁', 'next_link' => '下一頁', 'full_tag_open' => '<ul class="pagination">', 'full_tag_close' => '</ul>', 'first_tag_open' => '<li class="f">', 'first_tag_close' => '</li>', 'prev_tag_open' => '<li class="p">', 'prev_tag_close' => '</li>', 'num_tag_open' => '<li>', 'num_tag_close' => '</li>', 'cur_tag_open' => '<li class="active"><a href="#">', 'cur_tag_close' => '</a></li>', 'next_tag_open' => '<li class="n">', 'next_tag_close' => '</li>', 'last_tag_open' => '<li class="l">', 'last_tag_close' => '</li>'), $configs))->create_links(); $work_ids = column_array(WorkTagMapping::find('all', array('select' => 'work_id', 'offset' => $offset, 'limit' => $limit, 'order' => 'work_id DESC', 'conditions' => $conditions)), 'work_id'); $works = $work_ids ? Work::find('all', array('order' => 'FIELD(id,' . implode(',', $work_ids) . ')', 'conditions' => array('is_enabled = ? AND id IN (?) AND destroy_user_id IS NULL', Work::ENABLE_YES, $work_ids))) : array(); if ($works) { $this->add_meta(array('name' => 'keywords', 'content' => implode(',', column_array($works, 'title')) . ',' . implode(',', Cfg::setting('site', 'site', 'keywords'))))->add_meta(array('name' => 'description', 'content' => $works[0]->mini_content(150)))->add_meta(array('property' => 'og:title', 'content' => $this->tag->name . '作品' . ' - ' . Cfg::setting('site', 'site', 'title')))->add_meta(array('property' => 'og:description', 'content' => $works[0]->mini_content(300)))->add_meta(array('property' => 'og:image', 'tag' => 'larger', 'content' => $img = $works[0]->cover->url('1200x630c'), 'alt' => $this->tag->name . '作品' . ' - ' . Cfg::setting('site', 'site', 'title')))->add_meta(array('property' => 'og:image:type', 'tag' => 'larger', 'content' => 'image/' . pathinfo($img, PATHINFO_EXTENSION)))->add_meta(array('property' => 'og:image:width', 'tag' => 'larger', 'content' => '1200'))->add_meta(array('property' => 'og:image:height', 'tag' => 'larger', 'content' => '630'))->add_meta(array('property' => 'article:modified_time', 'content' => $works[0]->updated_at->format('c')))->add_meta(array('property' => 'article:published_time', 'content' => $works[0]->created_at->format('c'))); if (($tags = column_array($works[0]->tags, 'name')) || ($tags = Cfg::setting('site', 'site', 'keywords'))) { foreach ($tags as $i => $tag) { if (!$i) { $this->add_meta(array('property' => 'article:section', 'content' => $tag))->add_meta(array('property' => 'article:tag', 'content' => $tag)); } else { $this->add_meta(array('property' => 'article:tag', 'content' => $tag)); } } } } return $this->set_title($this->tag->name . '作品' . ' - ' . Cfg::setting('site', 'site', 'title'))->set_class('works')->set_method('index')->load_view(array('tag' => $this->tag, 'works' => $works, 'pagination' => $pagination, 'columns' => $columns)); }
public function work($id) { if (!($id && ($work = Work::find('one', array('conditions' => array('id = ? AND is_enabled = ? AND destroy_user_id IS NULL', $id, Work::ENABLE_YES)))))) { return array(); } if ($work->tags) { usort($work->tags, function ($a, $b) { return count($a->mappings) < count($b->mappings); }); } if ($work->tags && ($work_ids = column_array(WorkTagMapping::find('all', array('select' => 'work_id', 'order' => 'RAND()', 'limit' => 5, 'conditions' => array('work_id != ? AND work_tag_id = ?', $work->id, $work->tags[0]->id))), 'work_id'))) { Work::addConditions($conditions, 'id IN (?)', $work_ids); } else { Work::addConditions($conditions, 'id != ?', $work->id); } Work::addConditions($conditions, 'is_enabled = ? AND destroy_user_id IS NULL', Work::ENABLE_YES); $others = Work::find('all', array('select' => 'id, title', 'limit' => 5, 'conditions' => $conditions)); return array('work' => array_merge($work->to_array(array('only' => array('id', 'title', 'content', 'pv'), 'methods' => array('site_show_page_last_uri'))), array('mini_content' => array('150' => $work->mini_content(150), '300' => $work->mini_content(300)), 'cover_url' => array('1200x630c' => $work->cover->url('1200x630c')), 'updated_at' => array('c' => $work->updated_at->format('c'), 'Y-m-d H:i:s' => $work->updated_at->format('Y-m-d H:i:s')), 'created_at' => array('c' => $work->created_at->format('c'), 'Y-m-d H:i:s' => $work->created_at->format('Y-m-d H:i:s')))), 'tags' => array_map(function ($tag) { return $tag->to_array(array('only' => array('id', 'name'))); }, $work->tags), 'others' => array_map(function ($work) { return $work->to_array(array('only' => array(), 'methods' => array('site_show_page_last_uri'))); }, $others), 'blocks' => array_map(function ($block) { return array('title' => $block->title, 'items' => array_map(function ($item) { return $item->to_array(array('only' => array('link', 'title'))); }, $block->items)); }, WorkBlock::find('all', array('include' => array('items'), 'conditions' => array('work_id = ?', $work->id)))), 'pictures' => array_map(function ($picture) { return array('name' => array('800w' => $picture->name->url('800w'))); }, $work->pictures)); }
public function index($tag_id, $offset = 0) { $columns = array(array('key' => 'title', 'title' => '標題', 'sql' => 'title LIKE ?'), array('key' => 'content', 'title' => '內容', 'sql' => 'content LIKE ?')); $configs = array('admin', $this->get_class(), $this->tag->id, 'works', '%s'); $conditions = conditions($columns, $configs); Work::addConditions($conditions, 'destroy_user_id IS NULL'); if ($work_id = column_array(WorkTagMapping::find('all', array('select' => 'work_id', 'conditions' => array('work_tag_id = ?', $this->tag->id))), 'work_id')) { Work::addConditions($conditions, 'id IN (?)', $work_id); } else { Work::addConditions($conditions, 'id = ?', -1); } $limit = 25; $total = Work::count(array('conditions' => $conditions)); $offset = $offset < $total ? $offset : 0; $this->load->library('pagination'); $pagination = $this->pagination->initialize(array_merge(array('total_rows' => $total, 'num_links' => 5, 'per_page' => $limit, 'uri_segment' => 0, 'base_url' => '', 'page_query_string' => false, 'first_link' => '第一頁', 'last_link' => '最後頁', 'prev_link' => '上一頁', 'next_link' => '下一頁', 'full_tag_open' => '<ul class="pagination">', 'full_tag_close' => '</ul>', 'first_tag_open' => '<li>', 'first_tag_close' => '</li>', 'prev_tag_open' => '<li>', 'prev_tag_close' => '</li>', 'num_tag_open' => '<li>', 'num_tag_close' => '</li>', 'cur_tag_open' => '<li class="active"><a href="#">', 'cur_tag_close' => '</a></li>', 'next_tag_open' => '<li>', 'next_tag_close' => '</li>', 'last_tag_open' => '<li>', 'last_tag_close' => '</li>'), $configs))->create_links(); $works = Work::find('all', array('offset' => $offset, 'limit' => $limit, 'order' => 'id DESC', 'include' => array('pictures'), 'conditions' => $conditions)); return $this->set_tab_index(2)->set_subtitle($this->tag->name . '內的作品列表')->load_view(array('tag' => $this->tag, 'works' => $works, 'pagination' => $pagination, 'columns' => $columns)); }
public function update() { if (!$this->has_post()) { return redirect_message(array('admin', $this->get_class(), $this->work->id, 'edit'), array('_flash_message' => '非 POST 方法,錯誤的頁面請求。')); } $posts = OAInput::post(); $cover = OAInput::file('cover'); if (!((string) $this->work->cover || $cover)) { return redirect_message(array('admin', $this->get_class(), $this->work->id, 'edit'), array('_flash_message' => '請選擇圖片(gif、jpg、png)檔案!', 'posts' => $posts)); } if ($msg = $this->_validation_posts($posts)) { return redirect_message(array('admin', $this->get_class(), $this->work->id, 'edit'), array('_flash_message' => $msg, 'posts' => $posts)); } if ($columns = array_intersect_key($posts, $this->work->table()->columns)) { foreach ($columns as $column => $value) { $this->work->{$column} = $value; } } $work = $this->work; $update = WorkTag::transaction(function () use($work, $cover) { if (!$work->save()) { return false; } if ($cover && !$work->cover->put($cover)) { return false; } return true; }); if (!$update) { return redirect_message(array('admin', $this->get_class(), $this->work->id, 'edit'), array('_flash_message' => '更新失敗!', 'posts' => $posts)); } if (($del_ids = array_diff(column_array($work->pictures, 'id'), $posts['pic_ids'])) && ($pictures = WorkPicture::find('all', array('select' => 'id, name', 'conditions' => array('id IN (?)', $del_ids))))) { foreach ($pictures as $picture) { WorkPicture::transaction(function () use($picture) { return $picture->destroy(); }); } } if ($pictures = OAInput::file('pictures[]')) { foreach ($pictures as $picture) { WorkPicture::transaction(function () use($picture, $work) { return verifyCreateOrm($pic = WorkPicture::create(array_intersect_key(array_merge($picture, array('work_id' => $work->id)), WorkPicture::table()->columns))) && $pic->name->put($picture); }); } } $ori_ids = column_array($work->mappings, 'work_tag_id'); if (($del_ids = array_diff($ori_ids, $posts['tag_ids'])) && ($mappings = WorkTagMapping::find('all', array('select' => 'id, work_tag_id', 'conditions' => array('work_id = ? AND work_tag_id IN (?)', $work->id, $del_ids))))) { foreach ($mappings as $mapping) { WorkTagMapping::transaction(function () use($mapping) { return $mapping->destroy(); }); } } if (($add_ids = array_diff($posts['tag_ids'], $ori_ids)) && ($tags = WorkTag::find('all', array('select' => 'id', 'conditions' => array('id IN (?)', $add_ids))))) { foreach ($tags as $tag) { WorkTagMapping::transaction(function () use($tag, $work) { return verifyCreateOrm(WorkTagMapping::create(array_intersect_key(array('work_tag_id' => $tag->id, 'work_id' => $work->id), WorkTagMapping::table()->columns))); }); } } $clean_blocks = WorkBlock::transaction(function () use($work) { foreach ($work->blocks as $block) { if (!$block->destroy()) { return false; } } return true; }); if ($blocks = $posts['blocks']) { foreach ($blocks as $block) { if (!($b = null) && WorkBlock::transaction(function () use($block, $work, &$b) { return verifyCreateOrm($b = WorkBlock::create(array_intersect_key(array_merge($block, array('work_id' => $work->id)), WorkBlock::table()->columns))); })) { if (($items = $block['items']) && $b) { foreach ($items as $item) { WorkBlockItem::transaction(function () use($item, $b) { return verifyCreateOrm(WorkBlockItem::create(array_intersect_key(array_merge($item, array('work_block_id' => $b->id)), WorkBlockItem::table()->columns))); }); } } } } } $this->_clean_cell($work); return redirect_message(($url = Session::getData('admin_works_index_url')) ? $url : array('admin', $this->get_class()), array('_flash_message' => '更新成功!')); }