1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/app/Models/LookupModel.php

115 lines
2.8 KiB
PHP
Raw Normal View History

2017-04-30 21:18:17 +02:00
<?php
namespace App\Models;
use Eloquent;
2017-05-02 20:55:36 +02:00
use Cache;
2017-04-30 21:18:17 +02:00
/**
* 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
2017-05-02 20:55:36 +02:00
if (env('MULTI_DB_CACHE_ENABLED') && $server = Cache::get($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 15:06:34 +02:00
if ($value && $lookupModel = static::where($field, '=', $value)->first()) {
2017-05-01 10:13:15 +02:00
$entity = new $className();
2017-05-01 15:06:34 +02:00
$server = $lookupModel->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}");
}
2017-05-02 20:55:36 +02:00
Cache::put($key, $server, 120);
2017-05-01 08:50:10 +02:00
} else {
config(['database.default' => $current]);
}
}
2017-05-02 11:09:01 +02:00
protected static function setDbServer($server, $isUser = false)
2017-05-01 08:50:10 +02:00
{
2017-05-02 11:09:01 +02:00
if (! env('MULTI_DB_ENABLED')) {
return;
}
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
}