-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.php
More file actions
106 lines (95 loc) · 3.1 KB
/
functions.php
File metadata and controls
106 lines (95 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
use App\Brand;
use App\Category;
use App\Image;
use App\Page;
use App\Product;
use App\Setting;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Schema;
if (! function_exists('categories')) {
function categories() {
return Category::with('image')
->inRandomOrder()
->get()
->map(function ($category) {
if (!$image = $category->image) {
$image = Image::whereHas('products.categories', function ($query) use ($category) {
$query->where('category_id', $category->id);
})->inRandomOrder()->first();
}
$category->image_src = asset($image->path ?? 'https://placehold.co/600x600?text=No+Product');
return $category;
});
}
}
if (! function_exists('brands')) {
function brands() {
return Brand::with('image')
->inRandomOrder()
->get()
->map(function ($brand) {
if (!$image = $brand->image) {
$image = Image::whereHas('products.brand', function ($query) use ($brand) {
$query->where('brand_id', $brand->id);
})->inRandomOrder()->first();
}
$brand->image_src = asset($image->path ?? 'https://placehold.co/600x600?text=No+Product');
return $brand;
});
}
}
if (! function_exists('pageRoutes')) {
function pageRoutes() {
try {
Schema::hasTable((new Page)->getTable())
&& Route::get('{page:slug}', 'PageController')
->where('page', 'test-page|'.implode(
'|', Page::get('slug')
->map->slug
->toArray()
))
->middleware('shortkode')
->name('page');
} catch (\Throwable $th) {
//throw $th;
}
}
}
if (! function_exists('setting')) {
function setting($name) {
return cache()->rememberForever('settings:'.$name, function () use ($name) {
return optional(Setting::whereName($name)->first())->value;
});
}
}
if (! function_exists('theMoney')) {
function theMoney($amount, $decimals = null, $currency = "TK") {
return $currency." <span>".number_format($amount, $decimals)."</span>";
}
}
function bytesToHuman($bytes) {
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
for ($i = 0; $bytes > 1024; $i++) {
$bytes /= 1024;
}
return round($bytes, 2) . ' ' . $units[$i];
}
function hasErr($errors, $params) {
foreach (explode('|', $params) as $param)
if ($errors->has($param))
return true;
return false;
}
function genSKU($repeat = 5, $length = null) {
$sku = null;
$length = $length ?: mt_rand(6, 10);
$charset = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
$multiplier = ceil($length / strlen($charset));
// Generate SKU
if (--$repeat) {
$sku = substr(str_shuffle(str_repeat($charset, $multiplier)), 1, $length);
Product::where('sku', $sku)->count() && genSKU($repeat);
}
return $sku;
}