location.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Class for locations
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Location
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2009 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. /**
  33. * class for locations
  34. *
  35. * These are stored in the DB as part of notice and profile records,
  36. * but since they're about the same in both, we have a separate class
  37. * for them.
  38. *
  39. * @category Location
  40. * @package StatusNet
  41. * @author Evan Prodromou <evan@status.net>
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  43. * @link http://status.net/
  44. */
  45. class Location
  46. {
  47. public $lat;
  48. public $lon;
  49. public $location_id;
  50. public $location_ns;
  51. private $_url;
  52. private $_rdfurl;
  53. var $names = array();
  54. /**
  55. * Constructor that makes a Location from a string name
  56. *
  57. * @param string $name Human-readable name (any kind)
  58. * @param string $language Language, default = common_language()
  59. *
  60. * @return Location Location with that name (or null if not found)
  61. */
  62. static function fromName($name, $language=null)
  63. {
  64. if (is_null($language)) {
  65. $language = common_language();
  66. }
  67. $location = null;
  68. // Let a third-party handle it
  69. Event::handle('LocationFromName', array($name, $language, &$location));
  70. return $location;
  71. }
  72. /**
  73. * Constructor that makes a Location from an ID
  74. *
  75. * @param integer $id Identifier ID
  76. * @param integer $ns Namespace of the identifier
  77. * @param string $language Language to return name in (default is common)
  78. *
  79. * @return Location The location with this ID (or null if none)
  80. */
  81. static function fromId($id, $ns, $language=null)
  82. {
  83. if (is_null($language)) {
  84. $language = common_language();
  85. }
  86. $location = null;
  87. // Let a third-party handle it
  88. Event::handle('LocationFromId', array($id, $ns, $language, &$location));
  89. return $location;
  90. }
  91. /**
  92. * Constructor that finds the nearest location to a lat/lon pair
  93. *
  94. * @param float $lat Latitude
  95. * @param float $lon Longitude
  96. * @param string $language Language for results, default = current
  97. *
  98. * @return Location the location found, or null if none found
  99. */
  100. static function fromLatLon($lat, $lon, $language=null)
  101. {
  102. if (is_null($language)) {
  103. $language = common_language();
  104. }
  105. $location = null;
  106. // Let a third-party handle it
  107. if (Event::handle('LocationFromLatLon',
  108. array($lat, $lon, $language, &$location))) {
  109. // Default is just the lat/lon pair
  110. $location = new Location();
  111. $location->lat = $lat;
  112. $location->lon = $lon;
  113. }
  114. return $location;
  115. }
  116. /**
  117. * Get the name for this location in the given language
  118. *
  119. * @param string $language language to use, default = current
  120. *
  121. * @return string location name or null if not found
  122. */
  123. function getName($language=null)
  124. {
  125. if (is_null($language)) {
  126. $language = common_language();
  127. }
  128. if (array_key_exists($language, $this->names)) {
  129. return $this->names[$language];
  130. } else {
  131. $name = null;
  132. Event::handle('LocationNameLanguage', array($this, $language, &$name));
  133. if (!empty($name)) {
  134. $this->names[$language] = $name;
  135. return $name;
  136. }
  137. }
  138. }
  139. /**
  140. * Get an URL suitable for this location
  141. *
  142. * @return string URL for this location or NULL
  143. */
  144. function getURL()
  145. {
  146. // Keep one cached
  147. if (is_string($this->_url)) {
  148. return $this->_url;
  149. }
  150. $url = null;
  151. Event::handle('LocationUrl', array($this, &$url));
  152. $this->_url = $url;
  153. return $url;
  154. }
  155. /**
  156. * Get an URL for this location, suitable for embedding in RDF
  157. *
  158. * @return string URL for this location or NULL
  159. */
  160. function getRdfURL()
  161. {
  162. // Keep one cached
  163. if (is_string($this->_rdfurl)) {
  164. return $this->_rdfurl;
  165. }
  166. $url = null;
  167. Event::handle('LocationRdfUrl', array($this, &$url));
  168. $this->_rdfurl = $url;
  169. return $url;
  170. }
  171. }