source: trunk/contactcenter/js/ccAddGroup.js @ 5613

Revision 5613, 13.2 KB checked in by angelo, 12 years ago (diff)

Ticket #2510 - Mensagem de alerta para contato sem email em um grupo

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