generated from jric11/baseProject
36 lines
3.4 KiB
PHP
36 lines
3.4 KiB
PHP
<?php
|
|
class Notes extends Base {
|
|
public const VALID_TYPES=['User'];
|
|
public function addNote(){
|
|
if(($_POST['step']??'prompt')==='add'){$this->write(0,$_POST['note_type']??'',MaintenanceService::id($_POST['note_reference']??null),$_POST['note_text']??'');return;}
|
|
$type=$_POST['note_type']??'';$id=MaintenanceService::id($_POST['note_reference']??null);
|
|
if(!in_array($type,self::VALID_TYPES,true))throw new AppException('Unsupported note type.');
|
|
$stmt=$this->connect()->prepare('SELECT user_serial FROM users WHERE user_serial=?');$stmt->execute([$id]);if(!$stmt->fetch())throw new AppException('User not found.',404);
|
|
$this->displayContent($this->applyXSL('<XML/>',$this->getXSL('addNote'),['NOTE_TYPE'=>$type,'NOTE_REFERENCE'=>$id]));
|
|
}
|
|
public function editNote(){
|
|
$id=MaintenanceService::id($_POST['note_serial']??null);$note=$this->getNote($id);
|
|
if(!$note->count())throw new AppException('Note not found.',404);
|
|
if(($_POST['step']??'prompt')==='update'){$this->write($id,(string)$note->record->note_type,(int)$note->record->note_reference,$_POST['note_text']??'');return;}
|
|
$this->displayContent($this->applyXSL($note,$this->getXSL('editNote')));
|
|
}
|
|
public function deleteNote(){
|
|
$stmt=$this->connect()->prepare("DELETE FROM notes WHERE note_serial=? AND note_type='User'");$stmt->execute([MaintenanceService::id($_POST['note_serial']??null)]);if(!$stmt->rowCount())throw new AppException('Note not found.',404);
|
|
}
|
|
public function write(int $id,string $type,int $reference,string $text): int {
|
|
$text=trim($text);if(!in_array($type,self::VALID_TYPES,true)||!$text||strlen($text)>65535)throw new AppException('Enter a valid user note (up to 65,535 bytes).');
|
|
$db=$this->connect();$db->beginTransaction();
|
|
try{
|
|
$stmt=$db->prepare('SELECT user_serial FROM users WHERE user_serial=? FOR UPDATE');$stmt->execute([$reference]);if(!$stmt->fetch())throw new AppException('The note must refer to an existing user.',409);
|
|
if($id){$stmt=$db->prepare('SELECT note_serial FROM notes WHERE note_serial=? AND note_type=? AND note_reference=? FOR UPDATE');$stmt->execute([$id,$type,$reference]);if(!$stmt->fetch())throw new AppException('Note not found.',404);
|
|
$stmt=$db->prepare('UPDATE notes SET note_text=?,note_origin=? WHERE note_serial=?');$stmt->execute([$text,$_SESSION[self::USER_NAME]??'',$id]);
|
|
}else{$stmt=$db->prepare('INSERT INTO notes(note_type,note_reference,note_text,note_origin) VALUES(?,?,?,?)');$stmt->execute([$type,$reference,$text,$_SESSION[self::USER_NAME]??'']);$id=(int)$db->lastInsertId();}
|
|
$db->commit();return $id;
|
|
}catch(Throwable $e){if($db->inTransaction())$db->rollBack();throw $e;}
|
|
}
|
|
public function getNote($id=0){$stmt=$this->connect()->prepare('SELECT * FROM view_notes WHERE note_serial=?');$stmt->execute([(int)$id]);return $this->getTableXML('notes',$stmt);}
|
|
public function getNotes($type='',$reference=0){$stmt=$this->connect()->prepare('SELECT * FROM view_notes WHERE note_type=? AND note_reference=? ORDER BY note_timestamp DESC,note_serial DESC');$stmt->execute([$type,(int)$reference]);return $this->getTableXML('notes',$stmt);}
|
|
public function getAllNotes($reference=0){return $this->getNotes('User',$reference);}
|
|
public function addNoteQuick($type='',$reference=0,$text=''){return $this->write(0,$type,(int)$reference,$text);}
|
|
}
|