Author: sevein
Date: Tue Aug 21 12:47:37 2012
New Revision: 12152

Log:
Change CSV import action to use our new CSV import task, using exec(). Command 
line is still recommended until we implement job scheduling in 2.x. Issue 140.

Modified:
   trunk/apps/qubit/modules/object/actions/importAction.class.php
   trunk/apps/qubit/modules/object/actions/importSelectAction.class.php
   trunk/apps/qubit/modules/object/templates/importSelectSuccess.php
   trunk/lib/Qubit.class.php
   trunk/lib/QubitCsvImport.class.php

Modified: trunk/apps/qubit/modules/object/actions/importAction.class.php
==============================================================================
--- trunk/apps/qubit/modules/object/actions/importAction.class.php      Tue Aug 
21 10:48:29 2012        (r12151)
+++ trunk/apps/qubit/modules/object/actions/importAction.class.php      Tue Aug 
21 12:47:37 2012        (r12152)
@@ -40,7 +40,7 @@
     }
 
     // set indexing preference
-    if ($request->getParameter('noindex'))
+    if (isset($request->noindex))
     {
       QubitSearch::getInstance()->disabled = true;
     }
@@ -49,114 +49,100 @@
       QubitSearch::getInstance()->getEngine()->enableBatchMode();
     }
 
-    try
+    // Zip file
+    if ('zip' == strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)))
     {
-      // choose import type based on file extension, eg. csv, xml
-      if ('csv' == pathinfo($file['name'], PATHINFO_EXTENSION))
+      // Check whether PHP Zip extension is installed
+      if (!class_exists('ZipArchive'))
       {
-        $importer = new QubitCsvImport;
-        $importer->import($file['tmp_name'], array('schema' => 
$request->getParameter('schema')));
+        $this->context->user->setFlash('error', $this->context->i18n->__('PHP 
Zip extension could not be found.'));
+        $this->redirect(array('module' => 'object', 'action' => 
'importSelect'));
       }
-      elseif ('xml' == pathinfo($file['name'], PATHINFO_EXTENSION))
+
+      // Create temporary directory
+      $zipDirectory = $file['tmp_name'].'-zip';
+      if (!file_exists($zipDirectory))
       {
-        $importer = new QubitXmlImport;
-        $importer->import($file['tmp_name'], array('strictXmlParsing' => 
false));
+        mkdir($zipDirectory, 0755);
       }
-      elseif ('zip' == pathinfo($file['name'], PATHINFO_EXTENSION) && 
class_exists('ZipArchive'))
-      {
-        $zipFolder = $file['tmp_name'].'-zip';
-        if (!file_exists($zipFolder))
-        {
-          mkdir($zipFolder, 0755);
-        }
 
-        // extract the zip archive into the temporary folder
-        // TODO: need some error handling here
-        $zip = new ZipArchive();
-        $zip->open($file['tmp_name']);
-        $zip->extractTo($zipFolder);
-        $zip->close();
+      // Extract the zip archive into the temporary folder
+      // TODO: need some error handling here
+      $zip = new ZipArchive();
+      $zip->open($file['tmp_name']);
+      $zip->extractTo($zipDirectory);
+      $zip->close();
 
-        $files = $this->dir_tree($zipFolder);
+      $files = Qubit::dirTree($zipDirectory);
 
-        // this code is from lib/importBulkTask.class.php
-        foreach ($files as $import_file)
-        {
-          // try to free up memory
-          unset($importer);
+      foreach ($files as $importFile)
+      {
+        // Try to free up memory
+        unset($importer);
 
-          // choose import type based on file extension, eg. csv, xml
-          if ('csv' == pathinfo($import_file, PATHINFO_EXTENSION))
-          {
+        // Choose import type based on file extension, eg. csv, xml
+        switch (strtolower(pathinfo($importFile, PATHINFO_EXTENSION)))
+        {
+          case 'csv':
             $importer = new QubitCsvImport;
-            $importer->import($import_file, array('schema'));
-          }
-          elseif ('xml' == pathinfo($import_file, PATHINFO_EXTENSION))
-          {
+            $importer->import($importFile);
+
+            break;
+
+          case 'xml':
             $importer = new QubitXmlImport;
-            $importer->import($import_file, array('strictXmlParsing' => 
false));
-          }
-          else
-          {
-            // move on to the next file
-            continue;
-          }
+            $importer->import($importFile, array('strictXmlParsing' => false));
+
+            break;
         }
       }
-      else
-      {
-        $this->context->user->setFlash('error', 
$this->context->i18n->__('Unable to import selected file'));
-        $this->redirect(array('module' => 'object', 'action' => 
'importSelect'));
-      }
     }
-    catch (sfException $e)
-    {
-      $this->context->user->setFlash('error', $e->getMessage());
-      $this->redirect(array('module' => 'object', 'action' => 'importSelect'));
-    }
-
-    // optimize index if enabled
-    if (!$request->getParameter('noindex'))
+    else
     {
-      QubitSearch::getInstance()->getEngine()->optimize();
-    }
-
-    $this->errors = $importer->getErrors();
-    $this->rootObject = $importer->getRootObject();
-    $this->objectType = strtr(get_class($this->rootObject), array('Qubit' => 
''));
-  }
+      // Import type, CSV or XML?
+      $importType = $request->getParameter('importType', 'xml');
 
-  protected function dir_tree($dir)
-  {
-    $path = '';
-    $stack[] = $dir;
-    while ($stack) 
-    {
-      $thisdir = array_pop($stack);
-      if ($dircont = scandir($thisdir))
+      try
       {
-        $i=0;
-        while (isset($dircont[$i]))
+        // Choose import type based on importType parameter
+        // This decision used to be based in the file extension but some users
+        // experienced problems when the extension was omitted
+        switch ($importType)
         {
-          if ($dircont[$i] !== '.' && $dircont[$i] !== '..'
-            // ignore system/hidden files
-            && !preg_match('/^\..*/', $dircont[$i]))
-          {
-            $current_file = "{$thisdir}/{$dircont[$i]}";
-            if (is_file($current_file))
-            {
-              $path[] = "{$thisdir}/{$dircont[$i]}";
-            }
-            elseif (is_dir($current_file))
-            {
-              $stack[] = $current_file;
-            }
-          }
-          $i++;
+          case 'csv':
+            $importer = new QubitCsvImport;
+            $importer->import($file['tmp_name'], $request->csvObjectType);
+
+            break;
+
+          case 'xml':
+            $importer = new QubitXmlImport;
+            $importer->import($file['tmp_name'], array('strictXmlParsing' => 
false));
+
+            break;
+
+          default:
+            $this->context->user->setFlash('error', 
$this->context->i18n->__('Unable to import selected file: unknown file 
extension.'));
+            $this->redirect(array('module' => 'object', 'action' => 
'importSelect', 'type' => $importType));
+
+            break;
         }
       }
-    }
+      catch (sfException $e)
+      {
+        $this->context->user->setFlash('error', $e->getMessage());
+        $this->redirect(array('module' => 'object', 'action' => 
'importSelect', 'type' => $importType));
+      }
 
-    return $path;
+      // Optimize index if enabled
+      if (!$request->getParameter('noindex'))
+      {
+        QubitSearch::getInstance()->getEngine()->optimize();
+      }
+
+      $this->errors = $importer->getErrors();
+      $this->rootObject = $importer->getRootObject();
+      $this->objectType = strtr(get_class($this->rootObject), array('Qubit' => 
''));
+    }
   }
 }

Modified: trunk/apps/qubit/modules/object/actions/importSelectAction.class.php
==============================================================================
--- trunk/apps/qubit/modules/object/actions/importSelectAction.class.php        
Tue Aug 21 10:48:29 2012        (r12151)
+++ trunk/apps/qubit/modules/object/actions/importSelectAction.class.php        
Tue Aug 21 12:47:37 2012        (r12152)
@@ -23,22 +23,28 @@
   {
     $this->form = new sfForm;
 
-    $this->type = strtolower($request->getParameter('type'));
+    // Check parameter
+    if (isset($request->type))
+    {
+      $this->type = $request->type;
+    }
 
     switch ($this->type)
     {
-      case 'xml':
-        $this->title = $this->context->i18n->__('Import an XML file');
+      case 'csv':
+        $this->title = $this->context->i18n->__('Import a CSV file');
 
         break;
 
-      case 'csv':
-        $this->title = $this->context->i18n->__('Import a CSV file');
+      case 'xml':
+        $this->title = $this->context->i18n->__('Import a XML file');
 
         break;
 
       default:
-        $this->title = $this->context->i18n->__('Import a file');
+        $this->redirect(array('module' => 'object', 'action' => 
'importSelect', 'type' => 'xml'));
+
+        break;
     }
   }
 }

Modified: trunk/apps/qubit/modules/object/templates/importSelectSuccess.php
==============================================================================
--- trunk/apps/qubit/modules/object/templates/importSelectSuccess.php   Tue Aug 
21 10:48:29 2012        (r12151)
+++ trunk/apps/qubit/modules/object/templates/importSelectSuccess.php   Tue Aug 
21 12:47:37 2012        (r12152)
@@ -18,14 +18,16 @@
       <input name="file" type="file"/>
     </div>
 
+    <input type="hidden" name="importType" value="<?php echo 
esc_entities($type) ?>"/>
+
     <?php if ('csv' == $type): ?>
       <div class="form-item">
         <label><?php echo __('Type') ?></label>
-        <select name="schema">
-          <option value=""><?php echo __('Auto-detect') ?></option>
-          <option value="isad">ISAD(G)</option>
-          <option value="rad">RAD</option>
-          <option value="isdiah">ISDIAH</option>
+        <select name="csvObjectType">
+          <option value="informationObject"><?php echo 
sfConfig::get('app_ui_label_informationobject') ?></option>
+          <option value="accession"><?php echo 
sfConfig::get('app_ui_label_accession', __('Accession')) ?></option>
+          <option value="authorityRecord"><?php echo 
sfConfig::get('app_ui_label_actor') ?></option>
+          <option value="event"><?php echo sfConfig::get('app_ui_label_event', 
__('Event')) ?></option>
         </select>
       </div>
     <?php endif; ?>

Modified: trunk/lib/Qubit.class.php
==============================================================================
--- trunk/lib/Qubit.class.php   Tue Aug 21 10:48:29 2012        (r12151)
+++ trunk/lib/Qubit.class.php   Tue Aug 21 12:47:37 2012        (r12152)
@@ -106,4 +106,45 @@
 
     return false != file_put_contents($tmpFileName, $contents) ? $tmpFileName 
: false;
   }
+
+  /**
+   * Given a directory,
+   *
+   * @param string $directory path
+   *
+   * @return array
+   */
+  public static function dirTree($directory)
+  {
+    $path = '';
+    $stack[] = $directory;
+    while ($stack)
+    {
+      $thisdir = array_pop($stack);
+      if ($dircont = scandir($thisdir))
+      {
+        $i=0;
+        while (isset($dircont[$i]))
+        {
+          if ($dircont[$i] !== '.' && $dircont[$i] !== '..'
+            // ignore system/hidden files
+            && !preg_match('/^\..*/', $dircont[$i]))
+          {
+            $current_file = "{$thisdir}/{$dircont[$i]}";
+            if (is_file($current_file))
+            {
+              $path[] = "{$thisdir}/{$dircont[$i]}";
+            }
+            elseif (is_dir($current_file))
+            {
+              $stack[] = $current_file;
+            }
+          }
+          $i++;
+        }
+      }
+    }
+
+    return $path;
+  }
 }

Modified: trunk/lib/QubitCsvImport.class.php
==============================================================================
--- trunk/lib/QubitCsvImport.class.php  Tue Aug 21 10:48:29 2012        (r12151)
+++ trunk/lib/QubitCsvImport.class.php  Tue Aug 21 12:47:37 2012        (r12152)
@@ -19,10 +19,11 @@
 
 /**
  * Import a CSV file into Qubit.
+ * This class is a wrapper to interact with the import CSV tasks located
+ * under the lib/task/import directory
  *
  * @package    Qubit
  * @subpackage library
- * @author     MJ Suhonos <[email protected]>
  * @version    svn:$Id: QubitCsvImport.class.php 9112 2011-05-20 01:35:23Z mj $
  */
 class QubitCsvImport
@@ -31,555 +32,74 @@
     $errors = null,
     $rootObject = null;
 
-  public function import($csvFile, $options = array())
+  public function import($csvFile, $type = null, $sourceName = null)
   {
-    // load the CSV document into an array
-    $data = $this->loadCSV($csvFile);
-
-    // information object schema element names
-    $names['rad'] = array_flip(array_map(array('sfInflector', 'underscore'), 
sfRadPluginEditAction::$NAMES));
-    $names['isad'] = array_flip(array_map(array('sfInflector', 'underscore'), 
sfIsadPluginEditAction::$NAMES));
-
-    // repository schema element 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'), 
ContactInformationEditComponent::$NAMES)));
-
-    // determine what kind of schema we're trying to import
-    $header = array_flip($data['header']);
-    unset($data['header']);
-
-    // see if any of these schemas match close enough
-    if (!in_array($options['schema'], array('rad', 'isad', 'isdiah')))
-    {
-      foreach ($names as $name => $elements)
-      {
-        $match = array_intersect_key($elements, $header);
-
-        $scores[$name] = count($match)/count($header);
-      }
-
-      arsort($scores);
-
-      // schema must match at least 50% of elements
-      if (current($scores) > 0.5)
-      {
-        $importSchema = key($scores);
-      }
-    }
-    else
-    {
-      $importSchema = $options['schema'];
-    }
-
-    if (!isset($importSchema))
+    // Find the proper task
+    switch ($type)
     {
-      $errorMsg = sfContext::getInstance()->i18n->__('Unable to import CSV 
file: cannot determine schema type');
-
-      throw new Exception($errorMsg);
-    }
-
-    // switch context and create a new object using the import schema
-    $this->context = sfContext::getInstance();
+      case 'accession':
+        $taskClassName = 'csv:accession-import';
 
-    switch ($importSchema)
-    {
-      case 'isdiah':
-        $action = new sfIsdiahPluginEditAction($this->context, 
'sfIsdiahPlugin', 'edit');
         break;
-      case 'rad':
-        $action = new sfRadPluginEditAction($this->context, 'sfRadPlugin', 
'edit');
-        break;
-      case 'isad':
-        $action = new sfIsadPluginEditAction($this->context, 'sfIsadPlugin', 
'edit');
-        break;
-    }
-
-    // we are not editing an existing object
-    unset($action->getRoute()->resource);
-
-    // populate parameter holder with properties for the object
-    foreach ($data as $index => $row)
-    {
-      $parameters = array_combine(array_map('lcfirst', 
array_map(array('sfInflector', 'camelize'), array_keys($header))), $row);
-
-      // generic mapping for hierarchical data
-      if (isset($parameters['id']))
-      {
-        $ids[$index] = $parameters['id'];
-      }
-
-      if (isset($parameters['parent']))
-      {
-        $parents[$index] = $parameters['parent'];
-      }
-
-      // special cases for various schemas to map to parameter holders
-      switch ($importSchema)
-      {
-        case 'isdiah':
-          $parameters = $this->mapIsdiah($parameters);
-          break;
-        case 'rad':
-          $parameters = $this->mapInformationObject($parameters);
-          $parameters = $this->mapRad($parameters);
-          break;
-        case 'isad':
-          $parameters = $this->mapInformationObject($parameters);
-          $parameters = $this->mapIsad($parameters);
-          break;
-      }
-
-      $parameterArray[] = $this->mapEdit($parameters);
-    }
-
-    // if we have hierarchy information, re-order data rows
-    if (isset($ids) && isset($parents))
-    {
-      array_multisort($parents, SORT_ASC, $ids, SORT_ASC, $parameterArray);
-    }
-
-    // emulate a POST form submission with given data
-    $request = $this->context->getRequest();
-    $request->setParameter('csvimport', true);
-
-    // populate and submit the form for each data row
-    $insertIds = array();
-    foreach ($parameterArray as $index => $parameters)
-    {
-      if (!empty($parameters['parent']))
-      {
-        $parameters['parent'] = $insertIds[$parameters['parent']];
-      }
-
-      // run the action to create and save the new object
-      $request->getParameterHolder()->add($parameters);
-      $action->execute($request);
-
-      // keep track of the insert ID for parenting
-      $insertIds[$parameters['id']] = $action->resource->id;
-
-      // set the rootObject to use for initial display in successful import
-      if (!$this->rootObject)
-      {
-        $this->rootObject = $action->resource;
-      }
-    }
-
-    return $this;
-  }
-
-  protected function loadCSV($csvFile)
-  {
-    // 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) 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))
-    {
-        $data[] = $row;
-    }
-
-    fclose($fh);
-
-    return $data;
-  }
-
-  protected function mapEdit($parameters)
-  {
-    // convert pipe-delimited values into multi-value
-    $n = 0;
-    foreach (explode('|', $parameters['parallelName']) as $new_parallelName)
-    {
-      $new_parallelNames['new'.$n] = $new_parallelName;
-      $n++;
-    }
-    $parameters['parallelName'] = $new_parallelNames;
-
-    $n = 0;
-    foreach (explode('|', $parameters['otherName']) as $new_otherName)
-    {
-      $new_otherNames['new'.$n] = $new_otherName;
-      $n++;
-    }
-    $parameters['otherName'] = $new_otherNames;
-
-    // check constrained values are valid
-    if (!isset($this->culture))
-    {
-      $culture = $this->context->user->getCulture();
-      $this->culture = sfCultureInfo::getInstance($culture);
-    }
-
-    // NB: this only matches ISO-639-2 codes, eg. "en"
-    if (!in_array($parameters['language'], 
array_keys($this->culture->getLanguages())))
-    {
-      unset($parameters['language']);
-    }
-
-    if (!in_array($parameters['languageOfDescription'], 
array_keys($this->culture->getLanguages())))
-    {
-      unset($parameters['languageOfDescription']);
-    }
-
-    // NB: this only matches Symfony script codes, eg. "Latn"
-    if (!in_array($parameters['script'], 
array_keys($this->culture->getScripts())))
-    {
-      unset($parameters['script']);
-    }
-
-    if (!in_array($parameters['scriptOfDescription'], 
array_keys($this->culture->getScripts())))
-    {
-      unset($parameters['scriptOfDescription']);
-    }
-
-    if (!isset($this->levelsOfDescription))
-    {
-      foreach (QubitTerm::getLevelsOfDescription() as $term)
-      {
-        $this->levelsOfDescription[] = $term;
-      }
-    }
-
-    if (!in_array($parameters['levelOfDescription'], 
$this->levelsOfDescription))
-    {
-      unset($parameters['levelOfDescription']);
-    }
 
-    if (!isset($this->descriptionDetailLevels))
-    {
-      foreach (QubitTerm::getDescriptionDetailLevels() as $term)
-      {
-        $this->descriptionDetailLevels[] = $term;
-      }
-    }
+      case 'actor':
+        $taskClassName = 'csv:authority-import';
 
-    if (!in_array($parameters['descriptionDetail'], 
$this->descriptionDetailLevels))
-    {
-      unset($parameters['descriptionDetail']);
-    }
-
-    if (!isset($this->descriptionStatuss))
-    {
-      foreach (QubitTerm::getDescriptionStatuses() as $term)
-      {
-        $this->descriptionStatuss[] = $term;
-      }
-    }
-
-    if (!in_array($parameters['descriptionStatus'], $this->descriptionStatuss))
-    {
-      unset($parameters['descriptionStatus']);
-    }
-
-    return $parameters;
-  }
-
-  protected function mapInformationObject($parameters)
-  {
-    // NB: any value for publication status means set to published
-    if (!empty($parameters['publicationStatus']))
-    {
-      $parameters['publicationStatus'] = 
QubitTerm::PUBLICATION_STATUS_PUBLISHED_ID;
-    }
-
-    if (!empty($parameters['repository']))
-    {
-      $repo = 
QubitRepository::getByAuthorizedFormOfName($parameters['repository']);
-
-      // if the repository does not exist, create it
-      if (empty($repo))
-      {
-        $repo = new QubitRepository();
-        $repo->authorizedFormOfName = $parameters['repository'];
-        $repo->save();
-      }
-      $parameters['repository'] = $this->context->routing->generate(null, 
array($repo, 'module' => 'repository'));
-    }
-
-    // subject access points
-    if (!isset($this->subjects))
-    {
-      foreach (QubitTerm::getSubjects() as $term)
-      {
-        $this->subjects[$term->__toString()] = $term;
-      }
-    }
-
-    $n = 0;
-    foreach (explode('|', $parameters['subjectAccessPoints']) as 
$new_subjectAccessPoint)
-    {
-      if (0 == strlen(trim($new_subjectAccessPoint)))
-      {
-        continue;
-      }
-
-      // if the subject does not exist, create it
-      if (!in_array($new_subjectAccessPoint, $this->subjects) && 
!empty($new_subjectAccessPoint))
-      {
-        $subject = new QubitTerm();
-        $subject->taxonomyId = QubitTaxonomy::SUBJECT_ID;
-        $subject->name = $new_subjectAccessPoint;
-        $subject->save();
-
-        $this->subjects[$subject->__toString()] = $subject;
-      }
-
-      $new_subjectAccessPoints['new'.$n] = 
$this->context->routing->generate(null, 
array($this->subjects[$new_subjectAccessPoint], 'module' => 'term'));
-      $n++;
-    }
-    $parameters['subjectAccessPoints'] = $new_subjectAccessPoints;
-
-    // place access points
-    if (!isset($this->places))
-    {
-      foreach (QubitTerm::getPlaces() as $term)
-      {
-        $this->places[$term->__toString()] = $term;
-      }
-    }
-
-    $n = 0;
-    foreach (explode('|', $parameters['placeAccessPoints']) as 
$new_placeAccessPoint)
-    {
-      if (0 == strlen(trim($new_placeAccessPoint)))
-      {
-        continue;
-      }
-
-      // if the place does not exist, create it
-      if (!in_array($new_placeAccessPoint, $this->places) && 
!empty($new_placeAccessPoint))
-      {
-        $place = new QubitTerm();
-        $place->taxonomyId = QubitTaxonomy::PLACE_ID;
-        $place->name = $new_placeAccessPoint;
-        $place->save();
-
-        $this->places[$place->__toString()] = $place;
-      }
-
-      $new_placeAccessPoints['new'.$n] = 
$this->context->routing->generate(null, 
array($this->places[$new_placeAccessPoint], 'module' => 'term'));
-      $n++;
-    }
-    $parameters['placeAccessPoints'] = $new_placeAccessPoints;
-
-    // name access points
-    $n = 0;
-    $this->getActors();
-    foreach (explode('|', $parameters['nameAccessPoints']) as 
$new_nameAccessPoint)
-    {
-      if (0 == strlen(trim($new_nameAccessPoint)))
-      {
-        continue;
-      }
-
-      // if the name does not exist, create it
-      if (!isset($this->actors[$new_nameAccessPoint]))
-      {
-        $name = new QubitActor();
-        $name->authorizedFormOfName = $new_nameAccessPoint;
-        $name->save();
-
-        $this->actors[$name->__toString()] = $name;
-      }
-
-      $new_nameAccessPoints['new'.$n] = 
$this->context->routing->generate(null, 
array($this->actors[$new_nameAccessPoint], 'module' => 'actor'));
-      $n++;
-    }
-    $parameters['nameAccessPoints'] = $new_nameAccessPoints;
-
-    return $parameters;
-  }
-
-  protected function mapRad($parameters)
-  {
-    $creationTerm = new QubitTerm();
-    $creationTerm->id = QubitTerm::CREATION_ID;
-    $creationUrl = $this->context->routing->generate(null, 
array($creationTerm, 'module' => 'term'));
-
-    // Creation dates
-    foreach (explode('|', $parameters['datesOfCreation']) as $date)
-    {
-      if (0 < strlen($date))
-      {
-        $parameters['editEvents'][] = array(
-          'type' => $creationUrl,
-          'date' => $date
-        );
-      }
-    }
-
-    // Link creators
-    foreach ($this->addCreatorsAndHistory($parameters) as $creator)
-    {
-      $parameters['editEvents'][] = array(
-        'type' => $creationUrl,
-        'actor' => $this->context->routing->generate(null, array($creator, 
'module' => 'actor')));
-    }
+        break;
 
-    if (!isset($this->materialTypes))
-    {
-      foreach (QubitTerm::getMaterialTypes() as $term)
-      {
-        $this->materialTypes[$term->__toString()] = 
$this->context->routing->generate(null, array($term, 'module' => 'term'));
-      }
-    }
+      case 'event':
+        $taskClassName = 'csv:event-import';
 
-    $n = 0;
-    foreach (explode('|', $parameters['type']) as $new_type)
-    {
-      if (in_array($new_type, array_keys($this->materialTypes)))
-      {
-        $new_types['new'.$n] = $this->materialTypes[$new_type];
-        $n++;
-      }
-    }
-    $parameters['type'] = $new_types;
+        break;
 
-    if (!isset($this->radTitleNotes))
-    {
-      foreach (QubitTerm::getRADTitleNotes() as $term)
-      {
-        $this->radTitleNotes[$term->id] = $term;
-      }
-    }
+      case 'informationObject':
+      default:
+        $taskClassName = 'csv:import';
 
-    if (!in_array($parameters['radTitleNoteType'], $this->radTitleNotes))
-    {
-      unset($parameters['radTitleNote']);
-      unset($parameters['radTitleNoteType']);
-    }
-    else
-    {
-      $parameters['radTitleNoteType'] = 
array_search($parameters['radTitleNoteType'], $this->radTitleNotes);
-    }
-
-    if (!isset($this->radNotes))
-    {
-      foreach (QubitTerm::getRADNotes() as $term)
-      {
-        $this->radNotes[$term->id] = $term;
-      }
+        break;
     }
 
-    if (!in_array($parameters['radNoteType'], $this->radNotes))
+    // Build command string
+    if (isset($sourceName))
     {
-      unset($parameters['radNote']);
-      unset($parameters['radNoteType']);
+      // Example: php symfony csv:import --source-name="$sourceName" 
/tmp/foobar
+      $command = sprintf('php %s %s --source-name=%s %s',
+        
escapeshellarg(sfConfig::get('sf_root_dir').DIRECTORY_SEPARATOR.'symfony'),
+        escapeshellarg($taskClassName),
+        escapeshellarg($sourceName),
+        escapeshellarg($csvFile));
     }
     else
     {
-      $parameters['radNoteType'] = array_search($parameters['radNoteType'], 
$this->radNotes);
+      // Example: php symfony csv:import /tmp/foobar
+      $command = sprintf('php %s %s %s',
+        
escapeshellarg(sfConfig::get('sf_root_dir').DIRECTORY_SEPARATOR.'symfony'),
+        escapeshellarg($taskClassName),
+        escapeshellarg($csvFile));
     }
 
-    return $parameters;
-  }
+    // stderr to stdout
+    $command .= ' 2>&1';
 
-  protected function mapIsad($parameters)
-  {
-    // dates of creation
-    $creationTerm = new QubitTerm();
-    $creationTerm->id = QubitTerm::CREATION_ID;
-    $termType = $this->context->routing->generate(null, array($creationTerm, 
'module' => 'term'));
+    // Run
+    exec($command, $output, $exitCode);
 
-    $n = 0;
-    foreach (explode('|', $parameters['datesOfCreation']) as $new_date)
+    // Throw exception if exit code is greater than zero
+    if (0 < $exitCode)
     {
-      // TODO: validate date/range format somehow
-      if (!empty($new_date))
-      {
-        $new_dates['new'.$n] = array('type' => $termType, 'date' => $new_date);
-        $n++;
-      }
-    }
-    $parameters['datesOfCreation'] = $new_dates;
+      $output = implode($output, "<br />");
 
-    // name access points
-    $n = 0;
-    foreach ($this->addCreatorsAndHistory($parameters) as $creator)
-    {
-      $new_creators['new'.$n] = $this->context->routing->generate(null, 
array($creator, 'module' => 'actor'));
-      $n++;
+      throw new sfException($output);
     }
-    $parameters['creators'] = $new_creators;
-
-    // convert pipe-delimited values into multi-value
-    $n = 0;
-    foreach (explode('|', $parameters['newArchivistNote']) as 
$new_archivistNote)
-    {
-      $new_archivistNotes['new'.$n] = $new_archivistNote;
-      $n++;
-    }
-    $parameters['newArchivistNote'] = $new_archivistNotes;
-
-    $n = 0;
-    foreach (explode('|', $parameters['newPublicationNote']) as 
$new_publicationNote)
-    {
-      $new_publicationNotes['new'.$n] = $new_publicationNote;
-      $n++;
-    }
-    $parameters['newPublicationNote'] = $new_publicationNotes;
-
-    $n = 0;
-    foreach (explode('|', $parameters['newNote']) as $new_Note)
-    {
-      $new_Notes['new'.$n] = $new_Note;
-      $n++;
-    }
-    $parameters['newNote'] = $new_Notes;
-
-    return $parameters;
-  }
-
-  protected function mapIsdiah($parameters)
-  {
-    // remove duplicate values
-    if ($parameters['parallelName'] == $parameters['authorizedFormOfName'])
-    {
-      unset($parameters['parallelName']);
-    }
-
-    if ($parameters['otherName'] == $parameters['authorizedFormOfName'])
-    {
-      unset($parameters['otherName']);
-    }
-
-    // NB: this is hacky, but required for an inconsistency in repository 
property names
-    if (!isset($this->descriptionDetailLevels))
-    {
-      foreach (QubitTerm::getDescriptionDetailLevels() as $term)
-      {
-        $this->descriptionDetailLevels[] = $term;
-      }
-    }
-
-    if (!in_array($parameters['descDetail'], $this->descriptionDetailLevels))
-    {
-      unset($parameters['descDetail']);
-    }
-
-    if (!isset($this->descriptionStatuss))
+    else
     {
-      foreach (QubitTerm::getDescriptionStatuses() as $term)
-      {
-        $this->descriptionStatuss[] = $term;
-      }
+      // Warnings
+      $this->errors = $output;
     }
 
-    if (!in_array($parameters['descStatus'], $this->descriptionStatuss))
-    {
-      unset($parameters['descStatus']);
-    }
+    // TODO, find getRootObject
 
-    return $parameters;
+    return $this;
   }
 
   /**
@@ -611,70 +131,4 @@
   {
     return $this->rootObject;
   }
-
-  public function getActors()
-  {
-    if (!isset($this->actors))
-    {
-      foreach (QubitActor::getOnlyActors() as $item)
-      {
-        $this->actors[$item->__toString()] = $item;
-      }
-    }
-
-    return $this->actors;
-  }
-
-  public function addCreatorsAndHistory($parameters)
-  {
-    $creators = $histories = array();
-    $i = 0;
-    $updated = false;
-
-    // Get array of existing actors
-    $this->getActors();
-
-    if (isset($parameters['creatorHistory']))
-    {
-      $histories = explode('|', $parameters['creatorHistory']);
-    }
-
-    foreach (explode('|', $parameters['creators']) as $creator)
-    {
-      if (0 == strlen(trim($creator)))
-      {
-        continue;
-      }
-
-      if (isset($this->actors[$creator]))
-      {
-        $actor = $this->actors[$creator];
-      }
-      else
-      {
-        $actor = new QubitActor;
-        $actor->authorizedFormOfName = $creator;
-        $updated = true;
-
-        // Add to array of existing actors
-        $this->actors[$creator] = $actor;
-      }
-
-      if (isset($histories[$i]))
-      {
-        $actor->history = $histories[$i];
-        $updated = true;
-      }
-
-      if ($updated)
-      {
-        $actor->save();
-      }
-
-      $creators[] = $actor;
-      $i++;
-    }
-
-    return $creators;
-  }
 }

-- 
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