Using Ajax in plugin

16 years 3 months ago #55117 by Narya
Using Ajax in plugin was created by Narya
Does the CB plugin API support ajax based background communication?


I'm trying to:
write (and release) a plugin that lets visitors search for users in the profile page. While the user is entering the name, similar usernames are meant to pop-up to be selected by the user. (An example of this can, for instance, be found at this site . Just begin typing a name in the view stats field to see what I mean.)


What I've found so far:
  • The javascript file handling http requests in CB. (comprofiler/js/cb.js)
  • The ajax based username check during registration (comprofiler.html.php cbSendUsernameCheck())

My problem is:
  • It does not look like there is a possibility to "forward" an ajax request to a plugin. The URL used by the username check is [code:1]index2.php?option=com_comprofiler&task=checkusernameavailability&no_html=1&format=raw[/code:1]. So it is handled as a specific (and not extendable) task in the comprofiler core class...
  • The CB Plugin Framework API is, at least in this part, outdated since it does not mention Ajax at all.

What would solve the problem:
  • Providing a real "no_html" API link through the Joomla index2.php that can access a user plugin directly. A simple new (parameterizable) task handled in the core file would do as I think.

I've managed to get what I've described in the beginning to work by piping the ajax requests through a component written by myself. But since that will only work for me it is of no use for the community. I am willing to release this as a plugin in this forum if I get any useable feedback on this here.


I think build-in Ajax support would be a nice extension of the whole CB API (if I did not simply miss it).


Regards,
Christopher L. ;)

Please Log in to join the conversation.

16 years 3 months ago #55163 by Narya
Replied by Narya on topic Re:Using Ajax in plugin
Maybe noone here is interested in extending CB API to support ajax, but I think it is as easy as adding the following code to the comprofiler.php

In the switch case:
[code:1]case "ajax":
runAjax();
break;[/code:1]

and in the body:
[code:1]function runAjax() {
global $_PLUGINS;
$_PLUGINS->loadPluginGroup('user');
$tab = mosGetParam($_REQUEST, "tab", ""«»);
$class = mosGetParam($_REQUEST, "class", ""«»);
if (strlen($tab) < 1 || strlen($class) < 1) return;
$args = array();
foreach ($_PLUGINS->_plugins as $plugin) {
if ($plugin->element == $tab) {
$results = $_PLUGINS->call( $plugin->id, "processAjaxRequest", $class, &$args );
return;
}
}
}[/code:1]

Then, you could have a "processAjaxRequest" function in your plugin and call it with the URL:
[code:1]index2.php?option=com_comprofiler&task=ajax&no_html=1&format=raw&tab=YourPluginTab&class=getYourPluginTab&whatever-additional-parameter-you-like=1[/code:1]


No good idea?

Post edited by: Narya, at: 2008/01/26 02:48

Please Log in to join the conversation.

16 years 3 months ago #55180 by beat
Replied by beat on topic Re:Using Ajax in plugin
Indeed good idea, and best news is that it's already implemented experimentally (that's why it's not yet in documentation) in current CB 1.1. :)

As a matter of fact, a few 3pds already use this for ajax and for component-like behavior in front-end for plugins (means on a whole page, without being in a tab of user-profile).

The method is called "getTabComponent" and works by invoking:

[code:1]index.php?option=com_comprofiler&task=tabclass&tab=getyourtabclassnametab&user=63
[/code:1]

or without modules for a pop-up typically:

[code:1]index2.php?option=com_comprofiler&task=tabclass&tab=getyourtabclassnametab&user=63
[/code:1]

or for very raw outupt without html around, for images or sound, or pdf, or plain text:

[code:1]index.php?option=com_comprofiler&task=tabclass&tab=getyourtabclassnametab&user=63&no_html=1&format=raw
[/code:1]

(no_html=1 is for mambo and joomla 1.x and format=raw for 1.5).

You need to be aware that for the raw output it's very sensitive to all included php files being silent. Any error or a white-space or blank line before <?php or after ?> in a language file, a mambot, or an index.php file will be outputed (asside of killing some joomla/mambo/cb functionality since it would also not allow to output headers properly).

This is the method to add to your tab class:

[code:1] /**
* WARNING: THIS METHOD IS EXPERIMENTAL !
* WARNING: UNCHECKED ACCESS! On purpose unchecked access for M2M operations
* Generates the HTML to display for a specific component-like page for the tab. WARNING: unchecked access !
*
* @param moscomprofilerTab $tab reflecting the tab database entry
* @param mosUser $user the user being displayed
* @param int $ui 1 for front-end, 2 for back-end
* @param array $postdata _POST data for saving edited tab content as generated with getEditTab
* @return string either string HTML for tab content, or false if ErrorMSG generated
*/
function getTabComponent( &$tab, &$user, $ui, &$postdata ) {
}
[/code:1]

Please be aware that you need to implement your own access-control for this (e.g. verifying that user is logged-in if it needs to be so), as this is wide-open by default (e.g. for server-to-server communications).

Beat - Community Builder Team Member

Before posting on forums: Read FAQ thoroughly -- Help us spend more time coding by helping others in this forum, many thanks :)
CB links: Our membership - CBSubs - Templates - Hosting - Forge - Send me a Private Message (PM) only for private/confidential info

Please Log in to join the conversation.

16 years 3 months ago #55198 by Narya
Replied by Narya on topic Re:Using Ajax in plugin
beat wrote:

for very raw outupt without html around, for images or sound, or pdf, or plain text:

[code:1]index.php?option=com_comprofiler&task=tabclass&tab=getyourtabclassnametab&user=63&no_html=1&format=raw
[/code:1]

(no_html=1 is for mambo and joomla 1.x and format=raw for 1.5).


Thanks for this extensive answer.
I assume you mean index2.php in the above example...

Your answer helped me to get the plugin I've been describing to work and I've packed it in a plugin installer file and posted it in this thread here.

I hope it will be of help.

Regards,
Christopher

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.170 seconds

Facebook Twitter LinkedIn