source: branches/2.2.0.1/contactcenter/js/ccAddGroup.js @ 4132

Revision 4132, 11.5 KB checked in by rafaelraymundo, 13 years ago (diff)

Ticket #1593 - Criação de grupos usando contatos apartir do catalogo geral.

  • 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')
127                        {
128                                showMessage(data['msg']);
129                                return;
130                        }
131
132                        _this.clear();
133                        _this.window.close();
134                       
135                        if (_this.afterSave)
136                        {
137                                switch (typeof(_this.afterSave))
138                                {
139                                        case 'function':
140                                                _this.afterSave();
141                                                break;
142
143                                        case 'string':
144                                                eval(_this.afterSave);
145                                                break;
146                                }
147                        }
148                }
149
150                var sdata = new Array();
151                var empty = true;
152               
153                sdata[0] = this.title.value;
154                var contacts = new Array();                             
155               
156                for (j =0; j < this.contact_in_list.length; j++)
157                        contacts[j] = this.contact_in_list.options[j].value;                   
158               
159                if(!this.title.value) {
160                        alert(Element('cc_msg_fill_field_name').value);
161                        this.title.focus();
162                        return false;
163                }
164                               
165                if(! contacts.length) {
166                        alert(Element('cc_msg_add_contact_to_group').value);
167                        return false;
168                }
169
170                sdata[1] = contacts;           
171                sdata[2] = this.group_id.value == 'undefined' ?         sdata[2] = 0 : sdata[2]  = this.group_id.value;                                                 
172                var sdata = 'add='+escape(serialize(sdata));
173                Connector.newRequest('cAddGroup.Send', CC_url+'add_group', 'POST', handler, sdata);
174        }
175
176        /*!
177
178                @method clear
179                @abstract Clear all Plugin Fields
180                @author Raphael Derosso Pereira
181
182        */
183        cAddGroup.prototype.clear = function (reload)
184        {
185                for (j =0; j < this.contact_in_list.options.length; j++) {
186                        this.contact_in_list.options[j].selected = false;
187                        this.contact_in_list.options[j--] = null;
188                }
189               
190                if(reload) {
191                        if(Element("contact_list"))
192                        for (j =0; j < Element("contact_list").options.length; j++) {
193                                        Element("contact_list").options[j].selected = false;
194                                        Element("contact_list").options[j--] = null;
195                        }
196                }
197                       
198                this.title.value = '';                         
199        }
200       
201        /* Função para remover contato da lista */     
202       
203        cAddGroup.prototype.remUser = function(){
204               
205                select_in = this.contact_in_list;                                                               
206
207                for(var i = 0;i < select_in.options.length; i++)                               
208                        if(select_in.options[i].selected)
209                                select_in.options[i--] = null;
210        }       
211       
212        /* Função para adicionar contato na lista */   
213        cAddGroup.prototype.addUser = function(){
214
215                var select = Element("contact_list");
216                var select_in = this.contact_in_list;
217               
218                for (i = 0 ; i < select.length ; i++) {                         
219
220                        if (select.options[i].selected) {
221                                isSelected = false;
222
223                                for(var j = 0;j < select_in.options.length; j++) {                                                                                                                                                     
224                                        if(select_in.options[j].value == select.options[i].value){
225                                                isSelected = true;                                             
226                                                break; 
227                                        }
228                                }
229
230                                if(!isSelected){
231
232                                        option = document.createElement('option');
233                                        option.value =select.options[i].value;
234                                        option.text = select.options[i].text;
235                                        option.selected = true;
236                                        select_in.options[select_in.options.length] = option;
237                                                                                       
238                                }
239                                                                                               
240                        }
241                }
242               
243                for (j =0; j < select.options.length; j++)
244                        select .options[j].selected = false;           
245        }       
246
247        cAddGroup.prototype.setSelectedSourceLevel = function(sourceLevel)
248        {
249            var ccAGSourceSelect = Element('ccAGSourceSelect');
250            var selectedLevel = '';
251            for (i = 0; i < ccAGSourceSelect.length; i++)
252            {
253                if (ccAGSourceSelect[i].selected == true)
254                {
255                    selectedLevel = ccAGSourceSelect[i].value;
256                }
257            }
258
259            if (selectedLevel != sourceLevel)
260            {
261                for (i = 0; i < ccAGSourceSelect.length; i++)
262                {
263                    if (ccAGSourceSelect[i].value == sourceLevel)
264                    {
265                        ccAGSourceSelect[i].selected = true;
266                    }
267                }
268            }
269            this.setCatalog();
270        }
271
272        cAddGroup.prototype.loadPersonalContacts = function(){
273            handler = function(data)
274            {
275                if (data)
276                {
277                    data = unserialize(data);
278                    if (data.result == 'ok')
279                    {
280                        var options_contact_list = Element('span_contact_list');
281                        var select_contact_list = '<select id="contact_list" multiple name="contact_list[]" style="width:280px" size="10">';
282                        select_contact_list += data['contact_list'] + "</select>";
283                        options_contact_list.innerHTML = select_contact_list;
284                    }
285                    return;
286                }
287
288                alert(get_lang('Erro ao carregar contatos pessoais.'));
289
290
291            }
292
293            Connector.newRequest('cAddGroup.loadPersonalContacts', CC_url+'get_group', 'GET', handler);
294
295        }
296
297        cAddGroup.prototype.clearSourceList = function(){
298            var contact_list = Element("contact_list");
299            var options = contact_list.options;
300            for (j =0; j < options.length; j++) {
301                contact_list.options[j].selected = false;
302                contact_list.options[j--] = null;
303            }
304        }
305
306        // usar ui_data.get_cards_data('all', 1);
307        cAddGroup.prototype.search = function(){
308
309            var type = Element('cc_type_contact').value;
310
311            var data = new Array();
312            data['fields'] = new Array();
313
314            // never search groups
315            data['fields']['id']     = 'contact.id_contact';
316            data['fields']['search'] = 'contact.names_ordered';
317
318            var ccAGSearchTerm = Element('ccAGSearchTerm');
319
320            data['search_for'] = ccAGSearchTerm.value;
321            data['ccAddGroup'] = true;
322
323            var invalidChars = /[\%\?]/;
324            if(invalidChars.test(data['search_for']) || invalidChars.test(data['search_for_area'])){
325                showMessage(Element('cc_msg_err_invalid_serch').value);
326                return;
327            }
328
329            var search_for = data['search_for'].split(' ');
330            var greaterThan4 = false;
331            var use_length = v_min;
332
333            for (i = 0; i < search_for.length; i++)
334            {
335                if (search_for[i].length >= use_length)
336                {
337                    greaterThan4 = true;
338                }
339            }
340
341            if (!greaterThan4){
342                alert("Favor fazer a consulta com pelo menos " + v_min + " caracteres!");
343                return;
344            }
345
346            var handler = function(data)
347            {
348                data = unserialize(data);
349
350                if( !data )
351                    return false;
352                ccAddGroup.clearSourceList();
353                ccAGSearchTerm.value = '';
354                if (typeof(data) != 'object')
355                {
356                    showMessage(Element('cc_msg_err_contacting_server').value);
357                    return false;
358                }
359
360                if (data[3].length > 300)
361                {
362                    alert("Mais de 300 resultados foram retornados! \n Favor refinar sua busca.");
363
364                    return false;
365                }
366
367                var contact_list = Element('contact_list');
368                for (var i=0; i < data[3].length; i++)
369                {
370
371                   var item = data[3][i];
372                   var id = 'ldap:'+data[11]+':'+item[6];
373                   var option = document.createElement('OPTION');
374                   option.value = id;
375                   option.text = item[1]+' ('+item[4]+')';
376                   contact_list.options[contact_list.options.length] = option;
377                }
378
379            }
380
381            Connector.newRequest('ccAGSearch', CC_url+'search&data='+serialize(data), 'GET', handler);
382
383        }
384
385        cAddGroup.prototype.setCatalog = function(){
386            var select = Element('ccAGSourceSelect');
387            var catalogLevel = '0.0';
388            for (i = 0 ; i < select.length ; i++) {
389                if (select.options[i].selected)
390                {
391                    catalogLevel = select.options[i].value;
392                    break;
393                }
394
395            }
396            ccTree.select(catalogLevel);
397
398            if (catalogLevel == '0.0')
399            {
400                ccAddGroup.loadPersonalContacts();
401            }
402            else
403                {
404                   ccAddGroup.clearSourceList();
405                }
406
407            //eval(refresh);
408
409        }
410       
411        /* Build the Object */
412        var ccAddGroup ;
413        var cAddGroup_pre_load = document.body.onload;
414        /* Se for IE, modifica a largura da coluna dos botoes.*/       
415        if(document.all)
416                document.getElementById('buttons').width = 140;
417
418        if (is_ie)
419        {
420                document.body.onload = function (e)
421                {
422                        cAddGroup_pre_load();
423                        ccAddGroup = new cAddGroup();
424                       
425                };
426        }
427        else
428        {
429                ccAddGroup = new cAddGroup();
430        }
Note: See TracBrowser for help on using the repository browser.