/
inc_action.php
707 lines (548 loc) · 27.2 KB
/
inc_action.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
<?php
class eiseAction {
public static $ts = array('ETD', 'ATD', 'ETA', 'ATA');
public function __construct($item, $arrAct, $options = array()){
$this->item = $item;
$this->oSQL = $item->oSQL;
$this->intra = $item->intra;
$this->entID = $item->conf['entID'];
$this->flagIsManagement = ( count(array_intersect($this->intra->arrUsrData['roleIDs'], explode(',', $this->item->conf['entManagementRoles']) ) ) > 0 );
$nd = $arrAct;
if(!$arrAct['actID'] && !$arrAct['aclGUID'])
$arrAct['actID'] = 2;
$types = array();
foreach(self::$ts as $_ts)
$types['acl'.$_ts] = 'datetime';
$types = array_merge($types, $this->item->conf['attr_types']);
if(!$options['flagDoNotRefresh'])
$this->item->getAllData(array('Master', 'Text','ACL'));
if($arrAct['aclGUID']){
$this->arrAction = $this->item->item['ACL'][$arrAct['aclGUID']];
if(!$this->arrAction){
throw new Exception("Action {$arrAct['aclGUID']}/{$arrAct['actID']} not found", 1);
}
$toTrace = array();
foreach($nd as $field=>$value){
if(strpos($field, '_'.$arrAct['aclGUID'])===False)
continue;
$toTrace[str_replace('_'.$arrAct['aclGUID'], '', $field)] = $value;
}
$this->conf = $item->conf['ACT'][$this->arrAction['aclActionID']];
$this->arrAction = array_merge(
$this->conf,
$this->arrAction,
$this->intra->arrPHP2SQL($toTrace, $types),
array('aclToDo'=> $nd['aclToDo'])
);
$this->arrAction = array_merge($this->arrAction, $this->getTraceData());
} else {
$this->conf = $item->conf['ACT'][(string)$arrAct['actID']];
$nd_SQL = $this->intra->arrPHP2SQL($nd, $types);
$this->arrAction = array_merge($this->conf, $nd_SQL);
}
switch($this->conf['actID']){
case 0:
case 3:
$this->arrAction['aclOldStatusID'] = $this->arrAction['actOldStatusID'][0];
$this->arrAction['aclNewStatusID'] = $this->arrAction['actNewStatusID'][0];
$this->arrAction['RLA'] = ($this->intra->arrUsrData['FlagWrite'] || $this->intra->arrUsrData['FlagDelete'] ? array($item->conf['RoleDefault']) : array());
break;
case 2:
$this->arrAction['aclOldStatusID'] = $item->staID;
$this->arrAction['aclNewStatusID'] = $item->staID;
$this->arrAction['RLA'] = ($this->intra->arrUsrData['FlagWrite'] || $this->intra->arrUsrData['FlagUpdate'] ? array($item->conf['RoleDefault']) : array());
break;
case 4:
if(!$this->flagIsManagement){
throw new Exception("Only management ({$this->item->conf['entManagementRoles']}) can run the Superaction");
}
$this->arrAction['aclOldStatusID'] = $item->staID;
$this->arrAction['aclNewStatusID'] = $arrAct['aclNewStatusID'];
$this->arrAction['aclComments'] = __("Status '%s' set by %s@%s:"
, $this->item->oSQL->d("SELECT staTitle{$this->intra->local} FROM stbl_status WHERE staID=".$this->item->oSQL->e($arrAct['aclNewStatusID']))
, $this->intra->arrUsrData['usrID']
, $this->intra->datetimeSQL2PHP(date('Y-m-d H:i:s')))
."<br>\n"
.$arrAct['aclComments'];
break;
default:
if($this->arrAction['actNewStatusID'][0]===null){ // if we move to the same status
$this->arrAction['aclNewStatusID'] = $item->staID;
// die('<pre>'.var_export($this->arrAction, true));
}
break;
}
if(!$this->conf && !$arrAct['aclGUID'])
throw new Exception("Action is not defined properly. Debug info: ".var_dump($arrAct, true));
}
public function execute(){
// proceed with the action
if ($this->arrAction["actFlagAutocomplete"] && !$this->arrAction["aclGUID"]){
$this->add();
$this->validate();
$this->finish();
} else {
if ($this->arrAction["aclGUID"]==""){
$this->add();
} else {
switch ($this->arrAction["aclToDo"]) {
case "finish":
$this->validate();
$this->finish();
break;
case "start":
$this->start();
break;
case "cancel":
$this->cancel();
break;
}
}
}
$aclGUID = $this->arrAction["aclGUID"];
return $aclGUID;
}
public function update($nd = null){
$aToUpdate_old = (array)@json_decode($this->arrAction['aclItemTraced'], true);
$aToUpdate = array();
foreach(array_merge(array_keys((array)$this->conf['aatFlagToTrack']), array('aclATA','aclATD','aclETA','aclETD')) as $atrID){
$nd_key = $atrID.'_'.$this->arrAction['aclGUID'];
if(isset($this->arrAction[$nd_key])){
$aToUpdate[$atrID] = $this->arrAction[$nd_key];
}
if(isset($nd[$nd_key])){
$aToUpdate[$atrID] = $nd[$nd_key];
} elseif (isset($nd[$atrID])) {
$aToUpdate[$atrID] = $nd[$atrID];
}
if (in_array($this->item->conf['ATR'][$atrID]['atrType'], array('boolean', 'checkbox'))) { // booleans are not transferrable via HTTP
$aToUpdate[$atrID] = (isset($aToUpdate[$atrID]) ? $aToUpdate[$atrID] : '0');
}
}
$aToUpdate = array_merge($aToUpdate_old, $aToUpdate);
$timestamps = $this->getTimeStamps($aToUpdate, 'ACL');
$aclComments = (isset($this->arrAction['aclComments_'.$this->arrAction['aclGUID']])
? $this->arrAction['aclComments_'.$this->arrAction['aclGUID']]
: (isset($nd['aclComments_'.$this->arrAction['aclGUID']])
? $nd['aclComments_'.$this->arrAction['aclGUID']]
: (isset($nd['aclComments'])
? $nd['aclComments']
: null)
)
);
$traced = $this->intra->arrPHP2SQL($aToUpdate, $this->item->conf['attr_types']);
$sqlACL = "UPDATE stbl_action_log SET # eiseAction::update() {$this->arrAction['actTitle']}
aclEditDate=NOW(), aclEditBy='{$this->intra->usrID}'
".(count($aToUpdate) ? ", aclItemTraced=".$this->oSQL->e(json_encode($traced)) : '')."
{$timestamps}
, aclComments = ".($aclComments ? $this->oSQL->e($aclComments) : 'aclComments')."
WHERE aclGUID='{$this->arrAction['aclGUID']}'";
$this->oSQL->q($sqlACL);
$sqlSTL = "UPDATE stbl_status_log LEFT OUTER JOIN stbl_action_log ON stlEntityItemID=aclEntityItemID AND aclGUID=stlArrivalActionID # eiseAction::update() {$this->arrAction['actTitle']}
SET stlATA=aclATA
, stlEditDate=NOW()
, stlEditBy = '{$this->item->intra->usrID}'
WHERE aclEntityItemID='{$this->item->id}' AND stlArrivalActionID='{$this->arrAction['aclGUID']}'";
$this->oSQL->q($sqlSTL);
}
public function add(){
// 0. Trigger beforeActionPlan hook
$this->item->beforeActionPlan($this->arrAction['actID'], $this->arrAction['aclOldStatusID'], $this->arrAction['aclNewStatusID']);
// 1. obtaining aclGUID
$this->arrAction["aclGUID"] = ($this->arrAction["aclGUID"] ? $this->arrAction["aclGUID"] : $this->oSQL->d("SELECT UUID() # add action {$this->arrAction['actTitle']}"));
$item_before = self::itemCleanUp($this->item->item_before, $this->item->conf['prefix']);
$aToTrace = array();
foreach((array)$this->conf['aatFlagToTrack'] as $field=>$props){
$aToTrace[$field] = (isset($this->item->item[$field]) && !$props['aatFlagEmptyOnInsert']
? $this->item->item[$field]
: null
);
$aToTrace[$field] = ($this->arrAction[$field]!==null
? $this->arrAction[$field]
: $aToTrace[$field]
);
}
$timestamps = $this->getTimeStamps($aToTrace, 'ACL');
// 2. insert new ACL
$sqlInsACL = "INSERT INTO stbl_action_log SET # add action {$this->arrAction['actTitle']}
aclGUID = '{$this->arrAction["aclGUID"]}'
, aclActionID = ".(int)$this->arrAction["actID"]."
, aclEntityItemID = '".$this->item->id."'
, aclOldStatusID = ".($this->arrAction['aclOldStatusID']===null ? "NULL" : "'".(string)$this->arrAction['aclOldStatusID']."'")."
, aclNewStatusID = ".($this->arrAction['aclNewStatusID']===null ? "NULL" : "'".(string)$this->arrAction['aclNewStatusID']."'")."
, aclActionPhase = 0
{$timestamps}
, aclItemBefore = ".$this->oSQL->e(json_encode($item_before))."
, aclItemDiff = NULL
, aclItemAfter = NULL
, aclItemTraced = ".$this->oSQL->e(json_encode($aToTrace))."
, aclComments = ".($this->arrAction['aclComments'] ? $this->oSQL->e($this->arrAction['aclComments']) : 'NULL')."
, aclInsertBy = '{$this->intra->usrID}', aclInsertDate = NOW(), aclEditBy='{$this->intra->usrID}', aclEditDate=NOW()";
$this->oSQL->q($sqlInsACL);
$this->arrAction = array_merge($this->arrAction, $this->oSQL->f("SELECT * FROM stbl_action_log WHERE aclGUID='{$this->arrAction['aclGUID']}'"));
// 3. Trigger onActionPlan hook
$this->item->onActionPlan($this->arrAction['actID'], $this->arrAction['aclOldStatusID'], $this->arrAction['aclNewStatusID']);
$this->arrAction['aclActionPhase'] = 0;
return $this->arrAction["aclGUID"];
}
function start(){
$this->arrAction['aclOldStatusID'] = $this->item->staID;
if (!in_array($this->item->staID, $this->conf['actOldStatusID']))
throw new Exception("Action {$this->conf["actTitle"]} cannot be started for {$this->id} because of its status ({$this->item->staID})");
if (($oldStatusID!==$newStatusID
&& $newStatusID!==""
)
|| $this->conf["actFlagInterruptStatusStay"]){
$this->onStatusDeparture($this->arrAction['aclOldStatusID']);
}
$this->item->onActionStart($this->arrAction['actID'], $this->arrAction['aclOldStatusID'], $this->arrAction['aclNewStatusID']);
$item_before = $this->item->item_before;
foreach ($item_before as $key => $value) {
if(is_array($value))
unset($item_before[$key]);
}
$sqlUpdACL = "UPDATE stbl_action_log SET
aclActionPhase=1
, aclOldStatusID = '{$this->item->staID}'
, aclItemBefore = ".$this->oSQL->e( json_encode( self::itemCleanUp($this->item->item_before, $this->item->conf['prefix']) ) )."
, aclStartBy='{$this->intra->usrID}', aclStartDate=NOW()
, aclEditBy='{$this->intra->usrID}', aclEditDate=NOW()
WHERE aclGUID='{$this->arrAction['aclGUID']}'";
$this->oSQL->do_query($sqlUpdACL);
$sqlUpdEntTable = "UPDATE {$this->item->conf["table"]} SET
{$this->item->conf['prefix']}ActionLogID='{$this->arrAction['aclGUID']}'
, {$this->item->conf['prefix']}EditBy='{$this->intra->usrID}', {$this->item->conf['prefix']}EditDate=NOW()
WHERE {$this->item->conf['PK']}='{$entItemID}'";
$this->oSQL->do_query($sqlUpdEntTable);
}
public function validate(){
$aclOldStatusID = (array_key_exists("aclOldStatusID", $this->arrAction)
? $this->arrAction["aclOldStatusID"]
: $this->item->item["{$this->item->conf['prefix']}StatusID"]
);
if($this->arrAction['actID']<=4){
switch( $this->conf['actID'] ){
case 1:
if($aclOldStatusID>0)
throw new Exception('Item is already created');
break;
case 2:
if(!$this->item->conf['STA'][$aclOldStatusID]['staFlagCanUpdate'] && !$this->flagFullEdit)
throw new Exception('Update is not allowed');
break;
case 3:
if(!$this->item->conf['STA'][$aclOldStatusID]['staFlagCanDelete'])
throw new Exception('Delete is not allowed');
break;
default:
break;
}
} else {
if( !$this->flagIsManagement )
if(!in_array($this->item->item["{$this->item->conf['prefix']}StatusID"], $this->conf['actOldStatusID'])){
throw new Exception($this->intra->translate("%s %s: Action \"%s\" could not run in status \"%s\""
, $this->item->conf['entTitle'.$this->intra->local]
, $this->item->id
, ( $this->conf['actTitle'.$this->intra->local] ? $this->conf['actTitle'.$this->intra->local] : $this->conf['actTitle'])
, $this->item->conf['STA'][$this->item->item["{$this->item->conf['prefix']}StatusID"]]['staTitle'.$this->intra->local]));
}
if($this->conf['actNewStatusID'][0] && $this->arrAction["aclNewStatusID"] != $this->conf['actNewStatusID'][0]){
throw new Exception($this->intra->translate("%s %s: Action \"%s\" could not run for destination status \"%s\""
, $this->item->conf['entTitle'.$this->intra->local]
, $this->item->id
, ( $this->conf['actTitle'.$this->intra->local] ? $this->conf['actTitle'.$this->intra->local] : $this->conf['actTitle'])
, $this->item->conf['STA'][(string)$this->arrAction["aclNewStatusID"]]['staTitle'.$this->intra->local]));
}
}
// mandatory items check
$aMissingFields = array();
foreach ((array)$this->arrAction['aatFlagMandatory'] as $atrID => $props) {
$v = ($this->arrAction[$atrID]
? $this->arrAction[$atrID]
: ($this->item->item[$atrID])
);
if(!$v || (is_numeric($v) && (double)$v===0.0 )){
$aMissingFields[] = $this->item->conf['ATR'][$atrID]['atrTitle'.$this->intra->local]." ({$atrID})";
}
}
if(count($aMissingFields)){
throw new Exception($this->intra->translate("Some fields are missing:\n%s", implode(",\n\t", $aMissingFields)));
}
}
public function finish(){
if (!$this->arrAction["aclActionPhase"]){
$this->item->onActionStart($this->arrAction['actID'], $this->arrAction['aclOldStatusID'], $this->arrAction['aclNewStatusID']);
}
$this->checkTimeLine();
$this->checkMandatoryFields();
$this->checkPermissions();
$item_before = self::itemCleanUp($this->item->item_before, $this->item->conf['prefix']);
$timestamps = $this->getTimeStamps();
$aTraced = $this->getTraceData();
if ($this->conf["actID"]!="2") {
$tracedFields = $this->intra->getSQLFields($this->item->table, $aTraced);
$userstamps = $this->getUserStamps();
$sqlMaster = "UPDATE {$this->item->conf['table']} LEFT OUTER JOIN stbl_action_log ON aclGUID='{$this->arrAction['aclGUID']}' SET
{$this->item->conf['prefix']}ActionLogID=aclGUID
{$tracedFields}
{$userstamps}
{$timestamps}
, {$this->item->conf['prefix']}EditBy='{$this->intra->usrID}', {$this->item->conf['prefix']}EditDate=NOW()
WHERE ".$this->item->getSQLWhere();
$this->oSQL->q($sqlMaster);
}
$this->item->getAllData(array('Master'));
$item_after = self::itemCleanUp($this->item->item, $this->item->conf['prefix']);
$item_diff = array_diff($item_before, $item_after);
// update started action as completed
$sqlUpdACL = "UPDATE stbl_action_log SET
aclActionPhase = 2
, aclItemAfter = ".$this->oSQL->e(json_encode($item_after))."
, aclItemDiff = ".$this->oSQL->e(json_encode($item_diff))."
".($this->conf["actID"]!="2" && count($aTraced)>0
? ", aclItemTraced=".$this->oSQL->e(json_encode($aTraced))
: ', aclItemTraced=NULL')."
".($this->arrAction['aclComments']
? ", aclComments=".$this->oSQL->e($this->arrAction['aclComments'])
: '')."
, aclStartBy=IFNULL(aclStartBy, '{$this->intra->usrID}'), aclStartDate=IFNULL(aclStartDate,NOW())
, aclFinishBy=IFNULL(aclFinishBy, '{$this->intra->usrID}'), aclFinishDate=IFNULL(aclFinishDate, NOW())
, aclEditDate=NOW(), aclEditBy='{$this->intra->usrID}'
WHERE aclGUID='".$this->arrAction["aclGUID"]."'";
$this->oSQL->q($sqlUpdACL);
// if status is changed or action requires status stay interruption, we insert status log entry and update master table
if (($this->arrAction["aclOldStatusID"]!==$this->arrAction["aclNewStatusID"]
&& (string)$this->arrAction["aclNewStatusID"]!=''
)
|| $this->conf["actFlagInterruptStatusStay"]){
$sql = Array();
$stlGUID = $this->oSQL->get_data($this->oSQL->do_query("SELECT UUID()"));
$sql[] = "UPDATE stbl_status_log SET
stlATD=(SELECT IFNULL(aclATD, aclATA) FROM stbl_action_log WHERE aclGUID='{$this->arrAction["aclGUID"]}')
, stlDepartureActionID='{$this->arrAction["aclGUID"]}'
, stlEditBy='{$this->intra->usrID}', stlEditDate=NOW()
WHERE stlEntityItemID='{$this->item->id}' AND stlATD IS NULL";
$sql[] = "INSERT INTO stbl_status_log (
stlGUID
, stlEntityID
, stlEntityItemID
, stlStatusID
, stlArrivalActionID
, stlDepartureActionID
, stlTitle
, stlTitleLocal
, stlATA
, stlATD
, stlInsertBy, stlInsertDate, stlEditBy, stlEditDate
) SELECT
'$stlGUID' as stlGUID
, '{$this->item->entID}' as stlEntityID
, aclEntityItemID
, aclNewStatusID as stlStatusID
, aclGUID as stlArrivalActionID
, NULL as stlDepartureActionID
, staTitle as stlTitle
, staTitleLocal as stlTitleLocal
, aclATA as stlATA
, NULL as stlATD
, '{$this->intra->usrID}' as stlInsertBy, NOW() as stlInsertDate, '{$this->intra->usrID}' as stlEditBy, NOW() as stlEditDate
FROM stbl_action_log
INNER JOIN stbl_action ON actID=aclActionID
INNER JOIN stbl_status ON aclNewStatusID=staID AND staEntityID='{$this->item->conf['entID']}'
WHERE aclGUID='{$this->arrAction['aclGUID']}'";
$arrSAT = $this->conf['STA'][$this->arrAction['aclNewStatusID']]['satFlagTrackOnArrival'];
if (@count($arrSAT)>0){
}
// after action is done, we update entity table with last status action log id
$sql[] = "UPDATE {$this->item->conf["entTable"]} SET
{$this->item->conf['prefix']}ActionLogID='{$this->arrAction["aclGUID"]}'
, {$this->item->conf['prefix']}StatusActionLogID='{$this->arrAction["aclGUID"]}'
, {$this->item->conf['prefix']}StatusID=".(int)$this->arrAction["aclNewStatusID"]."
, {$this->item->conf['prefix']}EditBy='{$this->intra->usrID}', {$this->item->conf['prefix']}EditDate=NOW()
WHERE {$this->item->table['PK'][0]}='{$this->item->id}'";
for($i=0;$i<count($sql);$i++){
$this->oSQL->do_query($sql[$i]);
}
$this->item->onStatusArrival($this->arrAction['aclNewStatusID']);
}
$this->item->onActionFinish($this->arrAction['actID'], $this->arrAction['aclOldStatusID'], $this->arrAction['aclNewStatusID']);
}
function cancel(){
$oSQL = $this->oSQL;
$entID = $this->item->conf["entID"];
$entItemID = $this->item->id;
$aclGUID = $this->arrAction["aclGUID"];
$this->item->onActionCancel($this->arrAction['aclActionID'], $this->arrAction['aclOldStatusID'], $this->arrAction['aclNewStatusID']);
if($this->arrAction['aclActionPhase']==0){
$oSQL->q("DELETE FROM stbl_action_log WHERE aclGUID='{$this->arrAction['aclGUID']}'");
} else {
$oSQL->q("UPDATE stbl_action_log SET
aclActionPhase=3
, aclCancelBy='{$this->intra->usrID}'
, aclCancelDate=NOW()
WHERE aclGUID='{$this->arrAction['aclGUID']}'");
}
}
function checkTimeLine(){
$oSQL = $this->oSQL;
$entID = $this->item->conf["entID"];
$entItemID = $this->item->id;
$aclGUID = $this->arrAction["aclGUID"];
if ($this->arrAction["actID"]=="2")
return true;
$sqlMaxATA = "SELECT
CASE WHEN DATEDIFF(
(SELECT aclATA FROM stbl_action_log WHERE aclGUID='{$aclGUID}')
, MAX(aclATA)
) < 0 THEN 0 ELSE 1 END as ATAnotLessThanPrevious
FROM stbl_action_log
WHERE aclEntityItemID='{$entItemID}' AND aclActionPhase=2 AND aclActionID>2";
if (!$oSQL->get_data($oSQL->do_query($sqlMaxATA))) {
$ata = $oSQL->d("SELECT aclATA FROM stbl_action_log WHERE aclGUID='{$aclGUID}'");
list($maxATA, $actTitle, $aclGUID) = $oSQL->fa($oSQL->q("SELECT aclATA, actTitle{$this->intra->local}, aclGUID
FROM stbl_action_log LEFT OUTER JOIN stbl_action ON aclActionID=actID
WHERE aclEntityItemID='{$entItemID}' AND aclActionPhase=2 AND aclActionID>2
ORDER BY aclATA DESC LIMIT 0,1"));
throw new Exception("ATA ({$ata}) for execueted action cannot be in the past for {$this->item->conf['entTitle']}:{$entItemID}, last action {$actTitle}: {$maxATA}");
}
$sqlATAATD = "SELECT
CASE WHEN DATEDIFF (aclATA, IFNULL(aclATD, aclATA)) < 0 THEN 0 ELSE 1 END as ATAnotLessThanATD
FROM stbl_action_log
WHERE aclGUID='{$aclGUID}'
";
if (!$oSQL->get_data($oSQL->do_query($sqlMaxATA))) {
throw new Exception("ATA for execueted action cannot be less than ATD");
}
return true;
}
function checkMandatoryFields(){
$oSQL = $this->oSQL;
$entID = $this->item->conf['entID'];
$entItemID = $this->item->id;
$entTable = $this->item->conf["entTable"];
$rwEnt = $this->item->item;
$flagAutocomplete = $this->arrAction["actFlagAutocomplete"];
$aclGUID = $this->arrAction["aclGUID"];
$aMandatoryFails = array();
$aChangedFails = array();
foreach((array)$this->arrAction["aatFlagMandatory"] as $atrID => $rwATR){
$oldValue = $this->item->item[$atrID];
if ($this->arrAction["aclGUID"]==""){
/*
$flagIsMissingOnForm = (int)($this->arrAction['actFlagAutocomplete'] && !isset($this->arrNewData[$atrID]));
$sqlCheckMandatory = "SELECT
CASE WHEN IFNULL({$atrID}, '')=''
AND NOT ".(int)$flagIsMissingOnForm."
THEN 0 ELSE 1 END as mandatoryOK
FROM {$entTable} WHERE {$this->entItemIDField}='{$entItemID}'";
$sqlCheckChanges = "SELECT
CASE WHEN IFNULL({$atrID}, '')='{$rwEnt[$atrID]}' THEN 0 ELSE 1 END as changedOK
FROM {$entTable} WHERE {$this->entItemIDField}='{$entItemID}'";
*/
} else {
if($rwATR['aatFlagMandatory'] && !$this->arrAction[$atrID])
$aMandatoryFails[] = $atrID;
if($rwATR['aatFlagToChange'] && $this->arrAction[$atrID]!=$this->item->item[$atrID])
$aChangedFails[] = $atrID;
}
}
if (count($aMandatoryFails)){
$strFields = '';
foreach($aMandatoryFails as $field)
$strFields .= ($strFields ? ', ' : '').$this->item->conf['ATR'][$field]["atrTitle{$this->intra->local}"];
throw new Exception($this->intra->translate("These fields are required: %s for %s", $strFields, $this->item->id));
}
if (count($aChangedFails)){
$strFields = '';
foreach($aChangedFails as $field)
$strFields .= ($strFields ? ', ' : '').$this->item->conf['ATR'][$field]["atrTitle{$this->intra->local}"];
throw new Exception($this->intra->translate("These fields should be changed: %s for %s", $strFields, $this->item->id));
}
if($this->conf['actFlagComment'] && !$this->arrAction['aclComments'])
throw new Exception($this->intra->translate("Action '%s' requires a comment", $this->conf['actTitle'.$this->intra->local]));
}
public function checkPermissions(){
if($this->arrAction['actFlagSystem']){ // system actions has no UI but it can be invoked in code
return;
}
$rwAct = $this->arrAction;
$aUserRoles = array_merge(array($this->item->conf['RoleDefault']), $this->intra->arrUsrData['roleIDs']);
if(count(array_intersect($aUserRoles, $rwAct['RLA']))==0)
throw new Exception($this->intra->translate("%s: user %s not authorized because not member of (%s)",$this->arrAction['actTitle'.$this->intra->local], $this->intra->usrID, implode(', ', $rwAct['RLA'])) );
$reason = '';
if(count($this->item->checkDisabledRoleMembership($this->intra->usrID, $rwAct, $reason)) > 0)
throw new Exception($this->intra->translate("Not authorized as %s", $reason));
}
public function getTimeStamps($nd = null, $flag='all', &$tsValues = array()){
$sql = '';
$a = array_merge($this->arrAction, (array)@json_decode($this->arrAction['aclItemTraced'], true), (array)$nd);
$valNull = 'NOW()';
foreach(self::$ts as $ts){
$val_ts = $a[$this->conf['aatFlagTimestamp'][$ts]];
$tsValues[$ts] = ($this->conf['actTrackPrecision']==='datetime'
? $this->intra->datetimePHP2SQL($val_ts, $valNull)
: $this->intra->datePHP2SQL($val_ts, $valNull)
);
}
// to prevent ATD, ET* from becomimg NOW()
$aclATA = $tsValues['ATA'];
foreach ($tsValues as $ts => $val) {
$tsValues[$ts] = ( $tsValues[$ts]==$valNull ? $aclATA : $tsValues[$ts] );
}
if(!$this->conf['actFlagHasEstimates']){
$tsValues['ETD'] = $tsValues['ATD'];
$tsValues['ETA'] = $tsValues['ATA'];
}
if(!$this->conf['actFlagHasDeparture']){
$tsValues['ETD'] = $tsValues['ETA'];
$tsValues['ATD'] = $tsValues['ATA'];
}
if(strtotime($this->item->oSQL->unq($tsValues['ATA'])) < strtotime($this->item->oSQL->unq($tsValues['ATD']))){
$tsValues['ATA'] = $tsValues['ATD'];
}
if(strtotime($this->item->oSQL->unq($tsValues['ETA'])) < strtotime($this->item->oSQL->unq($tsValues['ETD']))){
$tsValues['ETA'] = $tsValues['ETD'];
}
foreach($tsValues as $ts=>$value){
$sql .= "\n, acl{$ts} = {$value}"; //.' /*'.var_export($this->conf['aatFlagTimestamp'], true).' -- '.var_export($s, true).'*/';
if ( $flag=='all' && in_array($ts, array_keys($this->conf['aatFlagTimestamp'])) ) {
$sql .= "\n, {$this->conf['aatFlagTimestamp'][$ts]}={$value}";
}
}
// echo('<pre>'.var_export($sql, true));
// echo('<pre>'.var_export( $flag=='all' , true));
return $sql;
}
public function getUserStamps(){
$sql = '';
foreach ( (array)$this->conf["aatFlagUserStamp"] as $atrID => $xx ) {
if(array_key_exists($atrID, (array)$this->arrAction["aatFlagToTrack"]))
continue;
$sql .= "\n, {$atrID} = ".$oSQL->e($this->intra->usrID);
}
return $sql;
}
public function getTraceData(){
$aRet = array();
$aTraced = @json_decode($this->arrAction['aclItemTraced'], true);
foreach((array)$this->conf['aatFlagToTrack'] as $field=>$props){
$aRet[$field] = (isset($this->arrAction[$field]) ? $this->arrAction[$field] : $aTraced[$field]);
}
$aRet_SQL = $this->intra->arrPHP2SQL($aRet, $this->item->table['columns_types']);
return $aRet_SQL;
}
static function itemCleanUp($item, $prefix = ''){
foreach ($item as $key => $value) {
if(is_array($value))
unset($item[$key]);
}
unset($item[$prefix.'EditBy']);
unset($item[$prefix.'EditDate']);
unset($item[$prefix.'InsertBy']);
unset($item[$prefix.'InsertDate']);
unset($item[$prefix.'StatusID']);
unset($item[$prefix.'StatusActionLogID']);
unset($item[$prefix.'ActionLogID']);
return $item;
}
}