Skip to content

Commit

Permalink
create faq
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Oct 16, 2023
1 parent c645eb8 commit 0d0a1e5
Show file tree
Hide file tree
Showing 16 changed files with 802 additions and 85 deletions.
142 changes: 142 additions & 0 deletions app/Http/Controllers/Admin/Data/FaqController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace App\Http\Controllers\Admin\Data;

use App\Http\Controllers\Controller;
use App\Models\Faq;
use App\Services\FaqService;
use Auth;
use Config;
use Illuminate\Http\Request;

class FaqController extends Controller {
/*
|--------------------------------------------------------------------------
| Admin / FAQ Controller
|--------------------------------------------------------------------------
|
| Handles creation/editing of FAQ questions and answers.
|
*/

/**
* Shows the faq index.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getFaqIndex(Request $request) {
$query = Faq::query();
$data = $request->only(['faq_category_id', 'name']);
if (isset($data['faq_category_id']) && $data['faq_category_id'] != 'none') {
$query->where('faq_category_id', $data['faq_category_id']);
}
if (isset($data['name'])) {
$query->where('name', 'LIKE', '%'.$data['name'].'%');
}

return view('admin.faq.faq', [
'faqs' => $query->paginate(20)->appends($request->query()),
'tags' => Config::get('lorekeeper.faq'),
]);
}

/**
* Shows the create faq page.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getCreateFaqQuestion() {
$tags = Config::get('lorekeeper.faq');
// tags is an array of names, make it so their key is their name also
$tags = array_combine($tags, $tags);
return view('admin.faq.create_edit_question', [
'faq' => new Faq,
'tags' => $tags,

]);
}

/**
* Shows the edit faq page.
*
* @param int $id
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getEditFaqQuestion($id) {
$faq = Faq::find($id);
if (!$faq) {
abort(404);
}
$tags = Config::get('lorekeeper.faq');
// tags is an array of names, make it so their key is their name also
$tags = array_combine($tags, $tags);
return view('admin.faq.create_edit_question', [
'faq' => $faq,
'tags' => $tags,
]);
}

/**
* Creates or edits an faq.
*
* @param App\Services\FaqService $service
* @param int|null $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postCreateEditFaqQuestion(Request $request, FaqService $service, $id = null) {
$id ? null : $request->validate(Faq::$createRules);
$data = $request->only([
'question', 'answer', 'tags', 'is_visible',
]);
if ($id && $service->updateFaq(Faq::find($id), $data, Auth::user())) {
flash('Faq updated successfully.')->success();
} elseif (!$id && $faq = $service->createFaq($data, Auth::user())) {
flash('Faq created successfully.')->success();

return redirect()->to('admin/data/faq/edit/'.$faq->id);
} else {
foreach ($service->errors()->getMessages()['error'] as $error) {
flash($error)->error();
}
}

return redirect()->back();
}

/**
* Gets the faq deletion modal.
*
* @param int $id
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getDeleteFaqQuestion($id) {
$faq = Faq::find($id);

return view('admin.faqs._delete_faq', [
'faq' => $faq,
]);
}

/**
* Creates or edits an faq.
*
* @param App\Services\FaqService $service
* @param int $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postDeleteFaqQuestion(Request $request, FaqService $service, $id) {
if ($id && $service->deleteFaq(Faq::find($id), Auth::user())) {
flash('Faq deleted successfully.')->success();
} else {
foreach ($service->errors()->getMessages()['error'] as $error) {
flash($error)->error();
}
}

return redirect()->to('admin/data/faqs');
}
}
43 changes: 43 additions & 0 deletions app/Http/Controllers/BrowseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use App\Models\Species\Species;
use App\Models\Species\Subtype;
use App\Models\User\User;
use App\Models\Faq;
use Auth;
use Config;
use Illuminate\Http\Request;
use Settings;

Expand Down Expand Up @@ -633,4 +635,45 @@ public function getSublist(Request $request, $key) {
'userOptions' => User::query()->orderBy('name')->pluck('name', 'id')->toArray(),
]);
}

/**
* Shows the frequently asked questions page.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getFaq(Request $request) {
$tags = Config::get('lorekeeper.faq');
// tags is an array of names, make it so their key is their name also
$tags = array_combine($tags, $tags);
return view('browse.faq', [
'faqs' => Faq::visible(Auth::check() ? Auth::user() : null)->get(),
'tags' => $tags,
]);
}

/**
* Returns query for the FAQ page.
*
*/
public function getFaqSearch(Request $request) {
$tags = $request->get('tags') ?? [];
$content = $request->get('content') ?? null;

return view('browse._faq_content', [
'faqs' => Faq::visible(Auth::check() ? Auth::user() : null)->where(function ($query) use ($tags, $content) {
if ($tags) {
// the query must contain ALL tags that are selected
foreach ($tags as $tag) {
// json decode the tag column
$query->whereJsonContains('tags', $tag);
}
}
if ($content) {
$query->where(function ($query) use ($content) {
$query->where('question', 'LIKE', '%'.$content.'%')->orWhere('answer', 'LIKE', '%'.$content.'%');
});
}
})->get(),
]);
}
}
60 changes: 60 additions & 0 deletions app/Models/Faq.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Models;

class Faq extends Model {

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'question', 'answer', 'parsed_answer', 'tags', 'is_visible',
];

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'faq';

/**
* Whether the model contains timestamps to be saved and updated.
*
* @var string
*/
public $timestamps = true;

/**
* Validation rules for creation.
*
* @var array
*/
public static $createRules = [
'question' => 'required',
'answer' => 'required',
];

/**********************************************************************************************
SCOPES
**********************************************************************************************/

/**
* Scope a query to only include visible posts.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeVisible($query, $user = null) {
if ($user && $user->isStaff) {
return $query;
}

return $query->where('is_visible', 1);
}
}
126 changes: 126 additions & 0 deletions app/Services/FaqService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace App\Services;

use Illuminate\Support\Str;
use App\Models\Faq;
use Config;
use DB;

class FaqService extends Service {
/*
|--------------------------------------------------------------------------
| Faq Service
|--------------------------------------------------------------------------
|
| Handles the creation and editing of faq categories and faq.
|
*/

/**********************************************************************************************
FAQ
**********************************************************************************************/

/**
* Creates a new faq.
*
* @param array $data
* @param \App\Models\User\User $user
*
* @return \App\Models\Faq\Faq|bool
*/
public function createFaq($data, $user) {
DB::beginTransaction();

try {
if (!isset($data['is_visible'])) {
$data['is_visible'] = 0;
}

$data['parsed_answer'] = parse($data['answer']);

// make tags array json
if (isset($data['tags'])) {
$data['tags'] = json_encode($data['tags']);
}

$faq = Faq::create($data);

if (!$this->logAdminAction($user, 'Created FAQ Question', 'Created FAQ Question:'. Str::words($faq->question, 5, '...'))) {
throw new \Exception('Failed to log admin action.');
}

return $this->commitReturn($faq);
} catch (\Exception $e) {
$this->setError('error', $e->getMessage());
}

return $this->rollbackReturn(false);
}

/**
* Updates an faq.
*
* @param \App\Models\Faq\Faq $faq
* @param array $data
* @param \App\Models\User\User $user
*
* @return \App\Models\Faq\Faq|bool
*/
public function updateFaq($faq, $data, $user) {
DB::beginTransaction();

try {

if (!isset($data['is_visible'])) {
$data['is_visible'] = 0;
}

$data['parsed_answer'] = parse($data['answer']);

// make tags array json
if (isset($data['tags'])) {
$data['tags'] = json_encode($data['tags']);
}

if (!$this->logAdminAction($user, 'Updated FAQ Question', 'Updated FAQ Question:'. Str::words($faq->question, 5, '...'))) {
throw new \Exception('Failed to log admin action.');
}

$faq->update($data);

return $this->commitReturn($faq);
} catch (\Exception $e) {
$this->setError('error', $e->getMessage());
}

return $this->rollbackReturn(false);
}

/**
* Deletes an faq.
*
* @param \App\Models\Faq\Faq $faq
* @param mixed $user
*
* @return bool
*/
public function deleteFaq($faq, $user) {
DB::beginTransaction();

try {
if (!$this->logAdminAction($user, 'Deleted FAQ Question', 'Deleted FAQ Question:'. Str::words($faq->question, 5, '...'))) {
throw new \Exception('Failed to log admin action.');
}
$faq->delete();

return $this->commitReturn(true);
} catch (\Exception $e) {
$this->setError('error', $e->getMessage());
}

return $this->rollbackReturn(false);
}
}
Loading

0 comments on commit 0d0a1e5

Please sign in to comment.