function fn_get_feature_selected_value($feature) { $value = null; if (strpos(ProductFeatures::getSelectable(), $feature['feature_type']) !== false) { if ($feature['feature_type'] == ProductFeatures::MULTIPLE_CHECKBOX) { foreach ($feature['variants'] as $v) { if ($v['selected']) { $value[] = $v['variant_id']; } } } else { $value = $feature['variant_id']; } } elseif (strpos(ProductFeatures::NUMBER_FIELD . ProductFeatures::DATE, $feature['feature_type']) !== false) { $value = $feature['value_int']; } else { $value = $feature['value']; } return $value; }
/** * Updates product feature * * @param array $feature_data Feature data * @param int $feature_id Feature identifier * @param string $lang_code 2-letters language code * @return int/boolean Feature identifier if product feature was updated, false otherwise */ function fn_update_product_feature($feature_data, $feature_id, $lang_code = DESCR_SL) { /** * Changes before product feature updating * * @param array $feature_data Feature data * @param int $feature_id Feature identifier * @param string $lang_code 2-letters language code */ fn_set_hook('update_product_feature_pre', $feature_data, $feature_id, $lang_code); if (fn_allowed_for('ULTIMATE') && Registry::get('runtime.company_id')) { if (!empty($feature_id) && $feature_id != NEW_FEATURE_GROUP_ID) { if (!fn_check_company_id('product_features', 'feature_id', $feature_id)) { fn_company_access_denied_notification(); return false; } unset($feature_data['company_id']); } } $deleted_variants = array(); $old_feature_data = array(); // If this feature belongs to the group, get categories assignment from this group if (!empty($feature_data['parent_id'])) { $gdata = db_get_row("SELECT categories_path, display_on_product, display_on_catalog, display_on_header FROM ?:product_features WHERE feature_id = ?i", $feature_data['parent_id']); if (!empty($gdata)) { $gdata = fn_filter_feature_group_data($gdata); $feature_data = fn_array_merge($feature_data, $gdata); } } if (!intval($feature_id)) { // check for intval as we use "0G" for new group $feature_data['feature_id'] = $feature_id = db_query("INSERT INTO ?:product_features ?e", $feature_data); foreach (fn_get_translation_languages() as $feature_data['lang_code'] => $_d) { db_query("INSERT INTO ?:product_features_descriptions ?e", $feature_data); } } else { $old_feature_data = fn_get_product_feature_data($feature_id, false, false, DESCR_SL); if (!isset($feature_data['categories_path']) && empty($old_feature_data['categories_path'])) { $feature_data['categories_path'] = ''; } $arow = db_query("UPDATE ?:product_features SET ?u WHERE feature_id = ?i", $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 (!$old_feature_data) { fn_set_notification('E', __('error'), __('object_not_found', array('[object]' => __('feature'))), '', '404'); $feature_id = false; } } if ($feature_id) { // If this feature is group, set its categories to all children if ($feature_data['feature_type'] == ProductFeatures::GROUP) { $u = array('categories_path' => !empty($feature_data['categories_path']) ? $feature_data['categories_path'] : '', 'display_on_product' => !empty($feature_data['display_on_product']) ? $feature_data['display_on_product'] : '', 'display_on_catalog' => !empty($feature_data['display_on_catalog']) ? $feature_data['display_on_catalog'] : '', 'display_on_header' => !empty($feature_data['display_on_header']) ? $feature_data['display_on_header'] : ''); $u = fn_filter_feature_group_data($u); db_query("UPDATE ?:product_features SET ?u WHERE parent_id = ?i", $u, $feature_id); } // Delete variants for simple features $old_categories = $old_feature_data ? fn_explode(',', $old_feature_data['categories_path']) : array(); // Get sub-categories for OLD categories if (!empty($old_categories)) { $_condition = array(); foreach ($old_categories as $category_id) { $_condition[] = db_quote('id_path LIKE ?l OR id_path LIKE ?l', $category_id . '/%', '%/' . $category_id . '/%'); } $sub_cat_ids = db_get_fields('SELECT category_id FROM ?:categories WHERE ' . implode(' OR ', $_condition)); $old_categories = array_merge($old_categories, $sub_cat_ids); } $new_categories = isset($feature_data['categories_path']) ? fn_explode(',', $feature_data['categories_path']) : array(); // Get sub-categories for NEW categories if (!empty($new_categories)) { $_condition = array(); foreach ($new_categories as $category_id) { $_condition[] = db_quote('id_path LIKE ?l OR id_path LIKE ?l', $category_id . '/%', '%/' . $category_id . '/%'); } $sub_cat_ids = db_get_fields('SELECT category_id FROM ?:categories WHERE ' . implode(' OR ', $_condition)); $new_categories = array_merge($new_categories, $sub_cat_ids); } $checked_types = ProductFeatures::getSelectable(); if ($old_feature_data && $feature_data['feature_type'] != $old_feature_data['feature_type']) { if (strpos($checked_types, $feature_data['feature_type']) === false) { fn_delete_product_feature_variants($feature_id); } } // Remove features values/variants if we changed categories list sort($old_categories); sort($new_categories); if (!fn_is_empty($new_categories) && (fn_is_empty($old_categories) || $old_categories != $new_categories)) { db_query('DELETE FROM ?:product_features_values WHERE feature_id = ?i AND product_id NOT IN (SELECT product_id FROM ?:products_categories WHERE link_type = ?s AND category_id IN (?a))', $feature_id, 'M', $new_categories); } if (strpos($checked_types, $feature_data['feature_type']) !== false) { fn_update_product_feature_variants($feature_id, $feature_data, $lang_code); } /** * Adds additional actions after product feature updating * * @param array $feature_data Feature data * @param int $feature_id Feature identifier * @param array $deleted_variants Deleted product feature variants identifiers * @param string $lang_code 2-letters language code */ fn_set_hook('update_product_feature_post', $feature_data, $feature_id, $deleted_variants, $lang_code); } return $feature_id; }
function fn_exim_product_feature_variants($feature, $feature_id, $variants, $lang_code) { $feature_type = $feature['type']; if (strpos(ProductFeatures::getSelectable(), $feature_type) !== false) { // variant IDs $vars = array(); foreach ($feature['variants'] as $variant) { $vars[] = $variant['name']; } $existent_variants = db_get_hash_single_array('SELECT pfvd.variant_id, variant FROM ?:product_feature_variant_descriptions AS pfvd ' . 'LEFT JOIN ?:product_feature_variants AS pfv ON pfv.variant_id = pfvd.variant_id ' . 'WHERE feature_id = ?i AND variant IN (?a) AND lang_code = ?s', array('variant_id', 'variant'), $feature_id, $vars, $lang_code); foreach ($feature['variants'] as $variant_data) { if (!in_array($variant_data['name'], $existent_variants)) { $variant_id = fn_add_feature_variant($feature_id, array('variant' => $variant_data['name'])); $existent_variants[$variant_id] = $variant_data['name']; } } if ($feature_type == ProductFeatures::MULTIPLE_CHECKBOX) { foreach ($feature['variants'] as $variant_data) { if (in_array($variant_data['name'], $existent_variants)) { $variant_id = array_search($variant_data['name'], $existent_variants); $variants[$feature_id][$variant_id] = $variant_id; } } } else { $variant_data = reset($feature['variants']); if (in_array($variant_data['name'], $existent_variants)) { $variant_id = array_search($variant_data['name'], $existent_variants); $variants[$feature_id] = $variant_id; } } } else { $variant_data = reset($feature['variants']); $variants[$feature_id] = $variant_data['name']; } return $variants; }
function fn_exim_product_feature_variants($feature, $feature_id, $variants, $lang_code) { $feature_type = $feature['type']; if (strpos(ProductFeatures::getSelectable(), $feature_type) !== false) { // variant IDs $vars = array(); foreach ($feature['variants'] as $variant) { $vars[] = $variant['name']; } $existent_variants = db_get_hash_single_array('SELECT pfvd.variant_id, variant FROM ?:product_feature_variant_descriptions AS pfvd ' . 'LEFT JOIN ?:product_feature_variants AS pfv ON pfv.variant_id = pfvd.variant_id ' . 'WHERE feature_id = ?i AND variant IN (?a) AND lang_code = ?s', array('variant_id', 'variant'), $feature_id, $vars, $lang_code); foreach ($feature['variants'] as $variant_data) { if (!in_array($variant_data['name'], $existent_variants)) { if (fn_allowed_for('MULTIVENDOR') && Registry::get('runtime.company_id')) { fn_set_notification('W', __('warning'), __('exim_vendor_cant_create_feature')); continue; } $variant_id = fn_add_feature_variant($feature_id, array('variant' => $variant_data['name'])); $existent_variants[$variant_id] = $variant_data['name']; } } if ($feature_type == ProductFeatures::MULTIPLE_CHECKBOX) { foreach ($feature['variants'] as $variant_data) { if (in_array($variant_data['name'], $existent_variants)) { $variant_id = array_search($variant_data['name'], $existent_variants); $variants[$feature_id][$variant_id] = $variant_id; } } } else { $variant_data = reset($feature['variants']); if (in_array($variant_data['name'], $existent_variants)) { $variant_id = array_search($variant_data['name'], $existent_variants); $variants[$feature_id] = $variant_id; } } } else { $variant_data = reset($feature['variants']); $variants[$feature_id] = $variant_data['name']; } return $variants; }