Blogs / Brandon Chambers / AJAX (jQuery) + eZ Publish: Demystified!

AJAX (jQuery) + eZ Publish: Demystified!

Monday 06 June 2011 2:00:15 pm

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

By : Brandon Chambers

This is a short tutorial about using the powerful combination of AJAX (jQuery) and eZ Publish. Please note this was also originally released before the REST API was developed for the current community release of eZ Publish.

Original post:  http://blog.divdesigns.com/2011/02/ajax-jquery-ez-publish-demystified/

 

eZ Publish is by nature, easy to understand in the administration panel, adding and editing content. However, once you look under the hood it's like a complex maze of code, intricately designed, but still a maze. While the template code, based on Smarty I believe, is well documented there's no single source to figure out how things work in PHP land.  You just find out for yourself by looking through other extensions as well as the kernel code, ask other people or by just plain "elbow grease." Well here's some findings I came across fumbling around on a project as well as spare time.

Tutorial Skill requirements:

  • JS
  • eZ Publish
  • PHP
  • Extension/Module/View installation & configuration

If you need to head over to eZ Systems' site for more information on installing and configuring an extension

Discovery

I came across two main ways to implement AJAX (jQuery) with eZPublish.

  • AJAX + a Custom Module
  • AJAX + eZJSCore + a Custom Class (implementing a special type of ezjscore class)

Which offers the most flexibility?

  • Both allow access to the eZ Publish class interface
  • Module approach allows flexibility of having a view associated with the AJAX functionality thus allowing up to three avenues of functionality for the script to run as. (AJAX Post + Regular Post + View)
  • Module approach allows access to the Module parameters array. (This allows one to use AJAX calls in an unconventional way using a post to /mymodule/param1/param2/param3 which isn't recommended)
  • eZJSCore approach has cleaner, more concise code.
  • eZJSCore approach allows for multiple functions in the class (don't forget to register your other functions in the INI files!) as opposed to a one-time run-through with the module approach. Although you can add other functions to a module, eZ Publish requires you to  add a separate PHP file.
  • Anyone find something else I'm missing?

Show me the Code!

AJAX + Custom Module:

So obviously you need a module and a view (and thus an extension). By looking at the JavaScript you'll see the module and view names I used. You can use whatever you want.

The JavaScript:

var data_sent = { message: "testing!"};$.ajax({    url: "/mymodule/post",    type: 'POST',    data: ( data_sent),    dataType: 'json',    success: function( data ){        if( data.response == "ok" )        {            alert( data.message );        }    }});

The Module code:

In post.php I usually have something along the lines of this initial setup:

if ( $http->hasPostVariable( 'message'' ) && $http->postVariable( 'message' ) ){    $response = array( 'response' => 'ok');    print( json_encode( $response ) );    eZExecution::cleanExit();}

That's IT!

AJAX + eZJSCore + Custom Class:

This approach requires you have eZJSCore installed and working.

One of the first things you'll notice with this approach by looking at the code is there's a little more of it, but it looks cleaner in the JavaScript code.

To get eZJSCore setup add your custom class to ezjscore.ini (preferably in your extension's settings directory):

[ezjscServer]FunctionList[]=ajaxFunc[ezjscServer_myajax]Class=MyAJAXFile=extension/myextension/classes/myajax.phpFunctions[]=ajaxFunc

The JavaScript:

//You can keep going with the argsvar  dataSent = {arg1: "123", arg2: "456"};$.ez(  'myajax::ajaxfunc', dataSent, function( ezp_data ){    if ( ezp_data.error_text )    {        alert( ezp_data.error_text );    }    else    {        alert( ezp_data.content );    }}

Custom PHP Class setup:

class MyAJAX extends ezjscServerFunctions{    public static function ajaxFunc( $args )    {        if ( isset( $args[0] ) && isset( $args[1] ) && isset( $args[2] ) && isset( $args[3] ) )        {            $var1 = htmlspecialchars( $args[0] );            $var2 = htmlspecialchars( $args[1] );            $var3 = htmlspecialchars( $args[2] );            $var4 = htmlspecialchars( $args[3] );        }        else if ( $http->hasPostVariable( 'arg1' ) && $http->hasPostVariable( 'arg2' ) && $http->hasPostVariable( 'arg3' ) && $http->hasPostVariable( 'arg4' ) )        {            $var1 = htmlspecialchars( $http->postVariable( 'arg1') );            $var2 = htmlspecialchars( $http->postVariable( 'arg2') );            $var3 = htmlspecialchars( $http->postVariable( 'arg3') );            $var4 = htmlspecialchars( $http->postVariable( 'arg4') );        }        else        {            return $error['args'];        }        return "ok";    }}

Use for debugging via HTTP:

http://dev.server.com/ezjscore/call/myajax::ajaxfunc::val1::val2::val3::val4

eZ Find + AJAX = Lightning Fast?

eZ Find definitely has its advantages and it would be a good idea to use eZ Find for large data sets.

Until then, get used to using the template code outlined in Ivo Lukac's guide to using eZ Find. There you can also find the advantages and disadvantages of eZ Find.

eZ debug

Timing: Jan 17 2025 21:05:14
Script start
Timing: Jan 17 2025 21:05:14
Module start 'content'
Timing: Jan 17 2025 21:05:14
Module end 'content'
Timing: Jan 17 2025 21:05:14
Script end

Main resources:

Total runtime0.1108 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0047 596.1719180.8281
Module start 'content' 0.00470.0042 777.0000105.3203
Module end 'content' 0.00900.1017 882.3203534.4531
Script end 0.1107  1,416.7734 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00292.6236200.0001
Check MTime0.00121.0392200.0001
Mysql Total
Database connection0.00060.543310.0006
Mysqli_queries0.070763.83951410.0005
Looping result0.00090.82871390.0000
Template Total0.101591.610.1015
Template load0.00080.713110.0008
Template processing0.100790.913410.1007
Override
Cache load0.00050.475910.0005
Sytem overhead
Fetch class attribute can translate value0.00060.496610.0006
XML
Image XML parsing0.00030.236610.0003
General
dbfile0.00454.0323200.0002
String conversion0.00000.004530.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs