Implemented UpdateAccountRequest

This commit is contained in:
Hillel Coren 2016-02-03 14:41:40 +02:00
parent b65fc1363e
commit 021f195c45
6 changed files with 121 additions and 98 deletions

View File

@ -19,6 +19,8 @@ use App\Ninja\Transformers\UserAccountTransformer;
use App\Http\Controllers\BaseAPIController; use App\Http\Controllers\BaseAPIController;
use Swagger\Annotations as SWG; use Swagger\Annotations as SWG;
use App\Http\Requests\UpdateAccountRequest;
class AccountApiController extends BaseAPIController class AccountApiController extends BaseAPIController
{ {
protected $accountRepo; protected $accountRepo;
@ -103,41 +105,14 @@ class AccountApiController extends BaseAPIController
return $this->processLogin($request); return $this->processLogin($request);
} }
public function update(Request $request) public function update(UpdateAccountRequest $request)
{ {
$rules = array( $account = Auth::user()->account;
'name' => 'required', $this->accountRepo->save($request->input(), $account);
);
$validator = Validator::make(Input::all(), $rules); $transformer = new AccountTransformer(null, $request->serializer);
$account = $this->createItem($account, $transformer, 'account');
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);
}
return $this->response($account);
} }
} }

View File

@ -29,6 +29,8 @@ use App\Events\UserLoggedIn;
use App\Events\UserSettingsChanged; use App\Events\UserSettingsChanged;
use App\Services\AuthService; use App\Services\AuthService;
use App\Http\Requests\UpdateAccountRequest;
class AccountController extends BaseController class AccountController extends BaseController
{ {
protected $accountRepo; protected $accountRepo;
@ -766,80 +768,52 @@ class AccountController extends BaseController
return Redirect::to('settings/'.ACCOUNT_NOTIFICATIONS); return Redirect::to('settings/'.ACCOUNT_NOTIFICATIONS);
} }
private function saveDetails() public function updateDetails(UpdateAccountRequest $request)
{ {
$rules = array( $account = Auth::user()->account;
'name' => 'required', $this->accountRepo->save($request->input(), $account);
'logo' => 'sometimes|max:'.MAX_LOGO_FILE_SIZE.'|mimes:jpeg,gif,png',
);
$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()) { $mimeType = $file->getMimeType();
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();
/* Logo image file */ if ($mimeType == 'image/jpeg') {
if ($file = Input::file('logo')) { $path = 'logo/'.$account->account_key.'.jpg';
$path = Input::file('logo')->getRealPath(); $file->move('logo/', $account->account_key.'.jpg');
File::delete('logo/'.$account->account_key.'.jpg'); } elseif ($mimeType == 'image/png') {
File::delete('logo/'.$account->account_key.'.png'); $path = 'logo/'.$account->account_key.'.png';
$file->move('logo/', $account->account_key.'.png');
$mimeType = $file->getMimeType(); } else {
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 (extension_loaded('fileinfo')) { if (extension_loaded('fileinfo')) {
$img = Image::make($path); $image = Image::make($path);
$img->interlace(false); $image->resize(200, 120, function ($constraint) {
$img->save(); $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()); // make sure image isn't interlaced
if (extension_loaded('fileinfo')) {
Session::flash('message', trans('texts.updated_settings')); $img = Image::make($path);
$img->interlace(false);
return Redirect::to('settings/'.ACCOUNT_COMPANY_DETAILS); $img->save();
}
} }
event(new UserSettingsChanged());
Session::flash('message', trans('texts.updated_settings'));
return Redirect::to('settings/'.ACCOUNT_COMPANY_DETAILS);
} }
private function saveUserDetails() private function saveUserDetails()

View File

@ -0,0 +1,31 @@
<?php namespace app\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Validation\Factory;
class UpdateAccountRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'logo' => 'sometimes|max:'.MAX_LOGO_FILE_SIZE.'|mimes:jpeg,gif,png',
];
}
}
//

View File

@ -121,6 +121,7 @@ Route::group(['middleware' => 'auth'], function() {
Route::post('settings/charts_and_reports', 'ReportController@showReports'); Route::post('settings/charts_and_reports', 'ReportController@showReports');
Route::post('settings/cancel_account', 'AccountController@cancelAccount'); Route::post('settings/cancel_account', 'AccountController@cancelAccount');
Route::post('settings/company_details', 'AccountController@updateDetails');
Route::get('settings/{section?}', 'AccountController@showSection'); Route::get('settings/{section?}', 'AccountController@showSection');
Route::post('settings/{section?}', 'AccountController@doSection'); Route::post('settings/{section?}', 'AccountController@doSection');

View File

@ -21,6 +21,24 @@ class Account extends Eloquent
protected $dates = ['deleted_at']; protected $dates = ['deleted_at'];
protected $hidden = ['ip']; 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 = [ public static $basicSettings = [
ACCOUNT_COMPANY_DETAILS, ACCOUNT_COMPANY_DETAILS,
ACCOUNT_USER_DETAILS, ACCOUNT_USER_DETAILS,
@ -140,6 +158,24 @@ class Account extends Eloquent
return $this->belongsTo('App\Models\TaxRate'); 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) public function isGatewayConfigured($gatewayId = 0)
{ {
$this->load('account_gateways'); $this->load('account_gateways');

View File

@ -516,4 +516,10 @@ class AccountRepository
return $userAccount ? $userAccount->id : false; return $userAccount ? $userAccount->id : false;
} }
public function save($data, $account)
{
$account->fill($data);
$account->save();
}
} }