[2] | 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 - Quick Add Plugin |
---|
| 16 | * |
---|
| 17 | * |
---|
| 18 | * USAGE INSTRUCTIONS |
---|
| 19 | * |
---|
| 20 | * Just insert in your PHP code the following statement: |
---|
| 21 | * |
---|
| 22 | * $ccContents = ExecMethod('contactcenter.ui_api.get_quick_add_plugin'); |
---|
| 23 | * |
---|
| 24 | * and $ccContents becomes a valid HTML that inserts everything that is needed |
---|
| 25 | * to use the JS Objects that Contact Center provides. Just remember that you |
---|
| 26 | * need to insert it before your JS code, so all objects are instantiated |
---|
| 27 | * correctly. |
---|
| 28 | * |
---|
| 29 | * Provides Objects: |
---|
| 30 | * |
---|
| 31 | * ccQuickAdd |
---|
| 32 | * |
---|
| 33 | * Provides Classes: |
---|
| 34 | * |
---|
| 35 | * dJSWin, dTabsManager, dFTree |
---|
| 36 | * |
---|
| 37 | */ |
---|
| 38 | |
---|
| 39 | function cQuickAdd () |
---|
| 40 | { |
---|
| 41 | // Private |
---|
| 42 | this._card; |
---|
| 43 | this._button = new Array(); |
---|
| 44 | this._fields; |
---|
| 45 | |
---|
| 46 | // Public |
---|
| 47 | this.window; |
---|
| 48 | this.afterSave; |
---|
| 49 | |
---|
| 50 | // Constructor |
---|
| 51 | var wHeight = 0; |
---|
| 52 | this._nFields = Element('ccQAnFields').value; |
---|
| 53 | this._fields = new Array(); |
---|
| 54 | |
---|
| 55 | for (var i = 0; i < this._nFields; i++) |
---|
| 56 | { |
---|
| 57 | this._fields[i] = Element('ccQuickAddI'+i); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | //wHeight = (i+1)*25+5; |
---|
| 61 | wHeight = Element('ccQAWinHeight').value; |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | this.window = new dJSWin({ |
---|
| 65 | id: 'ccQuickAddDOM', |
---|
| 66 | content_id: 'ccQuickAddContent', |
---|
| 67 | width: '255px', |
---|
| 68 | height: wHeight+'px', |
---|
| 69 | title_color: '#3978d6', |
---|
| 70 | bg_color: '#eee', |
---|
| 71 | title: Element('ccQATitle').value, |
---|
| 72 | title_text_color: 'white', |
---|
| 73 | button_x_img: Element('cc_phpgw_img_dir').value+'/winclose.gif', |
---|
| 74 | border: true }); |
---|
| 75 | |
---|
| 76 | this.window.draw(); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /*! |
---|
| 80 | @method associateAsButton |
---|
| 81 | @abstract Associates the button functions with the spacified DOM Element |
---|
| 82 | @author Raphael Derosso Pereira |
---|
| 83 | |
---|
| 84 | @param div DOMElement The HTML DOM element that will "host" the |
---|
| 85 | plugin button. |
---|
| 86 | |
---|
| 87 | @param func function The function that returns the data to be used |
---|
| 88 | to pre-populate the quickAdd fields. The return format MUST be |
---|
| 89 | an Array like: |
---|
| 90 | |
---|
| 91 | var return_data = new Array(); |
---|
| 92 | return_data[0] = <value>; // Value for the first field |
---|
| 93 | return_data[1] = <value>; // Value for the second field |
---|
| 94 | ... |
---|
| 95 | |
---|
| 96 | */ |
---|
| 97 | cQuickAdd.prototype.associateAsButton = function (div, func) |
---|
| 98 | { |
---|
| 99 | var _this = this; |
---|
| 100 | |
---|
| 101 | if (func) |
---|
| 102 | { |
---|
| 103 | div.onclick = function() { _this.window.open(); _this.setValues(func()); }; |
---|
| 104 | } |
---|
| 105 | else |
---|
| 106 | { |
---|
| 107 | div.onclick = function() { _this.window.open(); }; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /*! |
---|
| 112 | |
---|
| 113 | @method setValues |
---|
| 114 | @abstract Set the contents of the QuickAdd window with the specified |
---|
| 115 | data |
---|
| 116 | @author Raphael Derosso Pereira |
---|
| 117 | |
---|
| 118 | @param data Array The data to be used |
---|
| 119 | |
---|
| 120 | */ |
---|
| 121 | cQuickAdd.prototype.setValues = function (data) |
---|
| 122 | { |
---|
| 123 | for (var i in data) |
---|
| 124 | { |
---|
| 125 | this._fields[i].value = data[i]; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | this.data = data; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /*! |
---|
| 132 | |
---|
| 133 | @method send |
---|
| 134 | @abstract Sends data to server |
---|
| 135 | @author Raphael Derosso Pereira |
---|
| 136 | |
---|
| 137 | */ |
---|
| 138 | cQuickAdd.prototype.send = function () |
---|
| 139 | { |
---|
| 140 | var _this = this; |
---|
| 141 | var handler = function (responseText) |
---|
| 142 | { |
---|
| 143 | Element('cc_debug').innerHTML = responseText; |
---|
| 144 | |
---|
| 145 | var data = unserialize(responseText); |
---|
| 146 | |
---|
| 147 | if (!data || typeof(data) != 'object') |
---|
| 148 | { |
---|
| 149 | showMessage(Element('cc_msg_err_contacting_server').value); |
---|
| 150 | return; |
---|
| 151 | } |
---|
| 152 | else if (data['status'] == 'alreadyExists') |
---|
| 153 | { |
---|
| 154 | showMessage(data['msg']); |
---|
| 155 | return; |
---|
| 156 | } |
---|
| 157 | else if (data['status'] != 'ok') |
---|
| 158 | { |
---|
| 159 | return; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | _this.clear(); |
---|
| 163 | _this.window.close(); |
---|
| 164 | |
---|
| 165 | if (_this.afterSave) |
---|
| 166 | { |
---|
| 167 | switch (typeof(_this.afterSave)) |
---|
| 168 | { |
---|
| 169 | case 'function': |
---|
| 170 | _this.afterSave(); |
---|
| 171 | break; |
---|
| 172 | |
---|
| 173 | case 'string': |
---|
| 174 | eval(_this.afterSave); |
---|
| 175 | break; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | var sdata = new Array(); |
---|
| 181 | var empty = true; |
---|
| 182 | |
---|
| 183 | for (var i in this._fields) |
---|
| 184 | { |
---|
| 185 | sdata[i] = this._fields[i].value; |
---|
| 186 | if (sdata[i] != '') |
---|
| 187 | { |
---|
| 188 | empty = false; |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | if (empty) return false; |
---|
| 193 | |
---|
| 194 | //Utiliza expressão regular para validar email |
---|
| 195 | var reEmail = /^[A-Za-z\d_-]+(\.[A-Za-z\d_-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/; |
---|
| 196 | |
---|
| 197 | if(!reEmail.test(this._fields[4].value)){ |
---|
| 198 | alert("O endereço de email '" + this._fields[4].value + "' não é válido!\n" + |
---|
| 199 | "Por favor informe um endereço válido."); |
---|
| 200 | return false; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | //Utiliza expressão regular para validar telefone |
---|
| 204 | var rePhone = /^[0-9|(-)| |-]{0,15}$/; |
---|
| 205 | |
---|
| 206 | if (!rePhone.test(this._fields[3].value)){ |
---|
| 207 | alert("O número de telefone '" + this._fields[3].value + "' não é valido! Utilize apenas números.\n" + |
---|
| 208 | "Parenteses, traços ou espaços em branco podem ser usados como carácter separador."); |
---|
| 209 | return false; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | var sdata = 'add='+escape(serialize(sdata)); |
---|
| 213 | |
---|
| 214 | Connector.newRequest('cQuickAdd.Send', CC_url+'quick_add', 'POST', handler, sdata); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /*! |
---|
| 218 | |
---|
| 219 | @method clear |
---|
| 220 | @abstract Clear all Plugin Fields |
---|
| 221 | @author Raphael Derosso Pereira |
---|
| 222 | |
---|
| 223 | */ |
---|
| 224 | cQuickAdd.prototype.clear = function () |
---|
| 225 | { |
---|
| 226 | for (var i in this._fields) |
---|
| 227 | { |
---|
| 228 | this._fields[i].value = ''; |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | /* Build the Object */ |
---|
| 234 | var ccQuickAdd; |
---|
| 235 | var cQuickAdd_pre_load = document.body.onload; |
---|
| 236 | |
---|
| 237 | if (is_ie) |
---|
| 238 | { |
---|
| 239 | document.body.onload = function (e) |
---|
| 240 | { |
---|
| 241 | cQuickAdd_pre_load(); |
---|
| 242 | ccQuickAdd = new cQuickAdd(); |
---|
| 243 | |
---|
| 244 | }; |
---|
| 245 | } |
---|
| 246 | else |
---|
| 247 | { |
---|
| 248 | ccQuickAdd = new cQuickAdd(); |
---|
| 249 | } |
---|