Source for file object.php

Documentation is available at object.php

  1. <?php
  2. /**
  3. * @package org.routamc.positioning
  4. * @author The Midgard Project, http://www.midgard-project.org
  5. * @version $Id: object.php 3829 2006-08-14 16:25:12Z bergie $
  6. * @copyright The Midgard Project, http://www.midgard-project.org
  7. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  8. */
  9.  
  10. /**
  11. * Positioning for a given Midgard object
  12. *
  13. * <b>Example:</b>
  14. *
  15. * <code>
  16. * <?php
  17. * $object_position = new org_routamc_positioning_object($article);
  18. * $coordinates = $object_position->get_coordinates();
  19. * if (!is_null($coordinates))
  20. * {
  21. * echo "<meta name=\"icbm\" value=\"{$coordinates['latitude']},{$coordinates['longitude']}\" />\n";
  22. * }
  23. * ?>
  24. * </code>
  25. *
  26. * @package org.routamc.positioning
  27. */
  28. class org_routamc_positioning_object extends midcom_baseclasses_components_purecode
  29. {
  30. /**
  31. * The object we're looking for position of
  32. *
  33. * @var midcom_core_dbobject
  34. */
  35. var $_object = null;
  36. /**
  37. * Initializes the class. The real startup is done by the initialize() call.
  38. */
  39. function org_routamc_positioning_object($object)
  40. {
  41. $this->_component = 'org.routamc.positioning';
  42. $this->_object = $object;
  43. parent::midcom_baseclasses_components_purecode();
  44. }
  45.  
  46. /**
  47. * Get location object for the object
  48. *
  49. * @return org_routamc_positioning_location_dba
  50. */
  51. function seek_location_object()
  52. {
  53. $qb = org_routamc_positioning_location_dba::new_query_builder();
  54. $qb->add_constraint('parent', '=', $this->_object->guid);
  55. $qb->add_constraint('relation', '=', ORG_ROUTAMC_POSITIONING_RELATION_IN);
  56. $matches = $qb->execute();
  57. if (count($matches) > 0)
  58. {
  59. return $matches[0];
  60. }
  61. return null;
  62. }
  63.  
  64. /**
  65. * Get log object based on creation time and creator of the object
  66. *
  67. * @return org_routamc_positioning_log_dba
  68. */
  69. function seek_log_object()
  70. {
  71. $qb = org_routamc_positioning_log_dba::new_query_builder();
  72. // TODO: Switch to metadata
  73. if (isset($this->_object->author))
  74. {
  75. $creator = $this->_object->author;
  76. }
  77. elseif ($this->_object->creator)
  78. {
  79. $creator = $this->_object->creator;
  80. }
  81. else
  82. {
  83. return null;
  84. }
  85. $qb->add_constraint('person', '=', $creator);
  86. $qb->add_constraint('date', '<=', $this->_object->created);
  87. $qb->add_order('date', 'DESC');
  88. $qb->set_limit(1);
  89. $matches = $qb->execute_unchecked();
  90. if (count($matches) > 0)
  91. {
  92. return $matches[0];
  93. }
  94. return null;
  95. }
  96.  
  97. /**
  98. * Get coordinates of the object
  99. *
  100. * @return Array
  101. */
  102. function get_coordinates($cache = true)
  103. {
  104. $coordinates = Array(
  105. 'latitude' => null,
  106. 'longitude' => null,
  107. 'altitude' => null,
  108. );
  109. // Check if the object has a location set
  110. $location = $this->seek_location_object();
  111. if (is_object($location))
  112. {
  113. $coordinates['latitude'] = $location->latitude;
  114. $coordinates['longitude'] = $location->longitude;
  115. $coordinates['altitude'] = $location->altitude;
  116.  
  117. // Consistency check
  118. // TODO: Use metadata.published
  119. if ($location->date != $this->_object->created)
  120. {
  121. if ($location->log)
  122. {
  123. // We are most likely pointing to wrong log. Remove this cached entry so we can recreate i
  124. // again below
  125. $location->delete();
  126. $cache = true;
  127. }
  128. else
  129. {
  130. // This location entry isn't coming from a log so it just needs to be rescheduled
  131. $location->date = $this->_object->created;
  132. $location->update();
  133. return $coordinates;
  134. }
  135. }
  136. else
  137. {
  138. return $coordinates;
  139. }
  140. }
  141. // No location set, seek based on creator and creation time
  142. $log = $this->seek_log_object();
  143. if (is_object($log))
  144. {
  145. $coordinates['latitude'] = $log->latitude;
  146. $coordinates['longitude'] = $log->longitude;
  147. $coordinates['altitude'] = $log->altitude;
  148. if ($cache)
  149. {
  150. // Cache the object's location into a location object
  151. $location = new org_routamc_positioning_location_dba();
  152. $location->log = $log->id;
  153. $location->type = ORG_ROUTAMC_POSITIONING_RELATION_IN;
  154. // TODO: Use metadata.published
  155. $location->date = $this->_object->created;
  156. $location->parent = $this->_object->id;
  157. $location->parentclass = get_class($this->_object->id);
  158. // TODO: Save parent component
  159. $location->latitude = $log->latitude;
  160. $location->longitude = $log->longitude;
  161. $location->altitude = $log->altitude;
  162. $location->create();
  163. }
  164. return $coordinates;
  165. }
  166. // No coordinates found, return null
  167. return null;
  168. }
  169. function set_metadata()
  170. {
  171. $coordinates = $this->get_coordinates();
  172. if (!is_null($coordinates))
  173. {
  174. // ICBM tag as defined by http://geourl.org/
  175. $_MIDCOM->add_meta_head
  176. (
  177. Array
  178. (
  179. 'name' => 'icbm',
  180. 'content' => "{$coordinates['latitude']},{$coordinates['longitude']}",
  181. )
  182. );
  183. }
  184. }
  185. }

Documentation generated on Tue, 15 Aug 2006 12:49:55 +0300 by phpDocumentor 1.3.0RC3