Skip to content

Commit

Permalink
Updated Companies for #3059
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed Dec 15, 2016
1 parent 6c8e932 commit c6ab34f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 36 deletions.
34 changes: 17 additions & 17 deletions app/Http/Controllers/CompaniesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Lang;
use Redirect;
use View;
use Illuminate\Http\Request;

/**
* This controller handles all actions related to Companies for
Expand All @@ -24,7 +25,7 @@ final class CompaniesController extends Controller
* @since [v1.8]
* @return View
*/
public function getIndex()
public function index()
{
return View::make('companies/index')->with('companies', Company::all());
}
Expand All @@ -36,7 +37,7 @@ public function getIndex()
* @since [v1.8]
* @return View
*/
public function getCreate()
public function create()
{
return View::make('companies/edit')->with('item', new Company);
}
Expand All @@ -48,14 +49,13 @@ public function getCreate()
* @since [v1.8]
* @return Redirect
*/
public function postCreate()
public function store(Request $request)
{
$company = new Company;

$company->name = e(Input::get('name'));
$company->name = e($request->input('name'));

if ($company->save()) {
return redirect()->to('admin/settings/companies')
return redirect()->route('companies.index')
->with('success', trans('admin/companies/message.create.success'));
} else {
return redirect()->back()->withInput()->withErrors($company->getErrors());
Expand All @@ -72,10 +72,10 @@ public function postCreate()
* @param int $companyId
* @return View
*/
public function getEdit($companyId)
public function edit($companyId)
{
if (is_null($item = Company::find($companyId))) {
return redirect()->to('admin/settings/companies')
return redirect()->route('companies.index')
->with('error', trans('admin/companies/message.does_not_exist'));
} else {
return View::make('companies/edit')->with('item', $item);
Expand All @@ -90,20 +90,20 @@ public function getEdit($companyId)
* @param int $companyId
* @return Redirect
*/
public function postEdit($companyId)
public function update(Request $request, $companyId)
{
if (is_null($company = Company::find($companyId))) {
return redirect()->to('admin/settings/companies')->with('error', trans('admin/companies/message.does_not_exist'));
return redirect()->route('companies.index')->with('error', trans('admin/companies/message.does_not_exist'));
} else {


$company->name = e(Input::get('name'));
$company->name = e($request->input('name'));

if ($company->save()) {
return redirect()->to('admin/settings/companies')
return redirect()->route('companies.index')
->with('success', trans('admin/companies/message.update.success'));
} else {
return redirect()->to("admin/settings/companies/$companyId/edit")
return redirect()->route('companies.edit', ['company' => $companyId])
->with('error', trans('admin/companies/message.update.error'));
}

Expand All @@ -118,23 +118,23 @@ public function postEdit($companyId)
* @param int $companyId
* @return Redirect
*/
public function postDelete($companyId)
public function destroy($companyId)
{
if (is_null($company = Company::find($companyId))) {
return redirect()->to('admin/settings/companies')
return redirect()->route('companies.index')
->with('error', trans('admin/companies/message.not_found'));
} else {
try {
$company->delete();
return redirect()->to('admin/settings/companies')
return redirect()->route('companies.index')
->with('success', trans('admin/companies/message.delete.success'));
} catch (\Illuminate\Database\QueryException $exception) {
/*
* NOTE: This happens when there's a foreign key constraint violation
* For example when rows in other tables are referencing this company
*/
if ($exception->getCode() == 23000) {
return redirect()->to('admin/settings/companies')
return redirect()->route('companies.index')
->with('error', trans('admin/companies/message.assoc_users'));
} else {
throw $exception;
Expand Down
3 changes: 2 additions & 1 deletion resources/views/companies/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
'createText' => trans('admin/companies/table.create') ,
'updateText' => trans('admin/companies/table.update'),
'helpTitle' => trans('admin/companies/general.about_companies_title'),
'helpText' => trans('admin/companies/general.about_companies_text')
'helpText' => trans('admin/companies/general.about_companies_text'),
'formAction' => ($item) ? route('companies.update', ['company' => $item->id]) : route('companies.store'),
])

{{-- Page content --}}
Expand Down
6 changes: 3 additions & 3 deletions resources/views/companies/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@stop

@section('header_right')
<a href="{{ route('create/company') }}" class="btn btn-primary pull-right">
<a href="{{ route('companies.create') }}" class="btn btn-primary pull-right">
{{ trans('general.create') }}</a>
@stop

Expand All @@ -32,11 +32,11 @@
<td>{{ $company->id }}</td>
<td>{{ $company->name }}</td>
<td>
<form method="POST" action="{{ route('delete/company', $company->id) }}" role="form">
<form method="POST" action="{{ route('companies.destroy', $company->id) }}" role="form">

<input type="hidden" name="_token" value="{{ csrf_token() }}" />

<a href="{{ route('update/company', $company->id) }}" class="btn btn-sm btn-warning"
<a href="{{ route('companies.edit', $company->id) }}" class="btn btn-sm btn-warning"
title="{{ trans('button.edit') }}">
<i class="fa fa-pencil icon-white"></i>
</a>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/layouts/default.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@
</a>
<ul class="dropdown-menu">
<li {!! (Request::is('settings/companies*') ? ' class="active"' : '') !!}>
<a href="{{ URL::to('admin/settings/companies') }}">
<a href="{{ route('companies.index') }}">
<i class="fa fa-building-o fa-fw"></i> @lang('general.companies')
</a>
</li>
Expand Down
15 changes: 4 additions & 11 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
'parameters' => ['customfield' => 'field_id', 'fieldset' => 'fieldset_id']
]);

Route::resource('companies', 'CompaniesController', [
'parameters' => ['company' => 'company_id']
]);



/*
Expand Down Expand Up @@ -141,17 +145,6 @@
Route::get('/', [ 'as' => 'settings/backups', 'uses' => 'SettingsController@getBackups' ]);
});

# Companies
Route::group([ 'prefix' => 'companies' ], function () {

Route::get('{companyId}/edit', ['as' => 'update/company', 'uses' => 'CompaniesController@getEdit']);
Route::get('create', ['as' => 'create/company', 'uses' => 'CompaniesController@getCreate']);
Route::get('/', ['as' => 'companies', 'uses' => 'CompaniesController@getIndex']);

Route::post('{companyId}/delete', ['as' => 'delete/company', 'uses' => 'CompaniesController@postDelete']);
Route::post('{companyId}/edit', 'CompaniesController@postEdit');
Route::post('create', 'CompaniesController@postCreate');
});



Expand Down
6 changes: 3 additions & 3 deletions tests/functional/CompaniesCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public function tryToTest(FunctionalTester $I)
{
$I->wantTo('Test Company Creation');
$I->lookForwardTo('seeing it load without errors');
$I->amOnPage(route('create/company'));
$I->amOnPage(route('companies.create'));
$I->seeInTitle('Create Company');
$I->see('Create Company', 'h1.pull-left');
}

public function failsEmptyValidation(FunctionalTester $I)
{
$I->wantTo("Test Validation Fails with blank elements");
$I->amOnPage(route('create/company'));
$I->amOnPage(route('companies.create'));
$I->click('Save');
$I->seeElement('.alert-danger');
$I->see('The name field is required.', '.alert-msg');
Expand All @@ -37,7 +37,7 @@ public function passesCorrectValidation(FunctionalTester $I)
'name' => $company->name
];
$I->wantTo("Test Validation Succeeds");
$I->amOnPage(route('create/company'));
$I->amOnPage(route('companies.create'));
$I->fillField('name', 'TestCompany');
$I->submitForm('form#create-form', $values);
$I->seeRecord('companies', $values);
Expand Down

0 comments on commit c6ab34f

Please sign in to comment.