diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index 1c43687e7ea732b1e1ea96485f70c0e026bf40e2..b7b2ee4deb0d4897bbd97f5e16ad240a59b02cda 100644 --- a/include/class.dynamic_forms.php +++ b/include/class.dynamic_forms.php @@ -137,9 +137,10 @@ class DynamicForm extends VerySimpleModel { return $this->get('deletable'); } - function instanciate($sort=1) { - return DynamicFormEntry::create(array( - 'form_id'=>$this->get('id'), 'sort'=>$sort)); + function instanciate($sort=1, $data=null) { + return DynamicFormEntry::create( + array('form_id'=>$this->get('id'), 'sort'=>$sort), + $data); } function data($data) { @@ -731,6 +732,9 @@ class DynamicFormEntry extends VerySimpleModel { } return $form; } + function getMedia() { + return $this->getForm()->getMedia(); + } function getFields() { if (!isset($this->_fields)) { @@ -967,9 +971,8 @@ class DynamicFormEntry extends VerySimpleModel { if (!parent::save($refetch || count($this->dirty))) return false; - foreach ($this->getFields() as $field) { - if (!($a = $field->getAnswer())) - continue; + foreach ($this->getAnswers() as $a) { + $field = $a->getField(); if ($this->object_type == 'U' && in_array($field->get('name'), array('name','email'))) @@ -1002,10 +1005,13 @@ class DynamicFormEntry extends VerySimpleModel { return parent::delete(); } - static function create($ht=false) { + static function create($ht=false, $data=null) { $inst = parent::create($ht); $inst->set('created', new SqlFunction('NOW')); - foreach ($inst->getForm()->getFields() as $f) { + $form = $inst->getForm(); + if ($data) + $form->setSource($data); + foreach ($form->getFields() as $f) { if (!$f->hasData()) continue; $a = DynamicFormEntryAnswer::create( array('field_id'=>$f->get('id'))); @@ -1124,6 +1130,15 @@ class DynamicFormEntryAnswer extends VerySimpleModel { $v = $this->toString(); return is_string($v) ? $v : (string) $this->getValue(); } + + function delete() { + if (!parent::delete()) + return false; + + // Allow the field to cleanup anything else in the database + $this->getField()->db_cleanup(); + return true; + } } class SelectionField extends FormField { diff --git a/include/class.forms.php b/include/class.forms.php index 5ac0738d6f417711bfac222007446d1b4e4c1372..0249de4d8cda24f5d7bdd41e72a8496183f6df29 100644 --- a/include/class.forms.php +++ b/include/class.forms.php @@ -110,6 +110,7 @@ class Form { include(STAFFINC_DIR . 'templates/dynamic-form.tmpl.php'); else include(CLIENTINC_DIR . 'templates/dynamic-form.tmpl.php'); + echo $this->getMedia(); } function getMedia() { @@ -396,6 +397,15 @@ class FormField { return $this->toString($this->value); } + /** + * When data for this field is deleted permanently from some storage + * backend (like a database), other associated data may need to be + * cleaned as well. This hook allows fields to participate when the data + * for a field is cleaned up. + */ + function db_cleanup() { + } + /** * Returns an HTML friendly value for the data in the field. */ @@ -1677,6 +1687,14 @@ class FileUploadField extends FormField { } return implode(', ', $files); } + + function db_cleanup() { + // Delete associated attachments from the database, if any + $this->getFiles(); + if (isset($this->attachments)) { + $this->attachments->deleteAll(); + } + } } class Widget { diff --git a/include/class.organization.php b/include/class.organization.php index 991952bd9807d91658c361985c1c81f940354438..32e2c1ec9dd2eaf05d04ea23fd53fc2cf34d88c1 100644 --- a/include/class.organization.php +++ b/include/class.organization.php @@ -105,15 +105,12 @@ class Organization extends OrganizationModel { var $_forms; function addDynamicData($data) { + $entry = $this->addForm(OrganizationForm::objects()->one(), 1, $data); + // FIXME: For some reason, the second save here is required or the + // custom data is not properly saved + $entry->save(); - $of = OrganizationForm::getInstance($this->id, true); - foreach ($of->getFields() as $f) - if (isset($data[$f->get('name')])) - $of->setAnswer($f->get('name'), $data[$f->get('name')]); - - $of->save(); - - return $of; + return $entry; } function getDynamicData($create=true) { @@ -193,12 +190,12 @@ class Organization extends OrganizationModel { } } - function addForm($form, $sort=1) { - $form = $form->instanciate(); - $form->set('sort', $sort); - $form->set('object_type', 'O'); - $form->set('object_id', $this->getId()); - $form->save(); + function addForm($form, $sort=1, $data) { + $entry = $form->instanciate($sort, $data); + $entry->set('object_type', 'O'); + $entry->set('object_id', $this->getId()); + $entry->save(); + return $entry; } function getFilterData() { @@ -307,6 +304,7 @@ class Organization extends OrganizationModel { $this->name = $name->getClean(); $this->save(); } + $cd->setSource($vars); $cd->save(); } @@ -340,6 +338,15 @@ class Organization extends OrganizationModel { return $this->save(); } + function delete() { + if (!parent::delete()) + return false; + + foreach ($this->getDynamicData(false) as $entry) { + $entry->delete(); + } + } + static function fromVars($vars) { if (!($org = Organization::lookup(array('name' => $vars['name'])))) { @@ -357,14 +364,15 @@ class Organization extends OrganizationModel { static function fromForm($form) { - if(!$form) return null; + if (!$form) + return null; //Validate the form $valid = true; if (!$form->isValid()) $valid = false; - //Make sure the email is not in-use + // Make sure the name is not in-use if (($field=$form->getField('name')) && $field->getClean() && Organization::lookup(array('name' => $field->getClean()))) { @@ -410,9 +418,9 @@ class OrganizationForm extends DynamicForm { return static::$form; } - static function getInstance($object_id=0, $new=false) { + static function getInstance($object_id=0, $new=false, $data=null) { if ($new || !isset(static::$instance)) - static::$instance = static::getDefaultForm()->instanciate(); + static::$instance = static::getDefaultForm()->instanciate(1, $data); static::$instance->object_type = 'O'; diff --git a/include/class.user.php b/include/class.user.php index 886c18db9759700a816045236fc67d44366cfb2c..db6e7936d3a4c6c32ee9151febdb99834e80686b 100644 --- a/include/class.user.php +++ b/include/class.user.php @@ -254,12 +254,12 @@ class User extends UserModel { return $this->created; } - function addForm($form, $sort=1) { - $form = $form->instanciate(); - $form->set('sort', $sort); - $form->set('object_type', 'U'); - $form->set('object_id', $this->getId()); - $form->save(); + function addForm($form, $sort=1, $data=null) { + $entry = $form->instanciate($sort, $data); + $entry->set('object_type', 'U'); + $entry->set('object_id', $this->getId()); + $entry->save(); + return $entry; } function to_json() { @@ -292,14 +292,11 @@ class User extends UserModel { } function addDynamicData($data) { - $uf = UserForm::getNewInstance(); - $uf->setClientId($this->id); - foreach ($uf->getFields() as $f) - if (isset($data[$f->get('name')])) - $uf->setAnswer($f->get('name'), $data[$f->get('name')]); - $uf->save(); - - return $uf; + $entry = $this->addForm(UserForm::objects()->one(), 1, $data); + // FIXME: For some reason, the second save here is required or the + // custom data is not properly saved + $entry->save(); + return $entry; } function getDynamicData($create=true) { diff --git a/include/client/templates/dynamic-form.tmpl.php b/include/client/templates/dynamic-form.tmpl.php index 12b3944075e71ad3788be4be515fadb5478a56e8..9c36caaed5787557b8f2ee6a4bc52320126d416b 100644 --- a/include/client/templates/dynamic-form.tmpl.php +++ b/include/client/templates/dynamic-form.tmpl.php @@ -5,8 +5,6 @@ ?> <tr><td colspan="2"><hr /> <div class="form-header" style="margin-bottom:0.5em"> - <?php print ($form instanceof DynamicFormEntry) - ? $form->getForm()->getMedia() : $form->getMedia(); ?> <h3><?php echo Format::htmlchars($form->getTitle()); ?></h3> <em><?php echo Format::htmlchars($form->getInstructions()); ?></em> </div> diff --git a/include/client/view.inc.php b/include/client/view.inc.php index 509315ff46057e90ba7bb316b9a87f047ab62050..1821aea04ef1e06ca51f36faad023eb56916e49e 100644 --- a/include/client/view.inc.php +++ b/include/client/view.inc.php @@ -179,6 +179,7 @@ if (!$ticket->isClosed() || $ticket->isReopenable()) { ?> if ($messageField->isAttachmentsEnabled()) { ?> <?php print $attachments->render(true); + print $attachments->getForm()->getMedia(); ?> <?php } ?> diff --git a/include/staff/orgs.inc.php b/include/staff/orgs.inc.php index fe805eb739bfa500fe9bf97b292e4f66a95ebb5e..3f500a6f1edf5cbe41ed7f3b2a57eb076f5832f0 100644 --- a/include/staff/orgs.inc.php +++ b/include/staff/orgs.inc.php @@ -205,7 +205,8 @@ $(function() { $(document).on('click', 'a.add-org', function(e) { e.preventDefault(); $.orgLookup('ajax.php/orgs/add', function (org) { - window.location.href = 'orgs.php?id='+org.id; + var url = 'orgs.php?id=' + org.id; + $.pjax({url: url, container: '#pjax-container'}) }); return false; diff --git a/include/staff/ticket-edit.inc.php b/include/staff/ticket-edit.inc.php index 2001e28505a525d15f0244f8d809c0e88d33bb8a..96d1abe6fc943457bf67cbfd3459582dd5b0da2c 100644 --- a/include/staff/ticket-edit.inc.php +++ b/include/staff/ticket-edit.inc.php @@ -134,7 +134,6 @@ if ($_POST) <?php if ($forms) foreach ($forms as $form) { $form->render(true, false, array('mode'=>'edit','width'=>160,'entry'=>$form)); - print $form->getForm()->getMedia(); } ?> </table> <table class="form_table" width="940" border="0" cellspacing="0" cellpadding="2"> diff --git a/include/staff/users.inc.php b/include/staff/users.inc.php index 3ac970a0d78a909b9b8d3d4cdff488ca09f0212e..ab21a960653dc950c66ede49df993c4eceaae6e4 100644 --- a/include/staff/users.inc.php +++ b/include/staff/users.inc.php @@ -257,10 +257,11 @@ $(function() { $(document).on('click', 'a.popup-dialog', function(e) { e.preventDefault(); $.userLookup('ajax.php/' + $(this).attr('href').substr(1), function (user) { + var url = window.location.href; if (user && user.id) - window.location.href = 'users.php?id='+user.id; - else - $.pjax({url: window.location.href, container: '#pjax-container'}) + url = 'users.php?id='+user.id; + $.pjax({url: url, container: '#pjax-container'}) + return false; }); return false; @@ -296,11 +297,14 @@ $(function() { $(document).on('dialog:close', function(e, json) { $form = $('form#users-list'); try { - var json = $.parseJSON(json); - $form.find('#org_id').val(json.id); - goBaby('setorg', true); + var json = $.parseJSON(json), + org_id = $form.find('#org_id'); + if (json.id) { + org_id.val(json.id); + goBaby('setorg', true); + } } - catch (e) { console.log(e); } + catch (e) { } }); }); </script> diff --git a/scp/js/scp.js b/scp/js/scp.js index 45186f42991eebc2efcd37fe3d2d562dd3e6c1c3..67bdca721289a744fd2a8de69340afd122579ce4 100644 --- a/scp/js/scp.js +++ b/scp/js/scp.js @@ -529,11 +529,11 @@ $.toggleOverlay = function (show) { return $.toggleOverlay(!$('#overlay').is(':visible')); } if (show) { - $('#overlay').fadeIn(); + $('#overlay').stop().hide().fadeIn(); $('body').css('overflow', 'hidden'); } else { - $('#overlay').fadeOut(); + $('#overlay').stop().fadeOut(); $('body').css('overflow', 'auto'); } }; @@ -570,13 +570,15 @@ $.dialog = function (url, codes, cb, options) { data: $form.serialize(), cache: false, success: function(resp, status, xhr) { - var done = $.Event('dialog:close'); if (xhr && xhr.status && codes && $.inArray(xhr.status, codes) != -1) { $.toggleOverlay(false); $popup.hide(); $('div.body', $popup).empty(); - if(cb) cb(xhr, resp); + if (cb && (false === cb(xhr, resp))) + // Don't fire event if callback returns false + return; + var done = $.Event('dialog:close'); $popup.trigger(done, [resp, status, xhr]); } else { $('div.body', $popup).html(resp); @@ -612,7 +614,7 @@ $.confirm = function(message, title) { var D = $.Deferred(), $popup = $('.dialog#popup'), hide = function() { - $('#overlay').hide(); + $.toggleOverlay(false); $popup.hide(); }; $('div#popup-loading', $popup).hide(); @@ -635,7 +637,7 @@ $.confirm = function(message, title) { .attr('value', __('OK')) .click(function() { hide(); D.resolve(); }) ))).append($('<div class="clear"></div>')); - $('#overlay').fadeIn(); + $.toggleOverlay(true); $popup.show(); return D.promise(); }; @@ -643,7 +645,7 @@ $.confirm = function(message, title) { $.userLookup = function (url, cb) { $.dialog(url, 201, function (xhr) { var user = $.parseJSON(xhr.responseText); - if (cb) cb(user); + if (cb) return cb(user); }, { onshow: function() { $('#user-search').focus(); } }); @@ -728,6 +730,7 @@ $(document).on('pjax:start', function() { $(window).unbind('beforeunload'); // Close popups $('.dialog .body').empty().parent().hide(); + $.toggleOverlay(false); // Close tooltips $('.tip_box').remove(); }); @@ -743,7 +746,8 @@ $(document).on('pjax:send', function(event) { // right $('#loadingbar').stop(false, true).width((50 + Math.random() * 30) + "%"); - $('#overlay').css('background-color','white').fadeIn(); + $('#overlay').css('background-color','white'); + $.toggleOverlay(true); }); $(document).on('pjax:complete', function() {