PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/extensions/geocoder/libraries/Result.php

https://bitbucket.org/wildanm/zurmo
PHP | 382 lines | 157 code | 28 blank | 197 comment | 10 complexity | 7f1f8381a16532b4e832ee78234e472d MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, BSD-2-Clause, GPL-3.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2009 Brian Armstrong
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. /**
  24. * Class to hold a GeoCode result for return from a given API
  25. *
  26. * @package GeoCoder
  27. * @author Brian Armstrong <brian@barmstrongconsulting.com>
  28. * @copyright (C) 2009 Brian Armstrong
  29. * @link http://barmstrongconsulting.com/
  30. * @version 1.0
  31. */
  32. class GeoCode_Result
  33. {
  34. /**
  35. * The driver associated with this result
  36. * @var GeoCode_Driver
  37. */
  38. private $_driver = null;
  39. /**
  40. * Holds the types of the containers that have maps in them
  41. * @var array
  42. */
  43. private static $_map_containers = array();
  44. /**
  45. * The number of maps we have rendered
  46. * @var integer
  47. */
  48. private static $_map_count = 0;
  49. /**
  50. * The number of points we have rendered
  51. * @var integer
  52. */
  53. private static $_point_count = 0;
  54. /**
  55. * The attributes array to hold all of our data
  56. * @var array
  57. */
  58. protected $attributes = array(
  59. /**
  60. * The accuracy code
  61. * @var integer
  62. */
  63. 'accuracy' => null,
  64. /**
  65. * The latitude in degrees
  66. * @var float
  67. */
  68. 'latitude' => null,
  69. /**
  70. * The longitude in degrees
  71. * @var float
  72. */
  73. 'longitude' => null,
  74. /**
  75. * The query that was sent to the API
  76. * @var string
  77. */
  78. 'query' => null,
  79. /**
  80. * The processed query, as returned by the API
  81. * @var string
  82. */
  83. 'clean_query' => null,
  84. /**
  85. * The state name
  86. * @var string
  87. */
  88. 'state' => null,
  89. /**
  90. * The county name
  91. * @var string
  92. */
  93. 'county' => null,
  94. /**
  95. * The city name
  96. * @var string
  97. */
  98. 'city' => null,
  99. /**
  100. * The zip code
  101. * NOTE: This needs to be treated as a string. Zip codes outside of the
  102. * United states are not guaranteed to be numeric. Example: Canada
  103. * @var string
  104. */
  105. 'zip' => null,
  106. /**
  107. * The street address
  108. * @var string
  109. */
  110. 'street' => null,
  111. /**
  112. * The country
  113. * @var string
  114. */
  115. 'country' => null,
  116. );
  117. /**
  118. * Initialize the result object
  119. *
  120. * @param GeoCode_Driver $driver
  121. * @param array $data
  122. * @return GeoCode_Result
  123. */
  124. public function __construct(GeoCode_Driver $driver, $data = array())
  125. {
  126. $this->_driver = $driver;
  127. // Assign all the data
  128. foreach ($data as $key => $value)
  129. {
  130. // Pass off to the set method
  131. $this->__set($key, $value);
  132. }
  133. }
  134. /**
  135. * Render a map of this result
  136. *
  137. * NOTE: For map types simply send the constant string, any
  138. * prefix will be appended automatically
  139. *
  140. * @param string $container_id - the id of the container object
  141. * @param array $options - the map options
  142. * @param string $type - the type of map to render
  143. */
  144. public function renderMap($container_id, $options = array(), $type = null)
  145. {
  146. if (!isset(self::$_map_containers[$container_id]))
  147. {
  148. // Get the type for this if it was not sent
  149. if ($type === null) $type = $this->_driver->getDriverName();
  150. // Lowercase the map type
  151. $type = strtolower($type);
  152. // Save the container id and the type
  153. self::$_map_containers[$container_id] = $type;
  154. // Render the view
  155. $map_script = $this->render("{$type}/map", array(
  156. 'query' => $this->query,
  157. 'latitude' => $this->latitude,
  158. 'longitude' => $this->longitude,
  159. 'container_id' => $container_id,
  160. 'options' => $options
  161. ), true);
  162. // Register the javascript
  163. Yii::app()->getClientScript()->registerScript("{$type}_map_js".(self::$_map_count++), $map_script, CClientScript::POS_READY);
  164. }
  165. // Render the map point
  166. $this->renderPoint($this->latitude, $this->longitude, $container_id, $this->clean_query);
  167. }
  168. /**
  169. * Render a point on the map. By default it will render the points associated
  170. * with the result in the last used container with the last used type. Make sure
  171. * that you have already made a call to render map with the given container_id
  172. *
  173. * @param float $lat
  174. * @param float $lon
  175. * @param string $container_id
  176. * @param string $description
  177. */
  178. public function renderPoint($lat, $lon, $container_id, $description=null)
  179. {
  180. // Validate the container
  181. if (!isset(self::$_map_containers[$container_id]))
  182. throw new GeoCode_Exception("Unknown map container '{$container_id}'");
  183. // Get our type
  184. $type = self::$_map_containers[$container_id];
  185. // Render the view
  186. $point_script = $this->render("{$type}/point", array(
  187. 'query' => $description,
  188. 'latitude' => ($lat === null) ? $this->latitude : $lat,
  189. 'longitude' => ($lon === null) ? $this->longitude : $lon,
  190. ), true);
  191. // Register the javascript
  192. Yii::app()->clientScript->registerScript("{$type}_point_js".(self::$_point_count++), $point_script, CClientScript::POS_READY);
  193. }
  194. /**
  195. * Get the string associated with the accuracy constant
  196. *
  197. * @return string
  198. */
  199. public function getAccuracyString()
  200. {
  201. return $this->_driver->getAccuracyString($this->accuracy);
  202. }
  203. /**
  204. * Get the latitude in radians
  205. *
  206. * @return float
  207. */
  208. public function getLatitudeRadians()
  209. {
  210. return isset($this->latitude) ? deg2rad((float)$this->latitude) : null;
  211. }
  212. /**
  213. * Get the longitude in radians
  214. *
  215. * @return float
  216. */
  217. public function getLongitudeRadians()
  218. {
  219. return isset($this->longitude) ? deg2rad((float)$this->longitude) : null;
  220. }
  221. /**
  222. * Render one of this extension's views
  223. *
  224. * @param string $_view_
  225. * @param mixed $_data_
  226. * @param boolean $_return_
  227. */
  228. private function render($_view_, $_data_=null, $_return_=false)
  229. {
  230. // Variable names are special so we don't get collisions with extract
  231. // Get the filename to render
  232. $_viewFile_ = $this->getViewFile($_view_);
  233. // Handle the data we passed
  234. if (is_array($_data_))
  235. extract($_data_, EXTR_PREFIX_SAME, 'data');
  236. else
  237. $_data_ = $data;
  238. // Render the file
  239. if ($_return_)
  240. {
  241. ob_start();
  242. ob_implicit_flush(false);
  243. require(str_replace("\\", "/", $_viewFile_));
  244. return ob_get_clean();
  245. }
  246. else
  247. {
  248. require(str_replace("\\", "/", $_viewFile_));
  249. }
  250. }
  251. /**
  252. * Get the path to the view file
  253. *
  254. * @param string $view_name
  255. * @return string
  256. */
  257. private function getViewFile($view_name)
  258. {
  259. // The base path of the view files
  260. $path = dirname(__FILE__) .
  261. DIRECTORY_SEPARATOR .'..'.
  262. DIRECTORY_SEPARATOR . 'views';
  263. $base_path = realpath($path);
  264. // View file not found
  265. if ($base_path === false)
  266. throw new CException("Cannot find view path '{$path}'");
  267. return $base_path . DIRECTORY_SEPARATOR . $view_name .'.php';
  268. }
  269. /**
  270. * Magic get method. If a get<Name> method exists, then it is called.
  271. * Otherwise we look in the attributes array for the key.
  272. * If nothing is found, an exception is thrown
  273. *
  274. * @param string $name
  275. * @return mixed
  276. */
  277. public function __get($name)
  278. {
  279. $method = 'get'.str_replace('_', '', $name);
  280. if (method_exists($this, $method))
  281. {
  282. return call_user_func(array($this, $method));
  283. }
  284. elseif (array_key_exists($name, $this->attributes))
  285. {
  286. return $this->attributes[$name];
  287. }
  288. else
  289. {
  290. throw new CException("Property '{$name}' does not exists");
  291. }
  292. }
  293. /**
  294. * Magic set method. If a set<Name> method exists, then it is called.
  295. * Otherwise we look in the attributes array for the key.
  296. * If nothing is found, an exception is thrown
  297. *
  298. * @param string $name
  299. * @param mixed $value
  300. */
  301. public function __set($name, $value)
  302. {
  303. $method = 'set'.str_replace('_', '', $name);
  304. if (method_exists($this, $method))
  305. {
  306. $this->$method($value);
  307. }
  308. elseif (array_key_exists($name, $this->attributes))
  309. {
  310. $this->attributes[$name] = $value;
  311. }
  312. else
  313. {
  314. throw new CException("Property '{$name}' does not exists");
  315. }
  316. }
  317. /**
  318. * Magic isset function. Checks if an attribute is set
  319. *
  320. * @param string $name
  321. * @return boolean
  322. */
  323. public function __isset($name)
  324. {
  325. $method = 'get'.str_replace('_', '', $name);
  326. if (method_exists($this, $method))
  327. {
  328. return $this->$method() !== null; // NULL returns FALSE
  329. }
  330. else
  331. {
  332. return (isset($this->attributes[$name]));
  333. }
  334. }
  335. /**
  336. * Magic unset function. Removes a given attribute
  337. *
  338. * @param string $name
  339. */
  340. public function __unset($name)
  341. {
  342. $method = 'set'.str_replace('_', '', $name);
  343. if (method_exists($this, $method))
  344. {
  345. $this->$method(null);
  346. }
  347. else
  348. {
  349. // Set to NULL, isset will return false
  350. $this->attributes[$name] = null;
  351. }
  352. }
  353. }
  354. ?>