Skip to content
Omeka plugin to add a way to create new records via ajax
PHP JavaScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
controllers
helpers
views/admin
AjaxCreate.php
README
README~
plugin.ini
plugin.php

README

AjaxCreate provides a mechanism to quickly create new Omeka Records with ajax while in the middle of another workflow.
It includes an icon to create a new Collection from the Item edit page, but can be configured to work with other Record types.

Use Case:
The configuration of a plugin or data about a record depends on the existance of a record of a particular type. For example, when creating a new Item, there is a select list for the Collection it belongs to; or the settings for an importer let you choose a Collection or an Item Type etc.

If you don't plan things out carefully in advance, the workflow can easily become circular -- users might have to abandon where they are in their task, create the new Collection or Item Type, then return to the task at hand.

AjaxCreate allows you to bring up a modal dialoge that asks simply for the name and description of a new record, creates it, and adds the option to the select list. 

N.B. Users will still want to complete the data for whatever record type they create (e.g., add collectors to a Collection, make it public, etc.).

Usage:
The following helper function returns a link to open the dialog:

function ajax_create_dialog(array $options, $callback = "AC.prependToSelect" , $label = false)
$options is an array in this form:
array('type'=> '', // the record type. Required.
	  'skipDescription' => false, // boolean whether to include the Description field in the dialog (Tags don't have descriptions)
	  'target' => '',  // usually, a jQuery selector for the select element to prepend a new option to
	  'label' => '', // optional label for the link to open the dialog. Defaults to "Create a new (type)".
)

$callback is a reference to a custom javascript function you provide.
If your UI works differently, you can provide your own javascript callback in $callback. The 'target' option can then be used in whatever way you see fit.


The data returned from the server is a JSON blob in this form:

{ id: '', // the new record id
  type: '', // the new record type
  name: '', 
  description: '',
  target: '', // the jQuery selector (or other target info if using a custom callback) to use
  callback: '', // the callback to use, if not stuffing results into a select
  status: '', // "OK" or "Error"
  message: '', // the error message
}

status and message are handled in a method before passing the data on to your callback, but you can always change the default behavior by overriding
AC.processResponse in your own javascript.


Examples:
Basic example adapted from adding the "Add Collection" link to Item edit pages.
<?php
	$options = array(
		'type'=> 'Collection',
		'target' => '#collection-id'	
	);
	$dialog_content = ajax_create_dialog($options);
	echo $dialog_content;	
?>

Using a custom callback

<script type="text/javascript">
function myCallback(data) {	
	// lookup the existing element
	var newCollectionDiv = document.getElementById('my-plugin-new-collection');

	// build the heading
	var newCollectionH = document.createElement('h3');
	newCollectionH.appendChild(document.createTextNode(data.name));

	// build the description
	newCollectionDesc = document.createElement('p');
	newCollectionDesc.appendChild(document.createTextNode('data.description'));

	//append heading and description to the div
	newCollectionDiv.appendChild(newCollectionH);
	newCollectionDiv.appendChild(newCollectionDesc);
}
</script>

<?php echo ajax_create_dialog(array('type'=>'Collection', 'target'=>'my-plugin-new-collection'), "myCallback"); ?>

Something went wrong with that request. Please try again.