Skip to content

Commit

Permalink
create friendly terms for Vocabs in database
Browse files Browse the repository at this point in the history
  • Loading branch information
Thom Mc committed May 13, 2015
1 parent d9b4c98 commit dad7560
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 6 deletions.
108 changes: 104 additions & 4 deletions ItemRelationsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ItemRelationsPlugin extends Omeka_Plugin_AbstractPlugin
protected $_filters = array(
'admin_items_form_tabs',
'admin_navigation_main',
'api_resources',
'api_resources',
);

public function filterApiResources($apiResources)
Expand Down Expand Up @@ -307,15 +307,43 @@ public function hookUpgrade($args)
$db->query($sql);
}
}
}

/* if ($oldVersion <= '2.1.0')
{
$sql = "SHOW COLUMNS FROM `{$db->ItemRelationsProperty}` LIKE 'friendly_part'";
$statement = $this->adapter->query($sql);
print_r($statement->execute());
}
*/ }

/**
* Add the translations.
*/
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');

$db = get_db();
$adapter = $db->getAdapter();

// if (floatval($oldVersion) <= '2.1')
{
$sql = "SHOW COLUMNS FROM `{$db->ItemRelationsProperty}` LIKE 'friendly_part'";
$statement = $adapter->query($sql);

// do we already have a column in the db for friendly_part?
if (! $adapter->query($sql)->fetch())
{
// Nein! add friendly parts to the db.
$this->populateFriendlyParts();
} /*
else
{
// Jawohl! do nothing.
} */
}
}


/**
* Define the ACL.
Expand All @@ -330,8 +358,8 @@ public function hookDefineAcl($args)
$vocabResource = new Zend_Acl_Resource('ItemRelations_Vocabularies');
$acl->add($indexResource);
$acl->add($vocabResource);
$acl->addResource('Relations');
$acl->allow(null, 'Relations');
$acl->addResource('Relations');
$acl->allow(null, 'Relations');
}

/**
Expand Down Expand Up @@ -638,4 +666,76 @@ public static function insertItemRelation($subjectItem, $propertyId, $objectItem

return true;
}

public function populateFriendlyParts()
{
$db = get_db();
$adapter = $db->getAdapter();

$sql = "ALTER TABLE `{$db->ItemRelationsProperty}` ADD friendly_part VARCHAR(100) AFTER local_part";

$adapter->query($sql);

// build a list of vocabs we have in the db
$vocabsResult = $db->getTable('ItemRelationsVocabulary')->findAll();
$dbVocabs = array(); // put the vocabs from the db into a handily-keyed format
foreach($vocabsResult as $row)
{
$dbVocabs[$row->namespace_prefix] = $row;
}

// get vocabs we have template for in PHP include
$templateFormalVocabularies = include 'formal_vocabularies.php';

$templateVocabularyNamespacesPresent = array(); // hold a list of templated vocabs
foreach ($templateFormalVocabularies as $templateFormalVocabulary)
{
$templateVocabularyNamespacesPresent[] = $templateFormalVocabulary['namespace_prefix'];

// create vocab in db if not present
if (! in_array($templateFormalVocabulary['namespace_prefix'], array_keys($dbVocabs)))
{
$vocabulary = new ItemRelationsVocabulary;
$vocabulary->name = $templateFormalVocabulary['name'];
$vocabulary->description = $templateFormalVocabulary['description'];
$vocabulary->namespace_prefix = $templateFormalVocabulary['namespace_prefix'];
$vocabulary->namespace_uri = $templateFormalVocabulary['namespace_uri'];
$vocabulary->custom = 0;
$vocabulary->save();

$vocabularyId = $vocabulary->id;
}
}

// should have all template vocabs in the db now, fetch them again
$vocabsResult = $db->getTable('ItemRelationsVocabulary')->findAll();
foreach($templateFormalVocabularies as $templateVocab)
{
// does this db vocab exist in the template? if not, skip it
if (empty($dbVocabs[$templateVocab['namespace_prefix']]))
{
continue;
}

// get this db vocab's properties from the db
$propertiesResult = $db->getTable('ItemRelationsProperty')->findByVocabularyId($dbVocabs[$templateVocab['namespace_prefix']]['id']);
// put them into a handily-keyed array so we can compare them with the template ones
$dbVocabProperties = array();
foreach ($propertiesResult as $row)
{
$dbVocabProperties[$row['local_part']] = $row;
}

foreach ($templateVocab['properties'] as $templateVocabProperty)
{
if ($dbVocabProperties[$templateVocabProperty['local_part']]->friendly_part != $templateVocabProperty['friendly_part'])
{
$property = $dbVocabProperties[$templateVocabProperty['local_part']];
$property->friendly_part = $templateVocabProperty['friendly_part'];
$property->save();
}
}
}
}

}
4 changes: 2 additions & 2 deletions plugin.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ link="http://omeka.org/codex/Plugins/ItemRelations"
support_link="http://omeka.org/forums/forum/plugins"
license="GPLv3"
omeka_minimum_version="2.0"
omeka_target_version="2.0"
version="2.0.3a"
omeka_target_version="2.3"
version="2.0.4"
tags="item,relation,rdf"

0 comments on commit dad7560

Please sign in to comment.