public function update() { $product_id = 0; // Redirect to product create if product_id is not exists if (isset($this->request->get['product_id'])) { $product_id = (int) $this->request->get['product_id']; } else { // Log hack attempt $this->security_log->write('Try to get product without product_id param'); $this->response->redirect($this->url->link('account/product/create')); } // Redirect to login page if user is not logged if (!$this->auth->isLogged()) { $this->response->redirect($this->url->link('account/account/login', 'redirect=' . base64_encode($this->url->link('account/product/update', 'product_id=' . $product_id)))); } // Check if user has product if (!$this->model_catalog_product->userHasProduct($this->auth->getId(), $product_id)) { // Log hack attempt $this->security_log->write('Try to get not own\'s product_id #' . $product_id); // Redirect to safe page $this->response->redirect($this->url->link('account/product')); } if ('POST' == $this->request->getRequestMethod() && $this->_validateProductForm()) { // Start transaction $this->db->beginTransaction(); // Add product $this->model_catalog_product->updateProduct($product_id, $this->request->post['category_id'], $this->request->post['currency_id'], $this->request->post['regular_price'], $this->request->post['exclusive_price'], $this->request->post['withdraw_address'], FilterUri::alias($this->request->post['product_description'][DEFAULT_LANGUAGE_ID]['title']), (int) $this->auth->isVerified()); // Add 301 rule if product has new URI $url = new Url($this->db, $this->request, $this->response, URL_BASE); $old_url = $this->url->link('catalog/product', 'product_id=' . $product_id); $new_url = $url->link('catalog/product', 'product_id=' . $product_id); if ($old_url != $new_url) { $this->model_common_redirect->createRedirect(301, str_replace(URL_BASE, $old_url), str_replace(URL_BASE, $new_url)); } // Add product description $this->model_catalog_product->deleteProductDescriptions($product_id); foreach ($this->request->post['product_description'] as $language_id => $product_description) { $this->model_catalog_product->createProductDescription($product_id, $language_id, $product_description['title'], $product_description['description']); } // Add Tags $this->model_catalog_product->deleteProductToTagByProductId($product_id); foreach ($this->request->post['product_description'] as $language_id => $product_description) { if (!empty($product_description['tags'])) { $tags = explode(',', $product_description['tags']); foreach ($tags as $tag) { // Add a new global tag if not exists $tag_id = $this->model_catalog_tag->createTag(mb_strtolower(trim($tag)), $language_id); // Add product to tag relation $this->model_catalog_product->addProductToTag($product_id, $tag_id); } } } // Add file $directory = DIR_STORAGE . $this->auth->getId() . DIR_SEPARATOR; if ($file_content = file_get_contents($directory . $this->request->post['product_file_id'] . '.' . STORAGE_FILE_EXTENSION)) { $this->model_catalog_product->deleteProductFiles($product_id); $product_file_id = $this->model_catalog_product->createProductFile($product_id, md5($file_content), sha1($file_content)); rename($directory . $this->request->post['product_file_id'] . '.' . STORAGE_FILE_EXTENSION, $directory . $product_file_id . '.' . STORAGE_FILE_EXTENSION); } // Add demos $this->model_catalog_product->deleteProductDemos($product_id); if (isset($this->request->post['demo'])) { foreach ($this->request->post['demo'] as $row => $demo) { $product_demo_id = $this->model_catalog_product->createProductDemo($product_id, $demo['sort_order'], $demo['url'], $this->request->post['main_demo'] == $row ? 1 : 0); foreach ($demo['title'] as $language_id => $title) { $this->model_catalog_product->createProductDemoDescription($product_demo_id, $language_id, $title); } } } // Update images $this->model_catalog_product->deleteProductImages($product_id); if (isset($this->request->post['image'])) { foreach ($this->request->post['image'] as $row => $image) { $product_image_id = $this->model_catalog_product->createProductImage($product_id, $image['sort_order'], $this->request->post['main_image'] == $row ? 1 : 0, isset($image['watermark']) ? 1 : 0); // Generate image titles foreach ($image['title'] as $language_id => $title) { $this->model_catalog_product->createProductImageDescription($product_image_id, $language_id, $title); } // Rename temporary file $directory = DIR_STORAGE . $this->auth->getId() . DIR_SEPARATOR; rename($directory . $image['product_image_id'] . '.' . STORAGE_IMAGE_EXTENSION, $directory . $product_image_id . '.' . STORAGE_IMAGE_EXTENSION); } // Generate unique image if others images is not exists } else { $product_image_id = $this->model_catalog_product->createProductImage($product_id, 1, 1, 0, 1); // Generate image titles from product title foreach ($this->request->post['product_description'] as $language_id => $product_description) { $this->model_catalog_product->createProductImageDescription($product_image_id, $language_id, $product_description['title']); } $identicon = new Identicon(); $image = new Image($identicon->generateImageResource(sha1($product_id), PRODUCT_IMAGE_ORIGINAL_WIDTH, PRODUCT_IMAGE_ORIGINAL_HEIGHT), true); $image->save(DIR_STORAGE . $this->auth->getId() . DIR_SEPARATOR . $product_image_id . '.' . STORAGE_IMAGE_EXTENSION); } // Add videos $this->model_catalog_product->deleteProductVideos($product_id); if (isset($this->request->post['video'])) { foreach ($this->request->post['video'] as $video) { $product_video_id = $this->model_catalog_product->createProductVideo($product_id, $video['source'], $video['sort_order'], $video['id']); foreach ($video['title'] as $language_id => $title) { $this->model_catalog_product->createProductVideoDescription($product_video_id, $language_id, $title); } } } // Add audios $this->model_catalog_product->deleteProductAudios($product_id); if (isset($this->request->post['audio'])) { foreach ($this->request->post['audio'] as $audio) { $product_audio_id = $this->model_catalog_product->createProductAudio($product_id, $audio['source'], $audio['sort_order'], $audio['id']); foreach ($audio['title'] as $language_id => $title) { $this->model_catalog_product->createProductAudioDescription($product_audio_id, $language_id, $title); } } } // Add specials $this->model_catalog_product->deleteProductSpecials($product_id); if (isset($this->request->post['special'])) { foreach ($this->request->post['special'] as $special) { $this->model_catalog_product->createProductSpecial($product_id, $special['regular_price'], $special['exclusive_price'], $special['date_start'], $special['date_end'], $special['sort_order']); } } $this->db->commit(); // Cleaning $this->cache->clean($this->auth->getId()); $this->storage->clean($this->auth->getId()); // Set success message $this->session->setUserMessage(array('success' => tt('Product successfully updated!'))); // Admin alert if current user is not verified (updated product has been disabled) if (!$this->auth->isVerified()) { $this->mail->setTo(MAIL_EMAIL_SUPPORT_ADDRESS); $this->mail->setSubject(sprintf(tt('Product has been updated - %s'), PROJECT_NAME)); $this->mail->setText(sprintf(tt('Product ID %s by %s has been updated and waiting for approving!'), $product_id, $this->auth->getUsername())); $this->mail->send(); } $this->response->redirect($this->url->link('account/product')); } $data = $this->_populateForm($this->url->link('account/product/update', 'product_id=' . $product_id)); $data['footer'] = $this->load->controller('common/footer'); $data['header'] = $this->load->controller('common/header'); $data['module_breadcrumbs'] = $this->load->controller('module/breadcrumbs', array(array('name' => tt('Home'), 'href' => $this->url->link('common/home'), 'active' => false), array('name' => tt('Product list'), 'href' => $this->url->link('account/product'), 'active' => false), array('name' => tt('Update product'), 'href' => $this->url->link('account/product/update', 'product_id=' . $product_id), 'active' => true))); // Renter the template $this->response->setOutput($this->load->view('account/product/product_form.tpl', $data)); }
public function update() { $product_id = 0; // Redirect to product create if product_id is not exists if (isset($this->request->get['product_id'])) { $product_id = (int) $this->request->get['product_id']; } else { // Log hack attempt $this->security_log->write('Try to get product without product_id param'); $this->response->redirect($this->url->link('account/product/create')); } // Redirect to login page if user is not logged if (!$this->auth->isLogged()) { $this->response->redirect($this->url->link('account/account/login', 'redirect=' . urlencode($this->url->link('account/product/update', 'product_id=' . $product_id)))); } // Check if user has product if (!$this->model_catalog_product->userHasProduct($this->auth->getId(), $product_id)) { // Log hack attempt $this->security_log->write('Try to get not own\'s product_id #' . $product_id); // Redirect to safe page $this->response->redirect($this->url->link('account/product')); } if ('POST' == $this->request->getRequestMethod() && $this->_validateProductForm()) { // Load dependencies $translate = new Translate(); $color = new Color(); // Create languages registry $languages = array(); foreach ($this->model_common_language->getLanguages() as $language) { $languages[$language->language_id] = $language->code; } // Set active directory $directory = DIR_STORAGE . $this->auth->getId() . DIR_SEPARATOR; // Start transaction $this->db->beginTransaction(); // Add product $this->model_catalog_product->updateProduct($product_id, $this->request->post['category_id'], $this->request->post['currency_id'], $this->request->post['regular_price'], $this->request->post['exclusive_price'], $this->request->post['withdraw_address'], FilterUri::alias($this->request->post['product_description'][$this->language->getId()]['title']), (int) $this->auth->isVerified()); // Add 301 rule if product has new URI $url = new Url($this->db, $this->request, $this->response, $this->url->link('common/home')); $old_url = $this->url->link('catalog/product', 'product_id=' . $product_id); $new_url = $url->link('catalog/product', 'product_id=' . $product_id); if ($old_url != $new_url) { $this->model_common_redirect->createRedirect(301, str_replace($this->url->link('common/home'), false, $old_url), str_replace($this->url->link('common/home'), false, $new_url)); } // Add product description $this->model_catalog_product->deleteProductDescriptions($product_id); foreach ($this->request->post['product_description'] as $language_id => $product_description) { $this->model_catalog_product->createProductDescription($product_id, $language_id, empty(trim($product_description['title'])) ? $translate->string($this->request->post['product_description'][$this->language->getId()]['title'], $this->language->getCode(), $languages[$language_id]) : $product_description['title'], empty(trim($product_description['description'])) ? $translate->string($this->request->post['product_description'][$this->language->getId()]['description'], $this->language->getCode(), $languages[$language_id]) : $product_description['description']); } // Add Tags $this->model_catalog_product->deleteProductToTagByProductId($product_id); // Prepare tags from request foreach ($this->request->post['product_description'] as $language_id => $product_description) { // Process current language not empty field only if (!empty($product_description['tags']) && $language_id == $this->language->getId()) { // Separate a tags string and create multilingual registry foreach (explode(',', $product_description['tags']) as $name) { // Get tag id $name = mb_strtolower(trim($name)); // Saved tags registry if ($tag = $this->model_catalog_tag->getTagByName($name)) { $tag_id = $tag->tag_id; } else { // Create new tag $tag_id = $this->model_catalog_tag->addTag(); // Create descriptions for each language foreach ($languages as $language_id => $code) { $this->model_catalog_tag->addTagDescription($tag_id, $language_id, $translate->string($name, $this->language->getCode(), $code)); } } // Save new relations $this->model_catalog_product->addProductToTag($product_id, $tag_id); } } } // Add file if ($file_content = file_get_contents($directory . $this->request->post['product_file_id'] . '.' . STORAGE_FILE_EXTENSION)) { $this->model_catalog_product->deleteProductFiles($product_id); $product_file_id = $this->model_catalog_product->createProductFile($product_id, md5($file_content), sha1($file_content)); rename($directory . $this->request->post['product_file_id'] . '.' . STORAGE_FILE_EXTENSION, $directory . $product_file_id . '.' . STORAGE_FILE_EXTENSION); } // Add demos $this->model_catalog_product->deleteProductDemos($product_id); if (isset($this->request->post['demo'])) { foreach ($this->request->post['demo'] as $row => $demo) { $product_demo_id = $this->model_catalog_product->createProductDemo($product_id, $demo['sort_order'], $demo['url'], $this->request->post['main_demo'] == $row ? 1 : 0); foreach ($demo['title'] as $language_id => $title) { $this->model_catalog_product->createProductDemoDescription($product_demo_id, $language_id, empty(trim($title)) ? $translate->string($demo['title'][$this->language->getId()], $this->language->getCode(), $languages[$language_id]) : $title); } } } // Update images $this->model_catalog_product->deleteProductImages($product_id); if (isset($this->request->post['image'])) { foreach ($this->request->post['image'] as $row => $image) { // Add new images $product_image_id = $this->model_catalog_product->createProductImage($product_id, $image['sort_order'], $this->request->post['main_image'] == $row ? 1 : 0, isset($image['watermark']) ? 1 : 0); // Generate image titles foreach ($image['title'] as $language_id => $title) { $this->model_catalog_product->createProductImageDescription($product_image_id, $language_id, empty(trim($title)) ? $translate->string($image['title'][$this->language->getId()], $this->language->getCode(), $languages[$language_id]) : $title); } // Extract image colors if ($color->setImage($directory . $image['product_image_id'] . '.' . STORAGE_IMAGE_EXTENSION) && ($colors = $color->getColors())) { foreach ($colors as $key => $value) { $this->model_catalog_product->createProductImageColor($product_image_id, $value['hex'], $value['hue'], $value['saturation'], $value['value'], $value['red'], $value['green'], $value['blue'], $value['frequency']); } } rename($directory . $image['product_image_id'] . '.' . STORAGE_IMAGE_EXTENSION, $directory . $product_image_id . '.' . STORAGE_IMAGE_EXTENSION); } // Generate unique image if others images is not exists } else { $product_image_id = $this->model_catalog_product->createProductImage($product_id, 1, 1, 0, 1); // Generate image titles from product title foreach ($this->request->post['product_description'] as $language_id => $product_description) { $this->model_catalog_product->createProductImageDescription($product_image_id, $language_id, empty(trim($product_description['title'])) ? $translate->string($this->request->post['product_description'][$this->language->getId()]['title'], $this->language->getCode(), $languages[$language_id]) : $product_description['title']); } $identicon = new Identicon(); $image = new Image($identicon->generateImageResource(sha1($product_id), PRODUCT_IMAGE_ORIGINAL_WIDTH, PRODUCT_IMAGE_ORIGINAL_HEIGHT), true); $image->save(DIR_STORAGE . $this->auth->getId() . DIR_SEPARATOR . $product_image_id . '.' . STORAGE_IMAGE_EXTENSION); } // Add videos $this->model_catalog_product->deleteProductVideos($product_id); if (isset($this->request->post['video'])) { foreach ($this->request->post['video'] as $video) { $product_video_id = $this->model_catalog_product->createProductVideo($product_id, isset($video['reduce']) ? 1 : 0, $video['sort_order']); foreach ($video['title'] as $language_id => $title) { $this->model_catalog_product->createProductVideoDescription($product_video_id, $language_id, empty(trim($title)) ? $translate->string($video['title'][$this->language->getId()], $this->language->getCode(), $languages[$language_id]) : $title); } rename($directory . $video['product_video_id'] . '.' . STORAGE_VIDEO_EXTENSION, $directory . $product_video_id . '.' . STORAGE_VIDEO_EXTENSION); } } // Add audios $this->model_catalog_product->deleteProductAudios($product_id); // Add audios if (isset($this->request->post['audio'])) { foreach ($this->request->post['audio'] as $audio) { $product_audio_id = $this->model_catalog_product->createProductAudio($product_id, isset($audio['cut']) ? 1 : 0, $audio['sort_order']); foreach ($audio['title'] as $language_id => $title) { $this->model_catalog_product->createProductAudioDescription($product_audio_id, $language_id, empty(trim($title)) ? $translate->string($audio['title'][$this->language->getId()], $this->language->getCode(), $languages[$language_id]) : $title); } rename($directory . $audio['product_audio_id'] . '.' . STORAGE_AUDIO_EXTENSION, $directory . $product_audio_id . '.' . STORAGE_AUDIO_EXTENSION); } } // Add specials $this->model_catalog_product->deleteProductSpecials($product_id); if (isset($this->request->post['special'])) { foreach ($this->request->post['special'] as $special) { $this->model_catalog_product->createProductSpecial($product_id, $special['regular_price'], $special['exclusive_price'], $special['date_start'], $special['date_end'], $special['sort_order']); } } // Add license conditions $this->model_catalog_product->deleteLicenseConditions($product_id); if (isset($this->request->post['license_conditions'])) { foreach ($this->request->post['license_conditions'] as $license_condition_id => $value) { $this->model_catalog_product->addLicenseConditionValue($product_id, $license_condition_id); } } $this->db->commit(); // Cleaning $this->cache->clean($this->auth->getId()); $this->storage->clean($this->auth->getId()); // Set success message $this->session->setUserMessage(array('success' => tt('Product successfully updated!'))); // Admin alert if current user is not verified (updated product has been disabled) if (!$this->auth->isVerified()) { $this->mail->setSender($this->auth->getEmail()); $this->mail->setFrom($this->auth->getEmail()); $this->mail->setTo(MAIL_EMAIL_SUPPORT_ADDRESS); $this->mail->setSubject(sprintf(tt('Product has been updated - %s'), PROJECT_NAME)); $this->mail->setText(sprintf(tt('Product ID %s by %s has been updated and waiting for approving!'), $product_id, $this->auth->getUsername())); $this->mail->send(); } $this->response->redirect($this->url->link('account/product')); } $data = $this->_populateForm($this->url->link('account/product/update', 'product_id=' . $product_id)); $data['footer'] = $this->load->controller('common/footer'); $data['header'] = $this->load->controller('common/header'); $data['module_breadcrumbs'] = $this->load->controller('module/breadcrumbs', array(array('name' => tt('Home'), 'href' => $this->url->link('common/home'), 'active' => false), array('name' => tt('Product list'), 'href' => $this->url->link('account/product'), 'active' => false), array('name' => tt('Update product'), 'href' => $this->url->link('account/product/update', 'product_id=' . $product_id), 'active' => true))); // Renter the template $this->response->setOutput($this->load->view('account/product/product_form.tpl', $data)); }