forked from ozmeka/plugin-ItemRelations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ItemRelationsPlugin.php
746 lines (668 loc) · 27 KB
/
ItemRelationsPlugin.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
<?php
/**
* Item Relations
* @copyright Copyright 2010-2014 Roy Rosenzweig Center for History and New Media
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
*/
require_once('controllers/ItemAutocompleteController.php');
/**
* Item Relations plugin.
*/
class ItemRelationsPlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array(
'install',
'uninstall',
'upgrade',
'config',
'config_form',
'define_acl',
'initialize',
'after_save_item',
'admin_items_show_sidebar',
'admin_items_search',
'admin_items_batch_edit_form',
'items_batch_edit_custom',
'public_items_show',
'public_head',
'items_browse_sql',
);
/**
* @var array Filters for the plugin.
*/
protected $_filters = array(
'admin_items_form_tabs',
'admin_navigation_main',
'api_resources',
);
public function filterApiResources($apiResources)
{
// For the resource URI: /api/your_resources/[:id]
$apiResources['item_relations'] = array(
// Module associated with your resource.
//'module' => 'ItemRelationsPlugin',
// Controller associated with your resource.
// Type of record associated with your resource.
'record_type' => 'ItemRelationsRelation',
// List of actions available for your resource.
'actions' => array(
'index', // GET request without ID
'get', // GET request with ID
'post', // POST request
'put', // PUT request (ID is required)
'delete', // DELETE request (ID is required)
),
// List of GET parameters available for your index action.
'index_params' => array('subject_item_id', 'object_item_id', 'property_id'),
);
//Added GET only for looking up properties, TODO Post and Put
$apiResources['item_relations_vocabularies'] = array(
// Module associated with your resource.
//'module' => 'ItemRelationsPlugin',
// Controller associated with your resource.
// Type of record associated with your resource.
'record_type' => 'ItemRelationsVocabulary',
// List of actions available for your resource.
'actions' => array(
'index', // GET request without ID
'get', // GET request with ID
'post', // POST request
'put', // PUT request (ID is required)
'delete', // DELETE request (ID is required)
),
// List of GET parameters available for your index action.
'index_params' => array('label', 'id', 'name', 'namespace_uri', 'namespace_prefix'),
);
//Added GET only for looking up properties, TODO Post and Put
$apiResources['item_relations_properties'] = array(
// Module associated with your resource.
//'module' => 'ItemRelationsPlugin',
// Controller associated with your resource.
// Type of record associated with your resource.
'record_type' => 'ItemRelationsProperty',
// List of actions available for your resource.
'actions' => array(
'index', // GET request without ID
'get', // GET request with ID
'post', // POST request
'put', // PUT request (ID is required)
'delete', // DELETE request (ID is required)
),
// List of GET parameters available for your index action.
'index_params' => array('label', 'id', 'vocabulary_id'),
);
/*
//Added GET only
$apiResources['autocomplete_item'] = array(
// Module associated with your resource.
// 'module' => 'ItemRelationsPlugin',
'module' => 'item-relations',
// Controller associated with your resource.
'controller' => 'item-autocomplete',
// Type of record associated with your resource.
// 'record_type' => 'Item',
// List of actions available for your resource.
'actions' => array(
// 'index', // GET request without ID
'get', // GET request with search term
//'post', // POST request
//'put', // PUT request (ID is required)
//'delete', // DELETE request (ID is required)
),
// List of GET parameters available for your index action.
// 'index_params' => array('label', 'id', 'vocabulary_id'),
);
*/
return $apiResources;
}
/**
* @var array Options and their default values.
*/
protected $_options = array(
'item_relations_public_append_to_items_show' => 1,
'item_relations_relation_format' => 'friendly_part'
);
/**
* Install the plugin.
*/
public function hookInstall()
{
// Create tables.
$db = $this->_db;
$sql = "
CREATE TABLE IF NOT EXISTS `$db->ItemRelationsVocabulary` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`description` text,
`namespace_prefix` varchar(100) NOT NULL,
`namespace_uri` varchar(200) DEFAULT NULL,
`custom` BOOLEAN NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$db->query($sql);
$sql = "
CREATE TABLE IF NOT EXISTS `$db->ItemRelationsProperty` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`vocabulary_id` int(10) unsigned NOT NULL,
`local_part` varchar(100) NOT NULL,
`friendly_part` varchar(100) DEFAULT NULL,
`label` varchar(100) DEFAULT NULL,
`description` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$db->query($sql);
$sql = "
CREATE TABLE IF NOT EXISTS `$db->ItemRelationsRelation` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`subject_item_id` int(10) unsigned NOT NULL,
`property_id` int(10) unsigned NOT NULL,
`object_item_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$db->query($sql);
// Install the formal vocabularies and their properties.
$formalVocabularies = include 'formal_vocabularies.php';
foreach ($formalVocabularies as $formalVocabulary) {
$vocabulary = new ItemRelationsVocabulary;
$vocabulary->name = $formalVocabulary['name'];
$vocabulary->description = $formalVocabulary['description'];
$vocabulary->namespace_prefix = $formalVocabulary['namespace_prefix'];
$vocabulary->namespace_uri = $formalVocabulary['namespace_uri'];
$vocabulary->custom = 0;
$vocabulary->save();
$vocabularyId = $vocabulary->id;
foreach ($formalVocabulary['properties'] as $formalProperty) {
$property = new ItemRelationsProperty;
$property->vocabulary_id = $vocabularyId;
$property->local_part = $formalProperty['local_part'];
$property->friendly_part = $formalProperty['friendly_part'];
$property->label = $formalProperty['label'];
$property->description = $formalProperty['description'];
$property->save();
}
}
// Install a custom vocabulary.
$customVocabulary = new ItemRelationsVocabulary;
$customVocabulary->name = 'Custom';
$customVocabulary->description = 'Custom vocabulary containing relations defined for this Omeka instance.';
$customVocabulary->namespace_prefix = ''; // cannot be NULL
$customVocabulary->namespace_uri = null;
$customVocabulary->custom = 1;
$customVocabulary->save();
$this->_installOptions();
}
/**
* Uninstall the plugin.
*/
public function hookUninstall()
{
$db = $this->_db;
// Drop the vocabularies table.
$sql = "DROP TABLE IF EXISTS `$db->ItemRelationsVocabulary`";
$db->query($sql);
// Drop the properties table.
$sql = "DROP TABLE IF EXISTS `$db->ItemRelationsProperty`";
$db->query($sql);
// Drop the relations table.
$sql = "DROP TABLE IF EXISTS `$db->ItemRelationsRelation`";
$db->query($sql);
$this->_uninstallOptions();
}
/**
* Display the plugin configuration form.
*/
public static function hookConfigForm()
{
$publicAppendToItemsShow = get_option('item_relations_public_append_to_items_show');
$relationFormat = get_option('item_relations_relation_format');
require dirname(__FILE__) . '/config_form.php';
}
/**
* Handle the plugin configuration form.
*/
public static function hookConfig()
{
set_option('item_relations_public_append_to_items_show',
(int)(boolean) $_POST['item_relations_public_append_to_items_show']);
set_option('item_relations_relation_format',
$_POST['item_relations_relation_format']);
}
/**
* Upgrade the plugin.
*
* @param array $args
*/
public function hookUpgrade($args)
{
$oldVersion = $args['old_version'];
$db = $this->_db;
if ($oldVersion <= '1.1') {
$sql = "
INSERT INTO `{$db->ItemRelationsProperty}`
(`vocabulary_id`, `local_part`, `label`, `description`)
VALUES
(1, 'abstract', 'Abstract', 'A summary of the resource.'),
(1, 'accessRights', 'Access Rights', 'Information about who can access the resource or an indication of its security status.'),
(1, 'accrualMethod', 'Accrual Method', 'The method by which items are added to a collection.'),
(1, 'accrualPeriodicity', 'Accrual Periodicity', 'The frequency with which items are added to a collection.'),
(1, 'accrualPolicy', 'Accrual Policy', 'The policy governing the addition of items to a collection.'),
(1, 'audience', 'Audience', 'A class of entity for whom the resource is intended or useful.'),
(1, 'contributor', 'Contributor', 'An entity responsible for making contributions to the resource.'),
(1, 'coverage', 'Coverage', 'The spatial or temporal topic of the resource, the spatial applicability of the resource, or the jurisdiction under which the resource is relevant.'),
(1, 'creator', 'Creator', 'An entity primarily responsible for making the resource.'),
(1, 'description', 'Description', 'An account of the resource.'),
(1, 'educationLevel', 'Audience Education Level', 'A class of entity, defined in terms of progression through an educational or training context, for which the described resource is intended.'),
(1, 'extent', 'Extent', 'The size or duration of the resource.'),
(1, 'format', 'Format', 'The file format, physical medium, or dimensions of the resource.'),
(1, 'instructionalMethod', 'Instructional Method', 'A process, used to engender knowledge, attitudes and skills, that the described resource is designed to support.'),
(1, 'language', 'Language', 'A language of the resource.'),
(1, 'license', 'License', 'A legal document giving official permission to do something with the resource.'),
(1, 'mediator', 'Mediator', 'An entity that mediates access to the resource and for whom the resource is intended or useful.'),
(1, 'medium', 'Medium', 'The material or physical carrier of the resource.'),
(1, 'provenance', 'Provenance', 'A statement of any changes in ownership and custody of the resource since its creation that are significant for its authenticity, integrity, and interpretation.'),
(1, 'publisher', 'Publisher', 'An entity responsible for making the resource available.'),
(1, 'rights', 'Rights', 'Information about rights held in and over the resource.'),
(1, 'rightsHolder', 'Rights Holder', 'A person or organization owning or managing rights over the resource.'),
(1, 'spatial', 'Spatial Coverage', 'Spatial characteristics of the resource.'),
(1, 'subject', 'Subject', 'The topic of the resource.'),
(1, 'tableOfContents', 'Table Of Contents', 'A list of subunits of the resource.'),
(1, 'temporal', 'Temporal Coverage', 'Temporal characteristics of the resource.'),
(1, 'type', 'Type', 'The nature or genre of the resource.')";
$db->query($sql);
}
if ($oldVersion <= '2.0') {
// Fix un-upgraded old table name if present.
$correctTableName = (bool) $db->fetchOne("SHOW TABLES LIKE '{$db->ItemRelationsRelation}'");
if (!$correctTableName) {
$sql = "RENAME TABLE `{$db->prefix}item_relations_item_relations` TO `{$db->ItemRelationsRelation}`";
$db->query($sql);
}
}
}
/**
* Add the translations.
*/
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
$db = get_db();
$adapter = $db->getAdapter();
// if (floatval($oldVersion) <= '2.1')
{
$sql = "SHOW COLUMNS FROM `{$db->ItemRelationsProperty}` LIKE 'friendly_part'";
$statement = $adapter->query($sql);
// do we already have a column in the db for friendly_part?
if (! $adapter->query($sql)->fetch())
{
// Nein! add friendly parts to the db.
$this->populateFriendlyParts();
} /*
else
{
// Jawohl! do nothing.
} */
}
}
/**
* Define the ACL.
*
* @param array $args
*/
public function hookDefineAcl($args)
{
$acl = $args['acl'];
//$relationsResource = new Zend_Acl_Resource('ItemRelations_Relations');
$indexResource = new Zend_Acl_Resource('ItemRelations_Index');
$vocabResource = new Zend_Acl_Resource('ItemRelations_Vocabularies');
$acl->add($indexResource);
$acl->add($vocabResource);
$acl->addResource('Relations');
$acl->allow(null, 'Relations');
}
/**
* Display item relations on the public items show page.
*/
public function hookPublicItemsShow() {
if (get_option('item_relations_public_append_to_items_show')) {
$item = get_current_record('item');
echo common('item-relations-show', array(
'subjectRelations' => self::prepareSubjectRelations($item),
'objectRelations' => self::prepareObjectRelations($item)
));
}
}
/**
* Add libraries and inits to page header on render
*/
public function hookPublicHead() {
queue_js("accordion.js");
}
/**
* Display item relations on the admin items show page.
*
* @param Item $item
*/
public function hookAdminItemsShowSidebar($args)
{
$item = $args['item'];
echo common('item-relations-show', array(
'subjectRelations' => self::prepareSubjectRelations($item),
'objectRelations' => self::prepareObjectRelations($item)
));
}
/**
* Display the item relations form on the admin advanced search page.
*/
public function hookAdminItemsSearch()
{
echo common('item-relations-advanced-search', array(
'formSelectProperties' => get_table_options('ItemRelationsProperty'))
);
}
/**
* Save the item relations after saving an item add/edit form.
*
* @param array $args
*/
public function hookAfterSaveItem($args)
{
if (!$args['post']) {
return;
}
$record = $args['record'];
$post = $args['post'];
$db = $this->_db;
// Save item relations.
if (isset($post['item_relations_property_id'])) {
foreach ($post['item_relations_property_id'] as $key => $propertyId) {
self::insertItemRelation(
$record,
$propertyId,
$post['item_relations_item_relation_object_item_id'][$key]
);
}
}
// Delete item relations.
if (isset($post['item_relations_item_relation_delete'])) {
foreach ($post['item_relations_item_relation_delete'] as $itemRelationId) {
$itemRelation = $db->getTable('ItemRelationsRelation')->find($itemRelationId);
// When an item is related to itself, deleting both relations
// simultaneously will result in an error. Prevent this by
// checking if the item relation exists prior to deletion.
if ($itemRelation) {
$itemRelation->delete();
}
}
}
}
/**
* Filter for an item relation after search page submission.
*
* @param array $args
*/
public function hookItemsBrowseSql($args)
{
$select = $args['select'];
$params = $args['params'];
if (isset($params['item_relations_property_id'])
&& is_numeric($params['item_relations_property_id'])
) {
$db = $this->_db;
// Set the field on which to join.
if (isset($params['item_relations_clause_part'])
&& $params['item_relations_clause_part'] == 'object'
) {
$onField = 'object_item_id';
} else {
$onField = 'subject_item_id';
}
$select
->join(
array('item_relations_relations' => $db->ItemRelationsRelation),
"item_relations_relations.$onField = items.id",
array()
)
->where('item_relations_relations.property_id = ?',
$params['item_relations_property_id']
);
}
}
/**
* Add custom fields to the item batch edit form.
*/
public function hookAdminItemsBatchEditForm()
{
$formSelectProperties = get_table_options('ItemRelationsProperty');
?>
<fieldset id="item-relation-fields">
<h2><?php echo __('Item Relations'); ?></h2>
<table>
<thead>
<tr>
<th><?php echo __('Subjects'); ?></th>
<th><?php echo __('Relation'); ?></th>
<th><?php echo __('Object'); ?></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo __('These Items'); ?></td>
<td><?php echo get_view()->formSelect('custom[item_relations_property_id]', null, array(), $formSelectProperties); ?></td>
<td>
<?php echo __('Item ID'); ?>
<?php echo get_view()->formText('custom[item_relations_item_relation_object_item_id]', null, array('size' => 6)); ?>
</td>
</tr>
</tbody>
</table>
</fieldset>
<?php
}
/**
* Process the item batch edit form.
*
* @param array $args
*/
public function hookItemsBatchEditCustom($args)
{
$item = $args['item'];
$custom = $args['custom'];
self::insertItemRelation(
$item,
$custom['item_relations_property_id'],
$custom['item_relations_item_relation_object_item_id']
);
}
/**
* Add the Item Relations link to the admin main navigation.
*
* @param array Navigation array.
* @return array Filtered navigation array.
*/
public function filterAdminNavigationMain($nav)
{
$nav[] = array(
'label' => __('Item Relations'),
'uri' => url('item-relations'),
'resource' => 'ItemRelations_Index',
'privilege' => 'index'
);
return $nav;
}
/**
* Add the "Item Relations" tab to the admin items add/edit page.
*
* @return array
*/
public function filterAdminItemsFormTabs($tabs, $args)
{
$item = $args['item'];
$formSelectProperties = get_table_options('ItemRelationsProperty');
$subjectRelations = self::prepareSubjectRelations($item);
$objectRelations = self::prepareObjectRelations($item);
ob_start();
include 'item_relations_form.php';
$content = ob_get_contents();
ob_end_clean();
$tabs['Item Relations'] = $content;
return $tabs;
}
/**
* Prepare subject item relations for display.
*
* @param Item $item
* @return array
*/
public static function prepareSubjectRelations(Item $item)
{
$subjects = get_db()->getTable('ItemRelationsRelation')->findBySubjectItemId($item->id);
$subjectRelations = array();
foreach ($subjects as $subject) {
if (!($item = get_record_by_id('item', $subject->object_item_id))) {
continue;
}
$subjectRelations[] = array(
'item_relation_id' => $subject->id,
'object_item_id' => $subject->object_item_id,
'object_item_title' => self::getItemTitle($item),
'relation_text' => $subject->getPropertyText(),
'relation_description' => $subject->property_description
);
}
return $subjectRelations;
}
/**
* Prepare object item relations for display.
*
* @param Item $item
* @return array
*/
public static function prepareObjectRelations(Item $item)
{
$objects = get_db()->getTable('ItemRelationsRelation')->findByObjectItemId($item->id);
$objectRelations = array();
foreach ($objects as $object) {
if (!($item = get_record_by_id('item', $object->subject_item_id))) {
continue;
}
$objectRelations[] = array(
'item_relation_id' => $object->id,
'subject_item_id' => $object->subject_item_id,
'subject_item_title' => self::getItemTitle($item),
'relation_text' => $object->getPropertyText(),
'relation_description' => $object->property_description
);
}
return $objectRelations;
}
/**
* Return a item's title.
*
* @param Item $item The item.
* @return string
*/
public static function getItemTitle($item)
{
$title = metadata($item, array('Dublin Core', 'Title'), array('no_filter' => true));
if (!trim($title)) {
$title = '#' . $item->id;
}
return $title;
}
/**
* Insert an item relation.
*
* @param Item|int $subjectItem
* @param int $propertyId
* @param Item|int $objectItem
* @return bool True: success; false: unsuccessful
*/
public static function insertItemRelation($subjectItem, $propertyId, $objectItem)
{
// Only numeric property IDs are valid.
if (!is_numeric($propertyId)) {
return false;
}
// Set the subject item.
if (!($subjectItem instanceOf Item)) {
$subjectItem = get_db()->getTable('Item')->find($subjectItem);
}
// Set the object item.
if (!($objectItem instanceOf Item)) {
$objectItem = get_db()->getTable('Item')->find($objectItem);
}
// Don't save the relation if the subject or object items don't exist.
if (!$subjectItem || !$objectItem) {
return false;
}
$itemRelation = new ItemRelationsRelation;
$itemRelation->subject_item_id = $subjectItem->id;
$itemRelation->property_id = $propertyId;
$itemRelation->object_item_id = $objectItem->id;
$itemRelation->save();
return true;
}
public function populateFriendlyParts()
{
$db = get_db();
$adapter = $db->getAdapter();
$sql = "ALTER TABLE `{$db->ItemRelationsProperty}` ADD friendly_part VARCHAR(100) AFTER local_part";
$adapter->query($sql);
// build a list of vocabs we have in the db
$vocabsResult = $db->getTable('ItemRelationsVocabulary')->findAll();
$dbVocabs = array(); // put the vocabs from the db into a handily-keyed format
foreach($vocabsResult as $row)
{
$dbVocabs[$row->namespace_prefix] = $row;
}
// get vocabs we have template for in PHP include
$templateFormalVocabularies = include 'formal_vocabularies.php';
$templateVocabularyNamespacesPresent = array(); // hold a list of templated vocabs
foreach ($templateFormalVocabularies as $templateFormalVocabulary)
{
$templateVocabularyNamespacesPresent[] = $templateFormalVocabulary['namespace_prefix'];
// create vocab in db if not present
if (! in_array($templateFormalVocabulary['namespace_prefix'], array_keys($dbVocabs)))
{
$vocabulary = new ItemRelationsVocabulary;
$vocabulary->name = $templateFormalVocabulary['name'];
$vocabulary->description = $templateFormalVocabulary['description'];
$vocabulary->namespace_prefix = $templateFormalVocabulary['namespace_prefix'];
$vocabulary->namespace_uri = $templateFormalVocabulary['namespace_uri'];
$vocabulary->custom = 0;
$vocabulary->save();
$vocabularyId = $vocabulary->id;
}
}
// should have all template vocabs in the db now, fetch them again
$vocabsResult = $db->getTable('ItemRelationsVocabulary')->findAll();
foreach($templateFormalVocabularies as $templateVocab)
{
// does this db vocab exist in the template? if not, skip it
if (empty($dbVocabs[$templateVocab['namespace_prefix']]))
{
continue;
}
// get this db vocab's properties from the db
$propertiesResult = $db->getTable('ItemRelationsProperty')->findByVocabularyId($dbVocabs[$templateVocab['namespace_prefix']]['id']);
// put them into a handily-keyed array so we can compare them with the template ones
$dbVocabProperties = array();
foreach ($propertiesResult as $row)
{
$dbVocabProperties[$row['local_part']] = $row;
}
foreach ($templateVocab['properties'] as $templateVocabProperty)
{
if ($dbVocabProperties[$templateVocabProperty['local_part']]->friendly_part != $templateVocabProperty['friendly_part'])
{
$property = $dbVocabProperties[$templateVocabProperty['local_part']];
$property->friendly_part = $templateVocabProperty['friendly_part'];
$property->save();
}
}
}
}
}