Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement polls edit and delete. #1126

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
93 changes: 93 additions & 0 deletions app/Http/Controllers/Staff/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,97 @@ public function store(StorePoll $request)
return redirect()->route('staff.polls.index')
->withSuccess('Your poll has been created.');
}

/**
* Poll Edit Form.
*
* @param $id
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit($id)
{
$poll = Poll::findOrFail($id);

return view('Staff.poll.edit', ['poll' => $poll]);
}

/**
* Update A New Poll.
*
* @param StorePoll $request
*
* @return Illuminate\Http\RedirectResponse
*/
public function update(StorePoll $request, $id)
{
$poll = Poll::findOrFail($id);
gyakkun marked this conversation as resolved.
Show resolved Hide resolved

$poll->title = $request->input('title');

// Remove the deleted options in poll
$oldOptionIds = collect($poll->options)->map(function ($option) {
return $option->id;
})->all();

$existingOldOptionIds = collect($request->input('option-id'))->map(function ($id) {
return intval($id);
})->all();

$idsOfOptionToBeRemove = array_diff($oldOptionIds, $existingOldOptionIds);

foreach ($idsOfOptionToBeRemove as $id) {
$option = Option::findOrFail($id);
$option->delete();
}

// Update existing options
$existingOldOptionContents = collect($request->input('option-content'))->map(function ($content) {
return strval($content);
})->all();

if (count($existingOldOptionContents) === count($existingOldOptionIds)) {
$len = count($existingOldOptionContents);
for ($i = 0; $i < $len; $i++) {
$option = Option::findOrFail($existingOldOptionIds[$i]);
$option->name = $existingOldOptionContents[$i];
$option->save();
}
}

// Insert new options
$newOptions = collect($request->input('new-option-content'))->map(function ($content) {
return new Option(['name' => $content]);
});

$poll->options()->saveMany($newOptions);

// Last work from store()
$poll_url = hrefPoll($poll);

$this->chat->systemMessage(
"A poll has been updated [url={$poll_url}]{$poll->title}[/url] vote on it now! :slight_smile:"
);

$poll->save();

return redirect()->route('staff.polls.index')
->withSuccess('Your poll has been edited.');
}

/**
* Delete A Poll.
*
* @param $id
*
* @return Illuminate\Http\RedirectResponse
*/
public function destroy($id)
HDVinnie marked this conversation as resolved.
Show resolved Hide resolved
{
$poll = Poll::findOrFail($id);
$poll->delete();

return redirect()->route('staff.polls.index')
->withSuccess('Poll has successfully been deleted');
}
}
1 change: 1 addition & 0 deletions resources/lang/en/poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'create-poll' => 'Create Poll',
'current' => 'Current Poll(s)',
'delete-option' => 'Delete Option',
'edit-poll' => 'Edit Poll',
'ip-checking' => 'Enable Duplicate IP Checking',
'ip-checking' => 'This poll has duplicate vote checking. You can only vote once.',
'ip-checking-warrning' => 'Only needed if site is in public mode',
Expand Down
1 change: 1 addition & 0 deletions resources/lang/zh-CN/poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'create-poll' => '建立投票',
'current' => '目前投票',
'delete-option' => '删除选项',
'edit-poll' => '编辑投票',
'ip-checking' => '启用重复IP检查机制',
'ip-checking' => '此投票有重复IP检查。你的IP只能投一次。',
'ip-checking-warrning' => '若网站在公开模式,才需要该选项',
Expand Down
1 change: 1 addition & 0 deletions resources/lang/zh-TW/poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'create-poll' => '建立投票',
'current' => '目前投票',
'delete-option' => '刪除選項',
'edit-poll' => '編輯投票',
'ip-checking' => '啟動重複IP檢查機制',
'ip-checking' => '此投票有重復的投票檢查。你只能投一次。',
'ip-checking-warrning' => '若網站在公開模式,才需此選項',
Expand Down
47 changes: 47 additions & 0 deletions resources/views/Staff/poll/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
@extends('layout.default')

@section('title')
<title>Polls - {{ config('other.title') }}</title>
@endsection

@section('breadcrumb')
<li>
<a href="{{ route('staff.dashboard.index') }}" itemprop="url" class="l-breadcrumb-item-link">
<span itemprop="title" class="l-breadcrumb-item-link-title">@lang('staff.staff-dashboard')</span>
</a>
</li>
<li>
<a href="{{ route('staff.polls.index') }}" itemprop="url" class="l-breadcrumb-item-link">
<span itemprop="title" class="l-breadcrumb-item-link-title">@lang('poll.polls')</span>
</a>
</li>
<li>
<a href="{{ route('staff.polls.edit', ['id'=> $poll->id]) }}" itemprop="url" class="l-breadcrumb-item-link">
<span itemprop="title" class="l-breadcrumb-item-link-title">@lang('poll.edit-poll') {{$poll->slug}}</span>
</a>
</li>
@endsection

@section('content')
<div class="container">
<div class="row">
<div class="page-title">
<h1>@lang('poll.edit-poll')</h1>
</div>
<div class="col-md-12">
<div class="panel panel-chat">
<div class="panel-heading">
@lang('common.edit')
gyakkun marked this conversation as resolved.
Show resolved Hide resolved
@lang(trans_choice('common.a-an-art',false))
@lang('poll.poll')
{{$poll->title}}
</div>
<div class="panel-body">
@include('Staff.poll.forms.update')
</div>
</div>
</div>
</div>
</div>

@endsection
85 changes: 85 additions & 0 deletions resources/views/Staff/poll/forms/update.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<form method="POST" action="{{ route('staff.polls.update', ['id' => $poll->id]) }}">
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

@csrf

<div class="form-group">
<label for="stitle">@lang('poll.title'):</label>
<label>
<input readonly type="number" name="poll-id" style="visibility: hidden" value="{{$poll->id}}">
<input type="text" name="title" class="form-control" value={{$poll->title}} required>
</label>
</div>

@foreach($poll->options as $key=>$option)
<div class="form-group <?php echo (++$key) >= 3 ? 'extra-option' : '' ?>" )>
<label for={{"option" . $key}}>@lang('poll.option') {{$key}}:</label>
<label>
<input readonly type="number" name="option-id[]" style="visibility: hidden" value="{{$option->id}}">
<input type="text" name="option-content[]" class="form-control" value={{$option->name}}>
</label>
</div>

@endforeach

<div class="more-options"></div>

<div class="form-group">
<button id="add" class="btn btn-primary">@lang('poll.add-option')</button>
<button id="del" class="btn btn-primary">@lang('poll.delete-option')</button>
</div>

<hr>

<div class="checkbox">
<label>
<input type="checkbox" name="ip_checking" value="1">@lang('poll.ip-checking') <span
class="text-red">({{ strtoupper(trans('poll.ip-checking-warrning')) }})</span>
</label>
</div>

<div class="checkbox">
<label>
<input type="checkbox" name="multiple_choice" value="1">@lang('poll.multiple-choice')
</label>
</div>

<div class="form-group">
<button type="submit" class="btn btn-primary">@lang('poll.edit-poll')</button>
</div>
</form>

@section('javascripts')
<script nonce="{{ Bepsvpt\SecureHeaders\SecureHeaders::nonce() }}">

var options = parseInt("<?php echo count($poll->options); ?>"); // Get the size of options passing in
var langOption = "<?php echo __('poll.option') ?> "

$('#add').on('click', function (e) {
e.preventDefault();
options = options + 1;
var optionHTML = '<div class="form-group extra-option"><label for="option' + options + '">'
+ langOption
+ options
+ ':</label>'
// + '<input readonly type="number" name="new-option-id[]" style="visibility: hidden" value="'+(-options)+'">'
+ '<input type="text" name="new-option-content[]" class="form-control" value="" required></div>';
$('.more-options').append(optionHTML);
});

$('#del').on('click', function (e) {
e.preventDefault();
options = (options > 2) ? options - 1 : 2;
$('.extra-option').last().remove();
});

</script>
@endsection
13 changes: 11 additions & 2 deletions resources/views/Staff/poll/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@
<td><a href="{{ url('/dashboard/polls/' . $poll->id) }}">{{ $poll->title }}</a></td>
<td>{{ date('d M Y', $poll->created_at->getTimestamp()) }}</td>
<td>
<a href="#" class="btn btn-warning">@lang('common.edit')</a>
<a href="#" class="btn btn-danger">@lang('common.delete')</a>
{{-- <a href="#" class="btn btn-warning">@lang('common.edit')</a>--}}
HDVinnie marked this conversation as resolved.
Show resolved Hide resolved
{{-- <a href="#" class="btn btn-danger">@lang('common.delete')</a>--}}

<form action="{{ route('staff.polls.destroy', ['id' => $poll->id]) }}" method="POST">
@csrf
@method('DELETE')
<a href="{{ route('staff.polls.edit', ['id' => $poll->id]) }}"
class="btn btn-warning">@lang('common.edit')</a>
<button type="submit" class="btn btn-danger">@lang('common.delete')</button>
</form>

</td>
</tr>
@endforeach
Expand Down
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,9 @@
Route::get('/{id}', 'PollController@show')->where('id', '[0-9]+')->name('show');
Route::get('/create', 'PollController@create')->name('create');
Route::post('/store', 'PollController@store')->name('store');
Route::get('/{id}/edit', 'PollController@edit')->name('edit');
Route::post('/{id}/update', 'PollController@update')->name('update');
Route::delete('/{id}/destroy', 'PollController@destroy')->name('destroy');
});
});

Expand Down