-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMOASNiceUrlsPlugin.php
111 lines (97 loc) · 3.34 KB
/
MOASNiceUrlsPlugin.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
<?php
/**
* @package omeka
* @subpackage moas-niceurls
* @copyright 2016 University of Nottingham
* @license MIT
* @author James Hodgson <[email protected]>
*/
class MOASNiceUrlsPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array(
'admin_head',
'before_save_item',
'before_save_record',
'define_routes',
'initialize',
'uninstall_message'
);
public function hookAdminHead()
{
queue_js_file('moas-niceurls');
queue_css_file('moas-niceurls');
}
public function hookBeforeSaveItem($args)
{
$this->validateSlugs($args['post']['Elements'], $args['record'], 'Item');
}
public function hookBeforeSaveCollection($args)
{
$this->validateSlugs($args['post']['Elements'], $args['record'], 'Collection');
}
public function hookBeforeSaveRecord($args)
{
if ($args['record'] instanceof ElementText && $args['record']->element_id == MOASNiceUrls_Helpers_Slugs::getSlugElementID()) {
$this->modifySlug($args['record']);
}
}
public function hookDefineRoutes($args)
{
if (is_admin_theme()) {
$args['router']->addConfig(new Zend_Config_Ini(
__DIR__ . '/routes.ini'
));
} else {
$router = $args['router'];
foreach (MOASNiceUrls_Helpers_Slugs::fetchSlugs() as $slug) {
$router->addRoute(
'moas_nice_urls_redirect_' . $slug['slug'],
new Zend_Controller_Router_Route(
$slug['slug'],
array(
'module' => 'moas-nice-urls',
'controller' => 'redirect',
'action' => 'redirect',
'id' => $slug['id']
)
)
);
}
}
}
public function hookInitialize()
{
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new MOASNiceUrls_Controller_Plugin_SlugGenerateFilter());
}
/**
* Display the uninstall message.
*/
public function hookUninstallMessage()
{
echo __('%sWarning%s: This will cause all nice urls to result in 404 errors.%s'
, '<p><strong>', '</strong>', '</p>');
}
private function validateSlugs($elements, $record, $recordType)
{
$slugElement = MOASNiceUrls_Helpers_Slugs::getSlugElementID();
$slugs = $elements[$slugElement];
$currentSlugs = is_null($record->id) ? array() : MOASNiceUrls_Helpers_Slugs::getRecordsSlugs($record->id, $recordType);
$filteredSlugs = array_filter($slugs, array(new MOASNiceUrls_Filters_ExistingSlugs($currentSlugs), 'filter'));
$errors = [];
foreach ($filteredSlugs as $slug) {
$slugError = MOASNiceUrls_Helpers_Slugs::validate($slug['text']);
if (!empty($slugError)) {
$errors[$slug['text']] = $slugError[0];
}
}
foreach ($errors as $slug => $error) {
$record->addError("Slug - '" . $slug . "'", $error);
}
}
private function modifySlug($record)
{
/** @var ElementText $record */
$record->setText(strtolower($record->getText()));
}
}