<?php
namespace App\Repositories;
use App\Contracts\UserInterface;
use App\User;
use Carbon\Carbon;
use Datatables;
class EloquentUserRepository implements UserInterface {
/**
* Create a new repository instance.
*
* @return void
*/
public function __construct() {}
/**
* Get users datatable json
* @return JSON Datatable json object
*/
function data() {
$users = User::select(array(
'id',
'name',
'email',
'user_level',
'date_joined',
'is_activated',
));
return Datatables::of($users)
->make(true);
}
/**
* Get user by user ID
* @param int $userId User ID
* @return json object
*/
function getUserById($userId) {
$user = User::select(array(
'id',
'name',
'username',
'email',
'address',
'phone_no_1',
'phone_no_2',
'date_joined',
'profile_image',
'password',
'user_level',
'is_activated',
'report_to',
))->where('users.id', '=', $userId)->first();
return \Response::json($user);
}
function activate(Array $payload) {
$user = User::where('id', $payload['uid'])->first();
if (!empty($user) && !is_null($user)) {
$currentDateTime = Carbon::now();
$user->user_level = ($payload['user_level']) ? $payload['user_level'] : Null;
$user->is_activated = ($payload['activate']) ? $payload['activate'] : 0;
if ($payload['activate'] == 1 && $user->date_joined == NULL) {
$user->date_joined = $currentDateTime;
}
$success = $user->save();
return $success;
}
}
function updateProfile(Array $payload) {
$user = User::where('id', $payload['uid'])->first();
if (!empty($user) && !is_null($user)) {
if (!empty($payload['prof_pic'])) {
$file = $payload['prof_pic'];
$destination = public_path() . "/images/profiles/" . $payload['uid'];
$ext = $file->getClientOriginalExtension();
$filename_original = $file->getClientOriginalName();
$filename = md5($filename_original) . '.' . $ext;
$path = $destination . '/' . $filename;
$uploaded = $file->move($destination, $filename);
if ($uploaded) {
$user->profile_image = $payload['uid'] . "/" . $filename;
}
}
$user->name = ($payload['name']) ? $payload['name'] : "";
$user->email = ($payload['email']) ? $payload['email'] : "";
$user->phone_no_1 = ($payload['phone_no1']) ? $payload['phone_no1'] : Null;
$user->phone_no_2 = ($payload['phone_no2']) ? $payload['phone_no2'] : Null;
$user->address = ($payload['address']) ? $payload['address'] : Null;
$user->report_to = ($payload['report_to']) ? $payload['report_to'] : Null;
$success = $user->save();
return $success;
}
}
}
namespace App\Repositories;
use App\Contracts\UserInterface;
use App\User;
use Carbon\Carbon;
use Datatables;
class EloquentUserRepository implements UserInterface {
/**
* Create a new repository instance.
*
* @return void
*/
public function __construct() {}
/**
* Get users datatable json
* @return JSON Datatable json object
*/
function data() {
$users = User::select(array(
'id',
'name',
'email',
'user_level',
'date_joined',
'is_activated',
));
return Datatables::of($users)
->make(true);
}
/**
* Get user by user ID
* @param int $userId User ID
* @return json object
*/
function getUserById($userId) {
$user = User::select(array(
'id',
'name',
'username',
'email',
'address',
'phone_no_1',
'phone_no_2',
'date_joined',
'profile_image',
'password',
'user_level',
'is_activated',
'report_to',
))->where('users.id', '=', $userId)->first();
return \Response::json($user);
}
function activate(Array $payload) {
$user = User::where('id', $payload['uid'])->first();
if (!empty($user) && !is_null($user)) {
$currentDateTime = Carbon::now();
$user->user_level = ($payload['user_level']) ? $payload['user_level'] : Null;
$user->is_activated = ($payload['activate']) ? $payload['activate'] : 0;
if ($payload['activate'] == 1 && $user->date_joined == NULL) {
$user->date_joined = $currentDateTime;
}
$success = $user->save();
return $success;
}
}
function updateProfile(Array $payload) {
$user = User::where('id', $payload['uid'])->first();
if (!empty($user) && !is_null($user)) {
if (!empty($payload['prof_pic'])) {
$file = $payload['prof_pic'];
$destination = public_path() . "/images/profiles/" . $payload['uid'];
$ext = $file->getClientOriginalExtension();
$filename_original = $file->getClientOriginalName();
$filename = md5($filename_original) . '.' . $ext;
$path = $destination . '/' . $filename;
$uploaded = $file->move($destination, $filename);
if ($uploaded) {
$user->profile_image = $payload['uid'] . "/" . $filename;
}
}
$user->name = ($payload['name']) ? $payload['name'] : "";
$user->email = ($payload['email']) ? $payload['email'] : "";
$user->phone_no_1 = ($payload['phone_no1']) ? $payload['phone_no1'] : Null;
$user->phone_no_2 = ($payload['phone_no2']) ? $payload['phone_no2'] : Null;
$user->address = ($payload['address']) ? $payload['address'] : Null;
$user->report_to = ($payload['report_to']) ? $payload['report_to'] : Null;
$success = $user->save();
return $success;
}
}
}
No comments:
Post a Comment