2018-02-04 20:50:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
use Form;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-03-10 11:54:29 +00:00
|
|
|
use Illuminate\Http\Request;
|
2022-03-19 13:54:32 +00:00
|
|
|
use Illuminate\Support\Facades\Input;
|
2018-02-04 20:50:59 +00:00
|
|
|
|
|
|
|
class Setting extends Model
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The database table used by the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'settings';
|
|
|
|
|
2018-02-07 13:37:40 +00:00
|
|
|
protected $fillable = [
|
2022-03-19 13:54:32 +00:00
|
|
|
'id', 'group_id', 'key', 'type', 'options', 'label', 'value', 'order', 'system',
|
2018-02-07 13:37:40 +00:00
|
|
|
];
|
|
|
|
|
2018-02-04 20:50:59 +00:00
|
|
|
/**
|
|
|
|
* Tell the Model this Table doesn't support timestamps.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $timestamps = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache storage for Settings.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $cache = [];
|
|
|
|
|
|
|
|
/**
|
2022-11-14 12:21:47 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @return object
|
2018-02-04 20:50:59 +00:00
|
|
|
*/
|
2022-11-14 12:21:47 +00:00
|
|
|
public static function getInput(Request $request): object
|
2018-02-04 20:50:59 +00:00
|
|
|
{
|
|
|
|
return (object) [
|
2022-03-10 11:54:29 +00:00
|
|
|
'value' => $request->input('value'),
|
|
|
|
'image' => $request->file('value'),
|
2018-02-04 20:50:59 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-02-05 14:21:54 +00:00
|
|
|
public function getListValueAttribute()
|
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
if ((bool) $this->system === true) {
|
2018-10-14 15:17:55 +00:00
|
|
|
$value = self::_fetch($this->key);
|
|
|
|
} else {
|
|
|
|
$value = self::fetch($this->key);
|
|
|
|
}
|
|
|
|
$this->value = $value;
|
2022-03-19 13:54:32 +00:00
|
|
|
switch ($this->type) {
|
2018-02-05 14:21:54 +00:00
|
|
|
case 'image':
|
2022-03-19 13:54:32 +00:00
|
|
|
if (! empty($this->value)) {
|
2018-02-07 13:37:40 +00:00
|
|
|
$value = '<a href="'.asset('storage/'.$this->value).'" title="'.__('app.settings.view').'" target="_blank">'.__('app.settings.view').'</a>';
|
2018-02-05 14:21:54 +00:00
|
|
|
} else {
|
2018-02-07 13:37:40 +00:00
|
|
|
$value = __('app.options.none');
|
2022-03-19 13:54:32 +00:00
|
|
|
}
|
2018-02-05 14:21:54 +00:00
|
|
|
break;
|
|
|
|
case 'boolean':
|
2022-03-19 13:54:32 +00:00
|
|
|
if ((bool) $this->value === true) {
|
2018-02-07 13:37:40 +00:00
|
|
|
$value = __('app.options.yes');
|
2018-02-05 14:21:54 +00:00
|
|
|
} else {
|
2018-02-07 13:37:40 +00:00
|
|
|
$value = __('app.options.no');
|
2022-03-19 13:54:32 +00:00
|
|
|
}
|
2018-02-05 14:21:54 +00:00
|
|
|
break;
|
|
|
|
case 'select':
|
2022-03-19 13:54:32 +00:00
|
|
|
if (! empty($this->value) && $this->value !== 'none') {
|
|
|
|
$options = (array) json_decode($this->options);
|
|
|
|
if ($this->key === 'search_provider') {
|
2022-03-13 19:30:24 +00:00
|
|
|
$options = Search::providers()->pluck('name', 'id')->toArray();
|
2022-03-19 13:54:32 +00:00
|
|
|
}
|
2022-11-17 00:20:54 +00:00
|
|
|
$value = (array_key_exists($this->value, $options)) ? __($options[$this->value]) : __('app.options.none');
|
2018-02-05 14:21:54 +00:00
|
|
|
} else {
|
2018-02-07 13:37:40 +00:00
|
|
|
$value = __('app.options.none');
|
2022-03-19 13:54:32 +00:00
|
|
|
}
|
2018-02-05 14:21:54 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-02-07 13:37:40 +00:00
|
|
|
$value = __($this->value);
|
2018-02-05 14:21:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEditValueAttribute()
|
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
if ((bool) $this->system === true) {
|
2018-10-15 13:35:14 +00:00
|
|
|
$value = self::_fetch($this->key);
|
|
|
|
} else {
|
|
|
|
$value = self::fetch($this->key);
|
|
|
|
}
|
|
|
|
$this->value = $value;
|
2022-03-19 13:54:32 +00:00
|
|
|
switch ($this->type) {
|
2018-02-05 14:21:54 +00:00
|
|
|
case 'image':
|
|
|
|
$value = '';
|
2022-03-19 13:54:32 +00:00
|
|
|
if (isset($this->value) && ! empty($this->value)) {
|
2018-02-07 13:37:40 +00:00
|
|
|
$value .= '<a class="setting-view-image" href="'.asset('storage/'.$this->value).'" title="'.__('app.settings.view').'" target="_blank"><img src="'.asset('storage/'.$this->value).'" /></a>';
|
2018-02-05 14:21:54 +00:00
|
|
|
}
|
|
|
|
$value .= Form::file('value', ['class' => 'form-control']);
|
2022-03-19 13:54:32 +00:00
|
|
|
if (isset($this->value) && ! empty($this->value)) {
|
2018-02-07 13:37:40 +00:00
|
|
|
$value .= '<a class="settinglink" href="'.route('settings.clear', $this->id).'" title="'.__('app.settings.remove').'">'.__('app.settings.reset').'</a>';
|
2018-02-05 14:21:54 +00:00
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-02-05 14:21:54 +00:00
|
|
|
break;
|
|
|
|
case 'boolean':
|
|
|
|
$checked = false;
|
2022-03-19 13:54:32 +00:00
|
|
|
if (isset($this->value) && (bool) $this->value === true) {
|
|
|
|
$checked = true;
|
|
|
|
}
|
2018-02-05 14:21:54 +00:00
|
|
|
$set_checked = ($checked) ? ' checked="checked"' : '';
|
|
|
|
$value = '
|
2018-02-09 15:14:54 +00:00
|
|
|
<input type="hidden" name="value" value="0" />
|
2018-02-05 14:21:54 +00:00
|
|
|
<label class="switch">
|
|
|
|
<input type="checkbox" name="value" value="1"'.$set_checked.' />
|
|
|
|
<span class="slider round"></span>
|
|
|
|
</label>';
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'select':
|
|
|
|
$options = json_decode($this->options);
|
2022-03-19 13:54:32 +00:00
|
|
|
if ($this->key === 'search_provider') {
|
2022-03-13 19:30:24 +00:00
|
|
|
$options = Search::providers()->pluck('name', 'id');
|
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
foreach ($options as $key => $opt) {
|
2018-02-07 13:37:40 +00:00
|
|
|
$options->$key = __($opt);
|
|
|
|
}
|
2018-02-05 14:21:54 +00:00
|
|
|
$value = Form::select('value', $options, null, ['class' => 'form-control']);
|
|
|
|
break;
|
2022-01-18 22:59:40 +00:00
|
|
|
case 'textarea':
|
|
|
|
$value = Form::textarea('value', null, ['class' => 'form-control', 'cols' => '44', 'rows' => '15']);
|
|
|
|
break;
|
2018-02-05 14:21:54 +00:00
|
|
|
default:
|
|
|
|
$value = Form::text('value', null, ['class' => 'form-control']);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2018-02-04 20:50:59 +00:00
|
|
|
public function group()
|
|
|
|
{
|
2022-03-19 13:54:33 +00:00
|
|
|
return $this->belongsTo(\App\SettingGroup::class, 'group_id');
|
2018-02-04 20:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function fetch($key)
|
|
|
|
{
|
2018-10-14 15:17:55 +00:00
|
|
|
$user = self::user();
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-10-14 15:17:55 +00:00
|
|
|
return self::_fetch($key, $user);
|
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-10-14 15:17:55 +00:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2022-03-19 13:54:32 +00:00
|
|
|
public static function _fetch($key, $user = null)
|
2018-10-14 15:17:55 +00:00
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
//$cachekey = ($user === null) ? $key : $key.'-'.$user->id;
|
|
|
|
//if (Setting::cached($cachekey)) {
|
|
|
|
// return Setting::$cache[$cachekey];
|
|
|
|
//} else {
|
|
|
|
$find = self::where('key', '=', $key)->first();
|
|
|
|
|
|
|
|
if (! is_null($find)) {
|
|
|
|
if ((bool) $find->system === true) { // if system variable use global value
|
|
|
|
$value = $find->value;
|
|
|
|
} else { // not system variable so use user specific value
|
|
|
|
// check if user specified value has been set
|
|
|
|
//print_r($user);
|
|
|
|
$usersetting = $user->settings()->where('id', $find->id)->first();
|
|
|
|
//print_r($user->settings);
|
|
|
|
//die(var_dump($usersetting));
|
|
|
|
//->pivot->value;
|
|
|
|
//echo "user: ".$user->id." --- ".$usersettings;
|
|
|
|
if (isset($usersetting) && ! empty($usersetting)) {
|
|
|
|
$value = $usersetting->pivot->uservalue;
|
|
|
|
} else { // if not get default from base setting
|
|
|
|
//$user->settings()->save($find, ['value' => $find->value]);
|
|
|
|
//$has_setting = $user->settings()->where('id', $find->id)->exists();
|
|
|
|
//if($has_setting) {
|
|
|
|
// $user->settings()->updateExistingPivot($find->id, ['uservalue' => (string)$find->value]);
|
|
|
|
//} else {
|
|
|
|
// $user->settings()->save($find, ['uservalue' => (string)$find->value]);
|
|
|
|
//}
|
2018-10-14 15:17:55 +00:00
|
|
|
$value = $find->value;
|
|
|
|
}
|
2018-02-04 20:50:59 +00:00
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
//Setting::add($cachekey, $value);
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//}
|
2018-02-04 20:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public static function add($key, $value)
|
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
self::$cache[$key] = $value;
|
2018-02-04 20:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function cached($key)
|
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
return array_key_exists($key, self::$cache);
|
2018-02-04 20:50:59 +00:00
|
|
|
}
|
2018-02-05 14:21:54 +00:00
|
|
|
|
2018-10-12 14:10:40 +00:00
|
|
|
/**
|
|
|
|
* The users that belong to the setting.
|
|
|
|
*/
|
|
|
|
public function users()
|
|
|
|
{
|
2022-03-19 13:54:33 +00:00
|
|
|
return $this->belongsToMany(\App\User::class)->using(\App\SettingUser::class)->withPivot('uservalue');
|
2018-10-14 15:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function user()
|
|
|
|
{
|
2018-10-14 16:27:28 +00:00
|
|
|
return User::currentUser();
|
2018-10-12 14:10:40 +00:00
|
|
|
}
|
2018-02-04 20:50:59 +00:00
|
|
|
}
|