Datamanager 2 images widget

As with all subclasses, the actual initialization is done in the initialize() function, not in the constructor, to allow for error handling.

This widget supports the images type or any subtype thereof.

All processing is done during the on_submit handlers, enforcing immediate update of the associated storage objects. No other solution is possible, since we need to transfer uploaded files somehow through multiple requests.

The type will show a tabular view of all uploaded images. Existing images have an editable title and can be deleted or replaced. A single new upload line is displayed always. There is no preview, but there is a download link.

Available configuration options:

  • set_name_and_title_on_upload use this if you want the user to be able to set the filename and title when uploading a form.
  • integer max_count Maximum number of images allowed for a field. Set this

Implementation notes:

The construction of the widget is relatively complex, it relies on a combination of static and input elements to do its work. It should be fairly customizable using CSS.

  1. Quickform Element Naming

All elements will be added in a group using the groupname[elementname] Feature of QF. Static elements are all prefixed s_, f.x. s_header. The actual elements use an e_, f.x. e_new_title. All elements in the new upload row append a new_ to this prefix as seen in the last example. Finally, elements referencing existing attachments append an exist_{$identifier}_ to the prefix, f.x. e_exist_{$identifier}_title.

  1. CSS names

The table gets the Name of the field as id and midcom_helper_datamanager2_widget_images as class. Each column also gets its own CSS class: filename, title, file, upload and delete. An additional class is assigned depending whether this is a row for an existing item (exist) or a new one (new). So a full class for the new filename element would be "new filename". Note, that the classes are assigned to both the td and input elements. The th elements do not have any additional class

  1. Image identifiers

The auto-generated image identifiers from the images base type are used.

package midcom.helper.datamanager2

 Methods

_get_filename_validation_script ()

_on_initialize ()

The initialization event handler post-processes the maxlength setting.

Returns

booleanIndicating Success

add_elements_to_form ($attributes)

Constructs the upload list.

Parameters

$attributes

freeze ()

Freeze the entire group, special handling applies to skipp all elements which cannot be frozen.

get_default ()

Populates the title fields with their defaults.

on_submit ($results)

The on_submit event handles all operations immediately.

This includes title updates (all are done regardless of actual updates).

Parameters

$results

sync_type_with_widget ($results)

Prepare for sorting the results

Parameters

$results

unfreeze ()

Unfreeze the entire group, special handling applies, the formgroup is replaced by a the full input widget set.

_add_controls ($info, $identifier)

Parameters

$info

$identifier

_add_image_row (string $identifier, boolean $frozen)

Adds a row for an existing image

Parameters

$identifier

stringThe identifier of the image to add.

$frozen

booleanSet this to true, if you want to skip all elements which cannot be frozen.

_add_new_upload_row (boolean $frozen)

Adds the new upload row to the bottom of the table.

Parameters

$frozen

booleanSet this to true, if you want to skip all elements which cannot be frozen.

_add_new_upload_row_old (boolean $frozen)

Adds the new upload row to the bottom of the table.

Parameters

$frozen

booleanSet this to true, if you want to skip all elements which cannot be frozen.

_add_table_header (boolean $frozen)

Adds the table header to the widget.

Parameters

$frozen

booleanSet this to true, if you want to skip all elements which cannot be frozen.

_check_for_update (string $identifier, array $values)

The following checks are made, in order:
  1. If the delete button was clicked, the image is dropped.
  2. If a new file has been uploaded, it replaces the current one.

Calls for images which are not listed in the form, will be silently ignored. This may happen, for example, if two users edit the same object simultaneoulsy, or during addition of new elements.

Parameters

$identifier

stringThe attachment identifier to check for updates.

$values

arrayThe values associated with our element group (not the full submit value list).

_check_new_upload (array $values)

Checks whether a new file has been uploaded.

If yes, it is processed.

Parameters

$values

arrayThe values associated with our element group (not the full submit value list).

_compute_elements (boolean $frozen)

Computes the element list to form the widget.

It populates the _elements member, which is initialized with a new, empty array during startup.

Parameters

$frozen

booleanSet this to true, if you want to skip all elements which cannot be frozen.

_get_preview_html ($info, $identifier)

Parameters

$info

$identifier

 Properties

 

integer $max_count

Maximum amount of images allowed to be stored in the same field
 

boolean $set_name_and_title_on_upload

Should the user be able to set the filename and title on upload? If so, set this to true.
 

Array $_elements

The list of elements added to the widget, indexed by their element name.
 

\HTML_QuickForm_Group $_group

The group of elements forming our widget.
 

integer $_sort_index

Sort index or what is the position in the list