155 lines
5.2 KiB
PHP
155 lines
5.2 KiB
PHP
<?php
|
|
/**
|
|
* app/plugins/dnd/models/languages.php
|
|
*
|
|
* This class is used for the manipulation of the dnd_languages 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 Languages extends DatabaseModel {
|
|
protected static $sourcebooks;
|
|
public $tableName = 'dnd_languages';
|
|
public $databaseMatrix = [
|
|
// Core
|
|
[ 'name', 'varchar', '32' ],
|
|
[ 'type', 'varchar', '32' ], // exotic / standard / other
|
|
[ 'typical_speakers', 'varchar', '32' ],
|
|
[ 'script', 'varchar', '32' ],
|
|
[ 'sound', 'text', '' ],
|
|
|
|
// General
|
|
[ 'createdBy', 'int', '11' ],
|
|
[ 'createdAt', 'int', '11' ],
|
|
[ 'privacy', 'varchar', '16' ],
|
|
[ 'version', 'varchar', '5' ],
|
|
[ 'sourcebookID', 'int', '11' ],
|
|
[ 'shortDescription', 'text', '' ],
|
|
[ 'description', 'text', '' ],
|
|
];
|
|
|
|
/**
|
|
* The model constructor.
|
|
*/
|
|
public function __construct() {
|
|
parent::__construct();
|
|
self::$sourcebooks = new SourcebooksModel;
|
|
}
|
|
|
|
public function create( $name, $privacy, $version, $sourcebookID = '', $type = '', $typical_speakers = '', $script = '', $sound = '', $description = '', $shortDescription = '' ) {
|
|
$fields = [
|
|
'name' => $name,
|
|
'type' => $type,
|
|
'typical_speakers' => $typical_speakers,
|
|
'script' => $script,
|
|
'sound' => $sound,
|
|
'privacy' => $privacy,
|
|
'version' => $version,
|
|
'sourcebookID' => $sourcebookID,
|
|
'shortDescription' => $shortDescription,
|
|
'description' => $description,
|
|
'createdBy' => App::$activeUser->ID,
|
|
'createdAt' => time(),
|
|
];
|
|
if ( ! self::$db->insert( $this->tableName, $fields ) ) {
|
|
new CustomException( 'Create' );
|
|
Debug::error( "Language: not created " . var_export($fields,true) );
|
|
return false;
|
|
}
|
|
return self::$db->lastId();
|
|
}
|
|
|
|
public function update( $id, $name, $privacy, $version, $sourcebookID = '', $type = '', $typical_speakers = '', $script = '', $sound = '', $description = '', $shortDescription = '' ) {
|
|
$fields = [
|
|
'name' => $name,
|
|
'type' => $type,
|
|
'typical_speakers' => $typical_speakers,
|
|
'script' => $script,
|
|
'sound' => $sound,
|
|
'privacy' => $privacy,
|
|
'version' => $version,
|
|
'sourcebookID' => $sourcebookID,
|
|
'shortDescription' => $shortDescription,
|
|
'description' => $description,
|
|
];
|
|
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
|
|
new CustomException( 'Update' );
|
|
Debug::error( "Language: $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 Languages found.' );
|
|
return false;
|
|
}
|
|
return $this->filter( $objects->results() );
|
|
}
|
|
|
|
public function paginatedList() {
|
|
$whereClause = ['createdBy', '=', App::$activeUser->ID];
|
|
$objects = self::$db->getPaginated( $this->tableName, $whereClause );
|
|
if ( ! $objects->count() ) {
|
|
Debug::info( 'No Language found.' );
|
|
return false;
|
|
}
|
|
return $this->filter( $objects->results() );
|
|
}
|
|
|
|
public function simpleList() {
|
|
$whereClause = ['createdBy', '=', App::$activeUser->ID];
|
|
$objects = self::$db->get( $this->tableName, $whereClause );
|
|
if ( ! $objects->count() ) {
|
|
Debug::warn( 'No Languages found.' );
|
|
return false;
|
|
}
|
|
|
|
$objects = $objects->results();
|
|
$out = [];
|
|
foreach ( $objects as &$object ) {
|
|
$out[ $object->name ] = $object->ID;
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|