Files
thetempusproject/app/plugins/dnd/models/feats.php
2024-08-04 21:15:59 -04:00

118 lines
3.8 KiB
PHP

<?php
/**
* app/plugins/dnd/models/feats.php
*
* This class is used for the manipulation of the dnd_feats database table.
*
* @package TTE Dungeons & Dragons
* @version 3.0
* @author Joey Kimsey <Joey@tabletopelite.com>
* @link https://TableTopElite.com
*/
namespace TheTempusProject\Models;
use TheTempusProject\Canary\Canary as Debug;
use TheTempusProject\Classes\DatabaseModel;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Bedrock\Classes\CustomException;
use TheTempusProject\Models\Sourcebooks as SourcebooksModel;
class Feats extends DatabaseModel {
protected static $sourcebooks;
public $tableName = 'dnd_feats';
public $databaseMatrix = [
// Name
[ 'name', 'varchar', '32' ],
// General
[ 'createdBy', 'int', '11' ],
[ 'createdAt', 'int', '11' ],
[ 'privacy', 'varchar', '16' ],
[ 'version', 'varchar', '5' ],
[ 'sourcebookID', 'int', '11' ],
[ 'shortDescription', 'text', '' ],
[ 'description', 'text', '' ],
[ 'image', 'text', '' ],
];
/**
* The model constructor.
*/
public function __construct() {
parent::__construct();
self::$sourcebooks = new SourcebooksModel;
}
public function create( $name, $privacy, $version, $sourcebookID, $description = '', $shortDescription = '' ) {
$fields = [
'name' => $name,
'privacy' => $privacy,
'version' => $version,
'sourcebookID' => $sourcebookID,
'description' => $description,
'shortDescription' => $shortDescription,
'createdBy' => App::$activeUser->ID,
'createdAt' => time(),
];
if ( ! self::$db->insert( $this->tableName, $fields ) ) {
new CustomException( 'Create' );
Debug::error( "Feat: not created " . var_export($fields,true) );
return false;
}
return self::$db->lastId();
}
public function update( $id, $name, $privacy, $version, $sourcebookID, $description = '', $shortDescription = '' ) {
$fields = [
'name' => $name,
'privacy' => $privacy,
'version' => $version,
'sourcebookID' => $sourcebookID,
'description' => $description,
'shortDescription' => $shortDescription,
];
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
new CustomException( 'Update' );
Debug::error( "Feat: $id not updated: $fields" );
return false;
}
return true;
}
public function byUser( $limit = null ) {
$whereClause = ['createdBy', '=', App::$activeUser->ID];
if ( empty( $limit ) ) {
$objects = self::$db->get( $this->tableName, $whereClause );
} else {
$objects = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] );
}
if ( !$objects->count() ) {
Debug::info( 'No Feats found.' );
return false;
}
return $this->filter( $objects->results() );
}
public function filter( $data, $params = [] ) {
$out = [];
foreach ( $data as $instance ) {
if ( !is_object( $instance ) ) {
$instance = $data;
$end = true;
}
$sourcebook = self::$sourcebooks->findById( $instance->sourcebookID );
if ( !empty( $sourcebook ) ) {
$instance->sourcebookName = $sourcebook->name;
} else {
$instance->sourcebookName = 'Unknown';
}
$out[] = $instance;
if ( !empty( $end ) ) {
$out = $out[0];
break;
}
}
return $out;
}
}