Example #1
0
 function get_tree($site_id = 0)
 {
     $with_spl = class_exists('SplFixedArray');
     $count = $this->set_where('site_id = %d', $site_id)->count_sql();
     if (!$count) {
         return;
     }
     self::$_tree_index_counter = -1;
     self::$_tree_index = $with_spl ? new SplFixedArray(1 + $count) : array();
     $nodes = array();
     $sql = sprintf("SELECT id, pid, title, html_title, url, c_children, active, b_system, b_featured FROM %s WHERE site_id = %d ORDER BY pid, position", $this->get_table(), $site_id);
     $return = array(tf_sat::TREE_URL => array(), tf_sat::TREE_ID => array());
     if ($query_id = $this->db->query($sql)) {
         while ($row = $this->db->fetch_row()) {
             $nodes[(int) $row['id']] = $row;
         }
     }
     core::dprint(array('count: %d, nodes: %d', $count, count($nodes)));
     if (empty($nodes)) {
         return $return;
     }
     $item = array('url' => '', 'c_children' => 1, 'id' => 0, 'level' => -1);
     $this->_get_tree($item, $nodes);
     core::dprint(array('_tree_index_counter: %d, _tree_index: %d, nodes: %d', self::$_tree_index_counter, count(self::$_tree_index), count($nodes)));
     // Note: splFixedArray return wrong count(self::$_tree_index)
     // $count = $with_spl ? ($count - 1) : $count;
     // 0 goes for root, skip it
     for ($index = 1; $index <= $count; $index++) {
         $check_ti = isset(self::$_tree_index[$index]);
         $check_node = $check_ti && isset($nodes[self::$_tree_index[$index]]);
         if (!$check_ti || !$check_node) {
             core::dprint(array('Waring! Update tree goes wrong, index %d | %s, %s', $index, $check_ti ? 'Y' : 'N', $check_node ? 'Y' : 'N'));
         } else {
             $r = $nodes[self::$_tree_index[$index]];
             $r['url'] = '/' . $r['url'];
             $return[tf_sat::TREE_URL][$r['url']] = $r['id'];
             $return[tf_sat::TREE_ID][$r['id']] = $r;
         }
     }
     core::dprint(array('$return[tf_sat::TREE_ID]: %d', count($return[tf_sat::TREE_ID])));
     /*
     //debug:
     core::var_dump(
         $count, 
         self::$_tree_index_counter,
         count((array)self::$_tree_index),
         array_diff((array)self::$_tree_index, $return[tf_sat::TREE_URL])            
     );
     */
     unset($nodes);
     self::$_tree_index = null;
     if ($count != count($return[tf_sat::TREE_ID])) {
         core::dprint(array('Update tree count mismatch! ALL: %d <> INDEX: %d', $count, count($return[tf_sat::TREE_ID])), core::E_CRIT);
     }
     return $return;
 }