1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Models/LookupModel.php

122 lines
3.2 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 {
2017-07-24 16:06:15 +02:00
abort(500, 'Lookup account not found for ' . $accountKey);
2017-04-30 21:18:17 +02:00
}
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);
// 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-10 10:38:21 +02:00
static::setDbServer($server);
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-10 10:38:21 +02:00
static::setDbServer($server);
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-10 17:18:48 +02:00
if ($field === 'oauth_user_key') {
2017-06-26 06:18:10 +02:00
$providerId = substr($value, 0, 1);
$oauthId = substr($value, 2);
2017-05-10 17:18:48 +02:00
$isFound = $entity::where('oauth_provider_id', '=', $providerId)
2017-07-12 18:51:15 +02:00
->where('oauth_user_id', '=', $oauthId)
->withTrashed()
->first();
2017-05-10 17:18:48 +02:00
} else {
2017-07-12 18:51:15 +02:00
$isFound = $entity::where($field, '=', $value)
->withTrashed()
->first();
2017-05-10 17:18:48 +02:00
}
if (! $isFound) {
2017-08-16 09:28:40 +02:00
abort(404, "Looked up {$className} not found: {$field} => {$value}");
2017-05-01 08:50:10 +02:00
}
2021-09-26 02:13:01 +02:00
Cache::put($key, $server, 120 * 60);
2017-05-01 08:50:10 +02:00
} else {
config(['database.default' => $current]);
}
}
2017-05-10 10:38:21 +02:00
protected static function setDbServer($server)
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-04-30 21:52:05 +02:00
public function getDbServer()
{
return $this->lookupAccount->lookupCompany->dbServer->name;
}
2017-04-30 21:18:17 +02:00
}