Author: mj
Date: Tue Oct 25 13:33:08 2011
New Revision: 10185

Log:
Begin refactor of CSV import to current 1.2 trunk (issue #140), starting with 
ISDIAH.

Modified:
   trunk/apps/qubit/modules/repository/actions/editAction.class.php
   trunk/lib/QubitCsvImport.class.php

Modified: trunk/apps/qubit/modules/repository/actions/editAction.class.php
==============================================================================
--- trunk/apps/qubit/modules/repository/actions/editAction.class.php    Tue Oct 
25 10:45:58 2011        (r10184)
+++ trunk/apps/qubit/modules/repository/actions/editAction.class.php    Tue Oct 
25 13:33:08 2011        (r10185)
@@ -226,6 +226,12 @@
       {
         $this->processForm();
 
+        $type = $request->getParameter('type');
+        if (!empty($type))
+        {
+          $this->resource->setTypeByName($type);
+        }
+
         $this->resource->save();
 
         if ($this->request->contact_type

Modified: trunk/lib/QubitCsvImport.class.php
==============================================================================
--- trunk/lib/QubitCsvImport.class.php  Tue Oct 25 10:45:58 2011        (r10184)
+++ trunk/lib/QubitCsvImport.class.php  Tue Oct 25 13:33:08 2011        (r10185)
@@ -33,7 +33,6 @@
 
   public function import($csvFile, $options = array())
   {
-
     // load the CSV document into an array
     $data = $this->loadCSV($csvFile, $options);
 
@@ -42,9 +41,8 @@
     $names['rad'] = array_flip(array_map(array('sfInflector', 'underscore'), 
sfRadPluginEditAction::$NAMES));
 
     // repository schema element names
-//    $names['actor'] = array_flip(array_map(array('sfInflector', 
'underscore'), ActorEditContactInformationAction::$NAMES));
     $names['isdiah'] = array_flip(array_map(array('sfInflector', 
'underscore'), sfIsdiahPluginEditAction::$NAMES));
-    $names['isdiah'] = array_merge($names['isdiah'], 
array_flip(array_map(array('sfInflector', 'underscore'), 
ActorEditContactInformationAction::$NAMES)));
+    $names['isdiah'] = array_merge($names['isdiah'], 
array_flip(array_map(array('sfInflector', 'underscore'), 
ContactInformationEditComponent::$NAMES)));
 
     // determine what kind of schema we're trying to import
     $header = array_flip($data['header']);
@@ -53,80 +51,61 @@
     // see if any of these schemas match close enough
     if (!in_array($options['schema'], array('isad', 'rad', 'isdiah')))
     {
-      foreach ($names as $name => $elements) {
+      foreach ($names as $name => $elements)
+      {
         $match = array_intersect_key($elements, $header);
 
         // NB: *very* simple test, if 50% of elements match, use that schema
-        if ((count($match)/count($header)) > 0.5) {
+        if ((count($match)/count($header)) > 0.5)
+        {
           $importSchema = $name;
           break;
         }
       }
-    } else {
+    }
+    else
+    {
       $importSchema = $options['schema'];
     }
 
-    if (!isset($importSchema)) {
+    if (!isset($importSchema))
+    {
       $errorMsg = sfContext::getInstance()->i18n->__('Unable to import CSV 
file: cannot determine schema type');
 
       throw new Exception($errorMsg);
     }
 
     // switch context and create a new repository object using the ISDIAH 
schema
-    $context = sfContext::getInstance();
+    $this->context = sfContext::getInstance();
 
     switch ($importSchema)
     {
       case 'isdiah':
-        $action = new sfIsdiahPluginEditAction($context, 'sfIsdiahPlugin', 
'edit');
+        $action = new sfIsdiahPluginEditAction($this->context, 
'sfIsdiahPlugin', 'edit');
         break;
     }
 
     unset($action->getRoute()->resource); // we are not editing an existing 
object
 
     // emulate a POST form submission with given data
-    $request = $context->getRequest();
+    $request = $this->context->getRequest();
     $request->setParameter('csvimport', true);
 
-    foreach ($data as $row) {
+    foreach ($data as $row)
+    {
 
       // populate parameter holder with properties for the object
-      $parameters = array_combine(array_map('lcfirst', 
array_map(array(sfInflector, 'camelize'), array_keys($header))), $row);
-
-      // special vases for various schemas to map to parameter holders
-      if ('isdiah' == $importSchema) {
+      $parameters = array_combine(array_map('lcfirst', 
array_map(array('sfInflector', 'camelize'), array_keys($header))), $row);
 
-        if ($parameters['parallelName'] == 
$parameters['authorizedFormOfName']) {
-          $parameters['parallelName'] = '';
-        }
-
-        if ($parameters['otherName'] == $parameters['authorizedFormOfName']) {
-          $parameters['otherName'] = '';
-        }
-
-        $n = 0;
-        foreach (explode('|', $parameters['parallelName']) as 
$new_parallelName) {
-          $new_parallelNames['new'.$n] = $new_parallelName;
-        }
-        $parameters['parallelName'] = $new_parallelNames;
-
-        $n = 0;
-        foreach (explode('|', $parameters['otherName']) as $new_otherName) {
-          $new_otherNames['new'.$n] = $new_otherName;
-        }
-        $parameters['otherName'] = $new_otherNames;
+      // special cases for various schemas to map to parameter holders
+      if ('isdiah' == $importSchema)
+      {
+        $parameters = $this->mapIsdiah($parameters);
       }
-
       // run the action to create and save the new object
       $request->getParameterHolder()->add($parameters);
       $action->execute($request);
 
-      // special vases for various schemas to set object relations
-      if ('isdiah' == $importSchema && !empty($parameters['type'])) {
-        $action->resource->setTypeByName($parameters['type']);
-        $action->resource->save();
-      }
-
       // set the rootObject to use for initial display in successful import
       if (!$this->rootObject)
       {
@@ -139,25 +118,88 @@
 
   protected function loadCSV($csvFile, $options = array())
   {
-      // make sure we get the right EOL character
-      ini_set('auto_detect_line_endings', true);
-      $fh = fopen($csvFile, 'rb');
-
-      // Get header (first) row
-      foreach (fgetcsv($fh, 1000) as $col => $label) {
-          // normalize the column labels to match defined fields
-          $header[$col] = preg_replace(array('/(\s|\/)+/', '/[^a-z_]/', 
'/_$/'), array('_', ''), strtolower(sfInflector::underscore($label)));
-      }
-      $data['header'] = $header;
+    // make sure we get the right EOL character
+    ini_set('auto_detect_line_endings', true);
+    $fh = fopen($csvFile, 'rb');
 
-      // the rest of data is n-indexed
-      while ($row = fgetcsv($fh, 1000)) {
-          $data[] = $row;
-      }
+    // Get header (first) row
+    foreach (fgetcsv($fh, 1000) as $col => $label)
+    {
+        // normalize the column labels to match defined fields
+        $header[$col] = preg_replace(array('/(\s|\/)+/', '/[^a-z_]/', '/_$/'), 
array('_', ''), strtolower(sfInflector::underscore($label)));
+    }
+    $data['header'] = $header;
+
+    // the rest of data is n-indexed
+    while ($row = fgetcsv($fh, 1000))
+    {
+        $data[] = $row;
+    }
+
+    fclose($fh);
+
+    return $data;
+  }
 
-      fclose($fh);
+  protected function mapIsdiah($parameters)
+  {
+    // remove duplicate values
+    if ($parameters['parallelName'] == $parameters['authorizedFormOfName'])
+    {
+      $parameters['parallelName'] = '';
+    }
+
+    if ($parameters['otherName'] == $parameters['authorizedFormOfName'])
+    {
+      $parameters['otherName'] = '';
+    }
+
+    // convert pipe-delimited values into multi-value
+    $n = 0;
+    foreach (explode('|', $parameters['parallelName']) as $new_parallelName)
+    {
+      $new_parallelNames['new'.$n] = $new_parallelName;
+    }
+    $parameters['parallelName'] = $new_parallelNames;
+
+    $n = 0;
+    foreach (explode('|', $parameters['otherName']) as $new_otherName)
+    {
+      $new_otherNames['new'.$n] = $new_otherName;
+    }
+    $parameters['otherName'] = $new_otherNames;
+
+    // check constrained values are valid
+    if (!isset($this->culture))
+    {
+      $culture = $this->context->user->getCulture();
+      $this->culture = sfCultureInfo::getInstance($culture);
+    }
+
+    if (!in_array($parameters['language'], $this->culture->getLanguages()))
+    {
+      $parameters['language'] = '';
+    }
+
+    if (!in_array($parameters['script'], $this->culture->getScripts()))
+    {
+      $parameters['script'] = '';
+    }
+
+    // TODO: FIX ME
+//    if (!in_array($parameters['descDetail'], 
QubitTaxonomy::getTermsById(QubitTaxonomy::DESCRIPTION_DETAIL_LEVEL_ID)))
+    if (!empty($parameters['descDetail']))
+    {
+      $parameters['descDetail'] = '';
+    }
+
+//    if (!in_array($parameters['descStatus'], 
QubitTaxonomy::getTermsById(QubitTaxonomy::DESCRIPTION_STATUS_ID)))
+    if (!empty($parameters['descStatus']))
+    {
+      $parameters['descStatus'] = '';
+    }
 
-      return $data;
+    return $parameters;
   }
 
   /**

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to