Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions app/Enums/PageMetaRobots.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Enums;

use App\Enums\EnumsBase;

/**
* ページのmeta robots設定
*/
final class PageMetaRobots extends EnumsBase
{
const noindex = 'noindex';
const nofollow = 'nofollow';
const noarchive = 'noarchive';
const nosnippet = 'nosnippet';
const noimageindex = 'noimageindex';
const notranslate = 'notranslate';

const enum = [
self::noindex => '検索結果に表示させない(noindex)',
self::nofollow => 'ページ内のリンク先を検索エンジンに辿らせない(nofollow)',
self::noarchive => '検索結果にキャッシュ(保存版)を出さない(noarchive)',
self::nosnippet => '検索結果にページの説明を表示させない(nosnippet)',
self::noimageindex => 'ページ内の画像を画像検索に載せない(noimageindex)',
];

/**
* 指定された値を説明文の配列に変換
*/
public static function descriptions(array $values): array
{
$members = static::getMembers();

$descriptions = [];
foreach ($values as $value) {
if (array_key_exists($value, $members)) {
$descriptions[] = $members[$value];
}
}

return $descriptions;
}
}
21 changes: 21 additions & 0 deletions app/Models/Common/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Page extends Model
'class',
'othersite_url',
'othersite_url_target',
'meta_robots',
'transfer_lower_page_flag',
'password',
];
Expand Down Expand Up @@ -539,6 +540,26 @@ public function getLayoutTitle()
}
}

/**
* メタrobotsの有効値を取得(自ページから親を遡って検索)
*/
public function getMetaRobots(?Collection $page_tree = null): ?string
{
if (empty($this->id)) {
return null;
}

$page_tree = $this->getPageTreeByGoingBackParent($page_tree);

foreach ($page_tree as $page) {
if (!empty($page->meta_robots)) {
return $page->meta_robots;
}
}

return null;
}

/**
* パスワードを要求するかの判断
*/
Expand Down
45 changes: 45 additions & 0 deletions app/Plugins/Manage/PageManage/PageManage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Plugins\Manage\PageManage;

use App\Enums\PageCvsIndex;
use App\Enums\PageMetaRobots;
use App\Enums\WebsiteType;
use App\Models\Common\Buckets;
use App\Models\Common\Frame;
Expand All @@ -12,6 +13,7 @@
use App\Models\Common\PageRole;
use App\Models\User\Contents\Contents;
use App\Plugins\Manage\ManagePluginBase;
use App\Rules\CustomValiMetaRobots;
use App\Rules\CustomValiTextMax;
use App\Rules\CustomValiUrlMax;
use App\Traits\Migration\MigrationTrait;
Expand Down Expand Up @@ -183,6 +185,7 @@ private function pageValidator($request, $page_id = null)
'ip_address' => ['nullable', new CustomValiTextMax()],
'othersite_url' => ['nullable', new CustomValiUrlMax()],
'class' => ['nullable', 'max:255'],
'meta_robots' => ['nullable', 'array', new CustomValiMetaRobots()],
]);
$validator->setAttributeNames([
'page_name' => 'ページ名',
Expand All @@ -193,10 +196,46 @@ private function pageValidator($request, $page_id = null)
'ip_address' => 'IPアドレス制限',
'othersite_url' => '外部サイトURL',
'class' => 'メニュークラス名',
'meta_robots' => '検索避け設定',
]);
return $validator;
}

/**
* meta robotsの入力値を正規化
*/
private function normalizeMetaRobots($request): ?string
{
$meta_robots = $request->input('meta_robots');

if (is_array($meta_robots)) {
$meta_robots = array_filter($meta_robots, function ($value) {
return $value !== null && $value !== '';
});

if (empty($meta_robots)) {
return null;
}

$meta_robots = array_unique($meta_robots);

$allowed = PageMetaRobots::getMemberKeys();
$meta_robots = array_values(array_intersect($allowed, $meta_robots));

if (empty($meta_robots)) {
return null;
}

return implode(',', $meta_robots);
}

if (is_string($meta_robots) && $meta_robots !== '') {
return in_array($meta_robots, PageMetaRobots::getMemberKeys(), true) ? $meta_robots : null;
}

return null;
}

/**
* CSVインポート時のエラーチェックルール
*/
Expand Down Expand Up @@ -235,6 +274,8 @@ public function store($request)
return redirect()->back()->withErrors($validator)->withInput();
}

$meta_robots = $this->normalizeMetaRobots($request);

// ページデータの登録
$page = new Page;
$page->page_name = $request->page_name;
Expand All @@ -251,6 +292,7 @@ public function store($request)
$page->othersite_url = $request->othersite_url;
$page->othersite_url_target = (isset($request->othersite_url_target) ? $request->othersite_url_target : 0);
$page->transfer_lower_page_flag = $request->transfer_lower_page_flag ?? 0;
$page->meta_robots = $meta_robots;
$page->class = $request->class;
$page->save();

Expand Down Expand Up @@ -278,6 +320,8 @@ public function update($request, $page_id)
return redirect()->back()->withErrors($validator)->withInput();
}

$meta_robots = $this->normalizeMetaRobots($request);

// ページデータの更新
Page::where('id', $page_id)
->update([
Expand All @@ -295,6 +339,7 @@ public function update($request, $page_id)
'othersite_url' => $request->othersite_url,
'othersite_url_target' => (isset($request->othersite_url_target) ? $request->othersite_url_target : 0),
'transfer_lower_page_flag' => $request->transfer_lower_page_flag ?? 0,
'meta_robots' => $meta_robots,
'class' => $request->class,
]);

Expand Down
61 changes: 61 additions & 0 deletions app/Rules/CustomValiMetaRobots.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use App\Enums\PageMetaRobots;

/**
* meta robots値のバリデーション
*/
class CustomValiMetaRobots implements Rule
{
/**
* @var array<string>
*/
private $invalidValues = [];

/**
* Determine if the validation rule passes.
*/
public function passes($attribute, $value)
{
if (is_null($value) || $value === '') {
return true;
}

$values = [];

if (is_array($value)) {
$values = $value;
} else {
$values = [$value];
}

$values = array_filter($values, function ($item) {
return $item !== null && $item !== '';
});

if (empty($values)) {
return true;
}

$allowed = PageMetaRobots::getMemberKeys();

$this->invalidValues = array_diff($values, $allowed);

return empty($this->invalidValues);
}

/**
* Get the validation error message.
*/
public function message()
{
if (!empty($this->invalidValues)) {
return ':attributeに不正な値(' . implode(', ', $this->invalidValues) . ')が含まれています。';
}

return ':attributeに不正な値が含まれています。';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddMetaRobotsToPagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('pages', function (Blueprint $table) {
$table->string('meta_robots')->nullable()->after('transfer_lower_page_flag')->comment('検索避け設定(robots meta)');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('pages', function (Blueprint $table) {
$table->dropColumn('meta_robots');
});
}
}
11 changes: 11 additions & 0 deletions resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@
@if (Configs::getConfigsValue($cc_configs, 'description'))
<meta name="description" content="{{ StringUtils::getNobrValue(Configs::getConfigsValue($cc_configs, 'description')) }}">
@endif
{{-- 検索避け設定 --}}
@php
$meta_robots = null;
if (isset($page)) {
$page_tree = app('request')->attributes->get('page_tree');
$meta_robots = $page->getMetaRobots($page_tree);
}
@endphp
@if ($meta_robots)
<meta name="robots" content="{{$meta_robots}}">
@endif

{{-- OGP Settings --}}
@if (Configs::getConfigsValue($cc_configs, 'og_site_name'))
Expand Down
25 changes: 25 additions & 0 deletions resources/views/plugins/manage/page/page.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @category ページ管理
--}}
@php
use App\Enums\PageMetaRobots;
use App\Models\Common\Page;
@endphp

Expand Down Expand Up @@ -196,6 +197,7 @@ function select_page(source_id, page_name) {
<th nowrap><i class="fas fa-window-restore" title="新ウィンドウ"></i></th>
<th nowrap><i class="fas fa-network-wired" title="IPアドレス制限"></i></th>
<th nowrap><i class="fas fa-external-link-alt" title="外部リンク"></i></th>
<th nowrap><i class="fas fa-robot" title="検索避け設定"></i></th>
<th nowrap><i class="fas fa-swatchbook" title="クラス名"></i></th>
</thead>
<tbody>
Expand Down Expand Up @@ -420,6 +422,29 @@ function select_page(source_id, page_name) {
<td class="table-text p-1 text-center">
<div>@if($page_item->othersite_url)<i class="fas fa-external-link-alt" title="{{$page_item->othersite_url}}"></i>@endif</div>
</td>
<td class="table-text p-1 text-center">
@if ($page_item->meta_robots)
@php
$meta_robots_descriptions = implode('、', PageMetaRobots::descriptions(explode(',', $page_item->meta_robots)));
$meta_robots_tooltip = $meta_robots_descriptions ?: $page_item->meta_robots;
@endphp
<div><i class="fas fa-robot" title="{{$meta_robots_tooltip}}"></i></div>
@else
@php
$meta_robots_parent = null;
foreach ($page_tree as $page_tmp) {
if ($page_tmp->meta_robots) {
$meta_robots_parent = $page_tmp->meta_robots;
break;
}
}
$meta_robots_parent_description = $meta_robots_parent ? implode('、', PageMetaRobots::descriptions(explode(',', $meta_robots_parent))) : '';
@endphp
@if ($meta_robots_parent)
<div><i class="fas fa-robot text-warning" title="{{$meta_robots_parent_description}}(親ページを継承)"></i></div>
@endif
@endif
</td>
<td class="table-text p-1 text-center">
<div>@if($page_item->class)<i class="fas fa-swatchbook" title="{{$page_item->class}}"></i>@endif</div>
</td>
Expand Down
Loading