Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide orphans and add treeview style #32

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
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
105 changes: 67 additions & 38 deletions CollectionTreePlugin.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
/**
* Collection Tree
*
*
* @copyright Copyright 2007-2012 Roy Rosenzweig Center for History and New Media
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
*/

/**
* The Collection Tree plugin.
*
*
* @package Omeka\Plugins\CollectionTree
*/
class CollectionTreePlugin extends Omeka_Plugin_AbstractPlugin
Expand All @@ -32,6 +32,8 @@ class CollectionTreePlugin extends Omeka_Plugin_AbstractPlugin
'public_items_search',
'admin_collections_show',
'public_collections_show',
'admin_head',
'public_head'
);

/**
Expand All @@ -50,8 +52,11 @@ class CollectionTreePlugin extends Omeka_Plugin_AbstractPlugin
*/
protected $_options = array(
'collection_tree_alpha_order' => 0,
'collection_tree_browse_only_root' => 0,
'collection_tree_show_subcollections' => 0,
'collection_tree_hide_orphans' => 0,
'collection_tree_treeview_style' => 0,
'collection_tree_treeview_expanded' => '',
'collection_tree_browse_only_root' => 0,
'collection_tree_search_descendant' => 0,
);

Expand All @@ -65,7 +70,7 @@ public function hookInstall()
{
// collection_id must be unique to satisfy the AT MOST ONE parent
// collection constraint.
$sql = "
$sql = "
CREATE TABLE IF NOT EXISTS `{$this->_db->CollectionTree}` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`parent_collection_id` int(10) unsigned NOT NULL,
Expand All @@ -90,7 +95,7 @@ public function hookInstall()
$collectionTree->save();
}
}

/**
* Uninstall the plugin.
*/
Expand All @@ -101,7 +106,7 @@ public function hookUninstall()

$this->_uninstallOptions();
}

/**
* Initialize the plugin.
*/
Expand All @@ -110,22 +115,22 @@ public function hookInitialize()
// Add translation.
add_translation_source(dirname(__FILE__) . '/languages');
}

/**
* Upgrade from earlier versions.
*/
public function hookUpgrade($args)
{
// Prior to Omeka 2.0, collection names were stored in the collections
// table; now they are stored as Dublin Core Title. This upgrade
// compensates for this by moving the collection names to the
// Prior to Omeka 2.0, collection names were stored in the collections
// table; now they are stored as Dublin Core Title. This upgrade
// compensates for this by moving the collection names to the
// collection_trees table.
if (version_compare($args['old_version'], '2.0', '<')) {

// Add the name column to the collection_trees table.
$sql = "ALTER TABLE {$this->_db->CollectionTree} ADD `name` TEXT NULL";
$this->_db->query($sql);

// Assign names to their corresponding collection_tree rows.
$collectionTreeTable = $this->_db->getTable('CollectionTree');
$collectionTable = $this->_db->getTable('Collection');
Expand Down Expand Up @@ -182,7 +187,7 @@ public function hookBeforeSaveCollection($args)
}
}
}

/**
* Save the parent/child relationship.
*/
Expand All @@ -196,18 +201,18 @@ public function hookAfterSaveCollection($args)
$collectionTree->collection_id = $collection->id;
$collectionTree->parent_collection_id = 0;
}

// Only save 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'];
}

$collectionTree->name = metadata($args['record'], array('Dublin Core', 'Title'));

// Fail silently if the record does not validate.
$collectionTree->save();
}

/**
* Handle collection deletions.
*
Expand All @@ -220,13 +225,13 @@ public function hookAfterDeleteCollection($args)
{
$collection = $args['record'];
$collectionTreeTable = $this->_db->getTable('CollectionTree');

// Delete the relationship with the parent collection.
$collectionTree = $collectionTreeTable->findByCollectionId($collection->id);
if ($collectionTree) {
$collectionTree->delete();
}

// Move child collections to root level by deleting their relationships.
$collectionTrees = $collectionTreeTable->findByParentCollectionId($collection->id);
foreach ($collectionTrees as $collectionTree) {
Expand Down Expand Up @@ -273,7 +278,7 @@ public function hookItemsBrowseSql($args)
// Collection can be an object when not called from search form.
? $params['descendant_or_self']->id
// Else this should be an integer.
: (integer) $params['descendant_or_self'];
: (int) $params['descendant_or_self'];

if (empty($collection)) {
return;
Expand All @@ -288,7 +293,8 @@ public function hookItemsBrowseSql($args)
$select->joinInner(
array('collection_tree_collections' => $this->_db->Collection),
'items.collection_id = collection_tree_collections.id',
array());
array()
);

// There are descendants.
if (count($collections) > 1) {
Expand Down Expand Up @@ -321,7 +327,7 @@ public function hookPublicItemsSearch($args)
}

/**
* Append items search checkbox to the advanced search page.
* Append items search checkbox to the advanced search page.
*
* @return string HTML
*/
Expand All @@ -342,26 +348,51 @@ protected function _itemsSearch($args)
*/
public function hookAdminCollectionsShow($args)
{
$this->_appendToCollectionsShow($args['collection']);
$this->_appendToCollectionsShow($args['collection'], 'admin');
}

/**
* Display the collection's parent collection and child collections.
*/
public function hookPublicCollectionsShow($args)
{
$this->_appendToCollectionsShow($args['collection']);
$this->_appendToCollectionsShow($args['collection'], 'public');
}
protected function _appendToCollectionsShow($collection)

protected function _appendToCollectionsShow($collection, $side)
{
$collectionTree = $this->_db->getTable('CollectionTree')->getCollectionTree($collection->id);
echo get_view()->partial(
'collections/collection-tree-list.php',
array('collection_tree' => $collectionTree)
);

if (count($collectionTree[0]['children']) > 0 || !(bool)get_Option('collection_tree_hide_orphans')) {
echo get_view()->partial(
'collections/collection-tree-list.php',
array('collection_tree' => $collectionTree, 'side' => $side)
);
}
}


/**
* Sets css and js in case treeview style is chosen
*/
public function hookPublicHead($args)
{
if (get_option('collection_tree_treeview_style')) {
queue_css_file('file-explore');
queue_js_file('file-explore');
}
}

/**
* Sets css and js in case treeview style is chosen
*/
public function hookAdminHead($args)
{
if (get_option('collection_tree_treeview_style')) {
queue_css_file('file-explore');
queue_js_file('file-explore');
}
}

/**
* Add the collection tree page to the admin navigation.
*/
Expand All @@ -370,7 +401,7 @@ public function filterAdminNavigationMain($nav)
$nav[] = array('label' => __('Collection Tree'), 'uri' => url('collection-tree'));
return $nav;
}

/**
* Add the collection tree page to the public navigation.
*/
Expand All @@ -379,15 +410,15 @@ public function filterPublicNavigationMain($nav)
$nav[] = array('label' => __('Collection Tree'), 'uri' => url('collection-tree'));
return $nav;
}

/**
* Display the parent collection form.
*/
public function filterAdminCollectionsFormTabs($tabs, $args)
{
$collection = $args['collection'];
$collectionTreeTable = $this->_db->getTable('CollectionTree');

$options = $collectionTreeTable->findPairsForSelectForm();
$options = array('0' => __('No parent collection')) + $options;

Expand All @@ -398,13 +429,12 @@ public function filterAdminCollectionsFormTabs($tabs, $args)
$parentCollectionId = 0;
}
$tabs['Parent Collection'] = get_view()->partial(
'collections/collection-tree-parent-form.php',
'collections/collection-tree-parent-form.php',
array('options' => $options, 'parent_collection_id' => $parentCollectionId)
);
return $tabs;
}


/**
* Filter items browse params to broaden the search to subcollections.
*
Expand All @@ -418,7 +448,7 @@ public function filterItemsBrowseParams($params)
&& !isset($params['subcollections'])
&& get_option('collection_tree_show_subcollections')
) {
$params['subcollections'] = 1;
$params['subcollections'] = 1;
}

if (!empty($params['subcollections'])) {
Expand All @@ -439,7 +469,6 @@ public function filterItemsBrowseParams($params)
return $params;
}


/**
* Manage search options for collections.
*
Expand Down
Loading