-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMediaBucketsPlugin.php
124 lines (111 loc) · 4.64 KB
/
MediaBucketsPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
define('MEDIABUCKETS_PLUGIN_DIR', PLUGIN_DIR . '/MediaBuckets');
include(MEDIABUCKETS_PLUGIN_DIR . '/functions.php');
class MediaBucketsPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array('install',
'items_browse_sql',
'config',
'config_form',
//'public_collections_show',
//'public_collections_browse_each'
);
protected $_filters = array(
'admin_navigation_main'
);
public function setUp()
{
parent::setUp();
add_shortcode('media_buckets', array('MediaBucketsPlugin', 'shortcode'));
}
public function hookInstall($args)
{
//install the MediaBucket ItemType
$metadata = array('name' => __("MediaBucket"),
'description' => __("A bucket to hold media for use elsewhere, usually not a real item")
);
$elements = array(
array('name' => 'collection',
'description' => 'A collection ID that this bucket is associated with'
),
array('name' => 'exhibit',
'description' => 'An exhibit ID that this bucket is associated with'
),
);
insert_item_type($metadata, $elements);
}
public function hookConfigForm($args)
{
include MEDIABUCKETS_PLUGIN_DIR . '/config_form.php';
}
public function hookConfig($args)
{
$post = $args['post'];
set_option('media_buckets_ignore_buckets', $post['media_buckets_ignore_buckets']);
}
public function hookItemsBrowseSql($args)
{
if (get_option('media_buckets_ignore_buckets')) {
$select = $args['select'];
$db = get_db();
//I don't feel like mucking about with checking whether the
//join on ItemType is already present, so I'll just
//grab the ItemType for its id
$itemType = $db->getTable('ItemType')->findByName('MediaBucket');
$select->where("item_type_id != ?", $itemType->id);
//WTF? != in a where clause ignores NULL values? wtfwtfwtf
$select->orWhere("item_type_id IS NULL");
}
}
public function filterAdminNavigationMain($tabs)
{
$tabs['MediaBuckets'] = array('uri' => url('media-buckets/buckets/browse'),
'label' => __('Media Buckets')
);
return $tabs;
}
public static function shortcode($args, $view)
{
if (isset($args['record_class']) && isset($args['id'])) {
$recordClass = Inflector::classify($args['record_class']);
$record = get_db()->getTable($recordClass)->find($args['id']);
$bucket = self::getBucketForRecord($record);
//huh. didn't like the camelCased arg name itemType
if (isset($args['gallery'])) {
if (isset($args['image_type'])) {
return media_buckets_record_gallery($record, array(), $args['image_type']);
} else {
return media_buckets_record_gallery($record);
}
} else {
if (isset($args['image_type'])) {
return media_buckets_record_image($record, 0, $args['image_type']);
} else {
return media_buckets_record_image($record);
}
}
}
}
public static function getBucketForRecord($record)
{
$db = get_db();
$itemType = $db->getTable('ItemType')->findByName('MediaBucket');
$elementName = Inflector::underscore(get_class($record));
$element = $db->getTable('Element')->findByElementSetNameAndElementName('Item Type Metadata', $elementName);
$searchArray = array('advanced'
=> array(
array('element_id' => $element->id,
'type' => 'is exactly',
'terms' => $record->id
)
)
);
$itemTable = $db->getTable('Item');
$select = $itemTable->getSelect();
$itemTable->applySearchFilters($select, $searchArray);
$select->where('item_type_id = ?', $itemType->id);
$select->limit(1);
$bucket = $db->getTable('Item')->fetchObject($select);
return $bucket;
}
}