$page]), 301, []);
}
$storage = new StorageClient([
'keyFilePath' => env('keyFilePath'),
]);
$bucket = $storage->bucket(env('storage'));
$unit_config = $this->get_topic_setting();
$topic_categorys = $this->get_topic_categorys();
$topic_category_url_name_arr = $this->get_topic_category_url_name_arr($topic_categorys);
$current = $page;
$perpage = $unit_config['perpage'];
$topic = $this->get_topic_pagination($page, $perpage);
foreach ($topic as $k => $v) {
// $gcs_image_arr = json_decode($v['gcs_image_arr'], true);
$gcs_image_arr = is_array($v['gcs_image_arr']) ? $v['gcs_image_arr'] : json_decode($v['gcs_image_arr'], true);
if (! is_null($gcs_image_arr) && isset($topic_category_url_name_arr[$v['pid']])) {
if (! is_dir(public_path() . "/userfiles/")) {
mkdir(public_path() . "/userfiles/", 0777);
}
if (! is_dir(public_path() . "/userfiles/Topic/")) {
mkdir(public_path() . "/userfiles/Topic/", 0777);
}
if (! is_dir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$v['pid']]}/")) {
mkdir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$v['pid']]}/", 0777);
}
if (! is_dir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$v['pid']]}/{$v['id']}/")) {
mkdir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$v['pid']]}/{$v['id']}/", 0777);
}
foreach ($gcs_image_arr as $key => $value) {
if (! is_file(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$v['pid']]}/{$v['id']}/" . str_replace(env('gcs_perfix'), '', $value))) {
$object = $bucket->object($value);
if ($object->exists()) {
$stream = $object->downloadToFile(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$v['pid']]}/{$v['id']}/" . str_replace(env('gcs_perfix'), '', $value));
}
}
}
}
}
$count = Topic::where('status', 1)->count();
$pages = ceil($count / $perpage);
if ($pages == 0) {$pages = 1;}
if ($page > $pages) {
return redirect(route('topic'), 301, []);
}
$page_result = Tools::get_prev_next($page, $pages);
$schema_breadcrumbList = [
'context' => 'https://schema.org',
'type' => 'BreadcrumbList',
'itemListElement' => [
[
'type' => 'ListItem',
'position' => 1,
'name' => '首页',
'item' => route('index'),
],
[
'type' => 'ListItem',
'position' => 2,
'name' => '专题研究',
'item' => route('topic'),
],
],
];
$schema = [
'context' => 'https://schema.org',
'type' => 'ItemList',
];
// LongBanner
$longbanner = BannerHandler::longbanner(2, "search_active", ['unit' => 'topic', 'cat' => ['topic']]);
// ShortBanner
$top_shortbanner = BannerHandler::shortbanner("top", ['unit' => 'topic', 'cat' => ['topic']]);
$shortbanner = BannerHandler::shortbanner("search_active", ['unit' => 'topic', 'cat' => ['topic']]);
// WebsiteLink
$websitelink = WebsiteLinkHandler::get_weblink(['unit' => 'topic', 'cat' => ['topic']]);
// CTA
$config = config('topic');
$cta_status = $config['cta_status'];
$cta_content = $config['cta_content'];
$data = [
'system_code' => 'topic',
'unit_config' => $unit_config,
'topic_categorys' => $topic_categorys,
'topic_category_url_name_arr' => $topic_category_url_name_arr,
'topic' => $topic,
'current' => $current,
'pages' => $pages,
'erotic_novel_tag' => $this->get_erotic_novel_tag(),
'long_banners' => $longbanner,
'short_banners' => $shortbanner,
'top_short_banners' => $top_shortbanner,
'cta_status' => $cta_status,
'cta_content' => $cta_content,
'websitelink' => $websitelink,
/* Schema LD-json */
'schema_breadcrumbList' => $schema_breadcrumbList,
'schema' => $schema,
'prev' => (! empty($page_result[0])) ? $page_result[0] : '',
'next' => (! empty($page_result[1])) ? $page_result[1] : '',
];
return view('topic', Layout_Setting::setting($data, 'topic'));
}
public function category_detail($category_url_name, $page = 1)
{
// 網址中包含空白,跳轉404
if (strpos($category_url_name, ' ') !== false) {
return redirect(404);
}
if ($page < 1 || ! is_numeric($page)) {
return redirect(route('topic_category', ['category_url_name' => $category_url_name]), 301, []);
}
if (gettype($page) != 'integer' && ! isset($page[1])) {
$page = intval($page);
if ($page < 10) {$page = '0' . $page;}
return redirect(route('topic_category_page', ['category_url_name' => $category_url_name, 'page' => $page]), 301, []);
}
$storage = new StorageClient([
'keyFilePath' => env('keyFilePath'),
]);
$bucket = $storage->bucket(env('storage'));
$unit_config = $this->get_topic_setting();
$topic_categorys = $this->get_topic_categorys();
$topic_category = $this->get_topic_category($category_url_name);
if (is_null($topic_category)) {
return redirect(route('topic'), 301, []);
}
$topic_category_url_name_arr = $this->get_topic_category_url_name_arr($topic_categorys);
$pid = $topic_category['id'];
$current = $page;
$perpage = $unit_config['perpage'];
$topic = $this->get_topic_category_pagination($category_url_name, $pid, $page, $perpage);
$count = Topic::where('pid', $pid)->where('status', 1)->count();
$pages = ceil($count / $perpage);
if ($pages == 0) {$pages = 1;}
if ($page > $pages) {
return redirect(route('topic_category', ['category_url_name' => $category_url_name]), 301, []);
}
$page_result = Tools::get_prev_next($page, $pages);
foreach ($topic as $k => $v) {
// $gcs_image_arr = json_decode($v['gcs_image_arr'], true);
$gcs_image_arr = is_array($v['gcs_image_arr']) ? $v['gcs_image_arr'] : json_decode($v['gcs_image_arr'], true);
if (! is_null($gcs_image_arr) && isset($topic_category_url_name_arr[$v['pid']])) {
if (! is_dir(public_path() . "/userfiles/")) {
mkdir(public_path() . "/userfiles/", 0777);
}
if (! is_dir(public_path() . "/userfiles/Topic/")) {
mkdir(public_path() . "/userfiles/Topic/", 0777);
}
if (! is_dir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$v['pid']]}/")) {
mkdir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$v['pid']]}/", 0777);
}
if (! is_dir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$v['pid']]}/{$v['id']}/")) {
mkdir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$v['pid']]}/{$v['id']}/", 0777);
}
foreach ($gcs_image_arr as $key => $value) {
if (! is_file(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$v['pid']]}/{$v['id']}/" . str_replace(env('gcs_perfix'), '', $value))) {
$object = $bucket->object($value);
if ($object->exists()) {
$stream = $object->downloadToFile(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$v['pid']]}/{$v['id']}/" . str_replace(env('gcs_perfix'), '', $value));
}
} else {
break;
}
}
}
}
$schema_breadcrumbList = [
'context' => 'https://schema.org',
'type' => 'BreadcrumbList',
'itemListElement' => [
[
'type' => 'ListItem',
'position' => 1,
'name' => '首页',
'item' => route('index'),
],
[
'type' => 'ListItem',
'position' => 2,
'name' => '专题研究',
'item' => route('topic'),
],
[
'type' => 'ListItem',
'position' => 3,
'name' => $topic_category['name'],
'item' => route('topic_category', ['category_url_name' => $topic_category['url_name']]),
],
],
];
$schema = [
'context' => 'https://schema.org',
'type' => 'ItemList',
];
// LongBanner
$longbanner = BannerHandler::longbanner(2, "search_active", ['unit' => 'topic', 'cat' => ['topic', $category_url_name]]);
// ShortBanner
$top_shortbanner = BannerHandler::shortbanner("top", ['unit' => 'topic', 'cat' => ['topic', $category_url_name]]);
$shortbanner = BannerHandler::shortbanner("search_active", ['unit' => 'topic', 'cat' => ['topic', $category_url_name]]);
// WebsiteLink
$websitelink = WebsiteLinkHandler::get_weblink(['unit' => 'topic', 'cat' => ['topic', $category_url_name]]);
// CTA
$config = config('topic');
$cta_status = $config['cta_status'];
$cta_content = $config['cta_content'];
$data = [
'system_code' => 'topic_category',
'bread' => ['url_name' => $category_url_name, 'cat_name' => $topic_category['name']],
'unit_config' => $unit_config,
'topic_categorys' => $topic_categorys,
'topic_category_url_name_arr' => $topic_category_url_name_arr,
'topic_category' => $topic_category,
'topic' => $topic,
'current' => $current,
'pages' => $pages,
'erotic_novel_tag' => $this->get_erotic_novel_tag(),
'long_banners' => $longbanner,
'short_banners' => $shortbanner,
'top_short_banners' => $top_shortbanner,
'cta_status' => $cta_status,
'cta_content' => $cta_content,
'websitelink' => $websitelink,
/* Schema LD-json */
'schema_breadcrumbList' => $schema_breadcrumbList,
'schema' => $schema,
'prev' => (! empty($page_result[0])) ? $page_result[0] : '',
'next' => (! empty($page_result[1])) ? $page_result[1] : '',
];
return view('topic', Layout_Setting::setting($data, 'topic_category', $category_url_name));
}
public function detail($category_url_name, $id)
{
// 網址中包含空白,跳轉404
if (strpos($category_url_name, ' ') !== false || strpos($id, ' ') !== false) {
return redirect(404);
}
if (! Redis::exists('data_topic_detail_' . $category_url_name . '_' . $id)) {
$topic_categorys = $this->get_topic_categorys();
$topic_category = $this->get_topic_category($category_url_name);
if (is_null($topic_category)) {
return redirect(route('topic'), 301, []);
}
$topic = $this->get_topic($id);
$topic_disabled = Topic::where('id', $id)->where('status', 0)->get()->first();
if ($topic_disabled) {
return redirect(route('topic_category', ['category_url_name' => $category_url_name]), 302, []);
}
if (is_null($topic)) {
return redirect(route('topic_category', ['category_url_name' => $category_url_name]), 301, []);
}
// 當 " env('keyFilePath'),
]);
$bucket = $storage->bucket(env('storage'));
$unit_config = $this->get_topic_setting();
$topic_category_url_name_arr = $this->get_topic_category_url_name_arr($topic_categorys);
$tags = $this->get_tags();
// $gcs_image_arr = json_decode($topic['gcs_image_arr'], true);
$gcs_image_arr = is_array($topic['gcs_image_arr']) ? $topic['gcs_image_arr'] : json_decode($topic['gcs_image_arr'], true);
if (! is_null($gcs_image_arr) && isset($topic_category_url_name_arr[$topic['pid']])) {
$image_path = "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/{$topic['id']}/";
if (! is_dir(public_path() . "/userfiles/")) {
mkdir(public_path() . "/userfiles/", 0777);
}
if (! is_dir(public_path() . "/userfiles/Topic/")) {
mkdir(public_path() . "/userfiles/Topic/", 0777);
}
if (! is_dir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/")) {
mkdir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/", 0777);
}
if (! is_dir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/{$topic['id']}/")) {
mkdir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/{$topic['id']}/", 0777);
}
foreach ($gcs_image_arr as $key => $value) {
if (! is_file(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/{$topic['id']}/" . str_replace(env('gcs_perfix'), '', $value))) {
$object = $bucket->object($value);
if ($object->exists()) {
$stream = $object->downloadToFile(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/{$topic['id']}/" . str_replace(env('gcs_perfix'), '', $value));
}
}
}
} else {
$image_path = "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/";
}
$data_image_arr = $topic['data_image_arr'];
if (! is_null($data_image_arr)) {
if (! is_dir(public_path() . "/userfiles/")) {
mkdir(public_path() . "/userfiles/", 0777);
}
if (! is_dir(public_path() . "/userfiles/Topic/")) {
mkdir(public_path() . "/userfiles/Topic/", 0777);
}
if (! is_dir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/")) {
mkdir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/", 0777);
}
if (! is_dir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/{$topic['id']}/")) {
mkdir(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/{$topic['id']}/", 0777);
}
foreach ($data_image_arr as $key => $value) {
if (! is_file(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/{$topic['id']}/" . str_replace(env('gcs_perfix'), '', $value))) {
$object = $bucket->object($value);
if ($object->exists()) {
$stream = $object->downloadToFile(public_path() . "/userfiles/Topic/topic_{$topic_category_url_name_arr[$topic['pid']]}/{$topic['id']}/" . str_replace(env('gcs_perfix'), '', $value));
}
}
}
}
$prev_bool = true;
$prev_count = 1;
while ($prev_bool) {
if ($id - $prev_count > 0) {
$item = DateFilterHandler::date_filter(Topic::select('id', 'pid', 'name'), ['status' => 1, 'id' => ($id - $prev_count)])->get()->first();
if (! is_null($item)) {
$item = $item->toArray();
$category = TopicCategory::select('url_name')->where('id', $item['pid'])->where('status', 1)->get()->first();
if (! is_null($category)) {
$prev_bool = false;
} else {
$prev_count++;
}
} else {
$prev_count++;
}
} else {
$item = DateFilterHandler::date_filter(Topic::select('id', 'pid', 'name'), ['status' => 1])->orderBy('id', 'desc')->take(1)->get()->first()->toArray();
$category = TopicCategory::select('url_name')->where('id', $item['pid'])->where('status', 1)->get()->first()->toArray();
if (! is_null($category)) {
$prev_bool = false;
} else {
$id = $item['id'];
$prev_count = 1;
}
}
}
$prev_topic = [
'id' => $item['id'],
'name' => $item['name'],
'category_url_name' => $category['url_name'],
];
$next_bool = true;
$next_count = 1;
$max_item = DateFilterHandler::date_filter(Topic::select('id'), ['status' => 1])->orderBy('id', 'desc')->take(1)->get()->first()->toArray();
$max_id = $max_item['id'];
while ($next_bool) {
if ($id + $next_count <= $max_id) {
$item = DateFilterHandler::date_filter(Topic::select('id', 'pid', 'name'), ['status' => 1, 'id' => ($id + $next_count)])->where('status', 1)->get()->first();
if (! is_null($item)) {
$item = $item->toArray();
$category = TopicCategory::select('url_name')->where('id', $item['pid'])->where('status', 1)->get()->first();
if (! is_null($category)) {
$next_bool = false;
} else {
$next_count++;
}
} else {
$next_count++;
}
} else {
$item = DateFilterHandler::date_filter(Topic::select('id', 'pid', 'name'), ['status' => 1])->orderBy('id', 'asc')->take(1)->get()->first()->toArray();
$category = TopicCategory::select('url_name')->where('id', $item['pid'])->where('status', 1)->get()->first();
if (! is_null($category)) {
$next_bool = false;
} else {
$id = $item['id'];
$next_count = 1;
}
}
}
$next_topic = [
'id' => $item['id'],
'name' => $item['name'],
'category_url_name' => $category['url_name'],
];
$schema_breadcrumbList = [
'context' => 'https://schema.org',
'type' => 'BreadcrumbList',
'itemListElement' => [
[
'type' => 'ListItem',
'position' => 1,
'name' => '首页',
'item' => route('index'),
],
[
'type' => 'ListItem',
'position' => 2,
'name' => '专题研究',
'item' => route('topic'),
],
[
'type' => 'ListItem',
'position' => 3,
'name' => $topic['name'],
'item' => route('topic_detail', ['category_url_name' => $topic_category['url_name'], 'id' => $id]),
],
],
];
if (strpos(url()->full(), '?') !== false) {
$route = explode('?', url()->full());
$route = $route[0];
} else {
$route = url()->full();
}
$logo = Setting_System::select('head_logo')->where('id', '1')->get()->first()->toArray();
$schema = [
'context' => 'https://schema.org',
'type' => 'Article',
'mainEntityOfPage' => [
'type' => 'WebPage',
'id' => $route,
],
'headline' => $topic['name'],
'image' => asset($topic['cover_image'] ? $image_path . $topic['cover_image'] : ''),
'author' => [
'type' => 'Organization',
'name' => config('all')['title'],
],
'publisher' => [
'type' => 'Organization',
'name' => config('all')['title'],
'logo' => [
'type' => 'ImageObject',
'url' => asset($logo['head_logo']),
],
],
'datePublished' => date(DATE_ISO8601, strtotime($topic['posted_at'])),
'dateModified' => date(DATE_ISO8601, strtotime($topic['updated_at'])),
];
// LongBanner
$longbanner = BannerHandler::longbanner(2, "search_active", ['unit' => 'topic', 'cat' => ['topic', $category_url_name]]);
// ShortBanner
$top_shortbanner = BannerHandler::shortbanner("top", ['unit' => 'topic', 'cat' => ['topic', $category_url_name]]);
$shortbanner = BannerHandler::shortbanner("search_active", ['unit' => 'topic', 'cat' => ['topic', $category_url_name]]);
// cta
$cta = CTAHandler::get_cta(1, ['unit' => 'topic', 'cat' => ['topic', $category_url_name]]);
$data = [
'system_code' => 'topic_detail',
'unit_config' => $unit_config,
'bread' => ['url_name' => $category_url_name, 'cat_name' => $topic_category['name']],
'topic_categorys' => $topic_categorys,
'topic_category' => $topic_category,
'topic' => $topic,
'tags' => $tags,
'erotic_novel_tag' => $this->get_erotic_novel_tag(),
'prev_topic' => $prev_topic,
'next_topic' => $next_topic,
'long_banners' => $longbanner,
'short_banners' => $shortbanner,
'top_short_banners' => $top_shortbanner,
'cta' => $cta,
'og_image' => asset($topic['cover_image'] ? $image_path . $topic['cover_image'] : ''),
/* Schema LD-json */
'schema_breadcrumbList' => $schema_breadcrumbList,
'schema' => $schema,
];
Redis::setex('data_topic_detail_' . $category_url_name . '_' . $id, env('cache_second'), json_encode($data));
} else {
$data = json_decode(Redis::get('data_topic_detail_' . $category_url_name . '_' . $id), true);
$this->get_topic($id);
$result = DateFilterHandler::date_filter(Topic::select('id'), ['status' => '1', 'id' => $id])->get()->first();
if (is_null($result)) {
Redis::del('data_topic_detail_' . $category_url_name . '_' . $id);
return redirect(route('topic_category', ['category_url_name' => $category_url_name]), 301, []);
}
}
// 獨立於 Redis 快取檢查之外:無論快取是否存在,每次瀏覽皆即時更新瀏覽次數
if(isset($topic) && $topic)
ViewRecordHandler::handler($topic instanceof Topic ? $topic : Topic::find($topic['id']));
return view('topic_detail', Layout_Setting::setting($data, 'topic_detail', $category_url_name, $id));
}
public function get_topic_setting()
{
return Setting_Topic::get()->first()->toArray();
}
public function get_erotic_novel_tag()
{
// 系統設定
$system_config = Setting_System::get()->first()->toArray();
return EroticNovelTag::select('url_name', 'name')->where('status', 1)->orderBy('sort', 'desc')->take($system_config['novel_tag'])->get()->toArray();
}
public function get_topic_categorys()
{
if (! Redis::exists('topic_categorys')) {
$data = TopicCategory::select('id', 'url_name', 'name')->where('status', 1)->orderBy('id', 'asc')->get()->toArray();
Redis::setex('topic_categorys', env('cache_second'), json_encode($data));
} else {
$data = json_decode(Redis::get('topic_categorys'), true);
}
return $data;
}
public function get_topic_category_url_name_arr($topic_categorys)
{
foreach ($topic_categorys as $k => $v) {
$cats_arr[$v['id']] = $v['url_name'];
}
return $cats_arr;
}
public function get_topic_pagination($page, $perpage)
{
$topic_category_arr = [];
$topic_arr = [];
$topic_category = TopicCategory::where('status', 1)->get()->toArray();
foreach ($topic_category as $key => $value) {
array_push($topic_category_arr, $value['id']);
}
$topic_arr = DateFilterHandler::date_filter_with_category_arr(Topic::select('id', 'pid', 'name', 'gcs_image_arr', 'cover_image'), ['status' => 1], ['pid' => $topic_category_arr])->orderBy('top', 'desc')->orderBy('sort', 'desc')->orderBy('view_record', 'desc')->paginate($perpage, ['*'], 'page', $page);
return $topic_arr;
}
public function get_topic_category($category_url_name)
{
$cat = TopicCategory::where('url_name', $category_url_name)->where('status', 1)->get()->first();
return (is_null($cat)) ? null : $cat->toArray();
}
public function get_topic_category_pagination($category_url_name, $pid, $page, $perpage)
{
$topic = DateFilterHandler::date_filter(Topic::select('id', 'pid', 'name', 'gcs_image_arr', 'cover_image'), ['status' => 1, 'pid' => $pid])->orderBy('top', 'desc')->orderBy('sort', 'desc')->orderBy('view_record', 'desc')->get()->toArray();
foreach ($topic as $key => $value) {
$cat = TopicCategory::where('id', $value['pid'])->where('status', 1)->get()->first();
if (is_null($cat)) {unset($topic[$key]);}
}
$topic = array_values($topic);
$data = [];
if ($perpage * ($page - 1) + $perpage < count($topic)) {
$limit = $perpage * ($page - 1) + $perpage;
} else {
$limit = count($topic);
}
for ($i = $perpage * ($page - 1); $i < $limit; $i++) {
array_push($data, $topic[$i]);
}
return $data;
}
public function get_topic($id)
{
if (! Redis::exists('topic_id_' . $id)) {
$data = Topic::where('id', $id)->where('status', 1)->get()->first();
Redis::setex('topic_id_' . $id, env('cache_second'), json_encode($data));
} else {
$data = json_decode(Redis::get('topic_id_' . $id), true);
if ($data) {
// 如果redis中有資料,則更新瀏覽次數
Topic::where('id', $id)->update(['view_record' => ($data['view_record'] + 1), 'updated_at' => \DB::raw('updated_at')]);
// 更新redis中的資料
$data['view_record'] = $data['view_record'] + 1;
Redis::setex('topic_id_' . $id, env('cache_second'), json_encode($data));
}
}
return $data;
}
public function get_tags()
{
$tags_arr = [];
$tags = ArticleTag::where('status', 1)->get()->toArray();
foreach ($tags as $k => $v) {
$tags_arr[$v['id']] = ['name' => $v['name'], 'url_name' => $v['url_name']];
}
return $tags_arr;
}
}