blue-twilight/app/Helpers/ConfigHelper.php

146 lines
3.9 KiB
PHP

<?php
namespace App\Helpers;
use App\Album;
use App\AlbumSources\IAlbumSource;
use App\AlbumSources\LocalFilesystemSource;
use App\Configuration;
class ConfigHelper
{
public function allowedAlbumViews()
{
return ['default', 'slideshow'];
}
public function allowedDateFormats()
{
return [
'Y-m-d - H:i',
'd/m/Y - H:i',
'm/d/Y - H:i',
'jS F Y - H:i',
];
}
public function albumSources()
{
$results = [];
$classes = [
LocalFilesystemSource::class
];
foreach ($classes as $class)
{
/** @var IAlbumSource $instance */
$instance = new $class;
$key = basename(str_replace('\\', '/', $class));
$results[$key] = trans($instance->getName());
}
return $results;
}
public function allowedThemeNames()
{
$results = [];
$directoryIterator = new \FilesystemIterator(dirname(dirname(__DIR__)) . '/resources/views/themes', \FilesystemIterator::SKIP_DOTS);
/** @var \SplFileInfo $fileInfo */
foreach ($directoryIterator as $fileInfo)
{
if (!$fileInfo->isDir() || strtolower($fileInfo->getFilename()) == 'base')
{
continue;
}
$themeName = strtolower($fileInfo->getFilename());
$themeDisplayName = $themeName;
$themeInfoFilename = sprintf('%s/theme.json', $fileInfo->getPathname());
if (file_exists($themeInfoFilename))
{
$themeInfo = json_decode(file_get_contents($themeInfoFilename));
if ($themeInfo !== false && isset($themeInfo->name))
{
$themeDisplayName = $themeInfo->name;
}
}
$results[$themeName] = $themeDisplayName;
}
return $results;
}
public function defaults()
{
$currentAppName = $this->get('app_name', false);
return array(
'allow_self_registration' => true,
'app_name' => trans('global.app_name'),
'date_format' => $this->allowedDateFormats()[0],
'default_album_view' => $this->allowedAlbumViews()[0],
'hotlink_protection' => false,
'items_per_page' => 12,
'items_per_page_admin' => 10,
'recaptcha_enabled_registration' => false,
'recaptcha_secret_key' => '',
'recaptcha_site_key' => '',
'require_email_verification' => true,
'restrict_original_download' => true,
'sender_address' => sprintf('hostmaster@%s', (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost')),
'sender_name' => (is_null($currentAppName) ? trans('global.app_name') : $currentAppName),
'smtp_server' => 'localhost',
'smtp_port' => 25,
'theme' => 'bootstrap3'
);
}
public function get($key, $defaultIfUnset = true)
{
$config = Configuration::where('key', $key)->first();
if (is_null($config))
{
if ($defaultIfUnset)
{
$defaults = $this->defaults();
return (isset($defaults[$key]) ? $defaults[$key] : null);
}
return null;
}
return $config->value;
}
public function getAll()
{
$results = array();
foreach (Configuration::all() as $config)
{
$results[$config->key] = $config->value;
}
return $results;
}
public function getOrCreateModel($key)
{
$config = Configuration::where('key', $key)->first();
if (is_null($config) || $config === false)
{
$config = new Configuration();
$config->key = $key;
$config->value = '';
$config->save();
}
return $config;
}
}