| Server IP : 13.235.167.51 / Your IP : 216.73.217.116 Web Server : Apache System : Linux machinox-server 6.17.0-1013-aws #13~24.04.1-Ubuntu SMP Fri Apr 24 21:36:58 UTC 2026 aarch64 User : root ( 0) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/app.machinox.in/app/Models/ |
Upload File : |
<?php
namespace App\Models;
use App\Helpers\NotificationHelper;
use App\Helpers\UtilityHelper;
use App\Traits\UploadTrait;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use stdClass;
use Illuminate\Support\Str;
class Notification extends Model
{
use HasFactory, UploadTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'description',
'action_id',
'image',
'action',
'branch_id',
'created_by',
'created_at'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = ['updated_at'];
public function added_by()
{
return $this->hasOne(AdminUser::class, 'id', 'created_by')->select("id", "uuid", "name", "mobile_number");;
}
public function getImageAttribute($value)
{
$folder = "notifications";
return $this->getURL($folder, $value);
}
public static function GetInfoById($id)
{
return self::where('id', $id)->first();
}
public function notified_users()
{
return $this->belongsToMany(User::class, 'notified_users', 'notification_id', 'user_id');
}
public const PerPageRecord = 10;
public static function GetList($request)
{
$offset = getValue($request->input('offset'), 0);
$sort_by = getValue($request->input('sort_by'), 'notifications.created_at');
$sort_order = getValue($request->input('sort_order'), 'DESC');
$keyword = getValue($request->input('keyword'));
$query = self::query()->whereNotNull("notifications.created_by")
->with([
'added_by'
])
->when($keyword, function ($q) use ($keyword) {
$q->Where('title', 'LIKE', '%' . $keyword . '%');
$q->orWhere('description', 'LIKE', '%' . $keyword . '%');
});
$countQuery = (clone $query);
$total = $countQuery->count();
$perPage = self::PerPageRecord;
$currentPage = max(1, (int)$offset + 1); // since your offset is page-indexed (0-based)
$totalPages = $perPage > 0 ? (int) ceil($total / $perPage) : 0;
$query->orderBy($sort_by, $sort_order)
->offset($offset * $perPage)
->limit($perPage);
$list = $query->get();
$nextOffset = ($list->count() === $perPage && $currentPage < $totalPages) ? ($offset + 1) : -1;
$data = [
'count' => $total,
'list' => $list,
'current_page' => $currentPage,
'next_offset' => $nextOffset,
'per_page' => $perPage,
'total_pages' => $totalPages,
];
return $data;
}
public static function SetNotification($userId, $title, $message, $action = null, $actionId = null, $hasSend = '0', $messageId = null)
{
$notification = new self;
$notification->title = $title;
$notification->description = $message;
$notification->action = $action;
$notification->action_id = $actionId;
$notification->message_id = $messageId;
$notification->save();
$data = [
"notification_id" => $notification->id,
'user_id' => $userId,
'has_send' => 0,
'created_at' => date("Y-m-d H:i:s"),
'updated_at' => date("Y-m-d H:i:s")
];
NotifiedUser::insert($data);
if ($hasSend == "1") {
$obj = [
"title" => $title,
"body" => $message,
"user_id" => $userId,
"action_id" => $actionId,
"action" => $action,
'message_id' => $notification->message_id,
];
NotificationHelper::SendPush($obj);
$notifiedUser = NotifiedUser::with('notification')
->where("notification_id", $notification->id)
->first();
if ($notifiedUser) {
$notifiedUser->has_send = "1";
$notifiedUser->save();
}
}
}
public static function SetInfo($request, $id = 0)
{
$sentTo = trim($request->input('sent_to'));
$userIds = [];
$notification = new Notification();
$notification->title = getValue($request->input('title'));
$notification->description = getValue($request->input('description'));
$notification->action = null;
$notification->created_by = ADMIN_USER_ID();
$notification->save();
$res = new stdClass;
$res->id = $notification->id;
$res->message = trans("messages.NOTIFICATION_SENT_SUCCESSFULLY");
if ($sentTo == 'SPECIFIC') {
$userIds = explode(',', $request->input('user_id'));
}
if ($sentTo == 'ALL') {
$users = User::select('id')
->get();
if (count($users) > 0) {
foreach ($users as $uid) {
$userIds[] = $uid->id;
}
} else {
throw new Exception(trans('messages.USER_INFO_NOT_FOUND'));
}
}
if (count($userIds) > 0) {
$counter = 0;
$data = [];
foreach ($userIds as $key => $user_id) {
$temp = [
"notification_id" => $notification->id,
'has_send' => '0',
'created_by' => ADMIN_USER_ID(),
'created_at' => date("Y-m-d H:i:s"),
'updated_at' => date("Y-m-d H:i:s")
];
$temp['user_id'] = $user_id;
$data[] = $temp;
++$counter;
if ($counter > 99 || $key == ($counter - 1)) {
$counter = 0;
NotifiedUser::insert($data);
$data = [];
}
}
if (count($data) > 0) {
NotifiedUser::insert($data);
}
}
return $res;
}
public static function DeleteInfo($request, $id)
{
$check = self::query()->where("id", $id)->first();
if ($check) {
NotifiedUser::where('notification_id', $id)->delete();
$check->delete();
return trans('messages.NOTIFICATION_DELETED_SUCCESSFULLY');
} else {
throw new \Exception(trans('messages.RECORD_NOT_FOUND'));
}
}
}