/[drupal]/contributions/modules/votesmart/modules/votesmart_field/votesmart_field.module
ViewVC logotype

Diff of /contributions/modules/votesmart/modules/votesmart_field/votesmart_field.module

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

revision 1.4, Tue Sep 9 01:11:46 2008 UTC revision 1.5, Wed Oct 1 00:08:28 2008 UTC
# Line 1  Line 1 
1  <?php // $Id: votesmart_field.module,v 1.3 2008/08/17 15:36:33 vauxia Exp $  <?php // $Id: votesmart_field.module,v 1.4 2008/09/09 01:11:46 vauxia Exp $
2    
3  /**  /**
4   * Implementation of hook_elements().   * Implementation of hook_elements().
# Line 143  function _votesmart_field_map_data(&$nod Line 143  function _votesmart_field_map_data(&$nod
143        if ($tok && isset($data[$tok])) {        if ($tok && isset($data[$tok])) {
144          if (!is_array($data[$tok])) $data[$tok] = array($data[$tok]);          if (!is_array($data[$tok])) $data[$tok] = array($data[$tok]);
145          foreach ($data[$tok] as $delta => $value) {          foreach ($data[$tok] as $delta => $value) {
146            $node->{$field_name}[$delta][$col] = $value;            // Special-case image fields.  I'm open to other ideas here!
147              if ($tok == 'photo') {
148                $file = _votesmart_field_save_file($value);
149                $node->{$field_name}[$delta] = (array) $file;
150                $node->{$field_name}[$delta]['list'] = 1;
151              }
152              else {
153                $node->{$field_name}[$delta][$col] = $value;
154              }
155          }          }
156        }        }
157      }      }
# Line 195  function _votesmart_field_map_form($node Line 203  function _votesmart_field_map_form($node
203    }    }
204    return $form;    return $form;
205  }  }
206    
207    /**
208     * Fake up a file upload and save it for use in an image field.  I'm sure there
209     * are much better ways of doing this, and I'd love some insight.  But for now
210     * this "works".
211     */
212    function _votesmart_field_save_file($url) {
213      global $user;
214    
215      $parts = parse_url($url);
216      $name = basename($parts['path']);
217      $path = file_directory_path() . '/votesmart'. $parts['path'];
218    
219      $dest = file_destination($path, FILE_EXISTS_REPLACE);
220    
221      if ($file = db_fetch_object(db_query("SELECT * FROM {files} WHERE filepath = '%s'", $dest))) {
222        //return $file;
223      }
224    
225      $data = file_get_contents($url);
226    
227      if (!is_dir($dir = dirname($dest))) {
228        mkdir($dir, 0777, TRUE);
229      }
230      file_put_contents($dest, $data);
231    
232      $file = new stdClass;
233      $file->uid = $user->uid;
234      $file->status = 1;
235      $file->timestamp = time();
236      $file->filepath = $dest;
237      $file->filename = $name;
238      $file->filemime = 'image/jpeg';
239      $file->filesize = strlen($data);
240    
241      drupal_write_record('files', $file);
242      return $file;
243    }

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.2