/**
  * Standard constructor
  * @param string optional to manually set resource_type 
  */
 function STSelectResource($restype = "")
 {
     global $lang;
     if ($restype == "") {
         $this->restype = $_SESSION["resource_type"];
     } else {
         $this->restype = $restype;
         $_SESSION["resource_type"] = $restype;
     }
     if ($this->restype == "CLUSTER") {
         $this->add(new WZSelectCluster("guid"));
         $this->explanation = $lang->get("expl_sel_cluster", "After selecting a cluster-template, a list with clusters will appear, where you can select one.");
     } else {
         if ($this->restype == "CLUSTERTEMPLATE") {
             $this->explanation = $lang->get("expl_sel_clt", "Please Select a cluster-template.");
             $clts = array();
             createCLTTree($clts);
             $this->add(new WZSelect("guid", $lang->get("sel_clt", "Select Cluster-Template"), $clts));
         } else {
             if ($this->restype == "PAGETEMPLATE") {
                 $spms = createNameValueArray("sitepage_master", "NAME", "SPM_ID", "DELETED=0 AND VERSION=0");
                 $this->add(new WZSelect("guid", $lang->get("sel_ptml", "Select Page-Template"), $spms));
                 $this->explanation = $lang->get("expl_sel_spm", "Please select a page-template.");
             } else {
                 if ($this->restype == "CHANNEL") {
                     $channels = createNameValueArray("channels", "NAME", "CHID");
                     $this->add(new WZSelect("guid", $lang->get("sel_ch", "Select Channel"), $channels));
                     $this->explanation = $lang->get("expl_sel_ch", "Please select a channel.");
                 } else {
                     if ($this->restype == "CHANNELCATEGORY") {
                         $channels = getChannelDropDownValues();
                         $this->add(new WZSelect("guid", $lang->get("sel_ch", "Select Channel"), $channels));
                         $this->explanation = $lang->get("expl_sel_ch", "Please select a channel.");
                     }
                 }
             }
         }
     }
 }
}
drawIFOHeader();
$value = value("value", "NUMERIC");
if ($value != "0") {
    $clnid = $value;
    $clt = getDBCell("cluster_node", "CLT_ID", "CLNID = {$value}");
} else {
    $clt = value("clt", "NUMERIC");
    $clnid = "-1";
}
td($style);
echo $lang->get("sel_clt");
tde();
tr();
$clts = array();
createCLTTree($clts);
$sb = new Dropdown("clt", $clts, $style, $clt, $width);
$sb->params = 'onChange="if (this.value != \'-1\') document.ifoform.submit();"';
$sb->draw();
tr();
td($style);
echo drawSpacer(5);
echo '<script language="JavaScript">';
echo '  parent.document.getElementById("' . $callback . '").value = "' . $clnid . '";';
echo '</script>';
tde();
if ($clt != "-1" && $clt != "0" && $clt != "") {
    $clusters = createNameValueArray("cluster_node", "NAME", "CLNID", "CLT_ID = {$clt} AND VERSION=0 AND DELETED=0");
    tr();
    td($style);
    echo $lang->get("sel_cluster", "Select Cluster") . " (" . (count($clusters) - 1) . ")";
/**
 * Create a tree of the CLuster-Templates.
 * Recursive function. 	  
 * @param array array with name-value pairs of the folders
 * @param string prefix, which to write in front of all foldernames. Leave blank, is internally used.
 * @param integer node where to start indexing
 * @param boolean show compound clusters only.
 */
function createCLTTree(&$folder, $prefix = "&nbsp;&gt;&nbsp;", $node = 0, $compoundsOnly = false)
{
    global $db;
    // find CLT in homenode first.
    $compoundsOnly > 0 ? $filter = " AND CLT_TYPE_ID=1 " : ($filter = "");
    $sql = "SELECT NAME, CLT_ID FROM cluster_templates WHERE DELETED=0 AND CATEGORY_ID = {$node}  AND VERSION=0 {$filter} ORDER BY NAME ASC";
    $query = new query($db, $sql);
    while ($query->getrow()) {
        $name = $query->field("NAME");
        $id = $query->field("CLT_ID");
        $nextID = count($folder);
        $folder[$nextID][0] = $prefix . $name;
        $folder[$nextID][1] = $id;
    }
    $query->free();
    $sql = "SELECT CATEGORY_ID, CATEGORY_NAME from categories WHERE DELETED = 0 AND PARENT_CATEGORY_ID={$node} ORDER BY CATEGORY_NAME ASC";
    $query = new query($db, $sql);
    while ($query->getrow()) {
        $name = $query->field("CATEGORY_NAME");
        $id = $query->field("CATEGORY_ID");
        $nprefix = $prefix . $name . $prefix;
        createCLTTree($folder, $nprefix, $id, $compoundsOnly);
    }
    $query->free();
}