CMSimple_XH 開発者ドキュメント
PageDataModel.php
1 <?php
2 
16 namespace XH;
17 
29 {
35  private $headings;
36 
42  public $params;
43 
49  public $data;
50 
56  public $temp_data;
57 
63  public $tabs;
64 
73  public function __construct(array $h, array $pageDataFields, array $tempData, array $pageData)
74  {
75  $this->headings = $h;
76  $this->params = !empty($pageDataFields)
77  ? $pageDataFields
78  : array('url', 'last_edit');
79  $this->temp_data = $tempData;
80  $this->data = $pageData;
81  $this->fixUp();
82  }
83 
91  public function storedFields()
92  {
93  $fields = $this->params;
94  $fields = array_merge($fields, array_keys($this->temp_data));
95  foreach ($this->data as $page) {
96  $fields = array_merge($fields, array_keys($page));
97  }
98  $fields = array_values(array_unique($fields));
99  return $fields;
100  }
101 
110  private function fixUp()
111  {
112  global $pd_s, $pd_current;
113 
114  foreach ($this->headings as $id => $value) {
115  foreach ($this->params as $param) {
116  if (!isset($this->data[$id][$param])) {
117  switch ($param) {
118  case 'url':
119  $this->data[$id][$param] = uenc(strip_tags($value));
120  break;
121  default:
122  $this->data[$id][$param] = '';
123  }
124  }
125  }
126  }
127  if (isset($pd_current)) {
128  $pd_current = $this->data[$pd_s];
129  }
130  }
131 
139  public function refresh(array $data = null)
140  {
141  if (isset($data)) {
142  $this->data = $data;
143  return $this->save();
144  }
145  return false;
146  }
147 
155  public function addParam($field)
156  {
157  $this->params[] = $field;
158  $this->fixUp();
159  }
160 
168  public function removeParam($field)
169  {
170  $n = array_search($field, $this->params);
171  array_splice($this->params, $n, 1);
172  foreach (array_keys($this->headings) as $id) {
173  unset($this->data[$id][$field]);
174  }
175  unset($this->temp_data[$field]);
176  }
177 
187  public function addTab($title, $view_file, $cssClass = null)
188  {
189  $this->tabs[$title] = array($view_file, $cssClass);
190  }
191 
199  public function findKey($key)
200  {
201  return $key >= 0 && $key < count($this->data)
202  ? $this->data[$key] : null;
203  }
204 
213  public function findFieldValue($field, $value)
214  {
215  $results = array();
216  foreach ($this->data as $id => $page) {
217  if (isset($page[$field])
218  && strpos($page[$field], $value) !== false
219  ) {
220  $results[$id] = $page;
221  }
222  }
223  return $results;
224  }
225 
235  public function findArrayfieldValue($field, $value, $separator)
236  {
237  $results = array();
238  foreach ($this->data as $id => $page) {
239  $array = explode($separator, $page[$field]);
240 
241  foreach ($array as $page_data) {
242  if ($value == trim($page_data)) {
243  $results[$id] = $page;
244  }
245  }
246  }
247  return $results;
248  }
249 
262  public function findFieldValueSortkey($field, $value, $sortKey, $sortFlag, $sep)
263  {
264  if ($sep) {
265  $results = $this->findArrayfieldValue($field, $value, $sep);
266  } else {
267  $results = $this->findFieldValue($field, $value);
268  }
269  $temp = array();
270  $ids = array();
271  foreach ($results as $key => $value) {
272  $temp[] = $value[$sortKey];
273  $ids[] = $key;
274  }
275  array_multisort($temp, $sortFlag, $ids);
276  $results = array();
277  if (is_array($ids) && count($ids) > 0) {
278  foreach ($ids as $id) {
279  $results[$id] = $this->data[$id];
280  }
281  }
282  return $results;
283  }
284 
292  public function create(array $params = array())
293  {
294  $clean = array();
295  foreach ($this->params as $field) {
296  $clean[$field] = '';
297  }
298  $page = array_merge($clean, $params);
299  return $page;
300  }
301 
311  public function appendPage(array $params)
312  {
313  $this->data[] = $params;
314  }
315 
324  public function replace(array $pages, $index)
325  {
326  array_splice($this->data, $index, 1, $pages);
327  return $this->save();
328  }
329 
337  public function storeTemp(array $page)
338  {
339  foreach ($page as $field => $value) {
340  if (in_array($field, $this -> params)) {
341  $this->temp_data[$field] = $value;
342  }
343  }
344  }
345 
353  public function delete($key)
354  {
355  array_splice($this->data, $key, 1);
356  return $this->save();
357  }
358 
367  public function updateKey($key, array $params)
368  {
369  foreach ($params as $field => $value) {
370  $this->data[$key][$field] = $value;
371  }
372  return $this->save();
373  }
374 
380  private function save()
381  {
382  return XH_saveContents();
383  }
384 }
findFieldValueSortkey($field, $value, $sortKey, $sortFlag, $sep)
if(XH_ADM) $pd_s
Definition: cms.php:1058
XH_saveContents()
Definition: adminfuncs.php:928
refresh(array $data=null)
$title
Definition: cms.php:100
findArrayfieldValue($field, $value, $separator)
create(array $params=array())
replace(array $pages, $index)
findFieldValue($field, $value)
$temp
Definition: cms.php:182
updateKey($key, array $params)
$h
Definition: cms.php:977
addTab($title, $view_file, $cssClass=null)
$pd_current
Definition: cms.php:1069
storeTemp(array $page)
__construct(array $h, array $pageDataFields, array $tempData, array $pageData)
uenc($s)
Definition: functions.php:1009
appendPage(array $params)