Ejemplo n.º 1
0
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  int $id
  * @param  int $action
  *
  * @return \Illuminate\Http\Response
  */
 public function postUpdate(Request $request, $id)
 {
     $response['status'] = 'error';
     $response['message'] = trans('products.not_updated');
     if (!empty($_POST)) {
         if (!empty(Input::get('image_sync'))) {
             if (empty($images = json_decode(Input::get('images'), TRUE))) {
                 $images = [];
             }
             $response['message'] = trans('products.images_not_synced');
             //Get current local images
             $images_array = [];
             if (!empty($id) && is_dir($this->images_path . $id) && is_dir($this->images_path . $id . DIRECTORY_SEPARATOR . Config::get('images.full_size'))) {
                 $local_images = array_diff(scandir($this->images_path . $id . DIRECTORY_SEPARATOR . Config::get('images.full_size')), array('..', '.'));
                 natcasesort($local_images);
                 $local_images = array_values(array_filter($local_images));
                 if (!empty($local_images) && is_array($local_images)) {
                     foreach ($local_images as $key => $data) {
                         if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
                             $data = iconv("WINDOWS-1251", "UTF-8", $data);
                         }
                         if (empty($images_array[$data])) {
                             $images_array[$data] = $key;
                         }
                     }
                 }
             }
             //Remove image
             if (!empty($remove_image = Input::get('remove_image'))) {
                 if (isset($images[$remove_image])) {
                     //Remove from array
                     unset($images[$remove_image]);
                     //If WIN reconvert to utf8
                     if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
                         $remove_image = iconv("UTF-8", "WINDOWS-1251", $remove_image);
                     }
                     //Remove local files
                     $images_to_remove = [$this->images_path . $id . DIRECTORY_SEPARATOR . Config::get('images.full_size') . DIRECTORY_SEPARATOR . $remove_image, $this->images_path . $id . DIRECTORY_SEPARATOR . Config::get('images.lg_icon_size') . DIRECTORY_SEPARATOR . $remove_image, $this->images_path . $id . DIRECTORY_SEPARATOR . Config::get('images.md_icon_size') . DIRECTORY_SEPARATOR . $remove_image, $this->images_path . $id . DIRECTORY_SEPARATOR . Config::get('images.sm_icon_size') . DIRECTORY_SEPARATOR . $remove_image];
                     foreach ($images_to_remove as $image) {
                         if (file_exists($image)) {
                             unlink($image);
                         }
                     }
                     //Remove message
                     $message_success = trans('products.image_removed');
                 }
             } else {
                 //Images save message
                 $message_success = trans('products.images_synced');
             }
             if (is_array($images)) {
                 $images = array_merge($images_array, $images);
             } else {
                 $images = $images_array;
             }
             if (!empty($images)) {
                 Model_Products::deleteAllImages($id);
                 if (Model_Products::setProductObjects(['images' => $images], $id) === TRUE) {
                     $response['status'] = 'success';
                     $response['message'] = $message_success;
                 }
             } else {
                 if (Model_Products::deleteAllImages($id) === TRUE) {
                     $response['status'] = 'success';
                     $response['message'] = $message_success;
                 }
             }
         } else {
             $error = FALSE;
             if (empty(trim(Input::get('title')))) {
                 $response['message'] = trans('products.title_required');
                 $error = TRUE;
             }
             if (empty(trim(Input::get('friendly_url')))) {
                 $response['message'] = trans('products.url_required');
                 $error = TRUE;
             }
             $available_sizes = [];
             if (!empty(Input::get('sizes')) && is_array(Input::get('sizes'))) {
                 foreach (Input::get('sizes') as $size_name => $size) {
                     if ($size['quantity'] > 0) {
                         $available_sizes[] = $size_name;
                     }
                 }
             }
             if (!empty(Input::get('quantity'))) {
                 $available = 1;
             } else {
                 $available = 0;
             }
             if ($error === FALSE) {
                 $data = ['title' => trim(Input::get('title')), 'description' => Input::get('description'), 'main_category' => Input::get('main_category'), 'quantity' => Input::get('quantity'), 'available' => $available, 'position' => Input::get('position'), 'active' => Input::get('active'), 'original_price' => Input::get('original_price'), 'price' => Input::get('price'), 'discount_price' => Input::get('discount_price'), 'discount_start' => Input::get('discount_start'), 'discount_end' => Input::get('discount_end'), 'created_at' => Input::get('created_at'), 'sizes' => Input::get('sizes'), 'page_title' => Input::get('page_title'), 'meta_description' => Input::get('meta_description'), 'meta_keywords' => Input::get('meta_keywords'), 'related_products' => Input::get('related_products'), 'dimensions_table' => Input::get('dimensions_table')];
                 if (Model_Products::updateProduct($id, $data) === TRUE) {
                     try {
                         //Manage sizes relations
                         if (!empty($available_sizes) && is_array($available_sizes)) {
                             Model_Products::saveProductToSize($id, $available_sizes);
                         }
                         //Manage categories relations
                         if (!empty(Input::get('categories')) && is_array(Input::get('categories'))) {
                             Model_Products::setProductToCategory($id, Input::get('categories'));
                         }
                         //Manage Friendly URL
                         Model_Products::setURL($id, Input::get('friendly_url'));
                         //Manage tags
                         $tags = Input::get('tags');
                         if (!empty($tags)) {
                             $tags = explode(',', $tags);
                             Model_Products::saveTags($id, $tags);
                         } else {
                             Model_Products::removeAllTags($id);
                         }
                         //Manage manufacturer
                         $manufacturer = Input::get('manufacturer');
                         if (!empty($manufacturer)) {
                             Model_Products::setManufacturer($id, $manufacturer);
                         }
                         //Manage material
                         $material = Input::get('material');
                         if (!empty($material)) {
                             Model_Products::setMaterial($id, $material);
                         }
                         //Manage colors
                         if (!empty(Input::get('colors')) && is_array(Input::get('colors'))) {
                             Model_Products::setColors($id, Input::get('colors'));
                         }
                         $response['status'] = 'success';
                         $response['message'] = trans('products.updated');
                     } catch (Exception $e) {
                         $response['message'] = $e;
                     }
                 } else {
                     $response['message'] = trans('products.not_updated');
                 }
             }
         }
     }
     return response()->json($response);
 }