Skip to content

Commit

Permalink
#311 QnA 리스트를 반환하는 위젯 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
xharpenParksuhyeon committed Aug 21, 2021
1 parent df2af2d commit 1092735
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 0 deletions.
130 changes: 130 additions & 0 deletions components/Widgets/QnaList/QnaListWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Xpressengine\Plugins\Board\Components\Widgets\QnaList;

use View;
use Illuminate\Support\Arr;
use Xpressengine\Config\ConfigEntity;
use Xpressengine\Http\Request;
use Xpressengine\Menu\Models\MenuItem;
use Xpressengine\Plugin\SupportInfoTrait;
use Xpressengine\Plugins\Board\{ConfigHandler, ReplyConfigHandler, Services\BoardService, UrlHandler};
use Xpressengine\Widget\AbstractWidget;

class QnaListWidget extends AbstractWidget
{
use SupportInfoTrait;

protected static $path = 'board/src/Widgets/QnaList';

/**
* render
*
* @return string
*/
public function render(): string
{
$widgetConfig = $this->setting();
$boardId = Arr::get($widgetConfig, 'board_id');
$boardMenuItem = MenuItem::where('type', 'board@board')->findOrFail($boardId);

$boardRequest = $this->getBoardRequest($widgetConfig);
$boardConfig = $this->getBoardConfig($boardMenuItem, Arr::get($widgetConfig, 'take'));

$boards = app(BoardService::class)->getItems($boardRequest, $boardConfig)->getCollection();
$moreUrl = Arr::get($widgetConfig, 'using_more') !== 'true' ? null : app(UrlHandler::class)->get('index', $boardRequest->all(), $boardId);

return $this->renderSkin([
'widgetConfig' => $widgetConfig,
'boards' => $boards,
'moreUrl' => $moreUrl,
]);
}

/**
* get board's request
*
* @param array $config
* @return Request
*/
private function getBoardRequest(array $config): Request
{
$request = new Request;

if ($adoptFilter = Arr::get($config, 'adopt_filter')) {
$key = null;
$key = $adoptFilter === 'only_adopted' ? 'has_adopted' : $key;
$key = $adoptFilter === 'only_unAdopted' ? 'has_not_adopted' : $key;

if ($key !== null) {
$request->merge([$key => '']);
}
}

if ($orderType = Arr::get($config, 'order_type')) {
$request->merge(['order_type' => $orderType]);
}

return $request;
}

/**
* get board's config
*
* @param MenuItem $boardMenuItem
* @param int|null $take
* @return ConfigEntity
*/
private function getBoardConfig(MenuItem $boardMenuItem, int $take = null): ConfigEntity
{
$boardConfig = ConfigHandler::make()->get($boardMenuItem->id);
$replyConfig = ReplyConfigHandler::make()->getActivated($boardMenuItem->id);

if (is_null($replyConfig)) {
throw new \LogicException("not found reply config");
}

if ($take !== null) {
$boardConfig->set('perPage', $take);
$boardConfig->set('pageCount', $take);
}

return $boardConfig;
}

/**
* render settings
*
* @param array $args
* @return string
*/
public function renderSetting(array $args = []): string
{
$boards = $this->getBoards();

return View::make('board::components.Widgets.QnaList.views.settings', [
'args' => $args,
'boards' => $boards
]);
}

/**
* get boards
*
* @return array
*/
protected function getBoards(): array
{
return MenuItem::where('type', 'board@board')
->where('activated', true)
->whereIn('id', ReplyConfigHandler::make()->getActivatedIds())
->get()
->map(function(MenuItem $menuItem) {
return [
'id' => $menuItem->id,
'text' => $menuItem->title,
];
})
->toArray();
}
}
10 changes: 10 additions & 0 deletions components/Widgets/QnaList/Skins/Basic/BasicQnaListSkin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Xpressengine\Plugins\Board\Components\Widgets\QnaList\Skins\Basic;

use Xpressengine\Skin\GenericSkin;

class BasicQnaListSkin extends GenericSkin
{
protected static $path = 'board/components/Widgets/QnaList/Skins/Basic';
}
39 changes: 39 additions & 0 deletions components/Widgets/QnaList/Skins/Basic/assets/css/skin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
body {
padding-top: 20px;
padding-bottom: 20px;
}

.header,
.footer {
padding-right: 15px;
padding-left: 15px;
}

.header {
padding-bottom: 20px;
margin-bottom: 30px;
border-bottom: 1px solid #e5e5e5;
}

.header h3 {
margin-top: 0;
margin-bottom: 0;
line-height: 40px;
font-size: 24px;
}

.footer {
padding-top: 19px;
color: #777;
border-top: 1px solid #e5e5e5;
}

.spot {
text-align: center;
color: inherit;
padding: 48px 30px;
border-radius: 6px;
border-bottom: 1px solid #e5e5e5;
margin-bottom: 30px;
background-color: #eee;
}
16 changes: 16 additions & 0 deletions components/Widgets/QnaList/Skins/Basic/info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
return [
'setting' => [
'sample_text' => [
'_type' => 'text',
'_section' => '기본설정',
'label' => '샘플 문구',
'placeholder' => '샘플용 설정 필드입니다.',
'description' => '샘플용 설정 필드입니다.',
],
],
'support' => [
'mobile' => true,
'desktop' => true
]
];
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{-- implement it!! --}}
3 changes: 3 additions & 0 deletions components/Widgets/QnaList/Skins/Basic/views/widget.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{-- implement it!! --}}
{{--{{ dd($widgetConfig) }}--}}
{{--{{ dd($moreUrl) }}--}}
46 changes: 46 additions & 0 deletions components/Widgets/QnaList/views/settings.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<div class="form-group">
<label>정렬</label>
<select name="order_type" class="form-control">
<option value="recently_created" @if(array_get($args, 'order_type') == 'recently_created') selected="selected" @endif >{{ xe_trans('board::recentlyCreated') }}</option>
<option value="recently_updated" @if(array_get($args, 'order_type') == 'recently_updated') selected="selected" @endif >{{ xe_trans('board::recentlyUpdated') }}</option>
<option value="assent_count" @if(array_get($args, 'order_type') == 'assent_count') selected="selected" @endif >{{xe_trans('board::assentOrder')}}</option>
<option value="assent_count" @if(array_get($args, 'order_type') == 'read_count') selected="selected" @endif >{{xe_trans('board::read_count')}}</option>
<option value="random" @if(array_get($args, 'order_type') == 'random') selected="selected" @endif >{{xe_trans('board::random')}}</option>
</select>
</div>

<div class="form-group">
<label>글 수</label>
<input type="number" placeholder="출력할 게시물의 수를 입력해주세요." name="take" class="form-control" value="{{ array_get($args, 'take', 5) }}" />
</div>

<div class="form-group">
<label>채택 여부에 따른 필터링</label>
<select name="adopt_filter" class="form-control">
<option value="" @if(\Illuminate\Support\Arr::get($args, 'adopt_filter', '')) selected="selected" @endif>사용안함</option>
<option value="only_adopted" @if(\Illuminate\Support\Arr::get($args, 'adopt_filter', '') === 'only_adopted') selected="selected" @endif>채택 완료된 게시글만 필터링</option>
<option value="only_unAdopted" @if(\Illuminate\Support\Arr::get($args, 'adopt_filter', '') === 'only_unAdopted') selected="selected" @endif>답변 작성이 필요한 게시글만 필터링</option>
</select>
</div>

<div class="form-group">
<label>QnA 게시판 선택</label>
<select name="board_id" class="form-control">
@foreach ($boards as $board)
<option value="{{ $board['id'] }}" @if(in_array($board['id'], (array)(\Illuminate\Support\Arr::get($args, 'board_id')))) selected="selected" @endif>{{ xe_trans($board['text']) }}</option>
@endforeach
</select>
</div>

<div class="form-group">
<label>더보기</label>
<select name="using_more" class="form-control">
<option value="using_more" @if(\Illuminate\Support\Arr::get($args, 'using_more', 'true') === 'true') selected="selected" @endif>사용</option>
<option value="using_more" @if(\Illuminate\Support\Arr::get($args, 'using_more', 'true') === 'false') selected="selected" @endif>사용안함</option>
</select>
</div>

<div class="form-group">
<label>더보기 명</label>
<input type="text" placeholder="더보기에 사용할 문구를 입력해주세요." name="more_text" class="form-control" value="{{ array_get($args, 'more_text', '') }}" />
</div>
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@
"class": "Xpressengine\\Plugins\\Board\\Components\\ToggleMenus\\Boards\\UnAdoptItem",
"name": "채택 취소 토글 메뉴",
"description": "토글 클릭 시 답글 하나를 채택을 취소 상태로 만들 수 있습니다."
},
"widget/board@qnalist": {
"name": "QnA 게시물 리스트 위젯",
"description": "QnA 게시물 리스트 위젯",
"class": "Xpressengine\\Plugins\\Board\\Components\\Widgets\\QnaList\\QnaListWidget"
},
"widget/board@qnalist/skin/board@basicQnaList": {
"name": "Basic QnA 게시물 리스트 스킨",
"description": "QnA 게시물 리스트 위젯에 대한 Basic 스킨",
"class": "Xpressengine\\Plugins\\Board\\Components\\Widgets\\QnaList\\Skins\\Basic\\BasicQnaListSkin"
}
},
"screenshots" : [
Expand Down

0 comments on commit 1092735

Please sign in to comment.