Eshop-Laravel/app/User.php

161 lines
5.3 KiB
PHP
Raw Normal View History

2019-10-18 09:25:37 +00:00
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB,Session, Hash , Image;
class User extends Model
{
static public function validate($request){
$valid = false;
$email = $request['email'];
$password = $request['password'];
$sql = "SELECT * FROM users u JOIN user_roles r ON u.id = r.uid WHERE u.email = ?";
$user = DB::select($sql,[$email]);
if($user){
$user = $user[0];
if (Hash::check( $password ,$user->password ) ) {
if ($user->role == 6) Session::put('is_admin' , true);
Session::put('user_id' , $user->id);
Session::put('user_name' , $user->name);
Session::flash('sm' , 'Welcome Back ' . $user->name);
$valid = true;
}else{
//Wrong Password
}
}else{
//User Not Found
}
return $valid;
}
static public function save_new($data){
$user = new self();
$user->name = $data['name'];
$user->email = $data['email'];
$user->password = bcrypt($data['password']);
$user->last_name = $data['last_name'];
$user->streetNum = $data['streetNum'];
$user ->city = $data['city'];
$user ->ZIP = $data['ZIP'];
$user->country = $data['country'];
$user->phone = $data['phone'];
$user ->profilePic = 'no_profile_pic.jpg';
$user->street = $data['street'];
$user ->state = $data['state'];
$user->save();
$uid = $user ->id;
DB::insert("INSERT INTO user_roles VALUES($uid,7)" );
Session::put('user_id' , $uid);
Session::put('user_name' , $data->name);
Session::flash('sm' , $data->name . ' Your account created , you\'re Now LogedIn ');
}
static public function getUserInfo(){
$sql = "SELECT * FROM users u WHERE u.id = ?";
$user = DB::select($sql , [Session::get('user_id')]);
return $user;
}
static public function updateUser($data){
$user = User::find(Session::get('user_id') );
$user ->name = $data['name'];
$user ->last_name = $data['last_name'];
$user ->streetNum = $data['streetNum'];
$user ->street = $data['street'];
$user ->city = $data['city'];
$user ->ZIP = $data['ZIP'];
$user ->country = $data['country'];
$user ->phone =$data['phone'];
Session::put('user_name' , $data['name']);
Session::flash('sm' , $data->name . ' Your account as Been Updated! ');
$user ->save();
}
static public function getPass($data){
$uid = Session::get('user_id');
$sql = "SELECT password FROM users u WHERE u.id = ? ";
$password_old = $data['password_old'];
$user_password = DB::select($sql,[$uid]);
if (Hash::check( $password_old , $user_password[0]->{'password'} ) ) {
$user = User::find(Session::get('user_id') );
$user ->password = bcrypt($data['password']);
$user ->save();
Session::flash('sm' , Session::get('user_name') . ' Your Password as Been Updated! ');
return true;
}else{
Session::flash('sm-error' , Session::get('user_name') . ' Please Enter your current Password ');
return false;
}
}
static public function getAllUsers(){
if(Session::get('is_admin') ){
$sql = "SELECT * FROM users u JOIN user_roles r ON u.id = r.uid";
return DB::select($sql , []);
}
}
static public function getUserInfoCms($uid){
if(is_numeric($uid)){
$sql = "SELECT * FROM users u WHERE u.id = ?";
return DB::select($sql , [$uid]);
}
}
static public function update_Pic($request , $id){
$image_name = self::loadImage($request);
//save the pic in the DB
$user = self::find($id);
if ($image_name) {
$user ->profilepic = $image_name;
}
$user ->save();
Session::flash('sm' , $user['name'].' Your Pic As been Updated!');
}
static private function loadImage($request){
$image_name = '';
if ($request->hasFile('image') && $request->file('image')->isValid()) {
//take the user pic to our server
$file = $request->file('image');
$image_name = date('Y.m.d.H.i.s') . '-' . $file->getClientOriginalName();
$request->file('image')->move( public_path() . '/images/profilePics' , $image_name);
//take the origin pic and make a new one by using Image Class to provent viruses
$img = Image::make(public_path() . '/images'.'/profilePics/' . $image_name);
//resize the pic to 300 Width
$img->resize(300, null, function ($constraint) {
$constraint->aspectRatio();
});
//save the new pic and overWrite the pic
$img ->save(public_path() . '/images'.'/profilePics/' . $image_name);
}
return $image_name;
}
static public function updateUserCms($request , $uid){
$user = User::find($uid);
$user ->name = $request['name'];
$user ->last_name = $request['last_name'];
$user ->streetNum = $request['streetNum'];
$user ->street = $request['street'];
$user ->city = $request['city'];
$user ->ZIP = $request['ZIP'];
$user ->country = $request['country'];
$user ->phone =$request['phone'];
Session::flash('sm' , $request->name . ' The account as Been Updated! ');
$user ->save();
}
}