PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/core/xpdo/transport/xpdoobjectvehicle.class.php

https://github.com/krisj/revolution
PHP | 439 lines | 350 code | 10 blank | 79 comment | 124 complexity | 7ee4eb0fe8e005b0140b35e5e963b75d MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0, GPL-3.0, LGPL-2.0
  1. <?php
  2. /*
  3. * Copyright 2006-2010 by Jason Coward <xpdo@opengeek.com>
  4. *
  5. * This file is part of xPDO.
  6. *
  7. * xPDO is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * xPDO is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * xPDO; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  18. * Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * Defines a class that represents an xPDOObject within a transportable package.
  22. *
  23. * @package xpdo
  24. * @subpackage transport
  25. */
  26. /**
  27. * Represents an xPDOObject within an {@link xPDOTransport} package.
  28. *
  29. * @package xpdo
  30. * @subpackage transport
  31. */
  32. class xPDOObjectVehicle extends xPDOVehicle {
  33. public $class = 'xPDOObjectVehicle';
  34. /**
  35. * Retrieve an xPDOObject instance represented in this vehicle.
  36. *
  37. * This method returns the main object contained in the payload, but you can optionally specify
  38. * a related_objects node within the payload to retrieve a specific dependent object.
  39. */
  40. public function get(& $transport, $options = array (), $element = null) {
  41. $object = null;
  42. $element = parent :: get($transport, $options, $element);
  43. if (isset ($element['class']) && isset ($element['object'])) {
  44. $vClass = $element['class'];
  45. if (!empty ($element['package'])) {
  46. $pkgPrefix = $element['package'];
  47. $pkgKeys = array_keys($transport->xpdo->packages);
  48. if ($pkgFound = in_array($pkgPrefix, $pkgKeys)) {
  49. $pkgPrefix = '';
  50. }
  51. elseif ($pos = strpos($pkgPrefix, '.')) {
  52. $prefixParts = explode('.', $pkgPrefix);
  53. $prefix = '';
  54. foreach ($prefixParts as $prefixPart) {
  55. $prefix .= $prefixPart;
  56. $pkgPrefix = substr($pkgPrefix, $pos +1);
  57. if ($pkgFound = in_array($prefix, $pkgKeys))
  58. break;
  59. $prefix .= '.';
  60. $pos = strpos($pkgPrefix, '.');
  61. }
  62. if (!$pkgFound)
  63. $pkgPrefix = $element['package'];
  64. }
  65. $vClass = (!empty ($pkgPrefix) ? $pkgPrefix . '.' : '') . $vClass;
  66. }
  67. $object = $transport->xpdo->newObject($vClass);
  68. if (is_object($object) && $object instanceof xPDOObject) {
  69. $options = array_merge($options, $element);
  70. $setKeys = false;
  71. if (isset ($options[xPDOTransport::PRESERVE_KEYS])) {
  72. $setKeys = (boolean) $options[xPDOTransport::PRESERVE_KEYS];
  73. }
  74. $object->fromJSON($element['object'], '', $setKeys, true);
  75. }
  76. }
  77. return $object;
  78. }
  79. /**
  80. * Install the xPDOObjects represented by vehicle into the transport host.
  81. */
  82. public function install(& $transport, $options) {
  83. $parentObj = null;
  84. $parentMeta = null;
  85. $installed = $this->_installObject($transport, $options, $this->payload, $parentObj, $parentMeta);
  86. return $installed;
  87. }
  88. /**
  89. * Install a single xPDOObject from the vehicle payload.
  90. *
  91. * @param xPDOTransport $transport The host xPDOTransport instance.
  92. * @param array $options Any optional attributes to apply to the installation.
  93. * @param array $element A node of the payload representing the object to install.
  94. * @param xPDOObject &$parentObject A reference to the object serving as a parent to the one
  95. * being installed.
  96. * @param array $fkMeta The foreign key relationship data that defines the relationship with the
  97. * parentObject.
  98. */
  99. protected function _installObject(& $transport, $options, $element, & $parentObject, $fkMeta) {
  100. $saved = false;
  101. $preserveKeys = false;
  102. $preExistingMode = xPDOTransport::PRESERVE_PREEXISTING;
  103. $upgrade = false;
  104. $exists = false;
  105. $object = $this->get($transport, $options, $element);
  106. if (is_object($object) && $object instanceof xPDOObject) {
  107. $vOptions = array_merge($options, $element);
  108. $vClass = $vOptions['class'];
  109. if ($transport->xpdo->getDebug() === true)
  110. $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Installing Vehicle: " . print_r($vOptions, true));
  111. if ($parentObject !== null && $fkMeta !== null) {
  112. if ($fkMeta['owner'] == 'local') {
  113. if ($object->get($fkMeta['foreign']) !== $parentObject->get($fkMeta['local'])) {
  114. $object->set($fkMeta['foreign'], $parentObject->get($fkMeta['local']));
  115. }
  116. }
  117. }
  118. $preserveKeys = !empty ($vOptions[xPDOTransport::PRESERVE_KEYS]);
  119. $upgrade = !empty ($vOptions[xPDOTransport::UPDATE_OBJECT]);
  120. if (!empty ($vOptions[xPDOTransport::UNIQUE_KEY])) {
  121. $uniqueKey = $object->get($vOptions[xPDOTransport::UNIQUE_KEY]);
  122. if (is_array($uniqueKey)) {
  123. $criteria = array_combine($vOptions[xPDOTransport::UNIQUE_KEY], $uniqueKey);
  124. } else {
  125. $criteria = array (
  126. $vOptions[xPDOTransport::UNIQUE_KEY] => $uniqueKey
  127. );
  128. }
  129. }
  130. elseif (isset ($vOptions['key_expr']) && isset ($vOptions['key_format'])) {
  131. //TODO: implement ability to generate new keys
  132. } else {
  133. $criteria = $vOptions[xPDOTransport::NATIVE_KEY];
  134. }
  135. if (!empty ($vOptions[xPDOTransport::PREEXISTING_MODE])) {
  136. $preExistingMode = intval($vOptions[xPDOTransport::PREEXISTING_MODE]);
  137. }
  138. if ($this->validate($transport, $object, $vOptions)) {
  139. if ($obj = $transport->xpdo->getObject($vClass, $criteria)) {
  140. $exists = true;
  141. if ($preExistingMode !== xPDOTransport::REMOVE_PREEXISTING) {
  142. $transport->_preserved[$vOptions['guid']] = array (
  143. 'criteria' => $criteria,
  144. 'object' => $obj->toArray('', true)
  145. );
  146. }
  147. if ($upgrade) {
  148. $obj->fromArray($object->toArray('', true), '', false, true);
  149. $object = $obj;
  150. }
  151. }
  152. elseif ($transport->xpdo->getDebug() === true) {
  153. $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Object for class {$vClass} not found; criteria: " . print_r($criteria, true));
  154. }
  155. if (!$exists || ($exists && $upgrade)) {
  156. if ($transport->xpdo->getOption('dbtype') === 'sqlsrv' && !$exists && $preserveKeys) {
  157. $transport->xpdo->exec("SET IDENTITY_INSERT {$transport->xpdo->getTableName($vClass)} ON");
  158. }
  159. $saved = $object->save();
  160. if ($transport->xpdo->getOption('dbtype') === 'sqlsrv' && !$exists && $preserveKeys) {
  161. $transport->xpdo->exec("SET IDENTITY_INSERT {$transport->xpdo->getTableName($vClass)} OFF");
  162. }
  163. if (!$saved) {
  164. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Error saving vehicle object of class {$vClass}; criteria: " . print_r($criteria, true));
  165. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Error saving vehicle object: " . print_r($vOptions, true));
  166. } else {
  167. if ($parentObject !== null && $fkMeta !== null) {
  168. if ($fkMeta['owner'] == 'foreign') {
  169. if ($object->get($fkMeta['foreign']) !== $parentObject->get($fkMeta['local'])) {
  170. $parentObject->set($fkMeta['local'], $object->get($fkMeta['foreign']));
  171. if (!$parentObject->save()) {
  172. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Error saving changes to parent object fk field {$fkMeta['local']}");
  173. }
  174. }
  175. }
  176. }
  177. }
  178. } else {
  179. $transport->xpdo->log(xPDO::LOG_LEVEL_INFO, "Skipping vehicle object of class {$vClass} (data object exists and cannot be upgraded); criteria: " . print_r($criteria, true));
  180. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Skipping vehicle object (data object exists and cannot be upgraded): ' . print_r($vOptions, true));
  181. }
  182. if (($saved || $exists) && !$this->_installRelated($transport, $object, $element, $options)) {
  183. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Could not install related objects for vehicle object of class {$vClass}; criteria: " . print_r($criteria, true));
  184. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Could not install related objects for vehicle: ' . print_r($vOptions, true));
  185. }
  186. if ($parentObject === null && !$this->resolve($transport, $object, $vOptions)) {
  187. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Could not resolve vehicle for object of class {$vClass}; criteria: " . print_r($criteria, true));
  188. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Could not resolve vehicle: ' . print_r($vOptions, true));
  189. }
  190. } else {
  191. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Could not validate vehicle object of class {$vClass}; criteria: " . print_r($criteria, true));
  192. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Could not validate vehicle object: ' . print_r($vOptions, true));
  193. }
  194. } else {
  195. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not load vehicle!');
  196. }
  197. return ($saved || ($exists && !$upgrade));
  198. }
  199. /**
  200. * Installs a related object from the vehicle.
  201. */
  202. public function _installRelated(& $transport, & $parent, $element, $options) {
  203. $installed = true;
  204. if (is_object($parent) && isset ($element[xPDOTransport::RELATED_OBJECTS]) && is_array($element[xPDOTransport::RELATED_OBJECTS])) {
  205. $installed = false;
  206. foreach ($element[xPDOTransport::RELATED_OBJECTS] as $rAlias => $rVehicles) {
  207. $parentClass = $parent->_class;
  208. $rMeta = $transport->xpdo->getFKDefinition($parentClass, $rAlias);
  209. if ($rMeta) {
  210. foreach ($rVehicles as $rKey => $rVehicle) {
  211. $installed = $this->_installObject($transport, $options, $rVehicle, $parent, $rMeta);
  212. }
  213. }
  214. }
  215. }
  216. return $installed;
  217. }
  218. /**
  219. * Uninstalls vehicle artifacts from the transport host.
  220. */
  221. public function uninstall(& $transport, $options) {
  222. $parentObj = null;
  223. $parentMeta = null;
  224. return $this->_uninstallObject($transport, $options, $this->payload, $parentObj, $parentMeta);
  225. }
  226. /**
  227. * Uninstall the xPDOObjects represented by a vehicle element from the transport host.
  228. */
  229. public function _uninstallObject(& $transport, $options, $element, & $parentObject, $fkMeta) {
  230. $uninstalled = false;
  231. $removed = false;
  232. $uninstallObject = true;
  233. $upgrade = false;
  234. $preExistingMode = xPDOTransport::PRESERVE_PREEXISTING;
  235. $object = $this->get($transport, $options, $element);
  236. if (is_object($object) && $object instanceof xPDOObject) {
  237. $vOptions = array_merge($options, $element);
  238. $vClass = $vOptions['class'];
  239. $upgrade = !empty($vOptions[xPDOTransport::UPDATE_OBJECT]);
  240. $preserveKeys = !empty ($vOptions[xPDOTransport::PRESERVE_KEYS]);
  241. if (!empty ($vOptions[xPDOTransport::UNIQUE_KEY])) {
  242. $uniqueKey = $object->get($vOptions[xPDOTransport::UNIQUE_KEY]);
  243. if (is_array($uniqueKey)) {
  244. $criteria = array_combine($vOptions[xPDOTransport::UNIQUE_KEY], $uniqueKey);
  245. } else {
  246. $criteria = array (
  247. $vOptions[xPDOTransport::UNIQUE_KEY] => $uniqueKey
  248. );
  249. }
  250. } else {
  251. $criteria = $vOptions[xPDOTransport::NATIVE_KEY];
  252. }
  253. if (!empty ($vOptions[xPDOTransport::PREEXISTING_MODE])) {
  254. $preExistingMode = intval($vOptions[xPDOTransport::PREEXISTING_MODE]);
  255. }
  256. if ($this->validate($transport, $object, $vOptions)) {
  257. $uninstalled = true;
  258. if (isset($vOptions[xPDOTransport::UNINSTALL_OBJECT])) {
  259. $uninstallObject = !empty($vOptions[xPDOTransport::UNINSTALL_OBJECT]);
  260. }
  261. $exists = false;
  262. if ($obj = $transport->xpdo->getObject($vClass, $criteria)) {
  263. $exists = true;
  264. $object = $obj;
  265. }
  266. if ($exists) {
  267. if ($uninstallObject && $upgrade && (!isset ($transport->_preserved[$vOptions['guid']]) || $preExistingMode === xPDOTransport::REMOVE_PREEXISTING)) {
  268. $removed = $object->remove();
  269. if (!$removed) {
  270. $uninstalled = false;
  271. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Error removing vehicle object of class {$vClass}; criteria: " . print_r($criteria, true));
  272. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Error removing vehicle object: ' . print_r($vOptions, true));
  273. }
  274. }
  275. if ($upgrade && $preExistingMode === xPDOTransport::RESTORE_PREEXISTING) {
  276. if (isset ($transport->_preserved[$vOptions['guid']])) {
  277. $preserved = $transport->_preserved[$vOptions['guid']]['object'];
  278. $object->fromArray($preserved, '', true, true);
  279. $restored = $object->save();
  280. if (!$restored) {
  281. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Error restoring preserved object of class {$vClass}: " . print_r($transport->_preserved[$vOptions['guid']], true));
  282. }
  283. }
  284. }
  285. } else {
  286. $transport->xpdo->log(xPDO::LOG_LEVEL_WARN, 'Skipping ' . $vClass . ' object (data object does not exist and cannot be removed): ' . print_r($criteria, true));
  287. }
  288. if (!$this->_uninstallRelated($transport, $object, $element, $options)) {
  289. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Could not uninstall related objects for vehicle object of class {$vClass}; criteria: " . print_r($criteria, true));
  290. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Could not uninstall related objects for vehicle: ' . print_r($vOptions, true));
  291. }
  292. if ($parentObject === null && !$this->resolve($transport, $object, $vOptions)) {
  293. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Could not resolve vehicle for object of class {$vClass}; criteria: " . print_r($criteria, true));
  294. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Could not resolve vehicle: ' . print_r($vOptions, true));
  295. }
  296. } else {
  297. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Could not validate vehicle object of class {$vClass}; criteria: " . print_r($criteria, true));
  298. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Could not validate vehicle object: ' . print_r($vOptions, true));
  299. }
  300. } else {
  301. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Problem instantiating object from vehicle');
  302. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Problem instantiating object from vehicle: ' . print_r(array_merge($options, $element), true));
  303. }
  304. return $uninstalled;
  305. }
  306. /**
  307. * Uninstalls related objects from the vehicle.
  308. */
  309. public function _uninstallRelated(& $transport, & $parent, $element, $options) {
  310. $uninstalled = true;
  311. if (is_object($parent) && isset ($element[xPDOTransport::RELATED_OBJECTS]) && is_array($element[xPDOTransport::RELATED_OBJECTS])) {
  312. $uninstalled = false;
  313. foreach ($element[xPDOTransport::RELATED_OBJECTS] as $rAlias => $rVehicles) {
  314. $parentClass = $parent->_class;
  315. $rMeta = $transport->xpdo->getFKDefinition($parentClass, $rAlias);
  316. if ($rMeta) {
  317. $results = array();
  318. foreach ($rVehicles as $rKey => $rVehicle) {
  319. $results[] = $this->_uninstallObject($transport, $options, $rVehicle, $parent, $rMeta);
  320. }
  321. $uninstalled = (array_search(false, $results) === false);
  322. }
  323. }
  324. }
  325. return $uninstalled;
  326. }
  327. /**
  328. * Put an xPDOObject representation into a transport package.
  329. *
  330. * This implementation supports the inclusion of related objects. Simply instantiate the related
  331. * objects that you want to include in the vehicle on the main object, and set
  332. * xPDOTransport::RELATED_OBJECTS => true in your attributes.
  333. */
  334. public function put(& $transport, & $object, $attributes = array ()) {
  335. parent :: put($transport, $object, $attributes);
  336. if (is_object($object)) {
  337. if (!isset ($this->payload['package'])) {
  338. if ($object instanceof xPDOObject) {
  339. $packageName = $object->_package;
  340. } else {
  341. $packageName = '';
  342. }
  343. $this->payload['package'] = $packageName;
  344. }
  345. if ($object instanceof xPDOObject) {
  346. $nativeKey = $object->getPrimaryKey();
  347. $this->payload['object'] = $object->toJSON('', true);
  348. $this->payload['native_key'] = $nativeKey;
  349. $this->payload['signature'] = md5($this->payload['class'] . '_' . $this->payload['guid']);
  350. if (isset ($this->payload[xPDOTransport::RELATED_OBJECTS]) && !empty ($this->payload[xPDOTransport::RELATED_OBJECTS])) {
  351. $relatedObjects = array ();
  352. foreach ($object->_relatedObjects as $rAlias => $related) {
  353. if (is_array($related)) {
  354. foreach ($related as $rKey => $rObj) {
  355. if (!isset ($relatedObjects[$rAlias]))
  356. $relatedObjects[$rAlias] = array ();
  357. $guid = md5(uniqid(rand(), true));
  358. $relatedObjects[$rAlias][$guid] = array ();
  359. $this->_putRelated($transport, $rAlias, $rObj, $relatedObjects[$rAlias][$guid]);
  360. }
  361. }
  362. elseif (is_object($related)) {
  363. if (!isset ($relatedObjects[$rAlias]))
  364. $relatedObjects[$rAlias] = array ();
  365. $guid = md5(uniqid(rand(), true));
  366. $relatedObjects[$rAlias][$guid] = array ();
  367. $this->_putRelated($transport, $rAlias, $related, $relatedObjects[$rAlias][$guid]);
  368. }
  369. }
  370. if (!empty ($relatedObjects))
  371. $this->payload['related_objects'] = $relatedObjects;
  372. }
  373. }
  374. elseif (is_object($object)) {
  375. $this->payload['object'] = $transport->xpdo->toJSON(get_object_vars($object));
  376. $this->payload['native_key'] = $this->payload['guid'];
  377. $this->payload['signature'] = md5($this->payload['class'] . '_' . $this->payload['guid']);
  378. }
  379. }
  380. }
  381. /**
  382. * Recursively put related objects into the vehicle.
  383. *
  384. * @access protected
  385. * @param xPDOTransport $transport The host xPDOTransport instance.
  386. * @param string $alias The alias representing the relation to the parent object.
  387. * @param xPDOObject &$object A reference to the dependent object being added into the vehicle.
  388. * @param array $payloadElement An element of the payload to place the dependent object in.
  389. */
  390. protected function _putRelated(& $transport, $alias, & $object, & $payloadElement) {
  391. if (is_array($payloadElement)) {
  392. if (is_object($object) && $object instanceof xPDOObject) {
  393. if (isset ($this->payload['related_object_attributes'][$alias]) && is_array($this->payload['related_object_attributes'][$alias])) {
  394. $payloadElement = array_merge($payloadElement, $this->payload['related_object_attributes'][$alias]);
  395. }
  396. elseif (isset ($this->payload['related_object_attributes'][$object->_class]) && is_array($this->payload['related_object_attributes'][$object->_class])) {
  397. $payloadElement = array_merge($payloadElement, $this->payload['related_object_attributes'][$object->_class]);
  398. }
  399. $payloadElement['class'] = $object->_class;
  400. $nativeKey = $object->getPrimaryKey();
  401. $payloadElement['object'] = $object->toJSON('', true);
  402. $payloadElement['guid'] = md5(uniqid(rand(), true));
  403. $payloadElement['native_key'] = $nativeKey;
  404. $payloadElement['signature'] = md5($object->_class . '_' . $payloadElement['guid']);
  405. $relatedObjects = array ();
  406. foreach ($object->_relatedObjects as $rAlias => $related) {
  407. if (is_array($related)) {
  408. foreach ($related as $rKey => $rObj) {
  409. if (!isset ($relatedObjects[$rAlias]))
  410. $relatedObjects[$rAlias] = array ();
  411. $guid = md5(uniqid(rand(), true));
  412. $relatedObjects[$rAlias][$guid] = array ();
  413. $this->_putRelated($transport, $rAlias, $rObj, $relatedObjects[$rAlias][$guid]);
  414. }
  415. }
  416. elseif (is_object($related)) {
  417. if (!isset ($relatedObjects[$rAlias]))
  418. $relatedObjects[$rAlias] = array ();
  419. $guid = md5(uniqid(rand(), true));
  420. $relatedObjects[$rAlias][$guid] = array ();
  421. $this->_putRelated($transport, $rAlias, $related, $relatedObjects[$rAlias][$guid]);
  422. }
  423. }
  424. if (!empty ($relatedObjects))
  425. $payloadElement['related_objects'] = $relatedObjects;
  426. }
  427. }
  428. }
  429. }