self::$_session = CRM_Core_Session::singleton();
}
- if (isset($_REQUEST['snippet']) && $_REQUEST['snippet']) {
+ // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
+ if (!empty($_REQUEST['snippet'])) {
if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) {
$this->_print = CRM_Core_Smarty::PRINT_PDF;
}
// FIXME - why does this number not match the constant?
- else if ($_REQUEST['snippet'] == 5) {
+ elseif ($_REQUEST['snippet'] == 5) {
$this->_print = CRM_Core_Smarty::PRINT_NOFORM;
}
+ elseif ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_JSON) {
+ $this->_print = CRM_Core_Smarty::PRINT_JSON;
+ }
else {
$this->_print = CRM_Core_Smarty::PRINT_SNIPPET;
}
if ($this->_print) {
if (in_array( $this->_print, array( CRM_Core_Smarty::PRINT_SNIPPET,
- CRM_Core_Smarty::PRINT_PDF, CRM_Core_Smarty::PRINT_NOFORM ))) {
+ CRM_Core_Smarty::PRINT_PDF, CRM_Core_Smarty::PRINT_NOFORM, CRM_Core_Smarty::PRINT_JSON ))) {
$content = self::$_template->fetch('CRM/common/snippet.tpl');
}
else {
array('paper_size' => 'a3', 'orientation' => 'landscape')
);
}
+ elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) {
+ CRM_Core_Page_AJAX::returnJsonResponse($content);
+ }
else {
echo $content;
}
});
}
+ var dialogCount = 0;
CRM.loadPage = function(url, options) {
options = options || {};
- url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=6';
var settings = {
- target: '#crm-ajax-dialog',
+ target: '#crm-ajax-dialog-' + (dialogCount++),
dialog: {
modal: true,
width: '65%',
$(this).remove();
}
},
- onLoad: null,
- type: 'Page',
- url: url
+ autoClose: true,
+ onFailure: function(data, settings) {
+ if (settings.dialog !== false && settings.autoClose) {
+ $(this).dialog('close');
+ }
+ CRM.alert(ts('Unable to reach the server. Please refresh this page in your browser and try again.'), ts('Network Error'), 'error');
+ },
+ type: 'Page'
};
$.extend(true, settings, options);
+ // Add snippet argument to url
+ if (url.search(/[&?]snippet=/) < 0) {
+ url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=6';
+ }
+ settings.url = url;
// Create new dialog
- if (settings.dialog && settings.target[0] == '#') {
+ if (settings.dialog !== false && settings.target[0] == '#') {
$('<div id="'+ settings.target.substring(1) +'" class="crm-container"><div class="crm-loading-element">' + ts('Loading') + '...</div></div>').dialog(settings.dialog);
}
$.getJSON(url, function(data) {
+ if (typeof(data) != 'object' || typeof(data.content) != 'string') {
+ settings.onFailure.call($(settings.target), data, settings);
+ return;
+ }
if (settings.dialog && !settings.dialog.title && data.title) {
$(settings.target).dialog('option', 'title', data.title);
}
settings.content = data.content;
$(settings.target).html(data.content).trigger('crm' + settings.type + 'Load', data);
if (typeof(settings.onLoad) == 'function') {
- settings.onLoad(data, settings);
+ settings.onLoad.call($(settings.target),data, settings);
}
});
+ return $(settings.target);
};
CRM.loadForm = function(url, options) {
options = options || {};
var settings = {
- target: '#crm-ajax-dialog',
validate: true,
- onLoad: null,
- onCancel: function(e) {
- if (settings.dialog !== false) {
- $(settings.target).dialog('close');
- }
+ onCancel: function(event, settings) {
return false;
},
onError: function(data, settings) {
- $(settings.target).html(data.content).trigger('crmFormReload', data);
+ $(this).html(data.content).trigger('crmFormReload', data);
if (typeof(data.errors) == 'object') {
- $.each(data.errors, function(target, msg) {
- $('[name="'+target+'"]').crmError(msg);
+ $.each(data.errors, function(formElement, msg) {
+ $('[name="'+formElement+'"]').crmError(msg);
});
}
- settings.onLoad(data, settings);
- },
- onSuccess: function(data, settings) {
- if (settings.dialog !== false) {
- $(settings.target).dialog('close');
- }
+ settings.onLoad.call($(settings.target), data, settings);
}
};
$.extend(settings, options, {type: 'Form'});
settings.onLoad = function(data, settings) {
- $(".cancel.form-submit", settings.target).click(settings.onCancel);
+ $(".cancel.form-submit", this).click(function(event) {
+ var returnVal = settings.onCancel.call($(settings.target), event, settings);
+ if (settings.dialog !== false && settings.autoClose && returnVal !== true) {
+ $(settings.target).dialog('close');
+ }
+ return returnVal === true ? returnVal : false;
+ });
if (settings.validate) {
- $("form", settings.target).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params);
+ $("form", this).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params);
}
- $("form", settings.target).ajaxForm({
+ $("form", this).ajaxForm({
url: settings.url,
dataType: 'json',
success: function(response) {
if (response.status == 'success') {
- settings.onSuccess(response, settings);
+ if (typeof(settings.onSuccess) === 'function') {
+ settings.onSuccess.call($(settings.target), response, settings);
+ }
$(settings.target).unblock().trigger('crmFormSuccess', data);
+ // Reset form for e.g. "save and new"
if (settings.resetButton && response.buttonName == settings.resetButton) {
$(settings.target).html(settings.content).trigger('crmFormLoad', data);
- settings.onLoad(data, settings);
+ settings.onLoad.call($(settings.target), data, settings);
+ }
+ else if (settings.dialog !== false && settings.autoClose) {
+ $(settings.target).dialog('close');
}
}
else {
- settings.onError(response, settings);
+ settings.onError.call($(settings.target), response, settings);
}
},
beforeSubmit: function() {
});
// Call original onLoad fn
if (typeof(options.onLoad) == 'function') {
- options.onLoad(data, settings);
+ options.onLoad.call($(settings.target), data, settings);
}
};
- CRM.loadPage(url, settings);
+ return CRM.loadPage(url, settings);
};
// Preprocess all cj ajax calls to display messages