Repository navigation
Expand file tree
/
Copy pathItemDuplicateCheckPlugin.php
More file actions
129 lines (110 loc) · 3.73 KB
/
Copy pathItemDuplicateCheckPlugin.php
File metadata and controls
129 lines (110 loc) · 3.73 KB
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
<?php
class ItemDuplicateCheckPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array(
'install',
'uninstall',
'initialize',
'define_acl',
'admin_head',
);
protected $_filters = array(
'admin_navigation_main',
);
public function hookInstall()
{
$db = $this->_db;
$sql = "
CREATE TABLE IF NOT EXISTS {$db->ItemDuplicateCheckRule} (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
item_type_id int(10) unsigned NULL DEFAULT NULL,
element_ids text NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (item_type_id) REFERENCES {$db->ItemType} (id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
";
$db->query($sql);
}
public function hookUninstall()
{
$db = $this->_db;
$db->query("DROP TABLE IF EXISTS {$db->ItemDuplicateCheckRule}");
}
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
}
public function hookDefineAcl($args)
{
// Restrict menu access to super and admin users
$args['acl']->addResource('ItemDuplicateCheck_Rules');
}
public function hookAdminHead()
{
queue_js_file('item_duplicate_check');
queue_js_string('Omeka.WEB_DIR = ' . js_escape(WEB_DIR) . ';');
}
public function filterAdminNavigationMain($nav)
{
$nav[] = array(
'label' => __('Item Duplicate Check'),
'uri' => url('item-duplicate-check/rules/list'),
'resource' => 'ItemDuplicateCheck_Rules'
);
return $nav;
}
}
function item_duplicate_check_get_duplicates($item)
{
$db = get_db();
$rules = $db->getTable('ItemDuplicateCheckRule')->findAll();
$duplicates = array();
foreach ($rules as $rule) {
if ($rule->item_type_id && $item->item_type_id != $rule->item_type_id) {
continue;
}
$elements = $rule->getElements();
if (empty($elements)) {
continue;
}
$select = $db
->select()
->from(array('i' => $db->Item), array('item_id' => 'id'));
$joins_added = array();
foreach ($elements as $element) {
$element_id = $element->id;
foreach ($item['Elements'][$element_id] as $value) {
$text = $value['text'];
if (0 != strlen(trim($text)) && function_exists('element_types_format')) {
$text = element_types_format($element_id, $text);
}
if (!isset($joins_added[$element_id])) {
$select->joinLeft(
array("et_$element_id" => $db->ElementText),
"i.id = et_{$element_id}.record_id AND et_{$element_id}.record_type = 'Item' AND et_{$element_id}.element_id = {$element_id}"
);
$joins_added[$element_id] = true;
}
$select->where("et_{$element_id}.text = ?", $text);
}
}
$where = $select->getPart(Zend_Db_Select::WHERE);
if (empty($where)) {
# This will just return the whole items table, abort
continue;
}
if (isset($item->id)) {
$select->where("i.id != ?", $item->id);
}
$select->limit(10);
$item_ids = $db->fetchCol($select);
foreach ($item_ids as $item_id) {
$duplicates[] = array(
'item' => $db->getTable('Item')->find($item_id),
'rule' => $rule,
);
}
}
return $duplicates;
}