Files
Global-Jain/app/helpers.php
2025-11-05 10:37:10 +05:30

477 lines
12 KiB
PHP

<?php
use Carbon\Carbon;
use App\Models\User;
use App\Models\UserMeta;
use App\Constant\Constant;
use App\Models\UserSocialLogin;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Models\UserNotificationSetting;
if (!function_exists('includeRouteFiles')) {
/**
* @param $folder
*
* Loops through a folder and requires all PHP files
* Searches sub-directories as well.
*
*/
function includeRouteFiles($folder)
{
$directory = $folder;
$handle = opendir($directory);
$directory_list = [$directory];
while (false !== ($filename = readdir($handle))) {
if ($filename != '.' && $filename != '..' && is_dir($directory . $filename)) {
array_push($directory_list, $directory . $filename . '/');
}
}
foreach ($directory_list as $directory) {
foreach (glob($directory . '*.php') as $filename) {
require $filename;
}
}
}
}
if (!function_exists('loggedInUser')) {
/**
* @return mixed
*
* Getting logged in user
*/
function loggedInUser()
{
return \Auth::user();
}
}
if (!function_exists('getUserMetaValue')) {
/**
* @param null $userId
* @param null $key
* @return mixed
*
* Getting User meta values by meta key
*
* @author Jaynil Parekh
*/
function getUserMetaValue($userId = null, $key = null)
{
if (!empty($userId) && !empty($key)) {
return UserMeta::where('user_id', $userId)->where('meta_key', $key)->pluck('meta_value')->first();
}
}
}
if (!function_exists('messagesCounter')) {
function messagesCounter($userID = '') {
try {
$user = \Auth::guard('sanctum')->user();
if ($userID == '') {
$userID = $user->id;
}
$result = DB::table('message_threads as mt')
->leftJoin('messages as m', 'mt.id', '=', 'm.message_thread_id')
->where(function ($query) use ($userID) {
$query->where('mt.receiver_id', '=', $userID)->orWhere('mt.sender_id', '=', $userID);
})
->where('m.user_id', '!=', $userID)
->where('m.status', '=', "0")->get()->count();
return $result;
} catch (\Exception $ex) {
Log::error($ex->getMessage());
return 0;
}
}
}
if (!function_exists('unreadMessageCounter')) {
function unreadMessageCounter($id) {
try {
$user = User::where('id', $id)->first();
$unreadMessageCount = $user->unread_message_count;
return $unreadMessageCount;
}catch (\Exception $ex) {
Log::error($ex->getMessage());
return 0;
}
}
}
if (!function_exists('unreadNotificationCounter')) {
function unreadNotificationCounter($id) {
try {
$user = User::find($id);
$user->unread_notification_count = $user->unread_notification_count + 1;
$user->save();
return $user->unread_notification_count;
}catch (\Exception $ex) {
Log::error($ex->getMessage());
return 0;
}
}
}
if (!function_exists('unreadSantMenuCounter')) {
function unreadSantMenuCounter($id, $flag) {
try {
$user = User::find($id);
$user->sant_menu = $flag;
$user->save();
return $user->sant_menu;
}catch (\Exception $ex) {
Log::error($ex->getMessage());
return 0;
}
}
}
if (!function_exists('unreadSanghMenuCounter')) {
function unreadSanghMenuCounter($id, $flag) {
try {
$user = User::find($id);
$user->sangh_menu = $flag;
$user->save();
return $user->sangh_menu;
}catch (\Exception $ex) {
Log::error($ex->getMessage());
return 0;
}
}
}
if (!function_exists('unreadShravakMenuCounter')) {
function unreadShravakMenuCounter($id, $flag) {
try {
$user = User::find($id);
$user->shravak_menu = $flag;
$user->save();
return $user->shravak_menu;
}catch (\Exception $ex) {
Log::error($ex->getMessage());
return 0;
}
}
}
if (!function_exists('unreadSanghInviteCounter')) {
function unreadSanghInviteCounter($id, $flag) {
try {
$user = User::find($id);
$user->pending_sangh_invitation = $flag;
$user->save();
return $user->pending_sangh_invitation;
}catch (\Exception $ex) {
Log::error($ex->getMessage());
return 0;
}
}
}
if (!function_exists('unreadSantActionCounter')) {
function unreadSantActionCounter($id, $flag) {
try {
$user = User::find($id);
$user->pending_sant_action = $flag;
$user->save();
return $user->pending_sant_action;
}catch (\Exception $ex) {
Log::error($ex->getMessage());
return 0;
}
}
}
if (!function_exists('unreadFriendRequestCounter')) {
function unreadFriendRequestCounter($id, $flag) {
try {
$user = User::find($id);
$user->pending_friend_request = $flag;
$user->save();
return $user->pending_friend_request;
}catch (\Exception $ex) {
Log::error($ex->getMessage());
return 0;
}
}
}
if (!function_exists('addUserSingleMetaValue')) {
/**
* @param null $userId
* @param null $key
* @param null $value
* @return bool
*
* Store single user meta key and value
*
*/
function addUserSingleMetaValue($userId = null, $key = null, $value = null)
{
if (!empty($userId) && !empty($key) && !empty($value)) {
$data['user_id'] = $userId;
$data['meta_key'] = $key;
$data['meta_value'] = $value;
if (UserMeta::create($data)) {
return true;
}
return false;
} else {
return false;
}
}
}
if (!function_exists('addUserMultipleMetaValue')) {
/**
* @param null $data
* @return bool
*
*
* Store multiple user meta keys and values
*
*/
function addUserMultipleMetaValue($data = null)
{
if (!empty($data)) {
if (UserMeta::insert($data)) {
return true;
}
return false;
} else {
return false;
}
}
}
if (!function_exists('updateUserMetaValue')) {
/**
* @param null $userId
* @param null $key
* @param null $value
* @return bool
*
* Update user meta value
*
*/
function updateUserMetaValue($userId = null, $key = null, $value = null)
{
if (!empty($userId) && !empty($key) && !empty($value)) {
UserMeta::where('user_id', $userId)->where('meta_key', $key)->update(['meta_value' => $value]);
return true;
}
}
}
if (!function_exists('removeUserMetaValue')) {
/**
* @param null $userId
* @param null $key
* @param null $value
* @return bool
*
* remove user meta value
*
*/
function removeUserMetaValue($userId = null, $key = null, $value = null)
{
if (!empty($userId) && !empty($key)) {
UserMeta::where('user_id', $userId)->where('meta_key', $key)->delete();
return true;
}
}
}
if (!function_exists('generateConfirmationCode')) {
/**
* @param int $length
* @return false|string
*
* Generate confirmation code
*
*/
function generateConfirmationCode($length = 6)
{
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
}
}
if (!function_exists('generateOtp')) {
/**
* @param int $length
* @return false|string
*
* Generate OTP
*
*/
function generateOtp($length = 6)
{
$pool = '0123456789';
return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
}
}
if (!function_exists('generateUsername')) {
/**
* @param $string
* @return string
*
* Generate username
*
*/
function generateUsername($string)
{
$pattern = " ";
$firstPart = substr(strstr(strtolower($string), $pattern, true), 0, 2);
$secondPart = substr(strstr(strtolower($string), $pattern, false), 0, 3);
$nrRand = rand(0, 100);
$str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$str_result = substr(str_shuffle($str_result), 0, 3);
return trim($firstPart) . trim($secondPart) . trim($nrRand) . trim($str_result);
}
}
if (!function_exists('loggedUserDetail')) {
/**
* @param $string
* @return string
*/
function loggedUserDetail($user)
{
if($user){
$userData['id'] = $user->id;
$userData['name'] = $user->name;
$userData['email'] = $user->email;
$userData['country_code'] = $user->country_code;
$userData['mobile'] = $user->mobile;
$userData['email_preview'] = hideEmailAddress($user->email);
$userData['avatar'] = $user->avatar;
$userData['is_profile_verified'] = $user->is_profile_verified;
$userData['login_type'] = UserSocialLogin::where('user_id',$user->id)->first() ? 1 : 0;
return $userData;
}else{
return $userData = [];
}
}
}
if (!function_exists('getUserByID')) {
/**
* @return array
*/
function getUserByID($id)
{
try {
return User::find($id);
} catch (Exception $ex) {
Log::error($ex->getMessage());
return [];
}
}
}
if (!function_exists('hideEmailAddress')) {
/**
* @param $string
* @return string
* @author Shailesh Jakhaniya
* @since 2020-06-19
*
* Hide the email address
*
*/
function hideEmailAddress($email)
{
if(filter_var($email, FILTER_VALIDATE_EMAIL))
{
list($first, $last) = explode('@', $email);
$first = str_replace(substr($first, '1'), str_repeat('*', strlen($first)-1), $first);
$last = explode('.', $last);
$last_domain = str_replace(substr($last['0'], '1'), str_repeat('*', strlen($last['0'])-1), $last['0']);
$hideEmailAddress = $first.'@'.$last_domain.'.'.$last['1'];
return $hideEmailAddress;
}
}
}
if (!function_exists('userNotificationSetting')) {
function userNotificationSetting($user)
{
try {
$checkNotification = UserNotificationSetting::where('user_id',$user->id)->count();
if($checkNotification == 0){
if($user->type == 2){
$typeArr = brandNotificationType();
}else{
$typeArr = freelancerNotificationType();
}
foreach($typeArr as $slug => $type){
UserNotificationSetting::insert([
'user_id' => $user->id,
'notification_type' => $slug,
'allow' => 0,
'created_at' => Carbon::now()
]);
}
}
}catch(Exception $ex){
Log::error($ex->getMessage());
}
}
}