Initial commit
This commit is contained in:
117
app/plugins/dnd/models/feats.php
Normal file
117
app/plugins/dnd/models/feats.php
Normal file
@ -0,0 +1,117 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user