function dod_modify_bonuses($generator_id)
{
    global $tables, $bonus_names;
    global $dod_bonuses, $dod_bonus;
    if (empty($generator_id)) {
        $GLOBALS['_dod_bonuses'] =& $dod_bonuses;
        cw_session_register('_dod_bonuses');
        $GLOBALS['_dod_bonus'] =& $dod_bonus;
        cw_session_register('_dod_bonus');
        return array(true, null);
        //return array(false, 'generator Id was not provided');
    }
    db_query("DELETE FROM {$tables['dod_bonuses']} WHERE generator_id = '{$generator_id}'");
    db_query("DELETE FROM {$tables['dod_bonus_details']} WHERE generator_id = '{$generator_id}'");
    $available_fields = array('bonus_id' => 'int', 'generator_id' => 'int', 'type' => 'string', 'apply' => 'int', 'coupon' => 'string', 'discount' => 'float', 'disctype' => 'int');
    $excl_from_base_list = array('bonus_id');
    foreach ($excl_from_base_list as $field) {
        if (isset($available_fields[$field])) {
            unset($available_fields[$field]);
        }
    }
    $optional_fields = array('discount', 'disctype');
    $date_fields = array();
    $skip_striptags_fields = array();
    if (empty($dod_bonuses[DOD_DISCOUNT])) {
        $dod_bonuses[DOD_DISCOUNT] = 1;
        $unused_dod_discount_bonus = 1;
    } else {
        $unused_dod_discount_bonus = 0;
    }
    if (empty($dod_bonuses) || !is_array($dod_bonuses)) {
        return array(true, null);
    }
    $available_btypes = array(DOD_DISCOUNT, DOD_FREE_PRODS, DOD_FREE_SHIP, DOD_COUPON);
    $bonuses = array();
    foreach ($dod_bonuses as $bonus_type => $trash) {
        if (!isset($dod_bonus[$bonus_type]) || empty($dod_bonus[$bonus_type]) || !in_array($bonus_type, $available_btypes)) {
            unset($dod_bonuses[$bonus_type]);
        } else {
            $bonuses[$bonus_type] = $dod_bonus[$bonus_type];
        }
    }
    unset($dod_bonus);
    if (empty($dod_bonuses) || empty($bonuses)) {
        return array(true, null);
    }
    $GLOBALS['_dod_bonuses'] =& $dod_bonuses;
    cw_session_register('_dod_bonuses');
    $errors = array();
    $tmp_optional_fields = $optional_fields;
    foreach ($bonuses as $bonus_type => $input_data) {
        $optional_fields = $tmp_optional_fields;
        $additional_lang_data = array();
        $pids = $cids = array();
        $input_data['generator_id'] = $generator_id;
        $input_data['type'] = $bonus_type;
        if ($bonus_type != DOD_COUPON) {
            $input_data['coupon'] = 1;
            if ($input_data['apply'] == DOD_APPLY_PRODS || $bonus_type == DOD_FREE_PRODS || $bonus_type == DOD_DISCOUNT) {
                if (!isset($input_data['products']) && !isset($input_data['cats']) && !isset($input_data['mans']) && !isset($input_data['attr']) || empty($input_data['products']) && empty($input_data['cats']) && empty($input_data['mans']) && empty($input_data['attr'])) {
                    $additional_lang_data = array('bonus' => cw_get_langvar_by_name($bonus_names[$bonus_type]));
                    $errors[] = cw_get_langvar_by_name('msg_dod_bonus_incorrect', $additional_lang_data);
                    continue;
                }
                if (isset($input_data['products']) && !empty($input_data['products'])) {
                    $products_data = array();
                    foreach ($input_data['products'] as $product_data) {
                        $product_data['id'] = trim($product_data['id']);
                        $products_data[$product_data['id']] = $product_data['quantity'];
                    }
                    $pids = array_keys($products_data);
                    $pids = cw_query_column("SELECT product_id as id FROM {$tables['products']} WHERE product_id IN ('" . implode("','", $pids) . "')");
                }
                if (isset($input_data['cats']) && !empty($input_data['cats'])) {
                    $cats_data = array();
                    foreach ($input_data['cats'] as $cat_data) {
                        $cat_data['id'] = trim($cat_data['id']);
                        $cats_data[$cat_data['id']] = $cat_data['quantity'];
                    }
                    $cids = array_keys($cats_data);
                    $cids = cw_query_column("SELECT category_id as id FROM {$tables['categories']} WHERE category_id IN ('" . implode("','", $cids) . "')");
                }
                if (isset($input_data['mans']) && !empty($input_data['mans'])) {
                    $mids = array_values($input_data['mans']);
                }
                if (isset($input_data['attr'])) {
                    // Prepare attributes data
                    if (isset($input_data['attr']) && !empty($input_data['attr'])) {
                        $attr_data = array();
                        foreach ($input_data['attr'] as $a_data) {
                            $attr_data[trim($a_data['attribute_id'])] = array('quantity' => $a_data['quantity'], 'value' => current($a_data['value']), 'operation' => $a_data['operation']);
                        }
                        $attrids = array_keys($attr_data);
                        $attrids = cw_query_column("SELECT attribute_id as id FROM {$tables['attributes']} WHERE attribute_id IN ('" . implode("','", $attrids) . "')");
                    }
                }
                if (empty($pids) && empty($cids) && empty($mids) && empty($attrids)) {
                    $additional_lang_data = array('bonus' => cw_get_langvar_by_name($bonus_names[$bonus_type]));
                    $errors[] = cw_get_langvar_by_name('msg_dod_bonus_incorrect', $additional_lang_data);
                    continue;
                }
            }
        }
        if ($bonus_type != DOD_DISCOUNT && $bonus_type != DOD_FREE_SHIP) {
            $input_data['discount'] = $input_data['disctype'] = null;
        } elseif ($bonus_type == DOD_FREE_SHIP) {
            $input_data['disctype'] = null;
        } else {
            $optional_fields = array();
        }
        if (in_array($bonus_type, array(DOD_FREE_PRODS, DOD_COUPON))) {
            $optional_fields[] = 'apply';
        }
        $data = array();
        foreach ($date_fields as $field) {
            if (isset($input_data[$field]) && !empty($input_data[$field])) {
                $input_data[$field] = cw_core_strtotime($input_data[$field]);
            }
        }
        $error = null;
        foreach ($available_fields as $field => $field_type) {
            if (isset($input_data[$field])) {
                $result = settype($input_data[$field], $field_type);
                if ($result === false) {
                    $error = 'msg_dod_incorrect_field_type';
                    $additional_lang_data = array('field_name' => $field);
                    break;
                }
                if (empty($input_data[$field]) && !($bonus_type == DOD_DISCOUNT && $unused_dod_discount_bonus)) {
                    if (in_array($field, $optional_fields)) {
                        $data[$field] = null;
                    } else {
                        $additional_lang_data = array('bonus' => cw_get_langvar_by_name($bonus_names[$bonus_type]));
                        $error = 'msg_dod_bonus_incorrect';
                        break;
                    }
                } else {
                    if ($field_type == 'string' && !in_array($field, $skip_striptags_fields)) {
                        $input_data[$field] = cw_strip_tags($input_data[$field]);
                    }
                    $data[$field] =& $input_data[$field];
                }
            } else {
                if ($field_type == 'bool') {
                    $data[$field] = 0;
                } else {
                    if (in_array($field, $optional_fields)) {
                        $data[$field] = null;
                    } else {
                        $additional_lang_data = array('bonus' => cw_get_langvar_by_name($bonus_names[$bonus_type]));
                        $error = 'msg_dod_bonus_incorrect';
                        break;
                    }
                }
            }
        }
        if (!empty($error)) {
            $errors[] = cw_get_langvar_by_name($error, $additional_lang_data);
            continue;
        }
        if (empty($data)) {
            continue;
        }
        if ($data['type'] == DOD_DISCOUNT) {
            $data['unused'] = $unused_dod_discount_bonus;
        }
        $bonus_id = cw_array2insert($tables['dod_bonuses'], cw_addslashes($data));
        if ($bonus_type == DOD_FREE_SHIP) {
            foreach ($input_data['methods'] as $trash => $shipping_id) {
                $data = array('generator_id' => $generator_id, 'bonus_id' => $bonus_id, 'object_id' => $shipping_id, 'object_type' => DOD_OBJ_TYPE_SHIPPING);
                cw_array2insert($tables['dod_bonus_details'], cw_addslashes($data));
            }
        }
        if ($bonus_type != DOD_COUPON) {
            if ($input_data['apply'] == DOD_APPLY_PRODS || $bonus_type == DOD_FREE_PRODS || $bonus_type == DOD_DISCOUNT) {
                if (!empty($pids)) {
                    $data = array();
                    $data['generator_id'] = $generator_id;
                    $data['bonus_id'] = $bonus_id;
                    foreach ($pids as $pid) {
                        $data['object_id'] = $pid;
                        $data['object_type'] = DOD_OBJ_TYPE_PRODS;
                        $data['quantity'] = $products_data[$pid];
                        if (empty($data['quantity'])) {
                            $data['quantity'] = 1;
                        }
                        cw_array2insert($tables['dod_bonus_details'], cw_addslashes($data));
                    }
                }
                if (!empty($cids)) {
                    $data = array();
                    $data['generator_id'] = $generator_id;
                    $data['bonus_id'] = $bonus_id;
                    foreach ($cids as $cid) {
                        $data['object_id'] = $cid;
                        $data['object_type'] = DOD_OBJ_TYPE_CATS;
                        $data['quantity'] = $cats_data[$cid];
                        if (empty($data['quantity'])) {
                            $data['quantity'] = 1;
                        }
                        cw_array2insert($tables['dod_bonus_details'], cw_addslashes($data));
                    }
                }
                if (!empty($mids)) {
                    $data = array();
                    $data['generator_id'] = $generator_id;
                    $data['bonus_id'] = $bonus_id;
                    $data['quantity'] = 1;
                    foreach ($mids as $mid) {
                        $data['object_id'] = $mid;
                        $data['object_type'] = DOD_OBJ_TYPE_MANS;
                        cw_array2insert($tables['dod_bonus_details'], cw_addslashes($data));
                    }
                }
                // Save attributes to condition details
                if (!empty($attrids)) {
                    $data = array();
                    $data['generator_id'] = $generator_id;
                    $data['bonus_id'] = $bonus_id;
                    $data['quantity'] = 1;
                    foreach ($attrids as $aid) {
                        $data['object_id'] = $aid;
                        $data['object_type'] = DOD_OBJ_TYPE_ATTR;
                        $data['param1'] = $attr_data[$aid]['value'];
                        $data['param2'] = $attr_data[$aid]['operation'];
                        cw_array2insert($tables['dod_bonus_details'], cw_addslashes($data));
                    }
                }
            }
        }
        unset($bonuses[$bonus_type]);
    }
    if (!empty($bonuses)) {
        $GLOBALS['_dod_bonus'] =& $bonuses;
        cw_session_register('_dod_bonus');
    }
    if (!empty($errors)) {
        $error = implode("<br />\n", $errors);
        return array(false, $error);
    }
    return array(true, null);
}
function ppd_add($product_id)
{
    global $tables, $top_message, $smarty, $available_fields;
    global $extra_fields, $optional_fields, $skip_striptags_fields;
    $product_id = (int) $product_id;
    global $new_files;
    if (!isset($new_files)) {
        $new_files = null;
        if (isset($_POST['new_files'])) {
            $new_files =& $_POST['new_files'];
        }
    }
    if (empty($new_files) || !is_array($new_files) || $_SERVER['REQUEST_METHOD'] != 'POST') {
        ppd_redirect($product_id);
    }
    if (isset($available_fields['file_id'])) {
        unset($available_fields['file_id']);
    }
    array_push($optional_fields, 'size');
    array_push($optional_fields, 'type_id');
    $error_description = null;
    $number_files = 0;
    foreach ($new_files as $new_file_key => $new_file) {
        $data = array();
        $error = null;
        $additional_lang_data = array();
        $_additional_lang_data = array('number' => ++$number_files);
        $_real_path = ppd_check_path($new_file['filename']);
        $new_file['size'] = null;
        if (empty($_real_path)) {
            $new_file['filename'] = null;
        }
        if (!empty($new_file['filename'])) {
            $new_file['size'] = ppd_get_filesize($_real_path);
            if (empty($new_file['size'])) {
                $error = 'msg_ppd_file_is_empty';
                $error_description .= cw_get_langvar_by_name('lbl_ppd_skipped_element', $_additional_lang_data) . ' ';
                $error_description .= cw_get_langvar_by_name($error) . '<br />';
                continue;
            }
            $_data = array();
            $_mime_type_by_ext = array();
            $_file_mime_type = ppd_get_mime_type($_real_path);
            $_file_extension = ppd_get_file_extension($_real_path);
            if (!empty($_file_mime_type)) {
                if (!empty($_file_extension)) {
                    $_mime_type_by_ext = cw_query_first('SELECT type_id, type FROM ' . $tables['ppd_types'] . ' WHERE extension = \'' . addslashes($_file_extension) . '\'');
                }
                $new_file['type_id'] = cw_query_first_cell('SELECT type_id FROM ' . $tables['ppd_types'] . ' WHERE type = \'' . addslashes($_file_mime_type) . '\'');
                if (isset($_mime_type_by_ext['type_id']) && $_mime_type_by_ext['type_id'] != $new_file['type_id']) {
                    $new_file['type_id'] = $_mime_type_by_ext['type_id'];
                }
                if (empty($new_file['type_id'])) {
                    $_data['type'] = $_file_mime_type;
                    $_data['extension'] = $_file_extension;
                    $_data['fileicon'] = null;
                    if (!empty($_file_extension)) {
                        $_data['fileicon'] = 'icon_' . $_file_extension . '.gif';
                    }
                    $new_file['type_id'] = cw_array2insert($tables['ppd_types'], $_data);
                }
            }
        }
        foreach ($available_fields as $field => $field_type) {
            if (!isset($new_file[$field])) {
                if (in_array($field, $optional_fields)) {
                    continue;
                } elseif (isset($extra_fields[$field])) {
                    $data[$field] = 0;
                } else {
                    $error = 'msg_ppd_empty_fields';
                    break;
                }
            } else {
                if (isset($extra_fields[$field]) && is_array($extra_fields[$field])) {
                    $_total_value = $_value = null;
                    foreach ($extra_fields[$field] as $extra_field) {
                        $_value = isset($new_file[$field][$extra_field]) ? $new_file[$field][$extra_field] : null;
                        @settype($_value, $field_type);
                        $_total_value += $_value;
                    }
                    $new_file[$field] = $_total_value;
                }
                $result = settype($new_file[$field], $field_type);
                if ($result === false) {
                    $error = 'msg_ppd_incorrect_field_type';
                    $additional_lang_data = array('field_name' => $field);
                    break;
                }
                if (empty($new_file[$field])) {
                    if (in_array($field, $optional_fields)) {
                        continue;
                    } else {
                        $error = 'msg_ppd_empty_fields';
                        break;
                    }
                }
                if ($field_type == 'string' && !in_array($field, $skip_striptags_fields)) {
                    $new_file[$field] = cw_strip_tags($new_file[$field]);
                }
                $data[$field] =& $new_file[$field];
            }
        }
        if (!empty($error)) {
            $error_description .= cw_get_langvar_by_name('lbl_ppd_skipped_element', $_additional_lang_data) . ' ';
            $error_description .= cw_get_langvar_by_name($error, $additional_lang_data) . '<br />';
        }
        if (empty($error) && !empty($data)) {
            $file_exists = cw_query_first_cell('SELECT file_id FROM ' . $tables['ppd_files'] . ' WHERE filename = \'' . addslashes($data['filename']) . '\' AND product_id = \'' . $product_id . '\'');
            if ($file_exists) {
                $error = 'msg_ppd_file_already_exists';
                $additional_lang_data = array('file' => $data['filename']);
                $error_description .= cw_get_langvar_by_name('lbl_ppd_skipped_element', $_additional_lang_data) . ' ';
                $error_description .= cw_get_langvar_by_name($error, $additional_lang_data) . '<br />';
                continue;
            }
            $data['product_id'] = $product_id;
            cw_array2insert($tables['ppd_files'], $data);
            unset($new_files[$new_file_key]);
        }
    }
    if (!empty($new_files)) {
        $new_files = array_values($new_files);
        $GLOBALS['_new_files'] =& $new_files;
        cw_session_register('_new_files');
    }
    if (!empty($error_description)) {
        $top_message = array('content' => $error_description, 'type' => 'E');
    } else {
        $top_message = array('content' => cw_get_langvar_by_name('msg_ppd_updated_succes'), 'type' => 'I');
        cw_session_unregister('_new_files');
    }
    ppd_redirect($product_id);
}
function ppd_filetype_add()
{
    global $tables, $top_message, $smarty, $available_fields;
    global $optional_fields, $skip_striptags_fields;
    global $new_types;
    if (!isset($new_types)) {
        $new_types = null;
        if (isset($_POST['new_types'])) {
            $new_types =& $_POST['new_types'];
        }
    }
    if (empty($new_types) || !is_array($new_types) || $_SERVER['REQUEST_METHOD'] != 'POST') {
        ppd_filetype_redirect();
    }
    if (isset($available_fields['type_id'])) {
        unset($available_fields['type_id']);
    }
    $error_description = null;
    $number_types = 0;
    foreach ($new_types as $new_type_key => $new_type) {
        $data = array();
        $error = null;
        $additional_lang_data = array();
        $_additional_lang_data = array('number' => ++$number_types);
        if (isset($new_type['extension']) && !empty($new_type['extension'])) {
            $_file_exists = cw_query_first_cell('SELECT type_id FROM ' . $tables['ppd_types'] . ' WHERE extension = \'' . addslashes($new_type['extension']) . '\'');
            if (!empty($_file_exists)) {
                $error = 'msg_ppd_exts_exists';
                $error_description .= cw_get_langvar_by_name('lbl_ppd_filetype_skipped', $_additional_lang_data) . ' ';
                $error_description .= cw_get_langvar_by_name($error) . '<br />';
                continue;
            }
        }
        foreach ($available_fields as $field => $field_type) {
            if (!isset($new_type[$field])) {
                if (in_array($field, $optional_fields)) {
                    continue;
                } else {
                    $error = 'msg_ppd_empty_fields';
                    break;
                }
            } else {
                $result = settype($new_type[$field], $field_type);
                if ($result === false) {
                    $error = 'msg_ppd_incorrect_field_type';
                    $additional_lang_data = array('field_name' => $field);
                    break;
                }
                if (empty($new_type[$field])) {
                    if (in_array($field, $optional_fields)) {
                        continue;
                    } else {
                        $error = 'msg_ppd_empty_fields';
                        break;
                    }
                }
                if ($field_type == 'string' && !in_array($field, $skip_striptags_fields)) {
                    $new_type[$field] = cw_strip_tags($new_type[$field]);
                }
                $data[$field] =& $new_type[$field];
            }
        }
        if (!empty($error)) {
            $error_description .= cw_get_langvar_by_name('lbl_ppd_filetype_skipped', $_additional_lang_data) . ' ';
            $error_description .= cw_get_langvar_by_name($error, $additional_lang_data) . '<br />';
        }
        if (empty($error) && !empty($data)) {
            cw_array2insert($tables['ppd_types'], $data);
            unset($new_types[$new_type_key]);
        }
    }
    if (!empty($new_types)) {
        $new_types = array_values($new_types);
        $GLOBALS['_new_types'] =& $new_types;
        cw_session_register('_new_types');
    }
    if (!empty($error_description)) {
        $top_message = array('content' => $error_description, 'type' => 'E');
        ppd_filetype_redirect(array('mode' => 'add'));
    } else {
        $top_message = array('content' => cw_get_langvar_by_name('msg_ppd_filetypes_updated_succes'), 'type' => 'I');
        cw_session_unregister('_new_types');
    }
    ppd_filetype_redirect();
}
function ps_modify_conditions($offer_id)
{
    global $tables, $cond_names;
    global $ps_conditions, $ps_conds;
    if (empty($offer_id)) {
        $GLOBALS['_ps_conditions'] =& $ps_conditions;
        cw_session_register('_ps_conditions');
        $GLOBALS['_ps_conds'] =& $ps_conds;
        cw_session_register('_ps_conds');
        return array(true, null);
        //return array(false, 'Offer Id was not provided');
    }
    db_query("DELETE FROM {$tables['ps_conditions']} WHERE offer_id = '{$offer_id}'");
    db_query("DELETE FROM {$tables['ps_cond_details']} WHERE offer_id = '{$offer_id}'");
    $available_fields = array('cond_id' => 'int', 'offer_id' => 'int', 'type' => 'string', 'coupon' => 'string');
    $excl_from_base_list = array('cond_id');
    foreach ($excl_from_base_list as $field) {
        if (isset($available_fields[$field])) {
            unset($available_fields[$field]);
        }
    }
    $optional_fields = array('coupon');
    $date_fields = array();
    $skip_striptags_fields = array();
    if (empty($ps_conditions) || !is_array($ps_conditions)) {
        return array(true, null);
    }
    $available_ctypes = array(PS_TOTAL, PS_SHIP_ADDRESS, PS_SPEC_PRODUCTS, PS_WEIGHT, PS_MEMBERSHIP, PS_USE_COUPON);
    $conditions = array();
    foreach ($ps_conditions as $cond_type => $trash) {
        if (!isset($ps_conds[$cond_type]) || empty($ps_conds[$cond_type]) || !in_array($cond_type, $available_ctypes)) {
            unset($ps_conditions[$cond_type]);
        } else {
            $conditions[$cond_type] = $ps_conds[$cond_type];
        }
    }
    unset($ps_conds);
    if (empty($ps_conditions) || empty($conditions)) {
        return array(true, null);
    }
    $GLOBALS['_ps_conditions'] =& $ps_conditions;
    cw_session_register('_ps_conditions');
    $errors = array();
    $tmp_optional_fields = $optional_fields;
    foreach ($conditions as $cond_type => $input_data) {
        $optional_fields = $tmp_optional_fields;
        $additional_lang_data = array();
        $pids = $cids = $mids = array();
        $input_data['offer_id'] = $offer_id;
        $input_data['type'] = $cond_type;
        if ($cond_type == PS_SPEC_PRODUCTS) {
            if (!isset($input_data['products']) && !isset($input_data['cats']) && !isset($input_data['mans']) && !isset($input_data['attr']) || empty($input_data['products']) && empty($input_data['cats']) && empty($input_data['mans']) && empty($input_data['attr'])) {
                $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                $errors[] = cw_get_langvar_by_name('msg_ps_cond_incorrect', $additional_lang_data);
                continue;
            }
            // Prepare products data
            if (isset($input_data['products']) && !empty($input_data['products'])) {
                $products_data = array();
                foreach ($input_data['products'] as $product_data) {
                    $product_data['id'] = trim($product_data['id']);
                    $products_data[$product_data['id']] = $product_data['quantity'];
                }
                $pids = array_keys($products_data);
                $pids = cw_query_column("SELECT product_id as id FROM {$tables['products']} WHERE product_id IN ('" . implode("','", $pids) . "')");
            }
            // Prepare categories data
            if (isset($input_data['cats']) && !empty($input_data['cats'])) {
                $cats_data = array();
                foreach ($input_data['cats'] as $cat_data) {
                    $cat_data['id'] = trim($cat_data['id']);
                    $cats_data[$cat_data['id']] = $cat_data['quantity'];
                }
                $cids = array_keys($cats_data);
                $cids = cw_query_column("SELECT category_id as id FROM {$tables['categories']} WHERE category_id IN ('" . implode("','", $cids) . "')");
            }
            // Prepare attributes data
            if (isset($input_data['attr']) && !empty($input_data['attr'])) {
                $attr_data = array();
                foreach ($input_data['attr'] as $a_data) {
                    $attr_data[trim($a_data['attribute_id'])] = array('quantity' => $a_data['quantity'], 'value' => current($a_data['value']), 'operation' => $a_data['operation']);
                }
                $attrids = array_keys($attr_data);
                $attrids = cw_query_column("SELECT attribute_id as id FROM {$tables['attributes']} WHERE attribute_id IN ('" . implode("','", $attrids) . "')");
            }
            // Prepare manufacturers data
            if (isset($input_data['mans']) && !empty($input_data['mans'])) {
                $mans_data = array();
                foreach ($input_data['mans'] as $man_data) {
                    $man_data['id'] = trim($man_data['id']);
                    $mans_data[$man_data['id']] = $man_data['quantity'];
                }
                $mids = array_keys($mans_data);
                $mids = cw_query_column("SELECT manufacturer_id as id FROM {$tables['manufacturers']} WHERE manufacturer_id IN ('" . implode("','", $mids) . "')");
            }
            if (empty($pids) && empty($cids) && empty($mids) && empty($attrids)) {
                $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                $errors[] = cw_get_langvar_by_name('msg_ps_cond_incorrect', $additional_lang_data);
                continue;
            }
        } elseif ($cond_type == PS_SHIP_ADDRESS) {
            if (!isset($input_data['zones']) || empty($input_data['zones'])) {
                $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                $errors[] = cw_get_langvar_by_name('msg_ps_cond_incorrect', $additional_lang_data);
            }
            $zone_ids = array_map(create_function('$id', 'return trim($id);'), $input_data['zones']);
            $zone_ids = cw_query_column("SELECT zone_id as id FROM {$tables['zones']} WHERE zone_id IN ('" . implode("','", $zone_ids) . "')");
            if (empty($zone_ids) || !is_array($zone_ids)) {
                $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                $errors[] = cw_get_langvar_by_name('msg_ps_cond_incorrect', $additional_lang_data);
            }
        } elseif ($cond_type == PS_TOTAL || $cond_type == PS_WEIGHT) {
            if (!isset($input_data['from']) || intval($input_data['from']) < 0 || !isset($input_data['till']) || intval($input_data['till']) < 0 || intval($input_data['till']) < intval($input_data['from']) && intval($input_data['till']) != 0 && intval($input_data['from']) != 0) {
                $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                $errors[] = cw_get_langvar_by_name('msg_ps_cond_incorrect', $additional_lang_data);
            }
        }
        $data = array();
        foreach ($date_fields as $field) {
            if (isset($input_data[$field]) && !empty($input_data[$field])) {
                $input_data[$field] = cw_core_strtotime($input_data[$field]);
            }
        }
        $error = null;
        foreach ($available_fields as $field => $field_type) {
            if (isset($input_data[$field])) {
                $result = settype($input_data[$field], $field_type);
                if ($result === false) {
                    $error = 'msg_ps_incorrect_field_type';
                    $additional_lang_data = array('field_name' => $field);
                    break;
                }
                if (empty($input_data[$field])) {
                    if (in_array($field, $optional_fields)) {
                        $data[$field] = null;
                    } else {
                        $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                        $error = 'msg_ps_cond_incorrect';
                        break;
                    }
                } else {
                    if ($field_type == 'string' && !in_array($field, $skip_striptags_fields)) {
                        $input_data[$field] = cw_strip_tags($input_data[$field]);
                    }
                    $data[$field] =& $input_data[$field];
                }
            } else {
                if ($field_type == 'bool') {
                    $data[$field] = 0;
                } else {
                    if (in_array($field, $optional_fields)) {
                        $data[$field] = null;
                    } else {
                        $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                        $error = 'msg_ps_cond_incorrect';
                        break;
                    }
                }
            }
        }
        if (!empty($error)) {
            $errors[] = cw_get_langvar_by_name($error, $additional_lang_data);
            continue;
        }
        if (empty($data)) {
            continue;
        }
        if ($cond_type == PS_WEIGHT || $cond_type == PS_TOTAL) {
            $data['total'] = intval($input_data['from']);
            $cond_id = cw_array2insert($tables['ps_conditions'], cw_addslashes($data));
            $data['total'] = intval($input_data['till']);
            $cond_id2 = cw_array2insert($tables['ps_conditions'], cw_addslashes($data));
        } else {
            $cond_id = cw_array2insert($tables['ps_conditions'], cw_addslashes($data));
        }
        if ($cond_type == PS_SPEC_PRODUCTS) {
            // Save products to condition details
            if (!empty($pids)) {
                $data = array();
                $data['offer_id'] = $offer_id;
                $data['cond_id'] = $cond_id;
                foreach ($pids as $pid) {
                    $data['object_id'] = $pid;
                    $data['object_type'] = PS_OBJ_TYPE_PRODS;
                    $data['quantity'] = $products_data[$pid];
                    if (empty($data['quantity'])) {
                        $data['quantity'] = 1;
                    }
                    cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
                }
            }
            // Save categories to condition details
            if (!empty($cids)) {
                $data = array();
                $data['offer_id'] = $offer_id;
                $data['cond_id'] = $cond_id;
                foreach ($cids as $cid) {
                    $data['object_id'] = $cid;
                    $data['object_type'] = PS_OBJ_TYPE_CATS;
                    $data['quantity'] = $cats_data[$cid];
                    if (empty($data['quantity'])) {
                        $data['quantity'] = 1;
                    }
                    cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
                }
            }
            // Save attributes to condition details
            if (!empty($attrids)) {
                $data = array();
                $data['offer_id'] = $offer_id;
                $data['cond_id'] = $cond_id;
                foreach ($attrids as $aid) {
                    $data['object_id'] = $aid;
                    $data['object_type'] = PS_OBJ_TYPE_ATTR;
                    $data['quantity'] = $attr_data[$aid]['quantity'];
                    $data['param1'] = $attr_data[$aid]['value'];
                    $data['param2'] = $attr_data[$aid]['operation'];
                    if (empty($data['quantity'])) {
                        $data['quantity'] = 1;
                    }
                    cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
                }
            }
            // Save manufacturers to condition details
            if (!empty($mids)) {
                $data = array();
                $data['offer_id'] = $offer_id;
                $data['cond_id'] = $cond_id;
                foreach ($mids as $mid) {
                    $data['object_id'] = $mid;
                    $data['object_type'] = PS_OBJ_TYPE_MANS;
                    $data['quantity'] = $mans_data[$mid];
                    if (empty($data['quantity'])) {
                        $data['quantity'] = 1;
                    }
                    cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
                }
            }
        } elseif ($cond_type == PS_SHIP_ADDRESS) {
            if (!empty($zone_ids)) {
                $data = array();
                $data['offer_id'] = $offer_id;
                $data['cond_id'] = $cond_id;
                foreach ($zone_ids as $zone_id) {
                    $data['object_id'] = $zone_id;
                    $data['object_type'] = PS_OBJ_TYPE_ZONES;
                    $data['quantity'] = null;
                    cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
                }
            }
        } elseif ($cond_type == PS_TOTAL || $cond_type == PS_WEIGHT) {
            $data = array();
            $data['offer_id'] = $offer_id;
            $data['cond_id'] = $cond_id;
            $data['object_id'] = 0;
            $data['object_type'] = PS_OBJ_TYPE_FROM;
            $data['quantity'] = 0;
            cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
            if (isset($cond_id2)) {
                $data['cond_id'] = $cond_id2;
                $data['object_type'] = PS_OBJ_TYPE_TILL;
                cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
            }
        } elseif ($cond_type == PS_MEMBERSHIP) {
            $data = array();
            $data['offer_id'] = $offer_id;
            $data['cond_id'] = $cond_id;
            $data['object_id'] = $input_data['membership'];
            $data['object_type'] = PS_OBJ_TYPE_MEMBERSHIP;
            $data['quantity'] = 0;
            cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
        }
        unset($conditions[$cond_type]);
    }
    if (!empty($conditions)) {
        $GLOBALS['_ps_conds'] =& $conditions;
        cw_session_register('_ps_conds');
    }
    if (!empty($errors)) {
        $error = implode("<br />\n", $errors);
        return array(false, $error);
    }
    return array(true, null);
}
function tabs_modify($tab_type = 'product', $product_id = 0)
{
    global $tables, $top_message, $smarty, $available_tab_fields, $optional_tab_fields, $skip_striptags_tab_fields;
    global $_pt_addon_tables;
    $product_id = (int) $product_id;
    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
        tabs_redirect($product_id);
    }
    global $tab_id;
    if (!isset($tab_id)) {
        $tab_id = isset($_POST['tab_id']) ? (int) $_POST['tab_id'] : 0;
    }
    $tab_id = (int) $tab_id;
    $product_id_condition = null;
    if ($tab_type == 'product') {
        if (empty($product_id)) {
            tabs_redirect();
        }
        $product_id_condition = 'product_id = \'' . $product_id . '\' AND ';
    }
    global $tab_data;
    if (!isset($tab_data)) {
        $tab_data = null;
        if (isset($_POST['tab_data'])) {
            $tab_data =& $_POST['tab_data'];
        }
    }
    $table = $_pt_addon_tables[$tab_type];
    if (empty($tab_id) || empty($tab_data) || !is_array($tab_data)) {
        tabs_redirect($product_id);
    }
    $tab_id = cw_query_first_cell('SELECT tab_id FROM ' . $tables[$table] . ' WHERE tab_id = \'' . $tab_id . '\'');
    if (empty($tab_id)) {
        tabs_redirect($product_id);
    }
    $error = null;
    $data = array();
    if (isset($available_tab_fields['tab_id'])) {
        unset($available_tab_fields['tab_id']);
    }
    $additional_lang_data = array();
    foreach ($available_tab_fields as $field => $field_type) {
        if (isset($tab_data[$field])) {
            $result = settype($tab_data[$field], $field_type);
            if ($result === false) {
                $error = 'msg_pt_incorrect_field_type';
                $additional_lang_data = array('field_name' => $field);
                break;
            }
            if (empty($tab_data[$field])) {
                if (in_array($field, $optional_tab_fields)) {
                    $data[$field] = null;
                }
            } else {
                if ($field_type == 'string' && !in_array($field, $skip_striptags_tab_fields)) {
                    $tab_data[$field] = cw_strip_tags($tab_data[$field]);
                }
                $data[$field] =& $tab_data[$field];
            }
        } else {
            if ($field_type == 'bool') {
                $data[$field] = 0;
            }
        }
    }
    if (!empty($error)) {
        $top_message = array('content' => cw_get_langvar_by_name($error, $additional_lang_data), 'type' => 'E');
        tabs_redirect($product_id, $tab_id);
    }
    if (empty($data)) {
        $error = 'msg_pt_nothing_to_update';
        $top_message = array('content' => cw_get_langvar_by_name($error, $additional_lang_data), 'type' => 'E');
        tabs_redirect($product_id, $tab_id);
    }
    $data['attributes'] = serialize($data['attributes']);
    cw_array2update($table, $data, $product_id_condition . 'tab_id = \'' . $tab_id . '\'');
    $top_message = array('content' => cw_get_langvar_by_name('msg_pt_updated_succes'), 'type' => 'I');
    tabs_redirect($product_id);
}
 foreach (array('_GET', '_POST', '_COOKIE', '_SERVER') as $__avar) {
     if (!$__quotes_qpc) {
         $GLOBALS[$__avar] = cw_addslashes($GLOBALS[$__avar]);
     } elseif (defined('CW_MAGIC_QUOTES_SYBASE')) {
         $GLOBALS[$__avar] = cw_stripslashes_sybase($GLOBALS[$__avar]);
         $GLOBALS[$__avar] = cw_addslashes($GLOBALS[$__avar]);
     } else {
         $GLOBALS[$__avar] = cw_addslashes_keys($GLOBALS[$__avar]);
     }
     foreach ($GLOBALS[$__avar] as $__var => $__res) {
         if (cw_allowed_var($__var)) {
             global ${$__var};
             // Only admin scripts and "html_*" vars are trusted until addons init.
             // Add your html_* vars into $cw_trusted_variables list in addon init to allow HTML tags
             if (APP_AREA != 'admin' && strpos($__var, 'html_') !== 0) {
                 $__res = cw_strip_tags($__res);
             }
             $GLOBALS[$__avar][$__var] = ${$__var} = $request_prepared[$__var] = $__res;
         } else {
             cw_unset($GLOBALS[$__avar], $__var);
         }
     }
     reset($GLOBALS[$__avar]);
 }
 foreach ($_FILES as $__name => $__value) {
     if (!cw_allowed_var($__name)) {
         continue;
     }
     ${$__name} = $__value['tmp_name'];
     foreach ($__value as $__k => $__v) {
         $__varname_ = $__name . "_" . $__k;