function makeElementView($value) { global $parser; global $db; global $app; global $data; $element_params = $parser->element_params; $tree = $GLOBALS['TREE_' . strtoupper($parser->element_type_info)]; if (!arrayIsOk($tree)) { $app->raiseError('Category type error. <b>TREE_' . strtoupper($parser->element_type_info) . '</b> not found. In category.type.php->makeElementView()'); } $cross_field = isset($element_params['cross_field']) ? $element_params['cross_field'] : 'name'; $node = $tree['node']; $point = $tree['point']; $old_data = $data->name; $data->set($node); $parents = $db->get("id={$value}", 'parents', $data->table) . '<' . $value . '>'; $parents_ids = treeToArray($parents); $parent_in = implode(',', $parents_ids); $rows = $db->select("id IN ({$parent_in})", '', "id,{$cross_field}", $data->table); //sort result by known parents $new_rows = array(); $hash = listToHash($rows, 'id', $cross_field); foreach ($parents_ids as $v) { if (isset($hash[$v])) { $new_rows[$v] = $hash[$v]; } } if ($this->view_root === false) { reset($new_rows); unset($new_rows[key($new_rows)]); //unset first } //back from hash to array $rows = array_values($new_rows); $data->set($old_data); $rez = arrayIsOk($rows) ? implode($this->delimiter, $rows) : ''; return $rez; }
/** * Linearize a tree to an array * * Keys are cumulated to a single 'key.sub_key.final_key' key name * * @param $array mixed[] * @param $ignore_key string if set, this key is ignored and set as the 'main' value of a node * @return mixed[] */ function treeToArray($array, $ignore_key = null) { $result = []; foreach ($array as $key => $val) { if (is_array($val)) { foreach (treeToArray($val, $ignore_key) as $sub_key => $sub_val) { $result[$key . (strval($sub_key) === strval($ignore_key) ? '' : DOT . $sub_key)] = $sub_val; } } else { $result[$key] = $val; } } return $result; }
function treeToIn($parents) { $str=implode(',',treeToArray($parents)); return '('.$str.')'; }