HtmlFormat.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2017 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 
19 
20 /**
21  * HtmlFormat implements the HTML request/response format. Since all data
22  * from the external representation arrives in form fields, grouping of values
23  * has to be done via the field names. So Nodes are represented by their values
24  * whose field names are of the form value-`name`-`oid`. All of these
25  * values will be removed from the request and replaced by Node instances
26  * representing the data. Each node is stored under its object id in the data array.
27  *
28  * HtmlFormat serializes the response to a template, which will be rendered
29  * by the View implementation defined in the configuration section 'view'.
30  * The template is determined by the current action key. The response property
31  * 'html_tpl_format' allows to select a specific version of that template file. E.g.
32  * if the template would be home.tpl, setting the 'html_tpl_format' property
33  * to 'mobile' would select the template home-mobile.tpl. If a version does
34  * not exist, it is ignored and the default template is used.
35  *
36  * @author ingo herwig <ingo@wemove.com>
37  */
38 class HtmlFormat extends AbstractFormat {
39 
40  private static $inputFieldNameDelimiter = '-';
41  private static $sharedView = null;
42 
43  /**
44  * @see Format::getMimeType()
45  */
46  public function getMimeType() {
47  return 'text/html';
48  }
49 
50  /**
51  * @see Format::isCached()
52  */
53  public function isCached(Response $response) {
54  if (self::$sharedView == null) {
55  self::$sharedView = ObjectFactory::getInstance('view');
56  }
57  $templateFile = $this->getTemplateFile($response);
58  $cacheId = $response->getCacheId();
59  return self::$sharedView->isCached($templateFile, $cacheId);
60  }
61 
62  /**
63  * @see Format::getCacheDate()
64  */
65  public function getCacheDate(Response $response) {
66  if (self::$sharedView == null) {
67  self::$sharedView = ObjectFactory::getInstance('view');
68  }
69  $templateFile = $this->getTemplateFile($response);
70  $cacheId = $response->getCacheId();
71  return self::$sharedView->getCacheDate($templateFile, $cacheId);
72  }
73 
74  /**
75  * @see AbstractFormat::deserializeValues()
76  */
77  protected function deserializeValues(Request $request) {
78  // construct nodes from values serialized as form fields
79  // nodes are encoded in separated fields with names value-<name>-<oid>
80  $values = $request->getValues();
81  foreach ($values as $key => $value) {
82  $valueDef = self::getValueDefFromInputControlName($key);
83  if ($valueDef != null) {
84  $oidStr = $valueDef['oid'];
85  if (strlen($oidStr) > 0) {
86  $node = &$this->getNode($oidStr);
87  $node->setValue($valueDef['name'], $value);
88  unset($values[$key]);
89  $values[$oidStr] = $node;
90  }
91  }
92  }
93  return $values;
94  }
95 
96  /**
97  * @see AbstractFormat::serializeValues()
98  */
99  protected function serializeValues(Response $response) {
100  // create the view
101  $view = ObjectFactory::getInstance('view');
102 
103  // assign the response data to the view
104  $values = $response->getValues();
105  foreach ($values as $key => $value) {
106  $view->setValue($key, $value);
107  }
108 
109  // display the view
110  $templateFile = $this->getTemplateFile($response);
111  $cacheId = $response->getCacheId();
112  $view->render($templateFile, $cacheId);
113 
114  return $values;
115  }
116 
117  /**
118  * Get the object value definition from a HTML input field name.
119  * @param $name The name of input field in the format value-`name`-`oid`, where name is the name
120  * of the attribute belonging to the node defined by oid
121  * @return An associative array with keys 'oid', 'language', 'name' or null if the name is not valid
122  */
123  protected static function getValueDefFromInputControlName($name) {
124  if (!(strpos($name, 'value') == 0)) {
125  return null;
126  }
127  $def = [];
128  $fieldDelimiter = StringUtil::escapeForRegex(self::$inputFieldNameDelimiter);
129  $pieces = preg_split('/'.$fieldDelimiter.'/', $name);
130  if (sizeof($pieces) != 3) {
131  return null;
132  }
133  $ignore = array_shift($pieces);
134  $def['language'] = array_shift($pieces);
135  $def['name'] = array_shift($pieces);
136  $def['oid'] = array_shift($pieces);
137 
138  return $def;
139  }
140 
141  /**
142  * Get the template file for the given response
143  * @param $response
144  */
145  protected function getTemplateFile(Response $response) {
146  if (self::$sharedView == null) {
147  self::$sharedView = ObjectFactory::getInstance('view');
148  }
149  // check if a view template is defined
150  $viewTpl = self::$sharedView->getTemplate($response->getSender(),
151  $response->getContext(), $response->getAction());
152  if (!$viewTpl) {
153  throw new ConfigurationException("View definition missing for ".
154  "response: ".$response->__toString());
155  }
156 
157  // check for html_format property in response
158  // and add the suffix if existing
159  $viewTplFile = WCMF_BASE.$viewTpl;
160  $format = $response->getProperty('html_tpl_format');
161  if (isset($format)) {
162  $ext = pathinfo($viewTplFile, PATHINFO_EXTENSION);
163  $formatViewTplFile = dirname($viewTplFile).'/'.basename($viewTplFile, ".$ext").'-'.$format.'.'.$ext;
164  }
165 
166  // give precedence to format specific file
167  $finalTplFile = isset($formatViewTplFile) && file_exists($formatViewTplFile) ?
168  $formatViewTplFile : $viewTplFile;
169  return $finalTplFile;
170  }
171 }
172 ?>
Response holds the response values that are used as output from Controller instances.
Definition: Response.php:20
getProperty($name)
Get a property.
AbstractFormat is used as base class for specialized formats.
getTemplateFile(Response $response)
Get the template file for the given response.
Definition: HtmlFormat.php:145
getCacheId()
Get the cache id.
HtmlFormat implements the HTML request/response format.
Definition: HtmlFormat.php:38
getContext()
Get the name of the context.
static getValueDefFromInputControlName($name)
Get the object value definition from a HTML input field name.
Definition: HtmlFormat.php:123
getAction()
Get the name of the action.
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:278
Request holds the request values that are used as input to Controller instances.
Definition: Request.php:18
getSender()
Get the name of the sending Controller.
static getInstance($name, $dynamicConfiguration=[])
getValues()
Get all key value pairs.
ConfigurationException signals an exception in the configuration.