Skip to content

Instantly share code, notes, and snippets.

@TrywaR
Created December 14, 2023 16:27
Show Gist options
  • Save TrywaR/2f64d5a45faa753f39ad976aa7374bc9 to your computer and use it in GitHub Desktop.
Save TrywaR/2f64d5a45faa753f39ad976aa7374bc9 to your computer and use it in GitHub Desktop.
<?
/**
* Note
*/
class note extends model implements elem
{
use base_fields;
static $table = ''; # Таблица в bd
static $id = '';
static $content = '';
static $date = '';
static $task_id = '';
static $client_id = '';
static $project_id = '';
static $user_id = '';
static $date_create = '';
static $date_update = '';
static $category_id = '';
public $show_client = false;
public $show_project = false;
public $show_category = false;
public $access = true; # Только доступные
function get_note(array $arrNote = []): array # Вывод заметки
{
global $oTextConv;
if (!$arrNote['id']) {
if (isset($this->id)) $arrNote = (array)$this;
else $arrNote = $this->get();
}
if ($this->show_category && (int)$arrNote['category_id']) {
$oCategory = new category($arrNote['category_id']);
$arrNote['category_val'] = $oCategory->get_category();
$arrNote['category_show'] = 'true';
}
if ($this->show_client && (int)$arrNote['client_id']) {
$oClient = new client($arrNote['client_id']);
$arrNote['client_val'] = $oClient->get_client();
$arrNote['client_show'] = 'true';
}
if ($this->show_project && (int)$arrNote['project_id']) {
$arrNote['project_show'] = 'true';
$oProject = new project($arrNote['project_id']);
$arrNote['project_val'] = $oProject->get_project();
}
$arrNote['content_prev'] = '';
if ($arrNote['content'] != '') {
$arrNote['content_prev'] = '<div class="_wiky block_wiky">' . $oTextConv->get($arrNote['content']) . '</div>';
$arrNote['content_show'] = 'true';
}
return $arrNote;
}
function get_notes(): array # Вывод заметок
{
global $oLock;
$this->sortMulti = ' `date` DESC, `date_update` DESC ';
if ($this->access) {
$iUserId = $_SESSION['user']['id'];
if ($iUserId && !$oLock->check('NotesShowAll')) {
$arrFields = db::query_all("SHOW COLUMNS FROM $this->table");
$this->select = '';
foreach ($arrFields as $arrField) $this->select .= ', c.' . $arrField['Field'];
$this->select = substr($this->select, 1);
$this->where = "c LEFT JOIN notes_users cu ON c.id = cu.note_id";
$this->where .= " WHERE (c.user_id = $iUserId OR cu.user_id = $iUserId OR public = 1)";
}
}
$arrNotes = $this->get();
if (!is_array($arrNotes)) return [];
if ($arrNotes['id']) $arrNotes = $this->get_note($arrNotes);
else foreach ($arrNotes as &$arrNote) $arrNote = $this->get_note($arrNote);
return $arrNotes;
}
function fields_actions(): array # набор полей для быстрой формы
{
return $this->fields_min();
}
function fields_min(): array # Минимальный набор полей для редактирования
{
$arrReturn = [];
$arrFields = $this->fields();
$arrReturn['id'] = $arrFields['id'];
$arrReturn['title'] = $arrFields['title'];
$arrReturn['user_id'] = $arrFields['user_id'];
$arrReturn['category_id'] = $arrFields['category_id'];
$arrReturn['client_id'] = $arrFields['client_id'];
$arrReturn['project_id'] = $arrFields['project_id'];
$arrReturn['task_id'] = $arrFields['task_id'];
$arrReturn['public'] = $arrFields['public'];
$arrReturn['content'] = $arrFields['content'];
return $arrReturn;
}
function fields(): array # Поля для редактирования
{
global $oLang;
$arrFields = $this->get_fields([
'id',
'user_id',
'date',
'date_create',
'date_update',
'category_id',
'client_id',
'project_id',
'task_id',
'public',
'content',
]);
// Managers
if ($this->id) {
$oUser = new user();
$arrFields['managers'] = $oUser->field_managers('notes_users', 'note_id', $this->id);
$arrFields['managers']['section'] = 3;
$arrFields['managers']['title'] = $oLang->get('Access');
}
return $arrFields;
}
function __construct($iNoteId = 0)
{
global $oGlobalProtect;
$this->table = 'notes';
if ($iNoteId) {
$mySql = "SELECT * FROM `" . $this->table . "`";
$mySql .= " WHERE `id` = '" . (int)$iNoteId . "'";
$arrNote = db::query($mySql);
$this->id = $arrNote['id'];
$this->content = $oGlobalProtect->protector(false, base64_decode($arrNote['content']));
$this->date = $arrNote['date'];
$this->client_id = $arrNote['client_id'];
$this->task_id = $arrNote['task_id'];
$this->project_id = $arrNote['project_id'];
$this->user_id = $arrNote['user_id'];
$this->date_create = $arrNote['date_create'];
$this->date_update = $arrNote['date_update'];
$this->category_id = $arrNote['category_id'];
} else {
$this->date_create = date("Y-m-d H:i:s");
$this->user_id = $_SESSION['user']['id'];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment