Files
dockerfluxodecaixapro/modules/notes/classes/Models/Subject.php
T
2026-09-01 10:46:29 -03:00

110 lines
2.7 KiB
PHP
Executable File

<?php
/**
* SysCMS - Content Management System
*
* Copyright (c) 2026 Marco Costa
* All rights reserved.
*
* Author: Marco Costa
* Email: contato@syspanel.com.br
* Project: SysCMS
*
* This software is licensed, not sold.
* Permission is granted to use this software
* subject to the terms of the SysCMS Commercial License.
*
* Redistribution, sublicensing, resale, or
* disclosure of this software, in whole or in part,
* is strictly prohibited without prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
*/
namespace Modules\Notes\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Class Subject
*
* Model for note subjects/categories.
*
* @package Modules\Notes\Models
*/
class Subject extends Model
{
/**
* @var string The table associated with the model.
*/
protected $table = 'notes_subjects';
/**
* @var string The primary key associated with the table.
*/
protected $primaryKey = 'subjects_id';
/**
* @var array The attributes that are mass assignable.
*/
protected $fillable = [
'subjects_idbus',
'subjects_iduser',
'subjects_name',
'subjects_color'
];
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
static::creating(function ($subject) {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if (empty($subject->subjects_idbus)) {
$subject->subjects_idbus = $_SESSION['user_idbus'] ?? null;
}
if (empty($subject->subjects_idbus)) {
$subject->subjects_idbus = 1;
}
if (empty($subject->subjects_iduser)) {
$subject->subjects_iduser = $_SESSION['user_id'] ?? null;
}
});
}
/**
* Scope a query to only include subjects for the current business and user.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCurrentUser($query)
{
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$busId = $_SESSION['user_idbus'] ?? null;
if (empty($busId)) {
$busId = 1;
}
$userId = $_SESSION['user_id'] ?? null;
return $query->where('subjects_idbus', $busId)
->where('subjects_iduser', $userId);
}
/**
* Get the notes for the subject.
*/
public function notes()
{
return $this->hasMany(Note::class, 'notes_subject_id', 'subjects_id');
}
}