/**
  * Before submission, TODO perhaps?
  */
 protected function preSubmit()
 {
     kohana::log('debug', 'In Subject_observation_Model::preSubmit() $this->submission is ' . print_r($this->submission, true));
     // if sample_id not set in occurrence submissions, then set it now
     if (array_key_exists('subModels', $this->submission)) {
         foreach ($this->submission['subModels'] as &$subModel) {
             if (array_key_exists('model', $subModel) && array_key_exists('id', $subModel['model']) && $subModel['model']['id'] === 'occurrences_subject_observation' && array_key_exists('superModels', $subModel['model'])) {
                 foreach ($subModel['model']['superModels'] as &$superModel) {
                     if (array_key_exists('model', $superModel) && array_key_exists('id', $superModel['model']) && $superModel['model']['id'] === 'occurrence' && array_key_exists('fields', $superModel['model']) && array_key_exists('sample_id', $superModel['model']['fields']) && array_key_exists('value', $superModel['model']['fields']['sample_id']) && $superModel['model']['fields']['sample_id']['value'] == 0 && array_key_exists('fields', $this->submission) && array_key_exists('sample_id', $this->submission['fields']) && array_key_exists('value', $this->submission['fields']['sample_id'])) {
                         $superModel['model']['fields']['sample_id']['value'] = $this->submission['fields']['sample_id']['value'];
                     }
                 }
             }
         }
     }
     return parent::presubmit();
 }
Ejemplo n.º 2
0
 /** 
  * Handle the case where a new record is created with a centroid_sref but without the geom being pre-calculated.
  * E.g. when importing from a shape file, or when JS is disabled on the client. 
  */
 protected function preSubmit()
 {
     // Allow a location to be submitted with a spatial ref and system but no centroid_geom. If so we
     // can work out the Geom
     if (!empty($this->submission['fields']['centroid_sref']['value']) && !empty($this->submission['fields']['centroid_sref_system']['value']) && empty($this->submission['fields']['centroid_geom']['value'])) {
         try {
             $this->submission['fields']['centroid_geom']['value'] = spatial_ref::sref_to_internal_wkt($this->submission['fields']['centroid_sref']['value'], $this->submission['fields']['centroid_sref_system']['value']);
         } catch (Exception $e) {
             $this->errors['centroid_sref'] = $e->getMessage();
         }
     } elseif (empty($this->submission['fields']['centroid_sref']['value']) && empty($this->submission['fields']['centroid_geom']['value']) && !empty($this->submission['fields']['boundary_geom']['value'])) {
         kohana::log('debug', 'working out centroid from boundary');
         // if the geom is supplied for the boundary, but not the centroid sref, then calculate it.
         // First, convert the boundary geom to a centroid using any provided system, else use LatLong (EPSG:4326)
         $boundary = $this->submission['fields']['boundary_geom']['value'];
         if (!empty($this->submission['fields']['centroid_sref_system']['value'])) {
             $centroid = $this->calcCentroid($boundary, $this->submission['fields']['centroid_sref_system']['value']);
         } else {
             $centroid = $this->calcCentroid($boundary);
         }
         $this->submission['fields']['centroid_geom']['value'] = $centroid['wkt'];
         $this->submission['fields']['centroid_sref']['value'] = $centroid['sref'];
         $this->submission['fields']['centroid_sref_system']['value'] = $centroid['sref_system'];
     }
     // Empty boundary geom is allowed but must be null
     if (isset($this->submission['fields']['boundary_geom']['value']) && empty($this->submission['fields']['boundary_geom']['value'])) {
         $this->submission['fields']['boundary_geom']['value'] = null;
     }
     return parent::presubmit();
 }
Ejemplo n.º 3
0
 /**
  * Before submission, map vague dates to their underlying database fields.
  */
 protected function preSubmit()
 {
     if (array_key_exists('date', $this->submission['fields'])) {
         $vague_date = vague_date::string_to_vague_date($this->submission['fields']['date']['value']);
         $this->submission['fields']['date_start']['value'] = $vague_date[0];
         $this->submission['fields']['date_end']['value'] = $vague_date[1];
         $this->submission['fields']['date_type']['value'] = $vague_date[2];
     }
     // Allow a sample to be submitted with a spatial ref and system but no Geom. If so we
     // can work out the Geom
     if (array_key_exists('entered_sref', $this->submission['fields']) && array_key_exists('entered_sref_system', $this->submission['fields']) && !(array_key_exists('geom', $this->submission['fields']) && $this->submission['fields']['geom']['value']) && $this->submission['fields']['entered_sref']['value'] && $this->submission['fields']['entered_sref_system']['value']) {
         try {
             $this->submission['fields']['geom']['value'] = spatial_ref::sref_to_internal_wkt($this->submission['fields']['entered_sref']['value'], $this->submission['fields']['entered_sref_system']['value']);
         } catch (Exception $e) {
             $this->errors['entered_sref'] = $e->getMessage();
         }
     }
     return parent::presubmit();
 }