/**
 * Adds or updates category
 *
 * @param array $category_data Category data
 * @param int $category_id Category identifier
 * @param string $lang_code Two-letter language code (e.g. 'en', 'ru', etc.)
 * @return int New or updated category identifier
 */
function fn_update_category($category_data, $category_id = 0, $lang_code = CART_LANGUAGE)
{
    /**
     * Update category data (running before fn_update_category() function)
     *
     * @param array  $category_data Category data
     * @param int    $category_id   Category identifier
     * @param string $lang_code     Two-letter language code (e.g. 'en', 'ru', etc.)
     */
    fn_set_hook('update_category_pre', $category_data, $category_id, $lang_code);
    // category title required
    if (empty($category_data['category'])) {
        //return false; // FIXME: management page doesn't have category name
    }
    if (isset($category_data['localization'])) {
        $category_data['localization'] = empty($category_data['localization']) ? '' : fn_implode_localizations($category_data['localization']);
    }
    if (isset($category_data['usergroup_ids'])) {
        $category_data['usergroup_ids'] = empty($category_data['usergroup_ids']) ? '0' : implode(',', $category_data['usergroup_ids']);
    }
    if (fn_allowed_for('ULTIMATE')) {
        fn_set_company_id($category_data);
    }
    $_data = $category_data;
    unset($_data['parent_id']);
    if (isset($category_data['timestamp'])) {
        $_data['timestamp'] = fn_parse_date($category_data['timestamp']);
    }
    if (isset($_data['position']) && empty($_data['position']) && $_data['position'] != '0' && isset($category_data['parent_id'])) {
        $_data['position'] = db_get_field("SELECT max(position) FROM ?:categories WHERE parent_id = ?i", $category_data['parent_id']);
        $_data['position'] = $_data['position'] + 10;
    }
    if (isset($_data['selected_layouts'])) {
        $_data['selected_layouts'] = serialize($_data['selected_layouts']);
    }
    if (isset($_data['use_custom_templates']) && $_data['use_custom_templates'] == 'N') {
        // Clear the layout settings if the category custom templates were disabled
        $_data['product_columns'] = $_data['selected_layouts'] = $_data['default_layout'] = '';
    }
    // create new category
    if (empty($category_id)) {
        if (fn_allowed_for('ULTIMATE') && empty($_data['company_id'])) {
            fn_set_notification('E', __('error'), __('need_company_id'));
            return false;
        }
        $create = true;
        $category_id = db_query("INSERT INTO ?:categories ?e", $_data);
        $_data['category_id'] = $category_id;
        foreach (fn_get_translation_languages() as $_data['lang_code'] => $v) {
            db_query("INSERT INTO ?:category_descriptions ?e", $_data);
        }
        $category_data['parent_id'] = !empty($category_data['parent_id']) ? $category_data['parent_id'] : 0;
        // update existing category
    } else {
        $category_data['old_company_id'] = db_get_field("SELECT company_id FROM ?:categories WHERE category_id = ?i", $category_id);
        $arow = db_query("UPDATE ?:categories SET ?u WHERE category_id = ?i", $_data, $category_id);
        $arow_description = db_query("UPDATE ?:category_descriptions SET ?u WHERE category_id = ?i AND lang_code = ?s", $_data, $category_id, $lang_code);
        if ($arow === false && $arow_description === false) {
            fn_set_notification('E', __('error'), __('object_not_found', array('[object]' => __('category'))), '', '404');
            $category_id = false;
        }
    }
    if ($category_id) {
        // regenerate id_path for all child categories of the updated category
        if (isset($category_data['parent_id'])) {
            fn_change_category_parent($category_id, intval($category_data['parent_id']));
        }
        // Log category add/update
        fn_log_event('categories', !empty($create) ? 'create' : 'update', array('category_id' => $category_id));
        // Assign usergroup to all subcategories
        if (!empty($_data['usergroup_to_subcats']) && $_data['usergroup_to_subcats'] == 'Y') {
            $id_path = db_get_field("SELECT id_path FROM ?:categories WHERE category_id = ?i", $category_id);
            db_query("UPDATE ?:categories SET usergroup_ids = ?s WHERE id_path LIKE ?l", $_data['usergroup_ids'], "{$id_path}/%");
        }
    }
    /**
     * Update category data (running after fn_update_category() function)
     *
     * @param array  $category_data Category data
     * @param int    $category_id   Category identifier
     * @param string $lang_code     Two-letter language code (e.g. 'en', 'ru', etc.)
     */
    fn_set_hook('update_category_post', $category_data, $category_id, $lang_code);
    return $category_id;
}
Beispiel #2
0
function fn_exim_1c_update_category($category_data, $category_id, $lang_code)
{
    $_data = $category_data;
    unset($_data['parent_id']);
    $_data['timestamp'] = fn_parse_date($category_data['timestamp']);
    if (empty($category_id)) {
        $create = true;
        $category_id = db_query("INSERT INTO ?:categories ?e", $_data);
        $_data['category_id'] = $category_id;
        foreach (fn_get_translation_languages() as $_data['lang_code'] => $v) {
            db_query("INSERT INTO ?:category_descriptions ?e", $_data);
        }
        $category_data['parent_id'] = !empty($category_data['parent_id']) ? $category_data['parent_id'] : 0;
    } else {
        db_query("UPDATE ?:categories SET ?u WHERE category_id = ?i", $_data, $category_id);
        db_query("UPDATE ?:category_descriptions SET ?u WHERE category_id = ?i AND lang_code = ?s", $_data, $category_id, $lang_code);
    }
    if ($category_id) {
        if (isset($category_data['parent_id'])) {
            fn_change_category_parent($category_id, intval($category_data['parent_id']));
        }
    }
    return $category_id;
}
Beispiel #3
0
function fn_update_category($category_data, $category_id = 0, $lang_code = CART_LANGUAGE)
{
    // category title required
    if (empty($category_data['category'])) {
        //return false; // FIXME: management page doesn't have category name
    }
    if (isset($category_data['localization'])) {
        $category_data['localization'] = empty($category_data['localization']) ? '' : fn_implode_localizations($category_data['localization']);
    }
    if (isset($category_data['usergroup_ids'])) {
        $category_data['usergroup_ids'] = empty($category_data['usergroup_ids']) ? '' : implode(',', $category_data['usergroup_ids']);
    }
    $_data = $category_data;
    if (isset($category_data['timestamp'])) {
        $_data['timestamp'] = fn_parse_date($category_data['timestamp']);
    }
    if (empty($_data['position']) && isset($_data['parent_id'])) {
        $_data['position'] = db_get_field("SELECT max(position) FROM ?:categories WHERE parent_id = ?i", $_data['parent_id']);
        $_data['position'] = $_data['position'] + 10;
    }
    if (!empty($_data['selected_layouts'])) {
        $_data['selected_layouts'] = serialize($_data['selected_layouts']);
    }
    if (isset($_data['use_custom_templates']) && $_data['use_custom_templates'] == 'N') {
        // Clear the layout settings if the category custom templates were disabled
        $_data['product_columns'] = $_data['selected_layouts'] = $_data['default_layout'] = '';
    }
    // create new category
    if (empty($category_id)) {
        $create = true;
        $category_id = db_query("INSERT INTO ?:categories ?e", $_data);
        if (empty($category_id)) {
            return false;
        }
        // now we need to update 'id_path' field, as we know $category_id
        /* Generate id_path for category */
        $parent_id = intval($_data['parent_id']);
        if ($parent_id == 0) {
            $id_path = $category_id;
        } else {
            $id_path = db_get_row("SELECT id_path FROM ?:categories WHERE category_id = ?i", $parent_id);
            $id_path = $id_path['id_path'] . '/' . $category_id;
        }
        db_query('UPDATE ?:categories SET ?u WHERE category_id = ?i', array('id_path' => $id_path), $category_id);
        //
        // Adding same category descriptions for all cart languages
        //
        $_data = $category_data;
        $_data['category_id'] = $category_id;
        foreach ((array) Registry::get('languages') as $_data['lang_code'] => $v) {
            db_query("INSERT INTO ?:category_descriptions ?e", $_data);
        }
        // update existing category
    } else {
        /* regenerate id_path for all child categories of the updated category */
        if (isset($category_data['parent_id'])) {
            fn_change_category_parent($category_id, intval($category_data['parent_id']));
        }
        db_query("UPDATE ?:categories SET ?u WHERE category_id = ?i", $_data, $category_id);
        $_data = $category_data;
        db_query("UPDATE ?:category_descriptions SET ?u WHERE category_id = ?i AND lang_code = ?s", $_data, $category_id, $lang_code);
    }
    // Log category add/update
    fn_log_event('categories', !empty($create) ? 'create' : 'update', array('category_id' => $category_id));
    // Assign usergroup to all subcategories
    if (!empty($category_data['usergroup_to_subcats']) && $category_data['usergroup_to_subcats'] == 'Y') {
        $id_path = db_get_field("SELECT id_path FROM ?:categories WHERE category_id = ?i", $category_id);
        db_query("UPDATE ?:categories SET usergroup_ids = ?s WHERE id_path LIKE ?l", $category_data['usergroup_ids'], "{$id_path}/%");
    }
    if (!empty($category_data['block_id'])) {
        fn_add_items_to_block($category_data['block_id'], $category_data['add_items'], $category_id, 'categories');
    }
    fn_set_hook('update_category', $category_data, $category_id, $lang_code);
    return $category_id;
}