/[drupal]/contributions/modules/geo/modules/geo_field/geo_field.module
ViewVC logotype

Diff of /contributions/modules/geo/modules/geo_field/geo_field.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.26 by vauxia, Mon Nov 9 17:44:40 2009 UTC revision 1.27 by vauxia, Tue Nov 17 19:07:21 2009 UTC
# Line 1  Line 1 
1  <?php // $Id: geo_field.module,v 1.25 2009/08/07 16:54:23 vauxia Exp $  <?php // $Id: geo_field.module,v 1.26 2009/11/09 17:44:40 vauxia Exp $
2    
3  /**  /**
4   * @file   * @file
# Line 94  function geo_field_settings($op, $field) Line 94  function geo_field_settings($op, $field)
94   * Implementation of hook_field().   * Implementation of hook_field().
95   */   */
96  function geo_field($op, &$node, $field, &$items, $teaser, $page) {  function geo_field($op, &$node, $field, &$items, $teaser, $page) {
97      static $widget_info;
98    
99      // We need the widget type, which indicates the gis format for conversion.
100      if (!isset($widget_info)) {
101        $widget_info  = _content_widget_types();
102      }
103    
104      // Set the $gis_input format based on the widget that is currently in use.
105      $gis_input = $widget_info[$field['widget']['type']]['gis input'];
106    
107    switch ($op) {    switch ($op) {
108    
# Line 133  function geo_field($op, &$node, $field, Line 142  function geo_field($op, &$node, $field,
142      case 'validate':      case 'validate':
143        foreach ($items as $k => $item) {        foreach ($items as $k => $item) {
144    
145            // Nothing to see here.
146          if(geo_content_is_empty($item, $field)) return;          if(geo_content_is_empty($item, $field)) return;
147    
148          if (($error = geo('wkt_validate', $item['geo'], $field['geo_type'])) !== TRUE) {          // Convert to $wkt and validate it.
149            $wkt = geo_value_convert($item['geo'], $gis_input, 'wkt');
150    
151            if (($error = geo('wkt_validate', $wkt, $field['geo_type'])) !== TRUE) {
152            $error_field = $field['field_name'];            $error_field = $field['field_name'];
153            form_set_error($error_field, $error);            form_set_error($error_field, $error);
154          }          }
         $items[$k] = $item;  
155        }        }
156        return;        return;
157    
158      case 'presave':      case 'presave':
159        foreach ($items as $k => $item) {        foreach ($items as $k => $item) {
         // Convert the wkt to binary data before the insert. This allows the  
         // input to pass through the binary query substitutions.  
160          if(!geo_content_is_empty($item, $field)) {          if(!geo_content_is_empty($item, $field)) {
161            $item['geo'] = db_result(db_query("SELECT GeomFromText('%s', %d)", $item['geo'], $field['srid']));  
162              // Convert to WKT (TODO we don't have direct-to-wkb functions yet!)
163              $wkt = geo_value_convert($item['geo'], $gis_input, 'wkt');
164    
165              // Convert the WKT to binary data before the insert. This allows the
166              // input to pass through the binary query substitutions.
167              $items[$k]['geo'] = db_result(db_query("SELECT GeomFromText('%s', %d)", $wkt, $field['srid']));
168          }          }
         $items[$k] = $item;  
169        }        }
170        return;        return;
171    
# Line 186  function geo_field_widget_info() { Line 201  function geo_field_widget_info() {
201   * Implementation of hook_widget().   * Implementation of hook_widget().
202   */   */
203  function geo_field_widget(&$form, &$form_state, $field, $items, $delta = 0) {  function geo_field_widget(&$form, &$form_state, $field, $items, $delta = 0) {
204    $info = module_invoke_all('gis_input_info');    $widget_info  = _content_widget_types();
205    $info = $info[$field['widget']['type']];    $info = $widget_info[$field['widget']['type']];
206    
207    foreach ($items as $key => $item) {    foreach ($items as $key => $item) {
208      // Convert from the storage format (wkb) to the element's expected format.      // Convert from the storage format (wkb) to the element's expected format.
209      if ($info['gis input']) {      if (!isset($item['geo'])) {
210        if (isset($item['wkb'])) {        if ($info['gis input']) {
211          $data = geo_wkb_get_data($item['wkb'], $info['gis input']);          if (isset($item['wkb'])) {
212          $items[$key] = $data['value'];            $data = geo_wkb_get_data($item['wkb'], $info['gis input']);
213        }            $items[$key]['geo'] = $data['value'];
214        else {          }
         $items[$key] = '';  
215        }        }
216      }      }
217    }    }
218    
219    $element['#theme'] = 'geo_fields';    $element['#theme'] = 'geo_fields';
220    $element['geo'] = array_merge($info['element'], array(    $element['geo'] = array_merge($info['element'], array(
221      '#default_value' => isset($items[$delta]) ? $items[$delta] : '',      '#default_value' => isset($items[$delta]['geo']) ? $items[$delta]['geo'] : NULL,
     '#gis_type' => $field['geo_type'],  
     '#gis_input' => $info['gis input'], // The format expected by the element.  
     '#gis_output' => 'wkt', // The format expected by geo_field's storage.  
222      '#delta' => $delta,      '#delta' => $delta,
223      '#title' => t($field['widget']['label']),      '#title' => t($field['widget']['label']),
224      '#required' => $field['required'],      '#required' => $field['required'],
225      '#description' => $field['widget']['description'],      '#description' => $field['widget']['description'],
226    ));    ));
227    
   if (!isset($element['geo']['#after_build'])) {  
     $element['geo']['#after_build'] = array();  
   }  
   $element['geo']['#after_build'][] = 'geo_element_set_value';  
228    return $element;    return $element;
229  }  }
230    

Legend:
Removed from v.1.26  
changed lines
  Added in v.1.27

  ViewVC Help
Powered by ViewVC 1.1.3