CMSimple_XH 開発者ドキュメント
PageDataRouter.php
1 <?php
15 namespace XH;
16 
29 {
35  private $model;
36 
42  private $currentInterests = array();
43 
52  public function __construct(array $h, array $pageDataFields, array $tempData, array $pageData)
53  {
54  $this->model = new PageDataModel($h, $pageDataFields, $tempData, $pageData);
55  }
56 
64  public function storedFields()
65  {
66  return $this->model->storedFields();
67  }
68 
78  public function getCurrentInterests()
79  {
80  return $this->currentInterests;
81  }
82 
90 // @codingStandardsIgnoreStart
91  public function add_interest($field)
92  {
93 // @codingStandardsIgnoreEnd
94  if (!in_array($field, $this->model->params)) {
95  $this->model->addParam($field);
96  }
97  $this->currentInterests[] = $field;
98  }
99 
110  public function removeInterest($field)
111  {
112  $this->model->removeParam($field);
113  $n = array_search($field, $this->currentInterests);
114  if ($n !== false) {
115  array_splice($this->currentInterests, $n, 1);
116  }
117  }
118 
128 // @codingStandardsIgnoreStart
129  public function add_tab($tab_name, $tab_view, $cssClass = null)
130  {
131 // @codingStandardsIgnoreEnd
132  $this->model->addTab($tab_name, $tab_view, $cssClass);
133  }
134 
142  public function refresh(array $data = null)
143  {
144  return $this->model->refresh($data);
145  }
146 
154 // @codingStandardsIgnoreStart
155  public function find_page($id)
156  {
157 // @codingStandardsIgnoreEnd
158  $page = $this->model->findKey($id);
159  return $page;
160  }
161 
167 // @codingStandardsIgnoreStart
168  public function find_all()
169  {
170 // @codingStandardsIgnoreEnd
171  return $this->model->data;
172  }
173 
181 // @codingStandardsIgnoreStart
182  public function new_page(array $params = array())
183  {
184 // @codingStandardsIgnoreEnd
185  $page = $this->model->create($params);
186  return $page;
187  }
188 
198  public function appendNewPage(array $params = array())
199  {
200  $pageData = $this->model->create($params);
201  $this->model->appendPage($pageData);
202  }
203 
212  public function insert(array $pages, $index)
213  {
214  return $this->model->replace($pages, $index);
215  }
216 
224  public function destroy($key)
225  {
226  return $this->model->delete($key);
227  }
228 
241 // @codingStandardsIgnoreStart
242  public function find_field_value($field, $value, $separator = null)
243  {
244 // @codingStandardsIgnoreEnd
245  if ($separator) {
246  $results = $this->model->findArrayfieldValue($field, $value, $separator);
247  return $results;
248  }
249  $results = $this->model->findFieldValue($field, $value);
250  return $results;
251  }
252 
265 // @codingStandardsIgnoreStart
266  public function find_field_value_sortkey($field, $value, $sortKey,
267  $sortFlag = null, $sep = null
268  ) {
269 // @codingStandardsIgnoreEnd
270  $results = $this->model->findFieldValueSortkey($field, $value, $sortKey, $sortFlag, $sep);
271  return $results;
272  }
273 
283 // @codingStandardsIgnoreStart
284  public function refresh_from_texteditor(array $headings, $index)
285  {
286 // @codingStandardsIgnoreEnd
287  if (count($headings) == 0) {
288  /*
289  * Current page has been deleted:
290  * Store it temporary, maybe the user
291  * wants to paste it in somewhere again,
292  * and remove it from the page infos
293  */
294  $this->keep_in_mind($index);
295  return $this->destroy($index);
296  } elseif (count($headings) > 1) {
297  /*
298  * At least one page was inserted:
299  * Create an array of the new pages
300  * and insert it into the page data
301  */
302  $new_pages = array();
303  $current_page = $this->find_page($index);
304  foreach ($headings as $heading) {
305  $url = trim(xh_rmws(strip_tags($heading)));
306  $url = uenc($url);
307 
308  switch ($url) {
309  case $current_page['url']:
310  /*
311  * Keeping the current page data:
312  * this attempt fails, if NEW pages are
313  * added AND current heading was CHANGED
314  */
315  foreach ($current_page as $field => $value) {
316  $params[$field] = $value;
317  }
318  break;
319  case $this->model->temp_data['url']:
320  /*
321  * This is the 'url' of the recently deleted
322  * page. Most probably it was just pasted in
323  * again. So don't be shy, get the old infos
324  * for this new page
325  */
326  foreach ($this->model->temp_data as $field => $value) {
327  $params[$field] = $value;
328  }
329  break;
330  default:
331  /*
332  * The 'url' is used for ... look right above
333  */
334  $params['url'] = $url;
335  break;
336  }
337  $params['last_edit'] = time();
338  $new_pages[] = $params;
339  $params = array();
340  }
341  return $this->model->replace($new_pages, $index);
342  } elseif (count($headings) == 1) {
343  /*
344  * The heading may have changed, stay up to date.
345  */
346  $url = trim(xh_rmws(strip_tags($headings[0])));
347  $params['url'] = uenc($url);
348  $params['last_edit'] = time();
349  return $this->update($index, $params);
350  }
351  }
352 
361  public function update($s, array $params)
362  {
363  $update_params = array();
364  foreach ($params as $field => $update) {
365  if (in_array($field, $this->model->params)) {
366  $update_params[$field] = $update;
367  }
368  }
369  return $this->model->updateKey($s, $params);
370  }
371 
385 // @codingStandardsIgnoreStart
386  public function create_tabs($s)
387  {
388 // @codingStandardsIgnoreElse
389  global $edit, $f, $o, $su, $xh_publisher;
390 
391  if (is_array($this->model->tabs)
392  && count($this->model->tabs) > 0 && $edit
393  ) {
394  if ($s == -1 && !$f && $o == '' && $su == '') { // Argh! :(
395  $pd_s = $xh_publisher->getFirstPublishedPage();
396  } else {
397  $pd_s = $s;
398  }
399  $page = $this->find_page($pd_s);
400  if ($pd_s > -1) {
401  $view_provider = new PageDataView($page, $this->model->tabs);
402  return $view_provider->tabs() . $view_provider->views();
403  }
404  }
405  return '';
406  }
407 
415 // @codingStandardsIgnoreStart
416  private function keep_in_mind($pd_s)
417  {
418 // @codingStandardsIgnoreEnd
419  $page = $this->find_page($pd_s);
420  $this->model->storeTemp($page);
421  }
422 
430  public function headAsPHP()
431  {
432  $flds = array();
433  foreach ($this->model->params as $param) {
434  $flds[] = "'" . addcslashes($param, '\'\\') . "'";
435  }
436  $o = "<?php\n\$page_data_fields=array("
437  . implode(',', $flds)
438  . ");\n";
439  $flds = array();
440  foreach ($this->model->temp_data as $key => $val) {
441  $escval = addcslashes($val, '\'\\');
442  $flds[] = "'$key'=>'$escval'";
443  }
444  $o .= "\$temp_data=array(\n"
445  . implode(",\n", $flds)
446  . "\n);\n?>\n";
447  return $o;
448  }
449 
459  public function pageAsPHP($id)
460  {
461  $data = $this->find_page($id);
462  $flds = array();
463  foreach ($data as $key => $val) {
464  $escval = addcslashes($val, '\'\\');
465  $flds[] = "'$key'=>'$escval'";
466  }
467  $o = "<?php\n\$page_data[]=array(\n"
468  . implode(",\n", $flds)
469  . "\n);\n?>\n";
470  return $o;
471  }
472 }
$xh_publisher
Definition: cms.php:940
if(XH_ADM) $pd_s
Definition: cms.php:1058
__construct(array $h, array $pageDataFields, array $tempData, array $pageData)
$su
Definition: cms.php:778
new_page(array $params=array())
$s
Definition: cms.php:953
$f
Definition: cms.php:836
find_field_value_sortkey($field, $value, $sortKey, $sortFlag=null, $sep=null)
insert(array $pages, $index)
$o
Definition: cms.php:113
$h
Definition: cms.php:977
update($s, array $params)
appendNewPage(array $params=array())
refresh(array $data=null)
find_field_value($field, $value, $separator=null)
uenc($s)
Definition: functions.php:1009
refresh_from_texteditor(array $headings, $index)
$edit
Definition: cms.php:602
add_tab($tab_name, $tab_view, $cssClass=null)