Пример #1
0
function fn_get_tags($params = array(), $items_per_page = 0)
{
    // Init filter
    $params = LastView::instance()->update('tags', $params);
    $default_params = array('page' => 1, 'items_per_page' => $items_per_page);
    $params = array_merge($default_params, $params);
    // Define sort fields
    $sortings = array('tag' => '?:tags.tag', 'status' => '?:tags.status', 'popularity' => 'popularity', 'users' => 'users');
    $conditions = fn_tags_build_conditions($params);
    $sorting = db_sort($params, $sortings, 'tag', 'asc');
    $limit = '';
    if (!empty($params['limit'])) {
        $limit = db_quote(' LIMIT 0, ?i', $params['limit']);
    } elseif (!empty($params['items_per_page'])) {
        $params['total_items'] = db_get_field("SELECT COUNT(DISTINCT(?:tags.tag_id)) FROM ?:tags LEFT JOIN ?:tag_links ON ?:tags.tag_id = ?:tag_links.tag_id WHERE 1 ?p", $conditions);
        $limit = db_paginate($params['page'], $params['items_per_page'], $params['total_items']);
    }
    $tags = db_get_hash_array("SELECT ?:tags.tag_id, ?:tag_links.object_id, ?:tag_links.object_type, ?:tags.tag, ?:tags.status, COUNT(?:tag_links.tag_id) as popularity " . "FROM ?:tags LEFT JOIN ?:tag_links ON ?:tag_links.tag_id = ?:tags.tag_id WHERE 1 ?p GROUP BY ?:tags.tag_id {$sorting} {$limit}", 'tag_id', $conditions);
    if (!empty($params['count_objects'])) {
        $objs = db_get_array("SELECT tag_id, COUNT(DISTINCT(object_id)) as count, object_type " . "FROM ?:tag_links WHERE tag_id IN (?n) GROUP BY tag_id, object_type", array_keys($tags));
        foreach ($objs as $v) {
            $tags[$v['tag_id']]['objects_count'][$v['object_type']] = $v['count'];
        }
    }
    // Generate popularity level
    foreach ($tags as $k => $v) {
        $level = ceil(log($v['popularity']));
        $tags[$k]['level'] = $level > TAGS_MAX_LEVEL ? TAGS_MAX_LEVEL : $level;
    }
    if (!empty($params['sort_popular'])) {
        $tags = fn_sort_array_by_key($tags, 'tag', SORT_ASC);
    }
    return array($tags, $params);
}
Пример #2
0
/**
 * Set hook to use by addons
 *
 * @param mixed $argN argument, passed to addon
 * @return boolean always true
 */
function fn_set_hook($arg0 = NULL, &$arg1 = NULL, &$arg2 = NULL, &$arg3 = NULL, &$arg4 = NULL, &$arg5 = NULL, &$arg6 = NULL, &$arg7 = NULL, &$arg8 = NULL, &$arg9 = NULL, &$arg10 = NULL, &$arg11 = NULL, &$arg12 = NULL, &$arg13 = NULL, &$arg14 = NULL, &$arg15 = NULL)
{
    $hooks = Registry::get('hooks');
    $test = $arg0;
    static $loaded_addons;
    static $callable_functions;
    static $hooks_already_sorted;
    for ($args = array(), $i = 0; $i < 16; $i++) {
        $name = 'arg' . $i;
        if ($i < func_num_args()) {
            $args[$i] =& ${$name};
        }
        unset(${$name}, $name);
    }
    //    if($test=="calculate_cart_items"){
    //        //var_dump($callback['func']); echo" ==== ";
    //        var_dump($args); echo"<br/>______________________________<br/>";
    //    }
    $hook_name = array_shift($args);
    // Check for the core functions
    $core_func = 'fn_core_' . $hook_name;
    if (is_callable($core_func)) {
        call_user_func_array($core_func, $args);
    }
    $edition_acronym = fn_get_edition_acronym(PRODUCT_EDITION);
    if (!empty($edition_acronym)) {
        $edition_hook_func = "fn_{$edition_acronym}_{$hook_name}";
        if (function_exists($edition_hook_func)) {
            call_user_func_array($edition_hook_func, $args);
        }
    }
    if (isset($hooks[$hook_name])) {
        // cache hooks sorting
        if (!isset($hooks_already_sorted[$hook_name])) {
            $hooks[$hook_name] = fn_sort_array_by_key($hooks[$hook_name], 'priority');
            $hooks_already_sorted[$hook_name] = true;
            Registry::set('hooks', $hooks, true);
        }
        foreach ($hooks[$hook_name] as $callback) {
            //cache loaded addon
            if (!isset($loaded_addons[$callback['addon']])) {
                // FIXME: duplicate with cache in fn_load_addon
                fn_load_addon($callback['addon']);
                $loaded_addons[$callback['addon']] = true;
            }
            // cache if hook function callable
            if (!isset($callable_functions[$callback['func']])) {
                if (!is_callable($callback['func'])) {
                    throw new DeveloperException("Hook {$callback['func']} is not callable");
                }
                $callable_functions[$callback['func']] = true;
            }
            call_user_func_array($callback['func'], $args);
        }
    }
    return true;
}
Пример #3
0
/**
 * Set hook to use by addons
 *
 * @param mixed $argN argument, passed to addon
 * @return boolean always true
 */
function fn_set_hook($arg0 = NULL, &$arg1 = NULL, &$arg2 = NULL, &$arg3 = NULL, &$arg4 = NULL, &$arg5 = NULL, &$arg6 = NULL, &$arg7 = NULL, &$arg8 = NULL, &$arg9 = NULL, &$arg10 = NULL, &$arg11 = NULL, &$arg12 = NULL, &$arg13 = NULL, &$arg14 = NULL, &$arg15 = NULL)
{
    $hooks = Registry::get('hooks');
    static $loaded_addons;
    static $callable_functions;
    static $hooks_already_sorted;
    for ($args = array(), $i = 0; $i < 10; $i++) {
        $name = 'arg' . $i;
        if ($i < func_num_args()) {
            $args[$i] =& ${$name};
        }
        unset(${$name}, $name);
    }
    $hook_name = array_shift($args);
    if (isset($hooks[$hook_name])) {
        // cache hooks sorting
        if (!isset($hooks_already_sorted[$hook_name])) {
            $hooks[$hook_name] = fn_sort_array_by_key($hooks[$hook_name], 'priority');
            $hooks_already_sorted[$hook_name] = true;
        }
        foreach ($hooks[$hook_name] as $callback) {
            //cache loaded addon
            if (!isset($loaded_addons[$callback['addon']])) {
                // FIXME: duplicate with cache in fn_load_addon
                fn_load_addon($callback['addon']);
                $loaded_addons[$callback['addon']] = true;
            }
            // cache if hook function callable
            if (!isset($callable_functions[$callback['func']])) {
                if (!is_callable($callback['func'])) {
                    die("Hook {$callback['func']} is not callable");
                }
                $callable_functions[$callback['func']] = true;
            }
            call_user_func_array($callback['func'], $args);
        }
    }
    return true;
}
Пример #4
0
/**
 * Set hook to use by addons
 *
 * @param mixed $argN argument, passed to addon
 * @return boolean always true
 */
function fn_set_hook($hook_name = NULL, &$arg1 = NULL, &$arg2 = NULL, &$arg3 = NULL, &$arg4 = NULL, &$arg5 = NULL, &$arg6 = NULL, &$arg7 = NULL, &$arg8 = NULL, &$arg9 = NULL, &$arg10 = NULL, &$arg11 = NULL, &$arg12 = NULL, &$arg13 = NULL, &$arg14 = NULL, &$arg15 = NULL)
{
    /**
     * @var bool[]|null $callable_functions Cache of validations that hook's function is callable groupped by func name.
     */
    static $callable_functions;
    /**
     * @var array $hooks_already_sorted Cache of hook lists ordered by addon priority and groupped by hook name.
     */
    static $hooks_already_sorted = array();
    /**
     * @var array|null $hooks Function's local cache of hooks that have been registered by addons.
     */
    static $hooks = null;
    /**
     * @var bool $addons_initiated Function's local cache of addons' initialization state.
     */
    static $addons_initiated = false;
    // We use local hooks cache that was filled at previous fn_set_hook() call
    // only if addons were already initiated at that call.
    if ($addons_initiated) {
        $update_hooks_cache = false;
    } else {
        $update_hooks_cache = true;
        // Update local cache of addons' init state
        $addons_initiated = Registry::get('addons_initiated');
    }
    if ($hooks === null || $update_hooks_cache || defined('DISABLE_HOOK_CACHE')) {
        // Updating local hooks cache
        $hooks = Registry::get('hooks');
        $hooks_already_sorted = array();
    }
    $arg_count = func_num_args();
    if ($arg_count === 1) {
        $args = array();
    } elseif ($arg_count === 2) {
        $args = array(&$arg1);
    } elseif ($arg_count === 3) {
        $args = array(&$arg1, &$arg2);
    } elseif ($arg_count === 4) {
        $args = array(&$arg1, &$arg2, &$arg3);
    } elseif ($arg_count === 5) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4);
    } elseif ($arg_count === 6) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5);
    } elseif ($arg_count === 7) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6);
    } elseif ($arg_count === 8) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7);
    } elseif ($arg_count === 9) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8);
    } elseif ($arg_count === 10) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9);
    } elseif ($arg_count === 11) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9, &$arg10);
    } elseif ($arg_count === 12) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9, &$arg10, &$arg11);
    } elseif ($arg_count === 13) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9, &$arg10, &$arg11, &$arg12);
    } elseif ($arg_count === 14) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9, &$arg10, &$arg11, &$arg12, &$arg13);
    } elseif ($arg_count === 15) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9, &$arg10, &$arg11, &$arg12, &$arg13, &$arg14);
    } elseif ($arg_count === 16) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9, &$arg10, &$arg11, &$arg12, &$arg13, &$arg14, &$arg15);
    }
    // Check for the core functions
    if (is_callable('fn_core_' . $hook_name)) {
        call_user_func_array('fn_core_' . $hook_name, $args);
    }
    $edition_acronym = fn_get_edition_acronym(PRODUCT_EDITION);
    if (!empty($edition_acronym) && function_exists("fn_{$edition_acronym}_{$hook_name}")) {
        call_user_func_array("fn_{$edition_acronym}_{$hook_name}", $args);
    }
    if (isset($hooks[$hook_name])) {
        // cache hooks sorting
        if (!isset($hooks_already_sorted[$hook_name])) {
            $hooks[$hook_name] = fn_sort_array_by_key($hooks[$hook_name], 'priority');
            $hooks_already_sorted[$hook_name] = true;
            Registry::set('hooks', $hooks, true);
        }
        foreach ($hooks[$hook_name] as $callback) {
            // cache if hook function callable
            if (!isset($callable_functions[$callback['func']])) {
                if (!is_callable($callback['func'])) {
                    throw new DeveloperException("Hook {$callback['func']} is not callable");
                }
                $callable_functions[$callback['func']] = true;
            }
            call_user_func_array($callback['func'], $args);
        }
    }
    return true;
}
Пример #5
0
/**
 * Gets product features list
 *
 * @param array $product Array with product data
 * @param string $display_on Code determines zone (product/catalog page) for that features are selected
 * @param string $lang_code 2-letters language code
 * @return array Product features
 */
function fn_get_product_features_list($product, $display_on = 'C', $lang_code = CART_LANGUAGE)
{
    /**
     * Changes params before getting product features list
     *
     * @param array  $product    Array with product data
     * @param string $display_on Code determines zone (product/catalog page) for that features are selected
     * @param string $lang_code  2-letters language code
     */
    fn_set_hook('get_product_features_list_pre', $product, $display_on, $lang_code);
    $product_id = $product['product_id'];
    $features_list = array();
    if ($display_on == 'H') {
        $condition = " AND f.display_on_header = 'Y'";
    } elseif ($display_on == 'C') {
        $condition = " AND f.display_on_catalog = 'Y'";
    } elseif ($display_on == 'CP') {
        $condition = " AND (f.display_on_catalog = 'Y' OR f.display_on_product = 'Y')";
    } elseif ($display_on == 'A') {
        $condition = '';
    } else {
        $condition = " AND f.display_on_product = 'Y'";
    }
    $path = !empty($product['main_category']) ? explode('/', db_get_field("SELECT id_path FROM ?:categories WHERE category_id = ?i", $product['main_category'])) : array();
    if (fn_allowed_for('ULTIMATE')) {
        $product['shared_product'] = fn_ult_is_shared_product($product_id);
        if ($product['shared_product'] == 'Y') {
            //we should get features for all categories, not only main
            $path = !empty($product['category_ids']) ? explode('/', implode('/', db_get_fields("SELECT id_path FROM ?:categories WHERE category_id IN (?a)", $product['category_ids']))) : array();
        }
    }
    $find_set = array(" f.categories_path = '' ");
    foreach ($path as $k => $v) {
        $find_set[] = db_quote(" FIND_IN_SET(?i, f.categories_path) ", $v);
    }
    $find_in_set = db_quote(" AND (?p)", implode('OR', $find_set));
    $condition .= $find_in_set;
    $fields = db_quote("v.feature_id, v.value, v.value_int, v.variant_id, f.feature_type, fd.description, fd.prefix, fd.suffix, vd.variant, f.parent_id, ft.filter_id, ft.field_type, f.position, gf.position as gposition");
    $join = db_quote("LEFT JOIN ?:product_features_values as v ON v.feature_id = f.feature_id " . " LEFT JOIN ?:product_features_descriptions as fd ON fd.feature_id = v.feature_id AND fd.lang_code = ?s" . " LEFT JOIN ?:product_feature_variants fv ON fv.variant_id = v.variant_id" . " LEFT JOIN ?:product_filters AS ft ON ft.feature_id = f.feature_id" . " LEFT JOIN ?:product_feature_variant_descriptions as vd ON vd.variant_id = fv.variant_id AND vd.lang_code = ?s" . " LEFT JOIN ?:product_features as gf ON gf.feature_id = f.parent_id AND gf.feature_type = ?s ", $lang_code, $lang_code, 'G');
    $condition = db_quote("f.status = 'A' AND IF(f.parent_id, (SELECT status FROM ?:product_features as df WHERE df.feature_id = f.parent_id), 'A') = 'A' AND v.product_id = ?i ?p AND (v.variant_id != 0 OR (f.feature_type != 'C' AND v.value != '') OR (f.feature_type = 'C') OR v.value_int != '') AND v.lang_code = ?s", $product_id, $condition, $lang_code);
    /**
     * Change SQL parameters before fetching product feature data
     *
     * @param string $fields    String of comma-separated SQL fields to be selected in an SQL-query
     * @param string $join      String with the complete JOIN information (JOIN type, tables and fields) for an SQL-query
     * @param string $condition String containing SQL-query condition possibly prepended with a logical operator (AND or OR)
     * @param array  $product   Array with product data
     * @param string $lang_code 2-letters language code
     */
    fn_set_hook('get_product_features_list_before_select', $fields, $join, $condition, $product, $display_on, $lang_code);
    $_data = db_get_array("SELECT {$fields} FROM ?:product_features as f {$join} WHERE {$condition} ORDER BY fd.description, fv.position");
    if (!empty($_data)) {
        foreach ($_data as $k => $feature) {
            if ($feature['feature_type'] == 'C') {
                if ($feature['value'] != 'Y' && $display_on != 'A') {
                    unset($_data[$k]);
                    continue;
                }
            }
            if (empty($features_list[$feature['feature_id']])) {
                $features_list[$feature['feature_id']] = $feature;
            }
            if (!empty($feature['variant_id'])) {
                // feature has several variants
                if (!empty($feature['filter_id'])) {
                    $range_data = array('range_id' => $feature['variant_id'], 'range_name' => $feature['variant'], 'feature_type' => $feature['feature_type']);
                    $features_list[$feature['feature_id']]['features_hash'] = fn_add_range_to_url_hash('', $range_data, $feature['field_type']);
                }
                $features_list[$feature['feature_id']]['variants'][$feature['variant_id']] = array('value' => $feature['value'], 'value_int' => $feature['value_int'], 'variant_id' => $feature['variant_id'], 'variant' => $feature['variant']);
            }
            unset($features_list[$feature['feature_id']]['filter_id']);
            unset($features_list[$feature['feature_id']]['field_type']);
        }
    }
    $groups = array();
    foreach ($features_list as $f_id => $data) {
        $groups[$data['parent_id']]['features'][$f_id] = $data;
        $groups[$data['parent_id']]['position'] = empty($data['parent_id']) ? $data['position'] : $data['gposition'];
    }
    $features_list = array();
    if (!empty($groups)) {
        $groups = fn_sort_array_by_key($groups, 'position');
        foreach ($groups as $g) {
            $g['features'] = fn_sort_array_by_key($g['features'], 'position');
            $features_list = fn_array_merge($features_list, $g['features']);
        }
    }
    unset($groups);
    foreach ($features_list as $f_id => $data) {
        unset($features_list[$f_id]['position']);
        unset($features_list[$f_id]['gposition']);
    }
    /**
     * Changes product features list data
     *
     * @param array  $features_list Product features
     * @param array  $product       Array with product data
     * @param string $display_on    Code determines zone (product/catalog page) for that features are selected
     * @param string $lang_code     2-letters language code
     */
    fn_set_hook('get_product_features_list_post', $features_list, $product, $display_on, $lang_code);
    return $features_list;
}
Пример #6
0
/**
 * Calculate products and shipping taxes
 *
 * @param array $cart Cart data
 * @param array $group_key Group number
 * @param array $group_products Products data
 * @param array $shipping_rates
 * @param array $auth Auth data
 * @return boolean Always false
 */
function fn_calculate_taxes(&$cart, $group_key, &$group_products, &$shipping_rates, $auth)
{
    /**
     * Prepare params before applying products and shipping taxes to cart
     *
     * @param array $cart           Cart data
     * @param array $group_products Products data
     * @param array $shipping_rates
     * @param array $auth           Auth data
     */
    fn_set_hook('calculate_taxes_pre', $cart, $group_products, $shipping_rates, $auth);
    $calculated_data = array();
    if (Registry::get('settings.General.tax_calculation') == 'unit_price') {
        // Tax calculation method based on UNIT PRICE
        // Calculate product taxes
        foreach ($group_products as $k => $product) {
            $taxes = fn_get_product_taxes($k, $cart, $group_products);
            if (empty($taxes)) {
                continue;
            }
            if (isset($product['subtotal'])) {
                if ($product['price'] == $product['subtotal'] && $product['amount'] != 1) {
                    $price = fn_format_price($product['price']);
                } else {
                    $price = fn_format_price($product['subtotal'] / $product['amount']);
                }
                $calculated_data['P_' . $k] = fn_calculate_tax_rates($taxes, $price, $product['amount'], $auth, $cart);
                $group_products[$k]['tax_summary'] = array('included' => 0, 'added' => 0, 'total' => 0);
                // tax summary for 1 unit of product
                $cart['products'][$k]['tax_summary'] = array('included' => 0, 'added' => 0, 'total' => 0);
                // tax summary for 1 unit of product
                // Apply taxes to product subtotal
                if (!empty($calculated_data['P_' . $k])) {
                    foreach ($calculated_data['P_' . $k] as $_k => $v) {
                        $group_products[$k]['taxes'][$_k] = $v;
                        $cart['products'][$k]['taxes'][$_k] = $v;
                        if ($taxes[$_k]['price_includes_tax'] != 'Y') {
                            $group_products[$k]['tax_summary']['added'] += $v['tax_subtotal'];
                            $cart['products'][$k]['tax_summary']['added'] += $v['tax_subtotal'];
                        } else {
                            $group_products[$k]['tax_summary']['included'] += $v['tax_subtotal'];
                            $cart['products'][$k]['tax_summary']['included'] += $v['tax_subtotal'];
                        }
                    }
                    $group_products[$k]['tax_summary']['total'] = $group_products[$k]['tax_summary']['added'] + $group_products[$k]['tax_summary']['included'];
                    $cart['products'][$k]['tax_summary']['total'] = $cart['products'][$k]['tax_summary']['added'] + $cart['products'][$k]['tax_summary']['included'];
                }
            }
        }
        // Calculate shipping taxes
        if (!empty($shipping_rates)) {
            foreach ($shipping_rates as $shipping_id => $shipping) {
                $taxes = fn_get_shipping_taxes($shipping_id, $shipping_rates, $cart);
                if (!empty($taxes)) {
                    $shipping_rates[$shipping_id]['taxes'] = array();
                    $calculate_rate = true;
                    if (!empty($cart['chosen_shipping'][$group_key]) && $cart['chosen_shipping'][$group_key] == $shipping_id) {
                        $calculated_data['S_' . $group_key . '_' . $shipping_id] = fn_calculate_tax_rates($taxes, $shipping['rate'], 1, $auth, $cart);
                        if (!empty($calculated_data['S_' . $group_key . '_' . $shipping_id])) {
                            foreach ($calculated_data['S_' . $group_key . '_' . $shipping_id] as $__k => $__v) {
                                if ($taxes[$__k]['price_includes_tax'] != 'Y') {
                                    $cart['display_shipping_cost'] += Registry::get('settings.Appearance.cart_prices_w_taxes') == 'Y' ? $__v['tax_subtotal'] : 0;
                                    $cart['tax_subtotal'] += $__v['tax_subtotal'];
                                }
                                if ($cart['stored_taxes'] == 'Y') {
                                    $cart['taxes'][$__k]['applies']['S_' . $group_key . '_' . $shipping_id] = $__v['tax_subtotal'];
                                }
                            }
                            $shipping_rates[$shipping_id]['taxes']['S_' . $group_key . '_' . $shipping_id] = $calculated_data['S_' . $group_key . '_' . $shipping_id];
                            $calculate_rate = false;
                        }
                    }
                    if ($calculate_rate) {
                        $cur_shipping_rates = fn_calculate_tax_rates($taxes, $shipping['rate'], 1, $auth, $cart);
                        if (!empty($cur_shipping_rates)) {
                            $shipping_rates[$shipping_id]['taxes'] = $cur_shipping_rates;
                        }
                    }
                }
            }
            foreach ($shipping_rates as $shipping_id => $shipping) {
                // Calculate taxes for each shipping rate
                $taxes = fn_get_shipping_taxes($shipping_id, $shipping_rates, $cart);
                $shipping_rates[$shipping_id]['taxed_price'] = 0;
                unset($shipping_rates[$shipping_id]['inc_tax']);
                if (!empty($taxes)) {
                    $shipping_rates[$shipping_id]['taxes'] = array();
                    $tax = fn_calculate_tax_rates($taxes, fn_format_price($shipping['rate']), 1, $auth, $cart);
                    $shipping_rates[$shipping_id]['taxes'] = $tax;
                    if (!empty($tax) && Registry::get('settings.Appearance.cart_prices_w_taxes') == 'Y') {
                        foreach ($tax as $_id => $_tax) {
                            if ($_tax['price_includes_tax'] != 'Y') {
                                $shipping_rates[$shipping_id]['taxed_price'] += $_tax['tax_subtotal'];
                            }
                        }
                        $shipping_rates[$shipping_id]['inc_tax'] = true;
                    }
                    if (!empty($shipping_rates[$shipping_id]['rate']) && $shipping_rates[$shipping_id]['taxed_price'] > 0) {
                        $shipping_rates[$shipping_id]['taxed_price'] += $shipping_rates[$shipping_id]['rate'];
                    }
                }
            }
        }
    } else {
        // Tax calculation method based on SUBTOTAL
        // Calculate discounted subtotal
        if (!isset($cart['subtotal_discount'])) {
            $cart['subtotal_discount'] = 0;
        }
        $discounted_subtotal = $cart['subtotal'] - $cart['subtotal_discount'];
        if ($discounted_subtotal < 0) {
            $discounted_subtotal = 0;
        }
        // Get discount distribution coefficient (DDC) between taxes
        if ($cart['subtotal'] > 0) {
            $ddc = $discounted_subtotal / $cart['subtotal'];
        } else {
            $ddc = 1;
        }
        //
        // Group subtotal by taxes
        //
        $subtotal = array();
        // Get products taxes
        foreach ($group_products as $cart_id => $product) {
            $taxes = fn_get_product_taxes($cart_id, $cart, $group_products);
            if (!empty($taxes)) {
                foreach ($taxes as $tax_id => $tax) {
                    if (empty($subtotal[$tax_id])) {
                        $subtotal[$tax_id] = fn_init_tax_subtotals($tax);
                    }
                    $_subtotal = $product['price'] == $product['subtotal'] && $product['amount'] != 1 ? fn_format_price($product['price'] * $product['amount']) : $product['subtotal'];
                    $subtotal[$tax_id]['subtotal'] += $_subtotal;
                    $subtotal[$tax_id]['applies']['P'] += $_subtotal;
                    $subtotal[$tax_id]['applies']['items']['P'][$cart_id] = true;
                    if (isset($product['company_id'])) {
                        if (!isset($subtotal[$tax_id]['group'][$group_key])) {
                            $subtotal[$tax_id]['group'][$group_key]['products'] = 0;
                        }
                        $subtotal[$tax_id]['group'][$group_key]['products'] += $_subtotal;
                        $priority_stack['products'][$group_key] = -1;
                        $applied_taxes['products'][$group_key] = 0;
                    }
                }
            }
        }
        // Get shipping taxes
        if (!empty($shipping_rates)) {
            foreach ($shipping_rates as $shipping_id => $shipping) {
                // Calculate taxes for each shipping rate
                $taxes = fn_get_shipping_taxes($shipping_id, $shipping_rates, $cart);
                $shipping_rates[$shipping_id]['taxed_price'] = 0;
                unset($shipping_rates[$shipping_id]['inc_tax']);
                // Display shipping with taxes at cart/checkout page
                if (!empty($taxes)) {
                    $shipping_rates[$shipping_id]['taxes'] = array();
                    $tax = fn_calculate_tax_rates($taxes, fn_format_price($shipping['rate']), 1, $auth, $cart);
                    $shipping_rates[$shipping_id]['taxes'] = $tax;
                    if (!empty($tax) && Registry::get('settings.Appearance.cart_prices_w_taxes') == 'Y') {
                        foreach ($tax as $_id => $_tax) {
                            if ($_tax['price_includes_tax'] != 'Y') {
                                $shipping_rates[$shipping_id]['taxed_price'] += $_tax['tax_subtotal'];
                            }
                        }
                        $shipping_rates[$shipping_id]['inc_tax'] = true;
                    }
                    if (!empty($shipping_rates[$shipping_id]['rate']) && $shipping_rates[$shipping_id]['taxed_price'] > 0) {
                        $shipping_rates[$shipping_id]['taxed_price'] += $shipping_rates[$shipping_id]['rate'];
                    }
                }
                if (!isset($cart['chosen_shipping'][$group_key]) || $cart['chosen_shipping'][$group_key] != $shipping_id) {
                    continue;
                }
                // Add shipping taxes to "tax" array
                if (!empty($taxes)) {
                    foreach ($taxes as $tax_id => $tax) {
                        if (empty($subtotal[$tax_id])) {
                            $subtotal[$tax_id] = fn_init_tax_subtotals($tax);
                        }
                        $subtotal[$tax_id]['subtotal'] += $shipping['rate'];
                        $subtotal[$tax_id]['applies']['S'] += $shipping['rate'];
                        $subtotal[$tax_id]['applies']['items']['S'][$group_key][$shipping_id] = true;
                        /*                        if (!isset($subtotal[$tax_id]['groups'][$group_key]['shippings'])) {
                                                    $subtotal[$tax_id]['groups'][$group_key]['shippings'] = 0;
                                                }
                        
                                                $subtotal[$tax_id]['groups'][$group_key]['shippings'] += $shipping['rate'];*/
                        $priority_stack['shippings'][$group_key] = -1;
                        $applied_taxes['shippings'][$group_key] = 0;
                    }
                }
            }
        }
        if (!empty($subtotal)) {
            $subtotal = fn_sort_array_by_key($subtotal, 'priority');
        }
        // Apply DDC and calculate tax rates
        $calculated_taxes = array();
        if (empty($priority_stack)) {
            $priority_stack['products'][0] = -1;
            $priority_stack['shippings'][0] = -1;
            $applied_taxes['products'][0] = 0;
            $applied_taxes['shippings'][0] = 0;
        }
        foreach ($subtotal as $tax_id => $_st) {
            if (empty($_st['tax_id'])) {
                $_st['tax_id'] = $tax_id;
            }
            $product_tax = fn_calculate_tax_rates(array($_st), fn_format_price($_st['applies']['P'] * $ddc), 1, $auth, $cart);
            $shipping_tax = fn_calculate_tax_rates(array($_st), fn_format_price($_st['applies']['S']), 1, $auth, $cart);
            if (empty($product_tax) && empty($shipping_tax)) {
                continue;
            }
            if (empty($_st['groups'])) {
                $_st['groups'][0]['products'] = $_st['applies']['P'];
                $_st['groups'][0]['shippings'] = $_st['applies']['S'];
            }
            foreach ($_st['groups'] as $group_key => $applies) {
                $apply_tax_stack = array('products' => 0, 'shippings' => 0);
                if (!isset($priority_stack['products'][$group_key])) {
                    $priority_stack['products'][$group_key] = -1;
                }
                if (!isset($priority_stack['shippings'][$group_key])) {
                    $priority_stack['shippings'][$group_key] = -1;
                }
                if ($priority_stack['products'][$group_key] < 0 && !empty($applies['products'])) {
                    $priority_stack['products'][$group_key] = $_st['priority'];
                } elseif (!empty($applies['products']) && $priority_stack['products'][$group_key] != $_st['priority']) {
                    $apply_tax_stack['products'] = $applied_taxes['products'][$group_key];
                    $priority_stack['products'][$group_key] = $_st['priority'];
                }
                if ($priority_stack['shippings'][$group_key] < 0 && !empty($applies['shippings'])) {
                    $priority_stack['shippings'][$group_key] = $_st['priority'];
                } elseif (!empty($applies['shippings']) && $priority_stack['shippings'][$group_key] != $_st['priority']) {
                    $apply_tax_stack['shippings'] = $applied_taxes['shippings'][$group_key];
                    $priority_stack['shippings'][$group_key] = $_st['priority'];
                }
                if (empty($calculated_data[$tax_id])) {
                    $calculated_data[$tax_id] = empty($product_tax) ? reset($shipping_tax) : reset($product_tax);
                }
                if (!empty($applies['products'])) {
                    $products_tax = fn_calculate_tax_rates(array($_st), fn_format_price($applies['products'] * $ddc + $apply_tax_stack['products']), 1, $auth, $cart);
                } else {
                    $products_tax[$tax_id]['tax_subtotal'] = 0;
                }
                if (!empty($applies['shippings'])) {
                    $shippings_tax = fn_calculate_tax_rates(array($_st), fn_format_price($applies['shippings'] + $apply_tax_stack['shippings']), 1, $auth, $cart);
                } else {
                    $shippings_tax[$tax_id]['tax_subtotal'] = 0;
                }
                if (!isset($applied_taxes['products'][$group_key])) {
                    $applied_taxes['products'][$group_key] = 0;
                }
                if (!isset($applied_taxes['shippings'][$group_key])) {
                    $applied_taxes['shippings'][$group_key] = 0;
                }
                if ($_st['price_includes_tax'] != 'Y') {
                    $applied_taxes['products'][$group_key] += $products_tax[$tax_id]['tax_subtotal'];
                    $applied_taxes['shippings'][$group_key] += $shippings_tax[$tax_id]['tax_subtotal'];
                }
                if (!isset($calculated_data[$tax_id]['applies']['P'])) {
                    $calculated_data[$tax_id]['applies']['P'] = 0;
                }
                if (!isset($calculated_data[$tax_id]['applies']['S'])) {
                    $calculated_data[$tax_id]['applies']['S'] = 0;
                }
                $calculated_data[$tax_id]['applies']['P'] += $products_tax[$tax_id]['tax_subtotal'];
                $calculated_data[$tax_id]['applies']['S'] += $shippings_tax[$tax_id]['tax_subtotal'];
                $calculated_data[$tax_id]['applies']['items'] = $_st['applies']['items'];
                $calculated_data[$tax_id]['tax_subtotal'] = $calculated_data[$tax_id]['applies']['P'] + $calculated_data[$tax_id]['applies']['S'];
            }
        }
    }
    /**
     * Processes products data after applying products and shipping taxes to cart)
     *
     * @param array $cart            Cart data
     * @param array $group_products  Products data
     * @param array $shipping_rates  Shipping rates data
     * @param array $auth            Auth data
     * @param array $calculated_data Payment taxes data
     */
    fn_set_hook('calculate_taxes_post', $cart, $group_products, $shipping_rates, $auth, $calculated_data);
    return $calculated_data;
}
Пример #7
0
 /**
  * Renders grid
  * @param  int    $grid Grid data to be rendered
  * @return string HTML code of rendered grid
  */
 private function _renderGrid($grid)
 {
     $content = '';
     if ($this->_area == 'A' || $grid['status'] != 'D') {
         if (isset($grid['children']) && !empty($grid['children'])) {
             $grid['children'] = fn_sort_array_by_key($grid['children'], 'grid_id');
             $parent_grid = $this->_parent_grid;
             $this->_parent_grid = $grid;
             foreach ($grid['children'] as $child_grid) {
                 $content .= $this->_renderGrid($child_grid);
             }
             $this->_parent_grid = $parent_grid;
         } else {
             $content .= $this->renderBlocks($grid);
         }
     }
     $this->_view->assign('content', $content);
     $this->_view->assign('parent_grid', $this->_parent_grid);
     $this->_view->assign('grid', $grid);
     return $this->_view->fetch($this->_theme . 'grid.tpl');
 }
Пример #8
0
*   (c) 2004 Vladimir V. Kalynyak, Alexey V. Vinokurov, Ilya M. Shalnev    *
*                                                                          *
* This  is  commercial  software,  only  users  who have purchased a valid *
* license  and  accept  to the terms of the  License Agreement can install *
* and use this program.                                                    *
*                                                                          *
****************************************************************************
* PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
* "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
****************************************************************************/
use Tygh\Registry;
if (!defined('BOOTSTRAP')) {
    die('Access denied');
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    return;
}
if ($mode == 'update' || $mode == 'manage') {
    $processors = Tygh::$app['view']->getTemplateVars('payment_processors');
    if (!empty($processors)) {
        foreach ($processors as &$processor) {
            if ($processor['processor'] == 'Kupivkredit') {
                $processor['russian'] = 'Y';
                $processor['type'] = 'R';
                $processor['position'] = 'a_99';
            }
        }
        $processors = fn_sort_array_by_key($processors, 'position');
        Tygh::$app['view']->assign('payment_processors', $processors);
    }
}
Пример #9
0
function fn_gather_reward_points_data(&$product, &$auth, $get_point_info = true)
{
    // Check, if the product has any option points modifiers
    if (empty($product['options_update']) && !empty($product['product_options'])) {
        foreach ($product['product_options'] as $_id => $option) {
            if (!empty($product['product_options'][$_id]['variants'])) {
                foreach ($product['product_options'][$_id]['variants'] as $variant) {
                    if (!empty($variant['point_modifier']) && floatval($variant['point_modifier'])) {
                        $product['options_update'] = true;
                        break 2;
                    }
                }
            }
        }
    }
    if (isset($product['exclude_from_calculate']) || isset($product['points_info']['reward']) && !(Registry::get('runtime.controller') == 'products' && Registry::get('runtime.mode') == 'options') || $get_point_info == false) {
        return false;
    }
    if (!isset($product['main_category'])) {
        $product['main_category'] = db_get_field("SELECT category_id FROM ?:products_categories WHERE product_id = ?i AND link_type = 'M'", $product['product_id']);
    }
    $candidates = array(PRODUCT_REWARD_POINTS => $product['product_id'], CATEGORY_REWARD_POINTS => $product['main_category'], GLOBAL_REWARD_POINTS => 0);
    $reward_points = array();
    foreach ($candidates as $object_type => $object_id) {
        $_reward_points = fn_get_reward_points($object_id, $object_type, $auth['usergroup_ids']);
        if ($object_type == CATEGORY_REWARD_POINTS && !empty($_reward_points)) {
            // get the "override point" setting
            $category_is_op = db_get_field("SELECT is_op FROM ?:categories WHERE category_id = ?i", $_reward_points['object_id']);
        }
        if ($object_type == CATEGORY_REWARD_POINTS && (empty($_reward_points) || $category_is_op != 'Y')) {
            // if there is no points of main category of the "override point" setting is disabled
            // then get point of secondary categories
            $secondary_categories = db_get_fields("SELECT category_id FROM ?:products_categories WHERE product_id = ?i AND link_type = 'A'", $product['product_id']);
            if (!empty($secondary_categories)) {
                $secondary_categories_points = array();
                foreach ($secondary_categories as $value) {
                    $_rp = fn_get_reward_points($value, $object_type, $auth['usergroup_ids']);
                    if (isset($_rp['amount'])) {
                        $secondary_categories_points[] = $_rp;
                    }
                    unset($_rp);
                }
                if (!empty($secondary_categories_points)) {
                    $sorted_points = fn_sort_array_by_key($secondary_categories_points, 'amount', Registry::get('addons.reward_points.several_points_action') == 'minimal_absolute' || Registry::get('addons.reward_points.several_points_action') == 'minimal_percentage' ? SORT_ASC : SORT_DESC);
                    $_reward_points = array_shift($sorted_points);
                }
            }
            if (!isset($_reward_points['amount'])) {
                if (Registry::get('addons.reward_points.higher_level_extract') == 'Y' && !empty($candidates[$object_type])) {
                    $id_path = db_get_field("SELECT REPLACE(id_path, '{$candidates[$object_type]}', '') FROM ?:categories WHERE category_id = ?i", $candidates[$object_type]);
                    if (!empty($id_path)) {
                        $c_ids = explode('/', trim($id_path, '/'));
                        $c_ids = array_reverse($c_ids);
                        foreach ($c_ids as $category_id) {
                            $__reward_points = fn_get_reward_points($category_id, $object_type, $auth['usergroup_ids']);
                            if (!empty($__reward_points)) {
                                // get the "override point" setting
                                $_category_is_op = db_get_field("SELECT is_op FROM ?:categories WHERE category_id = ?i", $__reward_points['object_id']);
                                if ($_category_is_op == 'Y') {
                                    $category_is_op = $_category_is_op;
                                    $_reward_points = $__reward_points;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        if (!empty($_reward_points) && ($object_type == GLOBAL_REWARD_POINTS || $object_type == PRODUCT_REWARD_POINTS && $product['is_op'] == 'Y' || $object_type == CATEGORY_REWARD_POINTS && (!empty($category_is_op) && $category_is_op == 'Y'))) {
            // if global points or category points (and override points is enabled) or product points (and override points is enabled)
            $reward_points = $_reward_points;
            break;
        }
    }
    if (isset($reward_points['amount'])) {
        if ((defined('ORDER_MANAGEMENT') || Registry::get('runtime.controller') == 'checkout') && isset($product['subtotal']) && isset($product['original_price'])) {
            if (Registry::get('addons.reward_points.points_with_discounts') == 'Y' && $reward_points['amount_type'] == 'P' && !empty($product['discounts'])) {
                $product['discount'] = empty($product['discount']) ? 0 : $product['discount'];
                $reward_points['coefficient'] = floatval($product['price']) ? ($product['price'] * $product['amount'] - $product['discount']) / $product['price'] * $product['amount'] / pow($product['amount'], 2) : 0;
            } else {
                $reward_points['coefficient'] = 1;
            }
        } else {
            $reward_points['coefficient'] = Registry::get('addons.reward_points.points_with_discounts') == 'Y' && $reward_points['amount_type'] == 'P' && isset($product['discounted_price']) ? $product['discounted_price'] / $product['price'] : 1;
        }
        if (isset($product['extra']['configuration'])) {
            if ($reward_points['amount_type'] == 'P') {
                // for configurable product calc reward points only for base price
                $price = $product['original_price'];
                if (!empty($product['discount'])) {
                    $price -= $product['discount'];
                }
                $reward_points['amount'] = $price * $reward_points['amount'] / 100;
            } else {
                $points_info = Registry::get("runtime.product_configurator.points_info");
                if (!empty($points_info[$product['product_id']])) {
                    $reward_points['amount'] = $points_info[$product['product_id']]['reward'];
                    $reward_points['coefficient'] = 1;
                }
            }
        } else {
            if ($reward_points['amount_type'] == 'P') {
                $reward_points['amount'] = $product['price'] * $reward_points['amount'] / 100;
            }
        }
        $reward_points['raw_amount'] = $reward_points['coefficient'] * $reward_points['amount'];
        $reward_points['raw_amount'] = !empty($product['selected_options']) ? fn_apply_options_modifiers($product['selected_options'], $reward_points['raw_amount'], POINTS_MODIFIER_TYPE) : $reward_points['raw_amount'];
        $reward_points['amount'] = round($reward_points['raw_amount']);
        $product['points_info']['reward'] = $reward_points;
    }
    fn_calculate_product_price_in_points($product, $auth, $get_point_info);
}
Пример #10
0
/**
 * Gets addons list
 *
 * @param array $params search params
 * @param int $items_per_page items per page for pagination
 * @param string $lang_code language code
 * @return array addons list and filtered search params
 */
function fn_get_addons($params, $items_per_page = 0, $lang_code = CART_LANGUAGE)
{
    $params = LastView::instance()->update('addons', $params);
    $default_params = array('type' => 'any');
    $params = array_merge($default_params, $params);
    $addons = array();
    $sections = Settings::instance()->getAddons();
    $all_addons = fn_get_dir_contents(Registry::get('config.dir.addons'), true, false);
    $installed_addons = db_get_hash_array('SELECT a.addon, a.status, b.name as name, b.description as description, a.separate, a.unmanaged, a.has_icon ' . 'FROM ?:addons as a LEFT JOIN ?:addon_descriptions as b ON b.addon = a.addon AND b.lang_code = ?s' . 'ORDER BY b.name ASC', 'addon', $lang_code);
    foreach ($installed_addons as $key => $addon) {
        $installed_addons[$key]['has_sections'] = Settings::instance()->sectionExists($sections, $addon['addon']);
        $installed_addons[$key]['has_options'] = $installed_addons[$key]['has_sections'] ? Settings::instance()->optionsExists($addon['addon'], 'ADDON') : false;
        // Check add-on snaphot
        if (!fn_check_addon_snapshot($key)) {
            $installed_addons[$key]['status'] = 'D';
            $installed_addons[$key]['snapshot_correct'] = false;
        } else {
            $installed_addons[$key]['snapshot_correct'] = true;
        }
    }
    foreach ($all_addons as $addon) {
        $addon_scheme = SchemesManager::getScheme($addon);
        if (in_array($params['type'], array('any', 'installed', 'active', 'disabled'))) {
            $search_status = $params['type'] == 'active' ? 'A' : ($params['type'] == 'disabled' ? 'D' : '');
            if (!empty($installed_addons[$addon])) {
                // exclude unmanaged addons from the list
                if ($installed_addons[$addon]['unmanaged'] == true) {
                    continue;
                }
                if (!empty($search_status) && $installed_addons[$addon]['status'] != $search_status) {
                    continue;
                }
                $addons[$addon] = $installed_addons[$addon];
                if ($addon_scheme != false && !$addon_scheme->getUnmanaged()) {
                    $addons[$addon]['originals'] = $addon_scheme->getOriginals();
                }
                fn_update_lang_objects('installed_addon', $addons[$addon]);
                // Generate custom description
                $func = 'fn_addon_dynamic_description_' . $addon;
                if (function_exists($func)) {
                    $addons[$addon]['description'] = $func($addons[$addon]['description']);
                }
            }
        }
        if (empty($installed_addons[$addon]) && empty($params['for_company']) && in_array($params['type'], array('any', 'not_installed'))) {
            if ($addon_scheme != false && !$addon_scheme->getUnmanaged()) {
                $addons[$addon] = array('status' => 'N', 'name' => $addon_scheme->getName(), 'snapshot_correct' => fn_check_addon_snapshot($addon), 'description' => $addon_scheme->getDescription(), 'has_icon' => $addon_scheme->hasIcon());
            }
        }
    }
    if (!empty($params['q'])) {
        foreach ($addons as $addon => $addon_data) {
            if (!preg_match('/' . preg_quote($params['q'], '/') . '/ui', $addon_data['name'], $m)) {
                unset($addons[$addon]);
            }
        }
    }
    $addons = fn_sort_array_by_key($addons, 'name', SORT_ASC);
    return array($addons, $params);
}
Пример #11
0
 /**
  * @param ExSimpleXmlElement $parent
  * @param $grids
  * @param array  $except_fields
  * @param string $lang_code
  */
 private function _buildGridStructure(&$parent, $grids, $except_fields = array(), $lang_code = DESCR_SL)
 {
     $except_block_fields = array_flip(array('block_id', 'snapping_id', 'grid_id', 'company_id'));
     foreach ($grids as $grid) {
         $xml_grid = $parent->addChild('grid');
         $blocks = Block::instance($this->_company_id)->getList(array('?:bm_snapping.*', '?:bm_blocks.*'), array($grid['grid_id']));
         if (!empty($blocks)) {
             $blocks = $blocks[$grid['grid_id']];
         }
         $attrs = array_diff_key($grid, $except_fields);
         foreach ($attrs as $attr => $value) {
             $xml_grid->addAttribute($attr, $value);
         }
         if (!empty($grid['children'])) {
             $grid['children'] = fn_sort_array_by_key($grid['children'], 'grid_id');
             $this->_buildGridStructure($xml_grid, $grid['children'], $except_fields, $lang_code);
         }
         if (!empty($blocks)) {
             $xml_blocks = $xml_grid->addChild('blocks');
             foreach ($blocks as $block_id => $block) {
                 $block_descr = Block::instance($this->_company_id)->getFullDescription($block['block_id']);
                 $block = array_merge(Block::instance($this->_company_id)->getById($block['block_id']), $block);
                 $block = array_diff_key($block, $except_block_fields);
                 $xml_block = $xml_blocks->addChild('block');
                 $this->_buildAttrStructure($xml_block, $block);
                 $xml_translations = $xml_block->addChild('translations');
                 foreach ($block_descr as $_lang_code => $data) {
                     if ($_lang_code == $lang_code) {
                         // We do not needed default language
                         continue;
                     }
                     $xml_translation = $xml_translations->addChildCData('translation', $data['name']);
                     $xml_translation->addAttribute('lang_code', $_lang_code);
                     unset($xml_translation);
                 }
                 $contents = Block::instance($this->_company_id)->getAllContents($block_id);
                 $xml_contents = $xml_block->addChild('contents');
                 foreach ($contents as $content) {
                     if (!empty($content['lang_code']) && $content['lang_code'] == $lang_code) {
                         continue;
                     }
                     if (!empty($content['content'])) {
                         $this->_buildAttrStructure($xml_contents, array('item' => array_diff_key($content, $except_block_fields)));
                     }
                 }
             }
         }
     }
 }
Пример #12
0
    $privileges_data = db_get_array("SELECT a.* FROM ?:privileges as a ORDER BY a.section_id");
    $_preload = array();
    foreach ($privileges_data as $key => $privilege) {
        $section = 'privilege_sections.' . $privilege['section_id'];
        if (!in_array($section, $_preload)) {
            $_preload[] = $section;
        }
        $_preload[] = 'privileges.' . $privilege['privilege'];
    }
    fn_preload_lang_vars($_preload);
    $_sections = array();
    foreach ($privileges_data as $key => $privilege) {
        $_sections[$privilege['section_id']] = __('privilege_sections.' . $privilege['section_id']);
        $privileges_data[$key]['description'] = __('privileges.' . $privilege['privilege']);
    }
    $privileges_data = fn_sort_array_by_key($privileges_data, 'description');
    asort($_sections);
    $privileges = array_fill_keys(array_keys($_sections), array());
    foreach ($privileges_data as $privilege) {
        $privilege['section'] = $_sections[$privilege['section_id']];
        $privileges[$privilege['section_id']][] = $privilege;
    }
    Registry::get('view')->assign('usergroup_privileges', $usergroup_privileges);
    Registry::get('view')->assign('usergroup_name', $usergroup_name);
    Registry::get('view')->assign('privileges', $privileges);
    Registry::set('navigation.tabs', $tabs);
} elseif ($mode == 'requests') {
    list($requests, $search) = fn_get_usergroup_requests($_REQUEST, Registry::get('settings.Appearance.admin_orders_per_page'));
    Registry::get('view')->assign('usergroup_requests', $requests);
    Registry::get('view')->assign('search', $search);
}
Пример #13
0
function fn_get_tags($params = array(), $items_per_page = 0)
{
    // Init filter
    $params = LastView::instance()->update('tags', $params);
    $default_params = array('page' => 1, 'items_per_page' => $items_per_page);
    /**
     * Change parameters for getting tags
     *
     * @param array $params Params list
     * @param int $items_per_page Tags per page
     * @param array $default_params Default params
     */
    fn_set_hook('get_tags_pre', $params, $items_per_page, $default_params);
    $params = array_merge($default_params, $params);
    $fields = array('?:tags.tag_id', '?:tag_links.object_id', '?:tag_links.object_type', '?:tags.tag', '?:tags.status', 'COUNT(?:tag_links.tag_id) as popularity');
    $joins = array('LEFT JOIN ?:tag_links ON ?:tag_links.tag_id = ?:tags.tag_id');
    $conditions = fn_tags_build_conditions($params);
    // Define sort fields
    $sortings = array('tag' => '?:tags.tag', 'status' => '?:tags.status', 'popularity' => 'popularity', 'users' => 'users');
    $sorting = db_sort($params, $sortings, 'tag', 'asc');
    $group = 'GROUP BY ?:tags.tag_id';
    /**
     * Gets tags
     *
     * @param array $params Params list
     * @param int $items_per_page Tags per page
     * @param array $fields List of SQL fields to be selected in an SQL-query
     * @param array $joins List of strings with the complete JOIN information (JOIN type, tables and fields) for an SQL-query
     * @param string $conditions String containing the SQL-query conditions prepended with a logical operator (AND or OR)
     * @param string $group String containing the SQL-query GROUP BY field
     * @param string $sorting String containing the SQL-query ORDER BY field
     */
    fn_set_hook('get_tags', $params, $items_per_page, $fields, $joins, $conditions, $group, $sorting);
    $limit = '';
    if (!empty($params['limit'])) {
        $limit = db_quote(' LIMIT 0, ?i', $params['limit']);
    } elseif (!empty($params['items_per_page'])) {
        $params['total_items'] = db_get_field("SELECT COUNT(DISTINCT(?:tags.tag_id)) FROM ?:tags LEFT JOIN ?:tag_links ON ?:tags.tag_id = ?:tag_links.tag_id WHERE 1 ?p", $conditions);
        $limit = db_paginate($params['page'], $params['items_per_page'], $params['total_items']);
    }
    $tags = db_get_hash_array("SELECT " . implode(', ', $fields) . " " . "FROM ?:tags " . implode(' ', $joins) . " WHERE 1 ?p {$group} {$sorting} {$limit}", 'tag_id', $conditions);
    if (!empty($params['count_objects'])) {
        $objs = db_get_array("SELECT tag_id, COUNT(DISTINCT(object_id)) as count, object_type " . "FROM ?:tag_links WHERE tag_id IN (?n) GROUP BY tag_id, object_type", array_keys($tags));
        foreach ($objs as $v) {
            $tags[$v['tag_id']]['objects_count'][$v['object_type']] = $v['count'];
        }
    }
    // Generate popularity level
    foreach ($tags as $k => $v) {
        $level = ceil(log($v['popularity']));
        $tags[$k]['level'] = $level > TAGS_MAX_LEVEL ? TAGS_MAX_LEVEL : $level;
    }
    if (!empty($params['sort_popular'])) {
        $tags = fn_sort_array_by_key($tags, 'tag', SORT_ASC);
    }
    /**
     * Change tags
     *
     * @param array $params Params list
     * @param int $items_per_page Tags per page
     * @param array $tags Tags
     */
    fn_set_hook('get_tags_post', $params, $items_per_page, $tags);
    return array($tags, $params);
}
Пример #14
0
 private function _checkStoreCost($stores)
 {
     $pickup_surcharge = 0;
     $check = false;
     $group_key = $this->_shipping_info['keys']['group_key'];
     $shipping_id = $this->_shipping_info['keys']['shipping_id'];
     if (!empty($_SESSION['cart']['select_store'])) {
         $select_store = $_SESSION['cart']['select_store'];
         if (!empty($select_store[$group_key][$shipping_id])) {
             $store_id = $select_store[$group_key][$shipping_id];
             if (!empty($stores[$store_id]['pickup_surcharge'])) {
                 $pickup_surcharge = $stores[$store_id]['pickup_surcharge'];
             } else {
                 $check = true;
             }
         } elseif (!empty($stores)) {
             $check = true;
         }
     } else {
         $check = true;
     }
     if ($check) {
         $stores = fn_sort_array_by_key($stores, 'pickup_surcharge');
         $first = array_shift($stores);
         $pickup_surcharge = $first['pickup_surcharge'];
         $_SESSION['cart']['select_store'][$group_key][$shipping_id] = $first['store_location_id'];
         $_SESSION['cart']['pickup_surcharge'][$group_key][$shipping_id] = $pickup_surcharge;
     }
     return $pickup_surcharge;
 }
Пример #15
0
 /**
  * Sorts menu items by position field
  * @param  array $menu menu items
  * @return array sorted menu items
  */
 private function _sort($menu)
 {
     return fn_sort_array_by_key($menu, 'position', SORT_ASC);
 }
Пример #16
0
function fn_twg_api_get_product_options($product, $lang_code = CART_LANGUAGE)
{
    $condition = $_status = $join = '';
    $extra_variant_fields = '';
    $option_ids = $variants_ids = $options = array();
    $_status .= " AND status = 'A'";
    $product_ids = $product['product_id'];
    $join = db_quote(" LEFT JOIN ?:product_options_descriptions as b\n         ON a.option_id = b.option_id AND b.lang_code = ?s ", $lang_code);
    $fields = "a.*, b.option_name, b.option_text, b.description, b.inner_hint, b.incorrect_message, b.comment";
    if (!empty($product_ids)) {
        $_options = db_get_hash_multi_array("SELECT " . $fields . " FROM ?:product_options as a " . $join . " WHERE a.product_id IN (?n)" . $condition . $_status . " ORDER BY a.position", array('product_id', 'option_id'), $product_ids);
        $fields = "c.product_id AS cur_product_id, a.*, " . "b.option_name, b.option_text, b.description, b.inner_hint, " . "b.incorrect_message, b.comment";
        $global_options = db_get_hash_multi_array("SELECT {$fields}" . " FROM ?:product_options as a" . " LEFT JOIN ?:product_options_descriptions as b ON a.option_id = b.option_id AND b.lang_code = ?s" . " LEFT JOIN ?:product_global_option_links as c ON c.option_id = a.option_id" . " WHERE c.product_id IN (?n) AND a.product_id = 0" . $condition . $_status . " ORDER BY a.position", array('cur_product_id', 'option_id'), $lang_code, $product_ids);
        foreach ((array) $product_ids as $product_id) {
            $_opts = (empty($_options[$product_id]) ? array() : $_options[$product_id]) + (empty($global_options[$product_id]) ? array() : $global_options[$product_id]);
            $options[$product_id] = fn_sort_array_by_key($_opts, 'position');
        }
    } else {
        //we need a separate query for global options
        $options = db_get_hash_multi_array("SELECT a.*, b.option_name, b.option_text, b.description, b.inner_hint, b.incorrect_message, b.comment" . " FROM ?:product_options as a" . $join . " WHERE a.product_id = 0" . $condition . $_status . " ORDER BY a.position", array('product_id', 'option_id'));
    }
    foreach ($options as $product_id => $_options) {
        $option_ids = array_merge($option_ids, array_keys($_options));
    }
    if (empty($option_ids)) {
        if (is_array($product_ids)) {
            return $options;
        } else {
            return !empty($options[$product_ids]) ? $options[$product_ids] : array();
        }
    }
    $_status = " AND a.status='A'";
    $v_fields = "a.variant_id, a.option_id, a.position, a.modifier, " . "a.modifier_type, a.weight_modifier, a.weight_modifier_type, " . "{$extra_variant_fields} b.variant_name";
    $v_join = db_quote("LEFT JOIN ?:product_option_variants_descriptions as b\n         ON a.variant_id = b.variant_id\n         AND b.lang_code = ?s", $lang_code);
    $v_condition = db_quote("a.option_id IN (?n) {$_status}", array_unique($option_ids));
    $v_sorting = "a.position, a.variant_id";
    $variants = db_get_hash_multi_array("SELECT {$v_fields} FROM ?:product_option_variants as a {$v_join} WHERE {$v_condition} ORDER BY {$v_sorting}", array('option_id', 'variant_id'));
    foreach ($variants as $option_id => $_variants) {
        $variants_ids = array_merge($variants_ids, array_keys($_variants));
    }
    if (isset($variants_ids) && empty($variants_ids)) {
        return is_array($product_ids) ? $options : $options[$product_ids];
    }
    $image_pairs = fn_get_image_pairs(array_unique($variants_ids), 'variant_image', 'V', true, true, $lang_code);
    foreach ($variants as $option_id => &$_variants) {
        foreach ($_variants as $variant_id => &$_variant) {
            $_variant['image_pair'] = !empty($image_pairs[$variant_id]) ? reset($image_pairs[$variant_id]) : array();
        }
    }
    foreach ($options as $product_id => &$_options) {
        foreach ($_options as $option_id => &$_option) {
            // Add variant names manually, if this option is "checkbox"
            if ($_option['option_type'] == 'C' && !empty($variants[$option_id])) {
                foreach ($variants[$option_id] as $variant_id => $variant) {
                    $variants[$option_id][$variant_id]['variant_name'] = $variant['position'] == 0 ? __('no') : __('yes');
                }
            }
            $_option['variants'] = !empty($variants[$option_id]) ? $variants[$option_id] : array();
        }
    }
    return is_array($product_ids) ? $options : $options[$product_ids];
}
Пример #17
0
     foreach ($sct as $k => $v) {
         // Get variants list
         $func = 'fn_settings_variants_' . strtolower($v['section_id']) . '_' . (!empty($v['subsection_id']) ? $v['subsection_id'] . '_' : '') . $v['option_name'];
         if (function_exists($func)) {
             $options[$sid][$k]['variants'] = $func();
             $options[$sid][$k]['userfunc'] = true;
         } elseif (strstr('SRMN', $v['option_type'])) {
             $options[$sid][$k]['variants'] = db_get_hash_single_array("SELECT ?:settings_variants.*, ?:settings_descriptions.description FROM ?:settings_variants ?p WHERE  ?:settings_variants.option_id = ?i ORDER BY ?:settings_variants.position", array('variant_name', 'description'), $descr, $v['option_id']);
             $options[$sid][$k]['dev_variants'] = db_get_hash_array("SELECT ?:settings_variants.*, ?:settings_descriptions.description FROM ?:settings_variants ?p WHERE  ?:settings_variants.option_id = ?i ORDER BY ?:settings_variants.position", 'variant_name', $descr, $v['option_id']);
         }
         if ($v['option_type'] == 'M' || $v['option_type'] == 'N' || $v['option_type'] == 'G') {
             parse_str($v['value'], $options[$sid][$k]['value']);
         }
     }
     $options[$sid] = fn_array_merge($options[$sid], $elements, false);
     $options[$sid] = fn_sort_array_by_key($options[$sid], 'position');
 }
 // [Page sections]
 if (isset($options['main'])) {
     Registry::set('navigation.tabs.main', array('title' => fn_get_lang_var('main'), 'js' => true));
 }
 foreach ($subsections as $k => $v) {
     Registry::set('navigation.tabs.' . $k, array('title' => $v['description'], 'js' => true));
 }
 $view->assign('notabs', isset($options['main']) && sizeof($subsections) < 1 || !isset($options['main']) && sizeof($subsections) <= 1 ? 'Y' : 'N');
 // [/Page sections]
 $section_columns = 3;
 // number of columns with sections links (in the frontend)
 $section_cols = fn_split($sections, $section_columns);
 $view->assign('options', @$options);
 $view->assign('sections', @$sections);
Пример #18
0
 /**
  * Sorts grids by order parameter
  *
  * @param  array $grids Hierarchic builded tree
  * @return array Sorted grids
  */
 public static function sortGrids($grids)
 {
     $static_grids = array();
     foreach ($grids as $id => $grid) {
         if ($grid['order'] == 0) {
             $static_grids[] = $id;
         }
         if (!empty($grid['children'])) {
             $grid['children'] = self::sortGrids($grid['children']);
         }
         $grids[$id] = $grid;
     }
     $grids = fn_sort_array_by_key($grids, 'order', SORT_ASC);
     $sorted_grids = array();
     foreach ($static_grids as $grid_id) {
         $sorted_grids += array($grid_id => $grids[$grid_id]);
         unset($grids[$grid_id]);
     }
     $sorted_grids += $grids;
     return $sorted_grids;
 }
Пример #19
0
/**
 * Gets addons list
 *
 * @param array $params search params
 * @param int $items_per_page items per page for pagination
 * @param string $lang_code language code
 * @return array addons list and filtered search params
 */
function fn_get_addons($params, $items_per_page = 0, $lang_code = CART_LANGUAGE)
{
    $params = LastView::instance()->update('addons', $params);
    $addons_counter = array('installed' => 0, 'activated' => 0, 'core' => 0, 'other' => 0);
    $default_params = array('type' => 'any');
    $params = array_merge($default_params, $params);
    $addons = array();
    $sections = Settings::instance()->getAddons();
    $all_addons = fn_get_dir_contents(Registry::get('config.dir.addons'), true, false);
    $installed_addons = db_get_hash_array('SELECT a.addon, a.status, b.name as name, b.description as description, a.separate, a.unmanaged, a.has_icon, a.install_datetime ' . 'FROM ?:addons as a LEFT JOIN ?:addon_descriptions as b ON b.addon = a.addon AND b.lang_code = ?s' . 'ORDER BY b.name ASC', 'addon', $lang_code);
    $addons_counter['installed'] = count($installed_addons);
    foreach ($installed_addons as $key => $addon) {
        $installed_addons[$key]['has_sections'] = Settings::instance()->sectionExists($sections, $addon['addon']);
        $installed_addons[$key]['has_options'] = $installed_addons[$key]['has_sections'] ? Settings::instance()->optionsExists($addon['addon'], 'ADDON') : false;
        // Check add-on snaphot
        if (!fn_check_addon_snapshot($key)) {
            $installed_addons[$key]['status'] = 'D';
            $installed_addons[$key]['snapshot_correct'] = false;
        } else {
            $installed_addons[$key]['snapshot_correct'] = true;
        }
        if ($installed_addons[$key]['status'] == 'A') {
            $addons_counter['activated']++;
        }
    }
    foreach ($all_addons as $addon) {
        $addon_scheme = SchemesManager::getScheme($addon);
        // skip addons with broken or missing scheme
        if (!$addon_scheme) {
            continue;
        }
        if ($addon_scheme->isCoreAddon()) {
            $addons_counter['core']++;
        } else {
            $addons_counter['other']++;
        }
        if (in_array($params['type'], array('any', 'installed', 'active', 'disabled'))) {
            $search_status = $params['type'] == 'active' ? 'A' : ($params['type'] == 'disabled' ? 'D' : '');
            if (!empty($installed_addons[$addon])) {
                // exclude unmanaged addons from the list
                if ($installed_addons[$addon]['unmanaged'] == true) {
                    continue;
                }
                if (!empty($search_status) && $installed_addons[$addon]['status'] != $search_status) {
                    continue;
                }
                $addons[$addon] = $installed_addons[$addon];
                $addons[$addon]['supplier'] = $addon_scheme->getSupplier();
                $addons[$addon]['supplier_link'] = $addon_scheme->getSupplierLink();
                $addons[$addon]['version'] = $addon_scheme->getVersion();
                $addons[$addon]['is_core_addon'] = $addon_scheme->isCoreAddon();
                $addons[$addon]['delete_url'] = '';
                $addons[$addon]['url'] = fn_url("addons.update?addon={$addon}&return_url=" . urlencode(Registry::get('config.current_url')));
                if (!Registry::get('runtime.company_id')) {
                    $addons[$addon]['delete_url'] = fn_url("addons.uninstall?addon={$addon}&redirect_url=" . urlencode(Registry::get('config.current_url')));
                }
                if ($addon_scheme != false && !$addon_scheme->getUnmanaged()) {
                    $addons[$addon]['originals'] = $addon_scheme->getOriginals();
                }
                fn_update_lang_objects('installed_addon', $addons[$addon]);
                if (is_file(Registry::get('config.dir.addons') . $addon . '/func.php')) {
                    require_once Registry::get('config.dir.addons') . $addon . '/func.php';
                    if (is_file(Registry::get('config.dir.addons') . $addon . '/config.php')) {
                        require_once Registry::get('config.dir.addons') . $addon . '/config.php';
                    }
                    // Generate custom description
                    $func = 'fn_addon_dynamic_description_' . $addon;
                    if (function_exists($func)) {
                        $addons[$addon]['description'] = $func($addons[$addon]['description']);
                    }
                    //Generate custom url
                    $url_func = 'fn_addon_dynamic_url_' . $addon;
                    if (function_exists($url_func)) {
                        list($addons[$addon]['url'], $addons[$addon]['delete_url']) = $url_func($addons[$addon]['url'], $addons[$addon]['delete_url']);
                    }
                }
            }
        }
        if (empty($installed_addons[$addon]) && empty($params['for_company']) && in_array($params['type'], array('any', 'not_installed'))) {
            if ($addon_scheme != false && !$addon_scheme->getUnmanaged()) {
                $addons[$addon] = array('status' => 'N', 'name' => $addon_scheme->getName(), 'version' => $addon_scheme->getVersion(), 'supplier' => $addon_scheme->getSupplier(), 'supplier_link' => $addon_scheme->getSupplierLink(), 'snapshot_correct' => fn_check_addon_snapshot($addon), 'description' => $addon_scheme->getDescription(), 'has_icon' => $addon_scheme->hasIcon(), 'is_core_addon' => $addon_scheme->isCoreAddon(), 'install_datetime' => null);
            }
        }
    }
    if (!empty($params['q'])) {
        foreach ($addons as $addon => $addon_data) {
            if (!preg_match('/' . preg_quote($params['q'], '/') . '/ui', $addon_data['name'] . $addon_data['supplier'], $m)) {
                unset($addons[$addon]);
            }
        }
    }
    if (!empty($params['source'])) {
        $is_core_addon = $params['source'] == 'core';
        foreach ($addons as $addon => $addon_data) {
            if ($is_core_addon != $addon_data['is_core_addon']) {
                unset($addons[$addon]);
            }
        }
    }
    $addons = fn_sort_array_by_key($addons, 'name', SORT_ASC);
    return array($addons, $params, $addons_counter);
}
Пример #20
0
} elseif ($mode == 'manage') {
    $all_addons = fn_get_dir_contents(DIR_ADDONS, true, false);
    $installed_addons = db_get_hash_array("SELECT a.addon, a.status, d.description as name, b.description, LENGTH(a.options) as has_options, d.object_id, d.object_type FROM ?:addons as a LEFT JOIN ?:addon_descriptions as d ON d.addon = a.addon AND d.object_id = '' AND d.object_type = 'A' AND d.lang_code = ?s LEFT JOIN ?:addon_descriptions as b ON b.addon = a.addon AND b.object_id = '' AND b.object_type = 'D' AND b.lang_code = ?s ORDER BY d.description ASC", 'addon', CART_LANGUAGE, CART_LANGUAGE);
    fn_update_lang_objects('installed_addons', $installed_addons);
    $addons_list = array();
    foreach ($all_addons as $addon) {
        if (!empty($installed_addons[$addon])) {
            $addons_list[$addon] = $installed_addons[$addon];
        } else {
            if (file_exists(DIR_ADDONS . $addon . '/addon.xml')) {
                $xml = simplexml_load_file(DIR_ADDONS . $addon . '/addon.xml');
                $addons_list[$addon] = array('status' => 'N', 'name' => (string) $xml->name);
            }
        }
    }
    $view->assign('addons_list', fn_sort_array_by_key($addons_list, 'name', SORT_ASC));
}
/**
 * Update addon options
 *
 * @param string $addon addon to update options for
 * @param string $addon_data options data
 * @return bool always true
 */
function fn_update_addon($addon, $addon_data)
{
    fn_get_schema('settings', 'actions', 'php', false, true);
    // Get old options
    $old_options = db_get_field("SELECT options FROM ?:addons WHERE addon = ?s", $addon);
    $old_options = fn_parse_addon_options($old_options);
    $ml_options = db_get_hash_single_array("SELECT object_id, description FROM ?:addon_descriptions WHERE addon = ?s AND object_id != '' AND object_type = 'L' AND lang_code = ?s", array('object_id', 'description'), $addon, CART_LANGUAGE);
Пример #21
0
 /**
  * Returns all block types
  * @static
  * @param  string $lang_code 2 letter language code
  * @return array  List of block types with name, icon and type
  */
 public static function getBlockTypes($lang_code = CART_LANGUAGE)
 {
     $scheme = self::_getScheme('blocks');
     $types = array();
     foreach ($scheme as $type => $params) {
         $types[$type] = array('type' => $type, 'name' => __('block_' . $type, '', $lang_code), 'icon' => '/media/images/block_manager/block_icons/default.png');
         if (!empty($params['icon'])) {
             $types[$type]['icon'] = $params['icon'];
         }
     }
     $types = fn_sort_array_by_key($types, 'name');
     return $types;
 }
Пример #22
0
/**
 * Set hook to use by addons
 *
 * @param mixed $argN argument, passed to addon
 * @return boolean always true
 */
function fn_set_hook($hook_name = NULL, &$arg1 = NULL, &$arg2 = NULL, &$arg3 = NULL, &$arg4 = NULL, &$arg5 = NULL, &$arg6 = NULL, &$arg7 = NULL, &$arg8 = NULL, &$arg9 = NULL, &$arg10 = NULL, &$arg11 = NULL, &$arg12 = NULL, &$arg13 = NULL, &$arg14 = NULL, &$arg15 = NULL)
{
    static $callable_functions;
    static $hooks_already_sorted;
    static $edition_acronym;
    static $hooks = null;
    static $addons_initiated = false;
    if (!$addons_initiated) {
        $addons_initiated = Registry::get('addons_initiated');
    }
    if ($hooks === null || !$addons_initiated || defined('DISABLE_HOOK_CACHE')) {
        $hooks = Registry::get('hooks');
    }
    $arg_count = func_num_args();
    if ($arg_count === 1) {
        $args = array();
    } elseif ($arg_count === 2) {
        $args = array(&$arg1);
    } elseif ($arg_count === 3) {
        $args = array(&$arg1, &$arg2);
    } elseif ($arg_count === 4) {
        $args = array(&$arg1, &$arg2, &$arg3);
    } elseif ($arg_count === 5) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4);
    } elseif ($arg_count === 6) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5);
    } elseif ($arg_count === 7) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6);
    } elseif ($arg_count === 8) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7);
    } elseif ($arg_count === 9) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8);
    } elseif ($arg_count === 10) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9);
    } elseif ($arg_count === 11) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9, &$arg10);
    } elseif ($arg_count === 12) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9, &$arg10, &$arg11);
    } elseif ($arg_count === 13) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9, &$arg10, &$arg11, &$arg12);
    } elseif ($arg_count === 14) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9, &$arg10, &$arg11, &$arg12, &$arg13);
    } elseif ($arg_count === 15) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9, &$arg10, &$arg11, &$arg12, &$arg13, &$arg14);
    } elseif ($arg_count === 16) {
        $args = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, &$arg7, &$arg8, &$arg9, &$arg10, &$arg11, &$arg12, &$arg13, &$arg14, &$arg15);
    }
    // Check for the core functions
    if (is_callable('fn_core_' . $hook_name)) {
        call_user_func_array('fn_core_' . $hook_name, $args);
    }
    $edition_acronym = fn_get_edition_acronym(PRODUCT_EDITION);
    if (!empty($edition_acronym) && function_exists("fn_{$edition_acronym}_{$hook_name}")) {
        call_user_func_array("fn_{$edition_acronym}_{$hook_name}", $args);
    }
    if (isset($hooks[$hook_name])) {
        // cache hooks sorting
        if (!isset($hooks_already_sorted[$hook_name])) {
            $hooks[$hook_name] = fn_sort_array_by_key($hooks[$hook_name], 'priority');
            $hooks_already_sorted[$hook_name] = true;
            Registry::set('hooks', $hooks, true);
        }
        foreach ($hooks[$hook_name] as $callback) {
            // cache if hook function callable
            if (!isset($callable_functions[$callback['func']])) {
                if (!is_callable($callback['func'])) {
                    throw new DeveloperException("Hook {$callback['func']} is not callable");
                }
                $callable_functions[$callback['func']] = true;
            }
            call_user_func_array($callback['func'], $args);
        }
    }
    return true;
}
Пример #23
0
function fn_get_tags($params = array(), $items_per_page = 0)
{
    // Init filter
    $params = fn_init_view('tags', $params);
    $default_params = array('page' => 1);
    $params = array_merge($default_params, $params);
    // Define sort fields
    $sortings = array('tag' => '?:tags.tag', 'status' => '?:tags.status', 'popularity' => 'popularity', 'users' => 'users');
    $directions = array('asc' => 'asc', 'desc' => 'desc');
    $conditions = '';
    if (!empty($params['see']) && $params['see'] == 'my' && empty($params['user_id'])) {
        return array(array(), array());
    }
    if (!empty($params['object_type'])) {
        $conditions .= db_quote(" AND ?:tag_links.object_type = ?s", $params['object_type']);
    }
    if (!empty($params['status'])) {
        $conditions .= db_quote(" AND ?:tags.status IN (?a)", $params['status']);
    }
    if (!empty($params['object_id'])) {
        $conditions .= db_quote(" AND ?:tag_links.object_id = ?s", $params['object_id']);
    }
    if (!empty($params['user_id'])) {
        $conditions .= db_quote(" AND ?:tag_links.user_id = ?s", $params['user_id']);
    }
    if (isset($params['tag']) && fn_string_no_empty($params['tag'])) {
        $conditions .= db_quote(" AND ?:tags.tag LIKE ?l", "%" . trim($params['tag']) . "%");
    }
    $my_tag_field = '';
    if (!empty($params['user_and_popular'])) {
        $conditions .= db_quote(" AND IF(?:tag_links.user_id = ?i, 1, ?:tags.status IN ('A'))", $params['user_and_popular']);
        $my_tag_field = db_quote(", COUNT(IF(?:tag_links.user_id = ?i, 1, NULL)) as my_tag", $params['user_and_popular']);
    }
    if (!empty($params['period']) && $params['period'] != 'A') {
        list($params['time_from'], $params['time_to']) = fn_create_periods($params);
        $conditions .= db_quote(" AND (?:tags.timestamp >= ?i AND ?:tags.timestamp <= ?i)", $params['time_from'], $params['time_to']);
    }
    $limit = '';
    if (!empty($params['limit'])) {
        $limit = db_quote(' LIMIT 0, ?i', $params['limit']);
    }
    if (!empty($items_per_page)) {
        $total = db_get_field("SELECT COUNT(DISTINCT(?:tags.tag_id)) FROM ?:tags LEFT JOIN ?:tag_links ON ?:tags.tag_id = ?:tag_links.tag_id WHERE 1 ?p", $conditions);
        $limit = fn_paginate($params['page'], $total, $items_per_page);
    }
    if (empty($params['sort_order']) || empty($directions[$params['sort_order']])) {
        $params['sort_order'] = 'asc';
    }
    if (empty($params['sort_by']) || empty($sortings[$params['sort_by']])) {
        $params['sort_by'] = 'tag';
    }
    $sorting = $sortings[$params['sort_by']] . ' ' . $directions[$params['sort_order']];
    // Reverse sorting (for usage in view)
    $params['sort_order'] = $params['sort_order'] == 'asc' ? 'desc' : 'asc';
    $tags = db_get_hash_array("SELECT ?:tags.tag_id, ?:tag_links.object_id, ?:tag_links.object_type, ?:tag_links.user_id, COUNT(?:tag_links.tag_id) as popularity, COUNT(DISTINCT(?:tag_links.user_id)) as users, ?:tags.tag, ?:tags.status {$my_tag_field} FROM ?:tags LEFT JOIN ?:tag_links ON ?:tag_links.tag_id = ?:tags.tag_id WHERE 1 ?p GROUP BY ?:tags.tag_id ORDER BY {$sorting} {$limit}", 'tag_id', $conditions);
    if (!empty($params['count_objects'])) {
        $objs = db_get_array("SELECT tag_id, COUNT(DISTINCT(object_id)) as count, object_type FROM ?:tag_links WHERE tag_id IN (?n) GROUP BY tag_id, object_type", array_keys($tags));
        foreach ($objs as $v) {
            $tags[$v['tag_id']]['objects_count'][$v['object_type']] = $v['count'];
        }
    }
    // Generate popularity level
    foreach ($tags as $k => $v) {
        $level = ceil(log($v['popularity']));
        $tags[$k]['level'] = $level > TAGS_MAX_LEVEL ? TAGS_MAX_LEVEL : $level;
    }
    if (!empty($params['sort_popular'])) {
        $tags = fn_sort_array_by_key($tags, 'tag', SORT_ASC);
    }
    return array($tags, $params);
}