[First checkin of the Hierarchy tom@wwworldmedia.com**20050615211535 This thing rocks. ] { addfile ./incl_hierarchy.phtml hunk ./incl_hierarchy.phtml 1 +levels = array(); + $this->DebuggableObject(); + } + + function addLevel($level_n, $label, $form_field_id, $get_values_callback, $cur_value = "") { + if (! preg_match("/[A-Za-z_]+[A-Za-z0-9_]*/", $form_field_id)) + $this->error("Invalid form field id \"$form_field_id\""); + if (! function_exists($get_values_callback)) + $this->error("Unknown function \"$get_values_callback\""); + + $this->levels[$level_n] = array( + "n" => $level_n, + "label" => $label, + "field_id" => $form_field_id, + "callback" => $get_values_callback, + "value" => $cur_value); + sajax_export($get_values_callback); + } + + function setTarget($type, $data_a = false, $data_b = false) { + if ($type == "var") $type = "variable"; + + if ($type == "variable") { + $this->target_type = $type; + $this->target_var = $data_a; + } else { + $this->error("setTarget(): \"$type\" is an unknown type"); + } + } + + function setValue($n_or_id, $value) { + if (is_numeric($n_or_id)) { + $this->levels[$n_or_id]["value"] = $value; + return true; + } else { + reset($this->levels); + foreach ($this->levels as $lvl) + if ($lvl["field_id"] == $n_or_id) { + $this->levels[$lvl->n]["value"] = $value; + return true; + } + } + } + + // Assign current values for each of the levels, based on the contents of $array. + function setValues($array) { + foreach ($this->levels as $lvl) { + $n = $lvl["n"]; + $id = $lvl["field_id"]; + if (! empty($array[$id])) + $this->levels[$n]["value"] = $array[$id]; + } + } + } + + class SelectHierarchy extends Hierarchy { + function SelectHierarchy() { + $this->Hierarchy(); + } + + function renderHeader() { + $html = ""; + return $html; + } + + function renderLevel($n) { + $this->debug("renderLevel($n).."); + reset($this->levels); + + $n_plus_1 = $n + 1; + $lvl = $this->levels[$n]; + $v_h = htmlspecialchars($lvl["value"]); + $id = $lvl["field_id"]; + $html = ""; + + $other_values_str = ""; + for ($i = $n - 1; $i >= 0; $i--) { + $other_values_str .= "Select.get(\"" . $this->levels[$i]["field_id"]."\"), "; + } + + // if we arent the final select.. + if ($n < count($this->levels)-1) + $next_action_str = "h_populate_" . $this->levels[$n_plus_1]["field_id"] . "();"; + else + $next_action_str = "h_finalize();"; + + $html .= " + + + + "; + + return $html; + } + + function renderFooter() { + if (empty($this->target_type)) + $this->error("renderFooter(): no target type configured"); + if ($this->target_type != "variable") + $this->error("renderFooter(): only target_type var supported"); + + reset($this->levels); + $concatter = array(); + for ($i = 0; $i < count($this->levels); $i++) { + $concatter[] = "Select.get(\"" . $this->levels[$i]["field_id"]."\")"; + } + $concat = join("+'%%%'+", $concatter); + $first_id = $this->levels[0]["field_id"]; + return " + + "; + } + + function render() { + $n = 0; + $html = $this->renderHeader(); + reset($this->levels); + foreach ($this->levels as $lvl) { + $html .= $this->renderLevel($n); + $n++; + } + $html .= $this->renderFooter(); + return $html; + } + + function handleRequest() { + $this->debug("handleRequest().."); + sajax_handle_client_request(); + return true; + } + } +?> }