I've built a couple of Add-Ons that have dashboard single pages. I really don't want to have multiple sections for each Add-On, so I have come up with this solution:
In the install() and upgrade() methods of the package controller:
$this->installDashboard($pkg);
Then the installDashboard() method in the package controller I add the single pages. Note that the package for the single page '/dashboard/sollos' is set to 'NULL'. I update the cFilename for these pages in the 'Pages' table and copy them from the package to the 'single_pages' directory at the root level.
function installDashboard($pkg) {
Loader::model('single_page');
$db = Loader::db();
$dashPages = array(
array('pkg'=>null,'path'=>'/dashboard/sollos','name'=>'Sollos Web Development','description'=>'Make your site a better place.')
,array('pkg'=>$pkg,'path'=>'/dashboard/sollos/new_addon','name'=>$this->pkgName,'description'=>$this->pkgDescription)
);
foreach ($dashPages as $dashPage) {
$dp = Page::getByPath($dashPage['path']);
if($dp->cID==0) {
$sp = SinglePage::add($dashPage['path'],$dashPage['pkg']);
$sp->update(array('cName'=>t($dashPage['name']), 'cDescription'=>t($dashPage['description'])));
$db->query("UPDATE Pages SET cFilename=? WHERE cID = ?", array($dashPage['path'].'.php',$sp->cID));
}
}function installDashboard($pkg) {
Loader::model('single_page');
$db = Loader::db();
$dashPages = array(
array('pkg'=>null,'path'=>'/dashboard/sollos','name'=>'Sollos Web Development','description'=>'Make your site a better place.')
,array('pkg'=>$pkg,'path'=>'/dashboard/sollos/new_addon','name'=>$this->pkgName,'description'=>$this->pkgDescription)
);
foreach ($dashPages as $dashPage) {
$dp = Page::getByPath($dashPage['path']);
if($dp->cID==0) {
$sp = SinglePage::add($dashPage['path'],$dashPage['pkg']);
$sp->update(array('cName'=>t($dashPage['name']), 'cDescription'=>t($dashPage['description'])));
$db->query("UPDATE Pages SET cFilename=? WHERE cID = ?", array($dashPage['path'].'.php',$sp->cID));
}
}
if (!is_dir(DIR_FILES_CONTENT.'/dashboard')) {
mkdir(DIR_FILES_CONTENT.'/dashboard', null, true);
}
copy($pkg->getPackagePath().'/single_pages/dashboard/sollos.php', DIR_FILES_CONTENT.'/dashboard/sollos.php');
}
Then in the uninstall() method of the controller I go through all the single pages installed on the site and search for pages that start with 'dashboard/sollos'. If only one exists, then it is the base single page and we remove it from the site.
function uninstall() {
parent::uninstall();
Loader::model('single_page');
$ssps = array();
$sps = SinglePage::getList();
foreach ($sps as $sp) {
if (strpos($sp->cPath,'/dashboard/sollos')!==false) {
$ssps[] = $sp;
}
}
if (count($ssps)<2) {
foreach($ssps as $ssp) {
$ssp->delete();
if (is_file(DIR_FILES_CONTENT.'/dashboard/sollos.php')) {
unlink(DIR_FILES_CONTENT.'/dashboard/sollos.php');function uninstall() {
parent::uninstall();
Loader::model('single_page');
$ssps = array();
$sps = SinglePage::getList();
foreach ($sps as $sp) {
if (strpos($sp->cPath,'/dashboard/sollos')!==false) {
$ssps[] = $sp;
}
}
if (count($ssps)<2) {
foreach($ssps as $ssp) {
$ssp->delete();
if (is_file(DIR_FILES_CONTENT.'/dashboard/sollos.php')) {
unlink(DIR_FILES_CONTENT.'/dashboard/sollos.php');
}
}
}
}
This seems to work in my development environment, but I don't know how well it would work in the wild; if there is an upgrade to the main single page in one Add-On, it would be necessary to update it in all Add-Ons.
Has anyone ever done this, or something similar, before? Any suggestions on making this approach better?
Essentially, it sounds like you would want to keep you top level dashboard page (/dashboard/sollos), so what you could do is just not pass the $pkg object when calling SinglePage::add on that one. What that will do is make it so that the page does not automatically uninstall when the package is removed. Then just do a check in your uninstall function to see if the package they are removing is the only one to be found under /dashboard/sollos, and if so just do