/** * Updates/creates staff member an returns created user id * * @param array $user_data - array of user data * @param int $user_id - staff member identifier * @param $lang_code - language code * @return int $user_id */ function fn_staff_update_staff_member_data($user_data, $user_id = 0, $lang_code = CART_LANGUAGE) { SecurityHelper::sanitizeObjectData('staff', $user_data); $is_valid_email = fn_staff_validate_email($user_data['email'], true); if (!$is_valid_email) { return false; } if ($user_data['linked_user_id']) { $linked_user_exists = fn_staff_check_linked_user_exists($user_data['linked_user_id']); if (!$linked_user_exists) { return false; } } $user_data['position'] = isset($user_data['position']) ? (int) $user_data['position'] : 0; // create new staff member if (empty($user_id)) { $user_id = db_query("INSERT INTO ?:staff ?e", $user_data); // update existing staff member } else { $arow = db_query("UPDATE ?:staff SET ?u WHERE user_id = ?i", $user_data, $user_id); if ($arow === false) { fn_set_notification('E', __('error'), __('object_not_found', array('[object]' => __('staff'))), '', '404'); $user_id = false; } } return $user_id; }
/** * Converts array to html hidden fields * * Type: function<br> * Name: array_to_fields<br> * @param array $param params list * @return object $template template object */ function smarty_function_array_to_fields($params, &$template) { $result = ''; $pattern = '<input type="hidden" name="%s" value="%s" />' . "\n"; foreach ($params['data'] as $name => $value) { if (empty($value)) { continue; } if (!empty($params['skip']) && in_array($name, $params['skip'])) { continue; } if (!empty($params['escape']) && in_array($name, $params['escape'])) { if (is_array($value)) { foreach ($value as $index => &$data) { $data = SecurityHelper::escapeHtml($data); } } else { $value = SecurityHelper::escapeHtml($value); } } if (is_array($value)) { foreach ($value as $index => $data) { $result .= sprintf($pattern, $name . '[' . $index . ']', $data); } } else { $result .= sprintf($pattern, $name, $value); } } return $result; }
function fn_buy_together_update_chain($item_id, $product_id, $item_data, $auth, $lang_code = CART_LANGUAGE) { if (empty($product_id) || $product_id == 0) { return false; } SecurityHelper::sanitizeObjectData('buy_together_chain', $item_data); $show_notice = true; $item_data['product_id'] = $product_id; if (!empty($item_data['products'])) { foreach ($item_data['products'] as $key => $product) { // Delete products with empty amount if (empty($product['amount']) || intval($product['amount']) == 0) { unset($item_data['products'][$key]); continue; } $item_data['products'][$key]['modifier'] = floatval($item_data['products'][$key]['modifier']); $is_restricted = false; fn_set_hook('buy_together_restricted_product', $product['product_id'], $auth, $is_restricted, $show_notice); if ($is_restricted) { unset($item_data['products'][$key]); } } $item_data['products'] = serialize($item_data['products']); } else { $item_data['products'] = array(); } if (!empty($item_data['date_from'])) { $item_data['date_from'] = fn_parse_date($item_data['date_from']); } if (!empty($item_data['date_to'])) { $item_data['date_to'] = fn_parse_date($item_data['date_to']); } if (empty($item_id) || $item_id == 0) { //Create a new chain $item_id = db_query("INSERT INTO ?:buy_together ?e", $item_data); if (empty($item_id)) { return false; } $_data = array(); $_data['chain_id'] = $item_id; $_data['name'] = !empty($item_data['name']) ? $item_data['name'] : ''; $_data['description'] = !empty($item_data['description']) ? $item_data['description'] : ''; foreach (fn_get_translation_languages() as $_data['lang_code'] => $v) { db_query("INSERT INTO ?:buy_together_descriptions ?e", $_data); } } else { //Update already existing chain $_data = array(); $_data['chain_id'] = $item_id; $_data['name'] = !empty($item_data['name']) ? $item_data['name'] : ''; $_data['description'] = !empty($item_data['description']) ? $item_data['description'] : ''; db_query("UPDATE ?:buy_together SET ?u WHERE chain_id = ?i", $item_data, $item_id); db_query("UPDATE ?:buy_together_descriptions SET ?u WHERE chain_id = ?i AND lang_code = ?s", $_data, $item_id, $lang_code); } return $item_id; }
/** * Generates session ID key. * * @return string New session ID */ protected function generateSessionID() { return SecurityHelper::generateRandomString(); }
/** * Updates page data by id or create new * * @param array $page_data Page data * @param int $page_id Page idetifier, if equals zero new page will be created * @param string $lang_code 2 letters language code * @return int Page identifier on success, false otherwise */ function fn_update_page($page_data, $page_id = 0, $lang_code = CART_LANGUAGE) { /** * Actions before all checks and initializations * * @param array $page_data Page data * @param int $page_id Page idetifier, if equals zero new page will be created * @param string $lang_code 2 letters language code */ fn_set_hook('update_page_pre', $page_data, $page_id, $lang_code); if (!empty($page_id) && !fn_check_company_id('pages', 'page_id', $page_id)) { fn_company_access_denied_notification(); return false; } if (!empty($page_data)) { SecurityHelper::sanitizeObjectData('page', $page_data); if (!empty($page_data['avail_from_timestamp'])) { $page_data['avail_from_timestamp'] = fn_parse_date($page_data['avail_from_timestamp']); } else { $page_data['avail_from_timestamp'] = 0; } if (!empty($page_data['avail_till_timestamp'])) { $page_data['avail_till_timestamp'] = fn_parse_date($page_data['avail_till_timestamp']) + 86399; } else { $page_data['avail_till_timestamp'] = 0; } if (isset($page_data['usergroup_ids'])) { $page_data['usergroup_ids'] = empty($page_data['usergroup_ids']) ? '0' : implode(',', $page_data['usergroup_ids']); } $page_data['add_items'] = empty($page_data['add_items']) ? array() : $page_data['add_items']; if (isset($page_data['timestamp'])) { $page_data['timestamp'] = fn_parse_date($page_data['timestamp']); } if (isset($page_data['localization'])) { $page_data['localization'] = empty($page_data['localization']) ? '' : fn_implode_localizations($page_data['localization']); } $old_page_data = array(); /** * Actions after all checks and initializations and before update page * * @param array $page_data Page data * @param int $page_id Page idetifier, if equals zero new page will be created * @param string $lang_code 2 letters language code */ fn_set_hook('update_page_before', $page_data, $page_id, $lang_code); $parent_id = isset($page_data['parent_id']) ? $page_data['parent_id'] : null; unset($page_data['parent_id']); if (empty($page_id)) { // page title required if (empty($page_data['page'])) { return false; } // add new page $create = true; $page_data['page_id'] = $page_id = db_query('INSERT INTO ?:pages ?e', $page_data); foreach (fn_get_translation_languages() as $page_data['lang_code'] => $v) { db_query('INSERT INTO ?:page_descriptions ?e', $page_data); } } else { $old_page_data = fn_get_page_data($page_id, $lang_code); $create = false; // page title is not updated if (empty($page_data['page'])) { unset($page_data['page']); } // update existing page db_query('UPDATE ?:pages SET ?u WHERE page_id = ?i', $page_data, $page_id); db_query('UPDATE ?:page_descriptions SET ?u WHERE page_id = ?i AND lang_code = ?s', $page_data, $page_id, $lang_code); } $page_data['parent_id'] = $parent_id; // regenerate id_path for child pages if (isset($page_data['parent_id'])) { fn_change_page_parent($page_id, intval($page_data['parent_id'])); } } /** * Actions after page update * * @param array $page_data Page data * @param int $page_id Page idetifier, if equals zero new page will be created * @param string $lang_code 2 letters language code * @param bool $create True if page was created, falce otherwise * @param array $old_page_data Page data before update */ fn_set_hook('update_page_post', $page_data, $page_id, $lang_code, $create, $old_page_data); return $page_id; }
/** * Add/remove html special chars * * @deprecated In favour of use Tygh\Tools\SecurityHelper::encodeHtml() * @since 4.3.1 * * @param mixed $data data to filter * @param bool $revert if true, decode special chars * @return mixed filtered variable */ function fn_html_escape($data, $revert = false) { return SecurityHelper::escapeHtml($data, $revert); }
/** * Generates session id * * @return string new session ID */ private static function _generateId() { return SecurityHelper::generateRandomString(); }
/** * Creates/Updates currency * * @param array $currency_data Currency information * @param int $currency_id Currency id * @param string $lang_code 2-letter language code * @return int Currency id */ function fn_update_currency($currency_data, $currency_id, $lang_code = DESCR_SL) { /** * Updates currency data before updating * * @param array $currency_data Currency information * @param int $currency_id Currency id * @param string $lang_code 2-letter language code */ fn_set_hook('update_currency_pre', $currency_data, $currency_id, $lang_code); $currency_data['currency_code'] = strtoupper($currency_data['currency_code']); $currency_data['coefficient'] = !empty($currency_data['is_primary']) || !isset($currency_data['coefficient']) ? 1 : $currency_data['coefficient']; $currency_data['symbol'] = empty($currency_data['symbol']) ? '' : SecurityHelper::sanitizeHtml($currency_data['symbol']); if (empty($currency_data['coefficient']) || floatval($currency_data['coefficient']) <= 0) { fn_set_notification('W', __('warning'), __('currency_rate_greater_than_null')); return false; } $is_exists = db_get_field("SELECT COUNT(*) FROM ?:currencies WHERE currency_code = ?s AND currency_id <> ?i", $currency_data['currency_code'], $currency_id); if (!empty($is_exists)) { fn_set_notification('E', __('error'), __('error_currency_exists', array('[code]' => $currency_data['currency_code']))); return false; } if (isset($currency_data['decimals']) && $currency_data['decimals'] > 2) { fn_set_notification('W', __('warning'), __('notice_too_many_decimals', array('[DECIMALS]' => $currency_data['decimals'], '[CURRENCY]' => $currency_data['currency_code']))); } if (!empty($currency_data['is_primary'])) { db_query("UPDATE ?:currencies SET is_primary = 'N' WHERE is_primary = 'Y'"); } if (empty($currency_id)) { $currency_id = db_query("INSERT INTO ?:currencies ?e", $currency_data); fn_create_description('currency_descriptions', 'currency_code', $currency_data['currency_code'], $currency_data); } else { $old_currency_code = db_get_field("SELECT currency_code FROM ?:currencies WHERE currency_id = ?i", $currency_id); db_query("UPDATE ?:currencies SET ?u WHERE currency_id = ?i", $currency_data, $currency_id); db_query('UPDATE ?:currency_descriptions SET ?u WHERE currency_code = ?s AND lang_code = ?s', $currency_data, $old_currency_code, $lang_code); } /** * Changes result of currency saving * * @param array $currency_data Currency information * @param int $currency_id Currency id * @param string $lang_code 2-letter language code * @param int Currency id */ fn_set_hook('update_currency_post', $currency_data, $currency_id, $lang_code, $currency_id); return $currency_id; }
function fn_update_company($company_data, $company_id = 0, $lang_code = CART_LANGUAGE) { $can_update = true; /** * Update company data (running before fn_update_company() function) * * @param array $company_data Company data * @param int $company_id Company identifier * @param string $lang_code Two-letter language code (e.g. 'en', 'ru', etc.) * @param boolean $can_update Flag, allows addon to forbid to create/update company */ fn_set_hook('update_company_pre', $company_data, $company_id, $lang_code, $can_update); if ($can_update == false) { return false; } SecurityHelper::sanitizeObjectData('company', $company_data); if (fn_allowed_for('MULTIVENDOR') && Registry::get('runtime.company_id')) { unset($company_data['comission'], $company_data['comission_type'], $company_data['categories'], $company_data['shippings']); } elseif (fn_allowed_for('ULTIMATE') && Registry::get('runtime.company_id')) { unset($company_data['storefront'], $company_data['secure_storefront']); } if (fn_allowed_for('ULTIMATE') && !Registry::get('runtime.company_id')) { if (isset($company_data['storefront'])) { if (empty($company_data['storefront'])) { fn_set_notification('E', __('error'), __('storefront_url_not_defined')); return false; } else { if (empty($company_data['secure_storefront'])) { $company_data['secure_storefront'] = $company_data['storefront']; } $company_data['storefront'] = Url::clean($company_data['storefront']); $company_data['secure_storefront'] = Url::clean($company_data['secure_storefront']); } } } unset($company_data['company_id']); $_data = $company_data; if (fn_allowed_for('MULTIVENDOR')) { // Check if company with same email already exists $is_exist = db_get_field("SELECT email FROM ?:companies WHERE company_id != ?i AND email = ?s", $company_id, $_data['email']); if (!empty($is_exist)) { $_text = 'error_vendor_exists'; fn_set_notification('E', __('error'), __($_text)); return false; } } if (fn_allowed_for('ULTIMATE') && !empty($company_data['storefront'])) { // Check if company with the same Storefront URL already exists $http_exist = db_get_row('SELECT company_id, storefront FROM ?:companies WHERE storefront = ?s', $company_data['storefront']); $https_exist = db_get_row('SELECT company_id, secure_storefront FROM ?:companies WHERE secure_storefront = ?s', $company_data['secure_storefront']); if (!empty($http_exist) || !empty($https_exist)) { if (empty($company_id)) { if (!empty($http_exist)) { fn_set_notification('E', __('error'), __('storefront_url_already_exists')); } else { fn_set_notification('E', __('error'), __('secure_storefront_url_already_exists')); } return false; } elseif (!empty($http_exist) && $company_id != $http_exist['company_id'] || !empty($https_exist) && $company_id != $https_exist['company_id']) { if (!empty($http_exist) && $company_id != $http_exist['company_id']) { fn_set_notification('E', __('error'), __('storefront_url_already_exists')); unset($_data['storefront']); } else { fn_set_notification('E', __('error'), __('secure_storefront_url_already_exists')); unset($_data['secure_storefront']); } return false; } } } if (isset($company_data['shippings'])) { $_data['shippings'] = empty($company_data['shippings']) ? '' : fn_create_set($company_data['shippings']); } if (!empty($_data['countries_list'])) { $_data['countries_list'] = implode(',', $_data['countries_list']); } else { $_data['countries_list'] = ''; } // add new company if (empty($company_id)) { // company title can't be empty if (empty($company_data['company'])) { fn_set_notification('E', __('error'), __('error_empty_company_name')); return false; } $_data['timestamp'] = TIME; $company_id = db_query("INSERT INTO ?:companies ?e", $_data); if (empty($company_id)) { return false; } $_data['company_id'] = $company_id; foreach (fn_get_translation_languages() as $_data['lang_code'] => $_v) { db_query("INSERT INTO ?:company_descriptions ?e", $_data); } $action = 'add'; // update company information } else { if (isset($company_data['company']) && empty($company_data['company'])) { unset($company_data['company']); } if (!empty($_data['status'])) { $status_from = db_get_field("SELECT status FROM ?:companies WHERE company_id = ?i", $company_id); } db_query("UPDATE ?:companies SET ?u WHERE company_id = ?i", $_data, $company_id); if (isset($status_from) && $status_from != $_data['status']) { fn_companies_change_status($company_id, $_data['status'], '', $status_from, true); } // unset data lang code as it determines company main language not description language unset($_data['lang_code']); db_query("UPDATE ?:company_descriptions SET ?u WHERE company_id = ?i AND lang_code = ?s", $_data, $company_id, $lang_code); $action = 'update'; } /** * Update company data (running after fn_update_company() function) * * @param array $company_data Company data * @param int $company_id Company integer identifier * @param string $lang_code Two-letter language code (e.g. 'en', 'ru', etc.) * @param string $action Flag determines if company was created (add) or just updated (update). */ fn_set_hook('update_company', $company_data, $company_id, $lang_code, $action); $logo_ids = array(); if ($action == 'add') { $theme_name = !empty($company_data['theme_name']) ? $company_data['theme_name'] : Registry::get('config.base_theme'); if (fn_allowed_for('ULTIMATE')) { $clone_from = !empty($company_data['clone_from']) && $company_data['clone_from'] != 'all' ? $company_data['clone_from'] : null; if (!is_null($clone_from)) { $theme_name = fn_get_theme_path('[theme]', 'C', $clone_from); } } if (fn_allowed_for('ULTIMATE')) { $logo_ids = fn_install_theme($theme_name, $company_id, false); } else { $logo_ids = fn_create_theme_logos_by_layout_id($theme_name, 0, $company_id, true); } } fn_attach_image_pairs('logotypes', 'logos', 0, $lang_code, $logo_ids); return $company_id; }
function fn_form_builder_update_page_post(&$page_data, &$page_id, &$lang_code) { // page form processing if (!empty($page_data['form'])) { $elements_data = empty($page_data['form']['elements_data']) ? array() : $page_data['form']['elements_data']; $general_data = empty($page_data['form']['general']) ? array() : $page_data['form']['general']; $elm_ids = array(); if (!empty($elements_data)) { // process elements foreach ($elements_data as $data) { if (empty($data['description']) && $data['element_type'] != FORM_SEPARATOR) { continue; } if (!empty($data['element_type']) && strpos(FORM_HEADER . FORM_SEPARATOR, $data['element_type']) !== false) { $data['required'] = 'N'; } $data['page_id'] = $page_id; if (!empty($data['element_id'])) { $data['object_id'] = $element_id = $data['element_id']; db_query('UPDATE ?:form_options SET ?u WHERE element_id = ?i', $data, $element_id); db_query('UPDATE ?:form_descriptions SET ?u WHERE object_id = ?i AND lang_code = ?s', $data, $element_id, $lang_code); } else { $data['object_id'] = $element_id = db_query('INSERT INTO ?:form_options ?e', $data); foreach (fn_get_translation_languages() as $data['lang_code'] => $_v) { db_query('INSERT INTO ?:form_descriptions ?e', $data); } } $elm_ids[] = $element_id; // process variants if (!empty($data['variants'])) { foreach ($data['variants'] as $k => $v) { if (empty($v['description'])) { continue; } $v['parent_id'] = $element_id; $v['element_type'] = FORM_VARIANT; // variant $v['page_id'] = $page_id; if (!empty($v['element_id'])) { $v['object_id'] = $v['element_id']; db_query('UPDATE ?:form_options SET ?u WHERE element_id = ?i', $v, $v['element_id']); db_query('UPDATE ?:form_descriptions SET ?u WHERE object_id = ?i AND lang_code = ?s', $v, $v['element_id'], $lang_code); } else { $v['object_id'] = $v['element_id'] = db_query('INSERT INTO ?:form_options ?e', $v); foreach (fn_get_translation_languages() as $v['lang_code'] => $_v) { db_query('INSERT INTO ?:form_descriptions ?e', $v); } } $elm_ids[] = $v['element_id']; } } } } // update or insert general form data if (!empty($general_data)) { SecurityHelper::sanitizeObjectData('form_general_data', $general_data); //$gdata = fn_trusted_vars('general_data', true); foreach ($general_data as $type => $data) { $elm_id = db_get_field("SELECT element_id FROM ?:form_options WHERE page_id = ?i AND element_type = ?s", $page_id, $type); $_description = array(); $_data = array('element_type' => $type, 'page_id' => $page_id, 'status' => 'A'); if ($type == FORM_RECIPIENT || $type == FORM_IS_SECURE) { $_data['value'] = $data; } $_description = array('description' => $data); if (empty($elm_id)) { $_description['object_id'] = $elm_id = db_query('INSERT INTO ?:form_options ?e', $_data); foreach (fn_get_translation_languages() as $_description['lang_code'] => $_v) { db_query('INSERT INTO ?:form_descriptions ?e', $_description); } } else { db_query('UPDATE ?:form_options SET ?u WHERE element_id = ?i', $_data, $elm_id); db_query('UPDATE ?:form_descriptions SET ?u WHERE object_id = ?i AND lang_code = ?s', $_description, $elm_id, $lang_code); } $elm_ids[] = $elm_id; } } // Delete obsolete elements $obsolete_ids = db_get_fields("SELECT element_id FROM ?:form_options WHERE page_id = ?i AND element_id NOT IN (?n)", $page_id, $elm_ids); if (!empty($obsolete_ids)) { db_query("DELETE FROM ?:form_options WHERE parent_id IN (?n)", $obsolete_ids); db_query("DELETE FROM ?:form_options WHERE element_id IN (?n)", $obsolete_ids); db_query("DELETE FROM ?:form_descriptions WHERE object_id IN (?n)", $obsolete_ids); } } }
protected function arrayToXml($item, $data, $level = 0, $attr = array()) { $tab = str_repeat(' ', $level); if (!is_array($data)) { $attr_str = ''; if (!empty($attr)) { foreach ($attr as $attr_key => $attr_value) { $attr_str .= ' ' . $attr_key . '="' . $attr_value . '"'; } } $txt = SecurityHelper::escapeHtml($data); $txt = str_replace(array('&laquo;', '&raquo;', '&#39;', '&quot;'), "'", $txt); return $tab . '<' . $item . $attr_str . '>' . str_replace(array('&amp;', '&hellip;', '&nbsp;', '&mdash;', '&reg;', '&ndash;', '&trade;'), '', $txt) . '</' . $item . ">\n"; } $xml = ''; foreach ($data as $item_type => $value) { if (is_numeric($item_type)) { if (is_array($value)) { $attr = !empty($value['attr']) ? $value['attr'] : array(); $attr_str = ''; if (!empty($attr)) { foreach ($attr as $attr_key => $attr_value) { $attr_str .= ' ' . $attr_key . '="' . $attr_value . '"'; } } if (isset($value['value'])) { $xml .= $tab . '<' . $item . $attr_str . '>' . SecurityHelper::escapeHtml($value['value']) . '</' . $item . ">\n"; } elseif (isset($value['items'])) { $xml .= $tab . '<' . $item . ">\n" . $this->arrayToXml($item_type, $value, $level + 1) . $tab . '</' . $item . ">\n"; } else { $xml .= $tab . '<' . $item . $attr_str . "/>\n"; } } else { $xml .= $this->arrayToXml($item, $value, $level); } } else { $xml .= $tab . '<' . $item . ">\n" . $this->arrayToXml($item_type, $value, $level + 1) . $tab . '</' . $item . ">\n"; } } return $xml; }
/** * Generates ekey for access to object * @param mixed $object_id object ID * @param string $type object type * @param int $ttl ekey TTL (from the current time) * @param string $ekey ekey ID if you generated it yourselves * @return string ekey ID */ function fn_generate_ekey($object_id, $type, $ttl = 0, $ekey = '') { $key = !empty($ekey) ? $ekey : SecurityHelper::generateRandomString(); if (is_numeric($object_id)) { $field_name = 'object_id'; } else { $field_name = 'object_string'; } $_data = array('object_type' => $type, 'ekey' => $key, 'ttl' => time() + $ttl); $_data[$field_name] = $object_id; db_query("REPLACE INTO ?:ekeys ?e", $_data); return $key; }
/** * Updates product option * * @param array $option_data option data array * @param int $option_id option ID (empty if we're adding the option) * @param string $lang_code language code to add/update option for * @return int ID of the added/updated option */ function fn_update_product_option($option_data, $option_id = 0, $lang_code = DESCR_SL) { /** * Changes parameters before update option data * * @param array $option_data Option data * @param int $option_id Option identifier * @param string $lang_code Two-letter language code (e.g. 'en', 'ru', etc.) */ fn_set_hook('update_product_option_pre', $option_data, $option_id, $lang_code); SecurityHelper::sanitizeObjectData('product_option', $option_data); // Add option if (empty($option_id)) { $action = 'create'; if (empty($option_data['product_id'])) { $option_data['product_id'] = 0; } $option_data['option_id'] = $option_id = db_query('INSERT INTO ?:product_options ?e', $option_data); foreach (fn_get_translation_languages() as $option_data['lang_code'] => $_v) { db_query("INSERT INTO ?:product_options_descriptions ?e", $option_data); } $create = true; // Update option } else { $action = 'update'; // if option inventory changed from Y to N, we should clear option combinations if (!empty($option_data['product_id']) && !empty($option_data['inventory']) && $option_data['inventory'] == 'N') { $condition = fn_get_company_condition('?:product_options.company_id'); $old_option_inventory = db_get_field("SELECT inventory FROM ?:product_options WHERE option_id = ?i {$condition}", $option_id); if ($old_option_inventory == 'Y') { $inventory_filled = db_get_field('SELECT COUNT(*) FROM ?:product_options_inventory WHERE product_id = ?i', $option_data['product_id']); if ($inventory_filled) { fn_delete_product_option_combinations($option_data['product_id']); } } } if (fn_allowed_for('ULTIMATE') && !empty($option_data['product_id']) && fn_ult_is_shared_product($option_data['product_id']) == 'Y') { $product_company_id = db_get_field('SELECT company_id FROM ?:products WHERE product_id = ?i', $option_data['product_id']); $option_id = fn_ult_update_shared_product_option($option_data, $option_id, Registry::ifGet('runtime.company_id', $product_company_id), $lang_code); if (Registry::get('runtime.company_id') && Registry::get('runtime.company_id') != $product_company_id) { $deleted_variants = array(); fn_set_hook('update_product_option_post', $option_data, $option_id, $deleted_variants, $lang_code); return $option_id; } } db_query("UPDATE ?:product_options SET ?u WHERE option_id = ?i", $option_data, $option_id); db_query("UPDATE ?:product_options_descriptions SET ?u WHERE option_id = ?i AND lang_code = ?s", $option_data, $option_id, $lang_code); } if (fn_allowed_for('ULTIMATE')) { // options of shared product under the shared store hasn't a company_id. No necessary for updating. if (!empty($option_data['company_id'])) { fn_ult_update_share_object($option_id, 'product_options', $option_data['company_id']); } if (!empty($option_data['product_id'])) { fn_ult_share_product_option($option_id, $option_data['product_id']); } } if (!empty($option_data['variants'])) { $var_ids = array(); // Generate special variants structure for checkbox (2 variants, 1 hidden) if ($option_data['option_type'] == 'C') { $option_data['variants'] = array_slice($option_data['variants'], 0, 1); // only 1 variant should be here reset($option_data['variants']); $_k = key($option_data['variants']); $option_data['variants'][$_k]['position'] = 1; // checked variant $v_id = db_get_field("SELECT variant_id FROM ?:product_option_variants WHERE option_id = ?i AND position = 0", $option_id); $option_data['variants'][] = array('position' => 0, 'variant_id' => $v_id); } $variant_images = array(); foreach ($option_data['variants'] as $k => $v) { if ((!isset($v['variant_name']) || $v['variant_name'] == '') && $option_data['option_type'] != 'C') { continue; } if ($action == 'create') { unset($v['variant_id']); } // Update product options variants if (isset($v['modifier'])) { $v['modifier'] = floatval($v['modifier']); if (floatval($v['modifier']) > 0) { $v['modifier'] = '+' . $v['modifier']; } } if (isset($v['weight_modifier'])) { $v['weight_modifier'] = floatval($v['weight_modifier']); if (floatval($v['weight_modifier']) > 0) { $v['weight_modifier'] = '+' . $v['weight_modifier']; } } $v['option_id'] = $option_id; if (empty($v['variant_id']) || !empty($v['variant_id']) && !db_get_field("SELECT variant_id FROM ?:product_option_variants WHERE variant_id = ?i", $v['variant_id'])) { $v['variant_id'] = db_query("INSERT INTO ?:product_option_variants ?e", $v); foreach (fn_get_translation_languages() as $v['lang_code'] => $_v) { db_query("INSERT INTO ?:product_option_variants_descriptions ?e", $v); } } else { db_query("UPDATE ?:product_option_variants SET ?u WHERE variant_id = ?i", $v, $v['variant_id']); db_query("UPDATE ?:product_option_variants_descriptions SET ?u WHERE variant_id = ?i AND lang_code = ?s", $v, $v['variant_id'], $lang_code); } $var_ids[] = $v['variant_id']; if ($option_data['option_type'] == 'C') { fn_delete_image_pairs($v['variant_id'], 'variant_image'); // force deletion of variant image for "checkbox" option } else { $variant_images[$k] = $v['variant_id']; } } if ($option_data['option_type'] != 'C' && !empty($variant_images)) { fn_attach_image_pairs('variant_image', 'variant_image', 0, $lang_code, $variant_images); } // Delete obsolete variants $condition = !empty($var_ids) ? db_quote('AND variant_id NOT IN (?n)', $var_ids) : ''; $deleted_variants = db_get_fields("SELECT variant_id FROM ?:product_option_variants WHERE option_id = ?i {$condition}", $option_id, $var_ids); if (!empty($deleted_variants)) { db_query("DELETE FROM ?:product_option_variants WHERE variant_id IN (?n)", $deleted_variants); db_query("DELETE FROM ?:product_option_variants_descriptions WHERE variant_id IN (?n)", $deleted_variants); foreach ($deleted_variants as $v_id) { fn_delete_image_pairs($v_id, 'variant_image'); } } } if (!fn_allowed_for('ULTIMATE:FREE')) { // Rebuild exceptions if (!empty($create) && !empty($option_data['product_id'])) { fn_recalculate_exceptions($option_data['product_id']); } } /** * Update product option (running after fn_update_product_option() function) * * @param array $option_data Array with option data * @param int $option_id Option identifier * @param array $deleted_variants Array with deleted variants ids * @param string $lang_code Language code to add/update option for */ fn_set_hook('update_product_option_post', $option_data, $option_id, $deleted_variants, $lang_code); return $option_id; }