<?php





namespace App\Models;





use Illuminate\Database\Eloquent\Model;


use App\Models\nn_info_lang;


use DB;


use LaravelLocalization;





class nn_info extends Model


{


    protected $table = 'nn_info';





    protected $fillable = [

        'slug',
        'sDate',
        'collection_id',		

    ];





    //_-_ CMS SIDE


    /*


    	Get function


    */


    public static function getInfo($id)


    {


    	$info = DB::table('nn_info')


                            ->where(['nn_info_lang.lang' => getCurrentLocale(), 'collection_id' => $id])


                            ->join('nn_info_lang', 'nn_info.id', '=', 'nn_info_lang.info_id')


                            ->select('nn_info.id', 'nn_info.show', 'nn_info_lang.name', 'nn_info_lang.description', 'nn_info_lang.imgurl', 'nn_info.position')


                            ->orderBy('nn_info.created_at','desc')


                            ->get();


        return $info;


    }
    public static function getCatalogForEdit()


    {


    	$info = DB::table('nn_info')


                            ->where(['nn_info_lang.lang' => getCurrentLocale(), 'collection_id' => 5])


                            ->join('nn_info_lang', 'nn_info.id', '=', 'nn_info_lang.info_id')


                            ->select('nn_info.id', 'nn_info.sDate', 'nn_info_lang.name', 'nn_info_lang.description', 'nn_info_lang.publications','nn_info_lang.teamposition','nn_info_lang.moreinfo','nn_info_lang.imgurl','nn_info_lang.ogimgurl','nn_info_lang.email','nn_info_lang.research','nn_info_lang.education')


                            ->orderBy('nn_info.created_at','desc')


                            ->get();





        return $info;


    }






	/*


    	Create function


    */


    public static function createInfo($input)


    {


        $info = self::create($input);





    	$insertedId = $info->id;


        $input['info_id'] = $insertedId;





        foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties){





            $input['lang'] = $localeCode;





            nn_info_lang::create($input);


            


        }





        return $info;


    }





    /*


        Edit function


    */


    public static function editInfo($id)


    {


       return nn_info_lang::where('info_id', '=', $id)->get();


    }


  public static function editInfoDates($id)


    {


       return nn_info::where('id', '=', $id)->get();


    }


    /*


        Update function


    */


    public static function updateInfo($input)
    {
       
		$infoid = nn_info_lang::where('id', '=', $input['id'])->firstOrFail();
		

        $infoLang = nn_info_lang::where('id', '=', $input['id'])


                                    ->where('lang', '=', $input['lang'])


                                    ->firstOrFail();

        
        $check = $infoLang->update($input);

        return $check;
    }





    //_-_ SITE SIDE


    /*


        get info items


    */


    public static function getInfoItemsForSite($id, $limit = 0, $exceptId = 0)


    {


        $query = DB::table('nn_menu_item')


                            ->where(['nn_menu_item.id' => $id])


                            ->join('nn_info', 'nn_menu_item.collection_id', '=', 'nn_info.collection_id')


                            ->join('nn_info_lang', 'nn_info.id', '=', 'nn_info_lang.info_id')


                            ->where(['nn_info_lang.lang' => getCurrentLocale()])


                            ->select('nn_info_lang.name', 'nn_info.slug', 'nn_info_lang.imgurl', 'nn_info_lang.description')


                            ->groupBy('nn_info_lang.info_id')


                            ->orderBy('nn_info.created_at', 'desc');





        if($limit) {


            $query->limit($limit);


            if($exceptId) {


                $query->where('nn_info.id', '!=', $exceptId);


            }


            $infoItems =  $query->get();


        } else {


            $infoItems =  $query->paginate(12);


        }





        return $infoItems;


    }





    /*


        get info item


    */


    public static function getInfoItem($slug)


    {


        $infoItem = DB::table('nn_info')


                            ->where(['nn_info.slug' => $slug])


                            ->join('nn_info_lang', 'nn_info_lang.info_id', '=', 'nn_info.id')


                            ->where(['nn_info_lang.lang' => getCurrentLocale()])


                            ->select('nn_info.id', 'nn_info.AstartDate', 'nn_info.AendDate', 'nn_info_lang.name', 'nn_info_lang.imgurl', 'nn_info.collection_id')


                            ->orderBy('nn_info.created_at', 'desc')


                            ->get();





        return $infoItem;


    }
	
	
	    public static function getevents()


    {


        $infoItem = DB::table('nn_info')
			
                            ->join('nn_info_lang', 'nn_info_lang.info_id', '=', 'nn_info.id')
                            ->where(['nn_info_lang.lang' => "ka"])
                            ->select('nn_info.id', 'nn_info.slug', 'nn_info.AstartDate', 'nn_info.AendDate', 'nn_info.AstartTime', 'nn_info.AendTime', 'nn_info_lang.name', 'nn_info_lang.imgurl', 'nn_info_lang.description as url')

                            ->orderBy('nn_info.created_at', 'desc')


                            ->get();





        return $infoItem;


    }
	
    public static function geteventssingle($slug)


    {


        $infoItem = DB::table('nn_info')
			->where(['nn_info.slug' => $slug])
                            ->join('nn_info_lang', 'nn_info_lang.info_id', '=', 'nn_info.id')
                            ->where(['nn_info_lang.lang' => "ka"])
                            ->select('nn_info.id', 'nn_info.slug', 'nn_info.AstartDate', 'nn_info.AendDate', 'nn_info.AstartTime', 'nn_info.AendTime', 'nn_info_lang.name', 'nn_info_lang.imgurl', 'nn_info_lang.description as url')

                            ->orderBy('nn_info.created_at', 'desc')


                            ->get();





        return $infoItem;


    }


}


