Jump to content

Adminbar


apeisa

Recommended Posts

Martin: Thanks!

Ryan: what would be best approach, if I would like to create sitemap for AdminBar? Or any other "customized" page?

My initial plan is this:

Check if there is template called "AbminBarPage"

->if not, create template (title & body fields)

Check if there is page /adminbar/sitemap/

-> if not, create page

-> put needed code into body

It would be probably good idea to actually produce each view as AdminBarPage, then this module wouldn't break on changes on admin templates (or if some site has custom admin etc).

Link to comment
Share on other sites

These are just thoughts, but my first idea was that it would provide full site map (well, maybe not full if there is very deep levels..) in open state, show clearly where the current page is. Then of course links to move to other pages. Probably no need to edit, move etc (since these are easily done through real admin).

In short: to provide quick look over the whole site and where the current page is.

Link to comment
Share on other sites

I made some progress here.

Most important thing first: PW source is very well documented and it just makes sense! After half an hour I started guessing method names and usually got them always right :P

I have now version working, where I create adminbar-template (with fieldgroup & title field), adminbar-templatefile & and finally create the page. I'm not sure if this is right way to go, but I see huge benefits this way:

  • Template-files that adminbar uses goes to site/templates/adminbar folder, so they are easily customized per site basis
  • We could have ie. "Information for content editors" field that is showed everyone who is editing content etc. Maybe more settings per different view etc?
  • This gives full control to create modal views as I like them to be - easier to develop than hooking. Of course there we have to be careful not to re-invent the wheel and keeping UI consistent with other aspects of PW

Only negative things I can think of now are

  • More templates, more pages, more templatefiles...
  • But probably just a good thing, if those can be visible only to superadmins (I don't know yet how UA works)

This was quick hack, but if Ryan confirms that this is not worst way of developing AdminBar further, I'll probably go forward and implement this at least on the views that current admin doesn't provide (I know correctly this means only the "full sitemap view" so far).

Link to comment
Share on other sites

re: content editors: just a note: as Ryan previously stated, everything about Users [user roles, intercommunication, data information, versioning] is mostly in very crude, unpolished state – because Pages and overall API had hightest priority. Therefore, for few things there are no solutions currently [who's editing what, etc]

Link to comment
Share on other sites

I have it working nicely... almost :) One little problem that I cannot figure out. For some reason this code gives me HTTP-error 500, but only once (first time it runs). Probably some silly error (I create stuff on wrong order or something similar..? Or save() doesn't return the object I assume it returns? Ryan, any ideas what I am doing wrong?

I have also attached full module if anyone is interested to try. There is one new feature and it is quick collapse-enabled sitemap to show where the current page is located and to quickly move around your site.

I know this code could be cleaned up a lot, but be gentle - I am just learning this stuff :)

private function _createAdminBarPages() {
	$abName = "adminbar";
	$abFieldGroup = $this->fieldgroups->get($abName);
	$abTemplate = $this->templates->get($abName);
	$abPage = $this->pages->get("/processwire/{$abName}/");

	if(is_null($abFieldGroup))
		$abFieldGroup = $this->_createAdminBarFieldGroup($abName);

	if(is_null($abTemplate))
		$abTemplate = $this->_createAdminBarTemplate($abName, $abFieldGroup);

	if(!file_exists($abTemplate->filename()))
		$this->_createAdminBarTemplateFile($abTemplate);

	if(!$abPage->id)
		$abPage = $this->_createAdminBarPage($abName, $abTemplate);

}

private function _createAdminBarFieldGroup($fieldGroupName) {
	$abFieldGroup = new Fieldgroup();
	$abFieldGroup->name = $fieldGroupName;
	$abFieldGroup->add("title");
	return $abFieldGroup->save();
}

private function _createAdminBarTemplate($templateName, $abFieldGroup) {
	$abTemplate = new Template();
	$abTemplate->name = $templateName;
	$abTemplate->setFieldgroup($abFieldGroup);
	return $abTemplate->save();
}

private function _createAdminBarPage($abName, $abTemplate) {

	// create /adminbar/ page under admin
	$abPage = new Page();
	$abPage->template = $abTemplate;
	$abPage->parent = $this->pages->get("/processwire/"); // TODO: How to remove hardcoding admin url??
	$abPage->title = $abName;
	$abPage->name = $abName;
	$abPage->status = 1031; // hidden & locked
	$abPage->save();

	// after that create /adminbar/adminbpar-sitemap/
	$abSitemapPage = new Page();
	$abSitemapPage->template = $abTemplate;
	$abSitemapPage->parent = $abPage;
	$abSitemapPage->title = $abName . '-sitemap';
	$abSitemapPage->name = $abName . '-sitemap';
	$abSitemapPage->status = 1031;
	return $abSitemapPage->save();
}

private function _createAdminBarTemplateFile($abTemplate) {
	copy(dirname(__FILE__)."/pages/sitemap.php", $abTemplate->filename());
}

AdminBar.zip

Link to comment
Share on other sites

Apeisa,

I tried this out and it works great! Nice work. The site map view is very useful too. Also, I think the code looks good! Here's a few suggestions and notes I've got:

The 500 error you got was thrown by ProcessWire. The problem is these lines:

return $abFieldGroup->save();
return $abTemplate->save();

All the save functions return a result status, basically true if it was successful, false if not. So to fix the error, you would need to change lines like the above to:

$abFieldGroup->save();
return $abFieldGroup;

$abTemplate->save();
return $abTemplate; 

When you are developing in ProcessWire, you may want to enable debug mode, so that errors are echoed to the screen. You can turn this on in /site/config.php. Look for the line that says $config->debug = false; and change it to true.

Next, I wanted to mention that the module interface provides install() and uninstall() methods, in case you want them:

/**
* Perform any installation procedures specific to this module, if needed. 
* 
* This method is OPTIONAL
*
*/
public function ___install();

/**
* Perform any uninstall procedures specific to this module, if needed. 
*
* This method is OPTIONAL
*
*/
public function ___uninstall();

There may be issues on many installations with your last function, _createAdminBarTemplateFile. The reason is that ProcessWire doesn't usually have write access to the templates dir (it depends on the server). So your installer may be want to look for this, or advise them to copy your template file in there if there isn't write access. Or, we could just make it a blank template (without a file) and then hook into it's render method so that your .module file can render the output instead. Let me know if you want to do that, and I can provide instructions.

Lastly, because the site map outputs a full sitemap, it may not scale to large installations. For instance, the site I am working on now has 5k+ pages, and while it renders them all, it takes several seconds. :) But on large installations, it might just run out of memory too. As a result, I would recommend placing a limit to the number of subpages that the sitemap will display. Maybe a "limit=50" per branch, just to account for the possibility of large sites. And once it reaches that limit, it might have a label that says "Go to the admin to view remaining pages" or something like that.

Again nice work on this, I am really enjoying using this module!

Link to comment
Share on other sites

Ryan, thanks for your answer. I wasn't sure about the write access on template dir (I develop on windows when there is basicly always write access).

So your installer may be want to look for this, or advise them to copy your template file in there if there isn't write access. Or, we could just make it a blank template (without a file) and then hook into it's render method so that your .module file can render the output instead. Let me know if you want to do that, and I can provide instructions.

Hmm.. I think that the latter is better. Or maybe both? If there is template file -> use that. If not -> render. Is that possible? The first reason I went for this "copy template file" route was that I didn't know any other way to make this work :)

Link to comment
Share on other sites

I think it might be better to just override the template rather than putting one in the /templates/ dir (unless you want to encourage them to edit it). I've modified the Template class so that you can change the target filename at runtime, so you'll want to grab the latest commit.

https://github.com/ryancramerdesign/ProcessWire/commit/0c8dc7d8e62c9a6a110791e00111c463ff00b01c

Next, move that template to the same directory as your module (/site/modules/AdminBar/adminbar.php).

Then in your AdminBar.module, add a new "before" page render hook. It will check to see if the page template is "adminbar", and if it is, it'll change the template filename to the one in your /site/modules/AdminBar/ dir.

<?php
public function beforePageRender($event) {
    $page = $event->object;
    if($page->template == 'adminbar') {
        $page->template->filename = $this->config->paths->AdminBar . "adminbar.php";
    }
}

Also, add this to your init() function in AdminBar.module:

$this->addHookBefore('Page::render', $this, 'beforePageRender');

If you do decide you want it to check if the file is in your /site/templates/ first, then just add that check to your beforePageRender method.

Now it should work exactly the same as before, except that it'll use the template file in your /site/modules/AdminBar/ dir rather than expecting one to be in /site/templates/.

Link to comment
Share on other sites

Lastly, because the site map outputs a full sitemap, it may not scale to large installations. For instance, the site I am working on now has 5k+ pages, and while it renders them all, it takes several seconds. :) But on large installations, it might just run out of memory too. As a result, I would recommend placing a limit to the number of subpages that the sitemap will display. Maybe a "limit=50" per branch, just to account for the possibility of large sites. And once it reaches that limit, it might have a label that says "Go to the admin to view remaining pages" or something like that.

I created this functionality and noticed that $page->numChildren gives num of all children (hidden also). Is there some selector that I can use to count only visible children?

Link to comment
Share on other sites

Well.. there is small bug. After deleting a page we get this error:

Call to a member function isAjax() on a non-object (line 62 of C:\apache\htdocs\pw\wire\core\admin.php)

This error message was shown because you are logged in as a Superuser.

This must be because of modal somehow? Deleting pages sends them to thrash (so it works), but we still get that error. I could just disable delete-tab on modal, but I think that is a nice feature. Any clues on this one Ryan?

Link to comment
Share on other sites

I should sort it out for IE, finally. But, unfortunately, no time :( I should also style that hide link a little {maybe another glyph?}

good work Antti.

Thanks Adam. And no reason for stress because of this, early days and most important thing is to learn module dev.

If someone has plans to use this on client site then please us know and we'll fix the "browser".

Link to comment
Share on other sites

I created this functionality and noticed that $page->numChildren gives num of all children (hidden also). Is there some selector that I can use to count only visible children?

There is. Try this:

$total = $page->children("limit=2")->getTotal();

You have to do "limit=2" (or higher) rather than "limit=1" because PW2 doesn't currently count totals on limit=1 selectors. That's just as a matter of efficiency, since all $pages->get() functions use limit=1 internally.

Link to comment
Share on other sites

This must be because of modal somehow? Deleting pages sends them to thrash (so it works), but we still get that error. I could just disable delete-tab on modal, but I think that is a nice feature. Any clues on this one Ryan?

I'm not sure why that error was turning up, and am out of time to take an in-depth look, but I was able to fix it easily. And actually I think it's something I should have had in there in the first place (an extra check to make sure that $controller is not null), so thank you for finding it. :) I have fixed this in the latest commit:

https://github.com/ryancramerdesign/ProcessWire/commit/99454cb68b0ccfe287ab82c68c9db7010a6f94be

AdminBar is looking great! Would you mind if I tweeted a link to your screencast?

Link to comment
Share on other sites

Updates: IE is fucking nightmare:


  •  
  • minor updates on github
     
  • still old readme [wanted to do the markdown version, no chance
     
  • IE7 isn't supported even by the PW Admin, no prob there [iE6 wasn't even tried]
     
  • IE8 is doing something funky with z-index, so hover doesn't work. I solved the width, but this is killing me. Also, BG of overlay isn't showing (because IE8 doesn't know rgba colors)
     
  • IE9 can't be virtualized in XP, and I don't have Win7 virtualized currently [neither install CD]. Also, IE9 is beta, so no hard feelings there

This should be it, I might look on the site tree later on too, because it's fugly.

Also I'm pissed, nothing works tonight. Good night, more updates tomorrow or later.

Link to comment
Share on other sites

I'm not sure why that error was turning up, and am out of time to take an in-depth look, but I was able to fix it easily. And actually I think it's something I should have had in there in the first place (an extra check to make sure that $controller is not null), so thank you for finding it. :) I have fixed this in the latest commit:

https://github.com/ryancramerdesign/ProcessWire/commit/99454cb68b0ccfe287ab82c68c9db7010a6f94be

AdminBar is looking great! Would you mind if I tweeted a link to your screencast?

Thanks for the fix. And sure you can tweet the screencast.

Link to comment
Share on other sites

I merged Adam's changes to adminbar. Also took cleaning further: I removed other options than modal and also cleaned js & css even more. It should work in IE now (I have only IE9 on my home computer and it works on that now. I am pretty confident that it works ok in other IE:s too - but no promises here :)).

Reasoning for taking options out: I think shortcuts will be the actual view that everyone uses: editing & creating will be done there. "Main" view will be for looking more information: who edited and when etc (hope to make that better later). And I think admin link should always take to admin front.

Also: I think this as real 1.0 release. I'll fix all the bugs that people may a rise, but any new features will be added with care. Of course all the optimizing to code and UI are most welcome (from Adam or anyone who is interested in contributing).

Link to comment
Share on other sites

I receive error when installing:

Exception: Can't save page 0: /adminbar/: It has no parent assigned (in www\phreshr.tv\wire\core\Pages.php line 337)

#0 [internal function]: Pages->___save(Object(Page))

#1 www\phreshr.tv\wire\core\Wire.php(241): call_user_func_array(Array, Array)

#2 www\phreshr.tv\wire\core\Wire.php(203): Wire->runHooks('save', Array)

#3 www\phreshr.tv\wire\core\Page.php(733): Wire->__call('save', Array)

#4 www\phreshr.tv\wire\core\Page.php(733): Pages->save(Object(Page))

#5 www\phreshr.tv\site\modules\AdminBar\AdminBar.module(312): Page->save()

#6 www\phreshr.tv\site\modules\AdminBar\AdminBar.module(284): AdminBar->_createAdminBarPage('adminbar', Object(Template))

#7 ww

This error message was shown because you are logged in as a Superuser.

Any suggestions on whats wrong? It had worked briefly. Then all of a sudden it didn't. I'm on a wamp stack and using php 5.3.5.

Link to comment
Share on other sites

Thanks for trying AdminBar and sorry for problems.

Error is probably that I have hardcoded the /processwire/ site as admin site. If you have renamed your admin (so that url is not /processwire/), then it will fail like that. You can make quick fix by editing two lines (chance them to have your admin page url):

https://github.com/apeisa/AdminBar/blob/master/AdminBar.module#L275

https://github.com/apeisa/AdminBar/blob/master/AdminBar.module#L308

I'll take a look how I could remove that hardcoding later today (I tried that earlier, but didn't work - Ryan, any clues? Probably some silly typo I had there...).

Link to comment
Share on other sites

You can get the admin URL from:

$config->urls->admin

Or if you want to retrieve the admin page:

$admin = $pages->get($config->adminRootPageID); 

Btw, I don't often do it, but it is good security to change your admin page name. You do this by editing it, clicking on the 'settings' tab and changing the name.

Link to comment
Share on other sites

Btw, I don't often do it, but it is good security to change your admin page name. You do this by editing it, clicking on the 'settings' tab and changing the name.

Heh, I made this and got redirect to 404. I thought it was somehow because of AdminBar.. but it wasn't. Spend few minutes itching my head and disabling all redirects on AdminBar.module... :)

Thanks for the tips Ryan. Now AdminBar works with any admin url, latest files are on GitHub.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...