Permalink
Browse files

Admin - FAQ (questions and answers with categories)

  • Loading branch information...
bpocallaghan committed Jul 9, 2017
1 parent a52d3bb commit cf3b928815970ee7afae6dc4733e40df28a3f902
@@ -0,0 +1,101 @@
<?php
namespace App\Http\Controllers\Admin\Faq;
use Redirect;
use App\Http\Requests;
use App\Models\FaqCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Admin\AdminController;
class CategoriesController extends AdminController
{
/**
* Display a listing of faq_category.
*
* @return Response
*/
public function index()
{
save_resource_url();
return $this->view('faq.categories.index')->with('items', FaqCategory::all());
}
/**
* Show the form for creating a new faq_category.
*
* @return Response
*/
public function create()
{
return $this->view('faq.categories.create_edit');
}
/**
* Store a newly created faq_category in storage.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$this->validate($request, FaqCategory::$rules, FaqCategory::$messages);
$this->createEntry(FaqCategory::class, $request->only('name'));
return redirect_to_resource();
}
/**
* Display the specified faq_category.
*
* @param FaqCategory $category
* @return Response
*/
public function show(FaqCategory $category)
{
return $this->view('faq.categories.show')->with('item', $category);
}
/**
* Show the form for editing the specified faq_category.
*
* @param FaqCategory $category
* @return Response
*/
public function edit(FaqCategory $category)
{
return $this->view('faq.categories.create_edit')->with('item', $category);
}
/**
* Update the specified faq_category in storage.
*
* @param FaqCategory $category
* @param Request $request
* @return Response
*/
public function update(FaqCategory $category, Request $request)
{
$this->validate($request, FaqCategory::$rules, FaqCategory::$messages);
$this->updateEntry($category, $request->only('name'));
return redirect_to_resource();
}
/**
* Remove the specified faq_category from storage.
*
* @param FaqCategory $category
* @param Request $request
* @return Response
*/
public function destroy(FaqCategory $category, Request $request)
{
$this->deleteEntry($category, $request);
return redirect_to_resource();
}
}
@@ -0,0 +1,107 @@
<?php
namespace App\Http\Controllers\Admin\Faq;
use App\Models\FaqCategory;
use Redirect;
use App\Http\Requests;
use App\Models\FAQ;
use Illuminate\Http\Request;
use App\Http\Controllers\Admin\AdminController;
class FaqsController extends AdminController
{
/**
* Display a listing of faq.
*
* @return Response
*/
public function index()
{
save_resource_url();
$items = FAQ::with('category')->get();
return $this->view('faq.index')->with('items', $items);
}
/**
* Show the form for creating a new faq.
*
* @return Response
*/
public function create()
{
$categories = FaqCategory::getAllList();
return $this->view('faq.create_edit', compact('categories'));
}
/**
* Store a newly created faq in storage.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$this->validate($request, FAQ::$rules, FAQ::$messages);
$this->createEntry(FAQ::class, $request->only('question', 'answer', 'category_id'));
return redirect_to_resource();
}
/**
* Display the specified faq.
*
* @param FAQ $faq
* @return Response
*/
public function show(FAQ $faq)
{
return $this->view('faq.show')->with('item', $faq);
}
/**
* Show the form for editing the specified faq.
*
* @param FAQ $faq
* @return Response
*/
public function edit(FAQ $faq)
{
$categories = FaqCategory::getAllList();
return $this->view('faq.create_edit', compact('categories'))->with('item', $faq);
}
/**
* Update the specified faq in storage.
*
* @param FAQ $faq
* @param Request $request
* @return Response
*/
public function update(FAQ $faq, Request $request)
{
$this->validate($request, FAQ::$rules, FAQ::$messages);
$this->updateEntry($faq, $request->only('question', 'answer', 'category_id'));
return redirect_to_resource();
}
/**
* Remove the specified faq from storage.
*
* @param FAQ $faq
* @param Request $request
* @return Response
*/
public function destroy(FAQ $faq, Request $request)
{
$this->deleteEntry($faq, $request);
return redirect_to_resource();
}
}
@@ -0,0 +1,89 @@
<?php
namespace App\Http\Controllers\Admin\Faq;
use App\Models\FAQ;
use App\Models\FaqCategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Titan\Controllers\TitanAdminController;
class OrderController extends TitanAdminController
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$html = $this->getOrderHtml();
return $this->view('faq.order')->with('itemsHtml', $html);
}
/**
* Update the order
* @param Request $request
* @return array
*/
public function updateOrder(Request $request)
{
$navigation = json_decode($request->get('list'), true);
foreach ($navigation as $key => $nav) {
$row = $this->updateListOrder($nav['id'], ($key + 1));
}
return ['result' => 'success'];
}
/**
* Generate the nestable html
*
* @param null $parent
*
* @return string
*/
private function getOrderHtml($parent = null)
{
$html = '<ol class="dd-list">';
$items = FaqCategory::with('faqs')->orderBy('name')->get();
foreach ($items as $key => $item) {
foreach ($item->faqs as $k => $faq) {
$html .= '<li class="dd-item" data-id="' . $faq->id . '">';
$html .= '<div class="dd-handle">';
$html .= '<strong>' . $item->name . '</strong> - ' . $faq->question . ' ' . ' <span style="float:right"> ' . $faq->answer_summary . ' </span></div>';
$html .= '</li>';
}
$html .= '<hr/>';
}
$html .= '</ol>';
return (count($items) >= 1 ? $html : '');
}
/**
* Update Navigation Item, with new list order and parent id (list and parent can change)
*
* @param $id
* @param $listOrder
* @param int $parentId
*
* @return mixed
*/
private function updateListOrder($id, $listOrder, $parentId = 0)
{
$row = FAQ::find($id);
$row->list_order = $listOrder;
$row->save();
return $row;
}
}
View
@@ -0,0 +1,49 @@
<?php
namespace App\Models;
use Titan\Models\TitanCMSModel;
use Bpocallaghan\Sluggable\HasSlug;
use Bpocallaghan\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\SoftDeletes;
class FAQ extends TitanCMSModel
{
use SoftDeletes, HasSlug;
protected $table = 'faqs';
protected $guarded = ['id'];
/**
* Validation rules for this model
*/
static public $rules = [
'question' => 'required|min:3:max:255',
'answer' => 'required|min:5:max:1500',
'category_id' => 'required|exists:faq_categories,id',
];
protected function getSlugOptions()
{
return SlugOptions::create()->generateSlugFrom('question');
}
/**
* Get the summary text
*
* @return mixed
*/
public function getAnswerSummaryAttribute()
{
return substr(strip_tags($this->attributes['answer']), 0, 80) . '...';
}
/**
* Get the category
*/
public function category()
{
return $this->belongsTo(FaqCategory::class, 'category_id', 'id');
}
}
View
@@ -0,0 +1,41 @@
<?php
namespace App\Models;
use Titan\Models\TitanCMSModel;
use Bpocallaghan\Sluggable\HasSlug;
use Illuminate\Database\Eloquent\SoftDeletes;
class FaqCategory extends TitanCMSModel
{
use SoftDeletes, HasSlug;
protected $table = 'faq_categories';
protected $guarded = ['id'];
/**
* Validation rules for this model
*/
static public $rules = [
'name' => 'required|min:3:max:255',
];
/**
* Get the faqs
*/
public function faqs()
{
return $this->hasMany(FAQ::class, 'category_id')->orderBy('list_order');
}
/**
* Get all the rows as an array (ready for dropdowns)
*
* @return array
*/
public static function getAllList()
{
return self::orderBy('name')->get()->pluck('name', 'id')->toArray();
}
}
Oops, something went wrong.

0 comments on commit cf3b928

Please sign in to comment.