2016-03-25 15:50:08 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
use App\Helpers\Helper;
|
|
|
|
use App\Models\Category as Category;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use Auth;
|
|
|
|
use DB;
|
|
|
|
use Input;
|
|
|
|
use Lang;
|
|
|
|
use Redirect;
|
|
|
|
use Str;
|
|
|
|
use View;
|
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
|
|
|
* This class controls all actions related to Categories for
|
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v1.0
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
class CategoriesController extends Controller
|
|
|
|
{
|
|
|
|
|
2016-03-25 18:07:12 -07:00
|
|
|
/**
|
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the categories listing, which is generated in getDatatable.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see CategoriesController::getDatatable() method that generates the JSON response
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getIndex()
|
|
|
|
{
|
|
|
|
// Show the page
|
|
|
|
return View::make('categories/index');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-03-25 18:07:12 -07:00
|
|
|
* Returns a form view to create a new category.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see CategoriesController::postCreate() method that stores the data
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getCreate()
|
|
|
|
{
|
|
|
|
// Show the page
|
|
|
|
$category_types= Helper::categoryTypeList();
|
|
|
|
return View::make('categories/edit')->with('category', new Category)
|
|
|
|
->with('category_types', $category_types);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-03-25 18:07:12 -07:00
|
|
|
* Validates and stores the new category data.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see CategoriesController::getCreate() method that makes the form.
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function postCreate()
|
|
|
|
{
|
|
|
|
|
|
|
|
// create a new model instance
|
|
|
|
$category = new Category();
|
|
|
|
|
|
|
|
// Update the category data
|
|
|
|
$category->name = e(Input::get('name'));
|
|
|
|
$category->category_type = e(Input::get('category_type'));
|
|
|
|
$category->eula_text = e(Input::get('eula_text'));
|
|
|
|
$category->use_default_eula = e(Input::get('use_default_eula', '0'));
|
|
|
|
$category->require_acceptance = e(Input::get('require_acceptance', '0'));
|
|
|
|
$category->checkin_email = e(Input::get('checkin_email', '0'));
|
|
|
|
$category->user_id = Auth::user()->id;
|
|
|
|
|
|
|
|
if ($category->save()) {
|
|
|
|
// Redirect to the new category page
|
2016-04-07 13:39:35 -07:00
|
|
|
return Redirect::to("admin/settings/categories")->with('success', trans('admin/categories/message.create.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// The given data did not pass validation
|
|
|
|
return Redirect::back()->withInput()->withErrors($category->getErrors());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the category create page
|
2016-04-07 13:39:35 -07:00
|
|
|
return Redirect::to('admin/settings/categories/create')->with('error', trans('admin/categories/message.create.error'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-25 18:07:12 -07:00
|
|
|
* Returns a view that makes a form to update a category.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see CategoriesController::postEdit() method saves the data
|
|
|
|
* @param int $categoryId
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getEdit($categoryId = null)
|
|
|
|
{
|
|
|
|
// Check if the category exists
|
|
|
|
if (is_null($category = Category::find($categoryId))) {
|
|
|
|
// Redirect to the blogs management page
|
2016-04-07 13:39:35 -07:00
|
|
|
return Redirect::to('admin/settings/categories')->with('error', trans('admin/categories/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show the page
|
|
|
|
//$category_options = array('' => 'Top Level') + Category::lists('name', 'id');
|
|
|
|
|
|
|
|
$category_options = array('' => 'Top Level') + DB::table('categories')->where('id', '!=', $categoryId)->lists('name', 'id');
|
|
|
|
$category_types= Helper::categoryTypeList();
|
|
|
|
|
|
|
|
return View::make('categories/edit', compact('category'))
|
|
|
|
->with('category_options', $category_options)
|
|
|
|
->with('category_types', $category_types);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-03-25 18:07:12 -07:00
|
|
|
* Validates and stores the updated category data.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see CategoriesController::getEdit() method that makes the form.
|
|
|
|
* @param int $categoryId
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function postEdit($categoryId = null)
|
|
|
|
{
|
|
|
|
// Check if the blog post exists
|
|
|
|
if (is_null($category = Category::find($categoryId))) {
|
|
|
|
// Redirect to the blogs management page
|
2016-04-07 13:39:35 -07:00
|
|
|
return Redirect::to('admin/categories')->with('error', trans('admin/categories/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the category data
|
|
|
|
$category->name = e(Input::get('name'));
|
|
|
|
$category->category_type = e(Input::get('category_type'));
|
|
|
|
$category->eula_text = e(Input::get('eula_text'));
|
|
|
|
$category->use_default_eula = e(Input::get('use_default_eula', '0'));
|
|
|
|
$category->require_acceptance = e(Input::get('require_acceptance', '0'));
|
|
|
|
$category->checkin_email = e(Input::get('checkin_email', '0'));
|
|
|
|
|
|
|
|
if ($category->save()) {
|
|
|
|
// Redirect to the new category page
|
2016-04-07 13:39:35 -07:00
|
|
|
return Redirect::to("admin/settings/categories")->with('success', trans('admin/categories/message.update.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
} // attempt validation
|
|
|
|
else {
|
|
|
|
// The given data did not pass validation
|
|
|
|
return Redirect::back()->withInput()->withErrors($category->getErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the category management page
|
2016-04-07 13:39:35 -07:00
|
|
|
return Redirect::back()->with('error', trans('admin/categories/message.update.error'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-25 18:07:12 -07:00
|
|
|
* Validates and marks a category as deleted.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $categoryId
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getDelete($categoryId)
|
|
|
|
{
|
|
|
|
// Check if the category exists
|
|
|
|
if (is_null($category = Category::find($categoryId))) {
|
|
|
|
// Redirect to the blogs management page
|
2016-04-07 13:39:35 -07:00
|
|
|
return Redirect::to('admin/settings/categories')->with('error', trans('admin/categories/message.not_found'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($category->has_models() > 0) {
|
2016-04-07 17:15:30 -07:00
|
|
|
return Redirect::to('admin/settings/categories')->with('error', trans('admin/categories/message.assoc_models'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-04-07 17:15:30 -07:00
|
|
|
} elseif ($category->accessories()->count() > 0) {
|
|
|
|
return Redirect::to('admin/settings/categories')->with('error', trans('admin/categories/message.assoc_accessories'));
|
|
|
|
|
|
|
|
} elseif ($category->consumables()->count() > 0) {
|
|
|
|
return Redirect::to('admin/settings/categories')->with('error', trans('admin/categories/message.assoc_consumables'));
|
|
|
|
|
|
|
|
} elseif ($category->components()->count() > 0) {
|
|
|
|
return Redirect::to('admin/settings/categories')->with('error', trans('admin/categories/message.assoc_components'));
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
|
|
|
|
|
|
|
$category->delete();
|
|
|
|
|
|
|
|
// Redirect to the locations management page
|
2016-04-07 13:39:35 -07:00
|
|
|
return Redirect::to('admin/settings/categories')->with('success', trans('admin/categories/message.delete.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-03-25 18:07:12 -07:00
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the categories detail view, which is generated in getDataView.
|
2016-03-25 01:18:05 -07:00
|
|
|
*
|
2016-03-25 18:07:12 -07:00
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see CategoriesController::getDataView() method that generates the JSON response
|
|
|
|
* @param int $categoryId
|
|
|
|
* @since [v1.8]
|
2016-03-25 01:18:05 -07:00
|
|
|
* @return View
|
2016-03-25 18:07:12 -07:00
|
|
|
*/
|
|
|
|
public function getView($categoryId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-03-25 18:07:12 -07:00
|
|
|
$category = Category::find($categoryId);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
if (isset($category->id)) {
|
|
|
|
return View::make('categories/view', compact('category'));
|
|
|
|
} else {
|
|
|
|
// Prepare the error message
|
2016-04-07 13:39:35 -07:00
|
|
|
$error = trans('admin/categories/message.does_not_exist', compact('id'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
// Redirect to the user management page
|
|
|
|
return Redirect::route('categories')->with('error', $error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-25 18:07:12 -07:00
|
|
|
/**
|
|
|
|
* Returns a JSON response with the data to populate the bootstrap table on the
|
|
|
|
* cateory listing page.
|
|
|
|
*
|
2016-04-07 17:23:52 -07:00
|
|
|
* @todo Refactor this nastiness. Assets do not behave the same as accessories, etc.
|
2016-03-25 18:07:12 -07:00
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see CategoriesController::getIndex() method that generates the view
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return String JSON
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getDatatable()
|
|
|
|
{
|
|
|
|
// Grab all the categories
|
2016-04-07 17:15:30 -07:00
|
|
|
$categories = Category::with('assets', 'accessories', 'consumables','components');
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
if (Input::has('search')) {
|
|
|
|
$categories = $categories->TextSearch(e(Input::get('search')));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input::has('offset')) {
|
|
|
|
$offset = e(Input::get('offset'));
|
|
|
|
} else {
|
|
|
|
$offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input::has('limit')) {
|
|
|
|
$limit = e(Input::get('limit'));
|
|
|
|
} else {
|
|
|
|
$limit = 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$allowed_columns = ['id','name','category_type'];
|
|
|
|
$order = Input::get('order') === 'asc' ? 'asc' : 'desc';
|
2016-03-25 15:24:12 -07:00
|
|
|
$sort = in_array(Input::get('sort'), $allowed_columns) ? e(Input::get('sort')) : 'created_at';
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
$categories = $categories->orderBy($sort, $order);
|
|
|
|
|
|
|
|
$catCount = $categories->count();
|
|
|
|
$categories = $categories->skip($offset)->take($limit)->get();
|
|
|
|
|
|
|
|
$rows = array();
|
|
|
|
|
|
|
|
foreach ($categories as $category) {
|
2016-04-07 17:15:30 -07:00
|
|
|
|
|
|
|
$actions = '<a href="'.route('update/category', $category->id).'" class="btn btn-warning btn-sm" style="margin-right:5px;">';
|
|
|
|
$actions .='<i class="fa fa-pencil icon-white"></i></a>';
|
|
|
|
$actions .='<a data-html="false" class="btn delete-asset btn-danger btn-sm';
|
|
|
|
if ($category->itemCount() > 0) {
|
|
|
|
$actions .=' disabled';
|
|
|
|
}
|
|
|
|
$actions .=' data-toggle="modal" href="'.route('delete/category', $category->id).'" data-content="'.trans('admin/categories/message.delete.confirm').'" data-title="'.trans('general.delete').' '.htmlspecialchars($category->name).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a>';
|
2016-03-25 01:18:05 -07:00
|
|
|
$rows[] = array(
|
|
|
|
'id' => $category->id,
|
|
|
|
'name' => (string)link_to('/admin/settings/categories/'.$category->id.'/view', $category->name) ,
|
|
|
|
'category_type' => ucwords($category->category_type),
|
2016-04-07 17:15:30 -07:00
|
|
|
'count' => $category->itemCount(),
|
2016-03-25 01:18:05 -07:00
|
|
|
'acceptance' => ($category->require_acceptance=='1') ? '<i class="fa fa-check"></i>' : '',
|
|
|
|
//EULA is still not working correctly
|
|
|
|
'eula' => ($category->getEula()) ? '<i class="fa fa-check"></i>' : '',
|
|
|
|
'actions' => $actions
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = array('total' => $catCount, 'rows' => $rows);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2016-03-25 18:07:12 -07:00
|
|
|
/**
|
|
|
|
* Returns JSON response that contains the data for the category detail page.
|
|
|
|
*
|
2016-04-07 17:23:52 -07:00
|
|
|
* @todo Refactor this nastiness. Assets do not behave
|
|
|
|
* the same as accessories, etc. Need to figure out if I should
|
|
|
|
* make separate controllers, or what. Too much copypasta if I try to work it in here.
|
2016-03-25 18:07:12 -07:00
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see CategoriesController::getView() method that generates the view
|
|
|
|
* @param int $categoryId
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return String JSON
|
|
|
|
*/
|
2016-04-07 17:23:52 -07:00
|
|
|
public function getDataView($categoryId)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
|
2016-04-07 17:23:52 -07:00
|
|
|
$category = Category::find($categoryId);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
if ($category->category_type =='asset') {
|
|
|
|
$category_assets = $category->assets;
|
|
|
|
} elseif ($category->category_type =='accessory') {
|
|
|
|
$category_assets = $category->accessories;
|
|
|
|
} elseif ($category->category_type =='consumable') {
|
|
|
|
$category_assets = $category->consumables;
|
|
|
|
} elseif ($category->category_type =='component') {
|
|
|
|
$category_assets = $category->components;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Input::has('search')) {
|
|
|
|
$category_assets = $category_assets->TextSearch(e(Input::get('search')));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input::has('offset')) {
|
|
|
|
$offset = e(Input::get('offset'));
|
|
|
|
} else {
|
|
|
|
$offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input::has('limit')) {
|
|
|
|
$limit = e(Input::get('limit'));
|
|
|
|
} else {
|
|
|
|
$limit = 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
$order = Input::get('order') === 'asc' ? 'asc' : 'desc';
|
|
|
|
|
|
|
|
$allowed_columns = ['id','name','serial','asset_tag'];
|
|
|
|
$sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at';
|
|
|
|
$count = $category_assets->count();
|
|
|
|
|
|
|
|
$rows = array();
|
|
|
|
|
|
|
|
foreach ($category_assets as $asset) {
|
|
|
|
|
|
|
|
$actions = '';
|
|
|
|
$inout='';
|
|
|
|
|
|
|
|
if ($asset->deleted_at=='') {
|
2016-04-07 17:15:30 -07:00
|
|
|
$actions = '<div style=" white-space: nowrap;"><a href="'.route('clone/hardware', $asset->id).'" class="btn btn-info btn-sm" title="Clone asset"><i class="fa fa-files-o"></i></a> <a href="'.route('update/'.$category->category_type, $asset->id).'" class="btn btn-warning btn-sm"><i class="fa fa-pencil icon-white"></i></a> <a data-html="false" class="btn delete-asset btn-danger btn-sm" data-toggle="modal" href="'.route('delete/hardware', $asset->id).'" data-content="'.trans('admin/hardware/message.delete.confirm').'" data-title="'.trans('general.delete').' '.htmlspecialchars($asset->asset_tag).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a></div>';
|
2016-03-25 01:18:05 -07:00
|
|
|
} elseif ($asset->deleted_at!='') {
|
|
|
|
$actions = '<a href="'.route('restore/hardware', $asset->id).'" class="btn btn-warning btn-sm"><i class="fa fa-recycle icon-white"></i></a>';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($asset->assetstatus) {
|
|
|
|
if ($asset->assetstatus->deployable != 0) {
|
|
|
|
if (($asset->assigned_to !='') && ($asset->assigned_to > 0)) {
|
2016-04-07 13:39:35 -07:00
|
|
|
$inout = '<a href="'.route('checkin/hardware', $asset->id).'" class="btn btn-primary btn-sm">'.trans('general.checkin').'</a>';
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
2016-04-07 13:39:35 -07:00
|
|
|
$inout = '<a href="'.route('checkout/hardware', $asset->id).'" class="btn btn-info btn-sm">'.trans('general.checkout').'</a>';
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$rows[] = array(
|
|
|
|
'id' => $asset->id,
|
2016-03-25 15:24:12 -07:00
|
|
|
'name' => (string)link_to('/hardware/'.$asset->id.'/view', e($asset->name)),
|
2016-03-25 01:18:05 -07:00
|
|
|
//'model' => $asset->model->name,
|
2016-03-25 15:24:12 -07:00
|
|
|
'asset_tag' => e($asset->asset_tag),
|
|
|
|
'serial' => e($asset->serial),
|
2016-03-25 01:18:05 -07:00
|
|
|
'assigned_to' => ($asset->assigneduser) ? (string)link_to(config('app.url').'/admin/users/'.$asset->assigneduser->id.'/view', $asset->assigneduser->fullName()): '',
|
|
|
|
'change' => $inout,
|
|
|
|
'actions' => $actions,
|
|
|
|
'companyName' => Company::getName($asset),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = array('total' => $count, 'rows' => $rows);
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|