-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtbs_plugin_mergeonfly.php
81 lines (70 loc) · 2.35 KB
/
tbs_plugin_mergeonfly.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
<?php
/*
********************************************************
TinyButStrong Plug-in: Merge On Fly
Version 1.1.1, on 2012-08-15, by Skrol29
********************************************************
*/
define('TBS_ONFLY','tbsMergeOnFly');
class tbsMergeOnFly {
function OnInstall($PackSize=10) {
$this->Version = '1.1.0';
$this->PackSize = $PackSize;
$this->Encaps = 0; // encapsualtion level of blocks
$this->IsActivated = false;
$this->Debug = false;
$this->CountSubRecords = false;
return array('OnCommand','BeforeMergeBlock','OnMergeSection','AfterMergeBlock');
}
function UpdateActivation() {
$this->IsActivated = ($this->PackSize>0) && ($this->Encaps==1);
}
function OnCommand($PackSize, $CountSubRecords=false) {
if ($this->Encaps==0) {
$this->PackSize = $PackSize;
$this->CountSubRecords = $CountSubRecords;
}
$this->UpdateActivation();
}
function BeforeMergeBlock(&$TplSource,&$BlockBeg,&$BlockEnd,$PrmLst) {
$this->Encaps++;
$this->UpdateActivation();
if ($this->IsActivated) {
$this->Counter = 0;
$Part2 = substr($TplSource,$BlockBeg);
$this->TBS->Source = substr($TplSource,0,$BlockBeg);
$this->TBS->Show(TBS_OUTPUT);
if ($this->Debug) echo "\n *DEBUG* BeforeMergeBlock : Flush, PackSize=".$this->PackSize.", CountSubRecords=".var_export($this->CountSubRecords,true).", Counter=".$this->Counter.".\n";
flush();
$TplSource = $Part2;
$BlockEnd = $BlockEnd - $BlockBeg;
$BlockBeg = 0;
}
}
function AfterMergeBlock(&$Buffer,&$DataSrc,&$LocR) {
$this->Encaps--;
if ($this->Encaps<=0) {
$this->PackSize = 0; // deactivate flushing
$this->Encaps = 0; // avoid encapsualtion errors
$this->CountSubRecords = false; // for optimization
if ($this->Debug) echo "\n *DEBUG* AfterMergeBlock : Deactivation.\n";
}
$this->UpdateActivation();
}
function OnMergeSection(&$Buffer,&$NewPart) {
// sub-record also count for the flusing.
if ($this->IsActivated) {
$this->Counter++;
if ($this->Counter>=$this->PackSize) {
echo $Buffer.$NewPart;
if ($this->Debug) echo "\n *DEBUG* OnMergeSection : Flush, Counter=".$this->Counter.".\n";
flush();
$Buffer = '';
$NewPart = '';
$this->Counter = 0;
}
} elseif ( $this->CountSubRecords && ($this->Encaps!=0) ) {
$this->Counter++;
}
}
}