source: sandbox/2.2.0.2/contactcenter/js/ccAddGroup.js @ 4477

Revision 4477, 11.6 KB checked in by airton, 13 years ago (diff)

Ticket #1912 - Permitir a criação de contatos sem email

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1  /***************************************************************************\
2  * eGroupWare - Contacts Center                                              *
3  * http://www.egroupware.org                                                 *
4  * Written by:                                                               *
5  *  - Raphael Derosso Pereira <raphaelpereira@users.sourceforge.net>         *
6  *  sponsored by Thyamad - http://www.thyamad.com                            *
7  * ------------------------------------------------------------------------- *
8  *  This program is free software; you can redistribute it and/or modify it  *
9  *  under the terms of the GNU General Public License as published by the    *
10  *  Free Software Foundation; either version 2 of the License, or (at your   *
11  *  option) any later version.                                               *
12  \***************************************************************************/
13
14        /*
15         * ContactCenter API - Add Group
16         *
17         * USAGE INSTRUCTIONS
18         */
19
20        function cAddGroup ()
21        {
22                // Private
23                this._card;
24                this._button = new Array();             
25
26                // Public
27                this.window;
28                this.afterSave;
29                this.load;
30               
31                // Constructor
32                var wHeight = 0;
33               
34                // Elements
35                this.title = Element('title');
36                this.contact_in_list = Element('contact_in_list');
37                this.group_id = Element('group_id');
38                               
39                ccAGWinHeight = 'ccAGWinHeightMO';
40
41                if (is_ie)
42                        ccAGWinHeight = 'ccAGWinHeightIE';
43
44                wHeight = Element(ccAGWinHeight).value;
45
46               
47                this.window = new dJSWin({
48                        id: 'ccAddGroup DOM',
49                        content_id: 'ccAddGroupContent',
50                        width: '700px',
51                        height: wHeight+'px',
52                        title_color: '#3978d6',
53                        bg_color: '#eee',
54                        title: Element('ccAGTitle').value,
55                        title_text_color: 'white',
56                        button_x_img: Element('cc_phpgw_img_dir').value+'/winclose.gif',
57                        border: true });
58
59                this.window.draw();
60               
61        }
62
63        /*!
64                @method associateAsButton
65                @abstract Associates the button functions with the spacified DOM Element
66                @author Raphael Derosso Pereira
67
68                @param div DOMElement The HTML DOM element that will "host" the
69                        plugin button.
70
71                @param func function The function that returns the data to be used
72                        to pre-populate the quickAdd fields. The return format MUST be
73                        an Array like:
74
75                                var return_data = new Array();
76                                return_data[0] = <value>;  // Value for the first field
77                                return_data[1] = <value>;  // Value for the second field
78                                ...
79               
80         */
81        cAddGroup.prototype.associateAsButton = function (div)
82        {
83                var _this = this;               
84                div.onclick = function() {
85                                       
86                        if (_this.load) {
87                                switch (typeof(_this.load)) {
88                               
89                                        case 'function':
90                                                _this.load();
91                                                break;
92
93                                        case 'string':
94                                                eval(_this.load);
95                                                break;
96                                }
97                        }
98                };
99               
100        }
101               
102        /*!
103       
104                @method send
105                @abstract Sends data to server
106                @author Raphael Derosso Pereira
107
108        */
109        cAddGroup.prototype.send = function ()
110        {
111                var _this = this;
112
113                var handler = function (responseText)
114                {
115                        Element('cc_debug').innerHTML = responseText;
116                        var data = unserialize(responseText);                           
117                       
118                        if (!data || typeof(data) != 'object')
119                        {
120                                showMessage(Element('cc_msg_err_contacting_server').value);
121                                return;
122                        }
123
124                        //showMessage(data['msg']);
125
126                        if (data['status'] != 'ok' && data['status'] != 'warning')
127                        {
128                                return;
129                        }
130                       
131                        if(data['status'] == 'warning')
132                                showMessage(data['msg']);
133
134                        _this.clear();
135                        _this.window.close();
136                       
137                        if (_this.afterSave)
138                        {
139                                switch (typeof(_this.afterSave))
140                                {
141                                        case 'function':
142                                                _this.afterSave();
143                                                break;
144
145                                        case 'string':
146                                                eval(_this.afterSave);
147                                                break;
148                                }
149                        }
150                }
151
152                var sdata = new Array();
153                var empty = true;
154               
155                sdata[0] = this.title.value;
156                var contacts = new Array();                             
157               
158                for (j =0; j < this.contact_in_list.length; j++)
159                        contacts[j] = this.contact_in_list.options[j].value;                   
160               
161                if(!this.title.value) {
162                        alert(Element('cc_msg_fill_field_name').value);
163                        this.title.focus();
164                        return false;
165                }
166                               
167                if(! contacts.length) {
168                        alert(Element('cc_msg_add_contact_to_group').value);
169                        return false;
170                }
171
172                sdata[1] = contacts;           
173                sdata[2] = this.group_id.value == 'undefined' ?         sdata[2] = 0 : sdata[2]  = this.group_id.value;                                                 
174                var sdata = 'add='+escape(serialize(sdata));
175                Connector.newRequest('cAddGroup.Send', CC_url+'add_group', 'POST', handler, sdata);
176        }
177
178        /*!
179
180                @method clear
181                @abstract Clear all Plugin Fields
182                @author Raphael Derosso Pereira
183
184        */
185        cAddGroup.prototype.clear = function (reload)
186        {
187                for (j =0; j < this.contact_in_list.options.length; j++) {
188                        this.contact_in_list.options[j].selected = false;
189                        this.contact_in_list.options[j--] = null;
190                }
191               
192                if(reload) {
193                        if(Element("contact_list"))
194                        for (j =0; j < Element("contact_list").options.length; j++) {
195                                        Element("contact_list").options[j].selected = false;
196                                        Element("contact_list").options[j--] = null;
197                        }
198                }
199                       
200                this.title.value = '';                         
201        }
202       
203        /* Função para remover contato da lista */     
204       
205        cAddGroup.prototype.remUser = function(){
206               
207                select_in = this.contact_in_list;                                                               
208
209                for(var i = 0;i < select_in.options.length; i++)                               
210                        if(select_in.options[i].selected)
211                                select_in.options[i--] = null;
212        }       
213       
214        /* Função para adicionar contato na lista */   
215        cAddGroup.prototype.addUser = function(){
216
217                var select = Element("contact_list");
218                var select_in = this.contact_in_list;
219               
220                for (i = 0 ; i < select.length ; i++) {                         
221
222                        if (select.options[i].selected) {
223                                isSelected = false;
224
225                                for(var j = 0;j < select_in.options.length; j++) {                                                                                                                                                     
226                                        if(select_in.options[j].value == select.options[i].value){
227                                                isSelected = true;                                             
228                                                break; 
229                                        }
230                                }
231
232                                if(!isSelected){
233
234                                        option = document.createElement('option');
235                                        option.value =select.options[i].value;
236                                        option.text = select.options[i].text;
237                                        option.selected = true;
238                                        select_in.options[select_in.options.length] = option;
239                                                                                       
240                                }
241                                                                                               
242                        }
243                }
244               
245                for (j =0; j < select.options.length; j++)
246                        select .options[j].selected = false;           
247        }       
248
249        cAddGroup.prototype.setSelectedSourceLevel = function(sourceLevel)
250        {
251            var ccAGSourceSelect = Element('ccAGSourceSelect');
252            var selectedLevel = '';
253            for (i = 0; i < ccAGSourceSelect.length; i++)
254            {
255                if (ccAGSourceSelect[i].selected == true)
256                {
257                    selectedLevel = ccAGSourceSelect[i].value;
258                }
259            }
260
261            if (selectedLevel != sourceLevel)
262            {
263                for (i = 0; i < ccAGSourceSelect.length; i++)
264                {
265                    if (ccAGSourceSelect[i].value == sourceLevel)
266                    {
267                        ccAGSourceSelect[i].selected = true;
268                    }
269                }
270            }
271            this.setCatalog();
272        }
273
274        cAddGroup.prototype.loadPersonalContacts = function(){
275            handler = function(data)
276            {
277                if (data)
278                {
279                    data = unserialize(data);
280                    if (data.result == 'ok')
281                    {
282                        var options_contact_list = Element('span_contact_list');
283                        var select_contact_list = '<select id="contact_list" multiple name="contact_list[]" style="width:280px" size="10">';
284                        select_contact_list += data['contact_list'] + "</select>";
285                        options_contact_list.innerHTML = select_contact_list;
286                    }
287                    return;
288                }
289
290                alert(get_lang('Erro ao carregar contatos pessoais.'));
291
292
293            }
294
295            Connector.newRequest('cAddGroup.loadPersonalContacts', CC_url+'get_group', 'GET', handler);
296
297        }
298
299        cAddGroup.prototype.clearSourceList = function(){
300            var contact_list = Element("contact_list");
301            var options = contact_list.options;
302            for (j =0; j < options.length; j++) {
303                contact_list.options[j].selected = false;
304                contact_list.options[j--] = null;
305            }
306        }
307
308        // usar ui_data.get_cards_data('all', 1);
309        cAddGroup.prototype.search = function(){
310
311            var type = Element('cc_type_contact').value;
312
313            var data = new Array();
314            data['fields'] = new Array();
315
316            // never search groups
317            data['fields']['id']     = 'contact.id_contact';
318            data['fields']['search'] = 'contact.names_ordered';
319
320            var ccAGSearchTerm = Element('ccAGSearchTerm');
321
322            data['search_for'] = ccAGSearchTerm.value;
323            data['ccAddGroup'] = true;
324
325            var invalidChars = /[\%\?]/;
326            if(invalidChars.test(data['search_for']) || invalidChars.test(data['search_for_area'])){
327                showMessage(Element('cc_msg_err_invalid_serch').value);
328                return;
329            }
330
331            var search_for = data['search_for'].split(' ');
332            var greaterThan4 = false;
333            var use_length = v_min;
334
335            for (i = 0; i < search_for.length; i++)
336            {
337                if (search_for[i].length >= use_length)
338                {
339                    greaterThan4 = true;
340                }
341            }
342
343            if (!greaterThan4){
344                alert("Favor fazer a consulta com pelo menos " + v_min + " caracteres!");
345                return;
346            }
347
348            var handler = function(data)
349            {
350                data = unserialize(data);
351
352                if( !data )
353                    return false;
354                ccAddGroup.clearSourceList();
355                ccAGSearchTerm.value = '';
356                if (typeof(data) != 'object')
357                {
358                    showMessage(Element('cc_msg_err_contacting_server').value);
359                    return false;
360                }
361
362                if (data[3].length > 300)
363                {
364                    alert("Mais de 300 resultados foram retornados! \n Favor refinar sua busca.");
365
366                    return false;
367                }
368
369                var contact_list = Element('contact_list');
370                for (var i=0; i < data[3].length; i++)
371                {
372
373                                        var item = data[3][i];
374                                        if (data[8] == 'bo_shared_people_manager'){
375                                                var id = data[3][i][6];
376                                        }       
377                                        else
378                                        {
379                                                var id = 'ldap:'+data[11]+':'+item[6];
380                                        }
381                                        var option = document.createElement('OPTION');
382                                        option.value = id;
383                                        option.text = item[1]+' ('+item[4]+')';
384                                        contact_list.options[contact_list.options.length] = option;
385                }
386
387            }
388
389            Connector.newRequest('ccAGSearch', CC_url+'search&data='+serialize(data), 'GET', handler);
390
391        }
392
393        cAddGroup.prototype.setCatalog = function(){
394            var select = Element('ccAGSourceSelect');
395            var catalogLevel = '0.0';
396            for (i = 0 ; i < select.length ; i++) {
397                if (select.options[i].selected)
398                {
399                    catalogLevel = select.options[i].value;
400                    break;
401                }
402
403            }
404            ccTree.select(catalogLevel);
405
406            if (catalogLevel == '0.0')
407            {
408                ccAddGroup.loadPersonalContacts();
409            }
410            else
411                {
412                   ccAddGroup.clearSourceList();
413                }
414
415            //eval(refresh);
416
417        }
418       
419        /* Build the Object */
420        var ccAddGroup ;
421        var cAddGroup_pre_load = document.body.onload;
422        /* Se for IE, modifica a largura da coluna dos botoes.*/       
423        if(document.all)
424                document.getElementById('buttons').width = 140;
425
426        if (is_ie)
427        {
428                document.body.onload = function (e)
429                {
430                        cAddGroup_pre_load();
431                        ccAddGroup = new cAddGroup();
432                       
433                };
434        }
435        else
436        {
437                ccAddGroup = new cAddGroup();
438        }
Note: See TracBrowser for help on using the repository browser.