From 021f195c45613763fbe352a31fe89c9f7b886f02 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 3 Feb 2016 14:41:40 +0200 Subject: [PATCH] Implemented UpdateAccountRequest --- app/Http/Controllers/AccountApiController.php | 41 ++----- app/Http/Controllers/AccountController.php | 104 +++++++----------- app/Http/Requests/UpdateAccountRequest.php | 31 ++++++ app/Http/routes.php | 1 + app/Models/Account.php | 36 ++++++ app/Ninja/Repositories/AccountRepository.php | 6 + 6 files changed, 121 insertions(+), 98 deletions(-) create mode 100644 app/Http/Requests/UpdateAccountRequest.php diff --git a/app/Http/Controllers/AccountApiController.php b/app/Http/Controllers/AccountApiController.php index 3f5a01d0cc03..fc909461353f 100644 --- a/app/Http/Controllers/AccountApiController.php +++ b/app/Http/Controllers/AccountApiController.php @@ -19,6 +19,8 @@ use App\Ninja\Transformers\UserAccountTransformer; use App\Http\Controllers\BaseAPIController; use Swagger\Annotations as SWG; +use App\Http\Requests\UpdateAccountRequest; + class AccountApiController extends BaseAPIController { protected $accountRepo; @@ -103,41 +105,14 @@ class AccountApiController extends BaseAPIController return $this->processLogin($request); } - public function update(Request $request) + public function update(UpdateAccountRequest $request) { - $rules = array( - 'name' => 'required', - ); + $account = Auth::user()->account; + $this->accountRepo->save($request->input(), $account); - $validator = Validator::make(Input::all(), $rules); - - if ($validator->fails()) { - $data = $validator->messages(); - return $this->errorResponse($data); - } else { - $account = Auth::user()->account; - $account->name = trim(Input::get('name')); - $account->id_number = trim(Input::get('id_number')); - $account->vat_number = trim(Input::get('vat_number')); - $account->work_email = trim(Input::get('work_email')); - $account->website = trim(Input::get('website')); - $account->work_phone = trim(Input::get('work_phone')); - $account->address1 = trim(Input::get('address1')); - $account->address2 = trim(Input::get('address2')); - $account->city = trim(Input::get('city')); - $account->state = trim(Input::get('state')); - $account->postal_code = trim(Input::get('postal_code')); - $account->country_id = Input::get('country_id') ? Input::get('country_id') : null; - $account->size_id = Input::get('size_id') ? Input::get('size_id') : null; - $account->industry_id = Input::get('industry_id') ? Input::get('industry_id') : null; - $account->email_footer = Input::get('email_footer'); - $account->save(); - - $transformer = new AccountTransformer(null, $request->serializer); - $account = $this->createItem($account, $transformer, 'account'); - - return $this->response($account); - } + $transformer = new AccountTransformer(null, $request->serializer); + $account = $this->createItem($account, $transformer, 'account'); + return $this->response($account); } } diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 02921782e035..454b9d357c58 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -29,6 +29,8 @@ use App\Events\UserLoggedIn; use App\Events\UserSettingsChanged; use App\Services\AuthService; +use App\Http\Requests\UpdateAccountRequest; + class AccountController extends BaseController { protected $accountRepo; @@ -766,80 +768,52 @@ class AccountController extends BaseController return Redirect::to('settings/'.ACCOUNT_NOTIFICATIONS); } - private function saveDetails() + public function updateDetails(UpdateAccountRequest $request) { - $rules = array( - 'name' => 'required', - 'logo' => 'sometimes|max:'.MAX_LOGO_FILE_SIZE.'|mimes:jpeg,gif,png', - ); + $account = Auth::user()->account; + $this->accountRepo->save($request->input(), $account); - $validator = Validator::make(Input::all(), $rules); + /* Logo image file */ + if ($file = Input::file('logo')) { + $path = Input::file('logo')->getRealPath(); + File::delete('logo/'.$account->account_key.'.jpg'); + File::delete('logo/'.$account->account_key.'.png'); - if ($validator->fails()) { - return Redirect::to('settings/'.ACCOUNT_COMPANY_DETAILS) - ->withErrors($validator) - ->withInput(); - } else { - $account = Auth::user()->account; - $account->name = trim(Input::get('name')); - $account->id_number = trim(Input::get('id_number')); - $account->vat_number = trim(Input::get('vat_number')); - $account->work_email = trim(Input::get('work_email')); - $account->website = trim(Input::get('website')); - $account->work_phone = trim(Input::get('work_phone')); - $account->address1 = trim(Input::get('address1')); - $account->address2 = trim(Input::get('address2')); - $account->city = trim(Input::get('city')); - $account->state = trim(Input::get('state')); - $account->postal_code = trim(Input::get('postal_code')); - $account->country_id = Input::get('country_id') ? Input::get('country_id') : null; - $account->size_id = Input::get('size_id') ? Input::get('size_id') : null; - $account->industry_id = Input::get('industry_id') ? Input::get('industry_id') : null; - $account->email_footer = Input::get('email_footer'); - $account->save(); + $mimeType = $file->getMimeType(); - /* Logo image file */ - if ($file = Input::file('logo')) { - $path = Input::file('logo')->getRealPath(); - File::delete('logo/'.$account->account_key.'.jpg'); - File::delete('logo/'.$account->account_key.'.png'); - - $mimeType = $file->getMimeType(); - - if ($mimeType == 'image/jpeg') { - $path = 'logo/'.$account->account_key.'.jpg'; - $file->move('logo/', $account->account_key.'.jpg'); - } elseif ($mimeType == 'image/png') { - $path = 'logo/'.$account->account_key.'.png'; - $file->move('logo/', $account->account_key.'.png'); - } else { - if (extension_loaded('fileinfo')) { - $image = Image::make($path); - $image->resize(200, 120, function ($constraint) { - $constraint->aspectRatio(); - }); - $path = 'logo/'.$account->account_key.'.jpg'; - Image::canvas($image->width(), $image->height(), '#FFFFFF') - ->insert($image)->save($path); - } else { - Session::flash('warning', 'Warning: To support gifs the fileinfo PHP extension needs to be enabled.'); - } - } - - // make sure image isn't interlaced + if ($mimeType == 'image/jpeg') { + $path = 'logo/'.$account->account_key.'.jpg'; + $file->move('logo/', $account->account_key.'.jpg'); + } elseif ($mimeType == 'image/png') { + $path = 'logo/'.$account->account_key.'.png'; + $file->move('logo/', $account->account_key.'.png'); + } else { if (extension_loaded('fileinfo')) { - $img = Image::make($path); - $img->interlace(false); - $img->save(); + $image = Image::make($path); + $image->resize(200, 120, function ($constraint) { + $constraint->aspectRatio(); + }); + $path = 'logo/'.$account->account_key.'.jpg'; + Image::canvas($image->width(), $image->height(), '#FFFFFF') + ->insert($image)->save($path); + } else { + Session::flash('warning', 'Warning: To support gifs the fileinfo PHP extension needs to be enabled.'); } } - event(new UserSettingsChanged()); - - Session::flash('message', trans('texts.updated_settings')); - - return Redirect::to('settings/'.ACCOUNT_COMPANY_DETAILS); + // make sure image isn't interlaced + if (extension_loaded('fileinfo')) { + $img = Image::make($path); + $img->interlace(false); + $img->save(); + } } + + event(new UserSettingsChanged()); + + Session::flash('message', trans('texts.updated_settings')); + + return Redirect::to('settings/'.ACCOUNT_COMPANY_DETAILS); } private function saveUserDetails() diff --git a/app/Http/Requests/UpdateAccountRequest.php b/app/Http/Requests/UpdateAccountRequest.php new file mode 100644 index 000000000000..854531d4a944 --- /dev/null +++ b/app/Http/Requests/UpdateAccountRequest.php @@ -0,0 +1,31 @@ + 'required', + 'logo' => 'sometimes|max:'.MAX_LOGO_FILE_SIZE.'|mimes:jpeg,gif,png', + ]; + } +} +// \ No newline at end of file diff --git a/app/Http/routes.php b/app/Http/routes.php index e9e3afc81de0..a314fa07dab5 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -121,6 +121,7 @@ Route::group(['middleware' => 'auth'], function() { Route::post('settings/charts_and_reports', 'ReportController@showReports'); Route::post('settings/cancel_account', 'AccountController@cancelAccount'); + Route::post('settings/company_details', 'AccountController@updateDetails'); Route::get('settings/{section?}', 'AccountController@showSection'); Route::post('settings/{section?}', 'AccountController@doSection'); diff --git a/app/Models/Account.php b/app/Models/Account.php index 08c10017c0a0..7893267a348b 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -21,6 +21,24 @@ class Account extends Eloquent protected $dates = ['deleted_at']; protected $hidden = ['ip']; + protected $fillable = [ + 'name', + 'id_number', + 'vat_number', + 'work_email', + 'website', + 'work_phone', + 'address1', + 'address2', + 'city', + 'state', + 'postal_code', + 'country_id', + 'size_id', + 'industry_id', + 'email_footer', + ]; + public static $basicSettings = [ ACCOUNT_COMPANY_DETAILS, ACCOUNT_USER_DETAILS, @@ -140,6 +158,24 @@ class Account extends Eloquent return $this->belongsTo('App\Models\TaxRate'); } + + public function setIndustryIdAttribute($value) + { + $this->attributes['industry_id'] = $value ?: null; + } + + public function setCountryIdAttribute($value) + { + $this->attributes['country_id'] = $value ?: null; + } + + public function setSizeIdAttribute($value) + { + $this->attributes['size_id'] = $value ?: null; + } + + + public function isGatewayConfigured($gatewayId = 0) { $this->load('account_gateways'); diff --git a/app/Ninja/Repositories/AccountRepository.php b/app/Ninja/Repositories/AccountRepository.php index 1fd6c7117dcc..af708fcb320f 100644 --- a/app/Ninja/Repositories/AccountRepository.php +++ b/app/Ninja/Repositories/AccountRepository.php @@ -516,4 +516,10 @@ class AccountRepository return $userAccount ? $userAccount->id : false; } + + public function save($data, $account) + { + $account->fill($data); + $account->save(); + } }