wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.Table.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.Table.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/util/class.Message.php");
20 require_once(BASE."wcmf/lib/persistence/class.PersistentObject.php");
21 require_once(BASE."wcmf/lib/persistence/class.PersistenceFacade.php");
22 
23 /**
24  * @class Table
25  * @ingroup Model
26  * @brief Table is a PersistentObject that holds a table structure.
27  * The rows of the table correspond to the value names of the PersistentObject
28  * and the columns to the datatypes. So if you want to set the value of
29  * row $i and column $j to $value call $table->setValue($i, $value, $j).
30  * To retrieve the corresponding value call $value = $table->getValue($i, $j).
31  *
32  * @author ingo herwig <ingo@wemove.com>
33  */
34 class Table extends PersistentObject
35 {
36  var $_valueProperties = array();
37  /**
38  * Constructor.
39  * @param type The Tables type.
40  * @param oid The Tables oid (, optional will be calculated if not given or not valid).
41  */
42  function Table($type, $oid=null)
43  {
44  $this->_type = $type;
45  if (!(isset($oid)) || !PersistenceFacade::isValidOID($oid))
46  {
47  // no oid is given -> new node
48  $id = md5(uniqid(ip2long($_SERVER['REMOTE_ADDR']) ^ (int)$_SERVER['REMOTE_PORT'] ^ @getmypid() ^ @disk_free_space('/tmp'), 1));
49  $this->_oid = PersistenceFacade::composeOID(array('type' => $this->_type, 'id' => $id));
50  $this->setState(STATE_NEW);
51  }
52  else
53  {
54  // old node
55  $this->_oid = $oid;
56  $this->setState(STATE_CLEAN);
57  }
58  }
59  /**
60  * Reset all row indizes to an ascending sequence starting by 0.
61  */
62  function reIndex()
63  {
64  foreach($this->getColumns() as $column)
65  $this->_data[$column] = array_slice($this->_data[$column], 0);
66  }
67  /**
68  * Get all row names of the table.
69  * @return An array of row names.
70  */
71  function getNumRows()
72  {
73  $columns = $this->getColumns();
74  return sizeof($this->getValueNames($columns[0]));
75  }
76  /**
77  * Get all column names of the table.
78  * @return An array of column names.
79  */
80  function getColumns()
81  {
82  return $this->getDataTypes();
83  }
84  /**
85  * Get all column values of a row.
86  * @param index The index of the row (zero based).
87  * @return An array of column values.
88  */
89  function getRow($index)
90  {
91  $row = array();
92  $columns = $this->getColumns();
93  for ($i=0; $i<sizeof($columns); $i++)
94  $row[$columns[$i]] = $this->getValue($index, $columns[$i]);
95  return $row;
96  }
97  /**
98  * Get all row values of a column.
99  * @param name The name of the cloumn.
100  * @return An array of row values.
101  */
102  function getColumn($name)
103  {
104  $column = array();
105  $numRows = $this->getNumRows();
106  for ($i=0; $i<$numRows; $i++)
107  $column[$i] = $this->getValue($i, $name);
108  return $column;
109  }
110  /**
111  * Insert a new row before a given row.
112  * @param index The index of the row before which the new row should be inserted (zero based) [maybe null].
113  * @param values An assoziative array with column name keys and values to insert.
114  * If the name is empty or does not exist the row will be appended to the end of the table.
115  * @note this method only works for numerical names.
116  */
117  function insertRow($index=null, $values=array())
118  {
119  $numRows = $this->getNumRows();
120  foreach($this->getColumns() as $column)
121  {
122  if ($index !== null && $index <= $numRows)
123  {
124  $firstRows = array_slice($this->_data[$column], 0, $index);
125  $lastRows = array_slice($this->_data[$column], $index);
126  array_push($firstRows, array('value' => $values[$column]));
127  $this->_data[$column] = array_merge($firstRows, $lastRows);
128  }
129  else
130  array_push($this->_data[$column], array('value' => $values[$column]));
131  }
132  $this->setState(STATE_DIRTY);
133  }
134  /**
135  * Remove a row.
136  * @param index The index of the row to remove (zero based).
137  * @note this method only works for numerical names.
138  */
139  function deleteRow($index)
140  {
141  foreach($this->getColumns() as $column)
142  array_splice($this->_data[$column], $index, 1);
143  $this->setState(STATE_DIRTY);
144  }
145  /**
146  * @see PersistenceObject::getValueProperties()
147  * We override this method because we only need to store the value properties once, not for every row.
148  * @note The type (= column name) must be given, the name (= row name) is ignored.
149  */
150  function getValueProperties($name, $type=null)
151  {
152  if ($type != null && is_array($this->_data[$type]))
153  return $this->_valueProperties[$type];
154  else
155  return null;
156  }
157  /**
158  * @see PersistenceObject::setValueProperties()
159  * We override this method because we only need to store the value properties once, not for every row.
160  * @note The type (= column name) must be given, the name (= row name) is ignored.
161  */
162  function setValueProperties($name, $properties, $type=null)
163  {
164  if ($type != null)
165  {
166  $this->_valueProperties[$type] = $properties;
168  return true;
169  }
170  else
171  return false;
172  }
173 }
174 ?>
$_valueProperties
Definition: class.Table.php:36
const STATE_DIRTY
setValueProperties($name, $properties, $type=null)
getValue($name, $type=null)
Table($type, $oid=null)
Definition: class.Table.php:42
deleteRow($index)
insertRow($index=null, $values=array())
const STATE_CLEAN
getColumns()
Definition: class.Table.php:80
getRow($index)
Definition: class.Table.php:89
const STATE_NEW
getColumn($name)
getNumRows()
Definition: class.Table.php:71
setState($state, $recursive=true)
getValueProperties($name, $type=null)
Table is a PersistentObject that holds a table structure. The rows of the table correspond to the val...
Definition: class.Table.php:34
reIndex()
Definition: class.Table.php:62
PersistentObject is the base class of all persistent objects. It implements the basic persistence met...