forked from corowne/lorekeeper
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c645eb8
commit 0d0a1e5
Showing
16 changed files
with
802 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.