/**
  * 0 no multi_lang
  * 1 multi_lang follow id_lang
  * 2 multi_lnag follow code_lang
  */
 public static function getPost($keys = array(), $multi_lang = 0)
 {
     $post = array();
     if ($multi_lang == 0) {
         foreach ($keys as $key) {
             // get value from $_POST
             $post[$key] = Tools::getValue($key);
         }
     } elseif ($multi_lang == 1) {
         foreach ($keys as $key) {
             // get value multi language from $_POST
             foreach (Language::getIDs(false) as $id_lang) {
                 $post[$key . '_' . (int) $id_lang] = Tools::getValue($key . '_' . (int) $id_lang);
             }
         }
     } elseif ($multi_lang == 2) {
         $languages = self::getLangAtt();
         foreach ($keys as $key) {
             // get value multi language from $_POST
             foreach ($languages as $id_code) {
                 $post[$key . '_' . $id_code] = Tools::getValue($key . '_' . $id_code);
             }
         }
     }
     return $post;
 }
 public function postProcess()
 {
     if (Tools::isSubmit('saveConfiguration')) {
         $keys = LeoBlogHelper::getConfigKey(false);
         $post = array();
         foreach ($keys as $key) {
             # validate module
             $post[$key] = Tools::getValue($key);
         }
         $multi_lang_keys = LeoBlogHelper::getConfigKey(true);
         foreach ($multi_lang_keys as $multi_lang_key) {
             foreach (Language::getIDs(false) as $id_lang) {
                 $post[$multi_lang_key . '_' . (int) $id_lang] = Tools::getValue($multi_lang_key . '_' . (int) $id_lang);
             }
         }
         LeoBlogConfig::updateConfigValue('cfg_global', serialize($post));
     }
 }
Beispiel #3
0
 public static function getPost($keys = array(), $lang = false)
 {
     $post = array();
     if ($lang === false) {
         foreach ($keys as $key) {
             // get value from $_POST
             $post[$key] = Tools::getValue($key);
         }
     }
     if ($lang === true) {
         foreach ($keys as $key) {
             // get value multi language from $_POST
             foreach (Language::getIDs(false) as $id_lang) {
                 $post[$key . '_' . (int) $id_lang] = Tools::getValue($key . '_' . (int) $id_lang);
             }
         }
     }
     return $post;
 }
Beispiel #4
0
 /**
  * Copy datas from $_POST to object
  *
  * @param object &$object Object
  * @param string $table Object table
  */
 protected function copyFromPost(&$object, $table)
 {
     /* Classical fields */
     foreach ($_POST as $key => $value) {
         if (array_key_exists($key, $object) && $key != 'id_' . $table) {
             /* Do not take care of password field if empty */
             if ($key == 'passwd' && Tools::getValue('id_' . $table) && empty($value)) {
                 continue;
             }
             /* Automatically encrypt password in MD5 */
             if ($key == 'passwd' && !empty($value)) {
                 $value = Tools::encrypt($value);
             }
             $object->{$key} = $value;
         }
     }
     /* Multilingual fields */
     $rules = call_user_func(array(get_class($object), 'getValidationRules'), get_class($object));
     if (count($rules['validateLang'])) {
         $language_ids = Language::getIDs(false);
         foreach ($language_ids as $id_lang) {
             foreach (array_keys($rules['validateLang']) as $field) {
                 if (Tools::isSubmit($field . '_' . (int) $id_lang)) {
                     $object->{$field}[(int) $id_lang] = Tools::getValue($field . '_' . (int) $id_lang);
                 }
             }
         }
     }
 }
 public function processImageLegends()
 {
     if (Tools::getValue('key_tab') == 'Images' && Tools::getValue('submitAddproductAndStay') == 'update_legends' && Validate::isLoadedObject($product = new Product((int) Tools::getValue('id_product')))) {
         $id_image = (int) Tools::getValue('id_caption');
         $language_ids = Language::getIDs(false);
         foreach ($_POST as $key => $val) {
             if (preg_match('/^legend_([0-9]+)/i', $key, $match)) {
                 foreach ($language_ids as $id_lang) {
                     if ($val && $id_lang == $match[1]) {
                         Db::getInstance()->execute('UPDATE ' . _DB_PREFIX_ . 'image_lang SET legend = "' . pSQL($val) . '" WHERE ' . ($id_image ? 'id_image = ' . (int) $id_image : 'EXISTS (SELECT 1 FROM ' . _DB_PREFIX_ . 'image WHERE ' . _DB_PREFIX_ . 'image.id_image = ' . _DB_PREFIX_ . 'image_lang.id_image AND id_product = ' . (int) $product->id . ')') . ' AND id_lang = ' . (int) $id_lang);
                     }
                 }
             }
         }
     }
 }
Beispiel #6
0
    /**
     * Load default routes group by languages
     */
    protected function loadRoutes($id_shop = null)
    {
        $context = Context::getContext();
        // Load custom routes from modules
        $modules_routes = Hook::exec('moduleRoutes', array('id_shop' => $id_shop), null, true, false);
        if (is_array($modules_routes) && count($modules_routes)) {
            foreach ($modules_routes as $module_route) {
                if (is_array($module_route) && count($module_route)) {
                    foreach ($module_route as $route => $route_details) {
                        if (array_key_exists('controller', $route_details) && array_key_exists('rule', $route_details) && array_key_exists('keywords', $route_details) && array_key_exists('params', $route_details)) {
                            if (!isset($this->default_routes[$route])) {
                                $this->default_routes[$route] = array();
                            }
                            $this->default_routes[$route] = array_merge($this->default_routes[$route], $route_details);
                        }
                    }
                }
            }
        }
        $language_ids = Language::getIDs();
        if (isset($context->language) && !in_array($context->language->id, $language_ids)) {
            $language_ids[] = (int) $context->language->id;
        }
        // Set default routes
        foreach ($language_ids as $id_lang) {
            foreach ($this->default_routes as $id => $route) {
                $this->addRoute($id, $route['rule'], $route['controller'], $id_lang, $route['keywords'], isset($route['params']) ? $route['params'] : array(), $id_shop);
            }
        }
        // Load the custom routes prior the defaults to avoid infinite loops
        if ($this->use_routes) {
            // Load routes from meta table
            $sql = 'SELECT m.page, ml.url_rewrite, ml.id_lang
					FROM `' . _DB_PREFIX_ . 'meta` m
					LEFT JOIN `' . _DB_PREFIX_ . 'meta_lang` ml ON (m.id_meta = ml.id_meta' . Shop::addSqlRestrictionOnLang('ml', $id_shop) . ')
					ORDER BY LENGTH(ml.url_rewrite) DESC';
            if ($results = Db::getInstance()->executeS($sql)) {
                foreach ($results as $row) {
                    if ($row['url_rewrite']) {
                        $this->addRoute($row['page'], $row['url_rewrite'], $row['page'], $row['id_lang'], array(), array(), $id_shop);
                    }
                }
            }
            // Set default empty route if no empty route (that's weird I know)
            if (!$this->empty_route) {
                $this->empty_route = array('routeID' => 'index', 'rule' => '', 'controller' => 'index');
            }
            // Load custom routes
            foreach ($this->default_routes as $route_id => $route_data) {
                if ($custom_route = Configuration::get('PS_ROUTE_' . $route_id, null, null, $id_shop)) {
                    if (isset($context->language) && !in_array($context->language->id, $language_ids)) {
                        $language_ids[] = (int) $context->language->id;
                    }
                    foreach ($language_ids as $id_lang) {
                        $this->addRoute($route_id, $custom_route, $route_data['controller'], $id_lang, $route_data['keywords'], isset($route_data['params']) ? $route_data['params'] : array(), $id_shop);
                    }
                }
            }
        }
    }
Beispiel #7
0
 /**
  * @deprecated 1.5.0.1 (use getFieldsLang())
  * @param array $fields_array
  *
  * @return array
  * @throws PrestaShopException
  */
 protected function getTranslationsFields($fields_array)
 {
     $fields = array();
     if ($this->id_lang == null) {
         foreach (Language::getIDs(false) as $id_lang) {
             $this->makeTranslationFields($fields, $fields_array, $id_lang);
         }
     } else {
         $this->makeTranslationFields($fields, $fields_array, $this->id_lang);
     }
     return $fields;
 }
 protected function manageEntityDeclinatedImages($directory, $normal_image_sizes)
 {
     $normal_image_size_names = array();
     foreach ($normal_image_sizes as $normal_image_size) {
         $normal_image_size_names[] = $normal_image_size['name'];
     }
     // If id is detected
     $object_id = $this->wsObject->urlSegment[2];
     if (!Validate::isUnsignedId($object_id)) {
         throw new WebserviceException('The image id is invalid. Please set a valid id or the "default" value', array(60, 400));
     }
     // For the product case
     if ($this->imageType == 'products') {
         // Get available image ids
         $available_image_ids = array();
         // New Behavior
         foreach (Language::getIDs() as $id_lang) {
             foreach (Image::getImages($id_lang, $object_id) as $image) {
                 $available_image_ids[] = $image['id_image'];
             }
         }
         $available_image_ids = array_unique($available_image_ids, SORT_NUMERIC);
         // If an image id is specified
         if ($this->wsObject->urlSegment[3] != '') {
             if ($this->wsObject->urlSegment[3] == 'bin') {
                 $current_product = new Product($object_id);
                 $this->wsObject->urlSegment[3] = $current_product->getCoverWs();
             }
             if (!Validate::isUnsignedId($object_id) || !in_array($this->wsObject->urlSegment[3], $available_image_ids)) {
                 throw new WebserviceException('This image id does not exist', array(57, 400));
             } else {
                 // Check for new image system
                 $image_id = $this->wsObject->urlSegment[3];
                 $path = implode('/', str_split((string) $image_id));
                 $image_size = $this->wsObject->urlSegment[4];
                 if (file_exists($directory . $path . '/' . $image_id . (strlen($this->wsObject->urlSegment[4]) > 0 ? '-' . $this->wsObject->urlSegment[4] : '') . '.jpg')) {
                     $filename = $directory . $path . '/' . $image_id . (strlen($this->wsObject->urlSegment[4]) > 0 ? '-' . $this->wsObject->urlSegment[4] : '') . '.jpg';
                     $orig_filename = $directory . $path . '/' . $image_id . '.jpg';
                 } else {
                     // else old system or not exists
                     $orig_filename = $directory . $object_id . '-' . $image_id . '.jpg';
                     $filename = $directory . $object_id . '-' . $image_id . '-' . $image_size . '.jpg';
                 }
             }
         } elseif ($this->wsObject->method == 'GET' || $this->wsObject->method == 'HEAD') {
             if ($available_image_ids) {
                 $this->output .= $this->objOutput->getObjectRender()->renderNodeHeader('image', array(), array('id' => $object_id));
                 foreach ($available_image_ids as $available_image_id) {
                     $this->output .= $this->objOutput->getObjectRender()->renderNodeHeader('declination', array(), array('id' => $available_image_id, 'xlink_resource' => $this->wsObject->wsUrl . 'images/' . $this->imageType . '/' . $object_id . '/' . $available_image_id), false);
                 }
                 $this->output .= $this->objOutput->getObjectRender()->renderNodeFooter('image', array());
             } else {
                 $this->objOutput->setStatus(404);
                 $this->wsObject->setOutputEnabled(false);
             }
         }
     } else {
         $orig_filename = $directory . $object_id . '.jpg';
         $image_size = $this->wsObject->urlSegment[3];
         $filename = $directory . $object_id . '-' . $image_size . '.jpg';
     }
     // in case of declinated images list of a product is get
     if ($this->output != '') {
         return true;
     } elseif (isset($image_size) && $image_size != '') {
         // Check the given size
         if ($this->imageType == 'products' && $image_size == 'bin') {
             $filename = $directory . $object_id . '-' . $image_id . '.jpg';
         } elseif (!in_array($image_size, $normal_image_size_names)) {
             $exception = new WebserviceException('This image size does not exist', array(58, 400));
             throw $exception->setDidYouMean($image_size, $normal_image_size_names);
         }
         if (!file_exists($filename)) {
             throw new WebserviceException('This image does not exist on disk', array(59, 500));
         }
         // Display the resized specific image
         $this->imgToDisplay = $filename;
         return true;
     } elseif (isset($orig_filename)) {
         $orig_filename_exists = file_exists($orig_filename);
         return $this->manageDeclinatedImagesCRUD($orig_filename_exists, $orig_filename, $normal_image_sizes, $directory);
     } else {
         return $this->manageDeclinatedImagesCRUD(false, '', $normal_image_sizes, $directory);
     }
 }
Beispiel #9
0
 /**
  * Webservice : getter for the product name
  */
 public function getWSProductName()
 {
     $res = array();
     foreach (Language::getIDs(true) as $id_lang) {
         $res[$id_lang] = Product::getProductName($this->id_product, $this->id_product_attribute, $id_lang);
     }
     return $res;
 }
 /**
  * Copy data values from $_POST to object
  *
  * @param ObjectModel &$object Object
  * @param string $table Object table
  */
 protected function copyFromPost(&$object, $table)
 {
     /* Classical fields */
     foreach ($_POST as $key => $value) {
         if (array_key_exists($key, $object) && $key != 'id_' . $table) {
             /* Do not take care of password field if empty */
             if ($key == 'passwd' && Tools::getValue('id_' . $table) && empty($value)) {
                 continue;
             }
             /* Automatically encrypt password in MD5 */
             if ($key == 'passwd' && !empty($value)) {
                 $value = Tools::encrypt($value);
             }
             $object->{$key} = $value;
         }
     }
     /* Multilingual fields */
     $class_vars = get_class_vars(get_class($object));
     $fields = array();
     if (isset($class_vars['definition']['fields'])) {
         $fields = $class_vars['definition']['fields'];
     }
     foreach ($fields as $field => $params) {
         if (array_key_exists('lang', $params) && $params['lang']) {
             foreach (Language::getIDs(false) as $id_lang) {
                 if (Tools::isSubmit($field . '_' . (int) $id_lang)) {
                     $object->{$field}[(int) $id_lang] = Tools::getValue($field . '_' . (int) $id_lang);
                 }
             }
         }
     }
 }
 public function postProcess()
 {
     // If id_order is sent, we instanciate a new Order object
     if (Tools::isSubmit('id_order') && Tools::getValue('id_order') > 0) {
         $order = new Order(Tools::getValue('id_order'));
         if (!Validate::isLoadedObject($order)) {
             $this->errors[] = Tools::displayError('The order cannot be found within your database.');
         }
         ShopUrl::cacheMainDomainForShop((int) $order->id_shop);
     }
     /* Update shipping number */
     if (Tools::isSubmit('submitShippingNumber') && isset($order)) {
         if ($this->tabAccess['edit'] === '1') {
             $order_carrier = new OrderCarrier(Tools::getValue('id_order_carrier'));
             if (!Validate::isLoadedObject($order_carrier)) {
                 $this->errors[] = Tools::displayError('The order carrier ID is invalid.');
             } elseif (!Validate::isTrackingNumber(Tools::getValue('tracking_number'))) {
                 $this->errors[] = Tools::displayError('The tracking number is incorrect.');
             } else {
                 // update shipping number
                 // Keep these two following lines for backward compatibility, remove on 1.6 version
                 $order->shipping_number = Tools::getValue('tracking_number');
                 $order->update();
                 // Update order_carrier
                 $order_carrier->tracking_number = pSQL(Tools::getValue('tracking_number'));
                 if ($order_carrier->update()) {
                     // Send mail to customer
                     $customer = new Customer((int) $order->id_customer);
                     $carrier = new Carrier((int) $order->id_carrier, $order->id_lang);
                     if (!Validate::isLoadedObject($customer)) {
                         throw new PrestaShopException('Can\'t load Customer object');
                     }
                     if (!Validate::isLoadedObject($carrier)) {
                         throw new PrestaShopException('Can\'t load Carrier object');
                     }
                     $templateVars = array('{followup}' => str_replace('@', $order->shipping_number, $carrier->url), '{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{id_order}' => $order->id, '{shipping_number}' => $order->shipping_number, '{order_name}' => $order->getUniqReference());
                     if (@Mail::Send((int) $order->id_lang, 'in_transit', Mail::l('Package in transit', (int) $order->id_lang), $templateVars, $customer->email, $customer->firstname . ' ' . $customer->lastname, null, null, null, null, _PS_MAIL_DIR_, true, (int) $order->id_shop)) {
                         Hook::exec('actionAdminOrdersTrackingNumberUpdate', array('order' => $order, 'customer' => $customer, 'carrier' => $carrier), null, false, true, false, $order->id_shop);
                         Tools::redirectAdmin(self::$currentIndex . '&id_order=' . $order->id . '&vieworder&conf=4&token=' . $this->token);
                     } else {
                         $this->errors[] = Tools::displayError('An error occurred while sending an email to the customer.');
                     }
                 } else {
                     $this->errors[] = Tools::displayError('The order carrier cannot be updated.');
                 }
             }
         } else {
             $this->errors[] = Tools::displayError('You do not have permission to edit this.');
         }
     } elseif (Tools::isSubmit('submitState') && isset($order)) {
         if ($this->tabAccess['edit'] === '1') {
             $order_state = new OrderState(Tools::getValue('id_order_state'));
             if (!Validate::isLoadedObject($order_state)) {
                 $this->errors[] = Tools::displayError('The new order status is invalid.');
             } else {
                 $current_order_state = $order->getCurrentOrderState();
                 if ($current_order_state->id != $order_state->id) {
                     // Create new OrderHistory
                     $history = new OrderHistory();
                     $history->id_order = $order->id;
                     $history->id_employee = (int) $this->context->employee->id;
                     $use_existings_payment = false;
                     if (!$order->hasInvoice()) {
                         $use_existings_payment = true;
                     }
                     $history->changeIdOrderState((int) $order_state->id, $order, $use_existings_payment);
                     $carrier = new Carrier($order->id_carrier, $order->id_lang);
                     $templateVars = array();
                     if ($history->id_order_state == Configuration::get('PS_OS_SHIPPING') && $order->shipping_number) {
                         $templateVars = array('{followup}' => str_replace('@', $order->shipping_number, $carrier->url));
                     }
                     // Save all changes
                     if ($history->addWithemail(true, $templateVars)) {
                         // synchronizes quantities if needed..
                         if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
                             foreach ($order->getProducts() as $product) {
                                 if (StockAvailable::dependsOnStock($product['product_id'])) {
                                     StockAvailable::synchronize($product['product_id'], (int) $product['id_shop']);
                                 }
                             }
                         }
                         Tools::redirectAdmin(self::$currentIndex . '&id_order=' . (int) $order->id . '&vieworder&token=' . $this->token);
                     }
                     $this->errors[] = Tools::displayError('An error occurred while changing order status, or we were unable to send an email to the customer.');
                 } else {
                     $this->errors[] = Tools::displayError('The order has already been assigned this status.');
                 }
             }
         } else {
             $this->errors[] = Tools::displayError('You do not have permission to edit this.');
         }
     } elseif (Tools::isSubmit('submitMessage') && isset($order)) {
         if ($this->tabAccess['edit'] === '1') {
             $customer = new Customer(Tools::getValue('id_customer'));
             if (!Validate::isLoadedObject($customer)) {
                 $this->errors[] = Tools::displayError('The customer is invalid.');
             } elseif (!Tools::getValue('message')) {
                 $this->errors[] = Tools::displayError('The message cannot be blank.');
             } else {
                 /* Get message rules and and check fields validity */
                 $rules = call_user_func(array('Message', 'getValidationRules'), 'Message');
                 foreach ($rules['required'] as $field) {
                     if (($value = Tools::getValue($field)) == false && (string) $value != '0') {
                         if (!Tools::getValue('id_' . $this->table) || $field != 'passwd') {
                             $this->errors[] = sprintf(Tools::displayError('field %s is required.'), $field);
                         }
                     }
                 }
                 foreach ($rules['size'] as $field => $maxLength) {
                     if (Tools::getValue($field) && Tools::strlen(Tools::getValue($field)) > $maxLength) {
                         $this->errors[] = sprintf(Tools::displayError('field %1$s is too long (%2$d chars max).'), $field, $maxLength);
                     }
                 }
                 foreach ($rules['validate'] as $field => $function) {
                     if (Tools::getValue($field)) {
                         if (!Validate::$function(htmlentities(Tools::getValue($field), ENT_COMPAT, 'UTF-8'))) {
                             $this->errors[] = sprintf(Tools::displayError('field %s is invalid.'), $field);
                         }
                     }
                 }
                 if (!count($this->errors)) {
                     //check if a thread already exist
                     $id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($customer->email, $order->id);
                     if (!$id_customer_thread) {
                         $customer_thread = new CustomerThread();
                         $customer_thread->id_contact = 0;
                         $customer_thread->id_customer = (int) $order->id_customer;
                         $customer_thread->id_shop = (int) $this->context->shop->id;
                         $customer_thread->id_order = (int) $order->id;
                         $customer_thread->id_lang = (int) $this->context->language->id;
                         $customer_thread->email = $customer->email;
                         $customer_thread->status = 'open';
                         $customer_thread->token = Tools::passwdGen(12);
                         $customer_thread->add();
                     } else {
                         $customer_thread = new CustomerThread((int) $id_customer_thread);
                     }
                     $customer_message = new CustomerMessage();
                     $customer_message->id_customer_thread = $customer_thread->id;
                     $customer_message->id_employee = (int) $this->context->employee->id;
                     $customer_message->message = Tools::getValue('message');
                     $customer_message->private = Tools::getValue('visibility');
                     if (!$customer_message->add()) {
                         $this->errors[] = Tools::displayError('An error occurred while saving the message.');
                     } elseif ($customer_message->private) {
                         Tools::redirectAdmin(self::$currentIndex . '&id_order=' . (int) $order->id . '&vieworder&conf=11&token=' . $this->token);
                     } else {
                         $message = $customer_message->message;
                         if (Configuration::get('PS_MAIL_TYPE', null, null, $order->id_shop) != Mail::TYPE_TEXT) {
                             $message = Tools::nl2br($customer_message->message);
                         }
                         $varsTpl = array('{lastname}' => $customer->lastname, '{firstname}' => $customer->firstname, '{id_order}' => $order->id, '{order_name}' => $order->getUniqReference(), '{message}' => $message);
                         if (@Mail::Send((int) $order->id_lang, 'order_merchant_comment', Mail::l('New message regarding your order', (int) $order->id_lang), $varsTpl, $customer->email, $customer->firstname . ' ' . $customer->lastname, null, null, null, null, _PS_MAIL_DIR_, true, (int) $order->id_shop)) {
                             Tools::redirectAdmin(self::$currentIndex . '&id_order=' . $order->id . '&vieworder&conf=11' . '&token=' . $this->token);
                         }
                     }
                     $this->errors[] = Tools::displayError('An error occurred while sending an email to the customer.');
                 }
             }
         } else {
             $this->errors[] = Tools::displayError('You do not have permission to delete this.');
         }
     } elseif (Tools::isSubmit('partialRefund') && isset($order)) {
         if ($this->tabAccess['edit'] == '1') {
             if (Tools::isSubmit('partialRefundProduct') && ($refunds = Tools::getValue('partialRefundProduct')) && is_array($refunds)) {
                 $amount = 0;
                 $order_detail_list = array();
                 $full_quantity_list = array();
                 foreach ($refunds as $id_order_detail => $amount_detail) {
                     $quantity = Tools::getValue('partialRefundProductQuantity');
                     if (!$quantity[$id_order_detail]) {
                         continue;
                     }
                     $full_quantity_list[$id_order_detail] = (int) $quantity[$id_order_detail];
                     $order_detail_list[$id_order_detail] = array('quantity' => (int) $quantity[$id_order_detail], 'id_order_detail' => (int) $id_order_detail);
                     $order_detail = new OrderDetail((int) $id_order_detail);
                     if (empty($amount_detail)) {
                         $order_detail_list[$id_order_detail]['unit_price'] = !Tools::getValue('TaxMethod') ? $order_detail->unit_price_tax_excl : $order_detail->unit_price_tax_incl;
                         $order_detail_list[$id_order_detail]['amount'] = $order_detail->unit_price_tax_incl * $order_detail_list[$id_order_detail]['quantity'];
                     } else {
                         $order_detail_list[$id_order_detail]['amount'] = (double) str_replace(',', '.', $amount_detail);
                         $order_detail_list[$id_order_detail]['unit_price'] = $order_detail_list[$id_order_detail]['amount'] / $order_detail_list[$id_order_detail]['quantity'];
                     }
                     $amount += $order_detail_list[$id_order_detail]['amount'];
                     if (!$order->hasBeenDelivered() || $order->hasBeenDelivered() && Tools::isSubmit('reinjectQuantities') && $order_detail_list[$id_order_detail]['quantity'] > 0) {
                         $this->reinjectQuantity($order_detail, $order_detail_list[$id_order_detail]['quantity']);
                     }
                 }
                 $shipping_cost_amount = (double) str_replace(',', '.', Tools::getValue('partialRefundShippingCost')) ? (double) str_replace(',', '.', Tools::getValue('partialRefundShippingCost')) : false;
                 if ($amount == 0 && $shipping_cost_amount == 0) {
                     if (!empty($refunds)) {
                         $this->errors[] = Tools::displayError('Please enter a quantity to proceed with your refund.');
                     } else {
                         $this->errors[] = Tools::displayError('Please enter an amount to proceed with your refund.');
                     }
                     return false;
                 }
                 $choosen = false;
                 $voucher = 0;
                 if ((int) Tools::getValue('refund_voucher_off') == 1) {
                     $amount -= $voucher = (double) Tools::getValue('order_discount_price');
                 } elseif ((int) Tools::getValue('refund_voucher_off') == 2) {
                     $choosen = true;
                     $amount = $voucher = (double) Tools::getValue('refund_voucher_choose');
                 }
                 if ($shipping_cost_amount > 0) {
                     if (!Tools::getValue('TaxMethod')) {
                         $tax = new Tax();
                         $tax->rate = $order->carrier_tax_rate;
                         $tax_calculator = new TaxCalculator(array($tax));
                         $amount += $tax_calculator->addTaxes($shipping_cost_amount);
                     } else {
                         $amount += $shipping_cost_amount;
                     }
                 }
                 $order_carrier = new OrderCarrier((int) $order->getIdOrderCarrier());
                 if (Validate::isLoadedObject($order_carrier)) {
                     $order_carrier->weight = (double) $order->getTotalWeight();
                     if ($order_carrier->update()) {
                         $order->weight = sprintf("%.3f " . Configuration::get('PS_WEIGHT_UNIT'), $order_carrier->weight);
                     }
                 }
                 if ($amount >= 0) {
                     if (!OrderSlip::create($order, $order_detail_list, $shipping_cost_amount, $voucher, $choosen, Tools::getValue('TaxMethod') ? false : true)) {
                         $this->errors[] = Tools::displayError('You cannot generate a partial credit slip.');
                     } else {
                         Hook::exec('actionOrderSlipAdd', array('order' => $order, 'productList' => $order_detail_list, 'qtyList' => $full_quantity_list), null, false, true, false, $order->id_shop);
                         $customer = new Customer((int) $order->id_customer);
                         $params['{lastname}'] = $customer->lastname;
                         $params['{firstname}'] = $customer->firstname;
                         $params['{id_order}'] = $order->id;
                         $params['{order_name}'] = $order->getUniqReference();
                         @Mail::Send((int) $order->id_lang, 'credit_slip', Mail::l('New credit slip regarding your order', (int) $order->id_lang), $params, $customer->email, $customer->firstname . ' ' . $customer->lastname, null, null, null, null, _PS_MAIL_DIR_, true, (int) $order->id_shop);
                     }
                     foreach ($order_detail_list as &$product) {
                         $order_detail = new OrderDetail((int) $product['id_order_detail']);
                         if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
                             StockAvailable::synchronize($order_detail->product_id);
                         }
                     }
                     // Generate voucher
                     if (Tools::isSubmit('generateDiscountRefund') && !count($this->errors) && $amount > 0) {
                         $cart_rule = new CartRule();
                         $cart_rule->description = sprintf($this->l('Credit slip for order #%d'), $order->id);
                         $language_ids = Language::getIDs(false);
                         foreach ($language_ids as $id_lang) {
                             // Define a temporary name
                             $cart_rule->name[$id_lang] = sprintf('V0C%1$dO%2$d', $order->id_customer, $order->id);
                         }
                         // Define a temporary code
                         $cart_rule->code = sprintf('V0C%1$dO%2$d', $order->id_customer, $order->id);
                         $cart_rule->quantity = 1;
                         $cart_rule->quantity_per_user = 1;
                         // Specific to the customer
                         $cart_rule->id_customer = $order->id_customer;
                         $now = time();
                         $cart_rule->date_from = date('Y-m-d H:i:s', $now);
                         $cart_rule->date_to = date('Y-m-d H:i:s', strtotime('+1 year'));
                         $cart_rule->partial_use = 1;
                         $cart_rule->active = 1;
                         $cart_rule->reduction_amount = $amount;
                         $cart_rule->reduction_tax = true;
                         $cart_rule->minimum_amount_currency = $order->id_currency;
                         $cart_rule->reduction_currency = $order->id_currency;
                         if (!$cart_rule->add()) {
                             $this->errors[] = Tools::displayError('You cannot generate a voucher.');
                         } else {
                             // Update the voucher code and name
                             foreach ($language_ids as $id_lang) {
                                 $cart_rule->name[$id_lang] = sprintf('V%1$dC%2$dO%3$d', $cart_rule->id, $order->id_customer, $order->id);
                             }
                             $cart_rule->code = sprintf('V%1$dC%2$dO%3$d', $cart_rule->id, $order->id_customer, $order->id);
                             if (!$cart_rule->update()) {
                                 $this->errors[] = Tools::displayError('You cannot generate a voucher.');
                             } else {
                                 $currency = $this->context->currency;
                                 $customer = new Customer((int) $order->id_customer);
                                 $params['{lastname}'] = $customer->lastname;
                                 $params['{firstname}'] = $customer->firstname;
                                 $params['{id_order}'] = $order->id;
                                 $params['{order_name}'] = $order->getUniqReference();
                                 $params['{voucher_amount}'] = Tools::displayPrice($cart_rule->reduction_amount, $currency, false);
                                 $params['{voucher_num}'] = $cart_rule->code;
                                 @Mail::Send((int) $order->id_lang, 'voucher', sprintf(Mail::l('New voucher for your order #%s', (int) $order->id_lang), $order->reference), $params, $customer->email, $customer->firstname . ' ' . $customer->lastname, null, null, null, null, _PS_MAIL_DIR_, true, (int) $order->id_shop);
                             }
                         }
                     }
                 } else {
                     if (!empty($refunds)) {
                         $this->errors[] = Tools::displayError('Please enter a quantity to proceed with your refund.');
                     } else {
                         $this->errors[] = Tools::displayError('Please enter an amount to proceed with your refund.');
                     }
                 }
                 // Redirect if no errors
                 if (!count($this->errors)) {
                     Tools::redirectAdmin(self::$currentIndex . '&id_order=' . $order->id . '&vieworder&conf=30&token=' . $this->token);
                 }
             } else {
                 $this->errors[] = Tools::displayError('The partial refund data is incorrect.');
             }
         } else {
             $this->errors[] = Tools::displayError('You do not have permission to delete this.');
         }
     } elseif (Tools::isSubmit('cancelProduct') && isset($order)) {
         if ($this->tabAccess['delete'] === '1') {
             if (!Tools::isSubmit('id_order_detail') && !Tools::isSubmit('id_customization')) {
                 $this->errors[] = Tools::displayError('You must select a product.');
             } elseif (!Tools::isSubmit('cancelQuantity') && !Tools::isSubmit('cancelCustomizationQuantity')) {
                 $this->errors[] = Tools::displayError('You must enter a quantity.');
             } else {
                 $productList = Tools::getValue('id_order_detail');
                 if ($productList) {
                     $productList = array_map('intval', $productList);
                 }
                 $customizationList = Tools::getValue('id_customization');
                 if ($customizationList) {
                     $customizationList = array_map('intval', $customizationList);
                 }
                 $qtyList = Tools::getValue('cancelQuantity');
                 if ($qtyList) {
                     $qtyList = array_map('intval', $qtyList);
                 }
                 $customizationQtyList = Tools::getValue('cancelCustomizationQuantity');
                 if ($customizationQtyList) {
                     $customizationQtyList = array_map('intval', $customizationQtyList);
                 }
                 $full_product_list = $productList;
                 $full_quantity_list = $qtyList;
                 if ($customizationList) {
                     foreach ($customizationList as $key => $id_order_detail) {
                         $full_product_list[(int) $id_order_detail] = $id_order_detail;
                         if (isset($customizationQtyList[$key])) {
                             $full_quantity_list[(int) $id_order_detail] += $customizationQtyList[$key];
                         }
                     }
                 }
                 if ($productList || $customizationList) {
                     if ($productList) {
                         $id_cart = Cart::getCartIdByOrderId($order->id);
                         $customization_quantities = Customization::countQuantityByCart($id_cart);
                         foreach ($productList as $key => $id_order_detail) {
                             $qtyCancelProduct = abs($qtyList[$key]);
                             if (!$qtyCancelProduct) {
                                 $this->errors[] = Tools::displayError('No quantity has been selected for this product.');
                             }
                             $order_detail = new OrderDetail($id_order_detail);
                             $customization_quantity = 0;
                             if (array_key_exists($order_detail->product_id, $customization_quantities) && array_key_exists($order_detail->product_attribute_id, $customization_quantities[$order_detail->product_id])) {
                                 $customization_quantity = (int) $customization_quantities[$order_detail->product_id][$order_detail->product_attribute_id];
                             }
                             if ($order_detail->product_quantity - $customization_quantity - $order_detail->product_quantity_refunded - $order_detail->product_quantity_return < $qtyCancelProduct) {
                                 $this->errors[] = Tools::displayError('An invalid quantity was selected for this product.');
                             }
                         }
                     }
                     if ($customizationList) {
                         $customization_quantities = Customization::retrieveQuantitiesFromIds(array_keys($customizationList));
                         foreach ($customizationList as $id_customization => $id_order_detail) {
                             $qtyCancelProduct = abs($customizationQtyList[$id_customization]);
                             $customization_quantity = $customization_quantities[$id_customization];
                             if (!$qtyCancelProduct) {
                                 $this->errors[] = Tools::displayError('No quantity has been selected for this product.');
                             }
                             if ($qtyCancelProduct > $customization_quantity['quantity'] - ($customization_quantity['quantity_refunded'] + $customization_quantity['quantity_returned'])) {
                                 $this->errors[] = Tools::displayError('An invalid quantity was selected for this product.');
                             }
                         }
                     }
                     if (!count($this->errors) && $productList) {
                         foreach ($productList as $key => $id_order_detail) {
                             $qty_cancel_product = abs($qtyList[$key]);
                             $order_detail = new OrderDetail((int) $id_order_detail);
                             if (!$order->hasBeenDelivered() || $order->hasBeenDelivered() && Tools::isSubmit('reinjectQuantities') && $qty_cancel_product > 0) {
                                 $this->reinjectQuantity($order_detail, $qty_cancel_product);
                             }
                             // Delete product
                             $order_detail = new OrderDetail((int) $id_order_detail);
                             if (!$order->deleteProduct($order, $order_detail, $qty_cancel_product)) {
                                 $this->errors[] = Tools::displayError('An error occurred while attempting to delete the product.') . ' <span class="bold">' . $order_detail->product_name . '</span>';
                             }
                             // Update weight SUM
                             $order_carrier = new OrderCarrier((int) $order->getIdOrderCarrier());
                             if (Validate::isLoadedObject($order_carrier)) {
                                 $order_carrier->weight = (double) $order->getTotalWeight();
                                 if ($order_carrier->update()) {
                                     $order->weight = sprintf("%.3f " . Configuration::get('PS_WEIGHT_UNIT'), $order_carrier->weight);
                                 }
                             }
                             if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && StockAvailable::dependsOnStock($order_detail->product_id)) {
                                 StockAvailable::synchronize($order_detail->product_id);
                             }
                             Hook::exec('actionProductCancel', array('order' => $order, 'id_order_detail' => (int) $id_order_detail), null, false, true, false, $order->id_shop);
                         }
                     }
                     if (!count($this->errors) && $customizationList) {
                         foreach ($customizationList as $id_customization => $id_order_detail) {
                             $order_detail = new OrderDetail((int) $id_order_detail);
                             $qtyCancelProduct = abs($customizationQtyList[$id_customization]);
                             if (!$order->deleteCustomization($id_customization, $qtyCancelProduct, $order_detail)) {
                                 $this->errors[] = Tools::displayError('An error occurred while attempting to delete product customization.') . ' ' . $id_customization;
                             }
                         }
                     }
                     // E-mail params
                     if ((Tools::isSubmit('generateCreditSlip') || Tools::isSubmit('generateDiscount')) && !count($this->errors)) {
                         $customer = new Customer((int) $order->id_customer);
                         $params['{lastname}'] = $customer->lastname;
                         $params['{firstname}'] = $customer->firstname;
                         $params['{id_order}'] = $order->id;
                         $params['{order_name}'] = $order->getUniqReference();
                     }
                     // Generate credit slip
                     if (Tools::isSubmit('generateCreditSlip') && !count($this->errors)) {
                         $product_list = array();
                         $amount = $order_detail->unit_price_tax_incl * $full_quantity_list[$id_order_detail];
                         $choosen = false;
                         if ((int) Tools::getValue('refund_total_voucher_off') == 1) {
                             $amount -= $voucher = (double) Tools::getValue('order_discount_price');
                         } elseif ((int) Tools::getValue('refund_total_voucher_off') == 2) {
                             $choosen = true;
                             $amount = $voucher = (double) Tools::getValue('refund_total_voucher_choose');
                         }
                         foreach ($full_product_list as $id_order_detail) {
                             $order_detail = new OrderDetail((int) $id_order_detail);
                             $product_list[$id_order_detail] = array('id_order_detail' => $id_order_detail, 'quantity' => $full_quantity_list[$id_order_detail], 'unit_price' => $order_detail->unit_price_tax_excl, 'amount' => isset($amount) ? $amount : $order_detail->unit_price_tax_incl * $full_quantity_list[$id_order_detail]);
                         }
                         $shipping = Tools::isSubmit('shippingBack') ? null : false;
                         if (!OrderSlip::create($order, $product_list, $shipping, $voucher, $choosen)) {
                             $this->errors[] = Tools::displayError('A credit slip cannot be generated. ');
                         } else {
                             Hook::exec('actionOrderSlipAdd', array('order' => $order, 'productList' => $full_product_list, 'qtyList' => $full_quantity_list), null, false, true, false, $order->id_shop);
                             @Mail::Send((int) $order->id_lang, 'credit_slip', Mail::l('New credit slip regarding your order', (int) $order->id_lang), $params, $customer->email, $customer->firstname . ' ' . $customer->lastname, null, null, null, null, _PS_MAIL_DIR_, true, (int) $order->id_shop);
                         }
                     }
                     // Generate voucher
                     if (Tools::isSubmit('generateDiscount') && !count($this->errors)) {
                         $cartrule = new CartRule();
                         $language_ids = Language::getIDs((bool) $order);
                         $cartrule->description = sprintf($this->l('Credit card slip for order #%d'), $order->id);
                         foreach ($language_ids as $id_lang) {
                             // Define a temporary name
                             $cartrule->name[$id_lang] = 'V0C' . (int) $order->id_customer . 'O' . (int) $order->id;
                         }
                         // Define a temporary code
                         $cartrule->code = 'V0C' . (int) $order->id_customer . 'O' . (int) $order->id;
                         $cartrule->quantity = 1;
                         $cartrule->quantity_per_user = 1;
                         // Specific to the customer
                         $cartrule->id_customer = $order->id_customer;
                         $now = time();
                         $cartrule->date_from = date('Y-m-d H:i:s', $now);
                         $cartrule->date_to = date('Y-m-d H:i:s', $now + 3600 * 24 * 365.25);
                         /* 1 year */
                         $cartrule->active = 1;
                         $products = $order->getProducts(false, $full_product_list, $full_quantity_list);
                         $total = 0;
                         foreach ($products as $product) {
                             $total += $product['unit_price_tax_incl'] * $product['product_quantity'];
                         }
                         if (Tools::isSubmit('shippingBack')) {
                             $total += $order->total_shipping;
                         }
                         if ((int) Tools::getValue('refund_total_voucher_off') == 1) {
                             $total -= (double) Tools::getValue('order_discount_price');
                         } elseif ((int) Tools::getValue('refund_total_voucher_off') == 2) {
                             $total = (double) Tools::getValue('refund_total_voucher_choose');
                         }
                         $cartrule->reduction_amount = $total;
                         $cartrule->reduction_tax = true;
                         $cartrule->minimum_amount_currency = $order->id_currency;
                         $cartrule->reduction_currency = $order->id_currency;
                         if (!$cartrule->add()) {
                             $this->errors[] = Tools::displayError('You cannot generate a voucher.');
                         } else {
                             // Update the voucher code and name
                             foreach ($language_ids as $id_lang) {
                                 $cartrule->name[$id_lang] = 'V' . (int) $cartrule->id . 'C' . (int) $order->id_customer . 'O' . $order->id;
                             }
                             $cartrule->code = 'V' . (int) $cartrule->id . 'C' . (int) $order->id_customer . 'O' . $order->id;
                             if (!$cartrule->update()) {
                                 $this->errors[] = Tools::displayError('You cannot generate a voucher.');
                             } else {
                                 $currency = $this->context->currency;
                                 $params['{voucher_amount}'] = Tools::displayPrice($cartrule->reduction_amount, $currency, false);
                                 $params['{voucher_num}'] = $cartrule->code;
                                 @Mail::Send((int) $order->id_lang, 'voucher', sprintf(Mail::l('New voucher for your order #%s', (int) $order->id_lang), $order->reference), $params, $customer->email, $customer->firstname . ' ' . $customer->lastname, null, null, null, null, _PS_MAIL_DIR_, true, (int) $order->id_shop);
                             }
                         }
                     }
                 } else {
                     $this->errors[] = Tools::displayError('No product or quantity has been selected.');
                 }
                 // Redirect if no errors
                 if (!count($this->errors)) {
                     Tools::redirectAdmin(self::$currentIndex . '&id_order=' . $order->id . '&vieworder&conf=31&token=' . $this->token);
                 }
             }
         } else {
             $this->errors[] = Tools::displayError('You do not have permission to delete this.');
         }
     } elseif (Tools::isSubmit('messageReaded')) {
         Message::markAsReaded(Tools::getValue('messageReaded'), $this->context->employee->id);
     } elseif (Tools::isSubmit('submitAddPayment') && isset($order)) {
         if ($this->tabAccess['edit'] === '1') {
             $amount = str_replace(',', '.', Tools::getValue('payment_amount'));
             $currency = new Currency(Tools::getValue('payment_currency'));
             $order_has_invoice = $order->hasInvoice();
             if ($order_has_invoice) {
                 $order_invoice = new OrderInvoice(Tools::getValue('payment_invoice'));
             } else {
                 $order_invoice = null;
             }
             if (!Validate::isLoadedObject($order)) {
                 $this->errors[] = Tools::displayError('The order cannot be found');
             } elseif (!Validate::isNegativePrice($amount) || !(double) $amount) {
                 $this->errors[] = Tools::displayError('The amount is invalid.');
             } elseif (!Validate::isGenericName(Tools::getValue('payment_method'))) {
                 $this->errors[] = Tools::displayError('The selected payment method is invalid.');
             } elseif (!Validate::isString(Tools::getValue('payment_transaction_id'))) {
                 $this->errors[] = Tools::displayError('The transaction ID is invalid.');
             } elseif (!Validate::isLoadedObject($currency)) {
                 $this->errors[] = Tools::displayError('The selected currency is invalid.');
             } elseif ($order_has_invoice && !Validate::isLoadedObject($order_invoice)) {
                 $this->errors[] = Tools::displayError('The invoice is invalid.');
             } elseif (!Validate::isDate(Tools::getValue('payment_date'))) {
                 $this->errors[] = Tools::displayError('The date is invalid');
             } else {
                 if (!$order->addOrderPayment($amount, Tools::getValue('payment_method'), Tools::getValue('payment_transaction_id'), $currency, Tools::getValue('payment_date'), $order_invoice)) {
                     $this->errors[] = Tools::displayError('An error occurred during payment.');
                 } else {
                     Tools::redirectAdmin(self::$currentIndex . '&id_order=' . $order->id . '&vieworder&conf=4&token=' . $this->token);
                 }
             }
         } else {
             $this->errors[] = Tools::displayError('You do not have permission to edit this.');
         }
     } elseif (Tools::isSubmit('submitEditNote')) {
         $note = Tools::getValue('note');
         $order_invoice = new OrderInvoice((int) Tools::getValue('id_order_invoice'));
         if (Validate::isLoadedObject($order_invoice) && Validate::isCleanHtml($note)) {
             if ($this->tabAccess['edit'] === '1') {
                 $order_invoice->note = $note;
                 if ($order_invoice->save()) {
                     Tools::redirectAdmin(self::$currentIndex . '&id_order=' . $order_invoice->id_order . '&vieworder&conf=4&token=' . $this->token);
                 } else {
                     $this->errors[] = Tools::displayError('The invoice note was not saved.');
                 }
             } else {
                 $this->errors[] = Tools::displayError('You do not have permission to edit this.');
             }
         } else {
             $this->errors[] = Tools::displayError('The invoice for edit note was unable to load. ');
         }
     } elseif (Tools::isSubmit('submitAddOrder') && ($id_cart = Tools::getValue('id_cart')) && ($module_name = Tools::getValue('payment_module_name')) && ($id_order_state = Tools::getValue('id_order_state')) && Validate::isModuleName($module_name)) {
         if ($this->tabAccess['edit'] === '1') {
             if (!Configuration::get('PS_CATALOG_MODE')) {
                 $payment_module = Module::getInstanceByName($module_name);
             } else {
                 $payment_module = new BoOrder();
             }
             $cart = new Cart((int) $id_cart);
             Context::getContext()->currency = new Currency((int) $cart->id_currency);
             Context::getContext()->customer = new Customer((int) $cart->id_customer);
             $bad_delivery = false;
             if (($bad_delivery = (bool) (!Address::isCountryActiveById((int) $cart->id_address_delivery))) || !Address::isCountryActiveById((int) $cart->id_address_invoice)) {
                 if ($bad_delivery) {
                     $this->errors[] = Tools::displayError('This delivery address country is not active.');
                 } else {
                     $this->errors[] = Tools::displayError('This invoice address country is not active.');
                 }
             } else {
                 $employee = new Employee((int) Context::getContext()->cookie->id_employee);
                 $payment_module->validateOrder((int) $cart->id, (int) $id_order_state, $cart->getOrderTotal(true, Cart::BOTH), $payment_module->displayName, $this->l('Manual order -- Employee:') . ' ' . substr($employee->firstname, 0, 1) . '. ' . $employee->lastname, array(), null, false, $cart->secure_key);
                 if ($payment_module->currentOrder) {
                     Tools::redirectAdmin(self::$currentIndex . '&id_order=' . $payment_module->currentOrder . '&vieworder' . '&token=' . $this->token);
                 }
             }
         } else {
             $this->errors[] = Tools::displayError('You do not have permission to add this.');
         }
     } elseif ((Tools::isSubmit('submitAddressShipping') || Tools::isSubmit('submitAddressInvoice')) && isset($order)) {
         if ($this->tabAccess['edit'] === '1') {
             $address = new Address(Tools::getValue('id_address'));
             if (Validate::isLoadedObject($address)) {
                 // Update the address on order
                 if (Tools::isSubmit('submitAddressShipping')) {
                     $order->id_address_delivery = $address->id;
                 } elseif (Tools::isSubmit('submitAddressInvoice')) {
                     $order->id_address_invoice = $address->id;
                 }
                 $order->update();
                 Tools::redirectAdmin(self::$currentIndex . '&id_order=' . $order->id . '&vieworder&conf=4&token=' . $this->token);
             } else {
                 $this->errors[] = Tools::displayError('This address can\'t be loaded');
             }
         } else {
             $this->errors[] = Tools::displayError('You do not have permission to edit this.');
         }
     } elseif (Tools::isSubmit('submitChangeCurrency') && isset($order)) {
         if ($this->tabAccess['edit'] === '1') {
             if (Tools::getValue('new_currency') != $order->id_currency && !$order->valid) {
                 $old_currency = new Currency($order->id_currency);
                 $currency = new Currency(Tools::getValue('new_currency'));
                 if (!Validate::isLoadedObject($currency)) {
                     throw new PrestaShopException('Can\'t load Currency object');
                 }
                 // Update order detail amount
                 foreach ($order->getOrderDetailList() as $row) {
                     $order_detail = new OrderDetail($row['id_order_detail']);
                     $fields = array('ecotax', 'product_price', 'reduction_amount', 'total_shipping_price_tax_excl', 'total_shipping_price_tax_incl', 'total_price_tax_incl', 'total_price_tax_excl', 'product_quantity_discount', 'purchase_supplier_price', 'reduction_amount', 'reduction_amount_tax_incl', 'reduction_amount_tax_excl', 'unit_price_tax_incl', 'unit_price_tax_excl', 'original_product_price');
                     foreach ($fields as $field) {
                         $order_detail->{$field} = Tools::convertPriceFull($order_detail->{$field}, $old_currency, $currency);
                     }
                     $order_detail->update();
                     $order_detail->updateTaxAmount($order);
                 }
                 $id_order_carrier = (int) $order->getIdOrderCarrier();
                 if ($id_order_carrier) {
                     $order_carrier = $order_carrier = new OrderCarrier((int) $order->getIdOrderCarrier());
                     $order_carrier->shipping_cost_tax_excl = (double) Tools::convertPriceFull($order_carrier->shipping_cost_tax_excl, $old_currency, $currency);
                     $order_carrier->shipping_cost_tax_incl = (double) Tools::convertPriceFull($order_carrier->shipping_cost_tax_incl, $old_currency, $currency);
                     $order_carrier->update();
                 }
                 // Update order && order_invoice amount
                 $fields = array('total_discounts', 'total_discounts_tax_incl', 'total_discounts_tax_excl', 'total_discount_tax_excl', 'total_discount_tax_incl', 'total_paid', 'total_paid_tax_incl', 'total_paid_tax_excl', 'total_paid_real', 'total_products', 'total_products_wt', 'total_shipping', 'total_shipping_tax_incl', 'total_shipping_tax_excl', 'total_wrapping', 'total_wrapping_tax_incl', 'total_wrapping_tax_excl');
                 $invoices = $order->getInvoicesCollection();
                 if ($invoices) {
                     foreach ($invoices as $invoice) {
                         foreach ($fields as $field) {
                             if (isset($invoice->{$field})) {
                                 $invoice->{$field} = Tools::convertPriceFull($invoice->{$field}, $old_currency, $currency);
                             }
                         }
                         $invoice->save();
                     }
                 }
                 foreach ($fields as $field) {
                     if (isset($order->{$field})) {
                         $order->{$field} = Tools::convertPriceFull($order->{$field}, $old_currency, $currency);
                     }
                 }
                 // Update currency in order
                 $order->id_currency = $currency->id;
                 // Update exchange rate
                 $order->conversion_rate = (double) $currency->conversion_rate;
                 $order->update();
             } else {
                 $this->errors[] = Tools::displayError('You cannot change the currency.');
             }
         } else {
             $this->errors[] = Tools::displayError('You do not have permission to edit this.');
         }
     } elseif (Tools::isSubmit('submitGenerateInvoice') && isset($order)) {
         if (!Configuration::get('PS_INVOICE', null, null, $order->id_shop)) {
             $this->errors[] = Tools::displayError('Invoice management has been disabled.');
         } elseif ($order->hasInvoice()) {
             $this->errors[] = Tools::displayError('This order already has an invoice.');
         } else {
             $order->setInvoice(true);
             Tools::redirectAdmin(self::$currentIndex . '&id_order=' . $order->id . '&vieworder&conf=4&token=' . $this->token);
         }
     } elseif (Tools::isSubmit('submitDeleteVoucher') && isset($order)) {
         if ($this->tabAccess['edit'] === '1') {
             $order_cart_rule = new OrderCartRule(Tools::getValue('id_order_cart_rule'));
             if (Validate::isLoadedObject($order_cart_rule) && $order_cart_rule->id_order == $order->id) {
                 if ($order_cart_rule->id_order_invoice) {
                     $order_invoice = new OrderInvoice($order_cart_rule->id_order_invoice);
                     if (!Validate::isLoadedObject($order_invoice)) {
                         throw new PrestaShopException('Can\'t load Order Invoice object');
                     }
                     // Update amounts of Order Invoice
                     $order_invoice->total_discount_tax_excl -= $order_cart_rule->value_tax_excl;
                     $order_invoice->total_discount_tax_incl -= $order_cart_rule->value;
                     $order_invoice->total_paid_tax_excl += $order_cart_rule->value_tax_excl;
                     $order_invoice->total_paid_tax_incl += $order_cart_rule->value;
                     // Update Order Invoice
                     $order_invoice->update();
                 }
                 // Update amounts of order
                 $order->total_discounts -= $order_cart_rule->value;
                 $order->total_discounts_tax_incl -= $order_cart_rule->value;
                 $order->total_discounts_tax_excl -= $order_cart_rule->value_tax_excl;
                 $order->total_paid += $order_cart_rule->value;
                 $order->total_paid_tax_incl += $order_cart_rule->value;
                 $order->total_paid_tax_excl += $order_cart_rule->value_tax_excl;
                 // Delete Order Cart Rule and update Order
                 $order_cart_rule->delete();
                 $order->update();
                 Tools::redirectAdmin(self::$currentIndex . '&id_order=' . $order->id . '&vieworder&conf=4&token=' . $this->token);
             } else {
                 $this->errors[] = Tools::displayError('You cannot edit this cart rule.');
             }
         } else {
             $this->errors[] = Tools::displayError('You do not have permission to edit this.');
         }
     } elseif (Tools::isSubmit('submitNewVoucher') && isset($order)) {
         if ($this->tabAccess['edit'] === '1') {
             if (!Tools::getValue('discount_name')) {
                 $this->errors[] = Tools::displayError('You must specify a name in order to create a new discount.');
             } else {
                 if ($order->hasInvoice()) {
                     // If the discount is for only one invoice
                     if (!Tools::isSubmit('discount_all_invoices')) {
                         $order_invoice = new OrderInvoice(Tools::getValue('discount_invoice'));
                         if (!Validate::isLoadedObject($order_invoice)) {
                             throw new PrestaShopException('Can\'t load Order Invoice object');
                         }
                     }
                 }
                 $cart_rules = array();
                 $discount_value = (double) str_replace(',', '.', Tools::getValue('discount_value'));
                 switch (Tools::getValue('discount_type')) {
                     // Percent type
                     case 1:
                         if ($discount_value < 100) {
                             if (isset($order_invoice)) {
                                 $cart_rules[$order_invoice->id]['value_tax_incl'] = Tools::ps_round($order_invoice->total_paid_tax_incl * $discount_value / 100, 2);
                                 $cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round($order_invoice->total_paid_tax_excl * $discount_value / 100, 2);
                                 // Update OrderInvoice
                                 $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']);
                             } elseif ($order->hasInvoice()) {
                                 $order_invoices_collection = $order->getInvoicesCollection();
                                 foreach ($order_invoices_collection as $order_invoice) {
                                     /** @var OrderInvoice $order_invoice */
                                     $cart_rules[$order_invoice->id]['value_tax_incl'] = Tools::ps_round($order_invoice->total_paid_tax_incl * $discount_value / 100, 2);
                                     $cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round($order_invoice->total_paid_tax_excl * $discount_value / 100, 2);
                                     // Update OrderInvoice
                                     $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']);
                                 }
                             } else {
                                 $cart_rules[0]['value_tax_incl'] = Tools::ps_round($order->total_paid_tax_incl * $discount_value / 100, 2);
                                 $cart_rules[0]['value_tax_excl'] = Tools::ps_round($order->total_paid_tax_excl * $discount_value / 100, 2);
                             }
                         } else {
                             $this->errors[] = Tools::displayError('The discount value is invalid.');
                         }
                         break;
                         // Amount type
                     // Amount type
                     case 2:
                         if (isset($order_invoice)) {
                             if ($discount_value > $order_invoice->total_paid_tax_incl) {
                                 $this->errors[] = Tools::displayError('The discount value is greater than the order invoice total.');
                             } else {
                                 $cart_rules[$order_invoice->id]['value_tax_incl'] = Tools::ps_round($discount_value, 2);
                                 $cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round($discount_value / (1 + $order->getTaxesAverageUsed() / 100), 2);
                                 // Update OrderInvoice
                                 $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']);
                             }
                         } elseif ($order->hasInvoice()) {
                             $order_invoices_collection = $order->getInvoicesCollection();
                             foreach ($order_invoices_collection as $order_invoice) {
                                 /** @var OrderInvoice $order_invoice */
                                 if ($discount_value > $order_invoice->total_paid_tax_incl) {
                                     $this->errors[] = Tools::displayError('The discount value is greater than the order invoice total.') . $order_invoice->getInvoiceNumberFormatted(Context::getContext()->language->id, (int) $order->id_shop) . ')';
                                 } else {
                                     $cart_rules[$order_invoice->id]['value_tax_incl'] = Tools::ps_round($discount_value, 2);
                                     $cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round($discount_value / (1 + $order->getTaxesAverageUsed() / 100), 2);
                                     // Update OrderInvoice
                                     $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']);
                                 }
                             }
                         } else {
                             if ($discount_value > $order->total_paid_tax_incl) {
                                 $this->errors[] = Tools::displayError('The discount value is greater than the order total.');
                             } else {
                                 $cart_rules[0]['value_tax_incl'] = Tools::ps_round($discount_value, 2);
                                 $cart_rules[0]['value_tax_excl'] = Tools::ps_round($discount_value / (1 + $order->getTaxesAverageUsed() / 100), 2);
                             }
                         }
                         break;
                         // Free shipping type
                     // Free shipping type
                     case 3:
                         if (isset($order_invoice)) {
                             if ($order_invoice->total_shipping_tax_incl > 0) {
                                 $cart_rules[$order_invoice->id]['value_tax_incl'] = $order_invoice->total_shipping_tax_incl;
                                 $cart_rules[$order_invoice->id]['value_tax_excl'] = $order_invoice->total_shipping_tax_excl;
                                 // Update OrderInvoice
                                 $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']);
                             }
                         } elseif ($order->hasInvoice()) {
                             $order_invoices_collection = $order->getInvoicesCollection();
                             foreach ($order_invoices_collection as $order_invoice) {
                                 /** @var OrderInvoice $order_invoice */
                                 if ($order_invoice->total_shipping_tax_incl <= 0) {
                                     continue;
                                 }
                                 $cart_rules[$order_invoice->id]['value_tax_incl'] = $order_invoice->total_shipping_tax_incl;
                                 $cart_rules[$order_invoice->id]['value_tax_excl'] = $order_invoice->total_shipping_tax_excl;
                                 // Update OrderInvoice
                                 $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']);
                             }
                         } else {
                             $cart_rules[0]['value_tax_incl'] = $order->total_shipping_tax_incl;
                             $cart_rules[0]['value_tax_excl'] = $order->total_shipping_tax_excl;
                         }
                         break;
                     default:
                         $this->errors[] = Tools::displayError('The discount type is invalid.');
                 }
                 $res = true;
                 foreach ($cart_rules as &$cart_rule) {
                     $cartRuleObj = new CartRule();
                     $cartRuleObj->date_from = date('Y-m-d H:i:s', strtotime('-1 hour', strtotime($order->date_add)));
                     $cartRuleObj->date_to = date('Y-m-d H:i:s', strtotime('+1 hour'));
                     $cartRuleObj->name[Configuration::get('PS_LANG_DEFAULT')] = Tools::getValue('discount_name');
                     $cartRuleObj->quantity = 0;
                     $cartRuleObj->quantity_per_user = 1;
                     if (Tools::getValue('discount_type') == 1) {
                         $cartRuleObj->reduction_percent = $discount_value;
                     } elseif (Tools::getValue('discount_type') == 2) {
                         $cartRuleObj->reduction_amount = $cart_rule['value_tax_excl'];
                     } elseif (Tools::getValue('discount_type') == 3) {
                         $cartRuleObj->free_shipping = 1;
                     }
                     $cartRuleObj->active = 0;
                     if ($res = $cartRuleObj->add()) {
                         $cart_rule['id'] = $cartRuleObj->id;
                     } else {
                         break;
                     }
                 }
                 if ($res) {
                     foreach ($cart_rules as $id_order_invoice => $cart_rule) {
                         // Create OrderCartRule
                         $order_cart_rule = new OrderCartRule();
                         $order_cart_rule->id_order = $order->id;
                         $order_cart_rule->id_cart_rule = $cart_rule['id'];
                         $order_cart_rule->id_order_invoice = $id_order_invoice;
                         $order_cart_rule->name = Tools::getValue('discount_name');
                         $order_cart_rule->value = $cart_rule['value_tax_incl'];
                         $order_cart_rule->value_tax_excl = $cart_rule['value_tax_excl'];
                         $res &= $order_cart_rule->add();
                         $order->total_discounts += $order_cart_rule->value;
                         $order->total_discounts_tax_incl += $order_cart_rule->value;
                         $order->total_discounts_tax_excl += $order_cart_rule->value_tax_excl;
                         $order->total_paid -= $order_cart_rule->value;
                         $order->total_paid_tax_incl -= $order_cart_rule->value;
                         $order->total_paid_tax_excl -= $order_cart_rule->value_tax_excl;
                     }
                     // Update Order
                     $res &= $order->update();
                 }
                 if ($res) {
                     Tools::redirectAdmin(self::$currentIndex . '&id_order=' . $order->id . '&vieworder&conf=4&token=' . $this->token);
                 } else {
                     $this->errors[] = Tools::displayError('An error occurred during the OrderCartRule creation');
                 }
             }
         } else {
             $this->errors[] = Tools::displayError('You do not have permission to edit this.');
         }
     } elseif (Tools::isSubmit('sendStateEmail') && Tools::getValue('sendStateEmail') > 0 && Tools::getValue('id_order') > 0) {
         if ($this->tabAccess['edit'] === '1') {
             $order_state = new OrderState((int) Tools::getValue('sendStateEmail'));
             if (!Validate::isLoadedObject($order_state)) {
                 $this->errors[] = Tools::displayError('An error occurred while loading order status.');
             } else {
                 $history = new OrderHistory((int) Tools::getValue('id_order_history'));
                 $carrier = new Carrier($order->id_carrier, $order->id_lang);
                 $templateVars = array();
                 if ($order_state->id == Configuration::get('PS_OS_SHIPPING') && $order->shipping_number) {
                     $templateVars = array('{followup}' => str_replace('@', $order->shipping_number, $carrier->url));
                 }
                 if ($history->sendEmail($order, $templateVars)) {
                     Tools::redirectAdmin(self::$currentIndex . '&id_order=' . $order->id . '&vieworder&conf=10&token=' . $this->token);
                 } else {
                     $this->errors[] = Tools::displayError('An error occurred while sending the e-mail to the customer.');
                 }
             }
         } else {
             $this->errors[] = Tools::displayError('You do not have permission to edit this.');
         }
     }
     parent::postProcess();
 }
Beispiel #12
0
 /**
  * @deprecated 1.5.0.1
  * @param Order $order
  * @return Discount
  */
 public static function createOrderDiscount($order, $productList, $qtyList, $name, $shipping_cost = false, $id_category = 0, $subcategory = 0)
 {
     $products = $order->getProducts(false, $productList, $qtyList);
     // Totals are stored in the order currency (or at least should be)
     $total = $order->getTotalProductsWithTaxes($products);
     $discounts = $order->getDiscounts(true);
     $total_tmp = $total;
     foreach ($discounts as $discount) {
         if ($discount['id_discount_type'] == Discount::PERCENT) {
             $total -= $total_tmp * ($discount['value'] / 100);
         } elseif ($discount['id_discount_type'] == Discount::AMOUNT) {
             $total -= $discount['value'] * ($total_tmp / $order->total_products_wt);
         }
     }
     if ($shipping_cost) {
         $total += $order->total_shipping;
     }
     // create discount
     $voucher = new Discount();
     $voucher->id_discount_type = Discount::AMOUNT;
     foreach (Language::getIDs((bool) $order) as $id_lang) {
         $voucher->description[$id_lang] = strval($name) . (int) $order->id;
     }
     $voucher->value = (double) $total;
     $voucher->name = 'V0C' . (int) $order->id_customer . 'O' . (int) $order->id;
     $voucher->id_customer = (int) $order->id_customer;
     $voucher->id_currency = (int) $order->id_currency;
     $voucher->quantity = 1;
     $voucher->quantity_per_user = 1;
     $voucher->cumulable = 1;
     $voucher->cumulable_reduction = 1;
     $voucher->minimal = (double) $voucher->value;
     $voucher->active = 1;
     $voucher->cart_display = 1;
     $now = time();
     $voucher->date_from = date('Y-m-d H:i:s', $now);
     $voucher->date_to = date('Y-m-d H:i:s', $now + 3600 * 24 * 365.25);
     /* 1 year */
     if (!$voucher->validateFieldsLang(false) || !$voucher->add()) {
         return false;
     }
     // set correct name
     $voucher->name = 'V' . (int) $voucher->id . 'C' . (int) $order->id_customer . 'O' . $order->id;
     if (!$voucher->update()) {
         return false;
     }
     return $voucher;
 }
Beispiel #13
0
 $product->id_shop_default = (int) Configuration::get('PS_SHOP_DEFAULT');
 //price
 $product->price = $prd['price'] ? $prd['price'] : 0;
 //Category
 //Qte
 $link_rewrite = is_array($product->link_rewrite) && isset($product->link_rewrite[$id_lang]) ? trim($product->link_rewrite[$id_lang]) : '';
 $valid_link = Validate::isLinkRewrite($link_rewrite);
 if (isset($product->link_rewrite[$id_lang]) && empty($product->link_rewrite[$id_lang]) || !$valid_link) {
     $link_rewrite = Tools::link_rewrite($product->name[$id_lang]);
     if ($link_rewrite == '') {
         $link_rewrite = 'friendly-url-autogeneration-failed';
     }
 }
 if (!(is_array($product->link_rewrite) && count($product->link_rewrite) && !empty($product->link_rewrite[$id_lang]))) {
     $res = array();
     foreach (Language::getIDs(false) as $id_lang) {
         $res[$id_lang] = $link_rewrite;
     }
     $product->link_rewrite = $res;
 }
 if ($product->save()) {
     $id_image = array();
     //delete existing images if "delete_existing_images" is set to 1
     $multiple_value_separator = ($separator = Tools::substr(strval(trim(Tools::getValue('multiple_value_separator'))), 0, 1)) ? $separator : ',';
     if (isset($prd['images']) && $prd['images']) {
         $image_url = explode('/', $prd['images']);
         $url = _PS_IMG_MGT_DIR_ . end($image_url);
         $product_has_images = (bool) Image::getImages($id_lang, $product->id);
         $image = new Image();
         $image->id_product = (int) $product->id;
         $image->position = Image::getHighestPosition($product->id) + 1;
 protected static function createMultiLangField($field)
 {
     $res = array();
     foreach (Language::getIDs(false) as $id_lang) {
         $res[$id_lang] = $field;
     }
     return $res;
 }
 public function postProcess()
 {
     if (Tools::isSubmit($this->table . 'Orderby') || Tools::isSubmit($this->table . 'Orderway')) {
         $this->filter = true;
     }
     if (Tools::isSubmit('submitAddorder_return_state')) {
         $id_order_return_state = Tools::getValue('id_order_return_state');
         // Create Object OrderReturnState
         $order_return_state = new OrderReturnState((int) $id_order_return_state);
         $order_return_state->color = Tools::getValue('color');
         $order_return_state->name = array();
         foreach (Language::getIDs(false) as $id_lang) {
             $order_return_state->name[$id_lang] = Tools::getValue('name_' . $id_lang);
         }
         // Update object
         if (!$order_return_state->save()) {
             $this->errors[] = Tools::displayError('An error has occurred: Can\'t save the current order\'s return status.');
         } else {
             Tools::redirectAdmin(self::$currentIndex . '&conf=4&token=' . $this->token);
         }
     }
     if (Tools::isSubmit('submitBulkdeleteorder_return_state')) {
         $this->className = 'OrderReturnState';
         $this->table = 'order_return_state';
         $this->boxes = Tools::getValue('order_return_stateBox');
         parent::processBulkDelete();
     }
     if (Tools::isSubmit('deleteorder_return_state')) {
         $id_order_return_state = Tools::getValue('id_order_return_state');
         // Create Object OrderReturnState
         $order_return_state = new OrderReturnState((int) $id_order_return_state);
         if (!$order_return_state->delete()) {
             $this->errors[] = Tools::displayError('An error has occurred: Can\'t delete the current order\'s return status.');
         } else {
             Tools::redirectAdmin(self::$currentIndex . '&conf=1&token=' . $this->token);
         }
     }
     if (Tools::isSubmit('submitAdd' . $this->table)) {
         $this->deleted = false;
         // Disabling saving historisation
         $_POST['invoice'] = (int) Tools::getValue('invoice_on');
         $_POST['logable'] = (int) Tools::getValue('logable_on');
         $_POST['send_email'] = (int) Tools::getValue('send_email_on');
         $_POST['hidden'] = (int) Tools::getValue('hidden_on');
         $_POST['shipped'] = (int) Tools::getValue('shipped_on');
         $_POST['paid'] = (int) Tools::getValue('paid_on');
         $_POST['delivery'] = (int) Tools::getValue('delivery_on');
         $_POST['pdf_delivery'] = (int) Tools::getValue('pdf_delivery_on');
         $_POST['pdf_invoice'] = (int) Tools::getValue('pdf_invoice_on');
         if (!$_POST['send_email']) {
             foreach (Language::getIDs(false) as $id_lang) {
                 $_POST['template_' . $id_lang] = '';
             }
         }
         return parent::postProcess();
     } elseif (Tools::isSubmit('delete' . $this->table)) {
         $order_state = new OrderState(Tools::getValue('id_order_state'), $this->context->language->id);
         if (!$order_state->isRemovable()) {
             $this->errors[] = $this->l('For security reasons, you cannot delete default order statuses.');
         } else {
             return parent::postProcess();
         }
     } elseif (Tools::isSubmit('submitBulkdelete' . $this->table)) {
         foreach (Tools::getValue($this->table . 'Box') as $selection) {
             $order_state = new OrderState((int) $selection, $this->context->language->id);
             if (!$order_state->isRemovable()) {
                 $this->errors[] = $this->l('For security reasons, you cannot delete default order statuses.');
                 break;
             }
         }
         if (!count($this->errors)) {
             return parent::postProcess();
         }
     } else {
         return parent::postProcess();
     }
 }
Beispiel #16
0
 public static function isOverridenByCurrentContext($key)
 {
     if (Configuration::isLangKey($key)) {
         $testContext = false;
         foreach (Language::getIDs(false) as $id_lang) {
             if (Shop::getContext() == Shop::CONTEXT_SHOP && Configuration::hasContext($key, $id_lang, Shop::CONTEXT_SHOP) || Shop::getContext() == Shop::CONTEXT_GROUP && Configuration::hasContext($key, $id_lang, Shop::CONTEXT_GROUP)) {
                 $testContext = true;
             }
         }
     } else {
         $testContext = Shop::getContext() == Shop::CONTEXT_SHOP && Configuration::hasContext($key, null, Shop::CONTEXT_SHOP) || Shop::getContext() == Shop::CONTEXT_GROUP && Configuration::hasContext($key, null, Shop::CONTEXT_GROUP) ? true : false;
     }
     return Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_ALL && $testContext;
 }
 /**
  * Processes submitted configuration variables
  */
 protected function postProcess()
 {
     // @TODO Nicer solution ?
     $castFunctions = array('boolval', 'doubleval', 'floatval', 'intval', 'strval');
     $langIds = Language::getIDs(false);
     $values = array();
     foreach ($this->getOptionFields() as $key => $field) {
         $htmlAllowed = isset($field['html']) && $field['html'];
         if ($field['type'] == 'textareaLang' || $field['type'] == 'textLang') {
             $values[$key] = array();
             foreach ($langIds as $id_lang) {
                 $value = Tools::getValue($key . '_' . $id_lang);
                 if ($field['cast'] && in_array($field['cast'], $castFunctions)) {
                     $value = call_user_func($field['cast'], $value);
                 }
                 $values[$key][$id_lang] = $value;
             }
         } else {
             $value = Tools::getValue($key);
             if ($field['cast'] && in_array($field['cast'], $castFunctions)) {
                 $value = call_user_func($field['cast'], $value);
             }
             $values[$key] = $value;
         }
         Configuration::updateValue($key, $values[$key], $htmlAllowed);
     }
     if ($values['CT_CFG_BLOCKCATEGORIES_FOOTER']) {
         $this->hookModule('blockcategories', 'footer');
     } else {
         $this->unhookModule('blockcategories', 'footer');
     }
 }
Beispiel #18
0
    public static function indexation($full = false, $id_product = false)
    {
        $db = Db::getInstance();
        if ($id_product) {
            $full = false;
        }
        if ($full && Context::getContext()->shop->getContext() == Shop::CONTEXT_SHOP) {
            $db->execute('DELETE si, sw FROM `' . _DB_PREFIX_ . 'search_index` si
				INNER JOIN `' . _DB_PREFIX_ . 'product` p ON (p.id_product = si.id_product)
				' . Shop::addSqlAssociation('product', 'p') . '
				INNER JOIN `' . _DB_PREFIX_ . 'search_word` sw ON (sw.id_word = si.id_word AND product_shop.id_shop = sw.id_shop)
				WHERE product_shop.`visibility` IN ("both", "search")
				AND product_shop.`active` = 1');
            $db->execute('UPDATE `' . _DB_PREFIX_ . 'product` p
				' . Shop::addSqlAssociation('product', 'p') . '
				SET p.`indexed` = 0, product_shop.`indexed` = 0
				WHERE product_shop.`visibility` IN ("both", "search")
				AND product_shop.`active` = 1
				');
        } elseif ($full) {
            $db->execute('TRUNCATE ' . _DB_PREFIX_ . 'search_index');
            $db->execute('TRUNCATE ' . _DB_PREFIX_ . 'search_word');
            ObjectModel::updateMultishopTable('Product', array('indexed' => 0));
        } else {
            $db->execute('DELETE si FROM `' . _DB_PREFIX_ . 'search_index` si
				INNER JOIN `' . _DB_PREFIX_ . 'product` p ON (p.id_product = si.id_product)
				' . Shop::addSqlAssociation('product', 'p') . '
				WHERE product_shop.`visibility` IN ("both", "search")
				AND product_shop.`active` = 1
				AND ' . ($id_product ? 'p.`id_product` = ' . (int) $id_product : 'product_shop.`indexed` = 0'));
            $db->execute('UPDATE `' . _DB_PREFIX_ . 'product` p
				' . Shop::addSqlAssociation('product', 'p') . '
				SET p.`indexed` = 0, product_shop.`indexed` = 0
				WHERE product_shop.`visibility` IN ("both", "search")
				AND product_shop.`active` = 1
				AND ' . ($id_product ? 'p.`id_product` = ' . (int) $id_product : 'product_shop.`indexed` = 0'));
        }
        // Every fields are weighted according to the configuration in the backend
        $weight_array = array('pname' => Configuration::get('PS_SEARCH_WEIGHT_PNAME'), 'reference' => Configuration::get('PS_SEARCH_WEIGHT_REF'), 'pa_reference' => Configuration::get('PS_SEARCH_WEIGHT_REF'), 'supplier_reference' => Configuration::get('PS_SEARCH_WEIGHT_REF'), 'pa_supplier_reference' => Configuration::get('PS_SEARCH_WEIGHT_REF'), 'ean13' => Configuration::get('PS_SEARCH_WEIGHT_REF'), 'pa_ean13' => Configuration::get('PS_SEARCH_WEIGHT_REF'), 'upc' => Configuration::get('PS_SEARCH_WEIGHT_REF'), 'pa_upc' => Configuration::get('PS_SEARCH_WEIGHT_REF'), 'description_short' => Configuration::get('PS_SEARCH_WEIGHT_SHORTDESC'), 'description' => Configuration::get('PS_SEARCH_WEIGHT_DESC'), 'cname' => Configuration::get('PS_SEARCH_WEIGHT_CNAME'), 'mname' => Configuration::get('PS_SEARCH_WEIGHT_MNAME'), 'tags' => Configuration::get('PS_SEARCH_WEIGHT_TAG'), 'attributes' => Configuration::get('PS_SEARCH_WEIGHT_ATTRIBUTE'), 'features' => Configuration::get('PS_SEARCH_WEIGHT_FEATURE'));
        // Those are kind of global variables required to save the processed data in the database every X occurrences, in order to avoid overloading MySQL
        $count_words = 0;
        $query_array3 = array();
        // Retrieve the number of languages
        $total_languages = count(Language::getIDs(false));
        $sql_attribute = Search::getSQLProductAttributeFields($weight_array);
        // Products are processed 50 by 50 in order to avoid overloading MySQL
        while (($products = Search::getProductsToIndex($total_languages, $id_product, 50, $weight_array)) && count($products) > 0) {
            $products_array = array();
            // Now each non-indexed product is processed one by one, langage by langage
            foreach ($products as $product) {
                if ((int) $weight_array['tags']) {
                    $product['tags'] = Search::getTags($db, (int) $product['id_product'], (int) $product['id_lang']);
                }
                if ((int) $weight_array['attributes']) {
                    $product['attributes'] = Search::getAttributes($db, (int) $product['id_product'], (int) $product['id_lang']);
                }
                if ((int) $weight_array['features']) {
                    $product['features'] = Search::getFeatures($db, (int) $product['id_product'], (int) $product['id_lang']);
                }
                if ($sql_attribute) {
                    $attribute_fields = Search::getAttributesFields($db, (int) $product['id_product'], $sql_attribute);
                    if ($attribute_fields) {
                        $product['attributes_fields'] = $attribute_fields;
                    }
                }
                // Data must be cleaned of html, bad characters, spaces and anything, then if the resulting words are long enough, they're added to the array
                $product_array = array();
                foreach ($product as $key => $value) {
                    if ($key == 'attributes_fields') {
                        foreach ($value as $pa_array) {
                            foreach ($pa_array as $pa_key => $pa_value) {
                                Search::fillProductArray($product_array, $weight_array, $pa_key, $pa_value, $product['id_lang'], $product['iso_code']);
                            }
                        }
                    } else {
                        Search::fillProductArray($product_array, $weight_array, $key, $value, $product['id_lang'], $product['iso_code']);
                    }
                }
                // If we find words that need to be indexed, they're added to the word table in the database
                if (is_array($product_array) && !empty($product_array)) {
                    $query_array = $query_array2 = array();
                    foreach ($product_array as $word => $weight) {
                        if ($weight) {
                            $query_array[$word] = '(' . (int) $product['id_lang'] . ', ' . (int) $product['id_shop'] . ', \'' . pSQL($word) . '\')';
                            $query_array2[] = '\'' . pSQL($word) . '\'';
                        }
                    }
                    if (is_array($query_array) && !empty($query_array)) {
                        // The words are inserted...
                        $db->execute('
						INSERT IGNORE INTO ' . _DB_PREFIX_ . 'search_word (id_lang, id_shop, word)
						VALUES ' . implode(',', $query_array), false);
                    }
                    $word_ids_by_word = array();
                    if (is_array($query_array2) && !empty($query_array2)) {
                        // ...then their IDs are retrieved
                        $added_words = $db->executeS('
						SELECT sw.id_word, sw.word
						FROM ' . _DB_PREFIX_ . 'search_word sw
						WHERE sw.word IN (' . implode(',', $query_array2) . ')
						AND sw.id_lang = ' . (int) $product['id_lang'] . '
						AND sw.id_shop = ' . (int) $product['id_shop'], true, false);
                        foreach ($added_words as $word_id) {
                            $word_ids_by_word['_' . $word_id['word']] = (int) $word_id['id_word'];
                        }
                    }
                }
                foreach ($product_array as $word => $weight) {
                    if (!$weight) {
                        continue;
                    }
                    if (!isset($word_ids_by_word['_' . $word])) {
                        continue;
                    }
                    $id_word = $word_ids_by_word['_' . $word];
                    if (!$id_word) {
                        continue;
                    }
                    $query_array3[] = '(' . (int) $product['id_product'] . ',' . (int) $id_word . ',' . (int) $weight . ')';
                    // Force save every 200 words in order to avoid overloading MySQL
                    if (++$count_words % 200 == 0) {
                        Search::saveIndex($query_array3);
                    }
                }
                $products_array[] = (int) $product['id_product'];
            }
            $products_array = array_unique($products_array);
            Search::setProductsAsIndexed($products_array);
            // One last save is done at the end in order to save what's left
            Search::saveIndex($query_array3);
        }
        return true;
    }
 public function getLiveEditUrl($live_edit_params)
 {
     $lang = '';
     $language_ids = Language::getIDs(true);
     if (Configuration::get('PS_REWRITING_SETTINGS') && !empty($language_ids) && count($language_ids) > 1) {
         $lang = Language::getIsoById($this->context->employee->id_lang) . '/';
     }
     unset($language_ids);
     // Shop::initialize() in config.php may empty $this->context->shop->virtual_uri so using a new shop instance for getBaseUrl()
     $this->context->shop = new Shop((int) $this->context->shop->id);
     $url = $this->context->shop->getBaseURL() . $lang . Dispatcher::getInstance()->createUrl('index', (int) $this->context->language->id, $live_edit_params);
     return $url;
 }
 public function generateRobotsFile()
 {
     if (!($write_fd = @fopen($this->rb_file, 'w'))) {
         $this->errors[] = sprintf(Tools::displayError('Cannot write into file: %s. Please check write permissions.'), $this->rb_file);
     } else {
         Hook::exec('actionAdminMetaBeforeWriteRobotsFile', array('rb_data' => &$this->rb_data));
         // PS Comments
         fwrite($write_fd, "# robots.txt automaticaly generated by PrestaShop e-commerce open-source solution\n");
         fwrite($write_fd, "# http://www.prestashop.com - http://www.prestashop.com/forums\n");
         fwrite($write_fd, "# This file is to prevent the crawling and indexing of certain parts\n");
         fwrite($write_fd, "# of your site by web crawlers and spiders run by sites like Yahoo!\n");
         fwrite($write_fd, "# and Google. By telling these \"robots\" where not to go on your site,\n");
         fwrite($write_fd, "# you save bandwidth and server resources.\n");
         fwrite($write_fd, "# For more information about the robots.txt standard, see:\n");
         fwrite($write_fd, "# http://www.robotstxt.org/robotstxt.html\n");
         // User-Agent
         fwrite($write_fd, "User-agent: *\n");
         // Private pages
         if (count($this->rb_data['GB'])) {
             fwrite($write_fd, "# Private pages\n");
             foreach ($this->rb_data['GB'] as $gb) {
                 fwrite($write_fd, 'Disallow: /*' . $gb . "\n");
             }
         }
         // Directories
         if (count($this->rb_data['Directories'])) {
             fwrite($write_fd, "# Directories\n");
             foreach ($this->rb_data['Directories'] as $dir) {
                 fwrite($write_fd, 'Disallow: */' . $dir . "\n");
             }
         }
         // Files
         if (count($this->rb_data['Files'])) {
             $language_ids = Language::getIDs();
             fwrite($write_fd, "# Files\n");
             foreach ($this->rb_data['Files'] as $iso_code => $files) {
                 foreach ($files as $file) {
                     if (!empty($language_ids)) {
                         fwrite($write_fd, 'Disallow: /*' . $iso_code . '/' . $file . "\n");
                     } else {
                         fwrite($write_fd, 'Disallow: /' . $file . "\n");
                     }
                 }
             }
         }
         // Sitemap
         if (file_exists($this->sm_file) && filesize($this->sm_file)) {
             fwrite($write_fd, "# Sitemap\n");
             $sitemap_filename = basename($this->sm_file);
             fwrite($write_fd, 'Sitemap: ' . (Configuration::get('PS_SSL_ENABLED') ? 'https://' : 'http://') . $_SERVER['SERVER_NAME'] . __PS_BASE_URI__ . $sitemap_filename . "\n");
         }
         Hook::exec('actionAdminMetaAfterWriteRobotsFile', array('rb_data' => $this->rb_data, 'write_fd' => &$write_fd));
         fclose($write_fd);
         $this->redirect_after = self::$currentIndex . '&conf=4&token=' . $this->token;
     }
 }
Beispiel #21
0
    /**
     * Duplicate features when duplicating a product
     *
     * @param int $id_product_old Old product id
     * @param int $id_product_old New product id
     */
    public static function duplicateFeatures($id_product_old, $id_product_new)
    {
        $return = true;
        $result = Db::getInstance()->executeS('
		SELECT *
		FROM `' . _DB_PREFIX_ . 'feature_product`
		WHERE `id_product` = ' . (int) $id_product_old);
        foreach ($result as $row) {
            $result2 = Db::getInstance()->getRow('
			SELECT *
			FROM `' . _DB_PREFIX_ . 'feature_value`
			WHERE `id_feature_value` = ' . (int) $row['id_feature_value']);
            // Custom feature value, need to duplicate it
            if ($result2['custom']) {
                $old_id_feature_value = $result2['id_feature_value'];
                unset($result2['id_feature_value']);
                $return &= Db::getInstance()->insert('feature_value', $result2);
                $max_fv = Db::getInstance()->getRow('
					SELECT MAX(`id_feature_value`) AS nb
					FROM `' . _DB_PREFIX_ . 'feature_value`');
                $new_id_feature_value = $max_fv['nb'];
                foreach (Language::getIDs(false) as $id_lang) {
                    $result3 = Db::getInstance()->getRow('
					SELECT *
					FROM `' . _DB_PREFIX_ . 'feature_value_lang`
					WHERE `id_feature_value` = ' . (int) $old_id_feature_value . '
					AND `id_lang` = ' . (int) $id_lang);
                    if ($result3) {
                        $result3['id_feature_value'] = (int) $new_id_feature_value;
                        $result3['value'] = pSQL($result3['value']);
                        $return &= Db::getInstance()->insert('feature_value_lang', $result3);
                    }
                }
                $row['id_feature_value'] = $new_id_feature_value;
            }
            $row['id_product'] = (int) $id_product_new;
            $return &= Db::getInstance()->insert('feature_product', $row);
        }
        return $return;
    }
Beispiel #22
0
    /**
     * Create a feature from import
     *
     * @param int $id_feature Feature id
     * @param int $id_product Product id
     * @param array $value Feature Value
     */
    public static function addFeatureImport($name, $position = false)
    {
        $rq = Db::getInstance()->getRow('
			SELECT `id_feature`
			FROM ' . _DB_PREFIX_ . 'feature_lang
			WHERE `name` = \'' . pSQL($name) . '\'
			GROUP BY `id_feature`
		');
        if (empty($rq)) {
            // Feature doesn't exist, create it
            $feature = new Feature();
            $feature->name = array_fill_keys(Language::getIDs(), (string) $name);
            if ($position) {
                $feature->position = (int) $position;
            } else {
                $feature->position = Feature::getHigherPosition() + 1;
            }
            $feature->add();
            return $feature->id;
        } elseif (isset($rq['id_feature']) && $rq['id_feature']) {
            if (is_numeric($position) && ($feature = new Feature((int) $rq['id_feature']))) {
                $feature->position = (int) $position;
                if (Validate::isLoadedObject($feature)) {
                    $feature->update();
                }
            }
            return (int) $rq['id_feature'];
        }
    }
Beispiel #23
0
    public static function addFeatureValueImport($id_feature, $value, $id_product = null, $id_lang = null, $custom = false)
    {
        $id_feature_value = false;
        if (!is_null($id_product) && $id_product) {
            $id_feature_value = Db::getInstance()->getValue('
				SELECT fp.`id_feature_value`
				FROM ' . _DB_PREFIX_ . 'feature_product fp
				INNER JOIN ' . _DB_PREFIX_ . 'feature_value fv USING (`id_feature_value`)
				WHERE fp.`id_feature` = ' . (int) $id_feature . '
				AND fv.`custom` = ' . (int) $custom . '
				AND fp.`id_product` = ' . (int) $id_product);
            if ($custom && $id_feature_value && !is_null($id_lang) && $id_lang) {
                Db::getInstance()->execute('
				UPDATE ' . _DB_PREFIX_ . 'feature_value_lang
				SET `value` = \'' . pSQL($value) . '\'
				WHERE `id_feature_value` = ' . (int) $id_feature_value . '
				AND `value` != \'' . pSQL($value) . '\'
				AND `id_lang` = ' . (int) $id_lang);
            }
        }
        if (!$custom) {
            $id_feature_value = Db::getInstance()->getValue('
				SELECT fv.`id_feature_value`
				FROM ' . _DB_PREFIX_ . 'feature_value fv
				LEFT JOIN ' . _DB_PREFIX_ . 'feature_value_lang fvl ON (fvl.`id_feature_value` = fv.`id_feature_value` AND fvl.`id_lang` = ' . (int) $id_lang . ')
				WHERE `value` = \'' . pSQL($value) . '\'
				AND fv.`id_feature` = ' . (int) $id_feature . '
				AND fv.`custom` = 0
				GROUP BY fv.`id_feature_value`');
        }
        if ($id_feature_value) {
            return (int) $id_feature_value;
        }
        // Feature doesn't exist, create it
        $feature_value = new FeatureValue();
        $feature_value->id_feature = (int) $id_feature;
        $feature_value->custom = (bool) $custom;
        $feature_value->value = array_fill_keys(Language::getIDs(false), $value);
        $feature_value->add();
        return (int) $feature_value->id;
    }
 /**
  * Start Webservice request
  * 	Check webservice activation
  * 	Check autentication
  * 	Check resource
  * 	Check HTTP Method
  * 	Execute the action
  * 	Display the result
  *
  * @param string $key
  * @param string $method
  * @param string $url
  * @param string $params
  * @param string $inputXml
  *
  * @return array Returns an array of results (headers, content, type of resource...)
  */
 public function fetch($key, $method, $url, $params, $bad_class_name, $inputXml = null)
 {
     // Time logger
     $this->_startTime = microtime(true);
     $this->objects = array();
     // Error handler
     set_error_handler(array($this, 'webserviceErrorHandler'));
     ini_set('html_errors', 'off');
     // Two global vars, for compatibility with the PS core...
     global $webservice_call, $display_errors;
     $webservice_call = true;
     $display_errors = strtolower(ini_get('display_errors')) != 'off';
     // __PS_BASE_URI__ is from Shop::$current_base_uri
     $this->wsUrl = Tools::getHttpHost(true) . __PS_BASE_URI__ . 'api/';
     // set the output object which manage the content and header structure and informations
     $this->objOutput = new WebserviceOutputBuilder($this->wsUrl);
     $this->_key = trim($key);
     $this->outputFormat = isset($params['output_format']) ? $params['output_format'] : $this->outputFormat;
     // Set the render object to build the output on the asked format (XML, JSON, CSV, ...)
     $this->objOutput->setObjectRender($this->getOutputObject($this->outputFormat));
     $this->params = $params;
     // Check webservice activation and request authentication
     if ($this->webserviceChecks()) {
         if ($bad_class_name) {
             $this->setError(500, 'Class "' . htmlspecialchars($bad_class_name) . '" not found. Please update the class_name field in the webservice_account table.', 126);
         }
         // parse request url
         $this->method = $method;
         $this->urlSegment = explode('/', $url);
         $this->urlFragments = $params;
         $this->_inputXml = $inputXml;
         $this->depth = isset($this->urlFragments['depth']) ? (int) $this->urlFragments['depth'] : $this->depth;
         try {
             // Method below set a particular fonction to use on the price field for products entity
             // @see WebserviceRequest::getPriceForProduct() method
             // @see WebserviceOutputBuilder::setSpecificField() method
             //$this->objOutput->setSpecificField($this, 'getPriceForProduct', 'price', 'products');
             if (isset($this->urlFragments['price'])) {
                 $this->objOutput->setVirtualField($this, 'specificPriceForCombination', 'combinations', $this->urlFragments['price']);
                 $this->objOutput->setVirtualField($this, 'specificPriceForProduct', 'products', $this->urlFragments['price']);
             }
         } catch (Exception $e) {
             $this->setError(500, $e->getMessage(), $e->getCode());
         }
         if (isset($this->urlFragments['language'])) {
             $this->_available_languages = $this->filterLanguage();
         } else {
             $this->_available_languages = Language::getIDs();
         }
         if (empty($this->_available_languages)) {
             $this->setError(400, 'language is not available', 81);
         }
         // Need to set available languages for the render object.
         // Thus we can filter i18n field for the output
         // @see WebserviceOutputXML::renderField() method for example
         $this->objOutput->objectRender->setLanguages($this->_available_languages);
         // check method and resource
         if (empty($this->errors) && $this->checkResource() && $this->checkHTTPMethod()) {
             // The resource list is necessary for build the output
             $this->objOutput->setWsResources($this->resourceList);
             // if the resource is a core entity...
             if (!isset($this->resourceList[$this->urlSegment[0]]['specific_management']) || !$this->resourceList[$this->urlSegment[0]]['specific_management']) {
                 // load resource configuration
                 if ($this->urlSegment[0] != '') {
                     /** @var ObjectModel $object */
                     $object = new $this->resourceList[$this->urlSegment[0]]['class']();
                     if (isset($this->resourceList[$this->urlSegment[0]]['parameters_attribute'])) {
                         $this->resourceConfiguration = $object->getWebserviceParameters($this->resourceList[$this->urlSegment[0]]['parameters_attribute']);
                     } else {
                         $this->resourceConfiguration = $object->getWebserviceParameters();
                     }
                 }
                 $success = false;
                 // execute the action
                 switch ($this->method) {
                     case 'GET':
                     case 'HEAD':
                         if ($this->executeEntityGetAndHead()) {
                             $success = true;
                         }
                         break;
                     case 'POST':
                         if ($this->executeEntityPost()) {
                             $success = true;
                         }
                         break;
                     case 'PUT':
                         if ($this->executeEntityPut()) {
                             $success = true;
                         }
                         break;
                     case 'DELETE':
                         $this->executeEntityDelete();
                         break;
                 }
                 // Need to set an object for the WebserviceOutputBuilder object in any case
                 // because schema need to get webserviceParameters of this object
                 if (isset($object)) {
                     $this->objects['empty'] = $object;
                 }
             } else {
                 $specificObjectName = 'WebserviceSpecificManagement' . ucfirst(Tools::toCamelCase($this->urlSegment[0]));
                 if (!class_exists($specificObjectName)) {
                     $this->setError(501, sprintf('The specific management class is not implemented for the "%s" entity.', $this->urlSegment[0]), 124);
                 } else {
                     $this->objectSpecificManagement = new $specificObjectName();
                     $this->objectSpecificManagement->setObjectOutput($this->objOutput)->setWsObject($this);
                     try {
                         $this->objectSpecificManagement->manage();
                     } catch (WebserviceException $e) {
                         if ($e->getType() == WebserviceException::DID_YOU_MEAN) {
                             $this->setErrorDidYouMean($e->getStatus(), $e->getMessage(), $e->getWrongValue(), $e->getAvailableValues(), $e->getCode());
                         } elseif ($e->getType() == WebserviceException::SIMPLE) {
                             $this->setError($e->getStatus(), $e->getMessage(), $e->getCode());
                         }
                     }
                 }
             }
         }
     }
     $return = $this->returnOutput();
     unset($webservice_call);
     unset($display_errors);
     return $return;
 }
Beispiel #25
0
 /**
  * Registers a module admin controller and install a back-office tab (optional)
  *
  * @param string $class_name - Controller class name without word 'Controller' at the end
  * @param string $tab_title - single string or a language array
  * @param string|int $tab_parent - Parent tab class name or ID
  * @return int|false
  */
 protected function registerAdminController($class_name, $tab_title = '', $tab_parent = -1)
 {
     $title = empty($tab_title) ? $class_name : $tab_title;
     $tab = new Tab();
     $tab->class_name = $class_name;
     $tab->module = $this->name;
     $tab->name = is_array($title) ? $title : array_fill_keys(Language::getIDs(), $title);
     if (!empty($tab_parent) && is_string($tab_parent)) {
         $tab->id_parent = (int) Tab::getIdFromClassName($tab_parent);
     } elseif (is_int($tab_parent)) {
         $tab->id_parent = $tab_parent;
     } else {
         $tab->id_parent = 0;
     }
     return $tab->add() ? (int) $tab->id : false;
 }