/**
  * create and add an individual element
  *
  * @param  SchemaProperty $schema_property
  * @param  int $userId
  * @param  int $fieldId
  * @param  int $statusId
  * @param string $language
  *
  * @param bool $isGenerated
  * @return SchemaPropertyElement
  */
 public static function createElement($schema_property, $userId, $fieldId, $statusId, $language = null, $isGenerated = false)
 {
     $element = new SchemaPropertyElement();
     $element->setCreatedUserId($userId);
     $element->setUpdatedUserId($userId);
     $element->setSchemaPropertyId($schema_property->getId());
     $element->setLanguage($language);
     $element->setStatusId($statusId);
     $element->setProfilePropertyId($fieldId);
     $element->setIsGenerated($isGenerated);
     return $element;
     //self::updateElement($schema_property, $element, $userId, $field, $con, $isSchemaProperty);
 }
Example #2
0
 /**
  * @param SchemaProperty     $property
  * @param Criteria           $cLang
  * @param ProfileProperty[] $propArray
  * @param Status[]          $statusArray
  * @param bool              $languageArray
  * @param                    $languageDefault
  *
  * @return array
  *
  */
 public function getResourceArray(SchemaProperty $property, Criteria $cLang, $propArray, $statusArray, $languageArray, $languageDefault)
 {
     //todo: this should be based on a constant rather than hard-coded;
     $lexicalAliasProperty = 27;
     //todo: remove hard coded registry URLs
     $resourceArray = [];
     $resourceArray["@id"] = $property->getUri();
     $resourceArray["isDefinedBy"] = array("@id" => $this->getUri(), "url" => "http://metadataregistry.org/schema/show/id/" . $this->getId() . ".html", "label" => $this->getName());
     $typeArray = array('property' => "Property", 'class' => "Class", 'subproperty' => "Property", 'subclass' => "Class");
     $resourceArray["url"] = "http://metadataregistry.org/schemaprop/show/id/" . $property->getId() . ".html";
     $elements = $property->getSchemaPropertyElementsRelatedBySchemaPropertyId($cLang);
     /** @var SchemaPropertyElement $element */
     foreach ($elements as $element) {
         /** @var string $ppi */
         $pproperty = $propArray[$element->getProfilePropertyId()];
         $ppi = $pproperty->getLabel();
         //id
         if (!$pproperty->getIsObjectProp()) {
             if ($pproperty->getHasLanguage() && $languageArray) {
                 //we're putting language related elements in a language specific array
                 self::addToGraph($resourceArray[$ppi][$element->getLanguage()], $element->getObject(), $pproperty->getIsSingleton());
             } else {
                 self::addToGraph($resourceArray[$ppi], $element->getObject(), $pproperty->getIsSingleton());
             }
         } else {
             $array = array();
             if ("status" !== $ppi) {
                 if (!in_array($statusArray[$element->getStatusId()][2], ["Deprecated", "Not Approved"])) {
                     $object = $element->getSchemaPropertyRelatedByRelatedSchemaPropertyId();
                     if (!$object) {
                         //there wasn't an ID so we look it up by the URI
                         $object = SchemaPropertyPeer::retrieveByUri($element->getObject());
                         if ($object) {
                             //we now have an ID
                             //todo: log that we did this
                             $element->setRelatedSchemaPropertyId($object->getId());
                             $element->save();
                         }
                     }
                     if ($object) {
                         //we got an object somehow
                         //todo: refactor this to build a language array for lexicalalias and label if uselanguagearray is true
                         //we'll need to get the array of available languages for the schema and do a for/next
                         //todo: we removed the language filter from the query, so we need to check for a language match here
                         $object->setLanguage($languageDefault);
                         if ($lexicalAliasProperty == $pproperty->getId()) {
                             $array = $object->getLexicalAlias();
                             $this->setLexicalArray($element->getObject(), $resourceArray["@id"], 308);
                         } else {
                             $array = array("@id" => $object->getUri(), "lexicalAlias" => $object->getLexicalAlias(), "url" => "http://metadataregistry.org/schemaprop/show/id/" . $object->getId() . ".html", "label" => $object->getLabel());
                             if (empty($array['lexicalAlias'])) {
                                 unset($array['lexicalAlias']);
                             }
                         }
                     } else {
                         if ('@type' == $ppi and isset($typeArray[$element->getObject()])) {
                             $array = $typeArray[$element->getObject()];
                         } else {
                             $array = array("@id" => $element->getObject());
                         }
                     }
                 }
             } else {
                 //it's a status
                 $status = $statusArray[$element->getObject()];
                 $array = array("@id" => $status[3], "lexicalAlias" => "http://metadataregistry.org/uri/RegStatus/" . $status[2] . ".en", "url" => "http://metadataregistry.org/concept/show/id/{$status['4']}.html", "label" => $status[2]);
                 //$resourceArray[ $ppi ] = self::addToGraph($array, $pproperty->getIsSingleton());
             }
             self::addToGraph($resourceArray[$ppi], $array, $pproperty->getIsSingleton());
         }
     }
     ksort($resourceArray, SORT_FLAG_CASE | SORT_NATURAL);
     return $resourceArray;
 }
 /**
  * Declares an association between this object and a SchemaProperty object.
  *
  * @param      SchemaProperty $v
  * @return     void
  * @throws     PropelException
  */
 public function setSchemaPropertyRelatedByRelatedSchemaPropertyId($v)
 {
     if ($v === null) {
         $this->setRelatedSchemaPropertyId(NULL);
     } else {
         $this->setRelatedSchemaPropertyId($v->getId());
     }
     $this->aSchemaPropertyRelatedByRelatedSchemaPropertyId = $v;
 }
function run_import_marc_vocabulary($task, $args)
{


    //check the argument counts
    if (count($args) < 1) {
        throw new Exception('You must provide a vocabulary type.');
    }

    if (count($args) < 2) {
        throw new Exception('You must provide a file name.');
    }

    if (count($args) < 3) {
        throw new Exception('You must provide a vocabulary id.');
    }

    //set the arguments
    $type          = strtolower($args[0]);
    $filePath      = $args[1];
    $id            = $args[2];
    $deleteMissing = (isset($args[3]) && ("-d" == $args[3]));

    //do some basic validity checks

    if (! in_array($type, array("schema", "vocab", "vocabulary"))) {
        throw new Exception('You must import into a schema or a vocab');
    }

    if ("vocabulary" == $type) {
        $type = "vocab";
    }

    if (! is_numeric($id)) {
        throw new Exception('You must provide a valid ID');
    }

    //does the file exist?
    if (! file_exists($filePath)) {
        throw new Exception('You must supply a valid file to import: ' . $filePath);
    }

    //is the file a valid type?
    if (preg_match('/^.+\.([[:alpha:]]{2,4})$/', $filePath, $matches)) {
        if (! in_array(strtolower($matches[1]), array("json", "rdf", "csv", "xml"))) {
            throw new Exception('You must provide a valid file type based on the extension');
        }
    } else {
        throw new Exception("File type cannot be determined from the file extension");
    }

    $fileType = $matches[1];

    //is the object a valid object?
    if ('vocab' == $type) {
        $vocabObj = VocabularyPeer::retrieveByPK($id);
        if (is_null($vocabObj)) {
            throw new Exception('Invalid vocabulary ID');
        }

        //set some defaults
        $baseDomain = $vocabObj->getBaseDomain();
        $language   = $vocabObj->getLanguage();
        $statusId   = $vocabObj->getStatusId();

        //get a skos property id map
        $skosMap = SkosPropertyPeer::getPropertyNames();

        //there has to be a hash or a slash
        $tSlash = preg_match('@(/$)@i', $vocabObj->getUri()) ? '' : '/';
        $tSlash = preg_match('/#$/', $vocabObj->getUri()) ? '' : $tSlash;
    } else {
        $schemaObj = SchemaPeer::retrieveByPK($id);
        if (is_null($schemaObj)) {
            throw new Exception('Invalid schema ID');
        }

        //set some defaults
        $baseDomain = $schemaObj->getUri();
        $language   = $schemaObj->getLanguage();
        $statusId   = $schemaObj->getStatusId();

        //get a element set property id map
        $profileId  = 1;
        $profile    = ProfilePeer::retrieveByPK($profileId);
        $elementMap = $profile->getAllProperties();

        //there has to be a hash or a slash
        $tSlash = preg_match('@(/$)@i', $baseDomain) ? '' : '/';
        $tSlash = preg_match('/#$/', $baseDomain) ? '' : $tSlash;
    }

    //     insert jon's user id
    $userId = 36;

    /* From here on the process is the same regardless of UI */

    //execute
    //     parse file to get the fields/columns and data
    $file = fopen($filePath, "r");
    if (! $file) {
        throw new Exception("Can't read supplied file");
    }

    //     check to see if file has been uploaded before
    //          check import history for file name
    $importHistory = FileImportHistoryPeer::retrieveByLastFilePath($filePath);
    //          if reimport
    //               get last import history for filename
    //               unserialize column map
    //               match column names to AP based on map
    //     look for matches in unmatched field/column names to AP (ideal)
    //     csv table of data --
    //          row1: parsed field names/column headers
    //          row2: select dropdown with available fields from object AP (pre-select known matches)
    //                each select identified by column number
    //          row3: display datatype of selected field (updated dynamically when field selected)
    //          row4-13: first 10 rows of parsed data from file
    //     require a column that can match to 'URI' (maybe we'll allow an algorithm later)
    //     require columns that are required by AP
    //     on reimport there should be a flag to 'delete missing properties' from the current data
    //     note: at some point there will be a reimport process that allows URI changing
    //          this will require that there be an OMR identifier embedded in the incoming data

    switch ($fileType) {
        case "csv":
            try {
                $reader = new aCsvReader($filePath);
            } catch(Exception $e) {
                throw new Exception("Not a happy CSV file!");
            }

            if ('vocab' == $type) {
                // Get array of heading names found
                $headings = $reader->getHeadings();
                $fields   = ConceptPeer::getFieldNames();

                //set the map
                //      $map[] = array("property" => "Uri", "column" => "URILocal");
                //      $map[] = array("property" => "prefLabel", "column" => "skos:prefLabel");
                //      $map[] = array("property" => "definition", "column" => "skos:definition");
                //      $map[] = array("property" => "notation", "column" => "skos:notation");
                //      $map[] = array("property" => "scopeNote", "column" => "skos:scopeNote");

                $map = array(
                  "uri"        => "URILocal",
                  "prefLabel"  => "skos:prefLabel",
                  "definition" => "skos:definition",
                  "notation"   => "skos:notation",
                  "scopeNote"  => "skos:scopeNote"
                );

                $rows = 0;

                //executeImport:

                //    serialize the column map
                try {
                    while ($row = $reader->getRow()) {
                        $rows ++;
                        //        lookup the URI (or the OMR ID if available) for a match
                        $uri        = $baseDomain . $row[$map["uri"]];
                        $concept    = ConceptPeer::getConceptByUri($uri);
                        $updateTime = time();
                        $language   = (isset($map['language'])) ? $row[$map['language']] : $vocabObj->getLanguage();

                        if (! $concept) {
                            //          create a new concept or element
                            $concept = new Concept();
                            $concept->setVocabulary($vocabObj);
                            $concept->setUri($uri);
                            /**
                             * @todo Need to handle updates for topconcept here, just like language
                             **/
                            $concept->setIsTopConcept(false);
                            $concept->updateFromRequest(
                                    $userId,
                                      fixMarcEncoding(rtrim($row[$map['prefLabel']])),
                                      $language,
                                      $statusId
                            );
                        } //don't update the concept if the preflabel matches
                        else if ($row[$map['prefLabel']] != $concept->getPrefLabel()) {
                            $concept->updateFromRequest($userId, fixMarcEncoding(rtrim($row[$map['prefLabel']])));
                        }

                        //there needs to be a language to lookup the properties unless it's an objectProperty
                        $rowLanguage = (isset($map['language'])) ? $row[$map['language']] : $concept->getLanguage();

                        foreach ($map as $key => $value) {
                            //we skip because we already did them
                            if (! in_array($key, array('uri', 'prefLabel', 'language'))) {
                                $skosId = $skosMap[$key];
                                //check to see if the property already exists
                                $property =
                                  ConceptPropertyPeer::lookupProperty($concept->getId(), $skosId, $rowLanguage);

                                //create a new property for each unmatched column
                                if (! empty($row[$value])) {
                                    if (! $property) {
                                        $property = new ConceptProperty();
                                        $property->setCreatedUserId($userId);
                                        $property->setConceptId($concept->getId());
                                        $property->setCreatedAt($updateTime);
                                        $property->setSkosPropertyId($skosId);
                                    }

                                    if (($row[$value] != $property->getObject()) ||
                                        ($rowLanguage != $property->getLanguage())
                                    ) {
                                        /**
                                         * @todo We need a check here for skos objectproperties and handle differently
                                         **/
                                        if ($rowLanguage != $property->getLanguage()) {
                                            $property->setLanguage($rowLanguage);
                                        }
                                        if ($row[$value] != $property->getObject()) {
                                            $property->setObject(fixMarcEncoding(rtrim($row[$value])));
                                        }
                                        $property->setUpdatedUserId($userId);
                                        $property->setUpdatedAt($updateTime);
                                        $property->save();
                                    }
                                } //the row value is empty
                                else if ($deleteMissing && $property) {
                                    $property->delete();
                                }
                            }
                        }

                        //          else
                        //               lookup and update concept or element
                        //               lookup and update each property
                        //          update the history for each property, action is 'import', should be a single timestamp for all (this should be automatic)
                        //          if 'delete missing properties' is true
                        //               delete each existing, non-required property that wasn't updated by the import
                    }
                } catch(Exception $e) {
                    //          catch
                    //            if there's an error of any kind, write to error log and continue
                    echo "Error on row: " . $rows . ", " . $uri . "\n" . $e . "\n";
                    continue;
                }
                $objects = $vocabObj->countConcepts();
            } else //it's an element set
            {
                $map  = array(
                  "uri"        => "uriLocalPart",
                  "name"       => "reg:name",
                  "definition" => "skos:definition",
                  "label"      => "rdfs:label",
                  "note"       => array("tag" => "tagCap", "ind1" => "ind1Cap", "ind2" => "ind2Cap", "sub" => "subCap")
                );
                $rows = 0;

                //executeImport:
                //    serialize the column map
                try {
                    while ($row = $reader->getRow()) {
                        //        lookup the URI (or the OMR ID if available) for a match

                        //There always has to be a URI on either update or create
                        if (! isset($row[$map["uri"]])) {
                            throw new Exception('Missing URI for row: ' . $reader->getRowCount());
                            continue;
                        }

                        $rows ++;
                        $uri         = $baseDomain . $tSlash . $row[$map["uri"]];
                        $property    = SchemaPropertyPeer::retrieveByUri($uri);
                        $updateTime  = time();
                        $rowLanguage = (isset($map['language'])) ? $row[$map['language']] : $language;
                        $rowStatusId = (isset($map['status'])) ? $row[$map['status']] : $statusId;

                        if (! $property) {
                            //          create a new property
                            /** @var SchemaProperty * */
                            $property = new SchemaProperty();
                            $property->setSchema($schemaObj);
                            $property->setUri($uri);
                            $property->setCreatedUserId($userId);
                            $property->setCreatedAt($updateTime);
                        }

                        $property->setLanguage($rowLanguage);
                        $property->setStatusId($rowStatusId);
                        $property->setUpdatedUserId($userId);
                        $property->setUpdatedAt($updateTime);

                        if (isset($row[$map["label"]])) {
                            $property->setLabel($row[$map["label"]]);
                        }

                        if (isset($row[$map["name"]])) {
                            $property->setName($row[$map["name"]]);
                        }

                        if (isset($row[$map["definition"]])) {
                            $property->setDefinition($row[$map["definition"]]);
                        }

                        if (is_array($map["note"])) {
                            $note = '';
                            foreach ($map["note"] as $key => $value) {
                                $caption = ! empty($row[$value]) ? " (" . $row[$value] . ")" : ' (no caption)';
                                $note .= ! empty($row[$key]) ? $key . ": " . $row[$key] . $caption . "<br />" : "";
                            }
                            $property->setNote($note);
                        } else {
                            if (isset($row[$map["note"]])) {
                                $property->setNote($row[$map["note"]]);
                            }
                        }
                        $property->saveSchemaProperty($userId);

                        /**
                         * @todo Need to handle domain and range
                         **/

                        foreach ($map as $key => $value) {
                            //we skip because we already did them
                            if (! in_array(
                              $key,
                              array('uri', 'status', 'language', 'label', 'name', 'definition', 'comment', 'note')
                            )
                            ) {
                                $elementId = $elementMap[$key];
                                //check to see if the property already exists
                                //note that this also checks the object value as well, so there's no way to update or delete an existing triple
                                //the sheet would have to conatin the identifier for the triple
                                $element = SchemaPropertyElementPeer::lookupElement(
                                                                    $schemaObj->getId(),
                                                                      $elementId,
                                                                      $map[$value]
                                );

                                //create a new property for each unmatched column
                                if (! empty($row[$value])) {
                                    if (! $element) {
                                        $element = new SchemaPropertyElement();
                                        $element->setCreatedUserId($userId);
                                        $element->setCreatedAt($updateTime);
                                        $element->setProfilePropertyId($elementId);
                                    }

                                    if (($row[$value] != $element->getObject()) ||
                                        ($rowLanguage != $element->getLanguage())
                                    ) {
                                        /**
                                         * @todo We need a check here for objectproperties and handle differently
                                         **/
                                        if ($rowLanguage != $element->getLanguage()) {
                                            $element->setLanguage($rowLanguage);
                                        }
                                        if ($row[$value] != $element->getObject()) {
                                            $element->setObject($row[$value]);
                                        }
                                        $element->setUpdatedUserId($userId);
                                        $element->setUpdatedAt($updateTime);
                                        $element->save();
                                    }
                                } //the row value is empty
                                else if ($deleteMissing && $element) {
                                    $element->delete();
                                }
                            }
                        }

                        //          else
                        //               lookup and update concept or element
                        //               lookup and update each property
                        //          update the history for each property, action is 'import', should be a single timestamp for all (this should be automatic)
                        //          if 'delete missing properties' is true
                        //               delete each existing, non-required property that wasn't updated by the import
                    }
                } catch(Exception $e) {
                    //          catch
                    //            if there's an error of any kind, write to error log and continue
                    echo "Error on row: " . $rows . ", " . $uri . "\n" . $e . "\n";
                    continue;
                }
                $objects = $schemaObj->countSchemaPropertys();
            }
            //     save the import history file (match timestamp to history entries)
            break;
        case "json":
            break;
        case "rdf":
            break;
        case "xml":
            break;
        default:
    }

    /* output to stdout*/
    //          number of objects imported (link to history, filtered on timestamp of import)
    echo "File:" . $filePath . ";\n     Objects imported: " . $objects . "; Rows read: " . $rows . "\n";
    //          number of errors (link to error log)

}
 /**
  * overload saveSchemaProperty
  *
  * @return mixed
  * @param  SchemaProperty $schema_property
  */
 protected function saveSchemaProperty($schema_property)
 {
     $userId = sfContext::getInstance()->getUser()->getSubscriberId();
     $affectedRows = $schema_property->saveSchemaProperty($userId);
     return $affectedRows;
 }
    /**
     * @param \SchemaPropertyElement | \SchemaPropertyElement[] $dbElement
     * @param \SchemaProperty                                   $property
     *
     * @return int
     */
    private function deleteElement(&$dbElement, &$property)
    {
        if ( ! is_array($dbElement)) {
            //we don't delete derived properties at this stage
//      if (in_array($dbElement->getProfilePropertyId(),[6,9]))
//      {
//        return 0;
//      }
            $profileProperty = $this->profileProperties[$dbElement->getProfilePropertyId()];
            if ($profileProperty->getIsInForm() and $property->getLanguage() == $dbElement->getLanguage()) {
                $this->setPropertyValue('',
                                        $property,
                                        $profileProperty->getName(), ! $profileProperty->getIsObjectProp());
            }
            $dbElement->setUpdatedUserId($this->userId);
            $dbElement->importId = $this->importId;

            //$affectedRows = $dbElement->save();
            return $dbElement->delete();
        } else {
            $affectedRows = 0;
            /** @var \SchemaPropertyElement $element */
            foreach ($dbElement as &$element) {
                $affectedRows += $this->deleteElement($element, $property);
            }

            return $affectedRows;
        }
    }
 /**
  * Method called to associate a SchemaProperty object to this object
  * through the SchemaProperty foreign key attribute
  *
  * @param      SchemaProperty $l SchemaProperty
  * @return     void
  * @throws     PropelException
  */
 public function addSchemaPropertyRelatedByIsSubpropertyOf(SchemaProperty $l)
 {
     $this->collSchemaPropertysRelatedByIsSubpropertyOf[] = $l;
     $l->setSchemaPropertyRelatedByIsSubpropertyOf($this);
 }
 /**
  * @param SchemaProperty $property
  * @param string                            $fieldName
  * @param string                            $object
  * @param int                               $objectId if the object has a related Id
  * @param int                               $userId
  * @param array                             $fieldIds
  * @param int                               $statusId
  * @param Connection                        $con
  *
  * @throws Exception
  * @throws PropelException
  * @return bool
  */
 public static function updateRelatedElements($property, $fieldName, $object, $objectId, $userId, $fieldIds, $statusId, $con)
 {
     if (isset($fieldIds[$fieldName])) {
         $profileId = $fieldIds[$fieldName]['id'];
     } else {
         return false;
     }
     $language = $fieldIds[$fieldName]['hasLang'] ? $property->getLanguage() : null;
     $element = SchemaPropertyElementPeer::lookupDetailElement($property->getId(), $profileId, $language);
     if ($element) {
         //no matter what we do, it's not generated any more
         $element->setIsGenerated(false);
         //did we make it null?
         if (0 === strlen(trim($object))) {
             //we have to make sure that it's not a subclass or subproperty
             if (('is_subproperty_of' == $fieldName || 'is_subclass_of' == $fieldName) && $property->getParentUri()) {
                 //there's a uri but it doesn't match anything registered
                 //so we have to delete just the reciprocal
                 $element->updateReciprocal('deleted', $userId, $property->getSchemaId(), $con);
             } else {
                 //delete the element
                 $element->delete($con);
             }
         } else {
             //modify it
             $element->setObject($object);
             $element->setRelatedSchemaPropertyId($objectId);
             $element->setUpdatedUserId($userId);
             $element->save();
         }
     } elseif ($profileId && $object) {
         //create one
         $element = SchemaPropertyElementPeer::createElement($property, $userId, $profileId, $statusId, $language, false);
         $element->setObject($object);
         $element->setRelatedSchemaPropertyId($objectId);
         $element->setIsSchemaProperty(TRUE);
         $element->save();
     }
     return true;
 }
 /**
  * Method called to associate a SchemaProperty object to this object
  * through the SchemaProperty foreign key attribute
  *
  * @param      SchemaProperty $l SchemaProperty
  * @return     void
  * @throws     PropelException
  */
 public function addSchemaProperty(SchemaProperty $l)
 {
     $this->collSchemaPropertys[] = $l;
     $l->setSchema($this);
 }
Example #10
0
 /**
  * Method called to associate a SchemaProperty object to this object
  * through the SchemaProperty foreign key attribute
  *
  * @param      SchemaProperty $l SchemaProperty
  * @return     void
  * @throws     PropelException
  */
 public function addSchemaPropertyRelatedByUpdatedUserId(SchemaProperty $l)
 {
     $this->collSchemaPropertysRelatedByUpdatedUserId[] = $l;
     $l->setUserRelatedByUpdatedUserId($this);
 }