コード例 #1
0
 protected function processField($field)
 {
     switch ($field->getName()) {
         case 'ehriCopyrightIssue':
         case 'ehriScope':
         case 'ehriPriority':
             $name = $field->getName();
             $this->isad->{$name} = $this->form->getValue($name);
             break;
         case 'otherName':
             $value = $filtered = $this->form->getValue($field->getName());
             foreach ($this[$field->getName()] as $item) {
                 if (isset($value[$item->id])) {
                     $item->name = $value[$item->id];
                     unset($filtered[$item->id]);
                 } else {
                     $item->delete();
                 }
             }
             foreach ($filtered as $item) {
                 $otherName = new QubitOtherName();
                 $otherName->name = $item;
                 $otherName->typeId = QubitTerm::OTHER_FORM_OF_NAME_ID;
                 $this->resource->otherNames[] = $otherName;
             }
             break;
         default:
             return parent::processField($field);
     }
 }
コード例 #2
0
 public function import($csvFile, $options = array())
 {
     // 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)) {
         $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();
     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;
 }