コード例 #1
0
ファイル: AssetsController.php プロジェクト: stijni/snipe-it
 /**
  * Validate and process asset edit form.
  *
  * @author [A. Gianotto] [<*****@*****.**>]
  * @param int $assetId
  * @since [v1.0]
  * @return Redirect
  */
 public function postEdit(AssetRequest $request, $assetId = null)
 {
     // Check if the asset exists
     if (!($asset = Asset::find($assetId))) {
         // Redirect to the asset management page with error
         return redirect()->to('hardware')->with('error', trans('admin/hardware/message.does_not_exist'));
     } elseif (!Company::isCurrentUserHasAccess($asset)) {
         return redirect()->to('hardware')->with('error', trans('general.insufficient_permissions'));
     }
     if ($request->has('status_id')) {
         $asset->status_id = e($request->input('status_id'));
     } else {
         $asset->status_id = null;
     }
     if ($request->has('warranty_months')) {
         $asset->warranty_months = e($request->input('warranty_months'));
     } else {
         $asset->warranty_months = null;
     }
     if ($request->has('purchase_cost')) {
         $asset->purchase_cost = e(Helper::formatCurrencyOutput($request->input('purchase_cost')));
     } else {
         $asset->purchase_cost = null;
     }
     if ($request->has('purchase_date')) {
         $asset->purchase_date = e($request->input('purchase_date'));
     } else {
         $asset->purchase_date = null;
     }
     if ($request->has('supplier_id')) {
         $asset->supplier_id = e($request->input('supplier_id'));
     } else {
         $asset->supplier_id = null;
     }
     // If the box isn't checked, it's not in the request at all.
     $asset->requestable = $request->has('requestable');
     if ($request->has('rtd_location_id')) {
         $asset->rtd_location_id = e($request->input('rtd_location_id'));
     } else {
         $asset->rtd_location_id = null;
     }
     if ($request->has('image_delete')) {
         unlink(public_path() . '/uploads/assets/' . $asset->image);
         $asset->image = '';
     }
     // Update the asset data
     $asset->name = e($request->input('name'));
     $asset->serial = e($request->input('serial'));
     $asset->company_id = Company::getIdForCurrentUser(e($request->input('company_id')));
     $asset->model_id = e($request->input('model_id'));
     $asset->order_number = e($request->input('order_number'));
     $asset->asset_tag = e($request->input('asset_tag'));
     $asset->notes = e($request->input('notes'));
     $asset->physical = '1';
     // Update the image
     if (Input::has('image')) {
         $image = $request->input('image');
         // See postCreate for more explaination of the following.
         $header = explode(';', $image, 2)[0];
         $extension = substr($header, strpos($header, '/') + 1);
         $image = substr($image, strpos($image, ',') + 1);
         $directory = public_path('uploads/assets/');
         // Check if the uploads directory exists.  If not, try to create it.
         if (!file_exists($directory)) {
             mkdir($directory, 0755);
         }
         $file_name = str_random(25) . "." . $extension;
         $path = public_path('uploads/assets/' . $file_name);
         try {
             Image::make($image)->resize(500, 500, function ($constraint) {
                 $constraint->aspectRatio();
                 $constraint->upsize();
             })->save($path);
             $asset->image = $file_name;
         } catch (\Exception $e) {
             \Input::flash();
             $messageBag = new \Illuminate\Support\MessageBag();
             $messageBag->add('image', $e->getMessage());
             \Session()->flash('errors', \Session::get('errors', new \Illuminate\Support\ViewErrorBag())->put('default', $messageBag));
             return response()->json(['image' => $e->getMessage()], 422);
         }
         $asset->image = $file_name;
     }
     // Update custom fields in the database.
     // Validation for these fields is handlded through the AssetRequest form request
     // FIXME: No idea why this is returning a Builder error on db_column_name.
     // Need to investigate and fix. Using static method for now.
     $model = AssetModel::find($request->get('model_id'));
     if ($model->fieldset) {
         foreach ($model->fieldset->fields as $field) {
             if ($field->field_encrypted == '1') {
                 if (Gate::allows('admin')) {
                     $asset->{\App\Models\CustomField::name_to_db_name($field->name)} = \Crypt::encrypt(e($request->input(\App\Models\CustomField::name_to_db_name($field->name))));
                 }
             } else {
                 $asset->{\App\Models\CustomField::name_to_db_name($field->name)} = e($request->input(\App\Models\CustomField::name_to_db_name($field->name)));
             }
         }
     }
     if ($asset->save()) {
         // Redirect to the new asset page
         \Session::flash('success', trans('admin/hardware/message.update.success'));
         return response()->json(['redirect_url' => route("view/hardware", $assetId)]);
     }
     \Input::flash();
     \Session::flash('errors', $asset->getErrors());
     return response()->json(['errors' => $asset->getErrors()], 500);
 }
コード例 #2
0
 /**
  * Returns a form view to edit a consumable.
  *
  * @author [A. Gianotto] [<*****@*****.**>]
  * @param  int $consumableId
  * @see ConsumablesController::getEdit() method that stores the form data.
  * @since [v1.0]
  * @return Redirect
  */
 public function postEdit($consumableId = null)
 {
     if (is_null($consumable = Consumable::find($consumableId))) {
         return redirect()->to('admin/consumables')->with('error', trans('admin/consumables/message.does_not_exist'));
     } elseif (!Company::isCurrentUserHasAccess($consumable)) {
         return redirect()->to('admin/consumables')->with('error', trans('general.insufficient_permissions'));
     }
     $consumable->name = e(Input::get('name'));
     $consumable->category_id = e(Input::get('category_id'));
     $consumable->location_id = e(Input::get('location_id'));
     $consumable->company_id = Company::getIdForCurrentUser(Input::get('company_id'));
     $consumable->order_number = e(Input::get('order_number'));
     $consumable->min_amt = e(Input::get('min_amt'));
     $consumable->manufacturer_id = e(Input::get('manufacturer_id'));
     $consumable->model_no = e(Input::get('model_no'));
     $consumable->item_no = e(Input::get('item_no'));
     if (e(Input::get('purchase_date')) == '') {
         $consumable->purchase_date = null;
     } else {
         $consumable->purchase_date = e(Input::get('purchase_date'));
     }
     if (e(Input::get('purchase_cost')) == '0.00') {
         $consumable->purchase_cost = null;
     } else {
         $consumable->purchase_cost = e(Input::get('purchase_cost'));
     }
     $consumable->qty = e(Input::get('qty'));
     if ($consumable->save()) {
         return redirect()->to("admin/consumables")->with('success', trans('admin/consumables/message.update.success'));
     }
     return redirect()->back()->withInput()->withErrors($consumable->getErrors());
 }
コード例 #3
0
 /**
  * Validates and stores the license form data submitted from the edit
  * license form.
  *
  * @author [A. Gianotto] [<*****@*****.**>]
  * @see LicensesController::getEdit() method that provides the form view
  * @since [v1.0]
  * @param int $licenseId
  * @return Redirect
  */
 public function postEdit($licenseId = null)
 {
     // Check if the license exists
     if (is_null($license = License::find($licenseId))) {
         // Redirect to the blogs management page
         return redirect()->to('admin/licenses')->with('error', trans('admin/licenses/message.does_not_exist'));
     } elseif (!Company::isCurrentUserHasAccess($license)) {
         return redirect()->to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
     }
     // Update the license data
     $license->name = e(Input::get('name'));
     $license->serial = e(Input::get('serial'));
     $license->license_email = e(Input::get('license_email'));
     $license->license_name = e(Input::get('license_name'));
     $license->notes = e(Input::get('notes'));
     $license->order_number = e(Input::get('order_number'));
     $license->depreciation_id = e(Input::get('depreciation_id'));
     $license->company_id = Company::getIdForCurrentUser(Input::get('company_id'));
     $license->purchase_order = e(Input::get('purchase_order'));
     $license->maintained = e(Input::get('maintained'));
     $license->reassignable = e(Input::get('reassignable'));
     if (e(Input::get('supplier_id')) == '') {
         $license->supplier_id = null;
     } else {
         $license->supplier_id = e(Input::get('supplier_id'));
     }
     // Update the asset data
     if (e(Input::get('purchase_date')) == '') {
         $license->purchase_date = null;
     } else {
         $license->purchase_date = e(Input::get('purchase_date'));
     }
     if (e(Input::get('expiration_date')) == '') {
         $license->expiration_date = null;
     } else {
         $license->expiration_date = e(Input::get('expiration_date'));
     }
     if (e(Input::get('termination_date')) == '') {
         $license->termination_date = null;
     } else {
         $license->termination_date = e(Input::get('termination_date'));
     }
     if (e(Input::get('purchase_cost')) == '') {
         $license->purchase_cost = null;
     } else {
         $license->purchase_cost = e(Input::get('purchase_cost'));
         //$license->purchase_cost = e(Input::get('purchase_cost'));
     }
     if (e(Input::get('maintained')) == '') {
         $license->maintained = 0;
     } else {
         $license->maintained = e(Input::get('maintained'));
     }
     if (e(Input::get('reassignable')) == '') {
         $license->reassignable = 0;
     } else {
         $license->reassignable = e(Input::get('reassignable'));
     }
     if (e(Input::get('purchase_order')) == '') {
         $license->purchase_order = '';
     } else {
         $license->purchase_order = e(Input::get('purchase_order'));
     }
     //Are we changing the total number of seats?
     if ($license->seats != e(Input::get('seats'))) {
         //Determine how many seats we are dealing with
         $difference = e(Input::get('seats')) - $license->licenseseats()->count();
         if ($difference < 0) {
             //Filter out any license which have a user attached;
             $seats = $license->licenseseats->filter(function ($seat) {
                 return is_null($seat->user);
             });
             //If the remaining collection is as large or larger than the number of seats we want to delete
             if ($seats->count() >= abs($difference)) {
                 for ($i = 1; $i <= abs($difference); $i++) {
                     //Delete the appropriate number of seats
                     $seats->pop()->delete();
                 }
                 //Log the deletion of seats to the log
                 $logaction = new Actionlog();
                 $logaction->asset_id = $license->id;
                 $logaction->asset_type = 'software';
                 $logaction->user_id = Auth::user()->id;
                 $logaction->note = abs($difference) . " seats";
                 $logaction->checkedout_to = null;
                 $log = $logaction->logaction('delete seats');
             } else {
                 // Redirect to the license edit page
                 return redirect()->to("admin/licenses/{$licenseId}/edit")->with('error', trans('admin/licenses/message.assoc_users'));
             }
         } else {
             for ($i = 1; $i <= $difference; $i++) {
                 //Create a seat for this license
                 $license_seat = new LicenseSeat();
                 $license_seat->license_id = $license->id;
                 $license_seat->user_id = Auth::user()->id;
                 $license_seat->assigned_to = null;
                 $license_seat->notes = null;
                 $license_seat->save();
             }
             //Log the addition of license to the log.
             $logaction = new Actionlog();
             $logaction->asset_id = $license->id;
             $logaction->asset_type = 'software';
             $logaction->user_id = Auth::user()->id;
             $logaction->note = abs($difference) . " seats";
             $log = $logaction->logaction('add seats');
         }
         $license->seats = e(Input::get('seats'));
     }
     // Was the asset created?
     if ($license->save()) {
         // Redirect to the new license page
         return redirect()->to("admin/licenses/{$licenseId}/view")->with('success', trans('admin/licenses/message.update.success'));
     }
     // Redirect to the license edit page
     return redirect()->to("admin/licenses/{$licenseId}/edit")->with('error', trans('admin/licenses/message.update.error'));
 }
コード例 #4
0
 /**
  * Return a view to edit a component.
  *
  * @author [A. Gianotto] [<*****@*****.**>]
  * @see ComponentsController::getEdit() method presents the form.
  * @param int $componentId
  * @since [v3.0]
  * @return Redirect
  */
 public function postEdit($componentId = null)
 {
     // Check if the blog post exists
     if (is_null($component = Component::find($componentId))) {
         // Redirect to the blogs management page
         return redirect()->to('admin/components')->with('error', trans('admin/components/message.does_not_exist'));
     } elseif (!Company::isCurrentUserHasAccess($component)) {
         return redirect()->to('admin/components')->with('error', trans('general.insufficient_permissions'));
     }
     // Update the component data
     $component->name = e(Input::get('name'));
     $component->category_id = e(Input::get('category_id'));
     $component->location_id = e(Input::get('location_id'));
     $component->company_id = Company::getIdForCurrentUser(Input::get('company_id'));
     $component->order_number = e(Input::get('order_number'));
     $component->min_amt = e(Input::get('min_amt'));
     if (e(Input::get('purchase_date')) == '') {
         $component->purchase_date = null;
     } else {
         $component->purchase_date = e(Input::get('purchase_date'));
     }
     if (e(Input::get('purchase_cost')) == '0.00') {
         $component->purchase_cost = null;
     } else {
         $component->purchase_cost = e(Input::get('purchase_cost'));
     }
     $component->total_qty = e(Input::get('total_qty'));
     // Was the component created?
     if ($component->save()) {
         // Redirect to the new component page
         return redirect()->to("admin/components")->with('success', trans('admin/components/message.update.success'));
     }
     return redirect()->back()->withInput()->withErrors($component->getErrors());
 }
コード例 #5
0
 /**
  * Save edited Accessory from form post
  *
  * @author [A. Gianotto] [<*****@*****.**>]
  * @param  int  $accessoryId
  * @return Redirect
  */
 public function postEdit(Request $request, $accessoryId = null)
 {
     // Check if the accessory exists
     if (is_null($accessory = Accessory::find($accessoryId))) {
         // Redirect to the accessory index page
         return redirect()->to('admin/accessories')->with('error', trans('admin/accessories/message.does_not_exist'));
     } elseif (!Company::isCurrentUserHasAccess($accessory)) {
         return redirect()->to('admin/accessories')->with('error', trans('general.insufficient_permissions'));
     }
     // Update the accessory data
     $accessory->name = e(Input::get('name'));
     if (e(Input::get('location_id')) == '') {
         $accessory->location_id = null;
     } else {
         $accessory->location_id = e(Input::get('location_id'));
     }
     $accessory->min_amt = e(Input::get('min_amt'));
     $accessory->category_id = e(Input::get('category_id'));
     $accessory->company_id = Company::getIdForCurrentUser(Input::get('company_id'));
     $accessory->manufacturer_id = e(Input::get('manufacturer_id'));
     $accessory->order_number = e(Input::get('order_number'));
     if (e(Input::get('purchase_date')) == '') {
         $accessory->purchase_date = null;
     } else {
         $accessory->purchase_date = e(Input::get('purchase_date'));
     }
     if (e(Input::get('purchase_cost')) == '0.00') {
         $accessory->purchase_cost = null;
     } else {
         $accessory->purchase_cost = e(Input::get('purchase_cost'));
     }
     $accessory->qty = e(Input::get('qty'));
     // Was the accessory updated?
     if ($accessory->save()) {
         // Redirect to the updated accessory page
         return redirect()->to("admin/accessories")->with('success', trans('admin/accessories/message.update.success'));
     }
     return redirect()->back()->withInput()->withErrors($accessory->getErrors());
 }