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:     global $cfg, $client, $lang;
47: 
48:     $db = cRegistry::getDb();
49: 
50:     $sql = "SELECT
51:                 a.name AS name,
52:                 a.idcat AS idcat,
53:                 b.parentid AS parentid,
54:                 c.level AS level
55:             FROM
56:                 " . $cfg['tab']['cat_lang'] . " AS a,
57:                 " . $cfg['tab']['cat'] . " AS b,
58:                 " . $cfg['tab']['cat_tree'] . " AS c
59:             WHERE
60:                 a.idlang   = " . (int) $lang . " AND
61:                 b.idclient = " . (int) $client . " AND
62:                 b.idcat    = " . (int) $idcat . " AND
63:                 c.idcat    = b.idcat AND
64:                 a.idcat    = b.idcat";
65: 
66:     $db->query($sql);
67:     $db->nextRecord();
68: 
69:     $parentid = $db->f('parentid');
70:     $thislevel = $db->f('level');
71: 
72:     if ($parentid != 0 && $thislevel >= $minLevel) {
73:         return cApiCatGetLevelNode($parentid, $minLevel);
74:     } else {
75:         return $idcat;
76:     }
77: }
78: 
79: