/**
  * Returns the whole folder structure in a tree
  *
  * @param array $filedirs if not empty will restrict to these EE upload dirs
  * @return array
  */
 public function get_folder_tree($filedirs = array())
 {
     $query = "SELECT f.*\n\t\t\t\t\tFROM exp_assets_folders AS f\n\t\t\t\t\tLEFT JOIN `exp_upload_prefs` AS p ON f.source_type = 'ee' AND f.filedir_id = p.id AND p.site_id = %s\n\t\t\t\t\tWHERE\n\t\t\t\t\t\t(f.source_type <> 'ee' OR p.id IS NOT NULL)\n\t\t\t\t\t\t%s\n\t\t\t\t\tORDER BY\n\t\t\t\t\t\tf.source_type ASC,\n\t\t\t\t\t\tf.full_path ASC";
     $filedir_query = "";
     if (!empty($filedirs) && is_array($filedirs)) {
         $parts = array();
         foreach ($filedirs as $filedir) {
             if (!strpos($filedir, ':')) {
                 $source_type = 'ee';
                 $source_id = $filedir;
             } else {
                 list($source_type, $source_id) = explode(":", $filedir);
             }
             if (!preg_match('/^[a-z0-9]{2}$/i', $source_type)) {
                 continue;
             }
             if ($source_type == 'ee') {
                 $parts[] = '(f.source_type = "ee" AND f.filedir_id = ' . intval($source_id) . ')';
             } else {
                 $parts[] = '(f.source_type = "' . $source_type . '" AND f.source_id = ' . intval($source_id) . ')';
             }
         }
         $filedir_query = 'AND (' . join(" OR ", $parts) . ')';
     }
     $sql = sprintf($query, $this->EE->config->item('site_id'), $filedir_query);
     $rows = $this->EE->db->query($sql)->result();
     $tree = array();
     $reference_store = array();
     foreach ($rows as $row) {
         $tree_node = (object) array('id' => $row->folder_id, 'name' => $row->folder_name, 'type' => $row->source_type, 'filedir_id' => $row->filedir_id, 'source_id' => $row->source_id, 'children' => array());
         if ($row->parent_id) {
             $reference_store[$row->parent_id]->children[] = $tree_node;
         } else {
             $tree[] = $tree_node;
         }
         $reference_store[$row->folder_id] = $tree_node;
     }
     // remove top-level folders (upload locations) that the member does not have permission to view
     require_once PATH_THIRD . 'assets/sources/ee/source.ee.php';
     $denied_filedirs = Assets_ee_source::get_denied_filedirs();
     $filtered_tree = array();
     foreach ($tree as $fieldir) {
         if (!in_array($fieldir->filedir_id, $denied_filedirs) || $fieldir->type != 'ee') {
             $filtered_tree[] = $fieldir;
         }
     }
     return $filtered_tree;
 }