/application/modules/public/models/RecordSearch2.php
PHP | 299 lines | 209 code | 29 blank | 61 comment | 28 complexity | 2d509a2db426bace06d0eeaf620119ff MD5 | raw file
- <?php
- /**
- * GeoContexter
- * @link http://code.google.com/p/geocontexter/
- * @package GeoContexter
- */
-
- /**
- * spatial search for records with no limit
- *
- USAGE:
- <pre>
- // tested with postgis 1.4.0
- //
- $record = new Geocontexter_Model_RecordSearch;
-
- $params = array('geom' => string, // ex.: ST_GeometryFromText('POINT(6.4 49.5)',4326)
- 'projects' => array of bigints, // Limit search to id_projects
- 'items' => array of bigints, // Limit search to id_items
- 'status' => array('=', 1000),
- 'date_from' => string,
- 'date_to' => string,
- 'buffer' => numeric,
- // get only custom attributes which have default_display = true (1)
- 'default_display' => bool,
-
- // if set, the function dont include an array var "attributes"
- 'no_transform_attributes' = bool);
-
- $result = $record->get( $params );
-
- if($result instanceof Mozend_ModelError)
- {
- $result->logError( __file__, __line__);
- }
- else
- {
- $this->view->result = $result;
- $totalNumRows = $record->totalNumRows();
- }
-
- </pre>
- * @package GeoContexter
- * @subpackage Module_Geocontexter
- * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
- * @author Armand Turpel <geocontexter@gmail.com>
- * @version $Rev: 828 $ / $LastChangedDate: 2011-02-27 10:30:28 +0100 (dim., 27 f?vr. 2011) $ / $LastChangedBy: armand.turpel $
- */
-
- class Public_Model_RecordSearch2 extends Mozend_Model
- {
- /**
- * @param array $params
- */
- public function get( & $params )
- {
- $this->set_params( $params );
-
- if($this->is_error() === true)
- {
- return new Mozend_ModelError( $this->get_error() );
- }
-
- // check on preferred lists only
- //
- $_default_display = null;
- if(isset($params['default_display']))
- {
- $_default_display = $params['default_display'];
- }
-
- // check on preferred lists only
- //
- $_no_transform_attributes = false;
- if(isset($params['no_transform_attributes']))
- {
- $_no_transform_attributes = true;
- }
-
- try
- {
- $sql = 'SELECT rec.*,
- reci.id_item AS id_taxon ,
- reci.title AS taxon,
- reci.synonym_of,
- reci.id_attribute_group AS taxon_id_attribute_group,
- reci.attribute_value AS taxon_attribute_value,
- ST_Y(recg.geom_point) AS latitude,
- ST_X(recg.geom_point) AS longitude
-
- FROM geocontexter.gc_record AS rec
-
- INNER JOIN geocontexter.gc_record_geometry AS recg
- ON recg.id_record = rec.id_record
-
- INNER JOIN geocontexter.gc_item AS reci
- ON rec.id_item = reci.id_item
-
- WHERE ST_DWithin(' . $this->sql_geom_column . ',' . $this->geom . ', ' . $this->sql_buffer . ')
-
- ' . $this->sql_id_project . '
-
- ' . $this->sql_id_status . '
-
- ' . $this->sql_date_from . '
-
- ' . $this->sql_date_to . '
-
- ORDER BY rec.date_record_start DESC';
-
- if(true === $_no_transform_attributes)
- {
- return $this->db->fetchAll($sql);
- }
-
- // json decode of attribute_values
- //
-
- $result = $this->db->fetchAll($sql);
-
- $attr_json = new Geocontexter_Model_AttributeJsonDecode;
-
- foreach($result as & $res)
- {
- if(($res['id_attribute_group'] == 'NULL') || empty($res['attribute_value']))
- {
- $res['attributes'] = array();
- }
- else
- {
- $res['attributes'] = $attr_json->decode( $res['attribute_value'], $res['id_attribute_group'], $_default_display );
- }
-
- if(($res['taxon_id_attribute_group'] == 'NULL') || empty($res['taxon_id_attribute_group']))
- {
- $res['taxon_attributes'] = array();
- }
- else
- {
- $res['taxon_attributes'] = $attr_json->decode( $res['taxon_attribute_value'], $res['taxon_id_attribute_group'], $_default_display );
- }
- }
-
- if($this->is_error() === true)
- {
- return new Mozend_ModelError( $this->get_error() );
- }
-
- return $result;
- }
- catch(Zend_Db_Exception $e)
- {
- $this->set_error('Caught exception: ' . get_class($e));
- $this->set_error('Message: ' . $e->getMessage());
-
- return new Mozend_ModelError( $this->get_error() );
- }
- }
-
- /**
- * set and validate parameters
- *
- *
- * @param array $params
- */
- private function set_params( & $params )
- {
- if(!isset($params['geom']))
- {
- $this->set_error('geom field isnt defined');
- }
- else
- {
- $this->geom = $params['geom'];
- }
-
-
- $this->sql_id_status = '';
- if(isset($params['status']))
- {
- if(!is_array($params['status']))
- {
- $this->set_error('status isnt from type array');
- }
- elseif(!isset($params['status'][0]))
- {
- $this->set_error('status array index 1 not set');
- }
- elseif(!isset($params['status'][1]))
- {
- $this->set_error('status array index 2 not set');
- }
- elseif(!in_array($params['status'][0],array('>','<','>=','<=','=')))
- {
- $this->set_error('status array index 1 string not recognized');
- }
- elseif(!is_int($params['status'][1]))
- {
- $this->set_error('status array index 2 not from type int');
- }
-
- $this->sql_id_status = 'AND rec.id_status ' . $params['status'][0] . $params['status'][1];
- }
-
- $this->sql_buffer = 0;
- if(isset($params['buffer']))
- {
- $this->sql_buffer = $params['buffer'];
- }
-
- $this->sql_id_project = '';
- if(isset($params['projects']))
- {
- if(!is_array($params['projects']))
- {
- $this->set_error('projects isnt from type array');
- }
- else
- {
- $_projects = '';
- $comma = '';
-
- foreach($params['projects'] as $id_project)
- {
- if(false === Zend_Validate::is($id_project, 'Digits'))
- {
- $this->set_error('id_project in item array isnt from type bigint: ' . var_export($id_project,true));
- }
- else
- {
- $_projects .= $comma . $id_project;
- $comma = ',';
- }
- }
- $this->sql_id_project = ' AND rec.id_project IN (' . $_projects . ') ';
- }
- }
-
- $this->sql_date_to = '';
-
- if(isset($params['date_to']))
- {
- if(false === preg_match("/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/",$params['date_to']))
- {
- $this->set_error('wrong date format (ex. 2010-09-24) for date_to: ' . $params['date_to']);
- }
- else
- {
- $this->sql_date_to = "AND rec.date_record_start <= '" . $params['date_to'] . "'";
- }
- }
-
- $this->sql_date_from = '';
-
- if(isset($params['date_from']))
- {
- if(false === preg_match("/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/",$params['date_from']))
- {
- $this->set_error('wrong date format (ex. 2010-09-24) for date_from: ' . $params['date_from']);
- }
- else
- {
- $this->sql_date_from = "AND rec.date_record_start >= '" . $params['date_from'] . "'";
- }
- }
-
- if(isset($params['default_display']))
- {
- if(false === is_bool($params['default_display']))
- {
- $this->set_error('default_display isnt from type boolean');
- }
- }
-
- if(isset($params['no_transform_attributes']))
- {
- if(false === is_bool($params['no_transform_attributes']))
- {
- $this->set_error('no_transform_attributes isnt from type boolean');
- }
- }
-
- if(isset($params['geometry_column']))
- {
- if(false === in_array($params['geometry_column'], array('point', 'linestring', 'polygon')))
- {
- $this->set_error('geometry_type must be one of string: "point", "linestring", "polygon"');
- }
- else
- {
- $this->sql_geom_column = 'recg.geom_' . $params['geometry_column'];
- }
- }
- else
- {
- $this->sql_geom_column = 'ST_ConvexHull(ST_Collect(recg.geom_point, recg.geom_linestring, recg.geom_polygon))';
- }
- }
- }