EGOCMS  24.0
EGOTEC Content-Managament-System
Ego_Progress.php
gehe zur Dokumentation dieser Datei
1 <?php
7 class Ego_Progress {
13  private $log;
14 
20  private $value;
21 
27  private $alreadyExists = false;
28 
36  public function __construct($name, $reset = true, $create = true) {
37  $path = $GLOBALS['egotec_conf']['log_dir'].'progress/';
38  if (!Ego_System::file_exists($path)) {
39  Ego_System::mkdir($path);
40  }
41  $this->log = $path.md5($name);
42 
43  // Fortschritt zurücksetzen
44  if ($reset) {
45  $this->clear();
46  }
47 
48  // Daten des Fortschritts ermitteln
49  if (!Ego_System::file_exists($this->log)) {
50  if ($create) {
51  // Logdatei anlegen, wenn diese nicht existiert
52  $this->value = array(
53  'name' => $name,
54  'start' => 0,
55  'end' => 0,
56  'limit' => 0,
57  'current' => 0,
58  'message' => '',
59  'data' => array()
60  );
61  $this->update(true);
62  }
63  } else {
64  $this->alreadyExists = true;
65  $this->value = json_decode(Ego_System::file_get_contents($this->log), true);
66  }
67  }
68 
74  private function reload() {
75  $this->value = json_decode(Ego_System::file_get_contents($this->log), true);
76  }
77 
83  public function hasFinished() {
84  return $this->value['current'] >= $this->value['limit'] && !empty($this->value['end']);
85  }
86 
92  public function hasEnded() {
93  $this->reload();
94  return !empty($this->value['end']);
95  }
96 
102  public function getValue() {
103  return $this->value;
104  }
105 
112  public function getValueByKey($key) {
113  return $this->value[$key];
114  }
115 
122  private function update($force = false) {
123  if ($force || $this->stillExists()) {
124  Ego_System::file_put_contents($this->log, json_encode($this->value, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
125  }
126  }
127 
134  public function setLimit($limit) {
135  $this->value['limit'] = $limit;
136  $this->update();
137  }
138 
145  public function setMessage($message = '') {
146  $this->value['message'] = $message;
147  $this->update();
148  }
149 
156  public function setData($data = array()) {
157  $this->value['data'] = $data;
158  $this->update();
159  }
160 
167  public function setContinue($data) {
168  $this->value['continue'] = $data;
169  $this->value['continue']['_ref'] = md5(microtime());
170  $this->update();
171  }
172 
178  public function clearContinue() {
179  if (!empty($this->value['continue'])) {
180  unset($this->value['continue']);
181  $this->update();
182  }
183  }
184 
191  public function getContinue($clear = true) {
192  if (!empty($this->value['continue'])) {
193  $continue = $this->value['continue'];
194  if ($clear) {
195  $this->clearContinue();
196  }
197  return $continue;
198  }
199  return null;
200  }
201 
210  public function increase($message = '', $data = array(), $amount = 1) {
211  $this->value['current'] += $amount;
212  if (!empty($message)) {
213  $this->value['message'] = $message;
214  }
215  if (!empty($data)) {
216  $this->value['data'] = $data;
217  }
218  $this->update();
219  }
220 
228  public function start($message = '', $data = array()) {
229  $this->value['start'] = time();
230  if (!empty($message)) {
231  $this->value['message'] = $message;
232  }
233  if (!empty($data)) {
234  $this->value['data'] = $data;
235  }
236  $this->update();
237  }
238 
246  public function end($message = '', $data = array()) {
247  $this->value['end'] = time();
248  $this->value['current'] = $this->value['limit'];
249  if (!empty($message)) {
250  $this->value['message'] = $message;
251  }
252  if (!empty($data)) {
253  $this->value['data'] = $data;
254  }
255  $this->update();
256  }
257 
265  public function error($message = '', $data = array()) {
266  $this->value['error'] = true;
267  $this->end($message, $data);
268  }
269 
275  public function clear() {
276  @unlink($this->log);
277  }
278 
284  public function exists() {
285  return $this->alreadyExists;
286  }
287 
293  public function stillExists() {
294  return Ego_System::file_exists($this->log);
295  }
296 }
297 ?>
setContinue($data)
__construct($name, $reset=true, $create=true)
getValueByKey($key)
end($message='', $data=array())
setMessage($message='')
getContinue($clear=true)
start($message='', $data=array())
error($message='', $data=array())
increase($message='', $data=array(), $amount=1)
setLimit($limit)
setData($data=array())
static file_put_contents($filename, $data, $flags=0, $context=null)
static file_exists($file)
static mkdir($dir, $mode=0755, $recursive=true)
Definition: Ego_System.php:669
static file_get_contents($filename, $utf8=true, $context=null)