-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBatchUploadPlugin.php
More file actions
208 lines (196 loc) · 6.75 KB
/
BatchUploadPlugin.php
File metadata and controls
208 lines (196 loc) · 6.75 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
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
<?php
/**
* The main plugin class.
*
* @package BatchUpload
*/
class BatchUploadPlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* List of hooks used by this plugin.
*
* @var array
*/
protected $_hooks = array(
'install',
'uninstall',
'upgrade',
'initialize',
'define_routes',
'after_save_batch_upload_job',
);
/**
* List of filters used by this plugin.
*
* @var array
*/
protected $_filters = array(
'admin_navigation_main',
'batch_upload_register_job_type',
);
protected $_supported_job_types = array(
'new_collection',
'existing_collection',
'individual_items',
);
/**
* HOOK: Installing the plugin.
*/
public function hookInstall()
{
$db = get_db();
$prefix = $db->prefix;
$db->query("CREATE TABLE IF NOT EXISTS `{$prefix}batch_upload_jobs` (
`id` int(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`step` int(10) NOT NULL DEFAULT 1,
`job_type` varchar(128) NOT NULL,
`target_type` varchar(64),
`target_id` int(10),
`data` LONGTEXT,
`owner_id` int(10) UNSIGNED NOT NULL,
`finished` timestamp NULL DEFAULT NULL,
`added` timestamp NOT NULL DEFAULT '2000-01-01 05:00:00',
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (`owner_id`) REFERENCES `{$prefix}users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
$db->query("CREATE TABLE IF NOT EXISTS `{$prefix}batch_upload_rows` (
`id` int(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`job_id` int(10) NOT NULL,
`order` int(10) NOT NULL,
`data` LONGTEXT,
FOREIGN KEY (`job_id`) REFERENCES `{$prefix}batch_upload_jobs`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
$db->query("CREATE TABLE IF NOT EXISTS `{$prefix}batch_upload_mapping_sets` (
`id` int(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(64) NOT NULL UNIQUE,
`job_id` int(10) UNIQUE,
`owner_id` int(10) UNSIGNED NOT NULL,
`added` timestamp NOT NULL DEFAULT '2000-01-01 05:00:00',
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (`job_id`) REFERENCES `{$prefix}batch_upload_jobs`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`owner_id`) REFERENCES `{$prefix}users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
$db->query("CREATE TABLE IF NOT EXISTS `{$prefix}batch_upload_mappings` (
`id` int(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`header` varchar(64) NOT NULL,
`order` int(10) NOT NULL,
`property` int(10) NOT NULL,
`html` tinyint(4) NOT NULL DEFAULT 0,
`mapping_set_id` int(10),
FOREIGN KEY (`mapping_set_id`) REFERENCES `{$prefix}batch_upload_mapping_sets`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
}
/**
* HOOK: Uninstalling the plugin.
*/
public function hookUninstall()
{
$db = get_db();
$prefix = $db->prefix;
$db->query("DROP TABLE IF EXISTS `{$prefix}batch_upload_rows`;");
$db->query("DROP TABLE IF EXISTS `{$prefix}batch_upload_mappings`;");
$db->query("DROP TABLE IF EXISTS `{$prefix}batch_upload_mapping_sets`;");
$db->query("DROP TABLE IF EXISTS `{$prefix}batch_upload_jobs`;");
}
/**
* HOOK: Upgrading the plugin.
*
* Run migrations in version ascending order, starting from the last unrun migration.
*/
public function hookUpgrade($args)
{
$oldVersion = $args['old_version'];
$newVersion = $args['new_version'];
$doMigrate = false;
$versions = array();
foreach (glob(dirname(__FILE__) . '/libraries/BatchUpload/Migration/*.php') as $migrationFile)
{
$className = 'BatchUpload_Migration_' . basename($migrationFile, '.php');
include $migrationFile;
$versions[$className::$version] = new $className();
}
uksort($versions, 'version_compare');
foreach ($versions as $version => $migration)
{
if (version_compare($version, $oldVersion, '>'))
{
$doMigrate = true;
}
if ($doMigrate)
{
$migration->up();
if (version_compare($version, $newVersion, '>'))
{
break;
}
}
}
}
/**
* HOOK: Initialization. Add wizard integrations.
*/
public function hookInitialize()
{
foreach ($this->_supported_job_types as $supported_job_type)
{
$klass = "BatchUpload_Wizard_" . Inflector::camelize($supported_job_type);
$wizard = new $klass();
$wizard->integrate();
}
}
/**
* HOOK: Defining routes.
*
* @param array $args
*/
public function hookDefineRoutes($args)
{
$args['router']->addConfig(new Zend_Config_Ini(dirname(__FILE__) . '/routes.ini', 'routes'));
}
/**
* HOOK: After batch upload job is saved.
* Run the "new job" hook to initialize data in the job.
*
* @param array $args
*/
public function hookAfterSaveBatchUploadJob($args)
{
if ($args['insert'])
{
$batch_upload_job = $args['record'];
fire_plugin_hook('batch_upload_' . Inflector::underscore($batch_upload_job->job_type) . '_job_new', array('job' => $batch_upload_job));
$batch_upload_job->save();
}
}
/**
* FILTER: Add entry to admin navigation menu.
*
* @param array $nav
* @return array
*/
public function filterAdminNavigationMain($nav)
{
$nav[] = array(
'label' => __('Batch Upload'),
'uri' => url(array(), 'batchupload_root'),
);
return $nav;
}
/**
* FILTER: Register job types available to this plugin.
*
* @param array $jobTypes
* @return array
*/
public function filterBatchUploadRegisterJobType($jobTypes)
{
foreach ($this->_supported_job_types as $supported_job_type)
{
$klass = "BatchUpload_Wizard_" . Inflector::camelize($supported_job_type);
$wizard = new $klass();
$jobTypes = $wizard->addType($jobTypes);
}
return $jobTypes;
}
}