Current File : /home/tradevaly/text.tradevaly.com.bd/app/helpers.php
<?php


if (! function_exists('site_setting')) {
    /**
     * Throw an HttpException with the given data.
     *
     * @param  \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Support\Responsable|int  $code
     * @param  string  $message
     * @param  array  $headers
     * @return never
     *
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
     */
    function site_setting($key)
    {
       $setting = App\Models\Setting::where('name', $key)->first();
       if($setting != null){
           return $setting->value;
       } else {
           return '';
       }
    }
}

function translate($key)
{
    return $key;
    $local = 'en';
    App::setLocale($local);

    $lang_array = include(base_path('resources/lang/' . $local . '/pages.php'));
    $processed_key = ucfirst(str_replace('_', ' ', remove_invalid_charcaters($key)));

    if (!array_key_exists($key, $lang_array)) {
        $lang_array[$key] = $processed_key;
        $str = "<?php return " . var_export($lang_array, true) . ";";
        file_put_contents(base_path('resources/lang/' . $local . '/pages.php'), $str);
        $result = $processed_key;
    } else {
        $result = __('pages.' . $key);
    }
    return $result;
}

function menuActive($routeName, $type = null)
{
    if ($type == 3) {
        $class = 'side-menu--open';
    } elseif ($type == 2) {
        $class = 'sidebar-submenu__open';
    } else {
        $class = 'active';
    }
    if (is_array($routeName)) {
        foreach ($routeName as $key => $value) {
            if (request()->routeIs($value)) {
                return $class;
            }
        }
    } elseif (request()->routeIs($routeName)) {
        return $class;
    }
}

function ip_country($ip){
    $newip = sprintf("%u",ip2long($ip));
     $country = \App\Models\Ip::select('country_code', 'country_name')->where(function ($query) use ($newip) {
 $query->where('begin_ip_num', '<=', $newip);
 $query->where('end_ip_num', '>=', $newip);
})->first();


$contra = \App\Models\Ip::select('country_code', 'country_name')->where('country_code', '=', 'BD')->first();

if($country != null){
   return $country;
} else {
return $contra;
}
 }


function impressionCount($id)
{
    $item = \App\Models\Advertise::where('id',$id)->first();
    $item->impression +=1;
    $item->save();
}
function logo(){
    return App\Models\Logo::find(1)->image;
}
function settings(){
    return App\Models\Logo::find(1);
}
function child_product_count($id){
    return App\Models\Product::where('status', 1)->where('child_id', $id)->count();
}
function product_count_by_category($id){
    return App\Models\Product::where('status', 1)->where('category_id', $id)->count();
}
function package_id($id){
    return App\Models\User::find($id)->package_id;
}

function page_banner($id){
    $page_banner =  App\Models\Banner::find($id);
    return $page_banner->image;
}
function role_name($id){
    $role_name = 'Not Found';
    $role =  App\Models\Role::findOrFail($id);
    if($role){
        $role_name = $role->name;
    }
    return $role_name;
}
function permissions(){
    $permissions =  App\Models\Permission::all();
    return $permissions;
}
function sub_menus($id){
    $permissions =  App\Models\SubMenu::where('main_menu_id',$id)->get();
    return $permissions;
}

function after_discount($amunt,$discount){
   return $amunt - ($amunt/100*$discount);
}
function check_permision($role,$permission){
    $check = false;
    $check =  App\Models\RoleHasPermission::where('role_id',$role)->where('permission_id',$permission)->exists();

    return $check;
}