/
Import.php
executable file
·487 lines (444 loc) · 12 KB
/
Import.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
/*
* CATS
* Import Library
*
* Copyright (C) 2005 - 2007 Cognizo Technologies, Inc.
*
*
* The contents of this file are subject to the CATS Public License
* Version 1.1a (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.catsone.com/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is "CATS Standard Edition".
*
* The Initial Developer of the Original Code is Cognizo Technologies, Inc.
* Portions created by the Initial Developer are Copyright (C) 2005 - 2007
* (or from the year in which this file was created to the year 2007) by
* Cognizo Technologies, Inc. All Rights Reserved.
*
*
* $Id: Import.php 3785 2007-12-03 21:59:23Z brian $
*/
class Import
{
private $_db;
private $_siteID;
public function __construct($siteID)
{
$this->_siteID = $siteID;
$this->_db = DatabaseConnection::getInstance();
}
/**
* Adds a record for an import group into the database.
*
* @param table import was affecting (contacts, companies, etc)
* @return importID
*/
public function add($table)
{
$sql = sprintf(
"INSERT INTO import (
module_name,
reverted,
site_id,
date_created
)
VALUES (
'%s',
0,
%s,
NOW()
)",
$table,
$this->_siteID
);
$queryResult = $this->_db->query($sql);
if (!$queryResult)
{
return -1;
}
return $this->_db->getLastInsertID();
}
/**
* Updates an import record with a HTML string containing all errors
* generated by the import for later viewing.
*
* @param importID
* @param error HTML string
* @param number of records added by import
* @return query response
*/
public function updateErrors($importID, $importErrors, $addedLines)
{
$sql = sprintf(
"UPDATE
import
SET
import_errors = %s,
added_lines = %s
WHERE
import_id = %s
AND
site_id = %s",
$this->_db->makeQueryStringOrNULL($importErrors),
$addedLines,
$importID,
$this->_siteID
);
$queryResult = $this->_db->query($sql);
return $queryResult;
}
/**
* Removes an import record from the database, preventing any import reverting.
* Effectively finalizes an import.
*
* @param importID
* @return void
*/
public function delete($importID)
{
$sql = sprintf(
"DELETE FROM
import
WHERE
import_id = %s
AND
site_id = %s",
$this->_db->makeQueryString($importID),
$this->_siteID
);
$queryResult = $this->_db->query($sql);
if (!$queryResult)
{
return false;
}
return true;
}
/**
* Returns an import record (probably for viewing the associated errors).
*
* @param importID
* @return record
*/
public function get($importID)
{
$sql = sprintf(
"SELECT
import.module_name AS moduleName,
import.import_id AS importID,
import.import_errors AS importErrors
FROM
import
WHERE
import.import_id = %s
AND
site_id = %s",
$importID,
$this->_siteID
);
$rs = $this->_db->getAssoc($sql);
return $rs;
}
/**
* Returns all imports for a site.
*
* @param importID
* @return array of records
*/
public function getAll()
{
$sql = sprintf(
"SELECT
import.module_name AS moduleName,
import.import_id AS importID,
import.added_lines AS addedLines,
import.site_id AS siteID,
import.import_errors AS importErrors,
DATE_FORMAT(
import.date_created, '%%m-%%d-%%y (%%h:%%i %%p)'
) AS dateCreated
FROM
import
WHERE
import.site_id = %s
AND
TO_DAYS(date_created) > TO_DAYS(DATE_SUB(NOW(), INTERVAL 7 DAY))",
$this->_siteID
);
$rs = $this->_db->getAllAssoc($sql);
return $rs;
}
/**
* Reverts a previous import.
*
* @param string table name - company / contact / candidate
* @param integer importID
* @return void
*/
public function revert($tableName, $importID)
{
$sql = sprintf(
"DELETE FROM
extra_field_settings
WHERE
import_id = %s
AND
site_id = %s",
$importID,
$this->_siteID
);
$this->_db->query($sql);
$sql = sprintf(
"DELETE FROM
%s
WHERE
import_id = %s
AND
site_id = %s",
$this->_db->escapeString($tableName),
$importID,
$this->_siteID
);
$this->_db->query($sql);
$sql = sprintf(
"DELETE FROM
extra_field
WHERE
import_id = %s
AND
site_id = %s",
$importID,
$this->_siteID
);
$this->_db->query($sql);
if ($tableName == 'contact')
{
$sql = sprintf(
"DELETE FROM
company
WHERE
import_id = %s
AND
site_id = %s",
$importID,
$this->_siteID
);
$this->_db->query($sql);
}
}
/**
* Adds a setting to the foreign_settings table, but avoids a duplicate entry.
*
* @param foreign table
* @param field name
* @param importID
* @return void
*/
public function addForeignSettingUnique($type, $field, $importID)
{
$sql = sprintf(
"SELECT
extra_field_settings.field_name AS fieldName
FROM
extra_field_settings
WHERE
extra_field_settings.site_id = %s
AND
extra_field_settings.field_name = %s
AND
extra_field_settings.data_item_type = %s",
$this->_siteID,
$this->_db->makeQueryStringOrNULL($field),
$type
);
$rs = $this->_db->getAssoc($sql);
if (!isset($rs['fieldName']))
{
$sql = sprintf(
"INSERT INTO extra_field_settings (
field_name,
site_id,
date_created,
import_id,
data_item_type
)
VALUES (
%s,
%s,
NOW(),
%s,
%s
)",
$this->_db->makeQueryStringOrNULL($field),
$this->_siteID,
$importID,
$type
);
$rs = $this->_db->query($sql);
}
}
/**
* Adds a set of records to the foreign entry table for an
* associated record in a normal data table.
*
* FIXME: Parameter documentation incorrect; types must be specified after @param.
*
* @param string foreign table
* @param array fields => values
* @param integer associated index on non foreign table
* @param integer importID
* @return integer getLastInsertID
*/
public function addForeign($type, $data, $assocID, $importID)
{
$ar = array();
$dataS = '';
foreach ($data AS $field => $value)
{
if ($value != '')
{
$ar[] = '(' . $assocID . ', '
. $this->_db->makeQueryStringOrNULL($field) . ', '
. $this->_db->makeQueryStringOrNULL($value) . ', '
. $importID . ',' .$this->_siteID . ', ' . $type . ')';
}
}
$dataS = implode(',', $ar);
if (!empty($dataS))
{
$sql = sprintf(
"INSERT INTO extra_field (
data_item_id,
field_name,
value,
import_id,
site_id,
data_item_type
)
VALUES %s",
$dataS
);
$queryResult = $this->_db->query($sql);
if (!$queryResult)
{
return -1;
}
return $this->_db->getLastInsertID();
}
}
}
class JobOrdersImport
{
private $_db;
private $_siteID;
public function __construct($siteID)
{
$this->_siteID = $siteID;
$this->_db = DatabaseConnection::getInstance();
}
/**
* Adds a record to the joborder table.
*
* @param array (field => value)
* @param userID
* @param importID
* @return joborderID
*/
public function add($dataNamed, $userID, $importID)
{
$sql = sprintf(
"SELECT
company.company_id AS companyID
FROM
company
WHERE
site_id = %s
AND
name = %s",
$this->_siteID,
$this->_db->makeQueryString($dataNamed['company'])
);
$rs = $this->_db->getAllAssoc($sql);
if(!$rs)
{
$companyID = -1;
}
else
{
$companyID = $rs[0]['companyID'];
}
unset($dataNamed['company']);
$dataColumns = array();
$data = array();
foreach ($dataNamed AS $dataColumn => $d)
{
$dataColumns[] = $dataColumn;
if(in_array($dataColumn, array("is_hot", "openings", "public")))
{
$data[] = $this->_db->makeQueryInteger($d);
}
else
{
$data[] = $this->_db->makeQueryStringOrNULL($d);
}
}
$sql = sprintf(
"INSERT INTO joborder (
%s,
recruiter,
entered_by,
owner,
site_id,
status,
date_created,
date_modified,
openings_available,
company_id,
contact_id,
company_department_id,
import_id
)
VALUES (
%s,
%s,
%s,
%s,
%s,
%s,
NOW(),
NOW(),
%s,
%s,
%s,
%s,
%s
)",
implode(",\n", $dataColumns),
implode(",\n", $data),
$userID,
$userID,
$userID,
$this->_siteID,
$this->_db->makeQueryString('Draft'),
$this->_db->makeQueryInteger($dataNamed['openings']),
$this->_db->makeQueryInteger($companyID),
$this->_db->makeQueryInteger(-1),
$this->_db->makeQueryInteger(0),
$importID
);
$queryResult = $this->_db->query($sql);
if (!$queryResult)
{
return -1;
}
return $this->_db->getLastInsertID();
}
}
?>