This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnhancedCollectionsPlugin.php
252 lines (217 loc) · 5.36 KB
/
EnhancedCollectionsPlugin.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
require 'functions.php';
/**
* Enhanced Collections Plugin
*
* @author Dave Widmer <[email protected]>
*/
class EnhancedCollectionsPlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var string The current version of the plugin
*/
protected $version = "0.1.0";
/**
* @var array All of the hooks used in this plugin
*/
protected $_hooks = array('install', 'uninstall', 'define_routes',
'admin_collections_browse_each', 'public_footer'
);
/**
* @var array The filters used in this plugin.
*/
protected $_filters = array('public_theme_name', 'public_navigation_items');
/**
* @var string The name of the theme...
*/
private $theme_name = null;
/**
* Installation hook.
*/
public function hookInstall()
{
$db = get_db();
$sql = "CREATE TABLE IF NOT EXISTS `{$db->prefix}enhanced_collections` (
`id` int(10) unsigned NOT NULL,
`theme` varchar(100) NOT NULL,
`per_page` smallint(5) unsigned NOT NULL,
`tracking_id` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
$db->query($sql);
$per_page = get_option('per_page_public');
$records = $db->getTable('Collection')->findAll();
foreach ($records as $row)
{
$collection = new EnhancedCollection;
$collection->setArray(array(
'id' => $row->id,
'per_page' => $per_page,
'theme' => "",
'tracking_id' => ''
));
$collection->save();
}
$this->_installOptions();
}
/**
* Uninstalls any options that have been set.
*/
public function hookUninstall()
{
$db = get_db();
$db->query("DROP TABLE IF EXISTS `{$db->prefix}enhanced_collections`");
$this->_uninstallOptions();
}
/**
* Add in routes.ini
*
* @param array $args The route arguments
*/
public function hookDefineRoutes($args)
{
$router = $args['router'];
$path = dirname(__FILE__).DIRECTORY_SEPARATOR.'routes.ini';
$router->addConfig(new Zend_Config_Ini($path, 'routes'));
}
/**
* Add a settings link to the browse screen.
*
* @param array $args An array holding the current collection object
*/
public function hookAdminCollectionsBrowseEach($args)
{
$collection = $args['collection'];
if (is_allowed($collection, 'edit'))
{
echo link_to_collection(__("Settings"), array(), 'settings', $collection);
}
}
/**
* Checks for the Google Analytics Tracking Id and adds it if necessary
*
* @param array $args The hook arguments
*/
public function hookPublicFooter($args)
{
if (is_admin_theme())
{
return;
}
$collection = null;
if (isset($args['collection']) AND $args['collection'] !== null)
{
$collection = get_db()->getTable('EnhancedCollection')->find($args['collection']);
}
if ($collection !== null)
{
if ( ! empty($collection->tracking_id))
{
echo $args['view']->partial(
'tracking-code.php',
array('id' => $collection->tracking_id)
);
}
}
}
/**
* Intercept the name of the public theme.
*
* @param string $name The name of the current theme
* @return string The name of the theme
*/
public function filterPublicThemeName($name)
{
if ($this->theme_name === null)
{
$id = $this->getCollectionId();
if ($id !== null)
{
$db = get_db();
$collection = $db->getTable('EnhancedCollection')->find($id);
if ( $collection !== null && ! empty($collection->theme))
{
$this->theme_name = $collection->theme;
$this->loadCommon($this->theme_name);
}
}
if ($this->theme_name === null)
{
$this->theme_name = $name;
}
}
return $this->theme_name;
}
/**
* Check for a collection and append it to the links.
*
* @param array $links The current links
* @return array Navigation links
*/
public function filterPublicNavigationItems($links)
{
$request = Zend_Controller_Front::getInstance()->getRequest();
$id = $request->getParam('collection');
if ($id !== null)
{
for ($i = 0, $len = count($links); $i < $len; $i++)
{
$links[$i]['uri'] = $links[$i]['uri']."?".http_build_query(array('collection' => $id));
}
}
return $links;
}
/**
* The id number of the current collection.
*
* @return int|null The collection id number or null if this isn't a collection page.
*/
protected function getCollectionId()
{
$request = Zend_Controller_Front::getInstance()->getRequest();
$controller = $request->getControllerName();
$action = $request->getActionName();
$id = null;
if ($controller === 'collections' && $action === 'show')
{
$id = $request->getParam('id');
}
else if ($controller === 'items')
{
if (in_array($action, array('browse', 'tags', 'search')) === true)
{
$id = $request->getParam('collection');
}
else if ($action === 'show')
{
$id = $this->getCollectionIdFromItem($request->getParam('id'));
}
}
return $id;
}
/**
* Find the id of the collection that the given item is apart of.
*
* @param int $id The item id
* @return int|null The collection id or null
*/
protected function getCollectionIdFromItem($id)
{
$db = get_db();
$item = $db->getTable('Item')->find($id);
return ($item === null) ? null : $item->collection_id;
}
/**
* Checks for a common.php file and load it up if it exists.
*
* @param string $theme The theme name
*/
private function loadCommon($theme)
{
$file = PUBLIC_THEME_DIR.'/'.$theme.'/common.php';
if (file_exists($file))
{
include_once $file;
}
}
}