A plugin is definitely required here. Unfortunately, the Pages module doesn't store data in a hierarchical format, and the logic used in the module control panel is not ideal for use as a plugin (as you need a properly nested array for that). So you're starting from scracth.
But here's something to get you started: all pages are stored in $this->EE->config->item('site_pages')
, as an array with site_ids as keys, and then further arrays with entry_ids as keys and page URIs as values. So you'll need to query the titles for each.
Here's some quick starter code:
$pages = $this->EE->config->item('site_pages');
if ($pages === FALSE OR count($pages[$this->EE->config->item('site_id')]['uris']) == 0)
{
return false;
}
$pages = $pages[$this->EE->config->item('site_id')]['uris'];
natcasesort($pages);
$titles = array();
$entry_ids = array_keys($pages);
$entry_ids = implode(',', $entry_ids);
$titles_q = $this->EE->db->query("SELECT entry_id, title FROM exp_channel_titles WHERE entry_id IN($entry_ids)");
if($titles_q->num_rows() > 0)
{
foreach($titles_q->result_array() as $entry)
{
extract($entry);
$titles[$entry_id] = $title;
}
}
print_r($pages);
print_r($titles);
So this gives you two arrays, both with the entry_id as a key, and one with the URI as the value, and another with the Title as the value. From here you'll need to do some sorting, explode the URI values using a forward slash as a delimiter, and build a new array, etc. Lots of array juggling to build this with a proper hierarchy, and in the order you want it.
In the end, you want to end up with something like this (to pass to $this->EE->TMPL->parse_variables
):
$pages = array(
[0] => array(
'entry_id' => XX,
'title' => 'About Us',
'uri' => '/about',
'children' => array(
[0] => array(
'entry_id' => XX,
'title' => 'Staff',
'uri' => '/about/staff'
),
[1] => array(
'entry_id' => XX,
'title' => 'History',
'uri' => '/about/history'
),
)
),
[1] => array(
'entry_id' => XX,
'title' => 'Location',
'uri' => '/location',
'children' => ''
)
[2] => array(
'entry_id' => XX,
'title' => 'Contact Us',
'uri' => '/contact',
'children' => ''
)
);