DEPRECATION WARNING

This documentation is not using the current rendering mechanism and is probably outdated. The extension maintainer should switch to the new system. Details on how to use the rendering mechanism can be found here.

EXT: extbase_ajax

Author:Christopher
Created:2010-12-19T01:57:23
Changed by:Christoph Balogh
Changed:2012-02-27T22:09:23.830000000
Classification:extbase_ajax
Description:The keywords help with categorizing and tagging of the manuals. You can combine two or more keywords and add additional keywords yourself. Please use at least one keyword from both lists. If your manual is NOT in english, see next tab "language" ---- forEditors (use this for editors / german "Redakteure") forAdmins (use this for Administrators) forDevelopers (use this for Developers) forBeginners (manuals covering TYPO3 basics) forIntermediates (manuals going into more depth) forAdvanced (covering the most advanced TYPO3 topics) see more: http://wiki.typo3.org/doc_template#tags ----
Keywords:extbase, ajax, modal, dialog
Author:Christoph Balogh
Email:cb@lustige-informatik.at
Language:en

img-1 img-2 EXT: extbase_ajax

Extension Key: extbase_ajax

Language: en

Version: 0.0.1

Keywords: extbase, ajax, modal, dialog

Copyright 2012, Christoph Balogh, <cb@lustige-informatik.at>

This document is published under the Open Content License

available from http://www.opencontent.org/opl.shtml

The content of this document is related to TYPO3

- a GNU/GPL CMS/Framework available from www.typo3.org

Table of Contents

`EXT: Name of your Extension 1 <#__RefHeading__5708_1738894311>`_

`Introduction 1 <#__RefHeading__5710_1738894311>`_

What does it do? 1

Screenshots 1

`Users manual 1 <#__RefHeading__467_413120346>`_

How does it work? 1

Example 1

FAQ 1

`Administration 1 <#__RefHeading__31511_818911409>`_

`Configuration 1 <#__RefHeading__31515_818911409>`_

Description 1

Method logUpdate 1

Method logAdd 1

Method logDelete 1

Deep Insight 1

`Known problems 1 <#__RefHeading__31525_818911409>`_

`To-Do list 1 <#__RefHeading__477_413120346>`_

`ChangeLog 1 <#__RefHeading__31623_818911409>`_

Introduction

What does it do?

This extension provides an easy and unified way to call Extbase controller actions via AJAX and render the Fluid forms in modal dialog boxes. Simple procedures to invoke modal dialog boxes, modal message boxes (replacing javascript alert's) and automatic redirects are included as well.

This is the first release of the extension uploaded to TER. We are using it in various extbase based extensions alongside with fed. You will need to have jQuery loaded. The extension checks by itself, if the required Version and plugins are available.

Example

When editing a domain object, this will be done usually in a fluid form. Most users require that this editing process will take place in a “modal” window, that can be handled like an ordinary application window like the one below:

img-3 Of course this can be realized by using the dialog() plugin of jQuery, but putting it alltogether will be an annoying task,especially, when you want to easily update elements already present on screen.

The extension gives you the ability to convert any element in your fluid template to a jQuery button, which will automatically call either a modal dialog, a modal message box, a modal confirmation box or a redirect to another page which can be opened in a new window as well.

In order to keep the things as fast as possible, all these functions are delegated to jQuery directly. There are no viewhelpers at this stage of development, but everything can be controlled by attributes added to the respective element.

Users manual

How does it work?

Example

Let us assume you are using a list view of extbase objects of your extension in a form like this:

img-4 Please note the two icons on the right side. When clicking the edit button, the object can be edited in a modal window (blocking the user from doing anything else on the page). When saving the object, the data will update the information in the list automatically.

When deleting the item the user will be prompted with a confirmation box. After confirming the deletion, the entire row will be faded out (and deleted in the table of course).

To obtain this, the following code is needed in your fluid template:

  <img
             class="modalDialog"
             src="EXT:.../edit.png"
alt=""
title="Your title"
extbaseajax=
     action: 'ajaxEdit',
     controller: 'Appointment',
     appointment: {appointment.uid}
savetext=”Save”
closetext=”Cancel”
     />

The tag for the delete icon looks similar but calls a confirmation box:

  <img
             class="modalConfirm"
src="EXT:.../delete.png"
alt=""
title="Your title"
extbaseajax=
     action: 'ajaxDelete',
     controller: 'Appointment',
     appointment: {appointment.uid}
text=”Delete appointment?
confirm=”Yes”
cancel=”Cancel”
onconfirm=$('#appointmentRow{appointment.uid}').fadeOut()
     />

When installing the extension additional javascript will be loaded that searches for the class names “modalDialog”, “modalConfirm”, “modalMessage” and “autoRedirect”. These classes will be used to autogenerate buttons from the respective tag, which is usally either a <span> tag or an <img> tag. Of course this can be used with any fluid (or other) viewhelper, the only thing is that a little bit more encapsulation and escaping is needed.

The AJAX request will be controlled by the extbaseajax attribute and some additional settings. The extbaseajax attribute is responsible fro the creation of the URI to be called.

The tag will be converted to a jQuery button() which will fire an AJAX request to a page type that can be configured either in your TS or via constant editor (at the moment only for one extension in the latter case).

The request will be handled by a special controller that must be present in your ext_localconf.php file. Usually the name of this controller will be “ajax” but this is not necessarily the case.

The form you defined in your template/partial combination will be rendered in a modal window automatically allowing you to edit the whole object. The save button again will be automatically bound to the form action, but extended by the page type thus again launching an AJAX request to the backend.

The reception of the data will be handled automatically as well. The only prerequisites are, that you have to return a json string from your controller.

The main benefit is, that you may copy the above piece of code to wherever you want in your fluid templates. As long as the object reference is known the editing will take place in the same way calling the same controller/action combination any time.

This is extremely useful when you have to edit the same objects (like users) from different views.

Required libraries

The extension needs jQuery v 1.7.1 with the plugins dialog,button and fadOut installed. You can either download this manually, generate it via fed:jQuery or have installed via the extension t3jquery (which is actually what we do). The only thing you have to take care of is the order in the resulting HTML document. Jquery must be on top of course.

jQuery

All of the elements are treated autmatically by the extension and all jQuery calls are made automatically except for the callbacks. We are thinking of additional wrapper funtions, but actually this will grow the size of the code and make things more complicated only. If you are slightly familiar with jQuery you will be able to control the behavior of your workflow.

Quick Start

Minimum requirements

For a quick start download and install t3jquery from the TER. Choose version Select the jQuery core, the dialog and the button widget as well as the fade effect. All other settings are optional (at least for extbase_ajax).

Next download and install extbase_jquery. Don't forget to include the static template. Otherwise you will not be able to use the constant editor.

In the constant editor you may leave all settings at the default values. If you are using extbase_ajax with only one extension you should set the following parameters:

pageType = 112233
extensionName = yourextensionname
pluginname = ajax

Please change yourextensionname to the name of your extension listed in the ext_amconf.php file. The name is without the tx_ and just lowercase. Keep this in mind. When the plugin does not work correctly you will have specified the wrong extension name here.

Set the page type to your needs. The default value is 112233.

Configuring your extension

Edit the ext_localconf.php file of your extension in the following way: Add a code snippet (preferably at the end of the controller/action definitions generated by the extension builder) of the following form:

Tx_Extbase_Utility_Extension::configurePlugin(
        $_EXTKEY,
        'Ajax',
        array(
                'Controller' =>  'ajaxEdit, ajaxUpdate, ajaxNew, ajaxCreate, … ',
                'Othercontroller' => 'ajaxEdit, ajaxUpdate, ajaxNew, ajaxCreate, … ',
        ),
        array(
                'Controller' =>  'ajaxEdit, ajaxUpdate, ajaxNew, ajaxCreate, … ',
                'Othercontroller' => 'ajaxEdit, ajaxUpdate, ajaxNew, ajaxCreate, … ',
        )
);

Please do not forget to clear the caches after changing ext_localconf.php.

The pluginname 'Ajax' must match the pluginname in the constants setting. Please note that the pluginname has to be in upper camel case in the ext_localconf.php but in lowercase in the the constants section.

The important thing is that you add the actions in the respective controller of the object. The typoscript will automatically take care of the page type and send your AJAX request to the correct controller.

I recommend to call the controllers that will be called by AJAX requests in the form ajaxEdit or ajaxUpdate, but this is not a must of course. Actually it is possible to use the same controllers for AJAX and non-AJAX calls at the same time, because all the header code will be disabled automatically. But in most cases you will refrain from non-AJAX calls anyway.

Configuring the Templates

Ordinary actions will return a rendered view in most cases. Using the extbase_ajax extension divides the actions mainly in two groups:

  • actions that render a form (or any other fluid view, usually ajaxNew or ajaxEdit )
  • actions that return the result of the from processing (usually ajaxCreate or ajaxUpdate )
Action Rendering a Form

This is the easycase.Just design your fluid template as usual. In most cases you will be able to take your original template. As mentioned before when omitting the 'ajax' prefix, the rendering process will look for the standard template files like New.html or Edit.html.

Processing Form Data

The actions that process the form data work slightly different than in “ordinary”, page based actions. Normally you would probably do a redirect in the updateAction or createAction. This redirect won't work in an AJAX based workflow.

Therefore the script expects a JSON encoded string holding the result values. The simplest form (e.g.when deleting a record) will be a JSON encoded array holding the value ['success' = 'ok'], which will be generated by the following code in your action:

return json_encode(array('success' => 'ok'));

Additional data may be returned in the same JSON-Array.this is extremely useful when you want to update table data on the main page. This will be performed in the following way:

$result = array(
        'success' => 'ok',
        'lastName345' => 'Last name',
        'firstname345' => 'First name',
);
return json_encode($result);

In this case the properties lastName and firstName will be attached to the array as well and the callback function will spread the values automaticallyin your “main” page.

Functions

Auto Redirect

The auto redirect autogenerates a link the will either redirect to a page or open a new window. This is very useful when displaying previews for pages to be printed. In this case the page will be rendered with its own page header settings in a fresh window or tab.

The content is fetched in the same way via AJAX call.

newwindow

a

newwindow

b

If set (to any value) a new window will be opened and the succeeding parameters will be considered.

wwidth

a

wwidth

b

Width of the new window

wheight

a

wheight

b

Height of the new window

wscrollbars

a

wscrollbars

b

Turn scrollbars on or off

title

a

title

b

Title of the new window

Using <f:image> elements

When you are using the fluid image element you have to consider the way that attributes are passed to the <f:image> element. When you add attributes to a HTML tag (most probably a <span> tag) you will have a construction like this:

<span
        class=”modalDialog”
        extbaseajax=”controller: 'yourcontroller', action: 'yourAction'
        onshow=”alert('Hello World')>
Button Text
</span>

When using a fluid viewhelper you will have to use the additionalAttributes attribute. This means that you have to convert the attributes to a javascript array and escape all occurences of the single quotation marks leading to the following syntax:

<f:image
        src=...
        alt=...
        class=”modalDialog”
        additionalAttributes={
                extbaseajax: '
                        controller: \'yourcontroller\',
                        action: \'yourAction\' ',
                onshow: 'alert(\'Hello World\')
        }/>

Please note how the attributes are converted in an array within additionalAttributes .

Walkthrough

To make installation easier, I provide a step-by-step walkthrough. I have this on a brand new website,but there might be errors in it.If something does not work immediately go back and check the steps again. For example: It is very important to have the static template installed, otherwise the necessary settings won't be considered.

Prepare your own extension

Since extbase_ajax does make sense only if you are programming your own extbase based extension, you should hav one right at hand to test it. The final step will lead you to certain changes in your own templates in order to test it.

Get a copy of jQuery or install t3jquery

You must have a working copy of jQuery. jQuery is not part of the extension. If you want to use t3jquery (as we do) you can generate the jQuery library directly from the backend.

Install extbase_ajax

Install the extension extbase_ajax.

After installing extbase_ajax be sure to have the static template added to your root page template. The static template is called AJAX for Extbase (extbase_ajax) . Please have a look at the setup.txt file for more information of the values inside the configuration. This will be important when changing the jQuery theme, for instance.

Clear the cache and go to the constant editor.The most important sections to change are:

Page Type

The page type defines the number of the page type that will be used to call your extension via AJAX. This is set to 112233 by default.

Your extension name

This is the most important setting: Please enter the name of your extension, that shall be made accessible via extbase_ajax. The name is the part between tx_ and _pi1. If there are underscores in your extension key, these must be usually left out, since we use this value to calculate the full pluginname.

Your pluginname

This set to ajax by default. In fact the pluginname will be used to create the complete controller/action reference using the following pattern: tx_<extensionname>_<pluginname>. This pluginname must match the controller/action settings in your ext_localconf.php.

Edit the ext_localconf.php of your extension

Locate the call to the method configurePlugin in this file and make a similar entry after the settings that are already there (possibly auto-inserted by extension_builder)in the form:

Tx_Extbase_Utility_Extension__configurePlugin(
        $_EXTKEY,
        '<Pluginname>',
        array(
                'Controller' => 'action1, action2, …',
                <more Controllers here>,
        ),
        array(
                'Controller' => 'action1, action2, …',
                <more Controllers here>,
        )
);

The <Pluginname> is the name of the plugin as entered in the constant editor. It must be Uppercase and is 'Ajax' when using the default settings.

I recommend to use special controller/action name pairs but you may use the existing controller/action pairs as well. If you have an extension with a main aggregate object order and standard actions (list, show, …) you can address these controller with the following setting:

Tx_Extbase_Utility_Extension__configurePlugin(
        $_EXTKEY,
        'Ajax',
        array(
                'Order' => 'edit, new, create, delete',
        ),
        array(
                'Order' => 'edit, new, create, delete',

        )
);

Please note, that you will have to list the actions also in the non- caching part of the configuration array. Please note as well, that the original name of your extension will not appear in this setting.

When you want to hard-wire the settings for the controller, pagetype and pluginname this can be transferred to the setup.txt of your extension as well. In this case you should take care for the order in which the extensions are loaded.

Adjust the HTML-Template

As a final step (before testing) you should change one of your templates. If you have a List.html template somewhere in your Resources section try to insert an edit button by changing the normal <f:link.action>...</f:link> to the following code:

<span
        class=”modalDialog”
        extbaseajax=
                controller: 'Order',
                action: 'edit',
                order: {order.uid}
        >Edit</span>

You should see an jQuery style edit button in place of the former link. When clicking the button a modal dialog window with your form data should appear.

Behind the Scenes

((generated))

jQuery

When installing the extension an additional call will be added to your document causing the binding of the classes added to your elements to the respective functions.

If you are using the classes with <img> tags the behavior is slightliy different: Since it is not possible to add child objects to an <img> tag, it will be wrapped by a <span> tag prior to executing the jQuery.button() plugin.

To be honest, this was developed because we did not realize that the jQuery.button() plugin is intended for text buttons only. In order to avoid IE from crashing, the additional wrap is necessary. All of the attributes will be moved from the <img> tag to the wrapping <span> tag.

AJAX call

The URI to be called will be calculated from the parameters passed to the javascript functions. The page type is taken from the settings in the constant editor. The AJAX call is performed by the jQuery ajax function. The callbacks will be handled automatically.

Return values

The return values will be treated in two different ways: If the return value can be interpreted as JSON encoded array, the array will be parsed and the processing of the return values takes place. You will have to return a value 'ok' for key 'success' at least.

If the return value is not a valid JSON encoded array, it will be sent directly to the currently open window. In case the form does not validate (and should be redisplayed), this will be automatically covered by the ajaxEdit or ajaxNew action in your controller. Therefore you don't have to take additional precautions iin these cases.

During the stage of development you will often run into the situation that a view is not defined or the controller does not work properly thus rendering an error. In this case the error itself will be displayed.

FAQ

Q: In which cases will this extension be useful for me?

A: If you are writing your own extension based on extbase/fluid and you need the ability to manipulate objects in “modal” dialog windows based on jQuery.

Q: In which cases will this extension not provide any help?

A: If you are not programming your own extension or if you are using the classical way, you won't be able to use this.

Q: Can I use this extension with other extensions based on extbase/fluid?

A: Of course. As long as the extension is configured to serve the special page type you need in your extension there is not any interference to be expected.

Q: Why is the Ferenderingnot supported by viewhelpers?

A: The viewhelpers won't do anything else than parsing the configuration attributes thus renering the correct values. This will cause additional processing to reach the same goals. That's why I did not wrap the settings once more in view helpers. The result will be processed by jQuery anyway.

Administration

There is not much to administer at the moment. After installing the extension and configuring the required values anything else will be done automatically.

If you feel that there should be anything to adjust additionally, please contact me.

Configuration

((generated))

Configuring the Extension

After having set the required values in the constant editor,the configuration is done.

Configure extbase_ajax to serve multiple extensions

If you want to use extbase_ajax in more than one extension of yours, it is a good idea add the settings to the setup.txt of your extension.

If you want to fire the AJAX call to a different pagetype you canspecify this via the attributes pluginname and type as additional attributes in the HTML tag.

Known problems

I know that the documentation is a little bit chaotic. I'll try to improve in the next weeks. If you have any issues regarding the usage of the extension don't hesitate to email me.

To-Do list

Improving the documentation

Making the JS even more generic.

Your suggestions are welcome.

ChangeLog

0.0.1

Version

0.0.1

Changes

Initial release uploaded to TER

0.0.0

Version

0.0.0

Changes

Development