Files
pathfinder/app/main/model/basicmodel.php
Mark Friedrich 5c1cdca936 v1.1.2 (#270)
* fixed #194 PHP 5.6 error

* - closed #102 added "set waypoint/destination" context menu to route finder module
- update "Select2" 4.0.0 -> 4.0.3
- update "Font Awesome" 4.6.1 -> 4.6.3

* - added *.js files for develop branch

* - closed #195 fixed "BASE" dir for subDir installations
- fixed "Home" menu link

* -  #195 improved js load path

* - added "clear cache" function for manually cache clearing to /setup #200 #105 #158
- added cache size information to /setup
- added current pathfinder  "VERSION" to /setup
- updated "requireJs" 2.1.20 ->2.2.0
- removed unnecessary page cache timings from static templates (page cache)

* - added "document_root", "port", "protocol" and "PHP framework version" to /setup page
- added new "shattered" wormhole types to "signature table", closed #182, #179

* - added new "delete old signatures" option to "signature reader" dialog, closed #95

* - added new housekeeping cronjob für cached files, closed #200
- added new cache size information to /setup page

* - fixed signature groupId/typeId "overwriting" for already known signatures. closed #207
- improved system search dialog. Added trim(); before "api/signatures-> search" request

* updated README.me

* fixed PHP error "default object from empty value", closed #209

* reduced image file size

* - added local storage (IndexedDB)
- added local storage for map scroll position. closed #69

* - added "notice" panel for upcoming release information
- improved layout for "release dialog" (GitHub API)
- improved pagespeed (removed render blocking javascripts)
- improved map scrollbar configuration
- improved Chrome browser custom scrollbar layout
- removed "sign up" buttons from "map panels", closed #214

* - fixed some session and cookie  bugs

* - added new requirement check for `max_input_vars` to /setup URL, closed #224

* - fixed isWormhole(); bug

* -v1.1.1 added js build files

* - removed IGB support #206
- removed location tracking by IGB

* - added build files for upcoming version 1.1.2
- improved ajax authentication check and "logout" notification | closed #198
- improved logging, added missing log file configuration to pathfinder.ini
- added  logging for "unauthorized" requests | closed #198
- updated js "jQuery" 1.11.3 -> 3.0.0 | #206
- updated js "datatables" plugin 1.10.7 -> 1.10.12 | #206
- updated js "mCustomScrollbar" 3.1.14 -> 3.1.4 | #206

* - fixed some minor bugs in signature table module

* - fixed type "Cataclysmic", closed #241

* - added new setup DB indexing for "system_neighbour"  table to /setup route, #125
- fixed system "TrueSec" rounding in "routes module", closed #109

* - fixed system "TrueSec" rounding in "routes module", closed #109

* - added new wormhole statics for "Thera", closed #240

* - fixed missing statics for constellation "21000062" , closed #232

* - added "static" wormholes for "shattered" systems , closed #180
- added im/export function for "index" tables (*.csv import), as an alternative to the *.sql import, closed #125

* - added new system tooltip for "region name", closed #236
- updated "Bootstrap" JS-library 3.3.0 -> 3.3.5

* - removed console.log(),,,

* minor bugfixes in /setup page

* - added basic support for Russian signatures, closed #256
- added warning notification for invalid signature stings

* - added basic support for Russian signatures, closed #256

* - added requirement check for "PDO", "PDO_MYSQL" to "/setup" route
- imrpved DB PDO connection (added "TIMEOUT", forced "ERRMODE")
- fixed broken "system alias" renaming dialog

* - fixed "system graph" module rendering if there was no data available
- improved "image gallery" initialization on landing page
- added navigation to /setup page
- updated "blueImpGallery" (fixed some bugs after jQuery 3.0 upgrade) 1.15.2 -> 2.21.3
- updated "blueImpGalleryBootstrap"  (fixed some bugs after jQuery 3.0 upgrade)  3.1.1 -> 3.4.2

* - JS build files vor 1.1.2

* Updated pathfinder.css
2016-07-29 20:19:17 +02:00

689 lines
18 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Created by PhpStorm.
* User: exodus4d
* Date: 16.02.15
* Time: 22:11
*/
namespace Model;
use DB\SQL\Schema;
use Exception;
use Controller;
use DB;
abstract class BasicModel extends \DB\Cortex {
/**
* Hive key with DB object
* @var string
*/
protected $db = 'DB_PF';
/**
* caching time of field schema - seconds
* as long as we don´t expect any field changes
* -> leave this at a higher value
* @var int
*/
//protected $ttl = 86400;
/**
* caching for relational data
* @var int
*/
protected $rel_ttl = 0;
/**
* ass static columns for this table
* -> can be overwritten in child models
* @var bool
*/
protected $addStaticFields = true;
/**
* field validation array
* @var array
*/
protected $validate = [];
/**
* getData() cache key prefix
* -> do not change, otherwise cached data is lost
* @var string
*/
private $dataCacheKeyPrefix = 'DATACACHE';
/**
* enables data export for this table
* -> can be overwritten in child models
* @var bool
*/
public static $enableDataExport = false;
/**
* enables data import for this table
* -> can be overwritten in child models
* @var bool
*/
public static $enableDataImport = false;
public function __construct($db = NULL, $table = NULL, $fluid = NULL, $ttl = 0){
$this->addStaticFieldConfig();
parent::__construct($db, $table, $fluid, $ttl);
// events -----------------------------------------
$this->afterinsert(function($self){
$self->afterinsertEvent($self);
$self->clearCacheData();
});
$this->afterupdate( function($self){
$self->afterupdateEvent($self);
$self->clearCacheData();
});
$this->beforeinsert( function($self){
$self->beforeInsertEvent($self);
});
$this->beforeerase( function($self){
$self->beforeeraseEvent($self);
});
$this->aftererase( function($self){
$self->aftereraseEvent($self);
});
}
/**
* @param string $key
* @param mixed $val
* @return mixed|void
* @throws Exception\ValidationException
*/
public function set($key, $val){
if($key == 'active'){
// prevent abuse
return;
}
if(
!$this->dry() &&
$key != 'updated'
){
if( $this->exists($key) ){
$currentVal = $this->get($key);
// if current value is not a relational object
// and value has changed -> update table col
if(is_object($currentVal)){
if(
is_numeric($val) &&
is_subclass_of($currentVal, 'Model\BasicModel') &&
$currentVal->_id !== (int)$val
){
$this->touch('updated');
}
}elseif($currentVal != $val){
$this->touch('updated');
}
}
}
// trim all values
if(is_string($val)){
$val = trim($val);
}
$valid = $this->validateField($key, $val);
if(!$valid){
$this->throwValidationError($key);
}else{
return parent::set($key, $val);
}
}
/**
* extent the fieldConf Array with static fields for each table
*/
private function addStaticFieldConfig(){
// add static fields to this mapper
// static tables (fixed data) do not require them...
if($this->addStaticFields){
$staticFieldConfig = [
'created' => [
'type' => Schema::DT_TIMESTAMP,
'default' => Schema::DF_CURRENT_TIMESTAMP,
'index' => true
],
'updated' => [
'type' => Schema::DT_TIMESTAMP,
'default' => Schema::DF_CURRENT_TIMESTAMP,
'index' => true
]
];
$this->fieldConf = array_merge($staticFieldConfig, $this->fieldConf);
}
}
/**
* validates a table column based on validation settings
* @param $col
* @param $val
* @return bool
*/
private function validateField($col, $val){
$valid = true;
if(array_key_exists($col, $this->validate)){
$fieldValidationOptions = $this->validate[$col];
foreach($fieldValidationOptions as $validateKey => $validateOption ){
if(is_array($fieldValidationOptions[$validateKey])){
$fieldSubValidationOptions = $fieldValidationOptions[$validateKey];
foreach($fieldSubValidationOptions as $validateSubKey => $validateSubOption ){
switch($validateKey){
case 'length':
switch($validateSubKey){
case 'min';
if(strlen($val) < $validateSubOption){
$valid = false;
}
break;
case 'max';
if(strlen($val) > $validateSubOption){
$valid = false;
}
break;
}
break;
}
}
}else{
switch($validateKey){
case 'regex':
$valid = (bool)preg_match($fieldValidationOptions[$validateKey], $val);
break;
}
}
// a validation rule failed
if(!$valid){
break;
}
}
}
return $valid;
}
/**
* get the cache key for this model
* ->do not set a key if the model is not saved!
* @param string $dataCacheTableKeyPrefix
* @return null|string
*/
protected function getCacheKey($dataCacheTableKeyPrefix = ''){
$cacheKey = null;
// set a model unique cache key if the model is saved
if( $this->id > 0){
// check if there is a given key prefix
// -> if not, use the standard key.
// this is useful for caching multiple data sets according to one row entry
$cacheKey = $this->dataCacheKeyPrefix;
$cacheKey .= '.' . strtoupper($this->table);
if($dataCacheTableKeyPrefix){
$cacheKey .= '.' . $dataCacheTableKeyPrefix . '_';
}else{
$cacheKey .= '.ID_';
}
$cacheKey .= (string) $this->_id;
}
return $cacheKey;
}
/**
* get cached data from this model
* @param string $dataCacheKeyPrefix - optional key prefix
* @return \stdClass|null
*/
protected function getCacheData($dataCacheKeyPrefix = ''){
$cacheKey = $this->getCacheKey($dataCacheKeyPrefix);
$cacheData = null;
if( !is_null($cacheKey) ){
$f3 = self::getF3();
if( $f3->exists($cacheKey) ){
$cacheData = $f3->get( $cacheKey );
}
}
return $cacheData;
}
/**
* update/set the getData() cache for this object
* @param $cacheData
* @param string $dataCacheKeyPrefix
* @param int $data_ttl
*/
public function updateCacheData($cacheData, $dataCacheKeyPrefix = '', $data_ttl = 300){
$cacheDataTmp = (array)$cacheData;
// check if data should be cached
// and cacheData is not empty
if(
$data_ttl > 0 &&
!empty( $cacheDataTmp )
){
$cacheKey = $this->getCacheKey($dataCacheKeyPrefix);
if( !is_null($cacheKey) ){
self::getF3()->set($cacheKey, $cacheData, $data_ttl);
}
}
}
/**
* unset the getData() cache for this object
*/
public function clearCacheData(){
$cacheKey = $this->getCacheKey();
if( !is_null($cacheKey) ){
$f3 = self::getF3();
if( $f3->exists($cacheKey) ){
$f3->clear($cacheKey);
}
}
}
/**
* Throws a validation error for a giben column
* @param $col
* @throws \Exception\ValidationException
*/
protected function throwValidationError($col){
throw new Exception\ValidationException('Validation failed: "' . $col . '".', $col);
}
/**
* set "updated" field to current timestamp
* this is useful to mark a row as "changed"
*/
protected function setUpdated(){
if($this->_id > 0){
$pfDB = DB\Database::instance()->getDB('PF');
$pfDB->exec(
["UPDATE " . $this->table . " SET updated=NOW() WHERE id=:id"],
[
[':id' => $this->_id]
]
);
}
}
/**
* get single dataSet by id
* @param $id
* @param int $ttl
* @return \DB\Cortex
*/
public function getById($id, $ttl = 3) {
return $this->getByForeignKey('id', (int)$id, ['limit' => 1], $ttl);
}
/**
* checks whether this model is active or not
* each model should have an "active" column
* @return bool
*/
public function isActive(){
return (bool)$this->active;
}
/**
* set active state for a model
* @param $value
*/
public function setActive($value){
$this->set('active', (int)$value);
}
/**
* get dataSet by foreign column (single result)
* @param $key
* @param $value
* @param array $options
* @param int $ttl
* @return \DB\Cortex
*/
public function getByForeignKey($key, $value, $options = [], $ttl = 60){
$querySet = [];
$query = [];
if($this->exists($key)){
$query[] = $key . " = :" . $key;
$querySet[':' . $key] = $value;
}
// check active column
if($this->exists('active')){
$query[] = "active = :active";
$querySet[':active'] = 1;
}
array_unshift($querySet, implode(' AND ', $query));
return $this->load( $querySet, $options, $ttl );
}
/**
* Event "Hook" function
* can be overwritten
* return false will stop any further action
*/
public function beforeInsertEvent(){
return true;
}
/**
* Event "Hook" function
* can be overwritten
* return false will stop any further action
*/
public function afterinsertEvent(){
return true;
}
/**
* Event "Hook" function
* can be overwritten
* return false will stop any further action
*/
public function afterupdateEvent(){
return true;
}
/**
* Event "Hook" function
* can be overwritten
* @return bool
*/
public function beforeeraseEvent($self){
return true;
}
/**
* Event "Hook" function
* can be overwritten
* @return bool
*/
public function aftereraseEvent($self){
return true;
}
/**
* function should be overwritten in child classes with access restriction
* @param CharacterModel $characterModel
* @return bool
*/
public function hasAccess(CharacterModel $characterModel){
return true;
}
/**
* function should be overwritten in parent classes
* @return bool
*/
public function isValid(){
return true;
}
/**
* export and download table data as *.csv
* this is primarily used for static tables
* @return bool
*/
public function exportData(){
$status = false;
if(static::$enableDataExport){
$tableModifier = static::getTableModifier();
$headers = $tableModifier->getCols();
// just get the records with existing columns
// -> no "virtual" fields or "new" columns
$this->fields($headers);
$allRecords = $this->find();
if($allRecords){
$tableData = $allRecords->castAll(0);
// format data -> "id" must be first key
foreach($tableData as &$rowData){
$rowData = [$this->primary => $rowData['_id']] + $rowData;
unset($rowData['_id']);
}
$sheet = \Sheet::instance();
$data = $sheet->dumpCSV($tableData, $headers);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: text/csv;charset=UTF-8');
header('Content-Disposition: attachment;filename=' . $this->getTable() . '.csv');
echo $data;
exit();
}
}
return $status;
}
/**
* import table data from a *.csv file
* @return bool
*/
public function importData(){
$status = false;
// rtrim(); for arrays (removes empty values) from the end
$rtrim = function($array = []){
return array_slice($array, 0, key(array_reverse(array_diff($array, ['']), 1))+1);
};
if(static::$enableDataImport){
$filePath = $this->getF3()->get('EXPORT') . 'csv/' . $this->getTable() . '.csv';
if(is_file($filePath)){
$handle = @fopen($filePath, 'r');
$keys = array_map('lcfirst', fgetcsv($handle, 0, ';'));
$keys = $rtrim($keys);
if(count($keys) > 0){
$tableData = [];
while (!feof($handle)) {
$tableData[] = array_combine($keys, $rtrim(fgetcsv($handle, 0, ';')));
}
// import row data
$status = $this->importStaticData($tableData);
$this->getF3()->status(202);
}else{
$this->getF3()->error(502, 'File could not be read');
}
}else{
$this->getF3()->error(404, 'File not found: ' . $filePath);
}
}
return $status;
}
/**
* insert/update static data into this table
* WARNING: rows will be deleted if not part of $tableData !
* @param array $tableData
* @return array
*/
protected function importStaticData($tableData = []){
$rowIDs = [];
$addedCount = 0;
$updatedCount = 0;
$deletedCount = 0;
foreach($tableData as $rowData){
// search for existing record and update columns
$this->getById($rowData['id']);
if($this->dry()){
$addedCount++;
}else{
$updatedCount++;
}
$this->copyfrom($rowData);
$this->save();
$rowIDs[] = $this->id;
$this->reset();
}
// remove old data
$oldRows = $this->find('id NOT IN (' . implode(',', $rowIDs) . ')');
if($oldRows){
foreach($oldRows as $oldRow){
$oldRow->erase();
$deletedCount++;
}
}
return ['added' => $addedCount, 'updated' => $updatedCount, 'deleted' => $deletedCount];
}
/**
* get the current class name
* -> namespace not included
* @return string
*/
public static function getClassName(){
$parts = explode('\\', static::class);
return end($parts);
}
/**
* factory for all Models
* @param string $model
* @param int $ttl
* @return BasicModel
* @throws \Exception
*/
public static function getNew($model, $ttl = 86400){
$class = null;
$model = '\\' . __NAMESPACE__ . '\\' . $model;
if(class_exists($model)){
$class = new $model( null, null, null, $ttl );
}else{
throw new \Exception('No model class found');
}
return $class;
}
/**
* get the framework instance (singleton)
* @return \Base
*/
public static function getF3(){
return \Base::instance();
}
/**
* debug log function
* @param string $text
* @param string $type
*/
public static function log($text, $type = null){
$type = isset($type) ? $type : 'DEBUG';
Controller\LogController::getLogger($type)->write($text);
}
/**
* get tableModifier class for this table
* @return bool|DB\SQL\TableModifier
*/
public static function getTableModifier(){
$df = parent::resolveConfiguration();
$schema = new Schema($df['db']);
$tableModifier = $schema->alterTable( $df['table'] );
return $tableModifier;
}
/**
* Check whether a (multi)-column index exists or not on a table
* related to this model
* @param array $columns
* @return bool|array
*/
public static function indexExists(array $columns = []){
$tableModifier = self::getTableModifier();
$df = parent::resolveConfiguration();
$check = false;
$indexKey = $df['table'] . '___' . implode('__', $columns);
$indexList = $tableModifier->listIndex();
if(array_key_exists( $indexKey, $indexList)){
$check = $indexList[$indexKey];
}
return $check;
}
/**
* set a multi-column index for this table
* @param array $columns Column(s) to be indexed
* @param bool $unique Unique index
* @param int $length index length for text fields in mysql
* @return bool
*/
public static function setMultiColumnIndex(array $columns = [], $unique = false, $length = 20){
$status = false;
$tableModifier = self::getTableModifier();
if( self::indexExists($columns) === false ){
$tableModifier->addIndex($columns, $unique, $length);
$buildStatus = $tableModifier->build();
if($buildStatus === 0){
$status = true;
}
}
return $status;
}
}