HtmlFormat.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
17 
18 /**
19  * HtmlFormat realizes the HTML request/response format. Since all data
20  * from the external representation arrives in form fields, grouping of values
21  * has to be done via the field names. So Nodes are represented by their values
22  * whose field names are of the form value-`name`-`oid`. All of these
23  * values will be removed from the request and replaced by Node instances
24  * representing the data. Each node is stored under its object id in the data array.
25  *
26  * HtmlFormat serializes the response to a template, which will be rendered
27  * by the View implementation defined in the configuration section 'view'.
28  * The template is determined by the current action key. The response property
29  * 'html_tpl_format' allows to select a specific version of that template file. E.g.
30  * if the template would be home.tpl, setting the 'html_tpl_format' property
31  * to 'mobile' would select the template home-mobile.tpl. If a version does
32  * not exist, it is ignored and the default template is used.
33  *
34  * @author ingo herwig <ingo@wemove.com>
35  */
36 class HtmlFormat extends AbstractFormat {
37 
38  private static $_inputFieldNameDelimiter = '-';
39 
40  /**
41  * @see Format::getMimeType()
42  */
43  public function getMimeType() {
44  return 'text/html';
45  }
46 
47  /**
48  * @see AbstractFormat::deserializeValues()
49  */
50  protected function deserializeValues($values) {
51  // construct nodes from values serialized as form fields
52  // nodes are encoded in separated fields with names value-<name>-<oid>
53  foreach ($values as $key => $value) {
54  $valueDef = self::getValueDefFromInputControlName($key);
55  if ($valueDef != null) {
56  $oidStr = $valueDef['oid'];
57  if (strlen($oidStr) > 0) {
58  $node = &$this->getNode($oidStr);
59  $node->setValue($valueDef['name'], $value);
60  unset($values[$key]);
61  $values[$oidStr] = $node;
62  }
63  }
64  }
65  return $values;
66  }
67 
68  /**
69  * @see AbstractFormat::serializeValues()
70  */
71  protected function serializeValues($values) {
72  // create the view
73  $view = ObjectFactory::getInstance('view');
74 
75  // check if a view template is defined
76  $response = $this->getResponse();
77  $viewTpl = $view->getTemplate($response->getSender(),
78  $response->getContext(), $response->getAction());
79  if (!$viewTpl) {
80  throw new ConfigurationException("View definition missing for ".
81  "response: ".$response->__toString());
82  }
83 
84  // assign the response data to the view
85  foreach ($values as $key => $value) {
86  $view->setValue($key, $value);
87  }
88 
89  // check for html_format property in response
90  // and add the suffix if existing
91  $viewTplFile = WCMF_BASE.$viewTpl;
92  $format = $response->getProperty('html_tpl_format');
93  if (isset($format)) {
94  $ext = pathinfo($viewTplFile, PATHINFO_EXTENSION);
95  $formatViewTplFile = dirname($viewTplFile).'/'.basename($viewTplFile, ".$ext").'-'.$format.'.'.$ext;
96  }
97 
98  // give precedence to format specific file
99  $finalTplFile = isset($formatViewTplFile) && file_exists($formatViewTplFile) ?
100  $formatViewTplFile : $viewTplFile;
101 
102  // display the view
103  $view->render($finalTplFile, $response->getCacheId());
104  return $values;
105  }
106 
107  /**
108  * Get the object value definition from a HTML input field name.
109  * @param $name The name of input field in the format value-`name`-`oid`, where name is the name
110  * of the attribute belonging to the node defined by oid
111  * @return An associative array with keys 'oid', 'language', 'name' or null if the name is not valid
112  */
113  protected static function getValueDefFromInputControlName($name) {
114  if (!(strpos($name, 'value') == 0)) {
115  return null;
116  }
117  $def = array();
118  $fieldDelimiter = StringUtil::escapeForRegex(self::$_inputFieldNameDelimiter);
119  $pieces = preg_split('/'.$fieldDelimiter.'/', $name);
120  if (sizeof($pieces) != 3) {
121  return null;
122  }
123  $ignore = array_shift($pieces);
124  $def['language'] = array_shift($pieces);
125  $def['name'] = array_shift($pieces);
126  $def['oid'] = array_shift($pieces);
127 
128  return $def;
129  }
130 }
131 ?>
AbstractFormat maybe used as base class for specialized formats.
HtmlFormat realizes the HTML request/response format.
Definition: HtmlFormat.php:36
static getInstance($name, $dynamicConfiguration=array())
static getValueDefFromInputControlName($name)
Get the object value definition from a HTML input field name.
Definition: HtmlFormat.php:113
static escapeForRegex($string)
Escape characters of a string for use in a regular expression Code from http://php.net/manual/de/function.preg-replace.php.
Definition: StringUtil.php:282
getResponse()
Get the currently deserialized response.
ConfigurationException signals an exception in the configuration.