All pastes #497464 Raw Edit

Stuff

public text v1 · immutable
·published 2007-05-19 21:25 UTC
rendered paste body
function fn_get_categories($category_id = 0, $visible = false, $current_category_id = 0, $simple = true, $plain = false, $lang_code='')
{
	global $db_tables, $cart_language, $auth;

	$lang_code = !empty($lang_code) ? $lang_code : $cart_language;

	$conditions = array();

	if (AREA == 'C') {
		$conditions[] = "$db_tables[categories].membership_id IN ('0', '$auth[membership_id]')";
		$conditions[] = "$db_tables[categories].avail='Y'";
	}

	if ($visible == true) {
		$categories_ids = $category_id;
		if (!empty($current_category_id)) {
			$cur_id_path = db_get_field("SELECT id_path FROM $db_tables[categories] WHERE category_id='$current_category_id'");
			if (!empty($cur_id_path)) {
				$categories_ids .= ','.str_replace('/', ',', $cur_id_path);
			}
		}
		$conditions[] = "$db_tables[categories].parent_id IN ($categories_ids)";
	} elseif (!empty($category_id)) {
		$from_id_path = db_get_field("SELECT id_path FROM $db_tables[categories] WHERE category_id='$category_id'");
		$conditions[] = "$db_tables[categories].id_path LIKE '$from_id_path/%'";
	}


	if (!empty($conditions)) {
		$conditions = 'WHERE '. implode(' AND ', $conditions);
	} else {
		$conditions = '';
	}

	if ($simple == true) {
		$select_fields = "$db_tables[categories].category_id, $db_tables[categories].parent_id, $db_tables[categories].id_path, $db_tables[category_descriptions].category, $db_tables[categories].position";
	} else {
		$select_fields = "$db_tables[categories].category_id, $db_tables[categories].parent_id, $db_tables[categories].id_path, $db_tables[category_descriptions].category, $db_tables[categories].position, $db_tables[categories].avail, $db_tables[categories].product_count";
	}

	$categories = db_get_hash_array("SELECT $select_fields FROM $db_tables[categories] LEFT JOIN $db_tables[category_descriptions] ON $db_tables[categories].category_id = $db_tables[category_descriptions].category_id AND $db_tables[category_descriptions].lang_code='$lang_code' $conditions", 'category_id');

	if (empty($categories)) {
		return array();
	}
	$tmp = array();
	// Group categories by the level (simple)
	if ($simple == true) {
		foreach ($categories as $k => $v) {
			$v['level'] = substr_count($v['id_path'], "/");
			if (AREA == 'A' && (!empty($current_category_id) || $v['level'] == 0)) {
				$v['has_children'] = db_get_field("SELECT category_id FROM $db_tables[categories] WHERE parent_id='$v[category_id]' LIMIT 1");
			}
			$tmp[$v['level']][$v['category_id']] = $v;
		}
	} else {
		// 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('/', $category_path);
			$v['level'] = substr_count($v['id_path'], "/");
			if (AREA == 'A' && (!empty($current_category_id) || $v['level'] == 0)) {
				$v['has_children'] = db_get_field("SELECT category_id FROM $db_tables[categories] WHERE parent_id='$v[category_id]' LIMIT 1");
			}
			$tmp[$v['level']][$v['category_id']] = $v;
		}
	}

	ksort($tmp, SORT_NUMERIC);
	$tmp = array_reverse($tmp);

	foreach ($tmp as $level => $v) {
		foreach ($v as $k => $data) {
			if (isset($tmp[$level+1][$data['parent_id']])) {
				$tmp[$level+1][$data['parent_id']]['subcategories'][] = $tmp[$level][$k];
				unset($tmp[$level][$k]);
			}
		}
	}

	$tmp = array_pop($tmp);

	fn_sort($tmp, 'subcategories', 'fn_cat_sort');

	if ($plain == true) {
		return fn_plain_categories($tmp);
	} else {
		return $tmp;
	}
}