public function handleUploadReciever() { $uploader = new \UploadHandler(); $uploader->allowedExtensions = ["jpeg", "jpg", "png", "gif"]; $uploader->chunksFolder = __DIR__ . '/../../www/chunks'; $name = Nette\Utils\Strings::webalize($uploader->getName(), '.'); //TODO: picture optimalization (?) $result = $uploader->handleUpload(__DIR__ . '/../../www/uploads', $name); try { $picture = $this->pictures->findOneBy(['uuid' => $uploader->getUuid()]); if (!$picture) { //FIXME: toto není optimální (zejména kvůli rychlosti) $picture = new Entity\Picture(); } $picture->uuid = $uploader->getUuid(); $picture->name = $name; $picture->created = new \DateTime('now'); $this->pictures->save($picture); } catch (\Exception $exc) { $uploader->handleDelete(__DIR__ . '/../../www/uploads'); $this->sendResponse(new Nette\Application\Responses\JsonResponse(['error' => $exc->getMessage()])); } //TODO: napřed předat do šablony nová data $this->redrawControl('pictures'); $this->sendResponse(new Nette\Application\Responses\JsonResponse($result)); }
public function postVendorRegister(Request $request) { try { $validator = $this->validator($request->all()); if ($validator->fails()) { $this->throwValidationException($request, $validator); } DB::beginTransaction(); // Create User first $user = $this->create($request->all(), config('app.role_vendor')); // Now validate / create vendor profile $data = $request->all(); $vendorValidator = $this->vendorValidator($data); if ($vendorValidator->fails()) { $this->throwValidationException($request, $vendorValidator); } // Attempt to upload the images $uploader = new UploadHandler(); if ($request->hasFile('logo_image_path') && $request->file('logo_image_path')->isValid() && $uploader->isImage($request->file('logo_image_path'))) { $newFilename = $uploader->uploadVendorAsset($request->file('logo_image_path')); $data['logo_image_path'] = $newFilename; } if ($request->hasFile('background_image_path') && $request->file('background_image_path')->isValid() && $uploader->isImage($request->file('background_image_path'))) { $newFilename = $uploader->uploadVendorAsset($request->file('background_image_path')); $data['background_image_path'] = $newFilename; } $vendor = $this->createVendor($user->id, $data); DB::commit(); \Auth::login($user); } catch (\Exception $ex) { DB::rollBack(); throw $ex; } // Send an email to us to notify of new vendor registration try { $mailData = ['to' => config('app.vendor_registration_notify_email'), 'from' => $user->email, 'subject' => 'Vendor ' . $user->email . ' has registered', 'body' => 'Vendor sign up: Email:' . $user->email . ', Company Name: ' . $vendor->company_name, 'sendRaw' => true]; $this->dispatch(new SendEmail($mailData)); } catch (\Exception $ex) { // If email fails do not stop registration from happening } return redirect($this->redirectPath()); }
public function profileAvatarUpdate(Request $request) { $user = \Auth::user(); if (!is_null($user)) { try { $action = $request->header('ACTION'); if (isset($action) && !is_null($action)) { $up = $user->userProfile; $uploader = new UploadHandler(); if ($action == 'UPDATE') { if ($request->hasFile('avatar_image_path')) { $avatarFilename = null; if (!is_null($up) && isset($up->avatar_image_path)) { $avatarFilename = $up->avatar_image_path; // If the extension has changed from the last upload // get the new filename and save it back to the DB // also delete the old file off the filesystem $changedFilename = $uploader->getNewImageExtension($request->file('avatar_image_path')->getClientOriginalName(), $avatarFilename); if ($changedFilename !== $avatarFilename) { $up->avatar_image_path = $changedFilename; $up->save(); $uploader->removeAvatar($avatarFilename); $avatarFilename = $changedFilename; } } $newFilename = $uploader->uploadAvatar($request->file('avatar_image_path'), $avatarFilename); if (!is_null($up)) { $up->avatar_image_path = $newFilename; $up->save(); } else { // UserProfile doesn't exist, create a row $input = ['user_id' => $user->id, 'avatar_image_path' => $newFilename]; $user->userProfile()->create($input); } $request->session()->put(config('app.session_key_avatar'), $newFilename); return response()->json(['error' => false, 'code' => 200, 'filename' => $newFilename], 200); } } else { if ($action == 'DELETE') { // Remove existing avatar if (!is_null($up)) { $uploader->removeAvatar($up->avatar_image_path); $up->avatar_image_path = null; $up->save(); } $request->session()->forget(config('app.session_key_avatar')); return response()->json(['error' => false, 'code' => 200], 200); } else { if ($action == 'CROP') { $cropData = explode(';', $request->header('cropdata')); $uploader->cropAvatar($up->avatar_image_path, $cropData); return response()->json(['error' => false, 'code' => 200, 'filename' => $up->avatar_image_path], 200); } } } } } catch (\Exception $ex) { return response()->json(['error' => true, 'message' => 'Server error while uploading/saving/deleting', 'exception' => $ex->getMessage(), 'code' => 500], 500); } } return redirect('/'); }
public function doImport(ProductImportOptions $pio) { //\Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix() $path = $pio->getUuid() . '/' . $pio->getFileName(); $fileContents = \Storage::disk('imports')->get($path); $csv = Reader::createFromString($fileContents); $csv->setDelimiter(','); if ($pio->isIncludeHeaders()) { $csv->setOffset(1); //because we don't want to insert the header } $recordsAdded = 0; $recordsUpdated = 0; $recordsFailed = 0; $recordCount = 0; $uploader = new UploadHandler(); $csv->each(function ($row) use(&$pio, &$recordsAdded, &$recordsUpdated, &$recordsFailed, &$recordCount, &$uploader) { /* Row Structure * *CSV row format: * * Main, * Sub1, * Sub2, * Description, * Brand, * Pack, * Size, * UOM, * MPC, * Contact, * GTIN, * Halal, * IsOrganicProduct, * KosherClassification, * CalculationSize, * CalculationSizeUnitofMeasure, * Calories, * CaloriesfromFat, * Protein, * Carbohydrates, * TotalDietaryFibre, * TotalSugar, * TotalFat, * SaturatedFat, * Sodium mg, * AllergenPeanuts, * AllergenTreeNuts, * AllergenMilk, * AllergenLactose, * AllergenEggs, * AllergenFish, * AllergenShellfish, * AllergenSoy, * AllergenGluten, * Image URL, * ItemDetail, * Preparation, * IngredientsEnglish, * BenefitsEnglish * */ if ($row[0] != null) { if (!$pio->isSimulate()) { \DB::beginTransaction(); } try { // Check to see if the record already exists (update) if not ignore existing items //$product = \App\Models\Product::where('uniquekey', '=', $row[8]) // ->orWhere('uniquekey', '=', $row[11])->first(); $isExisting = false; //if(!is_null($product) && isset($product)) //{ //$isExisting = true; //} if ($isExisting && !$pio->isSimulate() && !$pio->isIgnoreExisting()) { // Clear any existing categories //\DB::table('product_categories')->where('product_id', '=', $product->id)->delete(); // Clear any existing allergens //\DB::table('product_allergens')->where('product_id', '=', $product->id)->delete(); } else { $product = new \App\Models\Product(); $product->vendor_id = $pio->getVendorId(); $product->uniquekey = isset($row[8]) ? $row[8] : $row[11]; // MPC or GTIN } $product->name = ucfirst(mb_strtolower($row[3])); $product->brand = ucfirst(mb_strtolower($row[4])); $product->pack = $row[5]; $product->size = $row[6]; $product->uom = $row[7]; $product->serving_size_uom = $row[7]; // Not sure this should be duplicated $product->mpc = $row[8]; $product->broker_contact = $row[9]; $product->gtin = $row[10]; $product->is_halal = $this->normalizeTrueFalseField($row[11]); $product->is_organic = $this->normalizeTrueFalseField($row[12]); $product->is_kosher = $this->normalizeTrueFalseField($row[13]); $product->calc_size = $row[14]; $product->calculation_size_uom = $row[15]; $product->calories = $row[16]; $product->calories_from_fat = $row[17]; $product->protein = $row[18]; $product->carbs = $row[19]; $product->fibre = $row[20]; $product->sugar = $row[21]; $product->total_fat = $row[22]; $product->saturated_fats = $row[23]; $product->sodium = $row[24]; //rows 25, 26, 27, 28, 29, 30, 31, 32, 33 are allergens // Product Image if ($row[34] !== '') { if ($pio->isDownloadImages()) { try { $newFilename = $uploader->getRemoteFile($row[34], null, public_path(config('app.product_storage'))); $product->product_image = $newFilename; } catch (Exception $ex) { /* If the image can't be downloaded, just don't set the field */ } } else { $product->product_image = $row[34]; } } $product->description = $row[35]; $product->preparation = $row[36]; $product->ingredient_deck = $row[37]; $product->features_benefits = $row[38]; $product->allergen_disclaimer = ''; $product->net_weight = 0; $product->gross_weight = 0; $product->tare_weight = 0; $product->serving_size = 0; $product->vendor_logo = ''; $product->pos_pdf = ''; if (!$isExisting) { $product->published = $pio->isAddAsActive(); } // Only create record, relationships and commit if not simulated if (!$pio->isSimulate() || $isExisting && !$pio->isIgnoreExisting()) { $product->save(); // Create any categories for the user product $catIdRoot = $this->createCategory($row[0], null); $catIdSub1 = $this->createCategory($row[1], $row[0]); $catIdSub2 = $this->createCategory($row[2], $row[1]); $this->createProductCategory($catIdRoot, $product->id); $this->createProductCategory($catIdSub1, $product->id); $this->createProductCategory($catIdSub2, $product->id); // Create any allergens for the user product if ($row[25] == 'Y') { $this->createProductAllergen(6, $product->id); } if ($row[26] == 'Y') { $this->createProductAllergen(9, $product->id); } if ($row[27] == 'Y') { $this->createProductAllergen(5, $product->id); } if ($row[28] == 'Y') { $this->createProductAllergen(4, $product->id); } if ($row[29] == 'Y') { $this->createProductAllergen(1, $product->id); } if ($row[30] == 'Y') { $this->createProductAllergen(2, $product->id); } if ($row[31] == 'Y') { $this->createProductAllergen(7, $product->id); } if ($row[32] == 'Y') { $this->createProductAllergen(8, $product->id); } if ($row[33] == 'Y') { $this->createProductAllergen(3, $product->id); } \DB::commit(); } if ($isExisting && !$pio->isIgnoreExisting()) { $recordsUpdated += 1; } else { $recordsAdded += 1; } } catch (\Exception $ex) { if (!$pio->isSimulate()) { \DB::rollBack(); } $recordsFailed += 1; } $recordCount += 1; return true; // Continue processing file } }); // Create a record of the import in the database //$productImport = new \App\Models\ProductImport(); //$productImport->user_id = $pio->getVendorId(); //$productImport->uuid = $pio->getUuid(); //$productImport->filename = $pio->getFileName(); //$productImport->save(); echo sprintf('Import complete, %s records added, %s records updated, %s records failed %s', $recordsAdded, $recordsUpdated, $recordsFailed, $pio->isSimulate() ? '(Simulated)' : ''); }
public function editUser(Request $request) { $action = $request->input('action'); $userId = $request->input('userid'); if ($action === 'UPDATE') { $user = $this->dataAccess->getUser($userId, 'roles'); if ($request->input('password') != '') { $user->password = bcrypt($request->input('password')); $user->save(); } $user->detachRoles($user->roles); $user->attachRole($request->input('role')); return redirect('admin/userview/' . $userId)->with('successMessage', 'User updated'); } else { if ($action === 'DELETE') { $uploader = new UploadHandler(); $user = $this->dataAccess->getUser($userId, 'roles'); $user->detachRoles($user->roles); $up = $user->userProfile; if (!is_null($up)) { $uploader->removeAvatar($up->avatar_image_path); } $this->dataAccess->deleteUser($userId); return redirect('admin/users')->with('successMessage', 'User deleted'); } } }
public function vendorProductsAction(Request $request) { $action = $request->input('action'); $selectedProducts = $request->input('products'); $vendorId = \Session::get(config('app.session_key_vendor')); if ($action === 'DELETE') { $uploader = new UploadHandler(); foreach ($selectedProducts as $productId) { $product = $this->dataAccess->getProductByIdVendor($productId, $vendorId, ['product_image']); if (isset($product)) { $uploader->removeProductAsset($product->product_image); $this->dataAccess->deleteProduct($productId, $vendorId); } } } else { if ($action === 'PUBLISH' || $action === 'UNPUBLISH') { foreach ($selectedProducts as $productId) { $this->dataAccess->updateProduct($productId, $vendorId, ['published' => $action === 'PUBLISH' ? 1 : 0]); } } } return redirect('product/vendor'); }