Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Import images from source.

  • Loading branch information...
commit 3ed4d9727bad1d86de6c20bc8ed70d57cc8565c2 1 parent 10905af
@mikl mikl authored
Showing with 80 additions and 1 deletion.
  1. +80 −1 kdb_feed_import/event.inc
View
81 kdb_feed_import/event.inc
@@ -4,6 +4,7 @@
* Import event feeds.
*/
+
/**
* Base class for all our migrations.
*/
@@ -66,8 +67,13 @@ class KDBFeedImportSource extends MigrateSource {
'startdate' => $element->getElementsByTagName('startdate')->item(0)->nodeValue,
'enddate' => $element->getElementsByTagName('enddate')->item(0)->nodeValue,
'price' => $element->getElementsByTagName('price')->item(0)->nodeValue,
+ 'images' => array(),
);
+ foreach ($element->getElementsByTagName('image') as $img) {
+ $entry->images[] = $img->nodeValue;
+ }
+
$institution = $element->getElementsByTagName('institution')->item(0)->getAttribute('id');
// Map the foreign site's institution ID to a local node ID.
@@ -99,6 +105,7 @@ class KDBFeedImportSource extends MigrateSource {
'email',
'phone',
'price',
+ 'images',
);
}
@@ -211,8 +218,13 @@ class KDBEventFeedImport extends KDBFeedImportBase {
$node->field_datetime[0]['value'] = self::convertTimestamp($row->startdate);
$node->field_datetime[0]['value2'] = self::convertTimestamp($row->enddate);
- if (!empty($price)) {
+ // Save files.
+ $node->field_list_image = array();
+ foreach ($row->images as $image_url) {
+ $node->field_list_image[] = self::saveFileFromURL($image_url);
+ }
+ if (!empty($price)) {
$matches = array();
// field_entry_price is supposed to be a number, so we sanitise it.
@@ -227,4 +239,71 @@ class KDBEventFeedImport extends KDBFeedImportBase {
}
}
}
+
+ /**
+ * Save imported file to disk.
+ */
+ public static function saveFileFromURL($url) {
+ $ch = curl_init(urldecode($url));
+
+ // Create a temporary file for the download.
+ $tempfile = tempnam(file_directory_temp(), 'kdb_feed_import');
+ $fp = fopen($tempfile, "w");
+
+ // Write the downloaded data to the temporary file.
+ curl_setopt($ch, CURLOPT_FILE, $fp);
+
+ // Follow redirects.
+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
+
+ // Don't include headers in output.
+ curl_setopt($ch, CURLOPT_HEADER, FALSE);
+
+ curl_exec($ch);
+ curl_close($ch);
+ fclose($fp);
+
+ // Get the original filename from the URL.
+ $url_parts = explode('/', parse_url($url, PHP_URL_PATH));
+ $filename = urldecode(array_pop($url_parts));
+
+ // Use transliteration module to sanitize the filename if possible.
+ if (function_exists('transliteration_clean_filename')) {
+ $filename = transliteration_clean_filename($filename);
+ }
+
+ // Make sure we have a suitable folder for saving the images.
+ $imagepath = file_create_path('kdb_feed_import');
+ $destination = $imagepath . '/' . $filename;
+
+ if (file_check_directory($imagepath, FILE_CREATE_DIRECTORY)) {
+ if (file_move($tempfile, $destination, FILE_EXISTS_REPLACE)) {
+ $file = array(
+ 'filename' => $filename,
+ 'filepath' => $destination,
+ 'filemime' => file_get_mimetype($destination),
+ 'filesize' => filesize($destination),
+ 'destination' => $destination,
+ 'status' => FILE_STATUS_PERMANENT,
+ );
+
+ // If file already exists, load its curent data and store it,
+ // before saving.
+ $query = db_query("SELECT * FROM {files} WHERE filepath = '%s'", array(
+ ':filepath' => $destination,
+ ));
+ if ($row = db_fetch_array($query)) {
+ $file = array_merge($row, $file);
+ drupal_write_record('files', $file, array('fid'));
+ }
+ // If not, create a new file entry.
+ else {
+ $file['timestamp'] = $_SERVER['REQUEST_TIME'];
+ drupal_write_record('files', $file);
+ }
+
+ return $file;
+ }
+ }
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.