Ejemplo n.º 1
0
 /**
  * save image to folder and name to db
  * @param  AssetRequest $request
  * @return void
  */
 public function saveImage(AssetRequest $request)
 {
     if (!$request->file('image')) {
         return null;
     }
     $uploadPath = '/images/assets/';
     $imageName = $this->id . '.' . $request->file('image')->getClientOriginalExtension();
     $imagePath = base_path() . '/public' . $uploadPath;
     $request->file('image')->move($imagePath, $imageName);
     $this->update(['image' => $uploadPath . $imageName]);
 }
Ejemplo n.º 2
0
 /**
  * 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);
 }
Ejemplo n.º 3
0
 /**
  * Update the specified resource in storage.
  *
  * @param AssetRequest $request
  * @param  int $id
  * @return \Illuminate\Http\Response
  */
 public function update(AssetRequest $request, $id)
 {
     Asset::find($id)->update($request->all());
     return redirect()->route('asset.index')->withMessage('Asset Updated Successfully')->withStatus('success');
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  AssetRequest  $request
  * @param  int  $id
  * @return Response
  */
 public function update(AssetRequest $request, $id)
 {
     $asset = Asset::findOrFail($id);
     $asset->update($request->all());
     $asset->saveImage($request);
     flash()->success('Success!', 'Asset updated successfully!');
     return redirect('assets');
 }