Exemplo n.º 1
0
 /**
  * Validate and process new asset form data.
  *
  * @author [A. Gianotto] [<*****@*****.**>]
  * @since [v1.0]
  * @return Redirect
  */
 public function postCreate(AssetRequest $request)
 {
     // create a new model instance
     $asset = new Asset();
     $asset->model()->associate(AssetModel::find(e(Input::get('model_id'))));
     $checkModel = config('app.url') . '/api/models/' . e(Input::get('model_id')) . '/check';
     $asset->name = e(Input::get('name'));
     $asset->serial = e(Input::get('serial'));
     $asset->company_id = Company::getIdForCurrentUser(e(Input::get('company_id')));
     $asset->model_id = e(Input::get('model_id'));
     $asset->order_number = e(Input::get('order_number'));
     $asset->notes = e(Input::get('notes'));
     $asset->asset_tag = e(Input::get('asset_tag'));
     $asset->user_id = Auth::user()->id;
     $asset->archived = '0';
     $asset->physical = '1';
     $asset->depreciate = '0';
     if (e(Input::get('status_id')) == '') {
         $asset->status_id = null;
     } else {
         $asset->status_id = e(Input::get('status_id'));
     }
     if (e(Input::get('warranty_months')) == '') {
         $asset->warranty_months = null;
     } else {
         $asset->warranty_months = e(Input::get('warranty_months'));
     }
     if (e(Input::get('purchase_cost')) == '') {
         $asset->purchase_cost = null;
     } else {
         $asset->purchase_cost = e(Input::get('purchase_cost'));
     }
     if (e(Input::get('purchase_date')) == '') {
         $asset->purchase_date = null;
     } else {
         $asset->purchase_date = e(Input::get('purchase_date'));
     }
     if (e(Input::get('assigned_to')) == '') {
         $asset->assigned_to = null;
     } else {
         $asset->assigned_to = e(Input::get('assigned_to'));
     }
     if (e(Input::get('supplier_id')) == '') {
         $asset->supplier_id = 0;
     } else {
         $asset->supplier_id = e(Input::get('supplier_id'));
     }
     if (e(Input::get('requestable')) == '') {
         $asset->requestable = 0;
     } else {
         $asset->requestable = e(Input::get('requestable'));
     }
     if (e(Input::get('rtd_location_id')) == '') {
         $asset->rtd_location_id = null;
     } else {
         $asset->rtd_location_id = e(Input::get('rtd_location_id'));
     }
     // Create the image (if one was chosen.)
     if (Input::has('image')) {
         $image = Input::get('image');
         // After modification, the image is prefixed by mime info like the following:
         // data:image/jpeg;base64,; This causes the image library to be unhappy, so we need to remove it.
         $header = explode(';', $image, 2)[0];
         // Grab the image type from the header while we're at it.
         $extension = substr($header, strpos($header, '/') + 1);
         // Start reading the image after the first comma, postceding the base64.
         $image = substr($image, strpos($image, ',') + 1);
         $file_name = str_random(25) . "." . $extension;
         $directory = public_path('uploads/assets/');
         // Check if the uploads directory exists.  If not, try to create it.
         if (!file_exists($directory)) {
             mkdir($directory, 0755);
         }
         $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);
         }
     }
     // 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) {
             $asset->{\App\Models\CustomField::name_to_db_name($field->name)} = e($request->input(\App\Models\CustomField::name_to_db_name($field->name)));
         }
     }
     // Was the asset created?
     if ($asset->save()) {
         if (Input::get('assigned_to') != '') {
             $user = User::find(e(Input::get('assigned_to')));
             $asset->checkOutToUser($user, Auth::user(), date('Y-m-d h:i:s'), '', 'Checked out on asset creation', e(Input::get('name')));
         }
         // Redirect to the asset listing page
         \Session::flash('success', trans('admin/hardware/message.create.success'));
         return response()->json(['redirect_url' => route('hardware')]);
     }
     \Input::flash();
     \Session::flash('errors', $asset->getErrors());
     return response()->json(['errors' => $asset->getErrors()], 500);
 }