/home/mip/mip/app/Modules/Client/Controllers/Pages/FAQController.php
<?php

namespace QxCMS\Modules\Client\Controllers\Pages;

use Auth;
use Session;
use Datatables;

use Illuminate\Http\Request;
use QxCMS\Http\Controllers\Controller;

use QxCMS\Modules\Client\Requests\Pages\FAQRequest;
use QxCMS\Modules\Client\Repositories\Pages\FAQRepositoryInterface as FAQ;
use QxCMS\Modules\Client\Repositories\Settings\Roles\PermissionRepositoryInterface as Permission;

class FAQController extends Controller
{
	protected $faq;
	protected $permission;
	protected $auth;
	protected $prefix_name = '';

	public function __construct(Permission $permission, FAQ $faq)
	{
		$this->faq = $faq;

		$this->permission = $permission;
		$this->auth = Auth::guard('client');
		$this->prefix_name = config('modules.client');
        $this->getDetails($this->faq->getModuleId());
	}

	public function permissions()
    {
        return $this->permission->getPermission($this->faq->getModuleId());
    }

    public function index() 
    {
    	$this->authorize('activated', $this->permissions());
    	$data['permissions'] = $this->permissions();
        $data['userLogs'] = $this->faq->getUserLogs($this->faq->getModuleId());
        return view('Client::pages.faq.index', $data, $this->getFormOptions());
    }

    public function create()
    {
        $this->authorize('create', $this->permissions());
        $data['permissions'] = $this->permissions();
        return view('Client::pages.faq.create', $data, $this->getFormOptions());
    }

    public function store(FAQRequest $request)
    {
        $this->authorize('create', $this->permissions());
        $faq = $this->faq->create($request);
        session()->flash('success', 'Successfully added.');

        return redirect()->route($this->prefix_name.'.faq.index');
    }

    public function show($id)
    {
        //
    }

    public function edit($hashid)
    {
        $this->authorize('update', $this->permissions());
        $id = decode($hashid);
        $data['permissions'] = $this->permissions();
        $data['faq'] = $this->faq->findById($id);
        $data['userLogs'] = $this->faq->getUserLogs($this->faq->getModuleId(), $id);
        return view('Client::pages.faq.edit', $data, $this->getFormOptions());
    }

    public function update(FAQRequest $request, $hashid)
    {
        $this->authorize('update', $this->permissions());
        $id = decode($hashid);
        $faq = $this->faq->update($id, $request);
        return redirect()->route($this->prefix_name.'.faq.index');
    }

    public function destroy($hashid)
    {
        $this->authorize('delete', $this->permissions());
        $id = decode($hashid);
        return $this->faq->delete($id);
    }

    public function details(Request $request, $hashid)
    {
        $id = decode($hashid);
        $faq = $this->faq->findById($id);
        if($request->ajax()){
            return $faq;
        }
    }

    public function getFAQData(Request $request)
    {
        $faq = $this->faq->select('*');

        $permissions = $this->permissions();
        return Datatables::of($faq)
            ->filter(function ($query) use ($request){
                if ($request->has('title')){
                    $query->where('title', 'like', "%{$request->get('title')}%");
                }
                if ($request->has('status')){
                    $query->where('status', '=', "{$request->get('status')}");
                }
            })
            ->editColumn('title', function($faq){
                return '<span data-toggle="tooltip" data-placement="top" title="View Details"><a href="#faq-details-modal" data-toggle="modal" data-details_url="'.route($this->prefix_name.'.faq.details', $faq->hashid).'">'.$faq->title.'</a></span>';
            })
            ->addColumn('action', function ($faq) use ($permissions){
                $html = '';
                if($this->auth->user()->can('update', $permissions)){
                    $html .= '<a href="'.route($this->prefix_name.'.faq.edit', $faq->hashid).'"><button type="button" data-toggle="tooltip" title="Edit" class="btn btn-warning btn-xs btn-flat edited"><span class="glyphicon glyphicon-pencil"></span></button></a>';
                }
                if($this->auth->user()->can('delete', $permissions)){
                    $html .= '&nbsp;&nbsp;<a href="#delete-'.$faq->hashid.'" class="btn btn-xs btn-flat btn-danger" id="btn-delete" data-action="'.route($this->prefix_name.'.faq.destroy', $faq->hashid).'" data-toggle="tooltip" data-placement="top" title="Delete"><i class="fa fa-trash-o"></i></a>';
                }
                return $html;
            })
            ->make(true);
    }

    protected function getFormOptions()
    {
        $data['status'] = Array('Publish' => 'Publish', 'Draft' => 'Draft', 'Inactive' => 'Inactive');
        return $data;
    }
}