2016-03-25 15:50:08 -07:00
|
|
|
<?php
|
2016-04-07 13:21:09 -07:00
|
|
|
namespace App\Http\Controllers;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
use App\Models\Company;
|
|
|
|
use Input;
|
|
|
|
use Lang;
|
|
|
|
use Redirect;
|
|
|
|
use View;
|
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
|
|
|
* This controller handles all actions related to Companies for
|
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v1.0
|
|
|
|
*/
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
final class CompaniesController extends Controller
|
|
|
|
{
|
2016-03-28 22:51:49 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns view to display listing of companies.
|
|
|
|
*
|
|
|
|
* @author [Abdullah Alansari] [<ahimta@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getIndex()
|
|
|
|
{
|
|
|
|
return View::make('companies/index')->with('companies', Company::all());
|
|
|
|
}
|
|
|
|
|
2016-03-28 22:51:49 -07:00
|
|
|
/**
|
|
|
|
* Returns view to create a new company.
|
|
|
|
*
|
|
|
|
* @author [Abdullah Alansari] [<ahimta@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getCreate()
|
|
|
|
{
|
|
|
|
return View::make('companies/edit')->with('company', new Company);
|
|
|
|
}
|
|
|
|
|
2016-03-28 22:51:49 -07:00
|
|
|
/**
|
|
|
|
* Save data from new company form.
|
|
|
|
*
|
|
|
|
* @author [Abdullah Alansari] [<ahimta@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function postCreate()
|
|
|
|
{
|
|
|
|
$company = new Company;
|
|
|
|
|
|
|
|
$company->name = e(Input::get('name'));
|
|
|
|
|
|
|
|
if ($company->save()) {
|
|
|
|
return Redirect::to('admin/settings/companies')
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('success', trans('admin/companies/message.create.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
|
|
|
return Redirect::back()->withInput()->withErrors($company->getErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-28 22:51:49 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return form to edit existing company.
|
|
|
|
*
|
|
|
|
* @author [Abdullah Alansari] [<ahimta@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @param int $companyId
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getEdit($companyId)
|
|
|
|
{
|
|
|
|
if (is_null($company = Company::find($companyId))) {
|
|
|
|
return Redirect::to('admin/settings/companies')
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('error', trans('admin/companies/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
|
|
|
return View::make('companies/edit')->with('company', $company);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 22:51:49 -07:00
|
|
|
/**
|
|
|
|
* Save data from edit company form.
|
|
|
|
*
|
|
|
|
* @author [Abdullah Alansari] [<ahimta@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @param int $companyId
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function postEdit($companyId)
|
|
|
|
{
|
|
|
|
if (is_null($company = Company::find($companyId))) {
|
2016-04-07 13:39:35 -07:00
|
|
|
return Redirect::to('admin/settings/companies')->with('error', trans('admin/companies/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
$company->name = e(Input::get('name'));
|
|
|
|
|
|
|
|
if ($company->save()) {
|
|
|
|
return Redirect::to('admin/settings/companies')
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('success', trans('admin/companies/message.update.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
|
|
|
return Redirect::to("admin/settings/companies/$companyId/edit")
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('error', trans('admin/companies/message.update.error'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 22:51:49 -07:00
|
|
|
/**
|
|
|
|
* Delete company
|
|
|
|
*
|
|
|
|
* @author [Abdullah Alansari] [<ahimta@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @param int $companyId
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function postDelete($companyId)
|
|
|
|
{
|
|
|
|
if (is_null($company = Company::find($companyId))) {
|
|
|
|
return Redirect::to('admin/settings/companies')
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('error', trans('admin/companies/message.not_found'));
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$company->delete();
|
|
|
|
return Redirect::to('admin/settings/companies')
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('success', trans('admin/companies/message.delete.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
} 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')
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('error', trans('admin/companies/message.assoc_users'));
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|