mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Implemented UpdateAccountRequest
This commit is contained in:
parent
b65fc1363e
commit
021f195c45
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
|
31
app/Http/Requests/UpdateAccountRequest.php
Normal file
31
app/Http/Requests/UpdateAccountRequest.php
Normal 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',
|
||||
];
|
||||
}
|
||||
}
|
||||
//
|
@ -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');
|
||||
|
||||
|
@ -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');
|
||||
|
@ -516,4 +516,10 @@ class AccountRepository
|
||||
|
||||
return $userAccount ? $userAccount->id : false;
|
||||
}
|
||||
|
||||
public function save($data, $account)
|
||||
{
|
||||
$account->fill($data);
|
||||
$account->save();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user