Exemplo n.º 1
0
/**
 * Convert multi-level array with "subitems" to plain representation
 *
 * @param array $data source array
 * @param string $key key with subitems
 * @param array $result resulting array, passed along multi levels
 * @return array structured data
 */
function fn_multi_level_to_plain($data, $key, $result = array())
{
    foreach ($data as $k => $v) {
        if (!empty($v[$key])) {
            unset($v[$key]);
            array_push($result, $v);
            $result = fn_multi_level_to_plain($data[$k][$key], $key, $result);
        } else {
            array_push($result, $v);
        }
    }
    return $result;
}
Exemplo n.º 2
0
/**
 * Returns list of pages
 * <i>$params</i> - Array of various parameters used for element selection:
 * <ul>
 *      <li>page_id - If defined and not zero, get data for the page with this id; otherwise get data for all pages</li>
 *      <li>item_ids - A comma-delimited page identifiers list; if defined, get data for the pages with these ids; otherwise get data for all pages</li>
 *      <li>except_id - Identifier of the page to be excluded from the result</li>
 *   	<li>parent_id - If defined and not zero, get data for the pages with this parent page id</li>
 *   	<li>active_page_id - Identifier of the page being currently shown</li>
 *   	<li>current_page_id - The same as <i>active_page_id</i></li>
 *
 *   	<li>add_root - If defined, additionally returns root element data</li>
 *  	<li>subpages - If defined, additionally returns subpages</li>
 *  	<li>get_tree - If defined, pages will be returned as tree; otherwise as list. Possible value: <i>plain</i></li>
 *  	<li>visible - For pages tree: show visible branch only</li>
 *
 *  	<li>page - Number of the current page for pagination</li>
 *
 *  	<li>pdescr - If defined, additionally returns descriptions.  Possible value: <i>Y</i></li>
 *
 *   	<li>vendor_pages - If defined, try to return pages for the company defined by <i>company_id</i></li>
 *   	<li>company_id - If <i>vendor_pages</i> is defined: if defined, get data for the company with this id</li>
 *
 *   	<li>neighbours - If defined, try to return neighbor pages for the page with the id <i>neighbours_page_id</i></li>
 *   	<li>neighbours_page_id -  If <i>neighbours</i> is defined: if defined, get neighbor pages for the page with this id</li>
 *
 *   	<li>limit - If defined, used to limit your MySQL query results by this value</li>
 *   	<li>sort_by - Table field to sort by, default is position</li>
 *   	<li>sort_order - Sorting direction, ascending or descending; Possible values: <i>asc</i> or <i>desc</i>, default is <i>asc</i></li>
 *
 *   	<li>status - If defined, returns pages only with this status. Can be comma delimited statuses list</li>
 *
 *  	<li>period - If defined, get pages by time period. Time period is generated by ::fn_create_periods</li>
 *   	<li>time_from - Returns pages created earlier than this time</li>
 *   	<li>time_to - Returns pages created later than this time</li>
 *
 *   	<li>parent_page_id - Deprecated, <i>parent_id</i> used instead</li>
 *   	<li>from_page_id - Deprecated, <i>parent_id</i> used instead</li>
 * </ul>
 * @param array $params Array of params
 * @param int $items_per_page  Limit items per page
 * @param string $lang_code 2-letter language code
 * @return array List of pages, params
 */
function fn_get_pages($params = array(), $items_per_page = 0, $lang_code = CART_LANGUAGE)
{
    /**
     * Changes params for selecting pages
     *
     * @param array  $params         Pages search params
     * @param int    $items_per_page Items per page
     * @param string $lang_code      Two-letter language code (e.g. 'en', 'ru', etc.)
     */
    fn_set_hook('get_pages_pre', $params, $items_per_page, $lang_code);
    $view_type = 'pages';
    if (!empty($params['page_type']) && fn_is_exclusive_page_type($params['page_type'])) {
        $view_type .= '_' . $params['page_type'];
    }
    // Init filter
    $params = LastView::instance()->update($view_type, $params);
    $default_params = array('page_id' => 0, 'page' => 1, 'visible' => false, 'get_tree' => '', 'pdescr' => '', 'subpages' => '', 'match' => '', 'page_type' => '', 'items_per_page' => $items_per_page);
    if (is_array($params)) {
        $params = array_merge($default_params, $params);
    } else {
        $params = $default_params;
    }
    if (empty($params['pname']) && empty($params['pdescr']) && empty($params['subpages'])) {
        $params['pname'] = 'Y';
    }
    $fields = array('?:pages.*');
    if (!empty($params['simple'])) {
        $fields[] = '?:page_descriptions.page';
    } else {
        $fields[] = '?:page_descriptions.*';
    }
    // Define sort fields
    $sortings = array('position' => array('?:pages.position', '?:page_descriptions.page'), 'name' => '?:page_descriptions.page', 'timestamp' => '?:pages.timestamp', 'type' => '?:pages.page_type', 'multi_level' => array('?:pages.parent_id', '?:pages.position', '?:page_descriptions.page'));
    $auth =& $_SESSION['auth'];
    $condition = '1';
    $join = $limit = $group_by = '';
    if (isset($params['q']) && fn_string_not_empty($params['q'])) {
        $params['q'] = trim($params['q']);
        if ($params['match'] == 'any') {
            $pieces = fn_explode(' ', $params['q']);
            $search_type = ' OR ';
        } elseif ($params['match'] == 'all') {
            $pieces = fn_explode(' ', $params['q']);
            $search_type = ' AND ';
        } else {
            $pieces = array($params['q']);
            $search_type = '';
        }
        $_condition = array();
        foreach ($pieces as $piece) {
            if (strlen($piece) == 0) {
                continue;
            }
            $tmp = array();
            if (!empty($params['pname']) && $params['pname'] == 'Y') {
                $tmp[] = db_quote("?:page_descriptions.page LIKE ?l", "%{$piece}%");
                // check search words
            }
            if ($params['pdescr'] == 'Y') {
                $tmp[] = db_quote("?:page_descriptions.description LIKE ?l", "%{$piece}%");
            }
            if (!empty($tmp)) {
                $_condition[] = '(' . implode(' OR ', $tmp) . ')';
            }
        }
        if (!empty($_condition)) {
            $condition .= ' AND (' . implode($search_type, $_condition) . ')';
        }
    }
    $condition .= fn_get_company_condition('?:pages.company_id');
    if (isset($params['parent_id']) && $params['parent_id'] !== '') {
        $p_ids = array();
        if ($params['subpages'] == 'Y') {
            $p_ids = db_get_fields("SELECT a.page_id FROM ?:pages as a LEFT JOIN ?:pages as b ON b.page_id = ?i WHERE a.id_path LIKE CONCAT(b.id_path, '/%')", $params['parent_id']);
        }
        $p_ids[] = $params['parent_id'];
        $condition .= db_quote(" AND ?:pages.parent_id IN (?n)", $p_ids);
    }
    if (isset($params['parent_page_id'])) {
        // set parent id, that was set in block properties
        $params['from_page_id'] = $params['parent_page_id'];
    }
    if (!empty($params['from_page_id'])) {
        $from_id_path = db_get_field("SELECT id_path FROM ?:pages WHERE page_id = ?i", $params['from_page_id']);
        $condition .= db_quote(" AND ?:pages.id_path LIKE ?l", "{$from_id_path}/%");
    }
    if (!empty($params['status'])) {
        $condition .= db_quote(" AND ?:pages.status IN (?a)", $params['status']);
    }
    if (!empty($params['vendor_pages']) && empty($params['company_id'])) {
        return array(array(), $params);
    } elseif (!empty($params['company_id'])) {
        $condition .= db_quote(" AND ?:pages.company_id = ?i", $params['company_id']);
    }
    if (empty($params['full_search'])) {
        $condition .= db_quote(" AND ?:pages.page_type IN (?a)", array_keys(fn_get_page_type_filter($params['page_type'])));
    }
    if (!empty($params['visible'])) {
        // for pages tree: show visible branch only
        $page_ids = array();
        if (!empty($params['current_page_id'])) {
            $cur_id_path = db_get_field("SELECT id_path FROM ?:pages WHERE page_id = ?i", $params['current_page_id']);
            if (!empty($cur_id_path)) {
                $page_ids = explode('/', $cur_id_path);
            }
        }
        if (!empty($from_id_path)) {
            $_page_ids = explode('/', $from_id_path);
            $page_ids = array_merge($page_ids, $_page_ids);
            $page_ids = array_unique($page_ids);
        }
        $page_ids[] = $params['page_id'];
        $condition .= db_quote(" AND ?:pages.parent_id IN (?n)", $page_ids);
    }
    if (!empty($params['period']) && $params['period'] != 'A') {
        list($params['time_from'], $params['time_to']) = fn_create_periods($params);
        $condition .= db_quote(" AND (?:pages.timestamp >= ?i AND ?:pages.timestamp <= ?i)", $params['time_from'], $params['time_to']);
    }
    if (!empty($params['item_ids'])) {
        // get only defined pages
        $condition .= db_quote(" AND ?:pages.page_id IN (?n)", explode(',', $params['item_ids']));
    }
    if (!empty($params['except_id']) && (empty($params['item_ids']) || !empty($params['item_ids']) && !in_array($params['except_id'], explode(',', $params['item_ids'])))) {
        $condition .= db_quote(' AND ?:pages.page_id != ?i AND ?:pages.parent_id != ?i', $params['except_id'], $params['except_id']);
    }
    if (AREA != 'A') {
        $condition .= " AND (" . fn_find_array_in_set($auth['usergroup_ids'], '?:pages.usergroup_ids', true) . ")";
        $condition .= fn_get_localizations_condition('?:pages.localization', true);
        $condition .= db_quote(" AND (use_avail_period = ?s OR (use_avail_period = ?s AND avail_from_timestamp <= ?i AND avail_till_timestamp >= ?i))", 'N', 'Y', TIME, TIME);
    }
    $join = db_quote('LEFT JOIN ?:page_descriptions ON ?:pages.page_id = ?:page_descriptions.page_id AND ?:page_descriptions.lang_code = ?s', $lang_code);
    if (!empty($params['limit'])) {
        $limit = db_quote(" LIMIT 0, ?i", $params['limit']);
    }
    if (!empty($params['neighbours'])) {
        $parent_ids = array();
        if (!empty($params['neighbours_page_id'])) {
            $id_path = db_get_field("SELECT id_path FROM ?:pages WHERE page_id = ?i", $params['neighbours_page_id']);
            $parent_ids = explode('/', $id_path);
            if (count($parent_ids) == 1) {
                array_unshift($parent_ids, 0);
            }
            $params['root_id'] = $parent_ids[0];
        } else {
            $parent_ids[] = 0;
        }
        $condition .= db_quote(" AND ?:pages.parent_id IN (?n)", array_unique($parent_ids));
    }
    fn_set_hook('get_pages', $params, $join, $condition, $fields, $group_by, $sortings, $lang_code);
    if (!empty($params['get_tree'])) {
        $params['sort_by'] = 'multi_level';
    }
    $sorting = db_sort($params, $sortings, 'position', 'asc');
    if (!empty($group_by)) {
        $group_by = ' GROUP BY ' . $group_by;
    }
    // Get search conditions
    if (!empty($params['get_conditions'])) {
        return array($fields, $join, $condition);
    }
    if (!empty($params['items_per_page'])) {
        $params['total_items'] = db_get_field("SELECT COUNT(DISTINCT(?:pages.page_id)) FROM ?:pages ?p WHERE ?p ?p ?p", $join, $condition, $group_by, $sorting);
        $limit = db_paginate($params['page'], $params['items_per_page'], $params['total_items']);
    }
    $pages = db_get_hash_array("SELECT " . implode(', ', $fields) . " FROM ?:pages ?p WHERE ?p ?p ?p ?p", 'page_id', $join, $condition, $group_by, $sorting, $limit);
    if (!empty($params['active_page_id']) && !empty($pages[$params['active_page_id']])) {
        $pages[$params['active_page_id']]['active'] = true;
        Registry::set('runtime.active_page_ids', explode('/', $pages[$params['active_page_id']]['id_path']));
    }
    if (!empty($pages)) {
        foreach ($pages as $k => $v) {
            $pages[$k]['level'] = substr_count($v['id_path'], '/');
        }
        if (!empty($params['get_tree'])) {
            $delete_keys = array();
            foreach ($pages as $k => $v) {
                if (!empty($v['parent_id']) && !empty($pages[$v['parent_id']])) {
                    $pages[$v['parent_id']]['subpages'][$v['page_id']] =& $pages[$k];
                    $delete_keys[] = $k;
                }
                if (!empty($v['parent_id']) && (!isset($params['root_id']) && empty($pages[$v['parent_id']]) || isset($params['root_id']) && $v['parent_id'] != $params['root_id']) && (empty($params['from_page_id']) || $params['from_page_id'] != $v['parent_id'])) {
                    // delete pages that don't have parent. FIXME: should be done on database layer
                    $delete_keys[] = $k;
                }
            }
            foreach ($delete_keys as $k) {
                unset($pages[$k]);
            }
        } elseif (!empty($params['item_ids'])) {
            $pages = fn_sort_by_ids($pages, explode(',', $params['item_ids']), 'page_id');
        }
        if ($params['get_tree'] == 'plain') {
            $pages = fn_multi_level_to_plain($pages, 'subpages');
        }
        if (!empty($params['get_children_count'])) {
            $where_condition = !empty($params['except_id']) ? db_quote(' AND page_id != ?i', $params['except_id']) : '';
            if ($params['get_tree'] == 'plain') {
                $_page_ids = array();
                foreach ($pages as $_p) {
                    $_page_ids[] = $_p['page_id'];
                }
            } else {
                $_page_ids = array_keys($pages);
            }
            $children = db_get_hash_single_array("SELECT parent_id, COUNT(page_id) as children FROM ?:pages WHERE parent_id IN (?n) ?p GROUP BY parent_id", array('parent_id', 'children'), $_page_ids, $where_condition);
            if (!empty($children)) {
                if ($params['get_tree'] == 'plain') {
                    foreach ($pages as $_id => $_p) {
                        if (!empty($children[$_p['page_id']])) {
                            $pages[$_id]['has_children'] = true;
                        }
                    }
                } else {
                    foreach ($children as $k => $v) {
                        $pages[$k]['has_children'] = !empty($v);
                    }
                }
            }
        }
    }
    if (!empty($params['add_root'])) {
        array_unshift($pages, array('page_id' => '', 'page' => $params['add_root']));
    }
    fn_dropdown_appearance_cut_second_third_levels($pages, 'subpages', $params);
    fn_set_hook('post_get_pages', $pages, $params, $lang_code);
    LastView::instance()->processResults($view_type, $pages, $params);
    return array($pages, $params);
}
Exemplo n.º 3
0
/**
 * Gets categories tree beginning from category identifier defined in params or root category
 * @param array $params Categories search params
 *      category_id - Root category identifier
 *      visible - Flag that defines if only visible categories should be included
 *      current_category_id - Identifier of current node for visible categories
 *      simple - Flag that defines if category path should be getted as set of category IDs
 *      plain - Flag that defines if continues list of categories should be returned
 *      --------------------------------------
 *      Examples:
 *      Gets whole categories tree:
 *      fn_get_categories()
 *      --------------------------------------
 *      Gets subcategories tree of the category:
 *      fn_get_categories(array(
 *          'category_id' => 123
 *      ))
 *      --------------------------------------
 *      Gets all first-level nodes of the category
 *      fn_get_categories(array(
 *          'category_id' => 123,
 *          'visible' => true
 *      ))
 *      --------------------------------------
 *      Gets all visible nodes of the category, start from the root
 *      fn_get_categories(array(
 *          'category_id' => 0,
 *          'current_category_id' => 234,
 *          'visible' => true
 *      ))
 * @param string $lang_code 2-letters language code
 * @return array Categories tree
 */
function fn_get_categories($params = array(), $lang_code = CART_LANGUAGE)
{
    /**
     * Changes params for the categories search
     *
     * @param array  $params    Categories search params
     * @param string $lang_code 2-letters language code
     */
    fn_set_hook('get_categories_pre', $params, $lang_code);
    $default_params = array('category_id' => 0, 'visible' => false, 'current_category_id' => 0, 'simple' => true, 'plain' => false, 'limit' => 0, 'item_ids' => '', 'group_by_level' => true, 'get_images' => false, 'category_delimiter' => '/', 'get_frontend_urls' => false);
    $params = array_merge($default_params, $params);
    $sortings = array('timestamp' => '?:categories.timestamp', 'name' => '?:category_descriptions.category', 'position' => array('?:categories.position', '?:category_descriptions.category'));
    $auth =& $_SESSION['auth'];
    $fields = array('?:categories.category_id', '?:categories.parent_id', '?:categories.id_path', '?:category_descriptions.category', '?:categories.position', '?:categories.status');
    if ($params['simple'] == false) {
        $fields[] = '?:categories.product_count';
    }
    if (empty($params['current_category_id']) && !empty($params['product_category_id'])) {
        $params['current_category_id'] = $params['product_category_id'];
    }
    $condition = '';
    if (fn_allowed_for('MULTIVENDOR')) {
        if (Registry::get('runtime.company_id')) {
            $company_id = Registry::get('runtime.company_id');
        } elseif (!empty($params['company_ids'])) {
            $company_id = (int) $params['company_ids'];
        }
        if (!empty($company_id)) {
            $company_data = fn_get_company_data($company_id);
            if (!empty($company_data['category_ids'])) {
                $company_condition = db_quote(' AND ?:categories.category_id IN (?n)', $company_data['category_ids']);
                $condition .= $company_condition;
            }
        }
    }
    if (AREA == 'C') {
        $_statuses = array('A');
        // Show enabled products/categories
        $condition .= fn_get_localizations_condition('?:categories.localization', true);
        $condition .= " AND (" . fn_find_array_in_set($auth['usergroup_ids'], '?:categories.usergroup_ids', true) . ")";
        $condition .= db_quote(" AND ?:categories.status IN (?a)", $_statuses);
    }
    if (!empty($params['status'])) {
        $condition .= db_quote(" AND ?:categories.status IN (?a)", $params['status']);
    }
    if (isset($params['parent_category_id'])) {
        // set parent id, that was set in block properties
        $params['category_id'] = $params['parent_category_id'];
    }
    if ($params['visible'] == true && empty($params['b_id'])) {
        if (!empty($params['current_category_id'])) {
            $cur_id_path = db_get_field("SELECT id_path FROM ?:categories WHERE category_id = ?i", $params['current_category_id']);
            if (!empty($cur_id_path)) {
                $parent_categories_ids = explode('/', $cur_id_path);
            }
        }
        if (!empty($params['category_id']) || empty($parent_categories_ids)) {
            $parent_categories_ids[] = $params['category_id'];
        }
        $parents_condition = db_quote(" AND ?:categories.parent_id IN (?n)", $parent_categories_ids);
    }
    // if we have company_condtion, skip $parents_condition, it will be processed later by PHP
    if (!empty($parents_condition) && empty($company_condition)) {
        $condition .= $parents_condition;
    }
    if (!empty($params['category_id'])) {
        $from_id_path = db_get_field("SELECT id_path FROM ?:categories WHERE category_id = ?i", $params['category_id']);
        $condition .= db_quote(" AND ?:categories.id_path LIKE ?l", "{$from_id_path}/%");
    }
    if (!empty($params['item_ids'])) {
        $condition .= db_quote(' AND ?:categories.category_id IN (?n)', explode(',', $params['item_ids']));
    }
    if (!empty($params['except_id']) && (empty($params['item_ids']) || !empty($params['item_ids']) && !in_array($params['except_id'], explode(',', $params['item_ids'])))) {
        $condition .= db_quote(' AND ?:categories.category_id != ?i AND ?:categories.parent_id != ?i', $params['except_id'], $params['except_id']);
    }
    if (!empty($params['period']) && $params['period'] != 'A') {
        list($params['time_from'], $params['time_to']) = fn_create_periods($params);
        $condition .= db_quote(" AND (?:categories.timestamp >= ?i AND ?:categories.timestamp <= ?i)", $params['time_from'], $params['time_to']);
    }
    $limit = $join = $group_by = '';
    /**
     * Changes SQL params for the categories search
     *
     * @param array  $params    Categories search params
     * @param string $join      Join parametrs
     * @param string $condition Request condition
     * @param array  $fields    Selectable fields
     * @param string $group_by  Group by parameters
     * @param array  $sortings  Sorting fields
     * @param string $lang_code Language code
     */
    fn_set_hook('get_categories', $params, $join, $condition, $fields, $group_by, $sortings, $lang_code);
    if (!empty($params['limit'])) {
        $limit = db_quote(' LIMIT 0, ?i', $params['limit']);
    }
    $sorting = db_sort($params, $sortings, 'position', 'asc');
    if (!empty($params['get_conditions'])) {
        return array($fields, $join, $condition, $group_by, $sorting, $limit);
    }
    $categories = db_get_hash_array('SELECT ' . implode(',', $fields) . " FROM ?:categories LEFT JOIN ?:category_descriptions ON ?:categories.category_id = ?:category_descriptions.category_id AND ?:category_descriptions.lang_code = ?s {$join} WHERE 1 ?p {$group_by} {$sorting} ?p", 'category_id', $lang_code, $condition, $limit);
    /**
     * Process categories list after getting it
     * @param array $categories Categories list
     * @param array $params     Categories search params
     */
    fn_set_hook('get_categories_after_sql', $categories, $params);
    if (empty($categories)) {
        return array(array());
    }
    if (fn_allowed_for('ULTIMATE')) {
        // we can't build the correct tree for vendors if there are not available parent categories
        if (!empty($company_condition)) {
            $selected_ids = array_keys($categories);
            $parent_ids = array();
            // so get skipped parent categories ids
            foreach ($categories as $v) {
                if ($v['parent_id'] && !in_array($v['parent_id'], $selected_ids)) {
                    $parent_ids = array_merge($parent_ids, explode('/', $v['id_path']));
                }
            }
            if (!empty($parent_ids)) {
                // and retrieve its data
                if (Registry::get('runtime.company_id') && !empty($company_condition)) {
                    $condition = str_replace($company_condition, '', $condition);
                }
                $condition .= db_quote(' AND ?:categories.category_id IN (?a)', $parent_ids);
                $fields[] = '1 as disabled';
                //mark such categories as disabled
                $parent_categories = db_get_hash_array('SELECT ' . implode(',', $fields) . " FROM ?:categories LEFT JOIN ?:category_descriptions ON ?:categories.category_id = ?:category_descriptions.category_id AND ?:category_descriptions.lang_code = ?s {$join} WHERE 1 ?p {$group_by} {$sorting} ?p", 'category_id', $lang_code, $condition, $limit);
                $categories = $categories + $parent_categories;
            }
            // process parents_condition if it was skipped
            if (!empty($parent_categories_ids)) {
                foreach ($categories as $k => $v) {
                    if (!in_array($v['parent_id'], $parent_categories_ids)) {
                        unset($categories[$k]);
                    }
                }
            }
        }
    }
    if (!empty($params['active_category_id']) && !empty($categories[$params['active_category_id']])) {
        $categories[$params['active_category_id']]['active'] = true;
        Registry::set('runtime.active_category_ids', explode('/', $categories[$params['active_category_id']]['id_path']));
    }
    $categories_list = array();
    if ($params['simple'] == true || $params['group_by_level'] == true) {
        $child_for = array_keys($categories);
        $where_condition = !empty($params['except_id']) ? db_quote(' AND category_id != ?i', $params['except_id']) : '';
        $has_children = db_get_hash_array("SELECT category_id, parent_id FROM ?:categories WHERE parent_id IN(?n) ?p", 'parent_id', $child_for, $where_condition);
    }
    // Group categories by the level (simple)
    if ($params['simple'] == true) {
        foreach ($categories as $k => $v) {
            $v['level'] = substr_count($v['id_path'], '/');
            if ((!empty($params['current_category_id']) || $v['level'] == 0) && isset($has_children[$k])) {
                $v['has_children'] = $has_children[$k]['category_id'];
            }
            $categories_list[$v['level']][$v['category_id']] = $v;
            if ($params['get_images'] == true) {
                $categories_list[$v['level']][$v['category_id']]['main_pair'] = fn_get_image_pairs($v['category_id'], 'category', 'M', true, true, $lang_code);
            }
        }
    } elseif ($params['group_by_level'] == true) {
        // Group categories by the level (simple) and literalize path
        foreach ($categories as $k => $v) {
            $path = explode('/', $v['id_path']);
            $category_path = array();
            foreach ($path as $__k => $__v) {
                $category_path[$__v] = @$categories[$__v]['category'];
            }
            $v['category_path'] = implode($params['category_delimiter'], $category_path);
            $v['level'] = substr_count($v['id_path'], "/");
            if ((!empty($params['current_category_id']) || $v['level'] == 0) && isset($has_children[$k])) {
                $v['has_children'] = $has_children[$k]['category_id'];
            }
            $categories_list[$v['level']][$v['category_id']] = $v;
            if ($params['get_images'] == true) {
                $categories_list[$v['level']][$v['category_id']]['main_pair'] = fn_get_image_pairs($v['category_id'], 'category', 'M', true, true, $lang_code);
            }
        }
    } else {
        $categories_list = $categories;
        if ($params['get_images'] == true) {
            foreach ($categories_list as $k => $v) {
                if ($params['get_images'] == true) {
                    $categories_list[$k]['main_pair'] = fn_get_image_pairs($v['category_id'], 'category', 'M', true, true, $lang_code);
                }
            }
        }
    }
    ksort($categories_list, SORT_NUMERIC);
    $categories_list = array_reverse($categories_list);
    foreach ($categories_list as $level => $v) {
        foreach ($v as $k => $data) {
            if (isset($data['parent_id']) && isset($categories_list[$level + 1][$data['parent_id']])) {
                $categories_list[$level + 1][$data['parent_id']]['subcategories'][] = $categories_list[$level][$k];
                unset($categories_list[$level][$k]);
            }
        }
    }
    if (!empty($params['get_frontend_urls'])) {
        foreach ($categories_list as &$category) {
            $category['url'] = fn_url('categories.view?category_id=' . $category['category_id'], 'C');
        }
    }
    if ($params['group_by_level'] == true) {
        $categories_list = array_pop($categories_list);
    }
    if ($params['plain'] == true) {
        $categories_list = fn_multi_level_to_plain($categories_list, 'subcategories');
    }
    if (!empty($params['item_ids'])) {
        $categories_list = fn_sort_by_ids($categories_list, explode(',', $params['item_ids']), 'category_id');
    }
    if (!empty($params['add_root'])) {
        array_unshift($categories_list, array('category_id' => 0, 'category' => $params['add_root']));
    }
    /**
     * Process categories list before cutting second and fird levels
     *
     * @param array $categories_list Categories list
     * @param array $params          Categories search params
     */
    fn_set_hook('get_categories_before_cut_levels', $categories_list, $params);
    fn_dropdown_appearance_cut_second_third_levels($categories_list, 'subcategories', $params);
    /**
     * Process final category list
     *
     * @param array  $categories_list Categories list
     * @param array  $params          Categories search params
     * @param string $lang_code       Language code
     */
    fn_set_hook('get_categories_post', $categories_list, $params, $lang_code);
    // process search results
    if (!empty($params['save_view_results'])) {
        $request = $params;
        $request['page'] = 1;
        $categories_res = $params['plain'] == true ? $categories_list : fn_multi_level_to_plain($categories_list, 'subcategories');
        foreach ($categories_res as $key => $item) {
            if (empty($item['category_id'])) {
                unset($categories_res[$key]);
            }
        }
        $request['total_items'] = $request['items_per_page'] = count($categories_res);
        LastView::instance()->processResults('categories', $categories_res, $request);
    }
    return array($categories_list, $params);
}
Exemplo n.º 4
0
function fn_seo_get_categories_post(&$categories, &$params, &$lang_code)
{
    if (AREA == 'C') {
        if (empty($params['plain'])) {
            $cats = fn_multi_level_to_plain($categories, 'subcategories');
        } else {
            $cats = $categories;
        }
        foreach ($cats as $k => $category) {
            fn_seo_cache_name('c', $category['category_id'], $category, isset($category['company_id']) ? $category['company_id'] : '', $lang_code);
        }
    }
    return true;
}
Exemplo n.º 5
0
function fn_seo_post_get_pages(&$pages, $params, $lang_code)
{
    if (empty($params['get_tree']) && $params['get_tree'] == 'plain') {
        $_pages = $pages;
    } else {
        $_pages = fn_multi_level_to_plain($pages, 'subpages');
    }
    foreach ($_pages as $_page) {
        SeoCache::set('a', $_page['page_id'], $_page, isset($_page['company_id']) ? $_page['company_id'] : '', $lang_code);
    }
}
Exemplo n.º 6
0
function fn_get_pages($params = array(), $items_per_page = 0, $lang_code = CART_LANGUAGE)
{
    // Init filter
    $params = fn_init_view('pages', $params);
    $default_params = array('page_id' => 0, 'page' => 1, 'visible' => false, 'get_tree' => '', 'items_per_page' => 0, 'pdescr' => '', 'subpages' => '');
    $params = array_merge($default_params, $params);
    if (empty($params['pname']) && empty($params['pdescr']) && empty($params['subpages'])) {
        $params['pname'] = 'Y';
    }
    $fields = array('?:pages.*', '?:page_descriptions.*');
    // Define sort fields
    $sortings = array('position' => array('?:pages.position', '?:page_descriptions.page'), 'name' => '?:page_descriptions.page', 'timestamp' => '?:pages.timestamp', 'type' => '?:pages.page_type', 'multi_level' => array('?:pages.parent_id', '?:pages.position', '?:page_descriptions.page'));
    $directions = array('asc' => 'asc', 'desc' => 'desc');
    $auth =& $_SESSION['auth'];
    $condition = '1';
    $join = $limit = $group_by = '';
    if (isset($params['q']) && fn_string_no_empty($params['q'])) {
        $params['q'] = trim($params['q']);
        if ($params['match'] == 'any') {
            $pieces = fn_explode(' ', $params['q']);
            $search_type = ' OR ';
        } elseif ($params['match'] == 'all') {
            $pieces = fn_explode(' ', $params['q']);
            $search_type = ' AND ';
        } else {
            $pieces = array($params['q']);
            $search_type = '';
        }
        $_condition = array();
        foreach ($pieces as $piece) {
            if (strlen($piece) == 0) {
                continue;
            }
            $tmp = array();
            if (!empty($params['pname']) && $params['pname'] == 'Y') {
                $tmp[] = db_quote("(?:page_descriptions.page LIKE ?l)", "%{$piece}%");
                // check search words
            }
            if ($params['pdescr'] == 'Y') {
                $tmp[] = db_quote("?:page_descriptions.description LIKE ?l", "%{$piece}%");
            }
            if (!empty($tmp)) {
                $_condition[] = '(' . implode(' OR ', $tmp) . ')';
            }
        }
        if (!empty($_condition)) {
            $condition .= ' AND ' . implode($search_type, $_condition);
        }
    }
    $condition .= fn_get_company_condition('?:pages.company_id');
    if (!empty($params['page_type'])) {
        $condition .= db_quote(" AND ?:pages.page_type = ?s", $params['page_type']);
    }
    if (isset($params['parent_id']) && $params['parent_id'] !== '') {
        $p_ids = array();
        if ($params['subpages'] == 'Y') {
            $p_ids = db_get_fields("SELECT a.page_id FROM ?:pages as a LEFT JOIN ?:pages as b ON b.page_id = ?i WHERE a.id_path LIKE CONCAT(b.id_path, '/%')", $params['parent_id']);
        }
        $p_ids[] = $params['parent_id'];
        $condition .= db_quote(" AND ?:pages.parent_id IN (?n)", $p_ids);
    }
    if (!empty($params['from_page_id'])) {
        $from_id_path = db_get_field("SELECT id_path FROM ?:pages WHERE page_id = ?i", $params['from_page_id']);
        $condition .= db_quote(" AND ?:pages.id_path LIKE ?l", "{$from_id_path}/%");
    }
    if (!empty($params['status'])) {
        $condition .= db_quote(" AND ?:pages.status IN (?a)", $params['status']);
    }
    if (!empty($params['vendor_pages']) && empty($params['company_id'])) {
        return array(array(), $params);
    } elseif (!empty($params['company_id'])) {
        $condition .= db_quote(" AND ?:pages.company_id = ?i", $params['company_id']);
    }
    if (!empty($params['visible'])) {
        // for pages tree: show visible branch only
        if (!empty($params['current_page_id'])) {
            $cur_id_path = db_get_field("SELECT id_path FROM ?:pages WHERE page_id = ?i", $params['current_page_id']);
            if (!empty($cur_id_path)) {
                $page_ids = explode('/', $cur_id_path);
            }
        }
        $page_ids[] = $params['page_id'];
        $condition .= db_quote(" AND ?:pages.parent_id IN (?n)", $page_ids);
    }
    if (!empty($params['period']) && $params['period'] != 'A') {
        list($params['time_from'], $params['time_to']) = fn_create_periods($params);
        $condition .= db_quote(" AND (?:pages.timestamp >= ?i AND ?:pages.timestamp <= ?i)", $params['time_from'], $params['time_to']);
    }
    if (!empty($params['item_ids'])) {
        // get only defined pages
        $condition .= db_quote(" AND ?:pages.page_id IN (?n)", explode(',', $params['item_ids']));
    }
    if (!empty($params['except_id']) && (empty($params['item_ids']) || !empty($params['item_ids']) && !in_array($params['except_id'], explode(',', $params['item_ids'])))) {
        $condition .= db_quote(' AND ?:pages.page_id != ?i AND ?:pages.parent_id != ?i', $params['except_id'], $params['except_id']);
    }
    if (AREA != 'A') {
        $condition .= " AND (" . fn_find_array_in_set($auth['usergroup_ids'], '?:pages.usergroup_ids', true) . ")";
        $condition .= fn_get_localizations_condition('?:pages.localization', true);
        $condition .= db_quote(" AND (use_avail_period = ?s OR (use_avail_period = ?s AND avail_from_timestamp >= ?i AND avail_till_timestamp <= ?i))", 'N', 'Y', TIME, TIME);
    }
    $join = db_quote('LEFT JOIN ?:page_descriptions ON ?:pages.page_id = ?:page_descriptions.page_id AND ?:page_descriptions.lang_code = ?s', $lang_code);
    if (!empty($params['b_id'])) {
        $join .= " LEFT JOIN ?:block_links ON ?:block_links.object_id = ?:pages.page_id AND ?:block_links.location = 'pages'";
        $condition .= db_quote(' AND ?:block_links.block_id = ?i', $params['b_id']);
    }
    if (!empty($params['limit'])) {
        $limit = db_quote(" LIMIT 0, ?i", $params['limit']);
    }
    fn_set_hook('get_pages', $params, $join, $condition, $fields, $group_by, $sortings);
    if (!empty($params['get_tree'])) {
        $params['sort_by'] = 'multi_level';
    }
    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'] = 'position';
    }
    $sorting = (is_array($sortings[$params['sort_by']]) ? implode(' ' . $directions[$params['sort_order']] . ', ', $sortings[$params['sort_by']]) : $sortings[$params['sort_by']]) . " " . $directions[$params['sort_order']];
    if (!empty($group_by)) {
        $group_by = ' GROUP BY ' . $group_by;
    }
    // Reverse sorting (for usage in view)
    $params['sort_order'] = $params['sort_order'] == 'asc' ? 'desc' : 'asc';
    // Get search conditions
    if (!empty($params['get_conditions'])) {
        return array($fields, $join, $condition);
    }
    $total = 0;
    if (!empty($items_per_page) && !empty($params['paginate'])) {
        $total = db_get_field("SELECT COUNT(DISTINCT(?:pages.page_id)) FROM ?:pages ?p WHERE ?p ?p ORDER BY ?p", $join, $condition, $group_by, $sorting);
        $limit = fn_paginate($params['page'], $total, $items_per_page);
    }
    $pages = db_get_hash_array("SELECT " . implode(', ', $fields) . " FROM ?:pages ?p WHERE ?p ?p ORDER BY ?p ?p", 'page_id', $join, $condition, $group_by, $sorting, $limit);
    if (!empty($pages)) {
        foreach ($pages as $k => $v) {
            $pages[$k]['level'] = substr_count($v['id_path'], '/');
        }
        if (!empty($params['get_tree'])) {
            $delete_keys = array();
            foreach ($pages as $k => $v) {
                if (!empty($v['parent_id']) && !empty($pages[$v['parent_id']])) {
                    $pages[$v['parent_id']]['subpages'][$v['page_id']] =& $pages[$k];
                    $delete_keys[] = $k;
                }
                if (!empty($v['parent_id']) && (!isset($params['root_id']) && empty($pages[$v['parent_id']]) || isset($params['root_id']) && $v['parent_id'] != $params['root_id']) && (empty($params['from_page_id']) || $params['from_page_id'] != $v['parent_id'])) {
                    // delete pages that don't have parent. FIXME: should be done on database layer
                    $delete_keys[] = $k;
                }
            }
            foreach ($delete_keys as $k) {
                unset($pages[$k]);
            }
        } elseif (!empty($params['item_ids'])) {
            $pages = fn_sort_by_ids($pages, explode(',', $params['item_ids']), 'page_id');
        }
        if ($params['get_tree'] == 'plain') {
            $pages = fn_multi_level_to_plain($pages, 'subpages');
        }
        if (!empty($params['get_children_count'])) {
            $where_condition = !empty($params['except_id']) ? db_quote(' AND page_id != ?i', $params['except_id']) : '';
            if ($params['get_tree'] == 'plain') {
                $_page_ids = array();
                foreach ($pages as $_p) {
                    $_page_ids[] = $_p['page_id'];
                }
            } else {
                $_page_ids = array_keys($pages);
            }
            $children = db_get_hash_single_array("SELECT parent_id, COUNT(page_id) as children FROM ?:pages WHERE parent_id IN (?n) ?p GROUP BY parent_id", array('parent_id', 'children'), $_page_ids, $where_condition);
            if (!empty($children)) {
                if ($params['get_tree'] == 'plain') {
                    foreach ($pages as $_id => $_p) {
                        if (!empty($children[$_p['page_id']])) {
                            $pages[$_id]['has_children'] = true;
                        }
                    }
                } else {
                    foreach ($children as $k => $v) {
                        $pages[$k]['has_children'] = !empty($v);
                    }
                }
            }
        }
    }
    if (!empty($params['add_root'])) {
        array_unshift($pages, array('page_id' => 0, 'page' => $params['add_root']));
    }
    fn_set_hook('post_get_pages', $pages, $params, $lang_code);
    return array($pages, $params);
}
Exemplo n.º 7
0
/**
 * Gets categories tree beginning from category identifier defined in params or root category
 * @param array $params Categories search params
 *      category_id - Root category identifier
 *      visible - Flag that defines if only visible categories should be included
 *      current_category_id - Identifier of current node for visible categories
 *      simple - Flag that defines if category path should be getted as set of category IDs
 *      plain - Flag that defines if continues list of categories should be returned
 *      --------------------------------------
 *      Examples:
 *      Gets whole categories tree:
 *      fn_get_categories()
 *      --------------------------------------
 *      Gets subcategories tree of the category:
 *      fn_get_categories(array(
 *          'category_id' => 123
 *      ))
 *      --------------------------------------
 *      Gets all first-level nodes of the category
 *      fn_get_categories(array(
 *          'category_id' => 123,
 *          'visible' => true
 *      ))
 *      --------------------------------------
 *      Gets all visible nodes of the category, start from the root
 *      fn_get_categories(array(
 *          'category_id' => 0,
 *          'current_category_id' => 234,
 *          'visible' => true
 *      ))
 * @param string $lang_code 2-letters language code
 * @return array Categories tree
 */
function fn_get_categories($params = array(), $lang_code = CART_LANGUAGE)
{
    /**
     * Changes params for the categories search
     *
     * @param array  $params    Categories search params
     * @param string $lang_code 2-letters language code
     */
    fn_set_hook('get_categories_pre', $params, $lang_code);
    $default_params = array('category_id' => 0, 'visible' => false, 'current_category_id' => 0, 'simple' => true, 'plain' => false, 'limit' => 0, 'item_ids' => '', 'group_by_level' => true, 'get_images' => false, 'category_delimiter' => '/', 'get_frontend_urls' => false, 'max_nesting_level' => null);
    $params = array_merge($default_params, $params);
    $sortings = array('timestamp' => '?:categories.timestamp', 'name' => '?:category_descriptions.category', 'position' => array('?:categories.is_trash', '?:categories.position', '?:category_descriptions.category'));
    $auth =& Tygh::$app['session']['auth'];
    $fields = array('?:categories.category_id', '?:categories.parent_id', '?:categories.id_path', '?:category_descriptions.category', '?:categories.position', '?:categories.status');
    if (!$params['simple']) {
        $fields[] = '?:categories.product_count';
    }
    if (empty($params['current_category_id']) && !empty($params['product_category_id'])) {
        $params['current_category_id'] = $params['product_category_id'];
    }
    $condition = '';
    if (AREA == 'C') {
        $_statuses = array('A');
        // Show enabled products/categories
        $condition .= fn_get_localizations_condition('?:categories.localization', true);
        $condition .= " AND (" . fn_find_array_in_set($auth['usergroup_ids'], '?:categories.usergroup_ids', true) . ")";
        $condition .= db_quote(" AND ?:categories.status IN (?a)", $_statuses);
    }
    if (!empty($params['status'])) {
        $condition .= db_quote(" AND ?:categories.status IN (?a)", $params['status']);
    }
    if (isset($params['parent_category_id'])) {
        // set parent id, that was set in block properties
        $params['category_id'] = $params['parent_category_id'];
    }
    if ($params['visible'] == true && empty($params['b_id'])) {
        if (!empty($params['current_category_id'])) {
            $cur_id_path = db_get_field("SELECT id_path FROM ?:categories WHERE category_id = ?i", $params['current_category_id']);
            if (!empty($cur_id_path)) {
                $parent_categories_ids = explode('/', $cur_id_path);
            }
        }
        if (!empty($params['category_id']) || empty($parent_categories_ids)) {
            $parent_categories_ids[] = $params['category_id'];
        }
        $condition .= db_quote(" AND ?:categories.parent_id IN (?n)", $parent_categories_ids);
    }
    if (!empty($params['category_id'])) {
        $from_id_path = db_get_field("SELECT id_path FROM ?:categories WHERE category_id = ?i", $params['category_id']);
        $condition .= db_quote(" AND ?:categories.id_path LIKE ?l", "{$from_id_path}/%");
    }
    if (!empty($params['item_ids'])) {
        $condition .= db_quote(' AND ?:categories.category_id IN (?n)', explode(',', $params['item_ids']));
    }
    if (!empty($params['except_id']) && (empty($params['item_ids']) || !empty($params['item_ids']) && !in_array($params['except_id'], explode(',', $params['item_ids'])))) {
        $condition .= db_quote(' AND ?:categories.category_id != ?i AND ?:categories.parent_id != ?i', $params['except_id'], $params['except_id']);
    }
    if (!empty($params['period']) && $params['period'] != 'A') {
        list($params['time_from'], $params['time_to']) = fn_create_periods($params);
        $condition .= db_quote(" AND (?:categories.timestamp >= ?i AND ?:categories.timestamp <= ?i)", $params['time_from'], $params['time_to']);
    }
    if (!empty($params['max_nesting_level'])) {
        if (!empty($params['parent_category_id'])) {
            $parent_nesting_level = (int) db_get_field("SELECT level FROM ?:categories WHERE category_id = ?i", $params['parent_category_id']);
        } else {
            $parent_nesting_level = 0;
        }
        $condition .= db_quote(" AND ?:categories.level <= ?i", $params['max_nesting_level'] + $parent_nesting_level);
    }
    $limit = $join = $group_by = '';
    /**
     * Changes SQL params for the categories search
     *
     * @param array  $params    Categories search params
     * @param string $join      Join parametrs
     * @param string $condition Request condition
     * @param array  $fields    Selectable fields
     * @param string $group_by  Group by parameters
     * @param array  $sortings  Sorting fields
     * @param string $lang_code Language code
     */
    fn_set_hook('get_categories', $params, $join, $condition, $fields, $group_by, $sortings, $lang_code);
    if (!empty($params['limit'])) {
        $limit = db_quote(' LIMIT 0, ?i', $params['limit']);
    }
    $sorting = db_sort($params, $sortings, 'position', 'asc');
    if (!empty($params['get_conditions'])) {
        return array($fields, $join, $condition, $group_by, $sorting, $limit);
    }
    $categories = db_get_hash_array('SELECT ' . implode(',', $fields) . " FROM ?:categories LEFT JOIN ?:category_descriptions ON ?:categories.category_id = ?:category_descriptions.category_id AND ?:category_descriptions.lang_code = ?s {$join} WHERE 1 ?p {$group_by} {$sorting} ?p", 'category_id', $lang_code, $condition, $limit);
    /**
     * Process categories list after getting it
     * @param array  $categories Categories list
     * @param array  $params     Categories search params
     * @param string $join       Join parametrs
     * @param string $condition  Request condition
     * @param array  $fields     Selectable fields
     * @param string $group_by   Group by parameters
     * @param array  $sortings   Sorting fields
     * @param string $sorting    Sorting parameters
     * @param string $limit      Limit parameter
     * @param string $lang_code  Language code
     */
    fn_set_hook('get_categories_after_sql', $categories, $params, $join, $condition, $fields, $group_by, $sortings, $sorting, $limit, $lang_code);
    if (empty($categories)) {
        return array(array());
    }
    // @TODO remove from here, because active category may not exist in the resulting set. This is the job for controller.
    if (!empty($params['active_category_id']) && !empty($categories[$params['active_category_id']])) {
        $categories[$params['active_category_id']]['active'] = true;
        Registry::set('runtime.active_category_ids', explode('/', $categories[$params['active_category_id']]['id_path']));
    }
    $categories_list = array();
    if ($params['simple'] == true || $params['group_by_level'] == true) {
        $child_for = array_keys($categories);
        $where_condition = !empty($params['except_id']) ? db_quote(' AND category_id != ?i', $params['except_id']) : '';
        $has_children = db_get_hash_array("SELECT category_id, parent_id FROM ?:categories WHERE parent_id IN(?n) ?p", 'parent_id', $child_for, $where_condition);
    }
    $category_ids = array();
    // Group categories by the level (simple)
    if ($params['simple'] == true) {
        foreach ($categories as $k => $v) {
            $v['level'] = substr_count($v['id_path'], '/');
            if ((!empty($params['current_category_id']) || $v['level'] == 0) && isset($has_children[$k])) {
                $v['has_children'] = $has_children[$k]['category_id'];
            }
            $categories_list[$v['level']][$v['category_id']] = $v;
            $category_ids[] = $v['category_id'];
        }
    } elseif ($params['group_by_level'] == true) {
        // Group categories by the level (simple) and literalize path
        foreach ($categories as $k => $v) {
            $path = explode('/', $v['id_path']);
            $category_path = array();
            foreach ($path as $__k => $__v) {
                $category_path[$__v] = @$categories[$__v]['category'];
            }
            $v['category_path'] = implode($params['category_delimiter'], $category_path);
            $v['level'] = substr_count($v['id_path'], "/");
            if ((!empty($params['current_category_id']) || $v['level'] == 0) && isset($has_children[$k])) {
                $v['has_children'] = $has_children[$k]['category_id'];
            }
            $categories_list[$v['level']][$v['category_id']] = $v;
            $category_ids[] = $v['category_id'];
        }
    } else {
        // @FIXME: Seems that this code isn't being executed anywhere
        $categories_list = $categories;
        $category_ids = fn_fields_from_multi_level($categories_list, 'category_id', 'category_id');
    }
    ksort($categories_list, SORT_NUMERIC);
    $categories_list = array_reverse($categories_list, !$params['simple'] && !$params['group_by_level']);
    // Lazy-load category image pairs
    if ($params['get_images']) {
        $image_pairs_for_categories = fn_get_image_pairs($category_ids, 'category', 'M', true, true, $lang_code);
    }
    // Rearrangement of subcategories and filling with images
    foreach ($categories_list as $level => $categories_of_level) {
        // Fill categories' image pairs for plain structure of array
        if ($params['get_images'] && !$params['simple'] && !$params['group_by_level'] && !empty($image_pairs_for_categories[$level])) {
            $categories_list[$level]['main_pair'] = reset($image_pairs_for_categories[$level]);
        }
        foreach ($categories_of_level as $category_id => $category_data) {
            // Fill categories' image pairs for multi-level structure of array
            if ($params['get_images'] && !empty($image_pairs_for_categories[$category_id]) && ($params['simple'] || $params['group_by_level'])) {
                $categories_list[$level][$category_id]['main_pair'] = reset($image_pairs_for_categories[$category_id]);
            }
            // Move subcategories to their parents' elements
            if (isset($category_data['parent_id']) && isset($categories_list[$level + 1][$category_data['parent_id']])) {
                $categories_list[$level + 1][$category_data['parent_id']]['subcategories'][] = $categories_list[$level][$category_id];
                unset($categories_list[$level][$category_id]);
            }
        }
    }
    if (!empty($params['get_frontend_urls'])) {
        foreach ($categories_list as &$category) {
            $category['url'] = fn_url('categories.view?category_id=' . $category['category_id'], 'C');
        }
    }
    if ($params['group_by_level'] == true) {
        $categories_list = array_pop($categories_list);
    }
    if ($params['plain'] == true) {
        $categories_list = fn_multi_level_to_plain($categories_list, 'subcategories');
    }
    if (!empty($params['item_ids'])) {
        $categories_list = fn_sort_by_ids($categories_list, explode(',', $params['item_ids']), 'category_id');
    }
    if (!empty($params['add_root'])) {
        array_unshift($categories_list, array('category_id' => 0, 'category' => $params['add_root']));
    }
    /**
     * Process categories list before cutting second and fird levels
     *
     * @param array $categories_list Categories list
     * @param array $params          Categories search params
     */
    fn_set_hook('get_categories_before_cut_levels', $categories_list, $params);
    fn_dropdown_appearance_cut_second_third_levels($categories_list, 'subcategories', $params);
    /**
     * Process final category list
     *
     * @param array  $categories_list Categories list
     * @param array  $params          Categories search params
     * @param string $lang_code       Language code
     */
    fn_set_hook('get_categories_post', $categories_list, $params, $lang_code);
    // process search results
    if (!empty($params['save_view_results'])) {
        $request = $params;
        $request['page'] = 1;
        $categories_res = $params['plain'] == true ? $categories_list : fn_multi_level_to_plain($categories_list, 'subcategories');
        foreach ($categories_res as $key => $item) {
            if (empty($item['category_id'])) {
                unset($categories_res[$key]);
            }
        }
        $request['total_items'] = $request['items_per_page'] = count($categories_res);
        LastView::instance()->processResults('categories', $categories_res, $request);
    }
    return array($categories_list, $params);
}
Exemplo n.º 8
0
function fn_get_categories($params = array(), $lang_code = CART_LANGUAGE)
{
    $default_params = array('category_id' => 0, 'visible' => false, 'current_category_id' => 0, 'simple' => true, 'plain' => false, 'sort_order' => 'desc', 'limit' => 0, 'sort_by' => 'position', 'item_ids' => '', 'group_by_level' => true, 'get_images' => false, 'category_delimiter' => '/');
    $params = array_merge($default_params, $params);
    $sortings = array('timestamp' => '?:categories.timestamp', 'name' => '?:category_descriptions.category', 'position' => array('?:categories.position', '?:category_descriptions.category'));
    $directions = array('asc' => 'asc', 'desc' => 'desc');
    $auth =& $_SESSION['auth'];
    $fields = array('?:categories.category_id', '?:categories.parent_id', '?:categories.id_path', '?:category_descriptions.category', '?:categories.position', '?:categories.status');
    if ($params['simple'] == false) {
        $fields[] = '?:categories.product_count';
    }
    if (empty($params['current_category_id']) && !empty($params['product_category_id'])) {
        $params['current_category_id'] = $params['product_category_id'];
    }
    $condition = '';
    if (defined('COMPANY_ID')) {
        $companies = Registry::get('s_companies');
        if (!empty($companies[COMPANY_ID]['categories'])) {
            $condition .= db_quote(' AND ?:categories.category_id IN (?n)', explode(',', $companies[COMPANY_ID]['categories']));
        }
    }
    if (AREA == 'C') {
        $_statuses = array('A');
        // Show enabled products/categories
        $condition .= fn_get_localizations_condition('?:categories.localization', true);
        $condition .= " AND (" . fn_find_array_in_set($auth['usergroup_ids'], '?:categories.usergroup_ids', true) . ")";
        $condition .= db_quote(" AND ?:categories.status IN (?a)", $_statuses);
    }
    if ($params['visible'] == true) {
        if (!empty($params['current_category_id'])) {
            $cur_id_path = db_get_field("SELECT id_path FROM ?:categories WHERE category_id = ?i", $params['current_category_id']);
            if (!empty($cur_id_path)) {
                $categories_ids = explode('/', $cur_id_path);
            }
        }
        $categories_ids[] = $params['category_id'];
        $condition .= db_quote(" AND ?:categories.parent_id IN (?n)", $categories_ids);
    }
    if (!empty($params['category_id'])) {
        $from_id_path = db_get_field("SELECT id_path FROM ?:categories WHERE category_id = ?i", $params['category_id']);
        $condition .= db_quote(" AND ?:categories.id_path LIKE ?l", "{$from_id_path}/%");
    }
    if (!empty($params['item_ids'])) {
        $condition .= db_quote(' AND ?:categories.category_id IN (?n)', explode(',', $params['item_ids']));
    }
    if (!empty($params['except_id']) && (empty($params['item_ids']) || !empty($params['item_ids']) && !in_array($params['except_id'], explode(',', $params['item_ids'])))) {
        $condition .= db_quote(' AND ?:categories.category_id != ?i AND ?:categories.parent_id != ?i', $params['except_id'], $params['except_id']);
    }
    if (!empty($params['period']) && $params['period'] != 'A') {
        list($params['time_from'], $params['time_to']) = fn_create_periods($params);
        $condition .= db_quote(" AND (?:categories.timestamp >= ?i AND ?:categories.timestamp <= ?i)", $params['time_from'], $params['time_to']);
    }
    $limit = $join = $group_by = '';
    if (!empty($params['b_id'])) {
        $join .= " LEFT JOIN ?:block_links ON ?:block_links.object_id = ?:categories.category_id AND ?:block_links.location = 'categories'";
        $condition .= db_quote(' AND ?:block_links.block_id = ?i', $params['b_id']);
        $params['group_by_level'] = false;
    }
    fn_set_hook('get_categories', $params, $join, $condition, $fields, $group_by, $sortings);
    if (!empty($params['limit'])) {
        $limit = db_quote(' LIMIT 0, ?i', $params['limit']);
    }
    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'] = 'position';
    }
    // Reverse sorting (for usage in view)
    $params['sort_order'] = $params['sort_order'] == 'asc' ? 'desc' : 'asc';
    $sorting = (is_array($sortings[$params['sort_by']]) ? implode(' ' . $directions[$params['sort_order']] . ', ', $sortings[$params['sort_by']]) : $sortings[$params['sort_by']]) . " " . $directions[$params['sort_order']];
    $categories = db_get_hash_array('SELECT ' . implode(',', $fields) . " FROM ?:categories LEFT JOIN ?:category_descriptions ON ?:categories.category_id = ?:category_descriptions.category_id AND ?:category_descriptions.lang_code = ?s {$join} WHERE 1 ?p {$group_by} ORDER BY {$sorting} ?p", 'category_id', $lang_code, $condition, $limit);
    fn_set_hook('get_categories_post', $categories);
    if (empty($categories)) {
        return array(array());
    }
    $tmp = array();
    if ($params['simple'] == true || $params['group_by_level'] == true) {
        $child_for = array_keys($categories);
        $where_condition = !empty($params['except_id']) ? db_quote(' AND category_id != ?i', $params['except_id']) : '';
        $has_children = db_get_hash_array("SELECT category_id, parent_id FROM ?:categories WHERE parent_id IN(?n) ?p", 'parent_id', $child_for, $where_condition);
    }
    // Group categories by the level (simple)
    if ($params['simple'] == true) {
        foreach ($categories as $k => $v) {
            $v['level'] = substr_count($v['id_path'], '/');
            if ((!empty($params['current_category_id']) || $v['level'] == 0) && isset($has_children[$k])) {
                $v['has_children'] = $has_children[$k]['category_id'];
            }
            $tmp[$v['level']][$v['category_id']] = $v;
            if ($params['get_images'] == true) {
                $tmp[$v['level']][$v['category_id']]['main_pair'] = fn_get_image_pairs($v['category_id'], 'category', 'M', true, true, $lang_code);
            }
        }
    } elseif ($params['group_by_level'] == true) {
        // Group categories by the level (simple) and literalize path
        foreach ($categories as $k => $v) {
            $path = explode('/', $v['id_path']);
            $category_path = array();
            foreach ($path as $__k => $__v) {
                $category_path[$__v] = @$categories[$__v]['category'];
            }
            $v['category_path'] = implode($params['category_delimiter'], $category_path);
            $v['level'] = substr_count($v['id_path'], "/");
            if ((!empty($params['current_category_id']) || $v['level'] == 0) && isset($has_children[$k])) {
                $v['has_children'] = $has_children[$k]['category_id'];
            }
            $tmp[$v['level']][$v['category_id']] = $v;
            if ($params['get_images'] == true) {
                $tmp[$v['level']][$v['category_id']]['main_pair'] = fn_get_image_pairs($v['category_id'], 'category', 'M', true, true, $lang_code);
            }
        }
    } else {
        $tmp = $categories;
        if ($params['get_images'] == true) {
            foreach ($tmp as $k => $v) {
                if ($params['get_images'] == true) {
                    $tmp[$k]['main_pair'] = fn_get_image_pairs($v['category_id'], 'category', 'M', true, true, $lang_code);
                }
            }
        }
    }
    ksort($tmp, SORT_NUMERIC);
    $tmp = array_reverse($tmp);
    foreach ($tmp as $level => $v) {
        foreach ($v as $k => $data) {
            if (isset($data['parent_id']) && isset($tmp[$level + 1][$data['parent_id']])) {
                $tmp[$level + 1][$data['parent_id']]['subcategories'][] = $tmp[$level][$k];
                unset($tmp[$level][$k]);
            }
        }
    }
    if ($params['group_by_level'] == true) {
        $tmp = array_pop($tmp);
    }
    if ($params['plain'] == true) {
        $tmp = fn_multi_level_to_plain($tmp, 'subcategories');
    }
    if (!empty($params['item_ids'])) {
        $tmp = fn_sort_by_ids($tmp, explode(',', $params['item_ids']), 'category_id');
    }
    if (!empty($params['add_root'])) {
        array_unshift($tmp, array('category_id' => 0, 'category' => $params['add_root']));
    }
    return array($tmp, $params);
}
Exemplo n.º 9
0
/**
 * Gets categories tree beginning from category identifier defined in params or root category
 * @param array $params Categories search params
 *      category_id - Root category identifier
 *      visible - Flag that defines if only visible categories should be included
 *      current_category_id - Identifier of current node for visible categories
 *      simple - Flag that defines if category path should be getted as set of category IDs
 *      plain - Flag that defines if continues list of categories should be returned
 *      --------------------------------------
 *      Examples:
 *      Gets whole categories tree:
 *      fn_ebay_get_categories()
 *      --------------------------------------
 *      Gets subcategories tree of the category:
 *      fn_ebay_get_categories(array(
 *          'category_id' => 123
 *      ))
 *      --------------------------------------
 *      Gets all first-level nodes of the category
 *      fn_ebay_get_categories(array(
 *          'category_id' => 123,
 *          'visible' => true
 *      ))
 *      --------------------------------------
 *      Gets all visible nodes of the category, start from the root
 *      fn_ebay_get_categories(array(
 *          'category_id' => 0,
 *          'current_category_id' => 234,
 *          'visible' => true
 *      ))
 * @param int $site_id
 * @param string $lang_code 2-letters language code
 * @return array Categories tree
 */
function fn_ebay_get_categories($params = array(), $site_id = 0, $lang_code = CART_LANGUAGE)
{
    $default_params = array('category_id' => 0, 'visible' => false, 'current_category_id' => 0, 'simple' => true, 'plain' => false, 'limit' => 0, 'item_ids' => '', 'group_by_level' => true, 'category_delimiter' => ',', 'max_nesting_level' => null);
    $params = array_merge($default_params, $params);
    $sortings = array('name' => '?:ebay_categories.name');
    $fields = array('category_id', 'parent_id', 'id_path', 'name', 'level', 'leaf', 'site_id');
    if (empty($params['current_category_id']) && !empty($params['product_category_id'])) {
        $params['current_category_id'] = $params['product_category_id'];
    }
    $condition = db_quote('AND site_id = ?i', $site_id);
    if (isset($params['parent_category_id'])) {
        // set parent id, that was set in block properties
        $params['category_id'] = $params['parent_category_id'];
    }
    if (empty($params['b_id'])) {
        $parent_categories_ids = array();
        if (!empty($params['current_category_id'])) {
            $cur_id_path = db_get_field("SELECT id_path FROM ?:ebay_categories WHERE category_id = ?i AND site_id = ?i", $params['current_category_id'], $site_id);
            if (!empty($cur_id_path)) {
                $parent_categories_ids = explode(',', $cur_id_path);
            }
        }
        if (!empty($params['category_id']) || empty($parent_categories_ids)) {
            $parent_categories_ids[] = $params['category_id'];
        }
        $parents_condition = db_quote(" AND ?:ebay_categories.parent_id IN (?n)", $parent_categories_ids);
    }
    // if we have company_condtion, skip $parents_condition, it will be processed later by PHP
    if (!empty($parents_condition) && empty($company_condition)) {
        $condition .= $parents_condition;
    }
    if (!empty($params['category_id'])) {
        $from_id_path = db_get_field("SELECT id_path FROM ?:ebay_categories WHERE category_id = ?i AND site_id = ?i", $params['category_id'], $site_id);
        $condition .= db_quote(" AND ?:ebay_categories.id_path LIKE ?l", "{$from_id_path},%");
    }
    if (!empty($params['item_ids'])) {
        $condition .= db_quote(' AND ?:ebay_categories.category_id IN (?n)', explode(',', $params['item_ids']));
    }
    if (!empty($params['except_id']) && (empty($params['item_ids']) || !empty($params['item_ids']) && !in_array($params['except_id'], explode(',', $params['item_ids'])))) {
        $condition .= db_quote(' AND ?:ebay_categories.category_id != ?i AND ?:ebay_categories.parent_id != ?i', $params['except_id'], $params['except_id']);
    }
    if (!empty($params['max_nesting_level'])) {
        $condition .= db_quote(" AND ?:ebay_categories.level <= ?i", $params['max_nesting_level']);
    }
    $limit = $join = $group_by = '';
    if (!empty($params['limit'])) {
        $limit = db_quote(' LIMIT 0, ?i', $params['limit']);
    }
    $sorting = db_sort($params, $sortings, 'name', 'asc');
    $categories = db_get_hash_array('SELECT ' . implode(',', $fields) . " FROM ?:ebay_categories WHERE 1 ?p {$group_by} {$sorting} ?p", 'category_id', $condition, $limit);
    if (empty($categories)) {
        return array(array());
    }
    // @TODO remove from here, because active category may not exist in the resulting set. This is the job for controller.
    if (!empty($params['active_category_id']) && !empty($categories[$params['active_category_id']])) {
        $categories[$params['active_category_id']]['active'] = true;
    }
    $categories_list = array();
    if ($params['simple'] == true || $params['group_by_level'] == true) {
        $child_for = array_keys($categories);
        $where_condition = !empty($params['except_id']) ? db_quote(' AND category_id != ?i', $params['except_id']) : '';
        $has_children = db_get_hash_array("SELECT category_id, parent_id FROM ?:ebay_categories WHERE parent_id IN(?n) AND site_id = ?i ?p", 'parent_id', $child_for, $site_id, $where_condition);
    }
    $category_ids = array();
    // Group categories by the level (simple)
    if ($params['simple'] == true) {
        foreach ($categories as $k => $v) {
            $v['level'] = substr_count($v['id_path'], ',');
            if ((!empty($params['current_category_id']) || $v['level'] == 0) && isset($has_children[$k])) {
                $v['has_children'] = $has_children[$k]['category_id'];
            }
            $categories_list[$v['level']][$v['category_id']] = $v;
            $category_ids[] = $v['category_id'];
        }
    } elseif ($params['group_by_level'] == true) {
        // Group categories by the level (simple) and literalize path
        foreach ($categories as $k => $v) {
            $path = explode('/', $v['id_path']);
            $category_path = array();
            foreach ($path as $__k => $__v) {
                $category_path[$__v] = @$categories[$__v]['category'];
            }
            $v['category_path'] = implode($params['category_delimiter'], $category_path);
            $v['level'] = substr_count($v['id_path'], ",");
            if ((!empty($params['current_category_id']) || $v['level'] == 0) && isset($has_children[$k])) {
                $v['has_children'] = $has_children[$k]['category_id'];
            }
            $categories_list[$v['level']][$v['category_id']] = $v;
            $category_ids[] = $v['category_id'];
        }
    } else {
        // @FIXME: Seems that this code isn't being executed anywhere
        $categories_list = $categories;
        $category_ids = fn_fields_from_multi_level($categories_list, 'category_id', 'category_id');
    }
    ksort($categories_list, SORT_NUMERIC);
    $categories_list = array_reverse($categories_list, !$params['simple'] && !$params['group_by_level']);
    // Rearrangement of subcategories and filling with images
    foreach ($categories_list as $level => $categories_of_level) {
        foreach ($categories_of_level as $category_id => $category_data) {
            // Move subcategories to their parents' elements
            if (isset($category_data['parent_id']) && isset($categories_list[$level + 1][$category_data['parent_id']])) {
                $categories_list[$level + 1][$category_data['parent_id']]['subcategories'][] = $categories_list[$level][$category_id];
                unset($categories_list[$level][$category_id]);
            }
        }
    }
    if ($params['group_by_level'] == true) {
        $categories_list = array_pop($categories_list);
    }
    if ($params['plain'] == true) {
        $categories_list = fn_multi_level_to_plain($categories_list, 'subcategories');
    }
    if (!empty($params['item_ids'])) {
        $categories_list = fn_sort_by_ids($categories_list, explode(',', $params['item_ids']), 'category_id');
    }
    if (!empty($params['add_root'])) {
        array_unshift($categories_list, array('category_id' => 0, 'category' => $params['add_root']));
    }
    fn_dropdown_appearance_cut_second_third_levels($categories_list, 'subcategories', $params);
    return array($categories_list, $params);
}