Skip to content

Commit

Permalink
Merge branch 'pu/cw/fileAsTemplate' into '2024.11'
Browse files Browse the repository at this point in the history
feature(Addressbook): have n_fileas as configurable template

See merge request tine20/tine20!4825
  • Loading branch information
corneliusweiss committed Jan 29, 2024
2 parents a3a2e4f + 9db1a53 commit 55d5f4f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 16 deletions.
17 changes: 15 additions & 2 deletions tine20/Addressbook/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ class Addressbook_Config extends Tinebase_Config_Abstract
*
* @var string
*/
const CONTACT_SALUTATION = 'contactSalutation';

public const CONTACT_SALUTATION = 'contactSalutation';

public const FILE_AS_TEMPLATE = 'fileAsTemplate';

/**
* fields for list type
*
Expand Down Expand Up @@ -332,6 +334,17 @@ class Addressbook_Config extends Tinebase_Config_Abstract
'setByAdminModule' => FALSE,
'setBySetupModule' => FALSE,
),
self::FILE_AS_TEMPLATE => [
//_('Template for file_as field')
self::LABEL => 'Template for file_as field',
//_('Template for file_as field')
self::DESCRIPTION => 'Template for file_as field',
self::TYPE => self::TYPE_STRING,
self::DEFAULT_STR => '{% if n_family %}{% set n = n_family %}{% elseif org_name %}{% set n = org_name %}{% endif %}{% if n %}{{ n }}{% endif %}{% if n_given and n_given != n %}{% if n %}, {% endif %}{{ n_given }}{% endif %}',
self::CLIENTREGISTRYINCLUDE => true,
self::SETBYADMINMODULE => true,
self::SETBYSETUPMODULE => false,
],
self::LIST_TYPE => array(
//_('List types available')
'label' => 'List types available',
Expand Down
26 changes: 16 additions & 10 deletions tine20/Addressbook/Model/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1199,19 +1199,25 @@ protected function _resolveAutoValues(array &$_data)
}
}
}

// always update fileas and fn
$_data['n_fileas'] = (!empty($_data['n_family'])) ? $_data['n_family']
: ((! empty($_data['org_name'])) ? $_data['org_name']
: ((isset($_data['n_fileas'])) ? $_data['n_fileas'] : ''));

if (!empty($_data['n_given']) && $_data['n_given'] !== $_data['n_fileas']) {
if (!empty($_data['n_fileas'])) {
$_data['n_fileas'] .= ', ';

if (empty($_data['n_fileas'])) {
$name = 'n_fileas';
$template = Addressbook_Config::getInstance()->{Addressbook_Config::FILE_AS_TEMPLATE};

$locale = Tinebase_Core::getLocale();
if (! $locale) {
$locale = Tinebase_Translation::getLocale();
}
$_data['n_fileas'] .= $_data['n_given'];
$twig = new Tinebase_Twig($locale, Tinebase_Translation::getTranslation(), [
Tinebase_Twig::TWIG_LOADER =>
new Tinebase_Twig_CallBackLoader(__METHOD__ . $name, time() - 1, function () use ($template) {
return $template;
})
]);
$_data['n_fileas'] = $twig->load(__METHOD__ . $name)->render($_data);
}

// always update fn
if (!empty($_data['n_given'])) {
$_data['n_fn'] = $_data['n_given'] . (!empty($_data['n_family']) ? ' ' . $_data['n_family'] : '');
} else {
Expand Down
35 changes: 32 additions & 3 deletions tine20/Addressbook/js/ContactEditDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

/*global Ext, Tine*/
import getTwingEnv from "twingEnv";
import { getAddressPanels } from "./AddressPanel";
import contactPropertiesGrid from "./ContactPropertiesGrid";

Expand Down Expand Up @@ -206,10 +207,9 @@ Tine.Addressbook.ContactEditDialog = Ext.extend(Tine.widgets.dialog.EditDialog,
!Tine.Tinebase.appMgr.get('Addressbook').featureEnabled('featureIndustry') ?
{
columnWidth: 0.64,
xtype: 'combo',
xtype: 'textfield',
fieldLabel: this.app.i18n._('Display Name'),
name: 'n_fn',
disabled: true
name: 'n_fileas'
} :
(
new Tine.Addressbook.IndustrySearchCombo({
Expand Down Expand Up @@ -398,6 +398,33 @@ Tine.Addressbook.ContactEditDialog = Ext.extend(Tine.widgets.dialog.EditDialog,
frame: false
});
Tine.Addressbook.ContactEditDialog.superclass.initComponent.apply(this, arguments);

// init suggestions
this.twingEnv = getTwingEnv();
const loader = this.twingEnv.getLoader();
loader.setTemplate('n_fileas', Tine.Tinebase.configManager.get('fileAsTemplate', 'Addressbook'));
this.getForm().items.each((field) => {
if (field.initKeyEvents) {
field.initKeyEvents();
}
field.on('keyup', this.suggestFields, this);
});
},

suggestFields: async function() {
const fieldName = 'n_fileas';
const field = this.getForm().findField(fieldName);
const value = field?.getValue();
if (field && (!value || value === field.suggestedValue)) {
this.onRecordUpdate();
// @FIXME twing can't cope with null values yet, remove this once twing fixed it
const data = JSON.parse(JSON.stringify(this.record.data).replace(/:null([,}])/g, ':""$1'));
const suggestion = await this.twingEnv.render(fieldName, data);

field.setValue(suggestion);
this.record.set(fieldName, suggestion);
field.suggestedValue = suggestion;
}
},

/**
Expand Down Expand Up @@ -537,6 +564,8 @@ Tine.Addressbook.ContactEditDialog = Ext.extend(Tine.widgets.dialog.EditDialog,
if(Tine.Tinebase.registry.get('currentAccount').contact_id == this.record.id) {
this.enableOwnPrivateFields();
}

this.suggestFields();
},

/**
Expand Down
4 changes: 4 additions & 0 deletions tine20/Tinebase/js/widgets/form/AutoCompleteField.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ Tine.Tinebase.widgets.form.AutoCompleteField = Ext.extend(Ext.form.ComboBox, {


Tine.Tinebase.widgets.form.AutoCompleteField.superclass.initComponent.call(this);
},

getValue: function() {
return this.getRawValue();
}
});

Expand Down
10 changes: 9 additions & 1 deletion tine20/library/ExtJS/src/widgets/form/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,24 @@ var myField = new Ext.form.NumberField({
this.mon(this.el, 'click', this.autoSize, this);
}
if(this.enableKeyEvents){
this.initKeyEvents();
}
},

initKeyEvents: function() {
this.enableKeyEvents = true;
if (this.el && !this.keyEventsInitialized) {
this.mon(this.el, {
scope: this,
keyup: this.onKeyUp,
paste: this.onPaste,
keydown: this.onKeyDown,
keypress: this.onKeyPress
});
this.keyEventsInitialized = true;
}
},

onMouseDown: function(e){
if(!this.hasFocus){
this.mon(this.el, 'mouseup', Ext.emptyFn, this, { single: true, preventDefault: true });
Expand Down

0 comments on commit 55d5f4f

Please sign in to comment.