Exemple #1
0
    private function saveTemplate()
    {
        $result = array();
        $result['error'] = '';
        $mode = Tools::getValue('mode');
        if ($mode == 'add') {
            $id = 0;
        } elseif ($mode == 'copy') {
            $id = (int) Tools::getValue('id');
        } else {
            $mode = 'edit';
            $id = (int) Tools::getValue('id');
        }
        if ($id < 0) {
            return $result;
        }
        $errors = array();
        $templates = array();
        $template_properties = array();
        $template_properties['pp_explanation'] = (int) Tools::getValue('pp_explanation');
        $template_properties['pp_price_text'] = (int) Tools::getValue('pp_price_text');
        $template_properties['pp_qty_text'] = (int) Tools::getValue('pp_qty_text');
        $template_properties['pp_unity_text'] = (int) Tools::getValue('pp_unity_text');
        $ext_method = (int) Tools::getValue($this->multidimensional_plugin ? 'pp_ext_method' : 'pp_ext_method_fallback');
        if ($ext_method == 3) {
            $ext_method = 2;
        }
        $display_mode = (int) Tools::getValue('pp_display_mode') != 0 ? 1 : 0;
        if ((int) Tools::getValue('pp_display_mode_retail_price', 0) > 0) {
            $display_mode += 2;
        }
        if ((int) Tools::getValue('pp_display_mode_base_unit_price', 0) > 0) {
            $display_mode += 4;
        }
        $price_display_mode = (int) Tools::getValue('pp_price_display_mode');
        if (!in_array($price_display_mode, array(0, 1, 16))) {
            $price_display_mode = 0;
        }
        $hidden = (int) Tools::getValue('pp_bo_hidden', 0) == 1 ? 1 : 0;
        $pp_bo_qty_available_display = (int) Tools::getValue('pp_bo_qty_available_display');
        if (!in_array($pp_bo_qty_available_display, array(0, 1, 2))) {
            $pp_bo_qty_available_display = 0;
        }
        $measurement_system = (int) Tools::getValue('pp_bo_measurement_system');
        $unit_price_ratio = $this->getFloatValue('unit_price_ratio');
        $minimal_price_ratio = $this->getFloatValue('minimal_price_ratio');
        $qty_policy = (int) Tools::getValue('pp_qty_policy', 0);
        $qty_policy = $qty_policy == 3 ? 2 : $qty_policy;
        if ($ext_method > 0) {
            $ext_policy = (int) Tools::getValue('pp_ext_policy', 0);
            if (!in_array($ext_policy, array(0, 1, 2))) {
                $ext_policy = 0;
            }
            $qty_policy = $ext_policy == 1 ? 0 : 2;
        }
        $qty_mode = $qty_policy ? (int) Tools::getValue('pp_qty_mode') != 0 ? 1 : 0 : 0;
        $minimal_quantity = $qty_policy == 2 ? $this->getFloatValue('minimal_quantity') : (int) Tools::getValue('minimal_quantity');
        $default_quantity = $qty_policy == 2 ? $this->getFloatValue('default_quantity') : (int) Tools::getValue('default_quantity');
        $qty_step = $qty_policy == 2 ? $this->getFloatValue('qty_step') : (int) Tools::getValue('qty_step');
        $ms = PP::resolveMS($measurement_system);
        foreach ($this->active_languages as $language) {
            $id_lang = $language['id_lang'];
            $template = array();
            $data = array();
            $data['id_pp_template'] = $id;
            $data['qty_policy'] = $qty_policy;
            $data['qty_mode'] = $qty_mode;
            $data['display_mode'] = $display_mode;
            $data['price_display_mode'] = $price_display_mode;
            $data['measurement_system'] = $measurement_system;
            $data['unit_price_ratio'] = $unit_price_ratio;
            $data['minimal_price_ratio'] = $minimal_price_ratio;
            $data['minimal_quantity'] = $minimal_quantity;
            $data['default_quantity'] = $default_quantity;
            $data['qty_step'] = $qty_step;
            $data['ext'] = $ext_method > 0 ? 1 : 0;
            $data['qty_available_display'] = $pp_bo_qty_available_display;
            $data['hidden'] = $hidden;
            $data['css'] = Tools::getValue('pp_css');
            $data['template_properties'] = $template_properties;
            PP::calcProductProperties($template, $data);
            $this->getValue($template, 'name', $this->l('name:'), $errors, $id_lang);
            $template['description'] = Tools::getValue('description_input_' . $id_lang);
            $templates[$id_lang][$id] = $template;
        }
        if (count($errors) == 0) {
            $db = Db::getInstance();
            if ($mode == 'edit') {
                $id_pp_template = $id;
            } else {
                $id_pp_template = $this->getNextId($db, 'pp_template', 'id_pp_template');
                $db->execute('INSERT INTO `' . _DB_PREFIX_ . 'pp_template` (id_pp_template, version) VALUE (' . $id_pp_template . ', 0)');
                foreach ($this->active_languages as $language) {
                    $templates[$language['id_lang']][$id]['id_pp_template'] = $id_pp_template;
                }
            }
            $db->autoExecute(_DB_PREFIX_ . 'pp_template', array('version' => PP::PP_TEMPLATE_VERSION, 'qty_policy' => $template['pp_qty_policy'], 'qty_mode' => $template['pp_qty_mode'], 'display_mode' => $template['pp_display_mode'], 'price_display_mode' => $template['pp_price_display_mode'], 'measurement_system' => $template['pp_bo_measurement_system'], 'unit_price_ratio' => $template['pp_unit_price_ratio'], 'minimal_price_ratio' => $template['pp_minimal_price_ratio'], 'minimal_quantity' => $template['db_minimal_quantity'], 'default_quantity' => $template['db_default_quantity'], 'qty_step' => $template['db_qty_step'], 'ext' => $template['pp_ext'], 'qty_available_display' => $template['pp_bo_qty_available_display'], 'hidden' => $template['pp_bo_hidden'], 'css' => $template['pp_css']), 'UPDATE', 'id_pp_template = ' . $id_pp_template);
            $db->delete(_DB_PREFIX_ . 'pp_template_property', 'id_pp_template = ' . $id_pp_template);
            array_walk($template_properties, create_function('&$value, $key, $id_pp_template', '$value = "(".$id_pp_template.",\'".$key."\',".$value.")";'), $id_pp_template);
            $db->execute('INSERT INTO ' . _DB_PREFIX_ . 'pp_template_property (id_pp_template,pp_name,id_pp_property) VALUES ' . implode(',', $template_properties));
            foreach ($this->active_languages as $language) {
                $id_lang = $language['id_lang'];
                $template = $templates[$id_lang][$id];
                $r = $db->getRow('SELECT * FROM `' . _DB_PREFIX_ . 'pp_template_lang` WHERE id_pp_template = ' . $id_pp_template . ' AND id_lang=' . $id_lang);
                if ($r === false) {
                    $r = array('description_1' => '', 'description_2' => '', 'id_pp_template' => $id_pp_template, 'id_lang' => $id_lang);
                }
                $auto_desc = 0;
                if ($template['description'] == '') {
                    $auto_desc = 1;
                    $template['description'] = self::generateDescription($template, $id_lang);
                }
                $r[$ms != 2 ? 'description_1' : 'description_2'] = pSQL($template['description'], true);
                $r[$ms != 2 ? 'auto_desc_1' : 'auto_desc_2'] = $auto_desc;
                $r['name'] = pSQL($template['name'], true);
                $db->delete(_DB_PREFIX_ . 'pp_template_lang', 'id_pp_template = ' . $id_pp_template . ' AND id_lang=' . $id_lang);
                $db->autoExecute(_DB_PREFIX_ . 'pp_template_lang', $r, 'INSERT');
            }
            $db->delete(_DB_PREFIX_ . 'pp_template_ext', 'id_pp_template = ' . $id_pp_template);
            $db->delete(_DB_PREFIX_ . 'pp_template_ext_prop', 'id_pp_template = ' . $id_pp_template);
            if ($template['pp_ext'] == 1) {
                $ext_title = (int) Tools::getValue('pp_ext_title', 0);
                $ext_property = (int) Tools::getValue('pp_ext_property', 0);
                $ext_text = (int) Tools::getValue('pp_ext_text', 0);
                //$ext_show_position = (int)Tools::getValue('pp_ext_show_position', 0);
                $s = (string) $id_pp_template;
                $s .= ',1';
                // type 1: quantity calculation based on dimensions
                $s .= ',' . $ext_policy;
                $s .= ',' . $ext_method;
                $s .= ',' . $ext_title;
                $s .= ',' . $ext_property;
                $s .= ',' . $ext_text;
                //$s .= ','.$ext_show_position;
                $db->execute('INSERT INTO ' . _DB_PREFIX_ . 'pp_template_ext (id_pp_template,type,policy,method,title,property,text) VALUES (' . $s . ')');
                if ($this->multidimensional_plugin) {
                    $this->multidimensional_plugin->saveTemplate($id_pp_template, self::DIMENSIONS);
                }
            }
            Hook::exec('ppropertiesAdmin', array('mode' => 'actionTemplateSave', 'id_pp_template' => $id));
            $templates = null;
            PP::resetTemplates();
        } else {
            $result['error'] .= $this->l('Please fix the following errors:');
            foreach ($errors as $error) {
                $result['error'] .= '
				<div>' . $error . '</div>';
            }
        }
        $result['templates'] = $templates;
        return $result;
    }