unioil-loyalty-app/app/Services/AdminService.php

258 lines
8.0 KiB
PHP

<?php
namespace App\Services;
use Response;
use Schema;
use Hash;
use Illuminate\Http\Request;
use App\Libraries\ListHelper;
use App\Contracts\AdminResourceInterface;
use App\Contracts\PasswordLogsResourceInterface;
use App\Admin;
use App\Helpers\HttpStatusCode;
use App\Libraries\UuidHelper;
use App\Helpers\CurrentUserHelper;
use App\Libraries\StaticContents;
class AdminService implements AdminResourceInterface
{
public $admin;
public $admin_id;
public $password_logs;
public function __construct(Request $request, PasswordLogsResourceInterface $password_logs)
{
$this->password_logs = $password_logs;
}
public function listing($params)
{
$currentUser = CurrentUserHelper::get_currentAdmin();
$pagination = ListHelper::validatePagination($params['page_size'],$params['page']);
$list = Admin::where('is_active','=',1);
if($params['search'] != null)
{
$list->where(function($query) use ($params){
$query->where('username', 'LIKE', '%'.$params['search'].'%');
$query->orWhere('firstname', 'LIKE', '%'.$params['search'].'%');
$query->orWhere('lastname', 'LIKE', '%'.$params['search'].'%');
$query->orWhere('email', 'LIKE', '%'.$params['search'].'%');
});
}
if($params['filter'])
{
$list->where(function($query) use ($params){
foreach($params['filter'] as $field => $value)
{
if($value != null)
{
if(is_array($value))
{
foreach ($value as $v) {
$v = $field == 'status' ? StaticContents::admin_status($v, true) : $v;
$query->orWhere($field, $v);
}
}
else
{
$value = $field == 'status' ? StaticContents::admin_status($value, true) : $value;
$query->orWhere($field, $value);
}
}
}
});
}
$sorting = $params['sorting'];
if(count($sorting) > 0)
{
$list = $list->orderBy($sorting['field'],$sorting['sort_order']);
}
if($pagination == true)
return $list->paginate($params['page_size']);
else
return $list->get();
}
public function getAll()
{
$this->admin = Admin::all();
return $this->admin;
}
public function getByField($data)
{
$this->admin = new Admin;
if(count($data))
{
foreach ($data as $field => $value) {
$this->admin = $this->admin->where($field,$value);
}
}
return $this->admin->get();
}
public function store(Request $request)
{
$currentUser = CurrentUserHelper::get_currentAdmin();
$this->admin = new Admin;
$uuid = new UuidHelper;
$this->admin->admin_uuid = $uuid->generate_uuid1();
$this->admin->username = strtolower($request->get('username'));
$this->admin->password = bcrypt($request->get('password'));
$this->admin->firstname = ucwords($request->get('firstname'));
$this->admin->lastname = ucwords($request->get('lastname'));
$this->admin->email = $request->get('email');
$this->admin->role = $request->get('role');
$this->admin->status = StaticContents::admin_status($request->get('status'), true);
$this->admin->created_by = $currentUser->admin_id;
if ($this->admin->save())
{
$this->password_logs->store($this->admin->admin_id,$request->get('password'),$currentUser->admin_id, true);
return $this->admin->admin_id;
}
else
{
return false;
}
}
public function changePassword(Request $request)
{
$this->admin = Admin::where('admin_uuid',$request->get('admin_uuid'))->first();
$this->admin->password = bcrypt($request->get('password'));
$this->admin->is_passwordChanged = 1;
if ($this->admin->save())
{
return true;
}
else
{
return false;
}
}
public function show($uuid)
{
$this->admin = Admin::whereAdminUuid($uuid)->first();
$this->admin->created_by = $this->admin->created_by ? CurrentUserHelper::getAdminName($this->admin->created_by) : null ;
$this->admin->updated_by = $this->admin->updated_by ? CurrentUserHelper::getAdminName($this->admin->updated_by) : null;
return $this->admin;
}
public function update($request,$uuid)
{
$currentUser = CurrentUserHelper::get_currentAdmin();
$this->admin = Admin::where('admin_uuid',$uuid)->first();
if($this->admin)
{
$this->admin->username = strtolower($request->username);
$this->admin->firstname = ucwords($request->firstname);
$this->admin->lastname = ucwords($request->lastname);
$this->admin->email = $request->email;
$this->admin->status = StaticContents::admin_status($request->status, true);
$this->admin->updated_by = $currentUser->admin_id;
if($request->has('role'))
$this->admin->role = $request->role;
if($request->has('password') && ($request->password != '' || $request->password != null))
{
$this->admin->password = bcrypt($request->password);
$this->admin->is_passwordChanged = 0;
}
if ($this->admin->save())
{
return $this->admin;
}
}
return false;
}
public function changeStatus($status,$uuid)
{
$currentUser = CurrentUserHelper::get_currentAdmin();
$this->admin = Admin::where('admin_uuid',$uuid)->first();
$this->admin->status = $status;
$this->admin->updated_by = $currentUser->admin_id;
if ($this->admin->save())
{
return true;
}
else
{
return false;
}
}
public function delete($uuid)
{
$currentUser = CurrentUserHelper::get_currentAdmin();
if(is_array($uuid))
{
$this->admin = Admin::whereIn('admin_uuid',$uuid)
->update([
'is_active' => 0,
'updated_by' => $currentUser->admin_id
]);
if($this->admin)
return true;
else
return false;
}
else
{
$this->admin = Admin::where('admin_uuid',$uuid)->first();
$this->admin->is_active = 0;
$this->admin->updated_by = $currentUser->admin_id;
if ($this->admin->save())
return true;
else
return false;
}
}
public function getDetailsWhereIn($field,$value,$relationship = null)
{
if($relationship)
{
$details = Admin::with($relationship)
->whereIn($field,$value)->get();
}
else
{
$details = Admin::whereIn($field,$value)->get();
}
return $details->toArray();
}
}