/home/mip/mip/app/Modules/Client/Controllers/Officers/OfficerController.php
<?php
namespace QxCMS\Modules\Client\Controllers\Officers;
use DB;
use Auth;
use Carbon\Carbon;
use Datatables;
use Illuminate\Http\Request;
use QxCMS\Http\Controllers\Controller;
use QxCMS\Modules\Client\Requests\Officers\OfficerRequest;
use QxCMS\Modules\Client\Repositories\Officers\OfficerRepositoryInterface as Officer;
use QxCMS\Modules\Client\Repositories\Settings\Roles\RoleRepositoryInterface as Role;
use QxCMS\Modules\Client\Repositories\Settings\Roles\PermissionRepositoryInterface as Permission;
class OfficerController extends Controller
{
protected $officer;
protected $role;
protected $permission;
protected $auth;
protected $prefix_name = '';
protected $module_id = 10;
public function __construct(Officer $officer, Role $role, Permission $permission)
{
$this->officer = $officer;
$this->role = $role;
$this->permission = $permission;
$this->auth = Auth::guard('client');
$this->prefix_name = config('modules.client');
$this->getDetails($this->module_id);
}
public function permissions()
{
return $this->permission->getPermission($this->module_id);
}
public function index()
{
$this->authorize('activated', $this->permissions());
return view('Client::officers.index', array(), $this->globalData());
}
public function create()
{
$this->authorize('create', $this->permissions());
return view('Client::officers.create', array(), $this->globalData());
}
public function store(OfficerRequest $request)
{
$this->authorize('create', $this->permissions());
$input = $request->all();
$input['role_id'] = ((strtolower($input['access_type']) == 'editor') ? $this->role->getEditorId():$this->role->getFieldOfficerId());
$officer = $this->officer->create($input);
session()->flash('success', 'Successfully added.');
return redirect($this->prefix_name.'/officers');
}
public function show($id)
{
$this->authorize('activated', $this->permissions());
return $id;
}
public function edit($hashid)
{
$this->authorize('update', $this->permissions());
$id = decode($hashid);
$data['officer'] = $this->officer->findById($id);
$data['userLogs'] = $this->officer->getUserLogs($this->module_id, $id);
return view('Client::officers.edit', $data, $this->globalData());
}
public function update(OfficerRequest $request, $hashid)
{
$this->authorize('update', $this->permissions());
$id = decode($hashid);
$input = $request->all();
$input['role_id'] = ((strtolower($input['access_type']) == 'editor') ? $this->role->getEditorId():$this->role->getFieldOfficerId());
$officer = $this->officer->update($id, $input);
return redirect($this->prefix_name.'/officers');
}
public function destroy($hashid)
{
$this->authorize('delete', $this->permissions());
$id = decode($hashid);
return $this->officer->delete($id);
}
public function globalData()
{
return [
'permissions' => $this->permissions(),
'access_types' => $this->officer->getAccessTypes(),
'userLogs' => $this->officer->getUserLogs($this->module_id)
];
}
public function getDatatablesIndex(Request $request)
{
$permissions = $this->permissions();
$officers = $this->officer->datatablesIndex();
$permissions = $this->permissions();
$dateformats = getDateFormatConfig('get');
return Datatables::of($officers)
->filter(function($query) use ($request) {
if($request->get('access_type')){
$query->where('access_type', $request->get('access_type'));
}
if ($keyword = $request->get('search')['value']) {
$query->whereRaw("name like ?", ["%{$keyword}%"]);
$query->whereRaw("email like ?", ["%{$keyword}%"]);
}
})
->editColumn('status', function($model){
if($model->status == 0) return '<span class="label label-danger">Inactive</span>';
return '<span class="label label-success">Active</span>';
})
->editColumn('validity_date', function($model) use ($dateformats) {
if($model->validity_date == '') return "<span class=\"label label-warning\">No Access Validity</label>";
else {
$validity_date = Carbon::createFromFormat($dateformats['php'], $model->validity_date);
if(!$validity_date->lt(Carbon::now())) {
return $model->validity_date;
} else {
return '<span class="label label-danger">Account Expired</label>';
}
}
})
->editColumn('access_type', function($model) {
$access_type = strtolower($model->access_type);
if($access_type == 'field officer') return ucwords($access_type) . ' ('.$model->access_view.')';
else return ucwords($access_type);
})
->addColumn('action', function ($model) use ($permissions) {
$html_out = '';
if($this->auth->user()->can('update', $permissions)) {
$html_out .= '<a href="'.url($this->prefix_name.'/officers/'.$model->hashid.'/edit').'" class="btn btn-xs btn-flat btn-warning" data-toggle="tooltip" data-placement="top" title="Edit"><i class="fa fa-pencil"></i></a>';
}
if($this->auth->user()->can('delete', $permissions)) {
$html_out .= ' <a href="#delete-'.$model->hashid.'" class="btn btn-xs btn-flat btn-danger" id="btn-delete" data-action="'.url($this->prefix_name).'/officers/'.$model->hashid.'" data-toggle="tooltip" data-placement="top" title="Delete"><i class="fa fa-trash-o"></i></a>';
}
return $html_out;
})
->make(true);
}
}