/** * Creates SEO name * @param integer $object_id object ID * @param string $object_type object type * @param string $object_name object name * @param integer $index index * @param string $dispatch dispatch (for static object type) * @param integer $company_id company ID * @param string $lang_code language code * @param boolean $create_redirect creates 301 redirect if set to true * @param string $area current working area * @return string SEO name */ function fn_create_seo_name($object_id, $object_type, $object_name, $index = 0, $dispatch = '', $company_id = '', $lang_code = CART_LANGUAGE, $create_redirect = false, $area = AREA) { /** * Create SEO name (running before fn_create_seo_name() function) * * @param int $object_id * @param string $object_type * @param string $object_name * @param int $index * @param string $dispatch * @param int $company_id * @param string $lang_code Two-letter language code (e.g. 'en', 'ru', etc.) */ fn_set_hook('create_seo_name_pre', $object_id, $object_type, $object_name, $index, $dispatch, $company_id, $lang_code); $non_latin_symbols = Registry::get('addons.seo.non_latin_symbols'); if ($area != 'C' && fn_allowed_for('ULTIMATE,ULTIMATE:FREE') && !empty($company_id)) { $non_latin_symbols = Settings::instance()->getValue('non_latin_symbols', 'seo', $company_id); } $_object_name = fn_generate_name($object_name, '', 0, $non_latin_symbols == 'Y'); $seo_var = fn_get_seo_vars($object_type); if (empty($_object_name)) { $_object_name = $seo_var['description'] . '-' . (empty($object_id) ? $dispatch : $object_id); } $condition = fn_get_seo_company_condition('?:seo_names.company_id', $object_type); $path_condition = ''; if (fn_check_seo_schema_option($seo_var, 'tree_options')) { $path_condition = db_quote(" AND path = ?s", fn_get_seo_parent_path($object_id, $object_type)); } $exist = db_get_field("SELECT name FROM ?:seo_names WHERE name = ?s ?p AND (object_id != ?i OR type != ?s OR dispatch != ?s OR lang_code != ?s) ?p", $_object_name, $path_condition, $object_id, $object_type, $dispatch, $lang_code, $condition); if (!$exist) { $_data = array('name' => $_object_name, 'type' => $object_type, 'object_id' => $object_id, 'dispatch' => $dispatch, 'lang_code' => $lang_code, 'path' => fn_get_seo_parent_path($object_id, $object_type)); if (fn_allowed_for('ULTIMATE')) { if (fn_get_seo_vars($object_type, 'not_shared')) { if (!empty($company_id)) { $_data['company_id'] = $company_id; } elseif (Registry::get('runtime.company_id')) { $_data['company_id'] = Registry::get('runtime.company_id'); } // Do not create SEO names for root if (empty($_data['company_id'])) { return ''; } } } if ($create_redirect) { $url = fn_generate_seo_url_from_schema(array('type' => $object_type, 'object_id' => $object_id, 'lang_code' => $lang_code), false); } $affected_rows = db_query("INSERT INTO ?:seo_names ?e ON DUPLICATE KEY UPDATE ?u", $_data, $_data); if ($affected_rows && $create_redirect) { fn_seo_update_redirect(array('src' => $url, 'type' => $object_type, 'object_id' => $object_id, 'company_id' => $company_id, 'lang_code' => $lang_code), 0, false); } } else { $index++; if ($index == 1) { $object_name = $object_name . SEO_DELIMITER . $lang_code; } else { $object_name = preg_replace("/-\\d+\$/", "", $object_name) . SEO_DELIMITER . $index; } $_object_name = fn_create_seo_name($object_id, $object_type, $object_name, $index, $dispatch, $company_id, $lang_code, $create_redirect); } /** * Create SEO name (running after fn_create_seo_name() function) * * @param int $_object_name * @param int $object_id * @param string $object_type * @param string $object_name * @param int $index * @param string $dispatch * @param int $company_id * @param string $lang_code Two-letter language code (e.g. 'en', 'ru', etc.) */ fn_set_hook('create_seo_name_post', $_object_name, $object_id, $object_type, $object_name, $index, $dispatch, $company_id, $lang_code); return $_object_name; }
/** * Creates SEO name * * @param integer $object_id object ID * @param string $object_type object type * @param string $object_name object name * @param integer $index index * @param string $dispatch dispatch (for static object type) * @param integer $company_id company ID * @param string $lang_code language code * @param boolean $create_redirect creates 301 redirect if set to true * @param string $area current working area * @param array $params Additional params * * @return string SEO name */ function fn_create_seo_name($object_id, $object_type, $object_name, $index = 0, $dispatch = '', $company_id = '', $lang_code = CART_LANGUAGE, $create_redirect = false, $area = AREA, $params = array()) { $cache_max_length = 200; static $names_cache = null; // Whether to cache fn_get_seo_parent_path() calls. $use_generated_paths_cache = isset($params['use_generated_paths_cache']) ? $params['use_generated_paths_cache'] : true; /** * Create SEO name (running before fn_create_seo_name() function) * * @param int $object_id * @param string $object_type * @param string $object_name * @param int $index * @param string $dispatch * @param int $company_id * @param string $lang_code Two-letter language code (e.g. 'en', 'ru', etc.) * @param array $params Additional params passed to fn_create_seo_name() function */ fn_set_hook('create_seo_name_pre', $object_id, $object_type, $object_name, $index, $dispatch, $company_id, $lang_code, $params); $seo_settings = fn_get_seo_settings($company_id); $non_latin_symbols = $seo_settings['non_latin_symbols']; $_object_name = fn_generate_name($object_name, '', 0, $non_latin_symbols == 'Y'); $seo_var = fn_get_seo_vars($object_type); if (empty($_object_name)) { $_object_name = $seo_var['description'] . '-' . (empty($object_id) ? $dispatch : $object_id); } $condition = fn_get_seo_company_condition('?:seo_names.company_id', $object_type, $company_id); $path_condition = ''; if (fn_check_seo_schema_option($seo_var, 'tree_options')) { $path_condition = db_quote(" AND path = ?s", fn_get_seo_parent_path($object_id, $object_type, $company_id, $use_generated_paths_cache)); } if (is_null($names_cache)) { $total = db_get_field("SELECT COUNT(*) FROM ?:seo_names WHERE 1 ?p", $condition); if ($total < $cache_max_length) { $names_cache = db_get_hash_single_array("SELECT name, 1 as val FROM ?:seo_names WHERE 1 ?p", array('name', 'val'), $condition); } else { $names_cache = array(); } } $exist = false; if (empty($names_cache) || !empty($names_cache[$_object_name])) { $exist = db_get_field("SELECT name FROM ?:seo_names WHERE name = ?s ?p AND (object_id != ?i OR type != ?s OR dispatch != ?s OR lang_code != ?s) ?p", $_object_name, $path_condition, $object_id, $object_type, $dispatch, $lang_code, $condition); } if (!$exist) { $_data = array('name' => $_object_name, 'type' => $object_type, 'object_id' => $object_id, 'dispatch' => $dispatch, 'lang_code' => $lang_code, 'path' => fn_get_seo_parent_path($object_id, $object_type, $company_id, $use_generated_paths_cache)); if (fn_allowed_for('ULTIMATE')) { if (fn_get_seo_vars($object_type, 'not_shared')) { if (!empty($company_id)) { $_data['company_id'] = $company_id; } elseif (Registry::get('runtime.company_id')) { $_data['company_id'] = Registry::get('runtime.company_id'); } // Do not create SEO names for root if (empty($_data['company_id'])) { return ''; } } } if ($create_redirect) { $url = fn_generate_seo_url_from_schema(array('type' => $object_type, 'object_id' => $object_id, 'lang_code' => $lang_code), false, array(), $company_id); } $affected_rows = db_query("INSERT INTO ?:seo_names ?e ON DUPLICATE KEY UPDATE ?u", $_data, $_data); if ($affected_rows && $create_redirect) { fn_seo_update_redirect(array('src' => $url, 'type' => $object_type, 'object_id' => $object_id, 'company_id' => $company_id, 'lang_code' => $lang_code), 0, false); } } else { $index++; if ($index == 1) { $object_name = $object_name . SEO_DELIMITER . $lang_code; } else { $object_name = preg_replace("/-\\d+\$/", "", $object_name) . SEO_DELIMITER . $index; } $_object_name = fn_create_seo_name($object_id, $object_type, $object_name, $index, $dispatch, $company_id, $lang_code, $create_redirect); } /** * Create SEO name (running after fn_create_seo_name() function) * * @param int $_object_name * @param int $object_id * @param string $object_type * @param string $object_name * @param int $index * @param string $dispatch * @param int $company_id * @param string $lang_code Two-letter language code (e.g. 'en', 'ru', etc.) * @param array $params Additional params passed to fn_create_seo_name() function */ fn_set_hook('create_seo_name_post', $_object_name, $object_id, $object_type, $object_name, $index, $dispatch, $company_id, $lang_code, $params); return $_object_name; }