first change
This commit is contained in:
163
app/Helpers/Helper.php
Normal file
163
app/Helpers/Helper.php
Normal file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\CCMS\Models\Setting;
|
||||
|
||||
if (!function_exists('setting')) {
|
||||
function setting($key = null)
|
||||
{
|
||||
$setting = Cache::has('setting') ? Cache::get('setting') : Cache::rememberForever('setting', function () {
|
||||
return Setting::get()->mapWithKeys(function (Setting $setting) {
|
||||
return [$setting->key => $setting->value];
|
||||
})->toArray();
|
||||
});
|
||||
|
||||
return array_key_exists($key, $setting) ? $setting[$key] : null;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('uploadImage')) {
|
||||
function uploadImage($file, $path = 'uploads')
|
||||
{
|
||||
$fileName = uniqid() . '.' . $file->getClientOriginalExtension();
|
||||
$filePath = Storage::disk('public')->putFileAs($path, $file, $fileName);
|
||||
return $filePath;
|
||||
}
|
||||
}
|
||||
|
||||
function getPageTemplateOptions()
|
||||
{
|
||||
$client = config('app.client');
|
||||
$pageTemplateOptions = collect(File::files(resource_path("views/client/{$client}/pages")))
|
||||
->mapWithKeys(function ($file) {
|
||||
$template = Str::replace('.blade.php', '', $file->getFilename());
|
||||
return [
|
||||
$template => $template,
|
||||
];
|
||||
});
|
||||
|
||||
return $pageTemplateOptions->all();
|
||||
}
|
||||
|
||||
if (!function_exists('getFormatted')) {
|
||||
function getFormatted($dateTime = null, $date = null, $time = null, $format = null)
|
||||
{
|
||||
$data = null;
|
||||
|
||||
switch (true) {
|
||||
case !is_null($dateTime):
|
||||
$data = $dateTime;
|
||||
$format ??= 'd M, Y h:i A';
|
||||
break;
|
||||
|
||||
case !is_null($date) && !is_null($time):
|
||||
|
||||
$data = "{$date} {$time}";
|
||||
$format ??= 'd M, Y h:i A';
|
||||
break;
|
||||
|
||||
case !is_null($date):
|
||||
|
||||
$data = $date;
|
||||
$format ??= 'd M, Y';
|
||||
break;
|
||||
|
||||
case !is_null($time):
|
||||
$data = $time;
|
||||
$format ??= 'h:i A';
|
||||
break;
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$formatted = Carbon::parse($data)->format($format);
|
||||
return $formatted;
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function getThemeColor($lightenFactor = 0.3)
|
||||
{
|
||||
$themeColor = setting('color') ?? '#be2400';
|
||||
|
||||
if (!preg_match('/^#[a-fA-F0-9]{6}$/', $themeColor)) {
|
||||
return $themeColor;
|
||||
}
|
||||
|
||||
$lighterColor = lightenColor($themeColor, $lightenFactor);
|
||||
|
||||
return [
|
||||
'original' => $themeColor,
|
||||
'lighter' => $lighterColor,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Lighten a hexadecimal color.
|
||||
*
|
||||
* @param string $hexColor
|
||||
* @param float $factor
|
||||
* @return string
|
||||
*/
|
||||
|
||||
function lightenColor(string $hexColor, float $factor = 0.2): string
|
||||
{
|
||||
$hexColor = ltrim($hexColor, '#');
|
||||
|
||||
// Convert hex to RGB
|
||||
$r = hexdec(substr($hexColor, 0, 2));
|
||||
$g = hexdec(substr($hexColor, 2, 2));
|
||||
$b = hexdec(substr($hexColor, 4, 2));
|
||||
|
||||
// Apply the lightening factor
|
||||
$r = min(255, (int) ($r + (255 - $r) * $factor));
|
||||
$g = min(255, (int) ($g + (255 - $g) * $factor));
|
||||
$b = min(255, (int) ($b + (255 - $b) * $factor));
|
||||
|
||||
// Convert back to hex
|
||||
return sprintf('#%02x%02x%02x', $r, $g, $b);
|
||||
}
|
||||
|
||||
function setEnvIfNotExists($key, $value)
|
||||
{
|
||||
$envFile = app()->environmentFilePath(); // Path to the .env file
|
||||
$str = file_get_contents($envFile);
|
||||
|
||||
// Check if the key exists in the .env file
|
||||
$keyExists = preg_match("/^{$key}=.*/m", $str);
|
||||
|
||||
if (!$keyExists) {
|
||||
// If the key doesn't exist, append it to the .env file
|
||||
$str .= "\n{$key}={$value}";
|
||||
file_put_contents($envFile, $str);
|
||||
}
|
||||
}
|
||||
|
||||
function isEmptyArray($array = [])
|
||||
{
|
||||
return is_array($array) && array_filter($array) === [];
|
||||
}
|
||||
|
||||
if (!function_exists('sendNotification')) {
|
||||
function sendNotification($user, $notification = [])
|
||||
{
|
||||
Notification::send($user, new App\Notifications\SendNotification($notification));
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('uploadImage')) {
|
||||
function uploadImage($file, $path = 'uploads')
|
||||
{
|
||||
$fileName = time() . '_' . $file->getClientOriginalName();
|
||||
$filePath = Storage::disk('public')->putFileAs($path, $file, $fileName);
|
||||
return $filePath;
|
||||
}
|
||||
}
|
||||
}
|
268
app/Helpers/QueryHelper.php
Normal file
268
app/Helpers/QueryHelper.php
Normal file
@@ -0,0 +1,268 @@
|
||||
<?php
|
||||
|
||||
use Modules\CCMS\Models\Blog;
|
||||
use Modules\CCMS\Models\Category;
|
||||
use Modules\CCMS\Models\Counter;
|
||||
use Modules\CCMS\Models\Country;
|
||||
use Modules\CCMS\Models\Faq;
|
||||
use Modules\CCMS\Models\FaqCategory;
|
||||
use Modules\CCMS\Models\Gallery;
|
||||
use Modules\CCMS\Models\GalleryCategory;
|
||||
use Modules\CCMS\Models\Institution;
|
||||
use Modules\CCMS\Models\Page;
|
||||
use Modules\CCMS\Models\Partner;
|
||||
use Modules\CCMS\Models\Service;
|
||||
use Modules\CCMS\Models\Slider;
|
||||
use Modules\CCMS\Models\Team;
|
||||
use Modules\CCMS\Models\Test;
|
||||
use Modules\CCMS\Models\Testimonial;
|
||||
use Modules\Menu\Models\Menu;
|
||||
|
||||
function getSliders(?int $limit = null, ?string $order = 'desc')
|
||||
{
|
||||
return Slider::query()
|
||||
->where('status', 1)
|
||||
->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
})
|
||||
->get();
|
||||
}
|
||||
|
||||
function getBlogCategories(?int $limit = null, ?string $order = 'desc')
|
||||
{
|
||||
return Category::query()
|
||||
->where('status', 1)
|
||||
->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
})
|
||||
->get();
|
||||
}
|
||||
|
||||
function getFAQs(?int $limit = null, ?string $order = 'desc')
|
||||
{
|
||||
return Faq::query()
|
||||
->where('status', 1)
|
||||
->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
})
|
||||
->get();
|
||||
}
|
||||
|
||||
function getTeams(?int $limit = null, ?string $order = 'desc')
|
||||
{
|
||||
return Team::query()
|
||||
->where('status', 1)
|
||||
->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
})
|
||||
->get();
|
||||
}
|
||||
|
||||
function getTestimonials(?int $limit = null, ?string $order = 'desc')
|
||||
{
|
||||
return Testimonial::query()
|
||||
->where('status', 1)
|
||||
->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
})
|
||||
->get();
|
||||
}
|
||||
|
||||
|
||||
function getPartners(?int $limit = null, ?string $order = 'desc', bool $paginate = false)
|
||||
{
|
||||
$query = Partner::query()
|
||||
->where('status', 1)
|
||||
->orderBy('order', $order);
|
||||
|
||||
if ($limit && $paginate) {
|
||||
return $query->paginate($limit);
|
||||
} elseif ($limit && !$paginate) {
|
||||
return $query->limit($limit)->get();
|
||||
} else {
|
||||
return $query->get();
|
||||
}
|
||||
}
|
||||
|
||||
function getGalleries(?int $limit = null, ?string $order = 'desc', bool $paginate = false)
|
||||
{
|
||||
$query = Gallery::query()
|
||||
->where('status', 1)
|
||||
->orderBy('order', $order);
|
||||
|
||||
if ($limit && $paginate) {
|
||||
return $query->paginate($limit);
|
||||
} elseif ($limit && !$paginate) {
|
||||
return $query->limit($limit)->get();
|
||||
} else {
|
||||
return $query->get();
|
||||
}
|
||||
}
|
||||
|
||||
function getBlogs(?int $limit = null, ?string $order = 'desc', bool $paginate = false)
|
||||
{
|
||||
$query = Blog::query()
|
||||
->where('status', 1)
|
||||
->orderBy('order', $order);
|
||||
|
||||
if ($limit && $paginate) {
|
||||
return $query->paginate($limit);
|
||||
} elseif ($limit && !$paginate) {
|
||||
return $query->limit($limit)->get();
|
||||
} else {
|
||||
return $query->get();
|
||||
}
|
||||
}
|
||||
|
||||
function getBlogBySlug(?string $slug)
|
||||
{
|
||||
return Blog::query()
|
||||
->where('status', 1)
|
||||
->where('slug', $slug)
|
||||
->first();
|
||||
}
|
||||
|
||||
function getCounters($limit = null, $order = 'desc')
|
||||
{
|
||||
return Counter::query()
|
||||
->where('status', 1)
|
||||
->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
})
|
||||
->get();
|
||||
}
|
||||
|
||||
function getServices($limit = null, $order = 'desc')
|
||||
{
|
||||
return Service::query()
|
||||
->where('status', 1)
|
||||
->where('parent_id', null)
|
||||
->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
})
|
||||
->get();
|
||||
}
|
||||
|
||||
function getInstitutions($limit = null, $order = 'desc')
|
||||
{
|
||||
return Institution::query()
|
||||
->where('status', 1)
|
||||
->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
})
|
||||
->get();
|
||||
}
|
||||
|
||||
function getClasses($limit = null, $order = 'desc')
|
||||
{
|
||||
return Test::query()
|
||||
->where('status', 1)
|
||||
->where('parent_id', null)
|
||||
->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
})
|
||||
->get();
|
||||
}
|
||||
|
||||
function getDestinations($limit = null, $order = 'desc')
|
||||
{
|
||||
return Country::query()
|
||||
->where('status', 1)
|
||||
->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
})
|
||||
->get();
|
||||
}
|
||||
|
||||
function getGalleriesByCategory(?int $limit = null, ?string $order = 'desc', ?string $category = null)
|
||||
{
|
||||
return GalleryCategory::query()
|
||||
->where('status', 1)
|
||||
->where('slug', $category)
|
||||
->with('galleries', function ($query) use ($limit, $order) {
|
||||
$query->where('status', 1)
|
||||
->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
});
|
||||
})
|
||||
->first();
|
||||
}
|
||||
|
||||
function getInstitutionsByCountry(?int $limit = null, ?string $order = 'desc', ?string $country = null)
|
||||
{
|
||||
return Country::query()
|
||||
->where('status', 1)
|
||||
->where('slug', $country)
|
||||
->with('institutions', function ($query) use ($limit, $order) {
|
||||
$query->where('status', 1)
|
||||
->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
});
|
||||
})
|
||||
->first();
|
||||
}
|
||||
|
||||
function getFAQsByCategory(?int $limit = null, ?string $order = 'desc', ?string $category = null)
|
||||
{
|
||||
return FaqCategory::query()
|
||||
->where('status', 1)
|
||||
->where('slug', $category)
|
||||
->with('faqs', function ($query) use ($limit, $order) {
|
||||
$query->where('status', 1)
|
||||
->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
});
|
||||
})
|
||||
->first();
|
||||
}
|
||||
|
||||
function getPageWithChildrenBySlug(?string $parent, ?string $slug, ?int $limit = null, ?string $order = 'desc')
|
||||
{
|
||||
$page = Page::where([
|
||||
'status' => 1,
|
||||
'type' => 'page',
|
||||
'slug' => $slug,
|
||||
])
|
||||
->when($parent, function ($query) use ($parent) {
|
||||
$query->where('parent', $parent);
|
||||
})
|
||||
->with(['children' => function ($query) use ($limit, $order) {
|
||||
$query->orderBy('order', $order)
|
||||
->when($limit, function ($query) use ($limit) {
|
||||
$query->limit($limit);
|
||||
});
|
||||
}])
|
||||
->first();
|
||||
|
||||
return $page;
|
||||
}
|
||||
|
||||
function getAllHeaderMenusWithChildren()
|
||||
{
|
||||
$headerMenuItems = Menu::where(['menu_location_id' => 1, 'parent_id' => null, 'status' => 1])->with('children', function ($query) {
|
||||
$query->where('status', 1)
|
||||
->orderBy('order', 'asc');
|
||||
})->orderBy('order', 'asc')->get();
|
||||
return $headerMenuItems;
|
||||
}
|
||||
|
||||
function getAllFooterMenusWithChildren()
|
||||
{
|
||||
$footerMenuItems = Menu::where(['menu_location_id' => 2, 'parent_id' => null, 'status' => 1])->with('children', function ($query) {
|
||||
$query->orderBy('order', 'asc');
|
||||
})->orderBy('order', 'asc')->get();
|
||||
return $footerMenuItems;
|
||||
}
|
Reference in New Issue
Block a user