/
inc_entity_item.php
2224 lines (1714 loc) · 80 KB
/
inc_entity_item.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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
include_once "inc_entity.php";
define("MAX_STL_LENGTH", 256);
class eiseEntityItem extends eiseEntity {
public $oSQL;
public $entID;
public $entItemID;
public $item;
public $arrTimestamp = Array('ETD', 'ETA', 'ATD', 'ATA');
public $arrActionPhase = Array('0' => "Planned"
, '1' => "Started"
, '2' => "Complete"
, '3' => "Cancelled");
protected $arrAction = Array(); // current action data
protected $arrNewData = Array(); // new data, it could be $_POST
protected $defaultDataToObtain = array('Text', 'ACL', 'STL', 'comments', 'files', 'messages');
function __construct( $oSQL, $intra, $entID, $entItemID, $conf = array() ){
$confDefault = array('flagArchive'=>false
, 'flagCreateEmptyObject' => false);
$confToMerge = is_array($conf) ? $conf : array('flagArchive'=>$conf);
$conf = array_merge($confDefault, $confToMerge);
parent::__construct($oSQL, $intra, $entID);
$this->entItemID = $entItemID;
$this->flagArchive = $conf['flagArchive'];
if ($entItemID || $conf['flagCreateEmptyObject']) {
if($entItemID){
if (!$flagArchive){
$this->getEntityItemData();
} else {
$this->getEntityItemDataFromArchive();
}
$this->staID = $this->item[$entID."StatusID"];
} else {
$this->staID = null;
}
} else
throw new Exception ("Entity item ID not set");
}
function refresh($toRefresh=array('Master')){
$this->item = array();
$this->arrAction = array();
if(!$this->flagArchive){
$this->getEntityItemAllData($toRefresh);
} else {
$this->getEntityItemDataFromArchive();
if(count($toRefresh)>1){
unset($toRefresh[array_search('Master', $toRefresh)]); //prevent double-retrieval of information
$this->getEntityItemAllData($toRefresh);
}
}
return $this->item;
}
function getEntityItemData(){
$oSQL = $this->oSQL;
$entID = $this->entID;
$entItemID = $this->entItemID;
$sqlEnt = "SELECT *
FROM {$this->conf['entTable']}
LEFT OUTER JOIN stbl_status STA ON STA.staID={$entID}StatusID AND staEntityID='$entID'
LEFT OUTER JOIN stbl_action_log ACL ON {$entID}ActionLogID=ACL.aclGUID
WHERE {$this->entItemIDField}=".$oSQL->e($entItemID);
$rsEnt = $oSQL->q($sqlEnt);
if ($oSQL->n($rsEnt)==0)
throw new Exception("Entity Item not found".": ".$entID."/".$entItemID, 404);
$rwItem = $oSQL->f($rsEnt);
$this->item = array_merge(
(is_array($this->item) ? $this->item : array())
, $rwItem);
return $rwItem;
}
function getEntityItemDataFromArchive(){
$oSQL = $this->oSQL;
$entID = $this->entID;
$entItemID = $this->entItemID;
$oSQL_arch = $this->intra->getArchiveSQLObject();
$sqlEnt = "SELECT *
FROM {$this->conf['entTable']}
WHERE {$this->entItemIDField}=".$oSQL->e($entItemID);
$rsEnt = $oSQL_arch->q($sqlEnt);
if ($oSQL_arch->n($rsEnt)==0)
throw new Exception("Entity Item not found in the Archive database {$oSQL_arch->dbname}".": ".$entID."/".$entItemID);
$this->item = $oSQL_arch->fetch_array($rsEnt);
return $this->item;
}
function getEntityItemAllData($toRetrieve = null){
if(!$this->entItemID)
return array();
if($toRetrieve===null)
$toRetrieve = $this->defaultDataToObtain;
if ($this->flagArchive) {
$arrData = json_decode($this->item["{$this->entID}Data"], true);
$this->item = array_merge($this->item, $arrData);
return $this->item;
}
// - Master table is $this->item
// attributes and combobox values
if($this->item["{$this->entID}StatusID"]!==null)
$this->staID = (int)$this->item["{$this->entID}StatusID"];
if(in_array('Master', $toRetrieve))
$this->getEntityItemData();
if(in_array('Text', $toRetrieve))
foreach($this->conf["ATR"] as $atrID=>$rwATR){
if (in_array($rwATR["atrType"], Array("combobox", "ajax_dropdown"))){
$this->item[$rwATR["atrID"]."_text"] = !isset($this->item[$rwATR["atrID"]."_text"])
? $this->getDropDownText($rwATR, $this->item[$rwATR["atrID"]])
: $this->item[$rwATR["atrID"]."_text"];
}
}
// collect incomplete/cancelled actions
if(in_array('ACL', $toRetrieve)) {
$this->item["ACL"] = Array();
$sqlACL = "SELECT * FROM stbl_action_log
WHERE aclEntityItemID='{$this->entItemID}'
AND aclActionPhase < 2
ORDER BY aclInsertDate DESC, aclOldStatusID DESC";
$rsACL = $this->oSQL->do_query($sqlACL);
while($rwACL = $this->oSQL->fetch_array($rsACL)){
$this->item["ACL"][$rwACL["aclGUID"]] = $this->getActionData($rwACL["aclGUID"]);
}
$this->item["ACL_Cancelled"] = Array();
$sqlACL = "SELECT * FROM stbl_action_log
WHERE aclEntityItemID='{$this->entItemID}'
AND aclActionPhase > 2
ORDER BY aclInsertDate DESC, aclOldStatusID DESC";
$rsACL = $this->oSQL->do_query($sqlACL);
while($rwACL = $this->oSQL->fetch_array($rsACL)){
$this->item["ACL_Cancelled"][$rwACL["aclGUID"]] = $this->getActionData($rwACL["aclGUID"]);
}
}
// collect status log and nested actions
if(in_array('STL', $toRetrieve)){
$this->item["STL"] = Array();
$this->getStatusData(null);
}
//comments
if(in_array('comments', $toRetrieve)){
$this->item["comments"] = Array();
$sqlSCM = "SELECT *
FROM stbl_comments
WHERE scmEntityItemID='{$this->entItemID}' ORDER BY scmInsertDate DESC";
$rsSCM = $this->oSQL->do_query($sqlSCM);
while ($rwSCM = $this->oSQL->f($rsSCM)){
$this->item["comments"][$rwSCM["scmGUID"]] = $rwSCM;
}
}
//files
if(in_array('files', $toRetrieve)){
$this->item["files"] = Array();
$sqlFile = "SELECT * FROM stbl_file WHERE filEntityID='{$this->entID}' AND filEntityItemID='{$this->entItemID}'
ORDER BY filInsertDate DESC";
$rsFile = $this->oSQL->do_query($sqlFile);
while ($rwFIL = $this->oSQL->f($rsFile)){
$this->item["files"][$rwFIL["filGUID"]] = $rwFIL;
}
}
//message
if(in_array('message', $toRetrieve)){
$this->item["messages"] = Array();//not yet
}
//echo "<pre>";
//print_r($this->item);
//die();
return $this->item;
}
function delete(){
$oSQL = $this->oSQL;
if ($this->flagArchive){
$oSQL_arch = $this->intra->getArchiveSQLObject();
$oSQL_arch->q("DELETE FROM {$this->conf["entTable"]} WHERE {$this->entItemIDField}=".$oSQL->e($this->entItemID));
return;
}
$rwEnt = $this->item;
$entItemID = $this->entItemID;
$entID = $this->entID;
$sqlDel[] = "DELETE {$this->conf["entTable"]}_log, stbl_action_log FROM {$this->conf["entTable"]}_log INNER JOIN stbl_action_log
ON aclGUID=l{$entID}GUID WHERE aclEntityItemID='{$entItemID}'";
$sqlDel[] = "DELETE {$this->conf["entTable"]}_log, stbl_status_log FROM {$this->conf["entTable"]}_log INNER JOIN stbl_status_log
ON stlGUID=l{$entID}GUID WHERE stlEntityItemID='{$entItemID}'";
$sqlDel[] = "DELETE FROM stbl_action_log WHERE aclEntityItemID='{$this->entItemID}'";
$sqlDel[] = "DELETE FROM stbl_status_log WHERE stlEntityItemID='{$this->entItemID}'";
// comments
$sqlDel[] = "DELETE FROM stbl_comments WHERE scmEntityItemID=".$oSQL->e($this->entItemID);
// files
$sqlDel[] = "DELETE FROM stbl_file WHERE filEntityItemID=".$oSQL->e($this->entItemID);
// master
$sqlDel[] = "DELETE FROM {$this->conf["entTable"]} WHERE {$this->entItemIDField}='{$this->entItemID}'";
for ($i=0;$i<count($sqlDel);$i++)
$oSQL->do_query($sqlDel[$i]);
}
// 'Creates' existing entity item
public function create(){
$this->doCreate();
}
// updates master table & action log
// doesn't re-read entity item data from the database
public function update($arrNewData, $flagUpdateMultiple = false, $flagFullEdit = false){
$this->updateMasterTable($arrNewData, $flagUpdateMultiple, $flagFullEdit);
$this->updateActionLog();
// if no action set with arrNewData we record an update
if (!isset($arrNewData["actID"]) && !isset($arrNewData["aclGUID"])){
$this->doUpdate(); // record update operation to the log
}
}
// executes action over entity item
public function doAction($arrNewData = null, $aclOldStatusID = null, $aclNewStatusID = null){
if ($arrNewData!==null){
$this->refresh(array('Master', 'ACL'));
if(!is_array($arrNewData) && is_numeric($arrNewData)){
$actID = $arrNewData;
$arrNewData = array("actID"=>$actID // update if aclGUID specified
, "aclOldStatusID"=>($aclOldStatusID!==null ? $aclOldStatusID : $this->staID)
, "aclNewStatusID"=>is_array($this->conf['ACT'][$actID]['actNewStatusID'])
? ($aclNewStatusID!==null ? $aclNewStatusID : $this->conf['ACT'][$actID]['actNewStatusID'][0])
: $this->conf['ACT'][$actID]['actNewStatusID']
);
}
$this->arrNewData = $arrNewData;
}
if (count($this->arrAction)==0){
$this->prepareActions();
}
// proceed with the action
if ($this->arrAction["actFlagAutocomplete"] && $this->arrAction["aclGUID"]==""){
$this->checkMandatoryFields();
$this->addAction();
$this->checkTimeLine();
$this->finishAction();
} else {
if ($this->arrAction["aclGUID"]==""){
$this->addAction();
} else {
switch ($this->arrNewData["aclToDo"]) {
case "finish":
$this->checkMandatoryFields();
$this->checkTimeLine();
$this->finishAction();
break;
case "start":
$this->startAction();
break;
case "cancel":
$this->cancelAction();
break;
}
}
}
$aclGUID = $this->arrAction["aclGUID"];
return $aclGUID;
}
// backward-compatibility
public function doSimpleAction($arrNewData){
$aclGUID = $this->doAction($arrNewData);
$this->refresh();
return $aclGUID;
}
// updates Master Table, Action Log, and add/start/finish/cancel action mentioned in arrNewData[aclToDo]
// returns aclGUID of performed action
public function doFullAction($arrNewData = Array()){
if (count($arrNewData)>0)
$this->update($arrNewData);
$aclGUID = $this->doAction();
$this->refresh();
return $aclGUID;
}
public function doCreate(){
$oSQL = $this->oSQL;
// check existance
if($oSQL->d('SELECT COUNT(*) FROM stbl_action_log WHERE aclEntityItemID='.$oSQL->e($this->entItemID).' AND aclActionID=1'))
throw new Exception('Create action already exists in log for '.$this->entItemID);
$this->doSimpleAction(Array( // Create
"actID"=> 1
));
}
public function doUpdate($flagForce = false){
if(!($this->conf['flagDontLogUpdates'] || $flagForce))
$this->doSimpleAction(Array( // Create
"actID"=> 2
));
}
public function doDelete(){
$this->doSimpleAction(Array( // Delete
"actID"=> 3
));
}
// adds record to stbl_action_log, {entTable}_log with initial data from Master Table
// returns aclGUID
public function addAction($arrAction = null){
$usrID = $this->intra->usrID;
$oSQL = $this->oSQL;
if ($arrAction!==null){
$this->arrAction = $arrAction;
}
// 1. obtaining aclGUID
$this->arrAction["aclGUID"] = $oSQL->d("SELECT UUID()");
// 2. insert new ACL
$sqlInsACL = "INSERT INTO `stbl_action_log`
(
`aclGUID`
, aclPredID
, `aclActionID`
, `aclEntityItemID`
, aclOldStatusID
, aclNewStatusID
, aclActionPhase
, aclETD
, aclETA
, aclATD
, aclATA
, `aclComments`
, `aclInsertBy`, `aclInsertDate`, `aclEditBy`, `aclEditDate`
) SELECT
'{$this->arrAction["aclGUID"]}' as aclGUID
, ".($this->item["{$this->entID}StatusActionLogID"]!="" ? "'".$this->item["{$this->entID}StatusActionLogID"]."'" : "NULL")." as aclPredID
, ".(int)$this->arrAction["actID"]."
, '".$this->entItemID."'
, ".((string)$this->arrAction['aclOldStatusID']=="" ? "NULL" : "'".(string)$this->arrAction['aclOldStatusID']."'")."
, ".((string)$this->arrAction['aclNewStatusID']=="" ? "NULL" : "'".(string)$this->arrAction['aclNewStatusID']."'")."
, '0'
, ".(isset($this->arrAction["aclETD_attr"]) ? $this->arrAction["aclETD_attr"] : "NULL")."
, ".(isset($this->arrAction["aclETA_attr"]) ? $this->arrAction["aclETA_attr"] : "NULL")."
, ".(isset($this->arrAction["aclATD_attr"]) ? $this->arrAction["aclATD_attr"] : "NULL")."
, ".(isset($this->arrAction["aclATA_attr"]) ? $this->arrAction["aclATA_attr"] : "NULL")."
, ".$oSQL->escape_string($this->arrAction["aclComments"])."
, '{$this->intra->usrID}', NOW(), '{$this->intra->usrID}', NOW()
FROM {$this->conf["entTable"]} WHERE {$this->entItemIDField}='{$this->entItemID}'";
$oSQL->do_query($sqlInsACL);
// 3. insert ATV
// generate script that copy data from the master table
$arrFields = Array();
foreach( (array)$this->arrAction["aatFlagToTrack"] as $atrID => $rwAAT ){
// define attributes for timestamp
if ($rwAAT["aatFlagTimestamp"]) {
$this->arrAction["acl".$rwAAT["aatFlagTimestamp"]."_attr"] = ($rwAAT["aatFlagEmptyOnInsert"] ? "NULL" : $atrID);
}
$arrFields["l".$atrID] = ($rwAAT["aatFlagEmptyOnInsert"]
? "NULL"
: ($rwAAT['aatFlagUserStamp']
? $oSQL->e($this->intra->usrID)
: $atrID)
);
}
if (count($arrFields)!=0){
$sqlInsATV = "INSERT INTO {$this->conf["entTable"]}_log (
l{$this->conf['entPrefix']}GUID
";
foreach ($arrFields as $field => $value)
$sqlInsATV .= ", ".$field;
$sqlInsATV .= ", l{$this->conf['entPrefix']}InsertBy, l{$this->conf['entPrefix']}InsertDate, l{$this->conf['entPrefix']}EditBy, l{$this->conf['entPrefix']}EditDate
) SELECT
'{$this->arrAction["aclGUID"]}'AS atvGUID
";
foreach ($arrFields as $field => $value)
$sqlInsATV .= ", ".$value;
$sqlInsATV .= "
, '{$usrID}' AS atvInsertBy, NOW() AS atvInsertDate, '{$usrID}' AS atvEditBy, NOW() AS atvEditDate
FROM {$this->conf["entTable"]} WHERE {$this->entItemIDField}='{$this->entItemID}'
";
$oSQL->do_query($sqlInsATV);
}
$this->onActionPlan($this->arrAction['actID'], $this->arrAction['aclOldStatusID'], $this->arrAction['aclNewStatusID']);
return $this->arrAction["aclGUID"];
}
public function findAction($aclOldStatusID, $aclNewStatusID, $aclActionID, $aclActionPhase = null, $date=null){
$arrACL = Array();
$sqlACL = "SELECT * FROM stbl_action_log
LEFT OUTER JOIN {$this->conf["entTable"]}_log ON aclGUID=l{$this->conf['entPrefix']}GUID
WHERE aclEntityItemID='{$this->entItemID}'
".($aclOldStatusID!==null
? " AND aclOldStatusID".(is_array($aclOldStatusID)
? " IN (".implode(", ", $aclOldStatusID).")"
: "=".(int)$aclOldStatusID
)
: "").
($aclNewStatusID!==null ? " AND aclNewStatusID".(is_array($aclNewStatusID)
? " IN (".implode(", ", $aclNewStatusID).")"
: "=".(int)$aclNewStatusID
)
: "").
($aclActionID!==null
? " AND aclActionID".(is_array($aclActionID)
? " IN (".implode(", ", $aclActionID).")"
: "=".(int)$aclActionID)
: "").
($aclActionPhase!==null
? " AND aclActionPhase".(is_array($aclActionPhase)
? " IN (".implode(", ", $aclActionPhase).")"
: "=".(int)$aclActionPhase)
: "").
($date!==null
? " AND IFNULL(aclATD, aclATA) BETWEEN '".$this->intra->getDateTimeByOperationTime($date, (isset($this->intra->conf['stpOperationDayStart']) ? $this->intra->conf['stpOperationDayStart'] : '00:00'))."'".
" AND '".$this->intra->getDateTimeByOperationTime($date, (isset($this->intra->conf['stpOperationDayEnd']) ? $this->intra->conf['stpOperationDayEnd'] : '23:59:59'))."'"
: "")."
ORDER BY aclInsertDate DESC
";
$rsACL = $this->oSQL->q($sqlACL);
while($rwACL = $this->oSQL->f($rsACL)){
$arrACL[] = $rwACL;
}
return $arrACL;
}
function finishAction(){
$usrID = $this->intra->usrID;
$oSQL = $this->oSQL;
if (!$this->arrAction["aclActionPhase"]){
$this->onActionStart($this->arrAction['actID'], $this->arrAction['aclOldStatusID'], $this->arrAction['aclNewStatusID']);
}
// update started action as completed
$sqlUpdACL = "UPDATE stbl_action_log SET
aclActionPhase = 2
, aclATA= IFNULL(aclATA, NOW())
, 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"]."'";
$oSQL->do_query($sqlUpdACL);
if ($this->arrAction["actID"]!="2") {
$sqlUpdEntTable = "UPDATE {$this->conf["entTable"]} LEFT OUTER JOIN stbl_action_log ON aclGUID='{$this->arrAction['aclGUID']}' SET
{$this->entID}ActionLogID=aclGUID
, {$this->conf['entPrefix']}EditBy='{$this->intra->usrID}', {$this->conf['entPrefix']}EditDate=NOW()";
// update tracked attributes
foreach( (array)$this->arrAction["aatFlagToTrack"] as $atrID=>$xx ){
$sqlUpdEntTable .= "\r\n, {$atrID} = (SELECT l{$atrID} FROM {$this->conf["entTable"]}_log WHERE l{$this->conf['entPrefix']}GUID='{$this->arrAction["aclGUID"]}')";
}
// update timestamps
foreach ( (array)$this->arrAction["aatFlagTimestamp"] as $timestamp => $atrID ) {
if(!array_key_exists($atrID, (array)$this->conf["ATR"]))
continue;
$sqlUpdEntTable .= "\r\n, {$atrID} = acl{$timestamp}";
}
// update userstamps
foreach ( (array)$this->arrAction["aatFlagUserStamp"] as $atrID => $xx ) {
if(array_key_exists($atrID, (array)$this->arrAction["aatFlagToTrack"]))
continue;
$sqlUpdEntTable .= "\r\n, {$atrID} = ".$oSQL->e($this->intra->usrID);
}
$sqlUpdEntTable .= "\r\n";
$sqlUpdEntTable .= "WHERE {$this->entItemIDField}='{$this->entItemID}'";
$oSQL->do_query($sqlUpdEntTable);
}
// update master table by attrbutes
if (count($this->arrAction["aatFlagToTrack"])>0){
$sqlUpdMaster = "UPDATE {$this->conf["entTable"]} SET
{$this->conf['entPrefix']}EditBy='{$this->intra->usrID}', {$this->conf['entPrefix']}EditDate=NOW()";
$sqlUpdMaster .= "\r\nWHERE {$this->entItemIDField}='{$this->entItemID}'";
$oSQL->do_query($sqlUpdMaster);
}
$this->onActionFinish($this->arrAction['actID'], $this->arrAction['aclOldStatusID'], $this->arrAction['aclNewStatusID']);
// if status is changed or action requires status stay interruption, we insert status log entry and update master table
if (((string)$this->arrAction["aclOldStatusID"]!=(string)$this->arrAction["aclNewStatusID"]
&& (string)$this->arrAction["aclNewStatusID"]!=""
)
|| $this->arrAction["actFlagInterruptStatusStay"]){
$sql = Array();
$stlGUID = $oSQL->get_data($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->entItemID}' 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->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->entID}'
WHERE aclGUID='{$this->arrAction["aclGUID"]}'";
$arrSAT = $this->conf['STA'][$this->arrAction['aclNewStatusID']]['satFlagTrackOnArrival'];
if (count($arrSAT)>0){
$sqlSAT = "INSERT INTO {$this->conf["entTable"]}_log (
l{$this->conf['entPrefix']}GUID
, l{$this->conf['entPrefix']}EditBy , l{$this->conf['entPrefix']}EditDate, l{$this->conf['entPrefix']}InsertBy, l{$this->conf['entPrefix']}InsertDate
";
foreach($arrSAT as $atrID=>$x)
$sqlSAT .= ", l{$atrID}";
$sqlSAT .= ") SELECT
'$stlGUID' as l{$this->conf['entPrefix']}GUID
, '{$this->intra->usrID}' as l{$this->conf['entPrefix']}EditBy , NOW() as l{$this->conf['entPrefix']}EditDate, '{$this->intra->usrID}' as {$this->conf['entPrefix']}InsertBy, NOW() as {$this->conf['entPrefix']}InsertDate
";
foreach($arrSAT as $atrID=>$ix)
$sqlSAT .= ", {$atrID}";
$sqlSAT .= " FROM {$this->conf["entTable"]} WHERE {$this->entItemIDField}='{$this->entItemID}'";
$sql[] = $sqlSAT;
}
// after action is done, we update entity table with last status action log id
$sql[] = "UPDATE {$this->conf["entTable"]} SET
{$this->entID}ActionLogID='{$this->arrAction["aclGUID"]}'
, {$this->entID}StatusActionLogID='{$this->arrAction["aclGUID"]}'
, {$this->entID}StatusID=".(int)$this->arrAction["aclNewStatusID"]."
, {$this->conf['entPrefix']}EditBy='{$this->intra->usrID}', {$this->conf['entPrefix']}EditDate=NOW()
WHERE {$this->entItemIDField}='{$this->entItemID}'";
for($i=0;$i<count($sql);$i++){
$oSQL->do_query($sql[$i]);
}
$this->onStatusArrival($this->arrAction['aclNewStatusID']);
}
$this->staID = $this->arrAction['aclNewStatusID'];
}
public function prepareActions(){
$oSQL = $this->oSQL;
$this->arrAction = Array();
if (empty($this->arrNewData["aclGUID"]) && empty($this->arrNewData["actID"])){
//throw new Exception('Neither Action ID nor Action Log GUID specified');
$this->arrNewData["actID"] = 2;
}
if(!$this->item['ACL']){
$this->getEntityItemAllData(array('ACL'));
}
//collect coming action
if ($this->arrNewData["aclGUID"]){
if(!$this->arrNewData['isUndo'])
$rwACT = $this->item['ACL'][$this->arrNewData["aclGUID"]]; //if ACL GUID specified, we try to locate action in ACL
else {
$rwACT = $this->getActionData($this->arrNewData["aclGUID"]);
}
} else {
$rwACT = $this->conf['ACT'][$this->arrNewData["actID"]]; // else we retrive action data from ACT associative array member
}
if (!$rwACT){
throw new Exception("Action not found for ID/GUID {$this->arrNewData["actID"]}/{$this->arrNewData["aclGUID"]}");
}
$aclOldStatusID = isset($this->arrNewData["aclOldStatusID"])
? $this->arrNewData["aclOldStatusID"]
: ($this->arrNewData["aclGUID"]
? $rwACT["aclOldStatusID"]
: $this->item["staID"]);
$aclNewStatusID = isset($this->arrNewData["aclNewStatusID"]) ? $this->arrNewData["aclNewStatusID"] : $rwACT["aclNewStatusID"];
$this->arrAction["aclComments"] = $this->arrNewData["aclComments"];
if(!$rwACT['actEntityID']){
switch($rwACT['actID']){
case 1:
if($aclOldStatusID>0)
throw new Exception('Item is already created');
break;
case 2:
if(!$this->conf['STA'][$aclOldStatusID]['staFlagCanUpdate'] && !$this->flagFullEdit)
throw new Exception('Update is not allowed');
break;
case 3:
if(!$this->conf['STA'][$aclOldStatusID]['staFlagCanDelete'])
throw new Exception('Delete is not allowed');
break;
default:
break;
}
} else {
if(!in_array($aclOldStatusID, $rwACT['actOldStatusID'])){
throw new Exception("Action {$rwACT['actID']} cannot be run for origin status ".$aclOldStatusID);
}
if(!in_array($aclNewStatusID, $rwACT['actNewStatusID'])){
throw new Exception("Action {$rwACT['actID']} cannot be run for destination status ".$aclOldStatusID);
}
}
$this->arrAction['aclOldStatusID'] = $aclOldStatusID;
$this->arrAction['aclNewStatusID'] = $aclNewStatusID;
$this->arrAction = array_merge($rwACT, $this->arrAction);
if($timestamp = $this->arrNewData['aclETD'])
$this->arrAction['aclETD_attr'] = $this->intra->datePHP2SQL($timestamp);
if($timestamp = $this->arrNewData['aclETA'])
$this->arrAction['aclETA_attr'] = $this->intra->datePHP2SQL($timestamp);
if($timestamp = $this->arrNewData['aclATD'])
$this->arrAction['aclATD_attr'] = $this->intra->datePHP2SQL($timestamp);
if($timestamp = $this->arrNewData['aclATA'])
$this->arrAction['aclATA_attr'] = $this->intra->datePHP2SQL($timestamp);
if (is_array($this->arrAction["aatFlagToTrack"]))
foreach($this->arrAction["aatFlagToTrack"] as $atrID=>$options){
// define attributes for timestamp
$arrTS = $this->conf['ACT'][$rwACT['actID']]['aatFlagTimestamp'];
if (in_array($atrID, $arrTS)) {
$this->arrAction["acl".array_search($atrID, $arrTS)."_attr"] = ($options["aatFlagEmptyOnInsert"] ? "NULL" : $atrID);
}
}
}
public function attachFile($fileNameOriginal, $fileContents, $fileMIME="Application/binary"){
$usrID = $this->intra->usrID;
$arrSetup = $this->intra->conf;
$oSQL = $this->oSQL;
$sqlGUID = "SELECT UUID() as GUID";
$fileGUID = $oSQL->get_data($oSQL->do_query($sqlGUID));
$filename = Date("Y/m/").$fileGUID.".att";
//saving the file
if(!file_exists($arrSetup["stpFilesPath"].Date("Y/m")))
mkdir($arrSetup["stpFilesPath"].Date("Y/m"), 0777, true);
//echo $arrSetup["stpFilesPath"].$filename;
$fh = fopen($arrSetup["stpFilesPath"].$filename, "w");
fwrite($fh, $fileContents, strlen($fileContents));
fclose($fh);
//making the record in the database
$sqlFileInsert = "
INSERT INTO stbl_file (
filGUID
, filEntityID
, filEntityItemID
, filName
, filNamePhysical
, filLength
, filContentType
, filInsertBy, filInsertDate, filEditBy, filEditDate
) VALUES (
'".$fileGUID."'
, '{$this->entID}'
, '{$this->entItemID}'
, '{$fileNameOriginal}'
, '$filename'
, '".strlen($fileContents)."'
, '{$fileMIME}'
, '{$this->intra->usrID}', NOW(), '{$this->intra->usrID}', NOW());
";
$oSQL->do_query($sqlFileInsert);
return $fileGUID;
}
function updateMasterTable($arrNewData = Array(), $flagUpdateMultiple = false, $flagFullEditMode = false){
if (count($arrNewData)>0)
$this->arrNewData = $arrNewData;
if (count($this->arrNewData)==0){
throw new Exception($intra->translate("New data set is empty for {$this->entID}/{$this->entItemID}"));
}
$oSQL = $this->oSQL;
$entID = $this->entID;
$entItemID = $this->entItemID;
$rwEnt = $this->item;
$intra = $this->intra;
// 1. update table by visible/editable attributes list
$atrToUpd = Array();
$strFieldList = "";
$arrATRToLoop = ($flagFullEditMode
? $this->conf['ATR']
: (is_array($this->conf['STA'][$this->item['staID']]['satFlagEditable'])
? $this->conf['STA'][$this->item['staID']]['satFlagEditable']
: array()
)
);
foreach ($arrATRToLoop as $atrID=>$FlagWrite){
$rwSAT = $this->conf['ATR'][$atrID];
if(!$rwSAT)
continue;
if ($rwSAT['atrFlagDeleted'] && !$flagFullEditMode)
continue;
if ((!$FlagWrite && !$flagFullEditMode) // not editable
|| ($arrNewData[$atrID]=="" && $flagUpdateMultiple) // empty on multiple updates
|| !isset($arrNewData[$rwSAT["atrID"]])) // not set
continue;
$toEval = "\"".str_replace("\$_POST", "\$this->arrNewData", $intra->getSQLValue(Array('Field'=>$rwSAT['atrID'], 'DataType'=>$rwSAT['atrType'])))."\"";
eval("\$newValue = ".$toEval.";");
if ($newValue!=$rwEnt[$rwSAT["atrID"]]){
$strFieldList .= "\r\n, `{$rwSAT["atrID"]}`={$newValue}";
}
if ($rwSAT["atrUOMTypeID"]){
$strFieldList .= ", {$rwSAT["atrID"]}_uomID=".($this->arrNewData["{$rwSAT["atrID"]}_uomID"]
? $oSQL->e($this->arrNewData["{$rwSAT["atrID"]}_uomID"])
: $oSQL->e($oSQL->d("SELECT uomID FROM stbl_uom WHERE uomType='{$rwSAT['atrUOMTypeID']}' AND uomRateToDefault=1.0 LIMIT 0,1"))
);
}
}
$sqlUpdateTable = "UPDATE {$this->conf["entTable"]} SET
{$entID}EditDate=NOW(), {$entID}EditBy='{$this->intra->usrID}'
{$strFieldList}
WHERE {$this->entItemIDField}='{$entItemID}'";
$oSQL->do_query($sqlUpdateTable);
}
function updateActionLog($arrNewData = Array()){
if (count($arrNewData)>0)
$this->arrNewData = $arrNewData;
if (count($this->arrNewData)==0){
throw new Exception($intra->translate("New data set is empty for {$this->entID}/{$this->entItemID}"));
}
if (count($this->arrAction)==0){
$this->prepareActions();
}
foreach($this->item['ACL'] as $aclGUID => $arrACL){
$this->updateActionLogItem($aclGUID, $arrACL);
}
}
function updateActionLogItem($aclGUID, $arrACL = null){
$oSQL = $this->oSQL;
$intra = $this->intra;
$strEntityLogFldToSet = "";
$tsfieldsToSet = "";
if ($arrACL===null){
$arrACL = $this->getActionData($aclGUID);
}
foreach ((array)$arrACL["AAT"] as $atrID => $arrAAT){
$newValue = null;
$strACLInputID = $atrID."_".$aclGUID;
if($arrAAT['aatFlagUserStamp']){
$strEntityLogFldToSet .= ", l{$atrID} = ".$oSQL->e($this->intra->usrID);
continue;
}
// if we have it in arrNewData: atrID_aclGUID
if (isset($this->arrNewData[$strACLInputID]) ) {
$toEval = "\"".str_replace("\$_POST", "\$this->arrNewData"
, $this->intra->getSQLValue(Array('Field'=>$strACLInputID, 'DataType'=>$this->conf['ATR'][$atrID]["atrType"])))."\"";
eval("\$newValue = ".$toEval.";");
}
if ($key = array_search($atrID, $arrACL["aatFlagTimestamp"])){
$strTimeStampInputID = "acl".$key."_".$aclGUID;
// if attribute is a timestamp:
if(isset($this->arrNewData[$strTimeStampInputID])){ // if we timestamp in arrNewData, we update attribute field in log table
$toEval = "\"".str_replace("\$_POST", "\$this->arrNewData"
, $this->intra->getSQLValue(Array('Field'=>$strTimeStampInputID, 'DataType'=>(!$arrACL['actTrackPrecision'] ? 'datetime' : $arrACL['actTrackPrecision']))))."\"";
eval("\$newValue = ".$toEval.";");
} else { // if there's no timestamp, we try to update ACL timestamp basing on
if ($newValue!==null){
$tsfieldsToSet .= "\r\n, acl{$key}={$newValue}";
if($arrACL["actFlagDepartureEqArrival"] && $key=="ATA"){
$tsfieldsToSet .= "\r\n, aclATD={$newValue}";
}
}
}
}
if ($newValue!==null){
$strEntityLogFldToSet .= ", l{$atrID} = {$newValue}";
}
}
$sqlToTrack = "UPDATE {$this->conf["entTable"]}_log SET
l{$this->conf['entPrefix']}EditBy='{$this->intra->usrID}', l{$this->conf['entPrefix']}EditDate=NOW()
{$strEntityLogFldToSet}
WHERE l{$this->conf['entPrefix']}GUID='{$aclGUID}'";
$this->oSQL->q($sqlToTrack);
foreach($this->arrTimestamp as $ts){
//echo "acl".$ts."_".$aclGUID." ";
if (isset($this->arrNewData["acl".$ts."_".$aclGUID])){
$toEval = "\"".str_replace("\$_POST", "\$this->arrNewData"
, $this->intra->getSQLValue(Array('Field'=>"acl".$ts."_".$aclGUID, 'DataType'=>(!$arrACL['actTrackPrecision'] ? 'datetime' : $arrACL['actTrackPrecision']))))."\"";
eval("\$newValue = ".$toEval.";");
$tsfieldsToSet .= "\r\n, acl{$ts}={$newValue}";
}
}
$sqlToTrack = "UPDATE stbl_action_log SET aclEditBy='{$this->intra->usrID}'
, aclEditDate=NOW()
{$tsfieldsToSet}
WHERE aclGUID='{$aclGUID}'";
$this->oSQL->q($sqlToTrack);
}
function updateStatusLogItem($stlGUID, $arrSTL = null){
$oSQL = $this->oSQL;
$intra = $this->intra;
if($arrSTL===null){
foreach($entItem->rwEnt["STL"] as $stlGUID_ => $rwSTL){
if ($stlGUID_==$stlGUID){
$arrSTL = $rwSTL;
break;
}
}
}