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