Пример #1
0
function fn_import_feature($data, &$processed_data, &$skip_record, $category_delimiter = '///')
{
    static $new_groups = array();
    $skip_record = true;
    $feature = reset($data);
    $langs = array_keys($data);
    $main_lang = reset($langs);
    if (Registry::get('runtime.company_id')) {
        $company_id = Registry::get('runtime.company_id');
    } else {
        if (!empty($feature['company'])) {
            $company_id = fn_get_company_id_by_name($feature['company']);
        } else {
            $company_id = isset($feature['company_id']) ? $feature['company_id'] : Registry::get('runtime.company_id');
        }
    }
    if (!empty($feature['feature_id'])) {
        $feature_id = db_get_field('SELECT ?:product_features.feature_id FROM ?:product_features WHERE feature_id = ?i', $feature['feature_id']);
    }
    $parent_id = fn_exim_get_product_feature_group_id($feature['parent_id'], $company_id, $new_groups, $main_lang);
    if (empty($feature_id)) {
        $condition = db_quote("WHERE description = ?s AND lang_code = ?s AND feature_type = ?s", $feature['description'], $main_lang, $feature['feature_type']);
        $condition .= db_quote(" AND parent_id = ?i", $parent_id);
        $feature_id = db_get_field('SELECT ?:product_features.feature_id FROM ?:product_features_descriptions ' . 'LEFT JOIN ?:product_features ON ?:product_features.feature_id = ?:product_features_descriptions.feature_id ' . $condition);
    }
    unset($feature['feature_id']);
    $feature['company_id'] = $company_id;
    $feature['parent_id'] = $parent_id;
    $feature['variants'] = array();
    if (!empty($feature['Variants'])) {
        $variants = str_getcsv($feature['Variants'], ',', "'");
        array_walk($variants, 'fn_trim_helper');
        list($origin_variants) = fn_get_product_feature_variants(array('feature_id' => $feature_id), 0, $main_lang);
        $feature['original_var_ids'] = implode(',', array_keys($origin_variants));
        foreach ($variants as $variant) {
            $feature['variants'][]['variant'] = $variant;
        }
    }
    $skip = false;
    if (empty($feature_id)) {
        $feature_id = fn_update_product_feature($feature, 0, $main_lang);
        $processed_data['N']++;
        fn_set_progress('echo', __('updating') . ' features <b>' . $feature_id . '</b>. ', false);
    } else {
        if (!fn_check_company_id('product_features', 'feature_id', $feature_id)) {
            $processed_data['S']++;
            $skip = true;
        } else {
            // Convert categories from Names to C_IDS: Electronics,Processors -> 3,45
            $_data = $feature;
            $_data['categories_path'] = fn_exim_get_features_convert_category_path($feature, $main_lang, $category_delimiter);
            fn_update_product_feature($_data, $feature_id, $main_lang);
            if (in_array($feature_id, $new_groups)) {
                $processed_data['N']++;
            } else {
                $processed_data['E']++;
                fn_set_progress('echo', __('creating') . ' features <b>' . $feature_id . '</b>. ', false);
            }
        }
    }
    if (!$skip) {
        fn_exim_set_product_feature_categories($feature_id, $feature, $main_lang, $category_delimiter);
        foreach ($data as $lang_code => $feature_data) {
            unset($feature_data['feature_id']);
            db_query('UPDATE ?:product_features_descriptions SET ?u WHERE feature_id = ?i AND lang_code = ?s', $feature_data, $feature_id, $lang_code);
        }
        if (fn_allowed_for('ULTIMATE')) {
            if (!empty($company_id)) {
                fn_exim_update_share_feature($feature_id, $company_id);
            }
        }
    }
    return $feature_id;
}
/**
 * If feature group exists return it id, else create such groups for all available langs
 *
 * @param string $group Group name
 * @param int $company_id Company identifier
 * @param string $lang_code 2-letter language code
 *
 * @return integer ID of group
 */
function fn_exim_check_feature_group($group, $company_id, $lang_code)
{
    $group_id = db_get_field("SELECT feature_id FROM ?:product_features_descriptions WHERE description = ?s AND lang_code = ?s LIMIT 1", $group, $lang_code);
    if (empty($group_id)) {
        $group_data = array('feature_id' => 0, 'description' => $group, 'lang_code' => $lang_code, 'feature_type' => 'G', 'company_id' => $company_id, 'status' => 'A');
        $group_id = fn_update_product_feature($group_data, 0, $lang_code);
    }
    if (fn_allowed_for('ULTIMATE')) {
        fn_exim_update_share_feature($group_id, $company_id);
    }
    return $group_id;
}