wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.NodeRDBMapper.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.NodeRDBMapper.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/model/mapper/class.RDBMapper.php");
20 require_once(BASE."wcmf/lib/persistence/class.PersistenceFacade.php");
21 require_once(BASE."wcmf/lib/persistence/converter/class.DataConverter.php");
22 require_once(BASE."wcmf/lib/model/class.Node.php");
23 
24 /**
25  * @class NodeRDBMapper
26  * @ingroup Mapper
27  * @brief NodeRDBMapper maps Node objects to a relational database schema where each Node
28  * type has its own table.
29  * It defines a persistence mechanism that specialized mappers customize by overriding
30  * the given template methods.
31  *
32  * @author ingo herwig <ingo@wemove.com>
33  */
34 class NodeRDBMapper extends RDBMapper
35 {
36  /**
37  * @see RDBMapper::createObject()
38  */
39  function &createObject($oid=null)
40  {
41  return new Node($this->getType(), $oid);
42  }
43  /**
44  * @see RDBMapper::appendObject()
45  */
46  function appendObject(&$object, &$dependendObject)
47  {
48  $object->addChild($dependendObject);
49  }
50  /**
51  * @see RDBMapper::applyDataOnLoad()
52  */
53  function applyDataOnLoad(&$object, $objectData, $attribs)
54  {
55  // we expect only one result row
56  $objectData['_data'] = $objectData['_data'];
57 
58  // set parent oids
59  $i = 0;
60  $parentoids = array();
61  while (array_key_exists('pid'.$i, $objectData['_data']))
62  {
63  if ($objectData['_data'][('pid'.$i)] !== null)
64  {
65  $parentoid = PersistenceFacade::composeOID(array('type' => $objectData['_data'][('ptype'.$i)], 'id' => array($objectData['_data'][('pid'.$i)])));
66  array_push($parentoids, $parentoid);
67  }
68  $i++;
69  }
70  $object->setProperty('parentoids', $parentoids);
71 
72  // set object data
73  foreach($objectData['_datadef'] as $dataItem)
74  {
75  if ($attribs == null || in_array($dataItem['name'], $attribs))
76  {
77  $value = $objectData['_data'][$dataItem['name']];
78  if (!is_null($value))
79  $value = $this->_dataConverter->convertStorageToApplication($value, $dataItem['db_data_type'], $dataItem['name']);
80  $object->setValue($dataItem['name'], $value, $dataItem['app_data_type']);
81  $valueProperties = array();
82  foreach($dataItem as $key => $value)
83  if ($key != 'name' && $key != 'app_data_type')
84  $valueProperties[$key] = $value;
85  $object->setValueProperties($dataItem['name'], $valueProperties, $dataItem['app_data_type']);
86  }
87  }
88  }
89  /**
90  * @see RDBMapper::applyDataOnCreate()
91  */
92  function applyDataOnCreate(&$object, $objectData, $attribs)
93  {
94  // set parent oids
95  $parentoids = array();
96  foreach($objectData['_parents'] as $parentDef)
97  {
98  $parentoid = PersistenceFacade::composeOID(array('type' => $parentDef['type'], 'id' => array(PersistenceFacade::getDummyId())));
99  array_push($parentoids, $parentoid);
100  }
101  $object->setProperty('parentoids', $parentoids);
102 
103  // set object data
104  foreach($objectData['_datadef'] as $dataItem)
105  {
106  if ($attribs == null || in_array($dataItem['name'], $attribs))
107  {
108  // don't override dummy ids
109  if (!$this->isPkValue($dataItem['name'], $dataItem['app_data_type']))
110  {
111  $value = $this->_dataConverter->convertStorageToApplication($dataItem['default'], $dataItem['db_data_type'], $dataItem['name']);
112  $object->setValue($dataItem['name'], $value, $dataItem['app_data_type']);
113  }
114  $valueProperties = array();
115  foreach($dataItem as $key => $value)
116  if ($key != 'name' && $key != 'app_data_type')
117  $valueProperties[$key] = $value;
118  $object->setValueProperties($dataItem['name'], $valueProperties, $dataItem['app_data_type']);
119  }
120  }
121  }
122  /**
123  * @see RDBMapper::getObjectDefinition()
124  *
125  * The _datadef key holds the following structure:
126  *
127  * An array of assoziative arrays with the keys 'name', 'app_data_type' plus application specific keys for every data item.
128  * All keys except 'name' and 'app_data_type' will become keys in the objects valueProperties array hold for each data item while 'name'
129  * and 'app_data_type' are used to identify the values, see PersistentObject::getValue().
130  * (e.g. array('name' => 'title', 'db_data_type' => 'VARCHAR(255)', 'app_data_type' => DATATYPE_ATTRIBUTE, 'default' => 'Hello World!')) @n
131  * Known keys are:
132  * - @em db_data_type: The database data type of the attribute. This may be used to decide on value conversions in the assoziated DataConverter class
133  * - @em default: The default value (will be set when creating a blank object, see PersistenceMapper::create())
134  * - @em restrictions_match: A regular expression that the value must match (e.g. '[0-3][0-9]\.[0-1][0-9]\.[0-9][0-9][0-9][0-9]' for date values)
135  * - @em restrictions_not_match: A regular expression that the value must NOT match
136  * - @em is_editable: true, false whether the value should be editable, see FormUtil::getInputControl()
137  * - @em input_type: The HTML input type for the value, see FormUtil::getInputControl()
138  * - @em display_type: The HTML display type for the value, see NodeUtil::getDisplayValue()
139  */
141  {
142  WCMFException::throwEx("getObjectDefinition() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
143  }
144 }
145 ?>
RDBMapper maps objects of one type to a relational database schema. It defines a persistence mechanis...
Node is the basic component for building trees (although a Node can have one than more parents)...
Definition: class.Node.php:118
throwEx($message, $file='', $line='')
& createObject($oid=null)
appendObject(&$object, &$dependendObject)
isPkValue($name, $dataType)
NodeRDBMapper maps Node objects to a relational database schema where each Node type has its own tabl...
applyDataOnLoad(&$object, $objectData, $attribs)
applyDataOnCreate(&$object, $objectData, $attribs)