132 lines
4.1 KiB
PHP
132 lines
4.1 KiB
PHP
<?php
|
|
/**
|
|
* app/plugins/dnd/models/items.php
|
|
*
|
|
* This class is used for the manipulation of the dnd_items 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\Bin\Canary as Debug;
|
|
use TheTempusProject\Classes\DatabaseModel;
|
|
use TheTempusProject\TheTempusProject as App;
|
|
use TheTempusProject\Bedrock\Classes\CustomException;
|
|
use TheTempusProject\Models\Sourcebooks as SourcebooksModel;
|
|
|
|
class Items extends DatabaseModel {
|
|
protected static $sourcebooks;
|
|
public $tableName = 'dnd_items';
|
|
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', '' ],
|
|
|
|
|
|
|
|
|
|
|
|
// type
|
|
// Weight
|
|
// class ( mundane / magical )
|
|
|
|
[ 'cost_copper', 'int', '12' ],
|
|
[ 'cost_silver', 'int', '12' ],
|
|
[ 'cost_gold', 'int', '12' ],
|
|
[ 'cost_platinum', 'int', '12' ],
|
|
[ 'cost_electrum', 'int', '12' ],
|
|
];
|
|
|
|
/**
|
|
* 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( "Item: 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( "Item: $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 Items 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;
|
|
}
|
|
}
|