/
cbupdaterWorker.php
236 lines (215 loc) · 8.05 KB
/
cbupdaterWorker.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
<?php
/*************************************************************************************************
* Copyright 2014 JPL TSolucio, S.L. -- This file is a part of TSOLUCIO coreBOS Customizations.
* Licensed under the vtiger CRM Public License Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You can redistribute it and/or modify it
* under the terms of the License. JPL TSolucio, S.L. reserves all rights not expressly
* granted by the License. coreBOS distributed by JPL TSolucio S.L. is distributed in
* the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Unless required by
* applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT ANY WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing
* permissions and limitations under the License. You may obtain a copy of the License
* at <http://corebos.org/documentation/doku.php?id=en:devel:vpl11>
*************************************************************************************************
* Module : cbupdater
* Version : 5.5.0
* Author : JPL TSolucio, S. L.
*************************************************************************************************/
// Turn on debugging level
$Vtiger_Utils_Log = true;
require_once 'include/utils/utils.php';
include_once('vtlib/Vtiger/Module.php');
require_once('vtlib/Vtiger/Package.php');
include_once('modules/cbupdater/cbupdater.php');
require 'modules/com_vtiger_workflow/VTEntityMethodManager.inc';
global $current_user,$adb,$app_strings;
set_time_limit(0);
ini_set('memory_limit','1024M');
if (empty($current_user) or $current_user->id != 1) {
$current_user = new Users();
$current_user->retrieveCurrentUserInfoFromFile(1); // admin
}
if (empty($current_language)) {
if(isset($_SESSION['authenticated_user_language']) && $_SESSION['authenticated_user_language'] != '') {
$current_language = $_SESSION['authenticated_user_language'];
} else {
if(!empty($current_user->language)) {
$current_language = $current_user->language;
} else {
$current_language = $default_language;
}
}
}
if (empty($app_strings)) $app_strings = return_application_language($current_language);
class cbupdaterWorker {
var $cbupdid;
var $cbupd_no;
var $author = '';
var $filename;
var $classname;
var $execstate = false;
var $systemupdate = false;
var $execdate;
var $updError = false;
var $query_count=0;
var $success_query_count=0;
var $failure_query_count=0;
var $success_query_array=array();
var $failure_query_array=array();
function __construct() {
global $adb,$log,$current_user;
echo "<table width=80% align=center border=1>";
$reflector = new ReflectionClass(get_class($this));
$fname = basename($reflector->getFileName(),'.php');
$cburs = $adb->pquery('select * from vtiger_cbupdater where filename=? and classname=?',
array($fname,get_class($this)));
if ($cburs and $adb->num_rows($cburs)>0) { // it exists, we load it
$cbu = $adb->fetch_array($cburs);
$this->cbupdid = $cbu['cbupdaterid'];
$this->cbupd_no = $cbu['cbupd_no'];
$this->author = $cbu['author'];
$this->filename = $cbu['filename'];
$this->classname = $cbu['classname'];
$this->execstate = $cbu['execstate'];
$this->systemupdate = ($cbu['systemupdate']=='1' ? true : false);
$this->execdate = $cbu['execdate'];
$this->updError = false;
} else { // it doesn't exist, we fail because it MUST exist
$this->sendError();
}
}
function applyChange() {
if ($this->hasError()) $this->sendError();
if ($this->isApplied()) {
$this->sendMsg('Changeset '.get_class($this).' already applied!');
} else {
// do your magic here
$this->sendMsg('Changeset '.get_class($this).' applied!');
$this->markApplied();
}
$this->finishExecution();
}
function undoChange() {
if ($this->hasError()) $this->sendError();
if ($this->isSystemUpdate()) {
$this->sendMsg('Changeset '.get_class($this).' is a system update, it cannot be undone!');
} else {
if ($this->isApplied()) {
// undo your magic here
$this->sendMsg('Changeset '.get_class($this).' undone!');
$this->markUndone();
} else {
$this->sendMsg('Changeset '.get_class($this).' not applied!');
}
}
$this->finishExecution();
}
function isApplied() {
return ($this->execstate=='Executed');
}
function isSystemUpdate() {
return $this->systemupdate;
}
function hasError() {
return ($this->updError or empty($this->cbupdid));
}
function markApplied() {
if ($this->hasError()) $this->sendError();
global $adb,$log;
$adb->pquery('update vtiger_cbupdater set execstate=?,execdate=CURDATE() where cbupdaterid=?', array('Executed',$this->cbupdid));
$this->execstate = 'Executed';
}
function markUndone() {
if ($this->hasError()) $this->sendError();
global $adb,$log;
$adb->pquery('update vtiger_cbupdater set execstate=?,execdate=NULL where cbupdaterid=?', array('Pending',$this->cbupdid));
$this->execstate = 'Pending';
}
function ExecuteQuery($query,$params=array()) {
global $adb,$log;
$status = $adb->pquery($query,$params);
$this->query_count++;
if(is_object($status)) {
echo '
<tr width="100%">
<td width="10%">'.get_class($status).'</td>
<td width="10%"><span style="color:green"> S </span></td>
<td width="80%">'.$query.'</td>
</tr>';
$success_query_array[$this->success_query_count++] = $query;
$log->debug("Query Success ==> $query");
} else {
echo '
<tr width="100%">
<td width="25%">'.$status.'</td>
<td width="5%"><span style="color:red"> F </span></td>
<td width="70%">'.$query.'</td>
</tr>';
$this->failure_query_array[$this->failure_query_count++] = $query;
$log->debug("Query Failed ==> $query \n Error is ==> [".$adb->database->ErrorNo()."]".$adb->database->ErrorMsg());
}
}
function deleteWorkflow($wfid) {
$this->ExecuteQuery("DELETE FROM com_vtiger_workflowtasks WHERE workflow_id=?",array($wfid));
$this->ExecuteQuery("DELETE FROM com_vtiger_workflows WHERE workflow_id=?", array($wfid));
}
function installManifestModule($module) {
$package = new Vtiger_Package();
ob_start();
$rdo = $package->importManifest("modules/$module/manifest.xml");
$out = ob_get_contents();
ob_end_clean();
$this->sendMsg($out);
if ($rdo) $this->sendMsg("$module installed!");
else $this->sendMsgError("ERROR installing $module!");
}
function isModuleInstalled($module) {
global $adb;
$tabrs = $adb->pquery('select count(*) from vtiger_tab where name=?',array($module));
return ($tabrs and $adb->query_result($tabrs, 0,0)==1);
}
function sendMsg($msg) {
echo '<tr width="100%"><td colspan=3>'.$msg.'</td></tr>';
}
function sendMsgError($msg) {
echo '<tr width="100%"><td colspan=3><span style="color:red">'.$msg.'</span></td></tr>';
}
function sendError() {
$this->updError = true;
echo '<tr width="100%"><td colspan=3<span style="color:red">ERROR: Class called without update record in application!!</span></td></tr></table>';
die();
}
function finishExecution() {
echo '</table>';
if (count($this->failure_query_array)>0) {
echo <<<EOT
<br /><br />
<b style="color:#FF0000">Failed Queries Log</b>
<div id="failedLog" style="border:1px solid #666666;width:90%;position:relative;height:200px;overflow:auto;left:5%;top:10px;">
EOT;
foreach($this->failure_query_array as $failed_query)
echo '<br><span style="color:red">'.$failed_query.'</span>';
echo '</div>';
}
echo <<<EOT
<br /><br />
<table width="35%" border="0" cellpadding="5" cellspacing="0" align="center" class="small">
<tr>
<td width="75%" align="right" nowrap>Total Number of queries executed : </td>
<td width="25%" align="left"><b>{$this->query_count}</b></td>
</tr>
<tr>
<td align="right">Queries Successed : </td>
<td align="left"><b style="color:#006600;">{$this->success_query_count}</b></td>
</tr>
<tr>
<td align="right">Queries Failed : </td>
<td align="left"><b style="color:#FF0000;">{$this->failure_query_count}</b></td>
</tr>
</table>
EOT;
}
}
?>