Advertisement
Guest User

Untitled

a guest
Jan 7th, 2017
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.83 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Models;
  4. use Illuminate\Support\Facades\Log;
  5.  
  6.  
  7. use DB;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Auth\Authenticatable;
  10. use Illuminate\Auth\Passwords\CanResetPassword;
  11. use Illuminate\Foundation\Auth\Access\Authorizable;
  12. use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  13. use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
  14. use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
  15. use SammyK\LaravelFacebookSdk\SyncableGraphNodeTrait as SyncableGraphNodeTrait;
  16. use Auth;
  17. use App\Models\FavoritePost;
  18. use App\Models\FavoriteUser;
  19.  
  20. class User extends Model implements AuthenticatableContract,
  21.     AuthorizableContract,
  22.     CanResetPasswordContract
  23. {
  24.     use Authenticatable, Authorizable, CanResetPassword, SyncableGraphNodeTrait;
  25.  
  26.     protected $table = 'user';
  27.     protected $hidden = ['password', 'salt', 'token', 'remember_token'];
  28.     protected $fillable =  ['first_name', 'last_name', 'email', 'phone', 'video', 'is_teacher', 'updated_at'];
  29.    
  30.     public function posts() {
  31.         return $this->hasMany('App\Models\Post');
  32.     }
  33.  
  34.    /* public function fav(){
  35.         return $this->hasMany('App\Models\FavoriteUser');
  36.    }*/
  37.    
  38.     public function considerPosts() {
  39.         return $this->belongsToMany('App\Models\Post', 'consider')->withTimestamps();
  40.     }
  41.    
  42.    // public function favouritedPosts() {
  43.         //return $this->belongsToMany('App\Models\Post', 'favorite_posts');
  44.      //   $posts = FavoritePost::where(['favorited_by_user_id'=>Auth::user()->id])->get();
  45.        // return $posts;
  46.     //}
  47.    
  48.     //public function favouritedUsers() {
  49.         // return $this->belongsToMany('App\Models\User', 'favorite_users', 'user_id', 'favorited_by_user_id');
  50.       //  $users = FavoritePost::where(['favorited_by_user_id'=>Auth::user()->id])->get();
  51.         //return $users;
  52.     //}
  53.    
  54.     public function usermeta(){
  55.         return $this->hasMany('App\Models\UserMeta');
  56.     }    
  57.    
  58.     public function vouchers() {
  59.         return $this->hasMany('App\Models\Voucher');
  60.     }
  61.  
  62.    
  63.    
  64.  
  65.     public function getMetaAttribute(){
  66.         $data = array();
  67.         if(!empty($this->usermeta)){
  68.             foreach($this->usermeta as $meta){
  69.                 $data[$meta->key] = $meta->value;
  70.             }
  71.         }
  72.        
  73.          return $data;
  74.     }
  75.  
  76.     public function getisFavoriteAttribute()
  77. {
  78.     $is_favorite = FavoriteUser::where(['favorited_by_user_id'=>Auth::user()->id,'user_id'=>$this->id])->first();
  79.        
  80.     $favorite = ( ($is_favorite) ? true : false);
  81.     return $favorite;
  82. }
  83.  
  84.     public function getAvailabilityAttribute(){
  85.         $data = array();
  86.         if(isset($this->meta['availability']))
  87.             $data = explode('|',$this->meta['availability']);
  88.         return $data;
  89.  
  90.     }
  91.    
  92.     public static function getSaltByEmail($email){
  93.         return self::where('email', $email)->value('salt');
  94.     }
  95.  
  96.     public static function noteUserLogin($user) {
  97. //        $user->last_login_at = new \DateTime();
  98.         $user->save();
  99.     }
  100.  
  101.     public static function _save($request, $id = false){
  102.         Log::debug("INIT");
  103.    
  104.         if ($id) {
  105.             Log::debug("FIRSTBRANCH");
  106.             $user = self::find($id);
  107.  
  108.         } else {
  109.             Log::debug("SECONDBRANCH");
  110.             $user = new User();
  111.         }
  112.         if ($request->email) {
  113.             $user->email = $request->email;
  114.         }
  115.         $user->first_name    = ucwords(strtolower($request->first_name));
  116.         $user->last_name     = ucwords(strtolower($request->last_name));
  117.          
  118.         if ($request->password) {
  119.             $user->salt     = uniqid(rand(), true);
  120.             $user->password = bcrypt($request->password . $user->salt);
  121.         }
  122.        
  123.         $user->save();
  124.         if(!$id)
  125.         $user->token = User::generateToken($user);
  126.    
  127.         return $user;
  128.     }
  129.    
  130.    
  131.     public static function _save_teacher($request, $email = false){
  132.      
  133.       if(Auth::check()){
  134.          
  135.         $user = Auth::user();
  136.    
  137.       }else
  138.       {
  139.         $user = new User();
  140.         $user->email = $request->email;
  141.          
  142.       }
  143.        
  144.         $user->first_name    = ucwords(strtolower($request->first_name));
  145.         $user->last_name     = ucwords(strtolower($request->last_name));
  146.         $user->phone     = $request->phone;
  147.         $user->video     = $request->video;
  148.         $user->is_teacher     = 1;
  149.  
  150.         if ($request->password) {
  151.             $user->salt     = uniqid(rand(), true);
  152.             $user->password = bcrypt($request->password . $user->salt);
  153.         }
  154.  
  155.         return $user->save();
  156.     }
  157.  
  158.     public static function getUserbyEmail($email){
  159.         return  self::where('email',$email)->first();
  160.     }
  161.  
  162.     /**
  163.      * @param $password
  164.      * @param $user
  165.      */
  166.     public  static function changePass($password, $user){
  167.         $user->salt = uniqid(rand(), true);
  168.         $user->password = bcrypt($password.$user->salt);
  169.         $user->token = NULL;
  170.         $user->save();
  171.  
  172.     }
  173.    
  174.     public static function checkToken($token){
  175.  
  176.         return self::where('token', $token)->first();
  177.     }
  178.  
  179.     public static  function generateToken($user){
  180.         $user->token = md5(microtime(true));
  181.         $user->save();
  182.         return $user->token;
  183.     }
  184.  
  185.     public static function getUsersExceptAdmin($admin){
  186.         $users = User::where('id', '!=', $admin)->where('status',1)->get();
  187.         return $users;
  188.     }
  189.    
  190.     public function renderLocation()
  191.     {        
  192.         if (!empty($this->city)) {          
  193.             $location = (empty($this->country)) ?
  194.                             $this->city :
  195.                             $this->city.', '.$this->country;
  196.         }
  197.         elseif (!empty($this->country)) {
  198.             $location = $this->country;
  199.         }
  200.         else {
  201.             $location = '';
  202.         }
  203.         return $location;
  204.     }
  205.    
  206.     public function getFullName()
  207.     {        
  208.         if (!empty($this->first_name)) {          
  209.             $fullname = (empty($this->last_name)) ?
  210.                             $this->first_name :
  211.                             $this->first_name.' '.$this->last_name;
  212.         }
  213.         elseif (!empty($this->last_name)) {
  214.             $fullname = $this->last_name;
  215.         }
  216.         else {
  217.             $fullname = '';
  218.         }
  219.         return $fullname;
  220.     }
  221.    
  222.     public function getImage()
  223.     {
  224.         return (!empty($this->image) && file_exists($this->image)) ? $this->image : 'images/avatar.png';
  225.     }
  226.  
  227.     public function getNameAttribute(){
  228.         return $this->first_name . ' ' . $this->last_name;
  229.     }
  230.  
  231.     public function getRatingsAttribute(){
  232.        
  233.         $orders_for_post = array();
  234.  
  235.         $post_rating = 0;      
  236.         $post_reviews = 0;     
  237.        if(count($this->posts)){
  238.            
  239.            foreach($this->posts as $post){
  240.                 $ratings = 0;
  241.                 $order_count = 0;    
  242.                
  243.                if(count($post->orders)){
  244.        
  245.                     foreach ($post->orders as $order){
  246.                         if($order->order_rating->rated){
  247.  
  248.  
  249.                         $user_who_reviewed = DB::table('orders')->select('user_id')->where('id',$order->order_rating->order_review->order_id)->first()->user_id;
  250.                         $name_of_user_who_reviewed = DB::table('user')->select('first_name','last_name')->where('id',$user_who_reviewed)->first();
  251.                         $name_of_user_who_reviewed = $name_of_user_who_reviewed->first_name.' '.$name_of_user_who_reviewed->last_name;
  252.  
  253.                         array_push($orders_for_post, array('review'=>$order->order_rating->order_review->public_review,'reviewer'=>$name_of_user_who_reviewed)) ;//rated order
  254.  
  255.                         $ratings +=  $order->order_rating->rating;
  256.                         $order_count ++;
  257.                             $post_reviews += 1;
  258.                         }
  259.  
  260.  
  261.                     }
  262.                    
  263.                     if($order_count)
  264.                     $ratings = $ratings/$order_count ;
  265.                }
  266.                $post_rating += $ratings;
  267.             }
  268.         }
  269.      
  270.        return (object)['rating'=>$post_rating,'count'=>$post_reviews,'orders_for_post'=>$orders_for_post];
  271.     }
  272.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement