From 65e959e84dd748305fe7f403354ff12b4108535d Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 4 Nov 2015 15:57:59 +0200 Subject: [PATCH] Added system settings page --- app/Http/Controllers/AccountController.php | 17 +++ app/Http/Controllers/AppController.php | 70 ++++++++- app/Http/routes.php | 2 + public/js/built.js | 2 +- resources/lang/en/texts.php | 2 + resources/views/accounts/nav.blade.php | 6 +- .../views/accounts/system_settings.blade.php | 36 +++++ resources/views/dashboard.blade.php | 8 +- .../views/partials/system_settings.blade.php | 112 ++++++++++++++ resources/views/setup.blade.php | 139 +----------------- 10 files changed, 245 insertions(+), 149 deletions(-) create mode 100644 resources/views/accounts/system_settings.blade.php create mode 100644 resources/views/partials/system_settings.blade.php diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index dca795945620..c0e7f1d096fd 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -174,6 +174,8 @@ class AccountController extends BaseController return self::showProducts(); } elseif ($section === ACCOUNT_TAX_RATES) { return self::showTaxRates(); + } elseif ($section === ACCOUNT_SYSTEM_SETTINGS) { + return self::showSystemSettings(); } else { $data = [ 'account' => Account::with('users')->findOrFail(Auth::user()->account_id), @@ -184,6 +186,21 @@ class AccountController extends BaseController } } + private function showSystemSettings() + { + if (Utils::isNinjaProd()) { + return Redirect::to('/'); + } + + $data = [ + 'account' => Account::with('users')->findOrFail(Auth::user()->account_id), + 'title' => trans("texts.system_settings"), + 'section' => ACCOUNT_SYSTEM_SETTINGS, + ]; + + return View::make("accounts.system_settings", $data); + } + private function showInvoiceSettings() { $account = Auth::user()->account; diff --git a/app/Http/Controllers/AppController.php b/app/Http/Controllers/AppController.php index 8c3b9b5fe59f..ce029ac15070 100644 --- a/app/Http/Controllers/AppController.php +++ b/app/Http/Controllers/AppController.php @@ -48,7 +48,7 @@ class AppController extends BaseController public function doSetup() { - if (Utils::isNinjaProd() || (Utils::isDatabaseSetup() && Account::count() > 0)) { + if (Utils::isNinjaProd()) { return Redirect::to('/'); } @@ -57,9 +57,10 @@ class AppController extends BaseController $app = Input::get('app'); $app['key'] = env('APP_KEY') ?: str_random(RANDOM_KEY_LENGTH); + $app['debug'] = Input::get('debug') ? 'true' : 'false'; $database = Input::get('database'); - $dbType = $database['default']; + $dbType = 'mysql'; // $database['default']; $database['connections'] = [$dbType => $database['type']]; $mail = Input::get('mail'); @@ -78,8 +79,12 @@ class AppController extends BaseController return Redirect::to('/setup')->withInput(); } + if (Utils::isDatabaseSetup() && Account::count() > 0) { + return Redirect::to('/'); + } + $config = "APP_ENV=production\n". - "APP_DEBUG=false\n". + "APP_DEBUG={$app['debug']}\n". "APP_URL={$app['url']}\n". "APP_KEY={$app['key']}\n\n". "DB_TYPE={$dbType}\n". @@ -120,17 +125,68 @@ class AppController extends BaseController return Redirect::to('/login'); } + public function updateSetup() + { + if (Utils::isNinjaProd()) { + return Redirect::to('/'); + } + + if (!Auth::check() && Utils::isDatabaseSetup() && Account::count() > 0) { + return Redirect::to('/'); + } + + if ( ! $canUpdateEnv = @fopen(base_path()."/.env", 'w')) { + Session::flash('error', 'Warning: Permission denied to write to .env config file, try running sudo chown www-data:www-data /path/to/ninja/.env'); + return Redirect::to('/settings/system_settings'); + } + + $app = Input::get('app'); + $db = Input::get('database'); + $mail = Input::get('mail'); + + $_ENV['APP_URL'] = $app['url']; + $_ENV['APP_DEBUG'] = Input::get('debug') ? 'true' : 'false'; + + $_ENV['DB_TYPE'] = 'mysql'; // $db['default']; + $_ENV['DB_HOST'] = $db['type']['host']; + $_ENV['DB_DATABASE'] = $db['type']['database']; + $_ENV['DB_USERNAME'] = $db['type']['username']; + $_ENV['DB_PASSWORD'] = $db['type']['password']; + + if ($mail) { + $_ENV['MAIL_DRIVER'] = $mail['driver']; + $_ENV['MAIL_PORT'] = $mail['port']; + $_ENV['MAIL_ENCRYPTION'] = $mail['encryption']; + $_ENV['MAIL_HOST'] = $mail['host']; + $_ENV['MAIL_USERNAME'] = $mail['username']; + $_ENV['MAIL_FROM_NAME'] = $mail['from']['name']; + $_ENV['MAIL_PASSWORD'] = $mail['password']; + $_ENV['MAIL_FROM_ADDRESS'] = $mail['username']; + } + + $config = ''; + foreach ($_ENV as $key => $val) { + $config .= "{$key}={$val}\n"; + } + + $fp = fopen(base_path()."/.env", 'w'); + fwrite($fp, $config); + fclose($fp); + + Session::flash('message', trans('texts.updated_settings')); + return Redirect::to('/settings/system_settings'); + } + private function testDatabase($database) { - $dbType = $database['default']; - + $dbType = 'mysql'; // $database['default']; Config::set('database.default', $dbType); - foreach ($database['connections'][$dbType] as $key => $val) { Config::set("database.connections.{$dbType}.{$key}", $val); } - + try { + DB::reconnect(); $valid = DB::connection()->getDatabaseName() ? true : false; } catch (Exception $e) { return $e->getMessage(); diff --git a/app/Http/routes.php b/app/Http/routes.php index 81bbea787e0f..95213fb93966 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -184,6 +184,7 @@ Route::group(['middleware' => 'auth'], function() { Route::post('credits/bulk', 'CreditController@bulk'); get('/resend_confirmation', 'AccountController@resendConfirmation'); + post('/update_setup', 'AppController@updateSetup'); }); // Route groups for API @@ -278,6 +279,7 @@ if (!defined('CONTACT_EMAIL')) { define('ACCOUNT_TEMPLATES_AND_REMINDERS', 'templates_and_reminders'); define('ACCOUNT_API_TOKENS', 'api_tokens'); define('ACCOUNT_CUSTOMIZE_DESIGN', 'customize_design'); + define('ACCOUNT_SYSTEM_SETTINGS', 'system_settings'); define('ACTION_RESTORE', 'restore'); define('ACTION_ARCHIVE', 'archive'); diff --git a/public/js/built.js b/public/js/built.js index 986575dbb9b2..4127db4eafd4 100644 --- a/public/js/built.js +++ b/public/js/built.js @@ -32231,7 +32231,7 @@ NINJA.parseRegExpLine = function(line, regExp, formatter, groupText) var parts = []; var lastIndex = 0; - while (match = regExp.exec(line)) { + while (match = regExp.exec(line + '\n')) { if (match.index > lastIndex) { parts.push(line.substring(lastIndex, match.index)); } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index a6ddecf599f6..3c62d26bf244 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -893,5 +893,7 @@ return array( 'quote_is_approved' => 'This quote is approved', 'apply_credit' => 'Apply Credit', + 'system_settings' => 'System Settings', + ); diff --git a/resources/views/accounts/nav.blade.php b/resources/views/accounts/nav.blade.php index b0df2e5e4643..ae7f38c2fa8b 100644 --- a/resources/views/accounts/nav.blade.php +++ b/resources/views/accounts/nav.blade.php @@ -16,7 +16,7 @@
{{ trans("texts.{$type}") }} - @if ($type == ADVANCED_SETTINGS && !Utils::isPro()) + @if ($type === ADVANCED_SETTINGS && !Utils::isPro()) {{ strtoupper(trans('texts.pro')) }} @endif
@@ -25,6 +25,10 @@ {{ trans("texts.{$section}") }} @endforeach + @if ($type === ADVANCED_SETTINGS && !Utils::isNinjaProd()) + {{ trans("texts.system_settings") }} + @endif
@endforeach diff --git a/resources/views/accounts/system_settings.blade.php b/resources/views/accounts/system_settings.blade.php new file mode 100644 index 000000000000..e18622a438ae --- /dev/null +++ b/resources/views/accounts/system_settings.blade.php @@ -0,0 +1,36 @@ +@extends('header') + +@section('content') + @parent + + {!! Former::open('/update_setup')->addClass('warn-on-exit') !!} + + @include('accounts.nav', ['selected' => ACCOUNT_SYSTEM_SETTINGS]) + +
+ + {!! Former::open()->rules([ + 'app[url]' => 'required', + //'database[default]' => 'required', + 'database[type][host]' => 'required', + 'database[type][database]' => 'required', + 'database[type][username]' => 'required', + 'database[type][password]' => 'required', + ]) !!} + + + @include('partials.system_settings') + +
+ +
+ {!! Button::success(trans('texts.save'))->submit()->large()->appendIcon(Icon::create('floppy-disk')) !!} +
+ + {!! Former::close() !!} + +@stop + +@section('onReady') + $('#app\\[url\\]').focus(); +@stop \ No newline at end of file diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index eaeef27e628f..3d0a64685fe9 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -198,8 +198,8 @@ {{ trans('texts.quote_number_short') }} {{ trans('texts.client') }} - {{ trans('texts.due_date') }} - {{ trans('texts.balance_due') }} + {{ trans('texts.valid_until') }} + {{ trans('texts.amount') }} @foreach ($upcoming as $invoice) @@ -229,8 +229,8 @@ {{ trans('texts.quote_number_short') }} {{ trans('texts.client') }} - {{ trans('texts.due_date') }} - {{ trans('texts.balance_due') }} + {{ trans('texts.valid_until') }} + {{ trans('texts.amount') }} @foreach ($pastDue as $invoice) diff --git a/resources/views/partials/system_settings.blade.php b/resources/views/partials/system_settings.blade.php new file mode 100644 index 000000000000..533a686dadf2 --- /dev/null +++ b/resources/views/partials/system_settings.blade.php @@ -0,0 +1,112 @@ +
+
+

Application Settings

+
+
+ {!! Former::text('app[url]')->label('URL')->value(isset($_ENV['APP_URL']) ? $_ENV['APP_URL'] : Request::root()) !!} + {!! Former::checkbox('debug') + ->label('Debug') + ->text(trans('texts.enable')) + ->check(config('app.debug')) !!} + +
+
+ +
+
+

Database Connection

+
+
+ {{--- Former::select('database[default]')->label('Driver')->options(['mysql' => 'MySQL', 'pgsql' => 'PostgreSQL', 'sqlite' => 'SQLite']) + ->value(isset($_ENV['DB_TYPE']) ? $_ENV['DB_TYPE'] : 'mysql') ---}} + {!! Former::plaintext('Driver')->value('MySQL') !!} + {!! Former::text('database[type][host]')->label('Host')->value('localhost') + ->value(isset($_ENV['DB_HOST']) ? $_ENV['DB_HOST'] : '') !!} + {!! Former::text('database[type][database]')->label('Database')->value('ninja') + ->value(isset($_ENV['DB_DATABASE']) ? $_ENV['DB_DATABASE'] : '') !!} + {!! Former::text('database[type][username]')->label('Username')->value('ninja') + ->value(isset($_ENV['DB_USERNAME']) ? $_ENV['DB_USERNAME'] : '') !!} + {!! Former::password('database[type][password]')->label('Password')->value('ninja') + ->value(isset($_ENV['DB_PASSWORD']) ? $_ENV['DB_PASSWORD'] : '') !!} + {!! Former::actions( Button::primary('Test connection')->small()->withAttributes(['onclick' => 'testDatabase()']), '  ' ) !!} +
+
+ + @if (!isset($_ENV['POSTMARK_API_TOKEN'])) +
+
+

Email Settings

+
+
+ {!! Former::select('mail[driver]')->label('Driver')->options(['smtp' => 'SMTP', 'mail' => 'Mail', 'sendmail' => 'Sendmail']) + ->value(isset($_ENV['MAIL_DRIVER']) ? $_ENV['MAIL_DRIVER'] : 'smtp') !!} + {!! Former::text('mail[host]')->label('Host') + ->value(isset($_ENV['MAIL_HOST']) ? $_ENV['MAIL_HOST'] : '') !!} + {!! Former::text('mail[port]')->label('Port') + ->value(isset($_ENV['MAIL_PORT']) ? $_ENV['MAIL_PORT'] : '587') !!} + {!! Former::select('mail[encryption]')->label('Encryption')->options(['tls' => 'TLS', 'ssl' => 'SSL']) + ->value(isset($_ENV['MAIL_ENCRYPTION']) ? $_ENV['MAIL_ENCRYPTION'] : 'tls') !!} + {!! Former::text('mail[from][name]')->label('From Name') + ->value(isset($_ENV['MAIL_FROM_NAME']) ? $_ENV['MAIL_FROM_NAME'] : '') !!} + {!! Former::text('mail[username]')->label('Email') + ->value(isset($_ENV['MAIL_USERNAME']) ? $_ENV['MAIL_USERNAME'] : '') !!} + {!! Former::password('mail[password]')->label('Password') + ->value(isset($_ENV['MAIL_PASSWORD']) ? $_ENV['MAIL_PASSWORD'] : '') !!} + {!! Former::actions( Button::primary('Send test email')->small()->withAttributes(['onclick' => 'testMail()']), '  ' ) !!} +
+
+ @endif + + \ No newline at end of file diff --git a/resources/views/setup.blade.php b/resources/views/setup.blade.php index b53288f483dc..49a6c7a03698 100644 --- a/resources/views/setup.blade.php +++ b/resources/views/setup.blade.php @@ -33,8 +33,8 @@
Warning: proc_open must be enabled.
@endif @if (!@fopen(base_path()."/.env", 'a')) -
Warning: Permission denied to write config file -
sudo chown yourname:www-data /path/to/ninja
+
Warning: Permission denied to write .env config file +
sudo chown www-data:www-data /path/to/ninja/.env
@endif If you need help you can either post to our support forum @@ -61,58 +61,7 @@ FLUSH PRIVILEGES; 'terms_checkbox' => 'required' ]) !!} -
-
-

Application Settings

-
-
- {!! Former::text('app[url]')->label('URL')->value(isset($_ENV['APP_URL']) ? $_ENV['APP_URL'] : Request::root()) !!} -
-
- -
-
-

Database Connection

-
-
- {!! Former::select('database[default]')->label('Driver')->options(['mysql' => 'MySQL', 'pgsql' => 'PostgreSQL', 'sqlite' => 'SQLite']) - ->value(isset($_ENV['DB_TYPE']) ? $_ENV['DB_TYPE'] : 'mysql') !!} - {!! Former::text('database[type][host]')->label('Host')->value('localhost') - ->value(isset($_ENV['DB_HOST']) ? $_ENV['DB_HOST'] : '') !!} - {!! Former::text('database[type][database]')->label('Database')->value('ninja') - ->value(isset($_ENV['DB_DATABASE']) ? $_ENV['DB_DATABASE'] : '') !!} - {!! Former::text('database[type][username]')->label('Username')->value('ninja') - ->value(isset($_ENV['DB_USERNAME']) ? $_ENV['DB_USERNAME'] : '') !!} - {!! Former::password('database[type][password]')->label('Password')->value('ninja') - ->value(isset($_ENV['DB_PASSWORD']) ? $_ENV['DB_PASSWORD'] : '') !!} - {!! Former::actions( Button::primary('Test connection')->small()->withAttributes(['onclick' => 'testDatabase()']), '  ' ) !!} -
-
- - -
-
-

Email Settings

-
-
- {!! Former::select('mail[driver]')->label('Driver')->options(['smtp' => 'SMTP', 'mail' => 'Mail', 'sendmail' => 'Sendmail']) - ->value(isset($_ENV['MAIL_DRIVER']) ? $_ENV['MAIL_DRIVER'] : 'smtp') !!} - {!! Former::text('mail[host]')->label('Host') - ->value(isset($_ENV['MAIL_HOST']) ? $_ENV['MAIL_HOST'] : '') !!} - {!! Former::text('mail[port]')->label('Port') - ->value(isset($_ENV['MAIL_PORT']) ? $_ENV['MAIL_PORT'] : '587') !!} - {!! Former::select('mail[encryption]')->label('Encryption')->options(['tls' => 'TLS', 'ssl' => 'SSL']) - ->value(isset($_ENV['MAIL_ENCRYPTION']) ? $_ENV['MAIL_ENCRYPTION'] : 'tls') !!} - {!! Former::text('mail[from][name]')->label('From Name') - ->value(isset($_ENV['MAIL_FROM_NAME']) ? $_ENV['MAIL_FROM_NAME'] : '') !!} - {!! Former::text('mail[username]')->label('Email') - ->value(isset($_ENV['MAIL_USERNAME']) ? $_ENV['MAIL_USERNAME'] : '') !!} - {!! Former::password('mail[password]')->label('Password') - ->value(isset($_ENV['MAIL_PASSWORD']) ? $_ENV['MAIL_PASSWORD'] : '') !!} - {!! Former::actions( Button::primary('Send test email')->small()->withAttributes(['onclick' => 'testMail()']), '  ' ) !!} -
-
- + @include('partials.system_settings')
@@ -133,87 +82,5 @@ FLUSH PRIVILEGES;
- - \ No newline at end of file