PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/application/modules/public/models/RecordSearch2.php

http://geocontexter.googlecode.com/
PHP | 299 lines | 209 code | 29 blank | 61 comment | 28 complexity | 2d509a2db426bace06d0eeaf620119ff MD5 | raw file
  1. <?php
  2. /**
  3. * GeoContexter
  4. * @link http://code.google.com/p/geocontexter/
  5. * @package GeoContexter
  6. */
  7. /**
  8. * spatial search for records with no limit
  9. *
  10. USAGE:
  11. <pre>
  12. // tested with postgis 1.4.0
  13. //
  14. $record = new Geocontexter_Model_RecordSearch;
  15. $params = array('geom' => string, // ex.: ST_GeometryFromText('POINT(6.4 49.5)',4326)
  16. 'projects' => array of bigints, // Limit search to id_projects
  17. 'items' => array of bigints, // Limit search to id_items
  18. 'status' => array('=', 1000),
  19. 'date_from' => string,
  20. 'date_to' => string,
  21. 'buffer' => numeric,
  22. // get only custom attributes which have default_display = true (1)
  23. 'default_display' => bool,
  24. // if set, the function dont include an array var "attributes"
  25. 'no_transform_attributes' = bool);
  26. $result = $record->get( $params );
  27. if($result instanceof Mozend_ModelError)
  28. {
  29. $result->logError( __file__, __line__);
  30. }
  31. else
  32. {
  33. $this->view->result = $result;
  34. $totalNumRows = $record->totalNumRows();
  35. }
  36. </pre>
  37. * @package GeoContexter
  38. * @subpackage Module_Geocontexter
  39. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  40. * @author Armand Turpel <geocontexter@gmail.com>
  41. * @version $Rev: 828 $ / $LastChangedDate: 2011-02-27 10:30:28 +0100 (dim., 27 f?Švr. 2011) $ / $LastChangedBy: armand.turpel $
  42. */
  43. class Public_Model_RecordSearch2 extends Mozend_Model
  44. {
  45. /**
  46. * @param array $params
  47. */
  48. public function get( & $params )
  49. {
  50. $this->set_params( $params );
  51. if($this->is_error() === true)
  52. {
  53. return new Mozend_ModelError( $this->get_error() );
  54. }
  55. // check on preferred lists only
  56. //
  57. $_default_display = null;
  58. if(isset($params['default_display']))
  59. {
  60. $_default_display = $params['default_display'];
  61. }
  62. // check on preferred lists only
  63. //
  64. $_no_transform_attributes = false;
  65. if(isset($params['no_transform_attributes']))
  66. {
  67. $_no_transform_attributes = true;
  68. }
  69. try
  70. {
  71. $sql = 'SELECT rec.*,
  72. reci.id_item AS id_taxon ,
  73. reci.title AS taxon,
  74. reci.synonym_of,
  75. reci.id_attribute_group AS taxon_id_attribute_group,
  76. reci.attribute_value AS taxon_attribute_value,
  77. ST_Y(recg.geom_point) AS latitude,
  78. ST_X(recg.geom_point) AS longitude
  79. FROM geocontexter.gc_record AS rec
  80. INNER JOIN geocontexter.gc_record_geometry AS recg
  81. ON recg.id_record = rec.id_record
  82. INNER JOIN geocontexter.gc_item AS reci
  83. ON rec.id_item = reci.id_item
  84. WHERE ST_DWithin(' . $this->sql_geom_column . ',' . $this->geom . ', ' . $this->sql_buffer . ')
  85. ' . $this->sql_id_project . '
  86. ' . $this->sql_id_status . '
  87. ' . $this->sql_date_from . '
  88. ' . $this->sql_date_to . '
  89. ORDER BY rec.date_record_start DESC';
  90. if(true === $_no_transform_attributes)
  91. {
  92. return $this->db->fetchAll($sql);
  93. }
  94. // json decode of attribute_values
  95. //
  96. $result = $this->db->fetchAll($sql);
  97. $attr_json = new Geocontexter_Model_AttributeJsonDecode;
  98. foreach($result as & $res)
  99. {
  100. if(($res['id_attribute_group'] == 'NULL') || empty($res['attribute_value']))
  101. {
  102. $res['attributes'] = array();
  103. }
  104. else
  105. {
  106. $res['attributes'] = $attr_json->decode( $res['attribute_value'], $res['id_attribute_group'], $_default_display );
  107. }
  108. if(($res['taxon_id_attribute_group'] == 'NULL') || empty($res['taxon_id_attribute_group']))
  109. {
  110. $res['taxon_attributes'] = array();
  111. }
  112. else
  113. {
  114. $res['taxon_attributes'] = $attr_json->decode( $res['taxon_attribute_value'], $res['taxon_id_attribute_group'], $_default_display );
  115. }
  116. }
  117. if($this->is_error() === true)
  118. {
  119. return new Mozend_ModelError( $this->get_error() );
  120. }
  121. return $result;
  122. }
  123. catch(Zend_Db_Exception $e)
  124. {
  125. $this->set_error('Caught exception: ' . get_class($e));
  126. $this->set_error('Message: ' . $e->getMessage());
  127. return new Mozend_ModelError( $this->get_error() );
  128. }
  129. }
  130. /**
  131. * set and validate parameters
  132. *
  133. *
  134. * @param array $params
  135. */
  136. private function set_params( & $params )
  137. {
  138. if(!isset($params['geom']))
  139. {
  140. $this->set_error('geom field isnt defined');
  141. }
  142. else
  143. {
  144. $this->geom = $params['geom'];
  145. }
  146. $this->sql_id_status = '';
  147. if(isset($params['status']))
  148. {
  149. if(!is_array($params['status']))
  150. {
  151. $this->set_error('status isnt from type array');
  152. }
  153. elseif(!isset($params['status'][0]))
  154. {
  155. $this->set_error('status array index 1 not set');
  156. }
  157. elseif(!isset($params['status'][1]))
  158. {
  159. $this->set_error('status array index 2 not set');
  160. }
  161. elseif(!in_array($params['status'][0],array('>','<','>=','<=','=')))
  162. {
  163. $this->set_error('status array index 1 string not recognized');
  164. }
  165. elseif(!is_int($params['status'][1]))
  166. {
  167. $this->set_error('status array index 2 not from type int');
  168. }
  169. $this->sql_id_status = 'AND rec.id_status ' . $params['status'][0] . $params['status'][1];
  170. }
  171. $this->sql_buffer = 0;
  172. if(isset($params['buffer']))
  173. {
  174. $this->sql_buffer = $params['buffer'];
  175. }
  176. $this->sql_id_project = '';
  177. if(isset($params['projects']))
  178. {
  179. if(!is_array($params['projects']))
  180. {
  181. $this->set_error('projects isnt from type array');
  182. }
  183. else
  184. {
  185. $_projects = '';
  186. $comma = '';
  187. foreach($params['projects'] as $id_project)
  188. {
  189. if(false === Zend_Validate::is($id_project, 'Digits'))
  190. {
  191. $this->set_error('id_project in item array isnt from type bigint: ' . var_export($id_project,true));
  192. }
  193. else
  194. {
  195. $_projects .= $comma . $id_project;
  196. $comma = ',';
  197. }
  198. }
  199. $this->sql_id_project = ' AND rec.id_project IN (' . $_projects . ') ';
  200. }
  201. }
  202. $this->sql_date_to = '';
  203. if(isset($params['date_to']))
  204. {
  205. if(false === preg_match("/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/",$params['date_to']))
  206. {
  207. $this->set_error('wrong date format (ex. 2010-09-24) for date_to: ' . $params['date_to']);
  208. }
  209. else
  210. {
  211. $this->sql_date_to = "AND rec.date_record_start <= '" . $params['date_to'] . "'";
  212. }
  213. }
  214. $this->sql_date_from = '';
  215. if(isset($params['date_from']))
  216. {
  217. if(false === preg_match("/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/",$params['date_from']))
  218. {
  219. $this->set_error('wrong date format (ex. 2010-09-24) for date_from: ' . $params['date_from']);
  220. }
  221. else
  222. {
  223. $this->sql_date_from = "AND rec.date_record_start >= '" . $params['date_from'] . "'";
  224. }
  225. }
  226. if(isset($params['default_display']))
  227. {
  228. if(false === is_bool($params['default_display']))
  229. {
  230. $this->set_error('default_display isnt from type boolean');
  231. }
  232. }
  233. if(isset($params['no_transform_attributes']))
  234. {
  235. if(false === is_bool($params['no_transform_attributes']))
  236. {
  237. $this->set_error('no_transform_attributes isnt from type boolean');
  238. }
  239. }
  240. if(isset($params['geometry_column']))
  241. {
  242. if(false === in_array($params['geometry_column'], array('point', 'linestring', 'polygon')))
  243. {
  244. $this->set_error('geometry_type must be one of string: "point", "linestring", "polygon"');
  245. }
  246. else
  247. {
  248. $this->sql_geom_column = 'recg.geom_' . $params['geometry_column'];
  249. }
  250. }
  251. else
  252. {
  253. $this->sql_geom_column = 'ST_ConvexHull(ST_Collect(recg.geom_point, recg.geom_linestring, recg.geom_polygon))';
  254. }
  255. }
  256. }