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

Revision 4502, 12.0 KB checked in by airton, 13 years ago (diff)

Ticket #1928 - Falhas na associação de um contato com um grupo de contato

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