00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 class PageData {
00013
00014
00015
00016
00017
00018 public $content = '';
00019
00020
00021
00022
00023
00024 public $head;
00025
00026
00027
00028
00029
00030 public $breadcrumb;
00031
00032
00033
00034
00035
00036 public $status;
00037
00038
00039
00040
00041
00042 public $status_code = CONTROLLER_OK;
00043
00044
00045
00046
00047
00048 public $pathstack = null;
00049
00050
00051
00052
00053
00054 public $blocks = array();
00055
00056
00057
00058
00059
00060 public $in_history = true;
00061
00062
00063
00064
00065
00066 public $router = null;
00067
00068
00069
00070
00071
00072 private $get = null;
00073
00074
00075
00076
00077
00078 private $post = null;
00079
00080
00081
00082
00083
00084 protected $cache_manager = null;
00085
00086
00087
00088
00089
00090 protected $render_decorator_classes = array();
00091
00092
00093
00094 public $page_template;
00095
00096
00097
00098
00099
00100
00101
00102
00103 public function __construct($cache_manager, $get, $post) {
00104 $this->page_template = Config::get_value(Config::PAGE_TEMPLATE);
00105 $this->status = Status::restore();
00106 $this->head = new HeadData();
00107
00108
00109
00110 if (empty($cache_manager)) { $cache_manager = new NoCacheCacheManager(); }
00111 $this->set_cache_manager($cache_manager);
00112 $this->pathstack = new PathStack();
00113
00114
00115 $tmp = Arr::force($get);
00116 array_walk_recursive($tmp, array($this, 'trim_array_content'));
00117
00118 unset($tmp[Config::get_value(Config::QUERY_PARAM_PATH_INVOKED)]);
00119 $this->get = new TracedArray($tmp);
00120
00121
00122 $tmp = Arr::force($post);
00123 array_walk_recursive($tmp, array($this, 'trim_array_content'));
00124 $this->post = new TracedArray($this->preprocess_post($tmp));
00125 }
00126
00127 public function __clone() {
00128
00129
00130 $this->cache_manager = clone($this->cache_manager);
00131 $this->get = clone($this->get);
00132 $this->post = clone($this->post);
00133 $this->head = clone($this->head);
00134 $this->pathstack = clone($this->pathstack);
00135 if ($this->status) {
00136 $this->status = clone($this->status);
00137 }
00138 }
00139
00140
00141
00142
00143
00144
00145
00146 private function trim_array_content(&$item, $key) {
00147 $item = trim($item);
00148 }
00149
00150
00151
00152
00153
00154
00155 private function preprocess_post($arr) {
00156 $arr = $this->clear_button_parameters($arr);
00157 $arr = $this->merge_uploaded_files($arr);
00158 return $arr;
00159 }
00160
00161
00162
00163
00164
00165
00166 private function merge_uploaded_files($arr) {
00167
00168
00169
00170
00171 $ret = array();
00172 foreach($_FILES as $name => $file_data) {
00173 $tmp = array();
00174 foreach($file_data as $key => $value) {
00175 if (is_array($value)) {
00176 array_walk_recursive($value, array($this, 'callback_transform_files'), $key);
00177 $value = Arr::force_keys_to_string($value);
00178 $tmp = array_merge_recursive($tmp, $value);
00179 }
00180 else {
00181 $tmp[$key] = $value;
00182 }
00183 }
00184 $ret[$name] = $tmp;
00185 }
00186 $ret = Arr::unforce_keys_from_string($ret);
00187 return array_merge_recursive($arr, $ret);
00188 }
00189
00190 private function callback_transform_files(&$item, &$key, $userdata = false) {
00191 $item = array($userdata => $item);
00192 }
00193
00194
00195
00196
00197
00198 private function clear_button_parameters($arr) {
00199 $candidates = array();
00200 foreach($arr as $key => $value) {
00201
00202 if (String::ends_with($key, '_x')) {
00203 $newkey = String::substr($key, 0, String::length($key) - 2);
00204 if (array_key_exists($newkey . '_y', $this->post)) {
00205 $candidates[$newkey] = 'computed';
00206 }
00207 }
00208 }
00209
00210 foreach ($candidates as $key => $value) {
00211 unset($arr[$key . '_x']);
00212 unset($arr[$key . '_y']);
00213 }
00214
00215 return array_merge($arr, $candidates);
00216 }
00217
00218
00219
00220
00221
00222
00223 public function get_post() {
00224 return $this->post;
00225 }
00226
00227
00228
00229
00230
00231
00232 public function get_get() {
00233 return $this->get;
00234 }
00235
00236
00237
00238
00239
00240
00241 public function has_post_data() {
00242 return $this->post->count() > 0;
00243 }
00244
00245
00246
00247
00248
00249
00250 public function raw_request_body() {
00251
00252 $ret = trim(file_get_contents('php://input'));
00253 return $ret;
00254 }
00255
00256
00257
00258
00259
00260
00261 public function get_cache_manager() {
00262 return $this->cache_manager;
00263 }
00264
00265
00266
00267
00268
00269
00270 public function set_cache_manager($cache_manager) {
00271 if (!empty($cache_manager)) {
00272 $this->cache_manager = $cache_manager;
00273 $this->cache_manager->initialize($this);
00274 }
00275 }
00276
00277
00278
00279
00280
00281
00282 public function add_render_decorator_class($dec) {
00283 $this->render_decorator_classes[] = $dec;
00284 }
00285
00286
00287
00288
00289
00290
00291
00292 public function get_render_decorators($route) {
00293 $ret = array();
00294 foreach($this->render_decorator_classes as $class) {
00295 $ret[] = new $class($route);
00296 }
00297 return $ret;
00298 }
00299
00300
00301
00302
00303
00304
00305 public function set_path($path) {
00306 $this->pathstack->set_path($path);
00307 }
00308
00309
00310
00311
00312
00313
00314 public function get_pathstack() {
00315 return $this->pathstack;
00316 }
00317
00318
00319
00320
00321
00322
00323 public function add_block($block, $position = false, $weight = false) {
00324 if ($block->is_valid()) {
00325 if (!empty($position)) {
00326 $block->set_position($position);
00327 }
00328 if (!empty($weight)) {
00329 $block->set_index($weight);
00330 }
00331 $this->blocks[] = $block;
00332 }
00333 }
00334
00335
00336
00337
00338 public function sort_blocks() {
00339 if (function_exists('gyro_block_sort')) {
00340 usort($this->blocks, 'gyro_block_sort');
00341 }
00342 }
00343
00344
00345
00346
00347
00348
00349 public function get_blocks($position = false) {
00350 $ret = array();
00351 foreach ($this->blocks as $block) {
00352 if ($position && $position != $block->get_position()) {
00353 continue;
00354 }
00355 $ret[] = $block;
00356 }
00357 return $ret;
00358 }
00359
00360
00361
00362
00363
00364
00365 public function error($msg) {
00366 if (empty($this->status)) {
00367 $this->status = new Status();
00368 }
00369 if ($msg instanceof Status) {
00370 $this->status->merge($msg);
00371 }
00372 else {
00373 $this->status->append($msg);
00374 }
00375 }
00376
00377
00378
00379
00380
00381
00382 public function message($msg) {
00383 if ($msg instanceof Message) {
00384 $this->status = $msg;
00385 }
00386 else {
00387 $this->status = new Message(strval($msg));
00388 }
00389 }
00390
00391
00392
00393
00394
00395
00396 public function successful() {
00397 return empty($this->status_code) || $this->status_code == CONTROLLER_OK;
00398 }
00399 }
00400