Observers

Feed 26 posts, 10 voices

Avatar
51 posts

Hi all,

The Observer pattern is a good way to extend frog functionality with plugins. Thank you Philippe to have implemented it!

I just don't found a good list of all events that's can be observeable (this page is not totally up-to-date).

Here the actual (r162) list:

  • view_page_edit_plugins
    Can be used to add custom field at the bottom of the page edit view
    Parameters:

    • $page: the Page model object
  • view_page_edit_tabs
    Can be used to add a tab in the page edit view
    Parameters:

    • $page: the Page model object
  • view_page_edit_popup
    ?
    No parameter.

  • page_edit_before_save
    Can be used to perform special stuff before saving the page. No parameter.

  • page_not_found
    When a page is not found
    No parameter.

  • page_found
    just before displayed the page
    Parameters:

    • $page: the Page model object

I have some idea to improve this list:

First, it would be nice to have a reference to the page object in parameter when it is possible and when there are a sense (for example the page_edit_before_save event have no parameter).

It would be nice to have more observers too. Here some ideas:

  • page_add_before_save (with a &$page params)
  • page_delete (with an $id or $page params)
  • part_edit_before_save (with a &$part param)
  • page_requested (with a $url param) : I need an event like this in the frontend/main.php before trying to found a page. It would be useful to implement a cache plugin.

What do you think about that ?

 
Avatar
343 posts

These observers have been updated in the v0.9.3! The view_page_edit_popup has been edited. The view_page_edit_popup was replaced by view_page_edit_plugins, but the code was not updated. Your ideas are pretty good.

 
Avatar
541 posts

ho yea I like those kind of idea :)

and I will really impress to have a cache plugin too.

I add those observer right now !!

 
Avatar
541 posts

with the page_requested one, i think I will trow the unparsed $url ( with full query ) !! and the parsed one will stil be available with the CURRENT_URI constant.

 
Avatar
541 posts

and for the page_add_before_save I can't pass the page if it is not yet saved !!

 
Avatar
51 posts

Thanks! :)

I will release a first version of the file cache plugin soon. I just need to do more tests and small doc.

 
Avatar
541 posts

good :)

I have add those observer: page_add_after_save [$page], page_edit_after_save [$page], page_delete [$page], part_edit_after_save [$part], page_requested [$full_query_url]

 
Avatar
486 posts

Can anyone give me a brief code example for using page_add_after_save?

For example, emailing the page contents somewhere…

I’m interested in doing an Upcoming.org plugin so that events created in Frog can automatically be published to Upcoming….

 
Avatar
51 posts

Hi ricks,

Sure, it’s really easy :-) See this exemple:

// in your plugin index.php file
Observer::observe('page_edit_after_save', 'my_simple_observer');

// in the same file
function my_simple_observer($page)
{
    /* do want do you want */
   var_dump($page);
}

But perhaps, for your need, it’s better to use the ‘part_edit_after_save’ event:

Observer::observe('part_edit_after_save', 'my_simple_part_observer');

function my_simple_part_observer($part)
{
  if('body' == $part->name)
  {
     /* do something with the body content ($part->content or $part->content_html) */
     var_dump($part);
  }
}
 
Avatar
486 posts

awesome. thank you, gido!

 
Avatar
651 posts

@gido – I shamelessy stole your code example and placed it in the site’s Docs section in preperation for a more complete document about Observers… when I find the time.

 
Avatar
51 posts

@mvdkleijn – no problems ;)

I have some idea to extends the layout system and I want to know if it would be possible to add following observers in frog core:

  • view_page_edit_layout
  • layout_edit_after_save or layout_edit_before_save

It’s really not urgent, but it would be nice to have an implementation of these events in all main controllers in frog admin.

 
Avatar
15 posts

Maybe my problem fits in here.

I have form, I use Observer – page_found and problem is when form has errors, how can I serve those error variables in frontend?

Lets say:

$name = '';
if(empty($name)) { $error = 'Your name is null'; }

So how can I server this $error variable into frontend?

thanks..

 
Avatar
651 posts

Marko, you could use the following code to produce a red “flash” error message with the message you want:

<?php Flash::set('error','Your name is null'); ?>

and then just redirect back to the original form/page. It depends a lot on your specific situation.

 
Avatar
184 posts

How would I go about observing:
/admin/setting/plugins

I’ve tried various attempts but alas nothing has worked thus far:
Observer::observe('view_backend_setting_index', 'orderplugins')

I thought there was a naming forumla, but I don’t see it yet!?

 
Avatar
651 posts

@BlueFrog – you observe events, not pages.. at least it sounds like you’re trying to observe a page. If you’re trying to observe people requesting a specific page, you’ll need the page_requested event. It provides you with an argument containing the page’s name.

Or… you’re trying to observe something for which there isn’t an event yet?

 
Avatar
184 posts

Hi Martijn,

Indeed, I’m trying to observe the admin Plugins page in the same way as ‘view_backend_list_plugin’ checks for plugin pages. Is this possible yet? I’m trying to modify the appearance of the Plugins page before it returns to the browser.

 
Avatar
651 posts

No such event… sorry. Though you can add it yourself offcouse… just needs the one line similar to:

<?php Observer::notify('view_page_edit_plugins', $page); ?>

Checkout …/frog/app/views/page/edit.php for this line.

Why do you want to alter the layout through an event? (just being curious here ;-))

 
Avatar
184 posts

Hi Martijn,

As I have come to understand it, there is no other way to override the actual html output of Frog in the backend!? CSS is a fine start, but in some instances I need to be sure that a user does not have access to something particular (a stylesheet can always be disabled to otherwise allow access to something).

Ultimately, I am trying to set up some finer role permissions … without actually having to go to the extent of writing a complicated plugin.

 
Avatar
32 posts

Ultimately, I am trying to set up some finer role permissions …

How will you stop role elevation? Every editor can write php in the pages…

 
Avatar
184 posts

Jonte > How will you stop role elevation?

I am aware of users being able to inject code in the admin area, thankfully non of my immediate users are external to the company nor are they knowledgable about such things as code injection.

Like many Frog users however, I am hoping these security issues are resolved in the core, by the time I need to expose the CMS to external clients.

 
Avatar
651 posts
Like many Frog users however, I am hoping these security issues are resolved in the core, 

The core will always try to be as secure as possible and as lean, simple, useable as possible. BUT, as long as people are allow to write PHP code, there is no way to stop role elevation. That said, I am looking at ways to (dis)allow people writing PHP based on there permissions.

I.e. only “developer” and “administrator” roles would be allowed to write PHP in the future. Still allows role elevation, but should lessen the risk factor.

 
Avatar
76 posts

What about allowing developers/editors to only include Snippets in pages – and only allowing administrators to create/edit snippets.

That way, the lesser privileged don’t get to write any potentially-dangerous code themselves, but can still utilise already-made and approved bits of code.

Or a configurable combination of the above items?

 
Avatar
184 posts

I.e. only “developer” and “administrator” roles would be allowed to write PHP in the future. Still allows role elevation, but should lessen the risk factor.

Following from Craig’s comment, perhaps allowing only developers to set/change roles/permissions of other developers could be an idea. Administrators in my view are editors with more managerial clout … hence they could allow no role/permission changes higher than an administrator!? Developers write code, thus any php code should be kept to page parts accessible only to other developers. That way, administrators or editors could write javascript or other code related content … but development is left to developers?

I currently have a modded version of Frog where certain page parts and snippet are only available to developers. As the developer, my job is to ensure that any tabs, snippets or content that are exposed to everyone else is WYSIWYG friendly. Perhaps a similar approach could be applied to Frog core!?

 
Avatar
651 posts

My thoughts for the future direction of the security system in Frog where actually more going towards a combination of roles and permissions.

The current “administrator” and “developer” roles would be changed to be containers for certain permissions.

You could then setup permissions on certain types of actions/objects. For example, a certain role could have a “view” permission on pages but no “modify” permission.

I was thinking about something like: “View”, “Create”, “Modify”, “Administer”

Still just a brainstorm though, haven’t really given it much concrete thought yet.

The underlying principle would be to keep it as simple as possible, have a good clean default and it should allow you to say things like “Users with role XYZ are allowed to create objects of type Page”.

Notice the work objects… If we’d use something like this, I’d like to use the same set of permissions for everything in Frog. Pages, Comments, Layouts, Snippets, etc.