PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Maps/MapDB/MapDBDataRetriever.php

http://github.com/modolabs/Kurogo-Mobile-Web
PHP | 156 lines | 124 code | 29 blank | 3 comment | 8 complexity | a64794313b60f068ff21cb2d14bf65ba MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. class MapDBDataRetriever extends DatabaseDataRetriever
  3. {
  4. protected $campus; // campus
  5. protected $categoryId;
  6. protected $lat;
  7. protected $lon;
  8. protected $distance = 0;
  9. public function init($args) {
  10. parent::init($args);
  11. $this->campus = $args['GROUP'];
  12. }
  13. public function setCategoryId($categoryId) {
  14. $this->categoryId = $categoryId;
  15. }
  16. public function setDistance($distance) {
  17. $this->distance = $distance;
  18. }
  19. public function setCoordinates(Array $coordinates) {
  20. $this->lat = $coordinates['lat'];
  21. $this->lon = $coordinates['lon'];
  22. }
  23. public function reset() {
  24. $this->lat = null;
  25. $this->lon = null;
  26. $this->distance = 0;
  27. }
  28. public function getPlacemarkDetails($placemarkId) {
  29. $center = $placemark->getGeometry()->getCenterCoordinate();
  30. $sql = 'SELECT property_name, property_value FROM '.self::PLACEMARK_PROPERTIES_TABLE
  31. .' WHERE placemark_id = ? AND lat = ? AND lon = ?';
  32. $params = array($placemark->getId(), $center['lat'], $center['lon']);
  33. $this->setSQL($sql);
  34. $this->setParameters($params);
  35. $this->setContext('type', null);
  36. return $this->retrieveData();
  37. }
  38. public function getPlacemarkStyle($placemarkId) {
  39. $style = $placemark->getStyle();
  40. if (method_exists($style, 'getId')) {
  41. $styleId = $style->getId();
  42. $this->setSQL(
  43. .'SELECT property_name, property_value FROM '.self::PLACEMARK_STYLES_TABLE
  44. .' WHERE style_id = ?');
  45. $this->setParameters(array($styleId));
  46. $this->setContext('type', null);
  47. return $this->retrieveData();
  48. }
  49. return null;
  50. }
  51. public function setContext($type)
  52. {
  53. switch ($type) {
  54. case 'category':
  55. $this->setupCategoryContext();
  56. break;
  57. case 'categories':
  58. $this->setupSubcategoryContext();
  59. break;
  60. case 'placemarks':
  61. $this->setupPlacemarkContext();
  62. break;
  63. }
  64. }
  65. // categoryForId
  66. public function setupCategoryContext() {
  67. $this->setSQL('SELECT * FROM '.self::CATEGORY_TABLE
  68. .' WHERE campus = ? AND category_id = ?');
  69. $this->setParameters(array($this->campus, $this->categoryId));
  70. $this->setContext('type', 'category');
  71. }
  72. // childrenForCategory
  73. public function setupSubcategoryContext() {
  74. $sql = 'SELECT * FROM '.self::CATEGORY_TABLE
  75. .' WHERE campus = ? AND parent_category_id = ?';
  76. $params = array($this->campus, $this->categoryId);
  77. $this->setSQL($sql);
  78. $this->setParameters($params);
  79. $this->setContext('type', 'category');
  80. }
  81. // featuresForCategory
  82. public function setupPlacemarkContext()
  83. {
  84. $sql = 'SELECT p.*, pc.category_id FROM '
  85. .self::PLACEMARK_TABLE.' p, '.self::PLACEMARK_CATEGORY_TABLE.' pc'
  86. .' WHERE p.placemark_id = pc.placemark_id'
  87. .' AND p.lat = pc.lat AND p.lon = pc.lon';
  88. $conditions = array();
  89. $params = array();
  90. $sort = array();
  91. $sortParams = array();
  92. if (isset($this->lat, $this->lon)) {
  93. if ($this->distance > 0) {
  94. $conditions[] = 'p.lat = ? AND p.lon = ?';
  95. $params[] = $this->lat;
  96. $params[] = $this->lon;
  97. } else {
  98. $center = array('lat' => $this->lat, 'lon' => $this->lon);
  99. $bbox = normalizedBoundingBox($center, $tolerance, null, null);
  100. $conditions[] = 'p.lat >= ? AND p.lat < ? AND p.lon >= ? AND p.lon < ?';
  101. array_push($params,
  102. $bbox['min']['lat'], $bbox['max']['lat'],
  103. $bbox['min']['lon'], $bbox['max']['lon']);
  104. $sort[] = 'ORDER BY (p.lat - ?)*(p.lat - ?) + (p.lon - ?)*(p.lon - ?)'
  105. $sortParams = array(
  106. $bbox['center']['lat'], $bbox['center']['lat'],
  107. $bbox['center']['lon'], $bbox['center']['lon'],
  108. );
  109. }
  110. }
  111. if (isset($this->categoryId)) {
  112. $conditions[] = 'pc.category_id = ?';
  113. $params[] = $this->categoryId;
  114. }
  115. if ($conditions) {
  116. $sql .= ' AND '.implode(' AND ', $conditions);
  117. }
  118. if ($sort) {
  119. $sql .= ' ORDER BY '.implode(', ', $sort);
  120. $params = array_merge($params, $sortParams);
  121. }
  122. $this->setSQL($sql);
  123. $this->setParameters($params);
  124. $this->setContext('type', 'placemark');
  125. return $this->retrieveData();
  126. }
  127. }