/**
  * renderForm contains all necessary initialization needed for all tabs
  *
  * @return void
  */
 public function renderForm()
 {
     // This nice code (irony) is here to store the product name, because the row after will erase product name in multishop context
     $this->product_name = $this->object->name[$this->context->language->id];
     if (!method_exists($this, 'initForm' . $this->tab_display)) {
         return;
     }
     $product = $this->object;
     // Product for multishop
     $this->context->smarty->assign('bullet_common_field', '');
     if (Shop::isFeatureActive() && $this->display == 'edit') {
         if (Shop::getContext() != Shop::CONTEXT_SHOP) {
             $this->context->smarty->assign(array('display_multishop_checkboxes' => true, 'multishop_check' => Tools::getValue('multishop_check')));
         }
         if (Shop::getContext() != Shop::CONTEXT_ALL) {
             $this->context->smarty->assign('bullet_common_field', '<img src="themes/' . $this->context->employee->bo_theme . '/img/bullet_orange.png" style="vertical-align: bottom" />');
             $this->context->smarty->assign('display_common_field', true);
         }
     }
     $this->tpl_form_vars['tabs_preloaded'] = $this->available_tabs;
     $this->tpl_form_vars['product_type'] = (int) Tools::getValue('type_product', $product->getType());
     $this->getLanguages();
     $this->tpl_form_vars['id_lang_default'] = Configuration::get('PS_LANG_DEFAULT');
     $this->tpl_form_vars['currentIndex'] = self::$currentIndex;
     $this->tpl_form_vars['display_multishop_checkboxes'] = Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_SHOP && $this->display == 'edit';
     $this->fields_form = array('');
     $this->tpl_form_vars['token'] = $this->token;
     $this->tpl_form_vars['combinationImagesJs'] = $this->getCombinationImagesJs();
     $this->tpl_form_vars['PS_ALLOW_ACCENTED_CHARS_URL'] = (int) Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL');
     $this->tpl_form_vars['post_data'] = Tools::jsonEncode($_POST);
     $this->tpl_form_vars['save_error'] = !empty($this->errors);
     // autoload rich text editor (tiny mce)
     $this->tpl_form_vars['tinymce'] = true;
     $iso = $this->context->language->iso_code;
     $this->tpl_form_vars['iso'] = file_exists(_PS_ROOT_DIR_ . '/js/tiny_mce/langs/' . $iso . '.js') ? $iso : 'en';
     $this->tpl_form_vars['ad'] = dirname($_SERVER['PHP_SELF']);
     if (Validate::isLoadedObject($this->object)) {
         $id_product = (int) $this->object->id;
     } else {
         $id_product = (int) Tools::getvalue('id_product');
     }
     $this->tpl_form_vars['form_action'] = $this->context->link->getAdminLink('AdminProducts') . '&amp;' . ($id_product ? 'id_product=' . (int) $id_product : 'addproduct');
     $this->tpl_form_vars['id_product'] = $id_product;
     // Transform configuration option 'upload_max_filesize' in octets
     $upload_max_filesize = Tools::getOctets(ini_get('upload_max_filesize'));
     // Transform configuration option 'upload_max_filesize' in MegaOctets
     $upload_max_filesize = $upload_max_filesize / 1024 / 1024;
     $this->tpl_form_vars['upload_max_filesize'] = $upload_max_filesize;
     $this->tpl_form_vars['country_display_tax_label'] = $this->context->country->display_tax_label;
     $this->tpl_form_vars['has_combinations'] = $this->object->hasAttributes();
     $this->product_exists_in_shop = true;
     if ($this->display == 'edit' && Validate::isLoadedObject($product) && Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP && !$product->isAssociatedToShop($this->context->shop->id)) {
         $this->product_exists_in_shop = false;
         if ($this->tab_display == 'Informations') {
             $this->displayWarning($this->l('Warning: The product does not exist in this shop.'));
         }
         $default_product = new Product();
         $definition = ObjectModel::getDefinition($product);
         foreach ($definition['fields'] as $field_name => $field) {
             if (isset($field['shop']) && $field['shop']) {
                 $product->{$field_name} = ObjectModel::formatValue($default_product->{$field_name}, $field['type']);
             }
         }
     }
     // let's calculate this once for all
     if (!Validate::isLoadedObject($this->object) && Tools::getValue('id_product')) {
         $this->errors[] = 'Unable to load object';
     } else {
         $this->_displayDraftWarning($this->object->active);
         // if there was an error while saving, we don't want to lose posted data
         if (!empty($this->errors)) {
             $this->copyFromPost($this->object, $this->table);
         }
         $this->initPack($this->object);
         $this->{'initForm' . $this->tab_display}($this->object);
         $this->tpl_form_vars['product'] = $this->object;
         if ($this->ajax) {
             if (!isset($this->tpl_form_vars['custom_form'])) {
                 throw new PrestaShopException('custom_form empty for action ' . $this->tab_display);
             } else {
                 return $this->tpl_form_vars['custom_form'];
             }
         }
     }
     $parent = parent::renderForm();
     $this->addJqueryPlugin(array('autocomplete', 'fancybox', 'typewatch'));
     return $parent;
 }
예제 #2
0
 /**
  * renderForm contains all necessary initialization needed for all tabs
  *
  * @return string|void
  */
 public function renderForm()
 {
     // This nice code (irony) is here to store the product name, because the row after will erase product name in multishop context
     $this->product_name = $this->object->name[$this->context->language->id];
     if (!method_exists($this, 'initForm' . $this->tab_display)) {
         return;
     }
     $product = $this->object;
     // Product for multishop
     $this->context->smarty->assign('bullet_common_field', '');
     if (Shop::isFeatureActive() && $this->display == 'edit') {
         if (Shop::getContext() != Shop::CONTEXT_SHOP) {
             $this->context->smarty->assign(array('display_multishop_checkboxes' => true, 'multishop_check' => Tools::getValue('multishop_check')));
         }
         if (Shop::getContext() != Shop::CONTEXT_ALL) {
             $this->context->smarty->assign('bullet_common_field', '<i class="icon-circle text-orange"></i>');
             $this->context->smarty->assign('display_common_field', true);
         }
     }
     $this->tpl_form_vars['tabs_preloaded'] = $this->available_tabs;
     $this->tpl_form_vars['product_type'] = (int) Tools::getValue('type_product', $product->getType());
     $this->getLanguages();
     $this->tpl_form_vars['id_lang_default'] = Configuration::get('PS_LANG_DEFAULT');
     $this->tpl_form_vars['currentIndex'] = self::$currentIndex;
     $this->tpl_form_vars['display_multishop_checkboxes'] = Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_SHOP && $this->display == 'edit';
     $this->fields_form = array('');
     $this->tpl_form_vars['token'] = $this->token;
     $this->tpl_form_vars['combinationImagesJs'] = $this->getCombinationImagesJs();
     $this->tpl_form_vars['PS_ALLOW_ACCENTED_CHARS_URL'] = (int) Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL');
     $this->tpl_form_vars['post_data'] = Tools::jsonEncode($_POST);
     $this->tpl_form_vars['save_error'] = !empty($this->errors);
     $this->tpl_form_vars['ps_force_friendly_product'] = Configuration::get('PS_FORCE_FRIENDLY_PRODUCT');
     // autoload rich text editor (tiny mce)
     $this->tpl_form_vars['tinymce'] = true;
     $iso = $this->context->language->iso_code;
     $this->tpl_form_vars['iso'] = file_exists(_PS_CORE_DIR_ . '/js/tiny_mce/langs/' . $iso . '.js') ? $iso : 'en';
     $this->tpl_form_vars['path_css'] = _THEME_CSS_DIR_;
     $this->tpl_form_vars['ad'] = __PS_BASE_URI__ . basename(_PS_ADMIN_DIR_);
     if (Validate::isLoadedObject($this->object)) {
         $id_product = (int) $this->object->id;
     } else {
         $id_product = (int) Tools::getvalue('id_product');
     }
     $this->tpl_form_vars['form_action'] = $this->context->link->getAdminLink('AdminProducts') . '&' . ($id_product ? 'id_product=' . (int) $id_product : 'addproduct');
     $this->tpl_form_vars['id_product'] = $id_product;
     //Добавляем в форму переменную с рекомендуемым reference = максимальный в базе + 1
     if (!$id_product) {
         $result = "";
         $sql = "SELECT LPAD(max(LPAD(RIGHT(`reference`, 5),5,0))+1,5,0) as ref FROM `" . _DB_PREFIX_ . "product`\nWHERE `reference` REGEXP '^-?[0-9]+\$'";
         $result = Db::getInstance()->getValue($sql);
         if ($result) {
             $this->tpl_form_vars['next_free_reference'] = $result;
         }
     }
     // Transform configuration option 'upload_max_filesize' in octets
     $upload_max_filesize = Tools::getOctets(ini_get('upload_max_filesize'));
     // Transform configuration option 'upload_max_filesize' in MegaOctets
     $upload_max_filesize = $upload_max_filesize / 1024 / 1024;
     $this->tpl_form_vars['upload_max_filesize'] = $upload_max_filesize;
     $this->tpl_form_vars['country_display_tax_label'] = $this->context->country->display_tax_label;
     $this->tpl_form_vars['has_combinations'] = $this->object->hasAttributes();
     $this->product_exists_in_shop = true;
     if ($this->display == 'edit' && Validate::isLoadedObject($product) && Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP && !$product->isAssociatedToShop($this->context->shop->id)) {
         $this->product_exists_in_shop = false;
         if ($this->tab_display == 'Informations') {
             $this->displayWarning($this->l('Warning: The product does not exist in this shop'));
         }
         $default_product = new Product();
         $definition = ObjectModel::getDefinition($product);
         foreach ($definition['fields'] as $field_name => $field) {
             if (isset($field['shop']) && $field['shop']) {
                 $product->{$field_name} = ObjectModel::formatValue($default_product->{$field_name}, $field['type']);
             }
         }
     }
     // let's calculate this once for all
     if (!Validate::isLoadedObject($this->object) && Tools::getValue('id_product')) {
         $this->errors[] = 'Unable to load object';
     } else {
         $this->_displayDraftWarning($this->object->active);
         // if there was an error while saving, we don't want to lose posted data
         if (!empty($this->errors)) {
             $this->copyFromPost($this->object, $this->table);
         }
         $this->initPack($this->object);
         $this->{'initForm' . $this->tab_display}($this->object);
         $this->tpl_form_vars['product'] = $this->object;
         if ($this->ajax) {
             if (!isset($this->tpl_form_vars['custom_form'])) {
                 throw new PrestaShopException('custom_form empty for action ' . $this->tab_display);
             } else {
                 return $this->tpl_form_vars['custom_form'];
             }
         }
     }
     $parent = parent::renderForm();
     $this->addJqueryPlugin(array('autocomplete', 'fancybox', 'typewatch'));
     return $parent;
 }
 public function renderForm()
 {
     $this->product_name = $this->object->name[$this->context->language->id];
     if (!method_exists($this, 'initForm' . $this->tab_display)) {
         return;
     }
     $product = $this->object;
     $this->context->smarty->assign('bullet_common_field', '');
     if (Shop::isFeatureActive() && $this->display == 'edit') {
         if (Shop::getContext() != Shop::CONTEXT_SHOP) {
             $this->context->smarty->assign(array('display_multishop_checkboxes' => true, 'multishop_check' => Tools::getValue('multishop_check')));
         }
         if (Shop::getContext() != Shop::CONTEXT_ALL) {
             $this->context->smarty->assign('bullet_common_field', '<i class="icon-circle text-orange"></i>');
             $this->context->smarty->assign('display_common_field', true);
         }
     }
     $this->tpl_form_vars['tabs_preloaded'] = $this->available_tabs;
     $this->tpl_form_vars['product_type'] = (int) Tools::getValue('type_product', $product->getType());
     $this->getLanguages();
     $this->tpl_form_vars['id_lang_default'] = Configuration::get('PS_LANG_DEFAULT');
     $this->tpl_form_vars['currentIndex'] = self::$currentIndex;
     $this->tpl_form_vars['display_multishop_checkboxes'] = Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_SHOP && $this->display == 'edit';
     $this->fields_form = array('');
     $this->tpl_form_vars['token'] = $this->token;
     $this->tpl_form_vars['combinationImagesJs'] = $this->getCombinationImagesJs();
     $this->tpl_form_vars['PS_ALLOW_ACCENTED_CHARS_URL'] = (int) Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL');
     $this->tpl_form_vars['post_data'] = json_encode($_POST);
     $this->tpl_form_vars['save_error'] = !empty($this->errors);
     $this->tpl_form_vars['mod_evasive'] = Tools::apacheModExists('evasive');
     $this->tpl_form_vars['mod_security'] = Tools::apacheModExists('security');
     $this->tpl_form_vars['ps_force_friendly_product'] = Configuration::get('PS_FORCE_FRIENDLY_PRODUCT');
     $this->tpl_form_vars['tinymce'] = true;
     $iso = $this->context->language->iso_code;
     $this->tpl_form_vars['iso'] = file_exists(_PS_CORE_DIR_ . '/js/tiny_mce/langs/' . $iso . '.js') ? $iso : 'en';
     $this->tpl_form_vars['path_css'] = _THEME_CSS_DIR_;
     $this->tpl_form_vars['ad'] = __PS_BASE_URI__ . basename(_PS_ADMIN_DIR_);
     if (Validate::isLoadedObject($this->object)) {
         $id_product = (int) $this->object->id;
     } else {
         $id_product = (int) Tools::getvalue('id_product');
     }
     $this->tpl_form_vars['form_action'] = $this->context->link->getAdminLink('AdminProducts') . '&' . ($id_product ? 'id_product=' . (int) $id_product : 'addproduct');
     $this->tpl_form_vars['id_product'] = $id_product;
     $upload_max_filesize = Tools::getOctets(ini_get('upload_max_filesize'));
     $upload_max_filesize = $upload_max_filesize / 1024 / 1024;
     $this->tpl_form_vars['upload_max_filesize'] = $upload_max_filesize;
     $this->tpl_form_vars['country_display_tax_label'] = $this->context->country->display_tax_label;
     $this->tpl_form_vars['has_combinations'] = $this->object->hasAttributes();
     $this->product_exists_in_shop = true;
     if ($this->display == 'edit' && Validate::isLoadedObject($product) && Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP && !$product->isAssociatedToShop($this->context->shop->id)) {
         $this->product_exists_in_shop = false;
         if ($this->tab_display == 'Informations') {
             $this->displayWarning($this->l('Warning: The product does not exist in this shop'));
         }
         $default_product = new Product();
         $definition = ObjectModel::getDefinition($product);
         foreach ($definition['fields'] as $field_name => $field) {
             if (isset($field['shop']) && $field['shop']) {
                 $product->{$field_name} = ObjectModel::formatValue($default_product->{$field_name}, $field['type']);
             }
         }
     }
     if (!Validate::isLoadedObject($this->object) && Tools::getValue('id_product')) {
         $this->errors[] = 'Unable to load object';
     } else {
         $this->_displayDraftWarning($this->object->active);
         if (!empty($this->errors)) {
             $this->copyFromPost($this->object, $this->table);
         }
         $this->initPack($this->object);
         $this->{'initForm' . $this->tab_display}($this->object);
         $this->tpl_form_vars['product'] = $this->object;
         if ($this->ajax) {
             if (!isset($this->tpl_form_vars['custom_form'])) {
                 throw new PrestaShopException('custom_form empty for action ' . $this->tab_display);
             } else {
                 return $this->tpl_form_vars['custom_form'];
             }
         }
     }
     $parent = parent::renderForm();
     $this->addJqueryPlugin(array('autocomplete', 'fancybox', 'typewatch'));
     return $parent;
 }
예제 #4
0
    /**
     * Takes current object ID, gets its values from database,
     * saves them in a new row and loads newly saved values as a new object.
     *
     * @return ObjectModel|false
     * @throws PrestaShopDatabaseException
     */
    public function duplicateObject()
    {
        $definition = ObjectModel::getDefinition($this);
        $res = Db::getInstance()->getRow('
					SELECT *
					FROM `' . _DB_PREFIX_ . bqSQL($definition['table']) . '`
					WHERE `' . bqSQL($definition['primary']) . '` = ' . (int) $this->id);
        if (!$res) {
            return false;
        }
        unset($res[$definition['primary']]);
        foreach ($res as $field => &$value) {
            if (isset($definition['fields'][$field])) {
                $value = ObjectModel::formatValue($value, $definition['fields'][$field]['type'], false, true, !empty($definition['fields'][$field]['allow_null']));
            }
        }
        if (!Db::getInstance()->insert($definition['table'], $res)) {
            return false;
        }
        $object_id = Db::getInstance()->Insert_ID();
        if (isset($definition['multilang']) && $definition['multilang']) {
            $result = Db::getInstance()->executeS('
			SELECT *
			FROM `' . _DB_PREFIX_ . bqSQL($definition['table']) . '_lang`
			WHERE `' . bqSQL($definition['primary']) . '` = ' . (int) $this->id);
            if (!$result) {
                return false;
            }
            foreach ($result as &$row) {
                foreach ($row as $field => &$value) {
                    if (isset($definition['fields'][$field])) {
                        $value = ObjectModel::formatValue($value, $definition['fields'][$field]['type'], false, true, !empty($definition['fields'][$field]['allow_null']));
                    }
                }
            }
            // Keep $row2, you cannot use $row because there is an unexplicated conflict with the previous usage of this variable
            foreach ($result as $row2) {
                $row2[$definition['primary']] = (int) $object_id;
                if (!Db::getInstance()->insert($definition['table'] . '_lang', $row2)) {
                    return false;
                }
            }
        }
        /** @var ObjectModel $object_duplicated */
        $object_duplicated = new $definition['classname']((int) $object_id);
        $object_duplicated->duplicateShops((int) $this->id);
        return $object_duplicated;
    }
예제 #5
0
 /**
  * @since 1.5.0
  * @param int $type FORMAT_COMMON or FORMAT_LANG or FORMAT_SHOP
  * @param int $id_lang If this parameter is given, only take lang fields
  * @return array
  */
 protected function formatFields($type, $id_lang = null)
 {
     $fields = array();
     // Set primary key in fields
     if (isset($this->id)) {
         $fields[$this->def['primary']] = $this->id;
     }
     foreach ($this->def['fields'] as $field => $data) {
         $value = $this->{$field};
         $fields[$field] = ObjectModel::formatValue($value, $data['type']);
     }
     return $fields;
 }
예제 #6
0
 /**
  * Format a value with the type of the given field
  *
  * @param mixed $value
  * @param string $field Field name
  * @return mixed
  */
 protected function formatValue($value, $field)
 {
     $info = $this->getFieldInfo($field);
     if (is_array($value)) {
         $results = array();
         foreach ($value as $item) {
             $results[] = ObjectModel::formatValue($item, $info['type'], true);
         }
         return $results;
     }
     return ObjectModel::formatValue($value, $info['type'], true);
 }
예제 #7
0
 /**
  * @since 1.5.0
  * @param int $type FORMAT_COMMON or FORMAT_LANG or FORMAT_SHOP
  * @param int $id_lang If this parameter is given, only take lang fields
  * @return array
  */
 protected function formatFields($type, $id_lang = null)
 {
     $fields = array();
     // Set primary key in fields
     if (isset($this->id)) {
         $fields[$this->def['primary']] = $this->id;
     }
     foreach ($this->def['fields'] as $field => $data) {
         // Only get fields we need for the type
         // E.g. if only lang fields are filtered, ignore fields without lang => true
         if ($type == self::FORMAT_LANG && empty($data['lang']) || $type == self::FORMAT_SHOP && empty($data['shop']) || $type == self::FORMAT_COMMON && (!empty($data['shop']) || !empty($data['lang']))) {
             continue;
         }
         if (is_array($this->update_fields)) {
             if ((!empty($data['lang']) || !empty($data['shop'])) && (empty($this->update_fields[$field]) || $type == self::FORMAT_LANG && empty($this->update_fields[$field][$id_lang]))) {
                 continue;
             }
         }
         // Get field value, if value is multilang and field is empty, use value from default lang
         $value = $this->{$field};
         if ($type == self::FORMAT_LANG && $id_lang && is_array($value)) {
             if (!empty($value[$id_lang])) {
                 $value = $value[$id_lang];
             } else {
                 if (!empty($data['required'])) {
                     $value = $value[Configuration::get('PS_LANG_DEFAULT')];
                 } else {
                     $value = '';
                 }
             }
         }
         // Format field value
         $fields[$field] = ObjectModel::formatValue($value, $data['type']);
     }
     return $fields;
 }