dbXapp 2.0
RAD, CMS, Module und Runtime-IDE fuer dbXapp
Loading...
Searching...
No Matches
dbxProcess.class.php
Go to the documentation of this file.
1<?php
2
3class dbxProcess extends dbxObj {
4
5 protected $_process_remember_modul = 'dbxProcess';
6
7 public function clear($section='') {
8 if (!$section) $section=$this->_section;
9 if ( $section) {
10 $this->del_property('*',$section);
11 } else {
12 $this->_properties=array();
13 }
14 }
15
16
17 public function add_norep($content) {
18 $oTPL = dbx()->get_system_obj('dbxTPL');
19 if (is_object($oTPL) && method_exists($oTPL, 'cleanup_optional_placeholders')) {
20 $content = $oTPL->cleanup_optional_placeholders((string)$content);
21 }
22
23 if (isset($_SESSION['dbx']['norep'])) {
24 $xnorep=$_SESSION['dbx']['norep'];
25 if (is_array($xnorep)) {
26 for($i=0; $i < 2; $i++) { // noreps can include noraps
27 foreach ($xnorep as $id => $norep) {
28 $xid= '['.$id.']';
29 $content = str_replace($xid,$norep,$content);
30 }
31 }
32 }
33 }
34 return $content;
35 }
36
37 protected function job_key($key) {
38 $key = preg_replace('/[^A-Za-z0-9_.:-]+/', '_', (string)$key);
39 $key = trim($key, '_');
40 return (strpos($key, 'dbxprocess_') === 0) ? $key : 'dbxprocess_' . $key;
41 }
42
43 public function init_job($key, $title = '', $tasks = array(), $meta = array()) {
44 $jobKey = $this->job_key($key);
45 $state = is_array($meta) ? $meta : array();
46
47 $state['proc_key'] = $jobKey;
48 $state['title'] = $title;
49 $state['tasks'] = is_array($tasks) ? array_values($tasks) : array();
50 $state['status'] = $state['status'] ?? 'running';
51 $state['percent'] = $state['percent'] ?? 0;
52 $state['step_percent'] = $state['step_percent'] ?? 0;
53 $state['task_pos'] = $state['task_pos'] ?? 0;
54 $state['step_pos'] = $state['step_pos'] ?? 0;
55 $state['message'] = $state['message'] ?? '';
56 $state['started_at'] = $state['started_at'] ?? date('Y-m-d H:i:s');
57 $state['updated_at'] = date('Y-m-d H:i:s');
58
59 $this->set_job($jobKey, $state);
60 return $state;
61 }
62
63 public function get_job($key) {
64 $state = dbx()->get_remember_var($this->job_key($key), array(), $this->_process_remember_modul);
65 return is_array($state) ? $state : array();
66 }
67
68 public function set_job($key, $state) {
69 if (!is_array($state)) {
70 $state = array();
71 }
72
73 $jobKey = $this->job_key($key);
74 $state['proc_key'] = $state['proc_key'] ?? $jobKey;
75 $state['updated_at'] = date('Y-m-d H:i:s');
76 dbx()->set_remember_var($jobKey, $state, $this->_process_remember_modul);
77 return $state;
78 }
79
80 public function clear_job($key) {
81 dbx()->set_remember_var($this->job_key($key), array(), $this->_process_remember_modul);
82 }
83
84 public function control_job($key, $cmd) {
85 $jobKey = $this->job_key($key);
86 $cmd = strtolower(trim((string)$cmd));
87
88 if ($cmd == 'restart') {
89 $this->clear_job($jobKey);
90 return array(
91 'proc_key' => $jobKey,
92 'status' => 'reset',
93 'message' => 'process restarted',
94 'percent' => 0,
95 'step_percent' => 0,
96 'updated_at' => date('Y-m-d H:i:s'),
97 );
98 }
99
100 $state = $this->get_job($jobKey);
101 if (!$state) {
102 $state = array(
103 'proc_key' => $jobKey,
104 'status' => 'new',
105 'message' => 'no active state',
106 'percent' => 0,
107 'step_percent' => 0,
108 );
109 }
110
111 $status = $state['status'] ?? 'new';
112
113 if ($cmd == 'pause' && !in_array($status, array('finished', 'error', 'canceled'), true)) {
114 $state['status'] = 'paused';
115 $state['paused_at'] = date('Y-m-d H:i:s');
116 $state['message'] = 'process paused';
117 } elseif (($cmd == 'resume' || $cmd == 'continue') && in_array($status, array('paused', 'canceled', 'new'), true)) {
118 $state['status'] = 'running';
119 $state['resumed_at'] = date('Y-m-d H:i:s');
120 $state['message'] = ($cmd == 'continue') ? 'process continued' : 'process resumed';
121 } elseif ($cmd == 'cancel' && !in_array($status, array('finished', 'error'), true)) {
122 $state['status'] = 'canceled';
123 $state['canceled_at'] = date('Y-m-d H:i:s');
124 $state['message'] = 'process canceled';
125 }
126
127 return $this->set_job($jobKey, $state);
128 }
129
130
131 public function fast_response($response,$interpreter=1) {
132 if ($interpreter) {
133 $oInterpreter=dbx()->get_system_obj("dbxInterpreter");
134 $response=$oInterpreter->run($response);
136 }
137 echo '<br><br><br>'.$response;
138 exit;
139 }
140
141
142
143 public function process() {
144 $empty=array();
145 $process =$this->_section;
146 $status =$this->get_property('status','next');
147 $processes=$this->get_property('processes',$empty);
148 $stepp =$this->get_property('stepp',-1);
149 $count =count($processes);
150
151
152
153
154 if ($status=='next') {
155 $stepp++;
156 $this->set_property('stepp',$stepp);
157 }
158 if ($stepp > $count) {
159 $status='end';
160 $stepp=$count;
161 }
162
163 //$content= "A-Count=($count) stepp=($stepp) status=($status) Process=($process)-B<br>";
164 $content='End';
165 if (isset($processes[$stepp])) $content=$processes[$stepp];
166
167 if ($status=='end') {
168 //dbx_debug("ENDE-Process");
169 $this->clear($this->_section);
170 }
171 dbx()->debug("process ($process) return=($content)");
172
173 return $content;
174 } //
175
176
177
178
179
180
181 public function add($process) {
182 $empty=array();
183 $processes=$this->get_property('processes',$empty);
184 $processes[]=$process;
185 $this->set_property('processes',$processes);
186 }
187
188
189
190
191 public function get_status() {
192 return $this->get_property('status','next');
193 }
194
195
196 public function init($section) {
197 $this->_section=$section;
198 $this->clear($section);
199 }
200
201 public function run($process='',$content=1,$fast_response=1) {
202 if (!$process) $process=$this->_section;
203 $this->_section=($process);
204 if ($content) {
205 $content =$this->process();
206 $processes=$this->get_property('processes');
207 $status =$this->get_status();
208 dbx()->debug("#Prozess Prozess=($process) Status=($status) content=($content) status=($status)",$processes);
209 if ($fast_response) $this->fast_response($content);
210 return $content;
211 } else {
212 $ok=$this->get_status();
213 return $ok;
214 }
215 }
216}
Gemeinsame Basisklasse fuer dbXapp-System-, Modul- und Include-Objekte.
Definition dbxKernel.php:63
get_property($name, $default='', $section='', $modul='modul')
Liest eine Objekt- oder Session-Property.
set_property($name, $value, $section='', $modul='modul')
Speichert eine Objekt- oder Session-Property.
Public $_section
Definition dbxKernel.php:66
del_property($name, $section='', $modul='modul')
Loescht eine Objekt- oder Session-Property.
set_job($key, $state)
clear($section='')
add_norep($content)
fast_response($response, $interpreter=1)
run($process='', $content=1, $fast_response=1)
init_job($key, $title='', $tasks=array(), $meta=array())
control_job($key, $cmd)
exit
Definition index.php:532
$response
Definition index.php:518
DBX schema administration.