location.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 Notice::locationOptions(...)
  56. *
  57. * @param array $options an array for example provided by Notice::locationOptions(...)
  58. *
  59. * @return Location Location with the given options (lat, lon, id, name)
  60. */
  61. static function fromOptions(array $options) {
  62. $location = new Location();
  63. foreach (['lat', 'lon', 'location_id', 'location_ns'] as $opt) {
  64. if (isset($options[$opt])) {
  65. $location->$opt = $options[$opt];
  66. }
  67. }
  68. return $location;
  69. }
  70. /**
  71. * Constructor that makes a Location from a string name
  72. *
  73. * @param string $name Human-readable name (any kind)
  74. * @param string $language Language, default = common_language()
  75. *
  76. * @return Location Location with that name (or null if not found)
  77. */
  78. static function fromName($name, $language=null)
  79. {
  80. if (is_null($language)) {
  81. $language = common_language();
  82. }
  83. $location = null;
  84. // Let a third-party handle it
  85. Event::handle('LocationFromName', array($name, $language, &$location));
  86. return $location;
  87. }
  88. /**
  89. * Constructor that makes a Location from an ID
  90. *
  91. * @param integer $id Identifier ID
  92. * @param integer $ns Namespace of the identifier
  93. * @param string $language Language to return name in (default is common)
  94. *
  95. * @return Location The location with this ID (or null if none)
  96. */
  97. static function fromId($id, $ns, $language=null)
  98. {
  99. if (is_null($language)) {
  100. $language = common_language();
  101. }
  102. $location = null;
  103. // Let a third-party handle it
  104. Event::handle('LocationFromId', array($id, $ns, $language, &$location));
  105. return $location;
  106. }
  107. /**
  108. * Constructor that finds the nearest location to a lat/lon pair
  109. *
  110. * @param float $lat Latitude
  111. * @param float $lon Longitude
  112. * @param string $language Language for results, default = current
  113. *
  114. * @return Location the location found, or null if none found
  115. */
  116. static function fromLatLon($lat, $lon, $language=null)
  117. {
  118. if (is_null($language)) {
  119. $language = common_language();
  120. }
  121. $location = null;
  122. // Let a third-party handle it
  123. if (Event::handle('LocationFromLatLon',
  124. array($lat, $lon, $language, &$location))) {
  125. // Default is just the lat/lon pair
  126. $location = new Location();
  127. $location->lat = $lat;
  128. $location->lon = $lon;
  129. }
  130. return $location;
  131. }
  132. /**
  133. * Get the name for this location in the given language
  134. *
  135. * @param string $language language to use, default = current
  136. *
  137. * @return string location name or null if not found
  138. */
  139. function getName($language=null)
  140. {
  141. if (is_null($language)) {
  142. $language = common_language();
  143. }
  144. if (array_key_exists($language, $this->names)) {
  145. return $this->names[$language];
  146. } else {
  147. $name = null;
  148. Event::handle('LocationNameLanguage', array($this, $language, &$name));
  149. if (!empty($name)) {
  150. $this->names[$language] = $name;
  151. return $name;
  152. }
  153. }
  154. }
  155. /**
  156. * Get an URL suitable for this location
  157. *
  158. * @return string URL for this location or NULL
  159. */
  160. function getURL()
  161. {
  162. // Keep one cached
  163. if (is_string($this->_url)) {
  164. return $this->_url;
  165. }
  166. $url = null;
  167. Event::handle('LocationUrl', array($this, &$url));
  168. $this->_url = $url;
  169. return $url;
  170. }
  171. /**
  172. * Get an URL for this location, suitable for embedding in RDF
  173. *
  174. * @return string URL for this location or NULL
  175. */
  176. function getRdfURL()
  177. {
  178. // Keep one cached
  179. if (is_string($this->_rdfurl)) {
  180. return $this->_rdfurl;
  181. }
  182. $url = null;
  183. Event::handle('LocationRdfUrl', array($this, &$url));
  184. $this->_rdfurl = $url;
  185. return $url;
  186. }
  187. }