0011286: allow contacts without org/family name
[tine20] / tine20 / Addressbook / js / SearchCombo.js
1 /*
2  * Tine 2.0
3  * contacts combo box and store
4  * 
5  * @package     Addressbook
6  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
7  * @author      Philipp Schüle <p.schuele@metaways.de>
8  * @copyright   Copyright (c) 2007-2011 Metaways Infosystems GmbH (http://www.metaways.de)
9  *
10  */
11
12 Ext.ns('Tine.Addressbook');
13
14 /**
15  * contact selection combo box
16  * 
17  * @namespace   Tine.Addressbook
18  * @class       Tine.Addressbook.SearchCombo
19  * @extends     Ext.form.ComboBox
20  * 
21  * <p>Contact Search Combobox</p>
22  * <p><pre>
23  * TODO         make this a twin trigger field with 'clear' button?
24  * TODO         add switch to filter for expired/enabled/disabled user accounts
25  * </pre></p>
26  * 
27  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
28  * @author      Philipp Schuele <p.schuele@metaways.de>
29  * @copyright   Copyright (c) 2007-2011 Metaways Infosystems GmbH (http://www.metaways.de)
30  * 
31  * @param       {Object} config
32  * @constructor
33  * Create a new Tine.Addressbook.SearchCombo
34  * 
35  * TODO         add     forceSelection: true ?
36  */
37 Tine.Addressbook.SearchCombo = Ext.extend(Tine.Tinebase.widgets.form.RecordPickerComboBox, {
38     
39     /**
40      * @cfg {Boolean} userOnly
41      */
42     userOnly: false,
43     
44     /**
45      * use account objects/records in get/setValue
46      * 
47      * @cfg {Boolean} legacy
48      * @legacy
49      * 
50      * TODO remove this later
51      */
52     useAccountRecord: false,
53     allowBlank: false,
54     
55     itemSelector: 'div.search-item',
56     minListWidth: 350,
57     
58     //private
59     initComponent: function(){
60         this.recordClass = Tine.Addressbook.Model.Contact;
61         this.recordProxy = Tine.Addressbook.contactBackend;
62
63         this.initTemplate();
64         Tine.Addressbook.SearchCombo.superclass.initComponent.call(this);
65     },
66     
67     /**
68      * is called in accountMode to reset the value
69      * @param value
70      */
71     processValue: function(value) {
72         if (this.useAccountRecord) {
73             if (value == '') {
74                 this.accountId = null;
75                 this.selectedRecord = null;
76             }
77         }
78         return Tine.Addressbook.SearchCombo.superclass.processValue.call(this, value);
79     },
80
81     /**
82      * use beforequery to set query filter
83      * 
84      * @param {Event} qevent
85      */
86     onBeforeQuery: function(qevent){
87         Tine.Addressbook.SearchCombo.superclass.onBeforeQuery.apply(this, arguments);
88         
89         if (this.userOnly) {
90             this.store.baseParams.filter.push({field: 'type', operator: 'equals', value: 'user'});
91         }
92     },
93     
94     /**
95      * init template
96      * @private
97      */
98     initTemplate: function() {
99         // Custom rendering Template
100         // TODO move style def to css ?
101         if (! this.tpl) {
102             this.tpl = new Ext.XTemplate(
103                 '<tpl for="."><div class="search-item">',
104                     '<table cellspacing="0" cellpadding="2" border="0" style="font-size: 11px;" width="100%">',
105                         '<tr>',
106                             '<td style="min-width: 20px;">{[this.getIcon(values)]}</td>',
107                             '<td width="30%"><b>{[this.encode(values.n_fileas, "fileas")]}</b><br/>{[this.encode(values.org_name)]}</td>',
108                             '<td width="25%">{[this.encode(values.adr_one_street)]}<br/>',
109                                 '{[this.encode(values.adr_one_postalcode)]} {[this.encode(values.adr_one_locality)]}</td>',
110                             '<td width="25%">{[this.encode(values.tel_work)]}<br/>{[this.encode(values.tel_cell)]}</td>',
111                             '<td width="20%">',
112                                 '<img width="45px" height="39px" src="{jpegphoto}" />',
113                             '</td>',
114                         '</tr>',
115                     '</table>',
116                 '</div></tpl>',
117                 {
118                     getIcon: function(contactData) {
119                         return Tine.Addressbook.ContactGridPanel.prototype.contactTypeRenderer.call(this, null, null, contactData);
120                     },
121                     encode: function(value, field) {
122                         if (value) {
123                             return Ext.util.Format.htmlEncode(value);
124                         } else {
125                             return field == 'fileas' ?
126                                 Tine.Addressbook.ContactGridPanel.prototype.displayNameRenderer(value) :
127                                 '';
128                         }
129                     }
130                 }
131             );
132         }
133     },
134     
135     getValue: function() {
136         if (this.useAccountRecord) {
137             if (this.selectedRecord) {
138                 return this.selectedRecord.get('account_id');
139             } else {
140                 return this.accountId;
141             }
142         } else {
143             return Tine.Addressbook.SearchCombo.superclass.getValue.call(this);
144         }
145     },
146
147     setValue: function (value) {
148         if (this.useAccountRecord) {
149             if (value) {
150                 if(value.accountId) {
151                     // account object
152                     this.accountId = value.accountId;
153                     value = value.accountDisplayName;
154                 } else if (typeof(value.get) == 'function') {
155                     // account record
156                     this.accountId = value.get('id');
157                     value = value.get('name');
158                 }
159             } else {
160                 this.accountId = null;
161                 this.selectedRecord = null;
162             }
163         }
164         return Tine.Addressbook.SearchCombo.superclass.setValue.call(this, value);
165     }
166
167 });
168
169 Ext.reg('addressbookcontactpicker', Tine.Addressbook.SearchCombo);
170 Tine.widgets.form.RecordPickerManager.register('Addressbook', 'Contact', Tine.Addressbook.SearchCombo);