PageRenderTime 164ms CodeModel.GetById 61ms app.highlight 8ms RepoModel.GetById 92ms app.codeStats 0ms

/lib/pkp/classes/core/DataObject.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 305 lines | 137 code | 30 blank | 138 comment | 27 complexity | 88ddcce2b38b417adb4f09241a25a30c MD5 | raw file
  1<?php
  2
  3/**
  4 * @file classes/core/DataObject.inc.php
  5 *
  6 * Copyright (c) 2000-2012 John Willinsky
  7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  8 *
  9 * @class DataObject
 10 * @ingroup core
 11 * @see Core
 12 *
 13 * @brief Any class with an associated DAO should extend this class.
 14 */
 15
 16// $Id$
 17
 18
 19class DataObject {
 20	/** Array of object data */
 21	var $_data;
 22
 23	/** @var array an array of MetadataAdapter instances (one per supported schema) */
 24	var $_metadataAdapters = array();
 25
 26	/**
 27	 * Constructor.
 28	 */
 29	function DataObject($callHooks = true) {
 30		// FIXME: Add meta-data schema plug-in support here to
 31		// dynamically add supported meta-data schemas.
 32
 33		$this->_data = array();
 34	}
 35
 36	//
 37	// Getters/Setters
 38	//
 39	function &getLocalizedData($key) {
 40		$localePrecedence = AppLocale::getLocalePrecedence();
 41		foreach ($localePrecedence as $locale) {
 42			$value =& $this->getData($key, $locale);
 43			if (!empty($value)) return $value;
 44			unset($value);
 45		}
 46
 47		// Fallback: Get the first available piece of data.
 48		$data =& $this->getData($key, null);
 49		if (!empty($data)) return $data[array_shift(array_keys($data))];
 50
 51		// No data available; return null.
 52		unset($data);
 53		$data = null;
 54		return $data;
 55	}
 56
 57	/**
 58	 * Get the value of a data variable.
 59	 * @param $key string
 60	 * @param $locale string (optional)
 61	 * @return mixed
 62	 */
 63	function &getData($key, $locale = null) {
 64		if (is_null($locale)) {
 65			if (isset($this->_data[$key])) {
 66				return $this->_data[$key];
 67			}
 68		} else {
 69			// see http://bugs.php.net/bug.php?id=29848
 70			if (isset($this->_data[$key]) && is_array($this->_data[$key]) && isset($this->_data[$key][$locale])) {
 71				return $this->_data[$key][$locale];
 72			}
 73		}
 74		$nullVar = null;
 75		return $nullVar;
 76	}
 77
 78	/**
 79	 * Set the value of a new or existing data variable.
 80	 * NB: Passing in null as a value will unset the
 81	 * data variable if it already existed.
 82	 * @param $key string
 83	 * @param $locale string (optional)
 84	 * @param $value mixed
 85	 */
 86	function setData($key, $value, $locale = null) {
 87		if (is_null($locale)) {
 88			if (is_null($value)) {
 89				if (isset($this->_data[$key])) unset($this->_data[$key]);
 90			} else {
 91				$this->_data[$key] = $value;
 92			}
 93		} else {
 94			if (is_null($value)) {
 95				// see http://bugs.php.net/bug.php?id=29848
 96				if (isset($this->_data[$key])) {
 97					if (is_array($this->_data[$key]) && isset($this->_data[$key][$locale])) unset($this->_data[$key][$locale]);
 98					// Was this the last entry for the data variable?
 99					if (empty($this->_data[$key])) unset($this->_data[$key]);
100				}
101			} else {
102				$this->_data[$key][$locale] = $value;
103			}
104		}
105	}
106
107	/**
108	 * Check whether a value exists for a given data variable.
109	 * @param $key string
110	 * @param $locale string (optional)
111	 * @return boolean
112	 */
113	function hasData($key, $locale = null) {
114		if (is_null($locale)) {
115			return isset($this->_data[$key]);
116		} else {
117			// see http://bugs.php.net/bug.php?id=29848
118			return isset($this->_data[$key]) && is_array($this->_data[$key]) && isset($this->_data[$key][$locale]);
119		}
120	}
121
122	/**
123	 * Return an array with all data variables.
124	 * @return array
125	 */
126	function &getAllData() {
127		return $this->_data;
128	}
129
130	/**
131	 * Set all data variables at once.
132	 * @param $data array
133	 */
134	function setAllData(&$data) {
135		$this->_data =& $data;
136	}
137
138	/**
139	 * Get ID of object.
140	 * @return int
141	 */
142	function getId() {
143		return $this->getData('id');
144	}
145
146	/**
147	 * Set ID of object.
148	 * @param $id int
149	 */
150	function setId($id) {
151		return $this->setData('id', $id);
152	}
153
154	//
155	// MetadataProvider interface implementation
156	//
157	/**
158	 * Add a meta-data adapter that will be supported
159	 * by this application entity. Only one adapter per schema
160	 * can be added.
161	 * @param $metadataAdapter MetadataAdapter
162	 */
163	function addSupportedMetadataAdapter(&$metadataAdapter) {
164		$metadataSchema =& $metadataAdapter->getMetadataSchema();
165		$metadataSchemaName = $metadataSchema->getName();
166
167		// Make sure that the meta-data schema is unique.
168		assert(!empty($metadataSchemaName) &&
169				!isset($this->_metadataAdapters[$metadataSchemaName]));
170
171		// Make sure that the adapter converts from/to this application entity
172		assert($metadataAdapter->supportsAsInput($this));
173
174		// Save adapter and schema
175		$this->_metadataAdapters[$metadataSchemaName] =& $metadataAdapter;
176	}
177
178	/**
179	 * Returns all supported meta-data adapters
180	 * @return array
181	 */
182	function &getSupportedMetadataAdapters() {
183		return $this->_metadataAdapters;
184	}
185
186	/**
187	 * Convenience method that returns an array
188	 * with all meta-data schemas that have corresponding
189	 * meta-data adapters.
190	 */
191	function &getSupportedMetadataSchemas() {
192		$supportedMetadataSchemas = array();
193		foreach($this->getSupportedMetadataAdapters() as $metadataAdapter) {
194			$supportedMetadataSchemas[] = $metadataAdapter->getMetadataSchema();
195		}
196		return $supportedMetadataSchemas;
197	}
198
199	/**
200	 * Retrieve the names of meta-data
201	 * properties of this data object.
202	 * @param $translated boolean if true, return localized field
203	 *  names, otherwise return additional field names.
204	 */
205	function getMetadataFieldNames($translated = true) {
206		// Create a list of all possible meta-data field names
207		$metadataFieldNames = array();
208		foreach($this->_metadataAdapters as $metadataSchemaName => $metadataAdapter) {
209			// Add the field names from the current adapter
210			$metadataFieldNames = array_merge($metadataFieldNames,
211					$metadataAdapter->getDataObjectMetadataFieldNames($translated));
212		}
213		$metadataFieldNames = array_unique($metadataFieldNames);
214		return $metadataFieldNames;
215	}
216
217	/**
218	 * Retrieve the names of meta-data
219	 * properties that need to be persisted
220	 * (i.e. that have data).
221	 * @param $translated boolean if true, return localized field
222	 *  names, otherwise return additional field names.
223	 * @return array an array of field names
224	 */
225	function getSetMetadataFieldNames($translated = true) {
226		// Retrieve a list of all possible meta-data field names
227		$metadataFieldNameCandidates = $this->getMetadataFieldNames($translated);
228
229		// Only retain those fields that have data
230		$metadataFieldNames = array();
231		foreach($metadataFieldNameCandidates as $metadataFieldNameCandidate) {
232			if($this->hasData($metadataFieldNameCandidate)) {
233				$metadataFieldNames[] = $metadataFieldNameCandidate;
234			}
235		}
236		return $metadataFieldNames;
237	}
238
239	/**
240	 * Retrieve the names of translated meta-data
241	 * properties that need to be persisted.
242	 * @return array an array of field names
243	 */
244	function getLocaleMetadataFieldNames() {
245		return $this->getMetadataFieldNames(true);
246	}
247
248	/**
249	 * Retrieve the names of additional meta-data
250	 * properties that need to be persisted.
251	 * @return array an array of field names
252	 */
253	function getAdditionalMetadataFieldNames() {
254		return $this->getMetadataFieldNames(false);
255	}
256
257	/**
258	 * Inject a meta-data description into this
259	 * data object.
260	 * @param $metadataDescription MetadataDescription
261	 * @param $replace boolean whether to delete existing meta-data
262	 * @return boolean true on success, otherwise false
263	 */
264	function injectMetadata(&$metadataDescription, $replace = false) {
265		$dataObject = null;
266		foreach($this->_metadataAdapters as $metadataAdapter) {
267			// The first adapter that supports the given description
268			// will be used to inject the meta-data into this data object.
269			if ($metadataAdapter->supportsAsInput($metadataDescription)) {
270				// Use adapter filter to convert from a meta-data
271				// description to a data object.
272				// NB: we pass in a reference to the data object which
273				// the filter will use to update the current instance
274				// of the data object.
275				$input = array(&$metadataDescription, &$this, $replace);
276				$dataObject =& $metadataAdapter->execute($input);
277				break;
278			}
279		}
280		return $dataObject;
281	}
282
283	/**
284	 * Inject a meta-data description into this
285	 * data object.
286	 * @param $metadataSchema MetadataSchema
287	 * @return $metadataDescription MetadataDescription
288	 */
289	function &extractMetadata(&$metadataSchema) {
290		$metadataDescription = null;
291		foreach($this->_metadataAdapters as $metadataAdapter) {
292			// The first adapter that supports the given meta-data schema
293			// will be used to extract meta-data from this data object.
294			$supportedMetadataSchema =& $metadataAdapter->getMetadataSchema();
295			if ($metadataSchema->getName() == $supportedMetadataSchema->getName()) {
296				// Use adapter filter to convert from a data object
297				// to a meta-data description.
298				$metadataDescription =& $metadataAdapter->execute($this);
299				break;
300			}
301		}
302		return $metadataDescription;
303	}
304}
305?>