1: <?php
2: /**
3: * This file contains CONTENIDO Category API functions.
4: *
5: * If you are planning to add a function, please make sure that:
6: * 1.) The function is in the correct place
7: * 2.) The function is documented
8: * 3.) The function makes sense and is generically usable
9: *
10: * @package Core
11: * @subpackage Backend
12: * @version SVN Revision $Rev:$
13: *
14: * @author Timo Hummel
15: * @copyright four for business AG <www.4fb.de>
16: * @license http://www.contenido.org/license/LIZENZ.txt
17: * @link http://www.4fb.de
18: * @link http://www.contenido.org
19: */
20:
21: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
22:
23: /**
24: * Seeks through the category tree and returns the node on a specific level.
25: *
26: * Example:
27: * + Category A (15)
28: * |-+ News (16)
29: * | |- News A (17)
30: * + Category B (18)
31: * |-+ Internal (19)
32: *
33: * Given you are in the leaf "News A" (idcat 17), and you want to get out in which
34: * "main" tree you are, you can call the function like this:
35: *
36: * cApiCatGetLevelNode(17,1);
37: *
38: * The example would return "Category A" (idcat 15). If you specify an invalid level,
39: * the results are undefined.
40: *
41: * @param int $idcat The category number
42: * @param int $minLevel The level to extract
43: * @return int The category node on a specific level
44: */
45: function cApiCatGetLevelNode($idcat, $minLevel = 0)
46: {
47: global $cfg, $client, $lang;
48:
49: $db = cRegistry::getDb();
50:
51: $sql = "SELECT
52: a.name AS name,
53: a.idcat AS idcat,
54: b.parentid AS parentid,
55: c.level AS level
56: FROM
57: " . $cfg['tab']['cat_lang'] . " AS a,
58: " . $cfg['tab']['cat'] . " AS b,
59: " . $cfg['tab']['cat_tree'] . " AS c
60: WHERE
61: a.idlang = " . (int) $lang . " AND
62: b.idclient = " . (int) $client . " AND
63: b.idcat = " . (int) $idcat . " AND
64: c.idcat = b.idcat AND
65: a.idcat = b.idcat";
66:
67: $db->query($sql);
68: $db->nextRecord();
69:
70: $parentid = $db->f('parentid');
71: $thislevel = $db->f('level');
72:
73: if ($parentid != 0 && $thislevel >= $minLevel) {
74: return cApiCatGetLevelNode($parentid, $minLevel);
75: } else {
76: return $idcat;
77: }
78: }
79:
80: