Skip to content

Commit

Permalink
Merge pull request #2 from Daniel-KM/open_taxonomy
Browse files Browse the repository at this point in the history
Open taxonomy
  • Loading branch information
jajm committed Dec 1, 2016
2 parents 11a9c4c + cfcbff3 commit af0b96c
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 20 deletions.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
Taxonomy (plugin for Omeka)
===========================

[Taxonomy] is a plugin for [Omeka] that allows to create vocabularies and to
assign them to elements to fill.

Unlike [Simple Vocab], the taxonomies contain a code and a value. This is
specially important to simplify the share and to respect standards, for example
for the list of countries [ISO 3166-1]. Even if the code is saved, it is not
viewable, because the true value is always displayed automatically.

New terms can be added by the admin, and, if allowed, by the user directly in
the main forms for items `admin/items/edit` and collections `admin/collections/edit`.


Installation
------------

Install first [Element Types].

Uncompress files and rename plugin folder "Taxonomy".

Then install it like any other Omeka plugin.


Troubleshooting
---------------

See online issues on the [plugin issues] page on GitHub.


License
-------

This plugin is published under [GNU/GPL v3].

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


Contact
-------

Current maintainers of the plugin:
* Julian Maurice (see [jajm])


Copyright
---------

* Copyright Julian Maurice for Biblibre, 2015
* Copyright Daniel Berthereau, 2016


[Taxonomy]: https://github.com/biblibre/omeka-plugin-Taxonomy
[Omeka]: https://omeka.org
[Simple Vocab]: https://github.com/omeka/plugin-SimpleVocab
[ISO 3166-1]: http://www.iso.org/iso/country_codes/country_codes
[Element Types]: https://github.com/biblibre/omeka-plugin-ElementTypes
[plugin issues]: https://github.com/biblibre/omeka-plugin-Taxonomy/issues
[GNU/GPL v3]: https://www.gnu.org/licenses/gpl-3.0.html
[jajm]: https://github.com/jajm
[Daniel-KM]: https://github.com/Daniel-KM "Daniel Berthereau"
98 changes: 79 additions & 19 deletions TaxonomyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class TaxonomyPlugin extends Omeka_Plugin_AbstractPlugin
'install',
'uninstall',
'initialize',
'admin_head',
);

protected $_filters = array(
Expand Down Expand Up @@ -50,6 +51,11 @@ public function hookInitialize()
add_translation_source(dirname(__FILE__) . '/languages');
}

public function hookAdminHead($args)
{
queue_js_file('taxonomy');
}

public function filterAdminNavigationMain($nav)
{
$nav[] = array(
Expand All @@ -64,6 +70,7 @@ public function filterElementTypesInfo($types) {
'label' => __('Taxonomy term'),
'filters' => array(
'ElementInput' => array($this, 'filterElementInput'),
'Save' => array($this, 'filterSave'),
'Display' => array($this, 'filterDisplay'),
),
'hooks' => array(
Expand All @@ -76,31 +83,87 @@ public function filterElementTypesInfo($types) {
public function filterElementInput($components, $args)
{
$view = get_view();
$db = get_db();
$db = $this->_db;

$element = $args['element'];
$element_id = $element->id;
$index = $args['index'];
$name = "Elements[$element_id][$index][text]";

$taxonomy_id = $args['element_type_options']['taxonomy_id'];
$open = !empty($args['element_type_options']['open']);
if ($taxonomy_id) {
$terms = $db->getTable('TaxonomyTerm')->findByTaxonomyId($taxonomy_id);
$options = array('' => '');
foreach ($terms as $term) {
$options[$term['code']] = $term['value'];
$terms = $db->getTable('TaxonomyTerm')->listByTaxonomy($taxonomy_id);
$options = array('' => '') + $terms;
if ($open) {
$options['insert_new_term'] = '> ' . __('Add a new code') . ' <';
}

$components['input'] = $view->formSelect($name, $args['value'], null, $options);
$components['html_checkbox'] = NULL;
$components['input'] = $view->formSelect($name, $args['value'],
array('class' => 'taxonomy taxonomy-open'), $options);
if ($open) {
$components['input'] .= $view->formText(
'taxonomy_input_' . $element_id,
'',
array(
'placeholder' => __('New code'),
'class' => 'taxonomy taxonomy-open',
'style' => 'display: none',
));
$components['input'] .= $view->formButton(
'taxonomy_insert_' . $element_id,
__('Enter new code'),
array(
'class' => 'taxonomy taxonomy-open button blue small',
'style' => 'display: none',
));
}
$components['html_checkbox'] = null;
}

return $components;
}

public function filterSave($text, $args)
{
$text = trim($text);
if (!strlen($text)) {
return $text;
}

if ($text == 'insert_new_term') {
return '';
}

$closed = empty($args['element_type_options']['open']);
if ($closed) {
return $text;
}

// Check if the term doesn't exist (prevent some issues).
$taxonomy_id = $args['element_type_options']['taxonomy_id'];
if (!$taxonomy_id) {
return $text;
}

$db = $this->_db;
$term = $db->getTable('TaxonomyTerm')->findByCode($taxonomy_id, $text);
if ($term) {
return $text;
}

// TODO The text should be already filtered by Omeka.
$term = new TaxonomyTerm();
$term->taxonomy_id = $taxonomy_id;
$term->code = $text;
$term->value = $text;
$term->save();

return $text;
}

public function filterDisplay($text, $args)
{
$db = get_db();
$db = $this->_db;

$taxonomy_id = $args['element_type_options']['taxonomy_id'];
if ($taxonomy_id) {
Expand All @@ -112,9 +175,10 @@ public function filterDisplay($text, $args)
return $text;
}

public function hookOptionsForm($args) {
public function hookOptionsForm($args)
{
$view = get_view();
$db = get_db();
$db = $this->_db;

$options = $args['element_type']['element_type_options'];

Expand All @@ -123,13 +187,9 @@ public function hookOptionsForm($args) {
foreach ($taxonomies as $taxonomy) {
$taxonomy_options[$taxonomy->id] = $taxonomy->name;
}

print $view->formLabel('taxonomy_id', __('Taxonomy')) . ' ';
print $view->formSelect(
'taxonomy_id',
isset($options) ? $options['taxonomy_id'] : '',
null,
$taxonomy_options
);
echo $view->partial('taxonomy/option-form.php', array(
'options' => $options,
'taxonomy_options' => $taxonomy_options,
));
}
}
2 changes: 1 addition & 1 deletion plugin.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ license = "GPLv3"
link = "https://github.com/biblibre/omeka-plugin-Taxonomy"
support_link = "https://github.com/biblibre/omeka-plugin-Taxonomy/issues"
omeka_minimum_version = "2.0"
omeka_target_version = "2.3"
omeka_target_version = "2.4.1"
required_plugins = "ElementTypes"
optional_plugins = ""
39 changes: 39 additions & 0 deletions views/admin/javascripts/taxonomy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
jQuery(document).bind("omeka:elementformload", function() {
jQuery("select.taxonomy-open").each(function() {
jQuery(this).change(function() {
var val = jQuery(this).val();
if (val == 'insert_new_term') {
jQuery(this).next('input.taxonomy-open').show();
jQuery(this).parent().find('button.taxonomy-open').show();
} else {
jQuery(this).next('input.taxonomy-open').hide();
jQuery(this).parent().find('button.taxonomy-open').hide();
}
}).change();

jQuery("button.taxonomy-open").click(function(){
var field = jQuery(this).parent().find('input.taxonomy-open');
var val = field.val().trim();
if (val.length == 0) {
return;
}
var select = jQuery(this).parent().find('select.taxonomy-open');
var exists = false;
select.find('option').each(function(){
if (this.value == val) {
exists = true;
return false;
}
});
if (!exists) {
var insertNewTerm = select.children("option[value='insert_new_term']")[0].outerHTML;
select.children("option[value='insert_new_term']").remove();
select.append('<option value="' + val + '">' + val + '</option>');
select.append(insertNewTerm);
}
select.val(val);
field.val('').hide();
jQuery(this).hide();
});
});
});
27 changes: 27 additions & 0 deletions views/admin/taxonomy/option-form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<div class="field">
<div class="two columns alpha">
<?php echo $this->formLabel('taxonomy_id', __('Taxonomy')); ?>
</div>
<div class="inputs five columns omega">
<?php echo $this->formSelect(
'taxonomy_id',
isset($options['taxonomy_id']) ? $options['taxonomy_id'] : '',
null,
$taxonomy_options
);
?>
</div>
</div>
<div class="field">
<div class="two columns alpha">
<?php echo $this->formLabel('open',__('Open')); ?>
</div>
<div class="inputs five columns omega">
<?php echo $this->formCheckbox('open', true,
array('checked' => isset($options['open']) ? $options['open'] : 0));
?>
<p class="explanation">
<?php echo __('If checked, the user will be able to add a new term via the form.'); ?>
</p>
</div>
</div>

0 comments on commit af0b96c

Please sign in to comment.