Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 58 additions & 21 deletions CollectionTreePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*/
class CollectionTreePlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array(
'install',
'uninstall',
Expand All @@ -23,17 +26,29 @@ class CollectionTreePlugin extends Omeka_Plugin_AbstractPlugin
'before_save_collection',
'after_save_collection',
'after_delete_collection',
'collection_browse_sql',
'collections_browse_sql',
'admin_collections_show',
'public_collections_show',
);

/**
* @var array Filters for the plugin.
*/
protected $_filters = array(
'admin_navigation_main',
'public_navigation_main',
'admin_collections_form_tabs',
'collections_select_options',
);


/**
* @var array Options and their default values.
*/
protected $_options = array(
'collection_tree_alpha_order' => 0,
'collection_tree_browse_only_root' => 0,
);

/**
* Install the plugin.
*
Expand All @@ -54,9 +69,9 @@ public function hookInstall()
UNIQUE KEY `collection_id` (`collection_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$this->_db->query($sql);
set_option('collection_tree_alpha_order', '0');

$this->_installOptions();

// Save all collections in the collection_trees table.
$collectionTable = $this->_db->getTable('Collection');
$collections = $this->_db->fetchAll("SELECT id FROM {$this->_db->Collection}");
Expand All @@ -77,8 +92,8 @@ public function hookUninstall()
{
$sql = "DROP TABLE IF EXISTS {$this->_db->CollectionTree}";
$this->_db->query($sql);
delete_option('collection_tree_alpha_order');

$this->_uninstallOptions();
}

/**
Expand Down Expand Up @@ -122,30 +137,34 @@ public function hookUpgrade($args)
}
}
}

/**
* Display the config form.
*/
public function hookConfigForm()
public function hookConfigForm($args)
{
echo get_view()->partial('plugins/collection-tree-config-form.php');
$view = get_view();
echo $view->partial('plugins/collection-tree-config-form.php');
}

/**
* Handle the config form.
*/
public function hookConfig()
public function hookConfig($args)
{
set_option('collection_tree_alpha_order', $_POST['collection_tree_alpha_order']);
$post = $args['post'];
foreach ($post as $key => $value) {
set_option($key, $value);
}
}

public function hookBeforeSaveCollection($args)
{
$collectionTree = $this->_db->getTable('CollectionTree')->findByCollectionId($args['record']->id);
if (!$collectionTree) {
return;
}

// Only validate the relationship during a form submission.
if (isset($args['post']['collection_tree_parent_collection_id'])) {
$collectionTree->parent_collection_id = $args['post']['collection_tree_parent_collection_id'];
Expand Down Expand Up @@ -206,20 +225,25 @@ public function hookAfterDeleteCollection($args)
}

/**
* Omit all child collections from the collection browse.
* Hook for collections browse: omit all child collections from the collection
* browse.
*/
public function hookCollectionBrowseSql($args)
public function hookCollectionsBrowseSql($args)
{
if (!is_admin_theme()) {
if (!get_option('collection_tree_browse_only_root')) {
return;
}
$sql = "
c.id NOT IN (
collections.id NOT IN (
SELECT ct.collection_id
FROM {$this->_db->CollectionTree} ct
WHERE ct.parent_collection_id != 0
)";
$args['select']->where($sql);
}
}

/**
* Display the collection's parent collection and child collections.
*/
Expand All @@ -231,9 +255,9 @@ public function hookAdminCollectionsShow($args)
/**
* Display the collection's parent collection and child collections.
*/
public function hookPublicCollectionsShow()
public function hookPublicCollectionsShow($args)
{
$this->_appendToCollectionsShow(get_current_record('collection'));
$this->_appendToCollectionsShow($args['collection']);
}

protected function _appendToCollectionsShow($collection)
Expand Down Expand Up @@ -285,4 +309,17 @@ public function filterAdminCollectionsFormTabs($tabs, $args)
);
return $tabs;
}

/**
* Manage search options for collections.
*
* @param array Search options for collections.
* @return array Filtered search options for collections.
*/
public function filterCollectionsSelectOptions($options)
{
$treeOptions = $this->_db->getTable('CollectionTree')->findPairsForSelectForm();
// Keep only chosen collections, in case another filter removed some.
return array_intersect_key($treeOptions, $options);
}
}
26 changes: 20 additions & 6 deletions views/admin/plugins/collection-tree-config-form.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
<div class="field">
<div id="collection_tree_alpha_order_label" class="two columns alpha">
<label for="collection_tree_alpha_order"><?php echo __('Order alphabetically?'); ?></label>
<div class="two columns alpha">
<?php echo $this->formLabel('collection_tree_alpha_order', __('Order alphabetically')); ?>
</div>
<div class="inputs five columns omega">
<p class="explanation"><?php echo __('Order the collection tree alphabetically? '
. 'This does not affect the order of the collections browse page.'); ?></p>
<?php echo $this->formCheckbox('collection_tree_alpha_order', null,
array('checked' => (bool) get_option('collection_tree_alpha_order'))); ?>
<p class="explanation"><?php
echo __('Order the collection tree alphabetically?');
echo __('This does not affect the order of the collections browse page.');
?></p>
<?php echo $this->formCheckbox('collection_tree_alpha_order', null,
array('checked' => (bool) get_option('collection_tree_alpha_order'))); ?>
</div>
</div>
<div class="field">
<div class="two columns alpha">
<?php echo $this->formLabel('collection_tree_browse_only_root', __('Browse collections')); ?>
</div>
<div class="inputs five columns omega">
<p class="explanation"><?php
echo __('The "Browse collections" page can be the normal one or, if checked, limited to root collections.');
?></p>
<?php echo $this->formCheckbox('collection_tree_browse_only_root', null,
array('checked' => (bool) get_option('collection_tree_browse_only_root'))); ?>
</div>
</div>