2017-04-30 21:18:17 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Eloquent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ExpenseCategory.
|
|
|
|
*/
|
|
|
|
class LookupModel extends Eloquent
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $timestamps = false;
|
|
|
|
|
2017-04-30 21:52:05 +02:00
|
|
|
public function lookupAccount()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\LookupAccount');
|
|
|
|
}
|
2017-04-30 21:18:17 +02:00
|
|
|
|
|
|
|
public static function createNew($accountKey, $data)
|
|
|
|
{
|
|
|
|
if (! env('MULTI_DB_ENABLED')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current = config('database.default');
|
|
|
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
|
|
|
|
|
|
|
$lookupAccount = LookupAccount::whereAccountKey($accountKey)->first();
|
|
|
|
|
|
|
|
if ($lookupAccount) {
|
|
|
|
$data['lookup_account_id'] = $lookupAccount->id;
|
|
|
|
} else {
|
|
|
|
abort('Lookup account not found for ' . $accountKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
static::create($data);
|
|
|
|
|
|
|
|
config(['database.default' => $current]);
|
|
|
|
}
|
2017-04-30 21:52:05 +02:00
|
|
|
|
2017-05-01 11:29:45 +02:00
|
|
|
public static function deleteWhere($where)
|
|
|
|
{
|
|
|
|
if (! env('MULTI_DB_ENABLED')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current = config('database.default');
|
|
|
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
|
|
|
|
|
|
|
static::where($where)->delete();
|
|
|
|
|
|
|
|
config(['database.default' => $current]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-01 08:50:10 +02:00
|
|
|
public static function setServerByField($field, $value)
|
|
|
|
{
|
|
|
|
if (! env('MULTI_DB_ENABLED')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = get_called_class();
|
|
|
|
$className = str_replace('Lookup', '', $className);
|
|
|
|
$key = sprintf('server:%s:%s:%s', $className, $field, $value);
|
2017-05-01 14:17:52 +02:00
|
|
|
$isUser = $className == 'App\Models\User';
|
2017-05-01 08:50:10 +02:00
|
|
|
|
|
|
|
// check if we've cached this lookup
|
|
|
|
if ($server = session($key)) {
|
2017-05-01 14:17:52 +02:00
|
|
|
static::setDbServer($server, $isUser);
|
2017-05-01 08:50:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current = config('database.default');
|
|
|
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
|
|
|
|
2017-05-01 11:29:45 +02:00
|
|
|
if ($value && $lookupUser = static::where($field, '=', $value)->first()) {
|
2017-05-01 10:13:15 +02:00
|
|
|
$entity = new $className();
|
2017-05-01 08:50:10 +02:00
|
|
|
$server = $lookupUser->getDbServer();
|
2017-05-01 14:17:52 +02:00
|
|
|
static::setDbServer($server, $isUser);
|
2017-05-01 08:50:10 +02:00
|
|
|
|
2017-05-01 10:13:15 +02:00
|
|
|
// check entity is found on the server
|
2017-05-01 08:50:10 +02:00
|
|
|
if (! $entity::where($field, '=', $value)->first()) {
|
|
|
|
abort("Looked up {$className} not found: {$field} => {$value}");
|
|
|
|
}
|
|
|
|
|
|
|
|
session([$key => $server]);
|
|
|
|
} else {
|
|
|
|
config(['database.default' => $current]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 14:17:52 +02:00
|
|
|
public static function setDbServer($server, $isUser = false)
|
2017-05-01 08:50:10 +02:00
|
|
|
{
|
|
|
|
config(['database.default' => $server]);
|
2017-05-01 14:17:52 +02:00
|
|
|
|
|
|
|
if ($isUser) {
|
|
|
|
session([SESSION_DB_SERVER => $server]);
|
|
|
|
}
|
2017-05-01 08:50:10 +02:00
|
|
|
}
|
|
|
|
|
2017-04-30 21:52:05 +02:00
|
|
|
public function getDbServer()
|
|
|
|
{
|
|
|
|
return $this->lookupAccount->lookupCompany->dbServer->name;
|
|
|
|
}
|
2017-04-30 21:18:17 +02:00
|
|
|
}
|