require_once(dirname(__FILE__).'/../apps/bootstrap.php'); ?> //$_GETのパラメータ - d:日付, c:カテゴリ, i:ブログID $sel = $qs = array(); foreach(array('d','c','i') as $k) $qs[$k] = $sel[$k] = Arr::get($_GET, $k, ''); if(!in_array($sel['c'], array_keys($blog_cats))) { $sel['c'] = '0'; } $sel['c'] = (int)$sel['c']; $dates = DB::query(" SELECT DATE_FORMAT(blog_date, '%Y-%m') AS blog_sel, DATE_FORMAT(blog_date, '%Y年%m月') AS blog_ym, COUNT(*) AS blog_cnt FROM blog WHERE ".($sel['c'] ? 'blog_cat = '.$sel['c'].' AND' : '')." (blog_st IS NULL OR blog_st <= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND (blog_ed IS NULL OR blog_ed >= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND blog_delflg = 0 GROUP BY blog_sel, blog_ym ORDER BY blog_sel DESC ")->all(); $cats = DB::query(" SELECT blog_cat, COUNT(*) AS blog_cnt FROM blog WHERE (blog_st IS NULL OR blog_st <= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND (blog_ed IS NULL OR blog_ed >= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND blog_delflg = 0 GROUP BY blog_cat ORDER BY blog_cat ")->all(); $blog_cnt = 0; foreach($cats as $cat) { $blog_cnt += $cat['blog_cnt']; } if($sel['d'] !== '') { $is_match = false; foreach($dates as $date) { $is_match = $sel['d'] == $date['blog_sel']; if($is_match) { break; } } if(!$is_match) $sel['d'] = ''; } if(!ctype_digit($sel['i'])) $sel['i'] = ''; if($sel['d'] && !$sel['i']) { $sel['i'] = DB::query(" SELECT blog_id FROM blog WHERE ".($sel['c'] ? 'blog_cat = '.$sel['c'].' AND' : '')." DATE_FORMAT(blog_date, '%Y-%m') = :blog_sel AND (blog_st = IS NULL OR blog_st <= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND (blog_ed = IS NULL OR blog_ed >= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND blog_delflg = 0 ORDER BY blog_id DESC LIMIT 0, 1 ")->one(array(':blog_sel' => $sel['d'])); } if($sel['i']) { $row = DB::query(" SELECT *, DATE_FORMAT(blog_date, '%Y年%m月%d日') AS blog_date FROM blog WHERE blog_id = :blog_id AND (blog_st = IS NULL OR blog_st <= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND (blog_ed = IS NULL OR blog_ed >= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND blog_delflg = 0 ")->row(array(':blog_id' => $sel['i'])); } //初期ページは最新の記事 if(empty($row)) { $row = DB::query(" SELECT *, DATE_FORMAT(blog_date, '%Y-%m') AS blog_sel, DATE_FORMAT(blog_date, '%Y年%m月%d日') AS formatted_blog_date FROM blog WHERE ".($sel['c'] ? 'blog_cat = '.$sel['c'].' AND' : '')." (blog_st IS NULL OR blog_st <= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND (blog_ed IS NULL OR blog_ed >= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND blog_delflg = 0 ORDER BY blog_date DESC, blog_id DESC LIMIT 0, 1 ")->row(); if($row) { $sel['d'] = $row['blog_sel']; $sel['i'] = $row['blog_id']; } } if($row) { $tmp = DB::query(" SELECT blog_id FROM blog WHERE ".($sel['c'] ? 'blog_cat = '.$sel['c'].' AND' : '')." DATE_FORMAT(blog_date, '%Y-%m') = :blog_sel AND (blog_st = IS NULL OR blog_st <= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND (blog_ed = IS NULL OR blog_ed >= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND blog_delflg = 0 ORDER BY blog_date DESC, blog_id DESC ")->all(array(':blog_sel' => $sel['d'])); $list = array(); foreach($tmp as $t) { $list[] = $t['blog_id']; } $pos = array_search($sel['i'], $list); if($pos !== false) { $stmt = DB::query(" SELECT blog_id, blog_title, DATE_FORMAT(blog_date, '%Y-%m') AS blog_sel, DATE_FORMAT(blog_date, '%Y年%m月%d日') AS blog_date FROM blog WHERE ".($sel['c'] ? 'blog_cat = '.$sel['c'].' AND' : '')." DATE_FORMAT(blog_date, '%Y-%m') = :blog_sel AND blog_id = :blog_id AND (blog_st = IS NULL OR blog_st <= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND (blog_ed = IS NULL OR blog_ed >= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND blog_delflg = 0 "); if($pos > 0) { $first = $stmt->row(array(':blog_sel' => $sel['d'], ':blog_id' => $list[0])); $prev = $stmt->row(array(':blog_sel' => $sel['d'], ':blog_id' => $list[$pos - 1])); } if($pos < count($list) - 1) { $last = $stmt->row(array(':blog_sel' => $sel['d'], ':blog_id' => $list[count($list) - 1])); $next = $stmt->row(array(':blog_sel' => $sel['d'], ':blog_id' => $list[$pos + 1])); } } } $newest = DB::query(" SELECT blog_id, DATE_FORMAT(blog_date, '%Y年%m月%d日') AS formatted_blog_date, blog_cat, blog_title FROM blog WHERE (blog_st IS NULL OR blog_st <= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND (blog_ed IS NULL OR blog_ed >= DATE_FORMAT(NOW(), '%Y-%m-%d')) AND blog_delflg = 0 ORDER BY blog_date DESC, blog_id DESC LIMIT 0, 5 ")->all(); ?>