Newer
Older
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
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief API to manage batchs
*
* @file
* @author <dev@maarch.org>
* @date $date$
* @version $Revision$
*/
/**
* Exit the batch with a return code, message in the log and
* in the database if necessary
*
* @param int $returnCode code to exit (if > O error)
* @param string $message message to the log and the DB
* @return nothing exit the program
*/
function Bt_exitBatch($returnCode, $message='')
{
if (file_exists($GLOBALS['lckFile'])) {
unlink($GLOBALS['lckFile']);
}
if ($returnCode > 0) {
$GLOBALS['totalProcessedResources']--;
if ($GLOBALS['totalProcessedResources'] == -1) {
$GLOBALS['totalProcessedResources'] = 0;
}
if ($returnCode < 100) {
if (file_exists($GLOBALS['errorLckFile'])) {
unlink($GLOBALS['errorLckFile']);
}
$semaphore = fopen($GLOBALS['errorLckFile'], "a");
fwrite($semaphore, '1');
fclose($semaphore);
}
Bt_writeLog(['level' => 'ERROR', 'message' => $message]);
Bt_logInDataBase($GLOBALS['totalProcessedResources'], 1, $message.' (return code: '. $returnCode.')');
} elseif ($message <> '') {
Bt_writeLog(['level' => 'INFO', 'message' => $message]);
Bt_logInDataBase($GLOBALS['totalProcessedResources'], 0, $message.' (return code: '. $returnCode.')');
}
Bt_updateWorkBatch();
exit($returnCode);
}
/**
* Insert in the database the report of the batch
* @param long $totalProcessed total of resources processed in the batch
* @param long $totalErrors total of errors in the batch
* @param string $info message in db
*/
function Bt_logInDataBase($totalProcessed=0, $totalErrors=0, $info='')
{
\History\models\BatchHistoryModel::create([
'module_name' => $GLOBALS['batchName'],
'batch_id' => $GLOBALS['wb'],
'info' => substr(str_replace('\\', '\\\\', str_replace("'", "`", $info)), 0, 999),
'total_processed' => $totalProcessed,
'total_errors' => $totalErrors
]);
}
/**
* Insert in the database a line for history
*/
function Bt_history($aArgs = [])
{
$user = \User\models\UserModel::get(['select' => ['id'], 'orderBy' => ["user_id='superadmin' desc"], 'limit' => 1]);
\History\controllers\HistoryController::add([
'tableName' => $aArgs['table_name'],
'recordId' => $aArgs['record_id'],
'eventType' => $aArgs['event_type'],
'eventId' => $aArgs['event_id'],
'userId' => $user[0]['id'],
'info' => $aArgs['info']
]);
}
/**
* Get the batch if of the batch
*
* @return nothing
*/
function Bt_getWorkBatch()
{
$parameter = \Parameter\models\ParameterModel::getById(['select' => ['param_value_int'], 'id' => $GLOBALS['batchName']."_id"]);
if (!empty($parameter)) {
$GLOBALS['wb'] = $parameter['param_value_int'] + 1;
} else {
\Parameter\models\ParameterModel::create(['id' => $GLOBALS['batchName']."_id", 'param_value_int' => 1]);
$GLOBALS['wb'] = 1;
}
}
/**
* Update the database with the new batch id of the batch
*
* @return nothing
*/
function Bt_updateWorkBatch()
{
\Parameter\models\ParameterModel::update(['id' => $GLOBALS['batchName']."_id", 'param_value_int' => $GLOBALS['wb']]);
}
/**
* Include the file requested if exists
*
* @param string $file path of the file to include
* @return nothing
*/
function Bt_myInclude($file)
{
if (file_exists($file)) {
include_once($file);
} else {
throw new IncludeFileError($file);
}
}
function Bt_writeLog($args = [])
{
\SrcCore\controllers\LogsController::add([
'isTech' => true,
'moduleId' => $GLOBALS['batchName'],
'level' => $args['level'],
'tableName' => '',
'recordId' => $GLOBALS['batchName'],
'eventType' => $GLOBALS['batchName'],
'eventId' => $args['message']
]);
}
function Bt_getReply($args = [])
{
$refEncode = str_replace('.', '%2E', urlencode($args['reference']));
$curlResponse = \SrcCore\models\CurlModel::execSimple([
'url' => rtrim($GLOBALS['urlSAEService'], '/') . '/medona/message/reference?reference=' . $refEncode,
'method' => 'GET',
'cookie' => 'LAABS-AUTH=' . urlencode($GLOBALS['token']),
'headers' => [
'Accept: application/json',
'Content-Type: application/json',
'User-Agent: ' . $GLOBALS['userAgent']
]
]);
if (!empty($curlResponse['errors'])) {
return ['errors' => 'Error returned by the route /organization/organization/Search : ' . $curlResponse['errors']];
} elseif ($curlResponse['code'] != 200) {
return ['errors' => 'Error returned by the route /organization/organization/Search : ' . $curlResponse['response']['message']];
}
return ['response' => $curlResponse['response']];
}