don't add account email address in contact search combo
[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         
61         if (this.recordClass === null) {
62             this.recordClass = Tine.Addressbook.Model.Contact;
63             this.recordProxy = Tine.Addressbook.contactBackend;
64         }
65
66         this.initTemplate();
67         Tine.Addressbook.SearchCombo.superclass.initComponent.call(this);
68     },
69     
70     /**
71      * is called in accountMode to reset the value
72      * @param value
73      */
74     processValue: function(value) {
75         if (this.useAccountRecord) {
76             if (value == '') {
77                 this.accountId = null;
78                 this.selectedRecord = null;
79             }
80         }
81         return Tine.Addressbook.SearchCombo.superclass.processValue.call(this, value);
82     },
83
84     /**
85      * use beforequery to set query filter
86      * 
87      * @param {Event} qevent
88      */
89     onBeforeQuery: function(qevent){
90         Tine.Addressbook.SearchCombo.superclass.onBeforeQuery.apply(this, arguments);
91         
92         if (this.userOnly) {
93             this.store.baseParams.filter.push({field: 'type', operator: 'equals', value: 'user'});
94         }
95     },
96     
97     /**
98      * init template
99      * @private
100      */
101     initTemplate: function() {
102         // Custom rendering Template
103         if (! this.tpl) {
104             this.tpl = new Ext.XTemplate(
105                 '<tpl for="."><div class="search-item addressbook-search-combo">',
106                     '<table>',
107                         '<tr>',
108                             '<td style="min-width: 20px;">{[Tine.Addressbook.ContactGridPanel.contactTypeRenderer(null, null, values)]}</td>',
109                             '<td width="30%"><b>{[Tine.Addressbook.ContactGridPanel.displayNameRenderer(values.n_fileas)]}</b><br/>,' +
110                                 '{[this.encode(values.org_name)]}</td>',
111                             '<td width="25%">{[this.encode(values.adr_one_street)]}<br/>',
112                                 '{[this.encode(values.adr_one_postalcode)]} {[this.encode(values.adr_one_locality)]}</td>',
113                             '<td width="25%">{[this.encode(values.tel_work)]}<br/>{[this.encode(values.tel_cell)]}</td>',
114                             '<td width="50px">',
115                                 '<img width="45px" height="39px" src="{jpegphoto}" />',
116                             '</td>',
117                         '</tr>',
118                     '</table>',
119                 '</div></tpl>'
120              );
121         }
122     },
123     
124     getValue: function() {
125         if (this.useAccountRecord) {
126             if (this.selectedRecord) {
127                 return this.selectedRecord.get('account_id');
128             } else {
129                 return this.accountId;
130             }
131         } else {
132             return Tine.Addressbook.SearchCombo.superclass.getValue.call(this);
133         }
134     },
135
136     setValue: function (value) {
137         if (this.useAccountRecord) {
138             if (value) {
139                 if(value.accountId) {
140                     // account object
141                     this.accountId = value.accountId;
142                     value = value.accountDisplayName;
143                 } else if (typeof(value.get) == 'function') {
144                     // account record
145                     this.accountId = value.get('id');
146                     value = value.get('name');
147                 }
148             } else {
149                 this.accountId = null;
150                 this.selectedRecord = null;
151             }
152         }
153         return Tine.Addressbook.SearchCombo.superclass.setValue.call(this, value);
154     }
155
156 });
157
158 Ext.reg('addressbookcontactpicker', Tine.Addressbook.SearchCombo);
159 Tine.widgets.form.RecordPickerManager.register('Addressbook', 'Contact', Tine.Addressbook.SearchCombo);