|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class EVCDataSet extends DataObject |
|
|
|
|
|
|
5
|
|
|
{ |
|
6
|
|
|
private static $default_sort = "LastEdited DESC"; |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* |
|
10
|
|
|
* @param string | null $code |
|
11
|
|
|
* |
|
12
|
|
|
* @return EVCDataSet |
|
|
|
|
|
|
13
|
|
|
*/ |
|
14
|
|
|
public static function find_or_create($code = null, $forceCreation = false) |
|
15
|
|
|
{ |
|
16
|
|
|
$obj = null; |
|
17
|
|
|
if ($code) { |
|
18
|
|
|
Convert::raw2sql($code); |
|
19
|
|
|
$obj = EVCDataSet::get()->filter(array("Code" => $code))->first(); |
|
20
|
|
|
if (!$obj) { |
|
21
|
|
|
$obj = EVCDataSet::get()->filter(array("URLSegment" => $code))->first(); |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
if (!$obj) { |
|
25
|
|
|
if ($forceCreation) { |
|
26
|
|
|
$obj = EVCDataSet::create(); |
|
27
|
|
|
$id = $obj->write(); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
return $obj; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
private static $db = array( |
|
|
|
|
|
|
34
|
|
|
"Title" => "Varchar(255)", |
|
35
|
|
|
"URLSegment" => "Varchar(255)", |
|
36
|
|
|
"Code" => "Varchar(7)", |
|
37
|
|
|
"IP" => "Varchar(15)", |
|
38
|
|
|
"Locked" => "Boolean", |
|
39
|
|
|
"Data" => "Text" |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
private static $indexes = array( |
|
|
|
|
|
|
43
|
|
|
"Code" => true |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
public function Link($action) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$page = EVCPage::get()->first(); |
|
49
|
|
|
return $this->MyLink($page, $action); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function MyLink($page, $action) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
if ($this->URLSegment) { |
|
|
|
|
|
|
55
|
|
|
return $page->AbsoluteLink("$action/$this->URLSegment/"); |
|
|
|
|
|
|
56
|
|
|
} else { |
|
57
|
|
|
return $page->AbsoluteLink("$action/$this->Code/"); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getCopyIfNeeded() |
|
62
|
|
|
{ |
|
63
|
|
|
if ($this->Locked) { |
|
|
|
|
|
|
64
|
|
|
$obj = EVCDataSet::find_or_create(null, true); |
|
65
|
|
|
$obj->Data = $this->Data; |
|
|
|
|
|
|
66
|
|
|
$obj->write(); |
|
67
|
|
|
return $obj; |
|
68
|
|
|
} |
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $key |
|
75
|
|
|
* @param string $value |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setValue($key, $value) |
|
80
|
|
|
{ |
|
81
|
|
|
$obj = $this->getCopyIfNeeded(); |
|
82
|
|
|
$array = unserialize($obj->Data); |
|
|
|
|
|
|
83
|
|
|
$array[$key] = $value; |
|
84
|
|
|
$obj->Data = serialize($array); |
|
|
|
|
|
|
85
|
|
|
$obj->write(); |
|
86
|
|
|
return $obj->Code; |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return string | null |
|
|
|
|
|
|
91
|
|
|
*/ |
|
92
|
|
|
public function returnValuesAsJS() |
|
93
|
|
|
{ |
|
94
|
|
|
$array = unserialize($this->Data); |
|
|
|
|
|
|
95
|
|
|
$json = null; |
|
96
|
|
|
if (is_array($array) && count($array)) { |
|
97
|
|
|
$json = ''; |
|
98
|
|
|
foreach ($array as $key => $value) { |
|
99
|
|
|
$json .= "\n".'EVC.ActualData.'.$key.' = '.(floatval($value)-0).';'; |
|
100
|
|
|
} |
|
101
|
|
|
if ($this->Locked) { |
|
|
|
|
|
|
102
|
|
|
$json .= "\n".'EVC.isLocked = true;'; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
return $json; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function onBeforeWrite() |
|
109
|
|
|
{ |
|
110
|
|
|
parent::onBeforeWrite(); |
|
111
|
|
|
if (!$this->IP) { |
|
|
|
|
|
|
112
|
|
|
$this->IP = $this->getIPAddress(); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
if (!$this->Code) { |
|
|
|
|
|
|
115
|
|
|
$this->Code = substr(hash("md5", uniqid()), 0, 7); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
if ($this->Title) { |
|
|
|
|
|
|
118
|
|
|
$filter = URLSegmentFilter::create(); |
|
119
|
|
|
$this->URLSegment = $filter->filter($this->Title); |
|
|
|
|
|
|
120
|
|
|
// Fallback to generic page name if path is empty (= no valid, convertable characters) |
|
121
|
|
|
if (!$this->URLSegment || $this->URLSegment == '-' || $this->URLSegment == '-1') { |
|
|
|
|
|
|
122
|
|
|
$this->URLSegment = $this->Code; |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
$originalURLSegment = $this->URLSegment; |
|
|
|
|
|
|
125
|
|
|
$originalTitle = $this->Title; |
|
|
|
|
|
|
126
|
|
|
$id = intval($this->ID) - 0; |
|
127
|
|
|
for ($i = 2; $i < 9999; $i++) { |
|
128
|
|
|
$count = EVCDataSet::get()->filter(array("URLSegment" => $this->URLSegment))->exclude(array("ID" => $id))->Count(); |
|
|
|
|
|
|
129
|
|
|
if ($count) { |
|
130
|
|
|
$this->URLSegment = $originalURLSegment."-".$i; |
|
|
|
|
|
|
131
|
|
|
$this->Title = $originalTitle." #".$i; |
|
|
|
|
|
|
132
|
|
|
} else { |
|
133
|
|
|
$i = 9999999; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
private function getIPAddress() |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
$ip = null; |
|
142
|
|
|
if (isset($_SERVER['HTTP_CLIENT_IP'])) { |
|
143
|
|
|
$ip = $_SERVER['HTTP_CLIENT_IP']; |
|
144
|
|
|
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
|
145
|
|
|
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
|
146
|
|
|
} elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { |
|
147
|
|
|
$ip = $_SERVER['HTTP_CF_CONNECTING_IP']; |
|
148
|
|
|
} |
|
149
|
|
|
if ( |
|
150
|
|
|
!$ip || |
|
151
|
|
|
!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) |
|
152
|
|
|
) { |
|
153
|
|
|
$ip = $_SERVER['REMOTE_ADDR']; |
|
154
|
|
|
} |
|
155
|
|
|
return $ip; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.