From a7eb15f1067fe9d6a1da9c0bd6b2671cb7b6f12a Mon Sep 17 00:00:00 2001
From: Nathan Rambeck <nathan@rambeck.com>
Date: Thu, 10 Nov 2011 10:59:41 -0500
Subject: [PATCH] D7 Changes

---
 path_redirect_import.info   |    9 ++--
 path_redirect_import.module |  118 ++++++++++++++++++++++--------------------
 redirect-import-sample.csv  |    7 +++
 3 files changed, 73 insertions(+), 61 deletions(-)
 mode change 100755 => 100644 path_redirect_import.info
 create mode 100644 redirect-import-sample.csv

diff --git a/path_redirect_import.info b/path_redirect_import.info
old mode 100755
new mode 100644
index b73c07c..48e9376
--- a/path_redirect_import.info
+++ b/path_redirect_import.info
@@ -1,5 +1,4 @@
-; $Id$
-name = 	Path redirect import
-description = Provides the ability to import path for Path Redirect
-core = 6.x
-dependencies[] = path_redirect
\ No newline at end of file
+name = Redirect Import
+description = Provides the ability to import redirects for the Redirect module
+core = 7.x
+dependencies[] = redirect
diff --git a/path_redirect_import.module b/path_redirect_import.module
index 6f34c21..2a70990 100755
--- a/path_redirect_import.module
+++ b/path_redirect_import.module
@@ -1,30 +1,15 @@
 <?php
-// $Id$
-/**
- * @file
- * Author: www.ag-prime.com
- */
-
-
-/**
- * Implementation of hook_perm().
- */
-function path_redirect_import_perm() {
-  return array(
-    'admin path redirect import',
-    );
-}
 
 /**
  * Implementation of hook_menu().
  */
 function path_redirect_import_menu() {
-  $items['admin/build/path-redirect/import'] = array(
+  $items['admin/config/search/redirect/import'] = array(
     'title' => 'Import',
     'description' => 'Import from CSV file',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('path_redirect_import_form'),
-    'access arguments' => array('admin path redirect import'),
+    'access arguments' => array('administer redirects'),
     'type' => MENU_LOCAL_TASK,
   );
   return $items;
@@ -38,10 +23,12 @@ function path_redirect_import_form() {
   $form['csv'] = array(
     '#type' => 'fieldset',
     '#title' => t('Import from .csv file'),
+    '#description' => t('To import redirects, you must create a CSV file in the same format as <a href="@sample_url">this sample file</a>.',
+                        array('@sample_url' => base_path() . drupal_get_path('module', 'path_redirect_import') . '/redirect-import-sample.csv'))
   );
   $form['csv']['csv_file'] = array(
     '#type' => 'file',
-    '#description' => t('File structure: "old url","new_url","redirect_code" (defaults to 301)'),
+    '#description' => t('The CSV file must include columns in the following order: "From URL","To URL","Redirect Code" (defaults to 301)'),
   );
   $form['submit'] = array('#type' => 'submit', '#value' => t('Import'));
   $form['#attributes'] = array('enctype' => "multipart/form-data");
@@ -51,28 +38,11 @@ function path_redirect_import_form() {
  * validate form function
  */
 function path_redirect_import_form_validate($form, &$form_state) {
-  if ($file = file_save_upload('csv_file')) {
-    $base = file_directory_path();
-    $target = "";
-    if (!is_dir($base . "/upload")) {
-      if (mkdir($base . "/upload"))
-        $target = "/upload";
-    }
-    else $target = "/upload";
-    if (!is_dir($base . "/upload/csv")) {
-      if (mkdir($base . "/upload/csv"))
-        $target = "/upload/csv";
-    }
-    else $target = "/upload/csv";
-    $destination = $base . $target . "/" . $file->filename;
-    if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
-      $form_state['uploaded_file'] = $destination;
-    }
-    else{
-      form_set_error('form', t('File could not be copied to its destination.'));
-    }
+  $validators = array('file_validate_extensions' => array('csv'));
+  if ($file = file_save_upload('csv_file', $validators)) {
+    $form_state['uploaded_file'] = $file;
   }
-  else{
+  else {
     form_set_error('form', t('File upload failed.'));
   }
 }
@@ -80,30 +50,34 @@ function path_redirect_import_form_validate($form, &$form_state) {
  * Submit form function
  */
 function path_redirect_import_form_submit($form, &$form_state) {
-  if (!isset($form_state['uploaded_file'])) return;
-  $_SESSION['allomundo_cards_upload_report'] = NULL;
-  if (!$f = fopen($form_state['uploaded_file'], 'r')) {
+  ini_set('auto_detect_line_endings', TRUE);
+  if (!isset($form_state['uploaded_file'])) {
+    return;
+  }
+  if (!$f = fopen($form_state['uploaded_file']->uri, 'r')) {
     form_set_error('form', t("Could not open uploaded file."));
   }
   $line_no = 0;
   $count = 0;
-  $output='';
   while ($line = fgetcsv($f, 1024, ",")) {
     $line_no++;
+    if ($line_no == 1) {
+      continue;
+    }
     $error = '';
     if (!is_array($line)) {
       $error .= t('Line %line_no is invalid.', array('%line_no' => $line_no)) . '<br />';
     }
-    if ($line[0] == '' || $line[1] == '') {
+    if (empty($line[0]) || empty($line[1])) {
       $error .= t('Line %line_no contains invalid data.', array('%line_no' => $line_no)) . '<br />';
     }
-    if ($line[2] == '') {
+    if (empty($line[2])) {
       $line[2] = '301';
     }
     $data = array(
-      'old_url' => $line[0],
-      'new_url' => $line[1],
-      'redirect_code' => $line[2],
+      'source' => $line[0],
+      'redirect' => $line[1],
+      'state_code' => $line[2],
     );
     if ($error == '') {
       if (!path_redirect_import_save_data($data)) {
@@ -117,6 +91,8 @@ function path_redirect_import_form_submit($form, &$form_state) {
       drupal_set_message($error, 'error');
     }
   }
+  fclose($f);
+  file_delete($form_state['uploaded_file']);
   if ($count > 0) {
     drupal_set_message(t('%count row(s) imported.', array('%count' => $count)), 'message');
   }
@@ -126,14 +102,44 @@ function path_redirect_import_form_submit($form, &$form_state) {
  * Function for save data from .csv file into the database
  */
 function path_redirect_import_save_data($data) {
-  $old_url = $data['old_url'];
-  $new_url = $data['new_url'];
-  $redirect_code = $data['redirect_code'];
-  $sql = "REPLACE INTO {path_redirect} (source, redirect, type) VALUES ('%s', '%s', %d)";
-  if (!$result = db_query($sql, $old_url, $new_url, $redirect_code)) {
-    return FALSE;
+  $redirect = (object)$data;
+  $redirect->override = 0;
+  $redirect_path = $redirect->redirect;
+  if ($redirect->redirect != '<front>') {
+    $parts = redirect_parse_url($redirect->redirect);
+    if (!empty($parts['fragment'])) {
+      $redirect->redirect_options['fragment'] = $parts['fragment'];
+    }
+    if (!empty($parts['query'])) {
+      $redirect->redirect_options['query'] = $parts['query'];
+    }
+    if (!empty($parts['scheme']) && $parts['scheme'] == 'https') {
+      $redirect->redirect_options['https'] = TRUE;
+    }
+    if (!url_is_external($parts['url'])) {
+      if (drupal_lookup_path('source', $parts['url'], LANGUAGE_NONE)) {
+        $redirect->redirect = drupal_get_normal_path($parts['url'], LANGUAGE_NONE);
+      }
+      else {
+        $redirect->redirect = FALSE;
+      }
+    }
+    else {
+      $redirect->redirect = $parts['url'];
+    }
+  }
+  redirect_object_prepare($redirect);
+  redirect_hash($redirect);
+  if ($existing = redirect_load_by_hash($redirect->hash)) {
+    drupal_set_message(t('The source "@source" is already being redirected', array('@source' => $redirect->source)));
+  }
+  elseif (empty($redirect->redirect)) {
+    drupal_set_message(t('The destination "@dest" URL/path does not exist', array('@dest' => $redirect_path)));
   }
   else {
-    return TRUE;
+    redirect_save($redirect);
   }
-}
\ No newline at end of file
+  return TRUE;
+}
+
+
diff --git a/redirect-import-sample.csv b/redirect-import-sample.csv
new file mode 100644
index 0000000..8edd2e1
--- /dev/null
+++ b/redirect-import-sample.csv
@@ -0,0 +1,7 @@
+From URL,To URL,Redirect Code
+about.html,who/about,
+about/george.html,who/about#george
+home.html,<front>,
+support/login.asp,https://support.mycompany.com/login.php,
+support/new-ticket.asp,https://support.mycompany.com/ticket.php?type=new
+news/latest,<front>,307
\ No newline at end of file
-- 
1.7.5.4

