-
Notifications
You must be signed in to change notification settings - Fork 3
/
vicidial_users_module.php
337 lines (263 loc) · 9.47 KB
/
vicidial_users_module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//set_include_path (get_include_path() .PATH_SEPARATOR.'/var/www/html/vtiger') ;
include_once 'vtlib/Vtiger/Module.php';
// Configuration:
//DB
$host = 'localhost';
$username = 'root';
$password = 'vicidialnow';
$db_name = 'asterisk';
//Source details
$table_name = "vicidial_inbound_groups";
$MODULENAME = 'GOinboundGroups';
$identifier = 'group_name';
$id_field = 'group_id';
$SOAP_url = 'http://128.199.48.154/mySOAP/index.php?wsdl';
$Vtiger_Utils_Log = true;
$filter_fields = array('user','full_name','user_group','phone_login');
$db = new mysqli($host, $username, $password, $db_name);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = <<<SQL
select * from information_schema.`COLUMNS` where TABLE_NAME = '$table_name'
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
function getVtigerProperties($field){
switch ($field['DATA_TYPE']){
case 'int':
case 'tinyint':
$columntype = 'int';
$typeofdata = 'N';
$uitype = '2';
break;
case 'varchar':
$columntype = 'varchar('.$field['CHARACTER_MAXIMUM_LENGTH'].')';
$typeofdata = 'V';
$uitype = '2';
break;
case 'datetime':
$columntype = 'datetime';
$typeofdata = 'D';
$uitype = '70';
break;
case 'enum':
$columntype = 'varchar('.$field['CHARACTER_MAXIMUM_LENGTH'].')';
$typeofdata = 'V';
$uitype = '15';
break;
default:
$typeofdata = 'V';
}
if ($field['IS_NULLABLE'] == 'YES')
$typeofdata.='~O';
else if ($field['IS_NULLABLE'] == 'NO')
$typeofdata.='~M';
return array('columntype' =>$columntype,'typeofdata' => $typeofdata,'uitype' => $uitype );
}
function getValuesFromEnum($enum){
if (($tmp = strstr($enum, '(')) !== false) {
$enum = substr(substr($tmp, 1),0,-1);
}
$arr = explode(',',$enum);
$arr = str_replace(array('\'', '"'), '', $arr);
return $arr;
}
$moduleInstance = Vtiger_Module::getInstance($MODULENAME);
if ($moduleInstance || file_exists('/modules/'.$MODULENAME)) {
echo "Module already present - choose a different name.";
} else {
$moduleInstance = new Vtiger_Module();
$moduleInstance->name = $MODULENAME;
$moduleInstance->parent= 'Tools';
$moduleInstance->save();
// Schema Setup
$moduleInstance->initTables();
// Field Setup
$block = new Vtiger_Block();
$block->label = 'LBL_'. strtoupper($moduleInstance->name) . '_INFORMATION';
$moduleInstance->addBlock($block);
$blockcf = new Vtiger_Block();
$blockcf->label = 'LBL_CUSTOM_INFORMATION';
$moduleInstance->addBlock($blockcf);
while($field = $result->fetch_assoc()){
//$row['VTIGER_properties'] = getVtigerProperties($row);
//$res[] = $row;
$fieldProperties = getVtigerProperties($field);
$$field['COLUMN_NAME'] = new Vtiger_Field();
$$field['COLUMN_NAME']->name = $field['COLUMN_NAME'];
$$field['COLUMN_NAME']->label= $field['COLUMN_NAME'];
$$field['COLUMN_NAME']->uitype= $fieldProperties['uitype'];
$$field['COLUMN_NAME']->column = $field['COLUMN_NAME']->name;
$$field['COLUMN_NAME']->columntype = $fieldProperties['columntype'];
$$field['COLUMN_NAME']->typeofdata = $fieldProperties['typeofdata'];
$$field['COLUMN_NAME']->defaultvalue = $field['COLUMN_DEFAULT'];
$block->addField($$field['COLUMN_NAME']);
if ($field['DATA_TYPE'] == 'enum'){
$$field['COLUMN_NAME']->setPicklistValues( getValuesFromEnum($field['COLUMN_TYPE']) );
}
var_dump(getValuesFromEnum($field['COLUMN_TYPE']) );
}
// Recommended common fields every Entity module should have (linked to core table)
$mfield1 = new Vtiger_Field();
$mfield1->name = 'assigned_user_id';
$mfield1->label = 'Assigned To';
$mfield1->table = 'vtiger_crmentity';
$mfield1->column = 'smownerid';
$mfield1->uitype = 53;
$mfield1->typeofdata = 'V~M';
$block->addField($mfield1);
$mfield2 = new Vtiger_Field();
$mfield2->name = 'CreatedTime';
$mfield2->label= 'Created Time';
$mfield2->table = 'vtiger_crmentity';
$mfield2->column = 'createdtime';
$mfield2->uitype = 70;
$mfield2->typeofdata = 'T~O';
$mfield2->displaytype= 2;
$block->addField($mfield2);
$mfield3 = new Vtiger_Field();
$mfield3->name = 'ModifiedTime';
$mfield3->label= 'Modified Time';
$mfield3->table = 'vtiger_crmentity';
$mfield3->column = 'modifiedtime';
$mfield3->uitype = 70;
$mfield3->typeofdata = 'T~O';
$mfield3->displaytype= 2;
$block->addField($mfield3);
// Filter Setup
$filter1 = new Vtiger_Filter();
$filter1->name = 'All';
$filter1->isdefault = true;
$moduleInstance->addFilter($filter1);
$filter1->addField($mfield1, 3);
$i = 1;
foreach ($filter_fields as $field){
if (isset($$field))
$filter1->addField($$field, $i);
$i++;
}
$moduleInstance->setEntityIdentifier($$identifier);
// Sharing Access Setup
$moduleInstance->setDefaultSharing();
// Webservice Setup
$moduleInstance->initWebservice();
mkdir('modules/'.$MODULENAME);
$content = "
<?php
include_once 'modules/Vtiger/CRMEntity.php';
class $MODULENAME extends Vtiger_CRMEntity {
var \$table_name = 'vtiger_<modulename>';
var \$table_index= '<modulename>id';
/**
* Mandatory table for supporting custom fields.
*/
var \$customFieldTable = Array('vtiger_<modulename>cf', '<modulename>id');
/**
* Mandatory for Saving, Include tables related to this module.
*/
var \$tab_name = Array('vtiger_crmentity', 'vtiger_<modulename>', 'vtiger_<modulename>cf');
/**
* Mandatory for Saving, Include tablename and tablekey columnname here.
*/
var \$tab_name_index = Array(
'vtiger_crmentity' => 'crmid',
'vtiger_<modulename>' => '<modulename>id',
'vtiger_<modulename>cf'=>'<modulename>id');
/**
* Mandatory for Listing (Related listview)
*/
var \$list_fields = Array (
/* Format: Field Label => Array(tablename, columnname) */
// tablename should not have prefix 'vtiger_'
'<entityfieldlabel>' => Array('<modulename>', '<entitycolumn>'),
'Assigned To' => Array('crmentity','smownerid')
);
var \$list_fields_name = Array (
/* Format: Field Label => fieldname */
'<entityfieldlabel>' => '<entityfieldname>',
'Assigned To' => 'assigned_user_id',
);
// Make the field link to detail view
var \$list_link_field = '<entityfieldname>';
// For Popup listview and UI type support
var \$search_fields = Array(
/* Format: Field Label => Array(tablename, columnname) */
// tablename should not have prefix 'vtiger_'
'<entityfieldlabel>' => Array('<modulename>', '<entitycolumn>'),
'Assigned To' => Array('vtiger_crmentity','assigned_user_id'),
);
var \$search_fields_name = Array (
/* Format: Field Label => fieldname */
'<entityfieldlabel>' => '<entityfieldname>',
'Assigned To' => 'assigned_user_id',
);
// For Popup window record selection
var \$popup_fields = Array ('<entityfieldname>');
// For Alphabetical search
var \$def_basicsearch_col = '<entityfieldname>';
// Column value to use on detail view record text display
var \$def_detailview_recname = '<entityfieldname>';
// Used when enabling/disabling the mandatory fields for the module.
// Refers to vtiger_field.fieldname values.
var \$mandatory_fields = Array('<entityfieldname>','assigned_user_id');
var \$default_order_by = '<entityfieldname>';
var \$default_sort_order='ASC';
/**
* Invoked when special actions are performed on the module.
* @param String Module name
* @param String Event Type
*/
function vtlib_handler(\$moduleName, \$eventType) {
global \$adb;
if(\$eventType == 'module.postinstall') {
// TODO Handle actions after this module is installed.
} else if(\$eventType == 'module.disabled') {
// TODO Handle actions before this module is being uninstalled.
} else if(\$eventType == 'module.preuninstall') {
// TODO Handle actions when this module is about to be deleted.
} else if(\$eventType == 'module.preupdate') {
// TODO Handle actions before this module is updated.
} else if(\$eventType == 'module.postupdate') {
// TODO Handle actions after this module is updated.
}
}
function save_module(){
include_once '/var/www/html/mySOAP/lib/nusoap.php' ;
\$url = '".$SOAP_url."';
\$client = new nusoap_client(\$url);
\$err = \$client->getError();
if (\$err) {
echo '<p><b>Error: ' . \$err . '</b></p>';
}
\$data = \$this->column_fields;
unset(\$data['assigned_user_id']);
unset(\$data['CreatedTime']);
unset(\$data['ModifiedTime']);
unset(\$data['createdtime']);
unset(\$data['modifiedtime']);
unset(\$data['modifiedby']);
\$data['".$id_field."'] = \$this->id;
\$return = \$client->call('create', array('entity_name'=>'".$table_name."','data' => \$data));
\$return = json_decode(\$return,1);
if (\$return['status'] == 'success')
return true;
else
die('Connection to goautodial failed');
}
}
";
$content = str_replace("<modulename>",strtolower($MODULENAME),$content);
$content = str_replace("<entityfieldname>",$identifier,$content);
$content = str_replace("<entityfieldlabel>",$identifier,$content);
//echo $content;
$fp = fopen('modules/'.$MODULENAME."/".$MODULENAME.".php","wb");
$status = fwrite($fp,$content);
var_dump($status);
fclose($fp);
echo "OK\n";
}