I wrote a mail "plugin" based on swiftmailer and using ajax as a mean
for sending user interactions back to the server. I followed the
discussion about the mail functionality to be added to habari and
habari's design philosophy. So here I am not trying to add a mail
functionality to Habari, nor presenting a release version of my plugin
per se, but rather I am experimenting with ajax and Habari.
That being, being new to Habari I had to ack the index.php code to be
able to make my asychronous calls to Habari, since I don't know yet
the "right" way to do ajax with Habari.
The technique I used allows me to load only the plugin concerned with
the request and return results from that plugin directly in the
response.
Here's how I proceded:
In entry.single.php I added at the relevant place this piece of code:
<?php if ( Plugins::is_loaded( 'Swiftmailer' ) ): ?>
<span class="post-mail-friend">
<?php $theme->show_mailer_prompt();?>
</span>
<?php endif; ?>
This is inserted by the theming engine as soon as a post is rendered
to the screen.
The theme_show_mailer_prompt( ) method in my plugin just returns a
template that I named 'swiftmailer', whose most relevant part to this
discussion is :
email post to a friend?
<script language="javascript">
function show_form_callback(responseText,statusCode,xhr)
{
//some code here
$('#send_email').livequery(
'click',
function(){$("<div>").load("/habari/",
{plugin:'swiftmailer',action:"mail"},send_email_callback)}
);
}
function show_form()
{
$("<div>").load("/habari/",
{plugin:'swiftmailer',action:"show_mail_form"},show_form_callback);
}
//some code here
</script>
So I send a 'plugin' token with the request specifying which plugin
should receive this request, and action to perform by the plugin.
In index.php I act based on the value of that token. The relevant code
in index.php is :
header( 'Content-Type: text/html;charset=utf-8' );
//used for ajax calls to specific plugins
if(array_key_exists('plugin',$_POST))
{
$plugins = Plugins::list_active();
foreach($plugins as $plugin)
{
$plugin_base = basename($plugin);
if($_POST['plugin'].'.plugin.php'==$plugin_base)
{
include_once($plugin);
Plugins::load($plugin);
Plugins::act('plugins_loaded');
// Start the session.
Session::init();
Plugins::act('init');
Plugins::act($_POST['action']);
exit();
}
}
echo($user_plugins.$_POST['plugin'].$ds.
$_POST['plugin'].'.plugin.php');
exit();
}
Then in my plugin (that I called unsurprisingly
swiftmailer.plugin.php) , I have methods like this one to perform the
action I want:
public function action_show_mail_form( )
{
$this->theme_load();
echo $this->theme->fetch('swiftmailer_form' );
}
with the theme_load function given here for clarity:
function theme_load()
{
$themes = new Themes();
$this->theme = $themes->create();
}
Now what I would like to how many Habari design principles I have
violated here :))
Moer seriously how to do this properly with Habari?
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/habari-dev
-~----------~----~----~----~------~----~------~--~---