<?php
	class Tree {
		 public function __construct() {
        $this->db = DBConnectorFactory::loadDriver();
    }

		public function buildHierarchyArrary(array $chuncks, $pid = 0) {
		    $tree = array();
		    foreach ($chuncks as $chunck) {
		        if ($chunck['Parent_ID'] == $pid) {
		            $children = $this->buildHierarchyArrary($chuncks, $chunck['Account_ID']);
		            if ($children)
		                $chunck['children'] = $children;
		            $tree[] = $chunck;
		        }
		    }
		    return $tree;
		}

		public function buildListFromHierarchyArrary(array $elements) {
			echo '<ol>';
			foreach ($elements as $key => $element) {
				echo '<li><a class="venobox" data-type="ajax" href="charts.php?ChartID='.$element['Account_ID'].'">' . $element['name'] . '</a>';
				if (array_key_exists('children', $element))
					$this->buildListFromHierarchyArrary($element['children']);
				echo '</li>';
			}
			echo '</ol>';
		}

		public function buildListFromHierarchyArraryList(array $elements) {
			// echo '<ol>';
			foreach ($elements as $key => $element) {
				echo '<li><div>'. $element['name'] . '</div>';
				if (array_key_exists('children', $element)) {
					echo '<ul><li><div>';
					$this->buildListFromHierarchyArraryList($element['children']);
				echo '</div></li>
				';
				}
				echo '</div></li>';
			}
			// echo '</ol>';
		}
		
		public function getChartOFAccounts(){
            //$this->db->query("SET NAMES utf8;");
            //$this->db->query("SET CHARACTER_SET utf8;");
			$query = $this->db->query('SELECT * FROM tbl_chart_of_accounts');
			$index = 0;
			while ($row = $this->db->assoc($query)){
				$row["Account_Name"] = ucwords($row["Account_Name"]);
	    		$data[] = $row;
	    	}
           
			return $data;
		}


	}
