/home/mip/mip/app/Modules/Client/Controllers/Posts/PostsController.php
<?php

namespace QxCMS\Modules\Client\Controllers\Posts;

use Auth;
use Datatables;
use Session;

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

use QxCMS\Modules\Client\Requests\Posts\PostRequest;
use QxCMS\Modules\Client\Repositories\Settings\Roles\PermissionRepositoryInterface as Permission;
use QxCMS\Modules\Client\Repositories\Posts\PostsRepositoryInterface as Posts;
use QxCMS\Modules\Client\Repositories\Posts\PostCategoryRepositoryInterface as PostCategory;

class PostsController extends Controller
{
	protected $posts;
	protected $permission;
	protected $auth;
	protected $prefix_name = '';

	public function __construct(Permission $permission, Posts $posts, PostCategory $postCategory)
	{
		$this->posts = $posts;
        $this->postCategory = $postCategory;

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

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

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

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

    public function store(PostRequest $request)
    {

        $this->authorize('create', $this->permissions());
        $posts = $this->posts->create($request); 
        session()->flash('success', 'Successfully added.');

        return redirect($this->prefix_name.'/posts');
    }

    public function show($id)
    {
        //
    }

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

    public function update(PostRequest $request, $hashid)
    {
        $this->authorize('update', $this->permissions());
        $id = decode($hashid);

        $posts = $this->posts->update($id, $request);
        return redirect($this->prefix_name.'/posts');
    }

    public function destroy($hashid)
    {
        $this->authorize('delete', $this->permissions());
        $id = decode($hashid);
        return $this->posts->delete($id);
    }
  
    public function getPostsData(Request $request)
    {
        $posts = $this->posts->select('*')->where('post_type', '=', 'Post')->with('author', 'category');

        $permissions = $this->permissions();
        return Datatables::of($posts)
            ->filter(function ($query) use ($request) {
                if ($request->has('title')) {
                    $query->where('title', 'like', "%{$request->get('title')}%");
                }
                if ($request->has('posts_category_id')) {
                    $query->where('posts_category_id', 'like', "%{$request->get('posts_category_id')}%");
                }
            })
            ->editColumn('category', function($row) {
                if($row->category == null) return 'N/A';
                else return $row->category->name;
            })
            ->editColumn('author', function($row) {
                if($row->author == null) return '';
                else return $row->author->name;
            })
            ->addColumn('date', function ($posts) {
                $html = "<small>Published <br> ".$posts->published_at."";
                if($posts->expired_at <> 0000-00-00){
                    $html.= "<br/>Expiring<br> ".$posts->expired_at."</small>";
                }else{
                    $html.= '';
                }
                return $html;
            })
            ->addColumn('action', function ($posts) use ($permissions){
                $html = '';
                if($this->auth->user()->can('update', $permissions)) {
                    $html .= '<a href="'.url('client').'/posts/edit/'.$posts->hashid.'"><button type="button" data-toggle="tooltip" title="Update" class="btn btn-warning btn-flat btn-xs edited"><span class="glyphicon glyphicon-pencil"></span></button></a>';
                }
                if($this->auth->user()->can('delete', $permissions)) {
                    $html .= '&nbsp;<button type="button" data-toggle="tooltip" title="Delete" class="btn btn-danger btn-xs btn-flat deleted" data-action="'.url('client').'/posts/destroy/'.$posts->hashid.'"><span class="glyphicon glyphicon-trash"></span></button>';
                }
                return $html;
            })
            ->orderColumn('date', 'posts.published_at $1, posts.expired_at $1')
            ->make(true);
    }

    protected function getFormOptions()
    {
        $data['category'] = $this->postCategory->postCategoryList();
        $data['visibility'] = Array('Public' => 'Public', 'Private' => 'Private');
        $data['status'] = Array('Publish' => 'Publish', 'Draft' => 'Draft', 'Inactive' => 'Inactive');
        return $data;
    }

}