Files
2026-09-01 10:46:29 -03:00

110 lines
2.6 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 Note
*
* Model for notes.
*
* @package Modules\Notes\Models
*/
class Note extends Model
{
/**
* @var string The table associated with the model.
*/
protected $table = 'notes';
/**
* @var string The primary key associated with the table.
*/
protected $primaryKey = 'notes_id';
/**
* @var array The attributes that are mass assignable.
*/
protected $fillable = [
'notes_idbus',
'notes_iduser',
'notes_title',
'notes_content',
'notes_subject_id',
'notes_is_pinned',
'notes_color',
'notes_image',
'notes_file'
];
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
static::creating(function ($note) {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$note->notes_idbus = $_SESSION['user_idbus'] ?? null;
if (empty($note->notes_idbus)) {
$note->notes_idbus = 1;
}
$note->notes_iduser = $_SESSION['user_id'] ?? null;
});
}
/**
* Scope a query to only include notes 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('notes_idbus', $busId)
->where('notes_iduser', $userId);
}
/**
* Get the subject that owns the note.
*/
public function subject()
{
return $this->belongsTo(Subject::class, 'notes_subject_id', 'subjects_id');
}
}