Skip to content

Commit 12f12a5

Browse files
committed
set up adapters for importing
1 parent ed2a228 commit 12f12a5

4 files changed

Lines changed: 127 additions & 2 deletions

File tree

UserProfilesPlugin.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class UserProfilesPlugin extends Omeka_Plugin_AbstractPlugin
66
{
77

88
protected $_hooks = array(
9-
'install',
9+
'install',
1010
'uninstall',
1111
'define_acl',
1212
'config',
@@ -22,7 +22,8 @@ class UserProfilesPlugin extends Omeka_Plugin_AbstractPlugin
2222
protected $_filters = array(
2323
'admin_navigation_main',
2424
'search_record_types',
25-
'api_resources'
25+
'api_resources',
26+
'api_import_omeka_adapters'
2627
);
2728

2829
protected $_options = null;
@@ -190,6 +191,32 @@ public function filterApiResources($resources, $arg)
190191
return $resources;
191192
}
192193

194+
public function filterApiImportOmekaAdapters($adapters, $args)
195+
{
196+
// Sequence is important here. Need the types and their elements mapped in first
197+
// Then the profiles themselves, then bring in the multi-valued elements then their values
198+
$typesAdapter = new ApiImport_ResponseAdapter_Omeka_GenericAdapter(null, $args['endpointUri'], 'UserProfilesType');
199+
$typesAdapter->setResourceProperties(array('element_set' => 'ElementSet'));
200+
$adapters['user_profiles_types'] = $typesAdapter;
201+
202+
$elementAdapter = new ApiImport_ResponseAdapter_Omeka_GenericAdapter(null, $args['endpointUri'], 'UserProfilesMultiElement)');
203+
$elementAdapter->setResourceProperties(array('element_set' => 'ElementSet'));
204+
$adapters['user_profiles_multielements'] = $elementAdapter;
205+
206+
$profileAdapter = new ApiImport_ResponseAdapter_Omeka_UserProfilesProfile(null, $args['endpointUri'], 'UserProfilesProfile');
207+
$adapters['user_profiles'] = $profileAdapter;
208+
209+
$valueAdapter = new ApiImport_ResponseAdapter_Omeka_GenericAdapter(null, $args['endpointUri'], 'UserProfilesMultiValue');
210+
$valueAdapter->setResourceProperties(array(
211+
'profile_type' => 'UserProfilesType',
212+
'multi' => 'UserProfilesMultiValue',
213+
'profile' => 'UserProfilesProfile'
214+
));
215+
$adapters['user_profiles_multivalues'] = $valueAdapter;
216+
217+
return $adapters;
218+
}
219+
193220
public function hookPublicItemsShow($args)
194221
{
195222
if(get_option('user_profiles_link_to_owner')) {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
class ApiImport_ResponseAdapter_Omeka_UserProfilesProfile extends ApiImport_ResponseAdapter_GenericAdapter
4+
{
5+
6+
protected $resourceProperties = array('type' => 'UserProfilesType');
7+
8+
protected $skipProperties = array('element_texts');
9+
10+
protected $userProperties = array('owner');
11+
12+
public function import()
13+
{
14+
if(! $this->record) {
15+
$this->record = new $this->recordType;
16+
}
17+
$this->setFromResponseData();
18+
$elementTexts = $this->elementTexts();
19+
$this->record->addElementTextsByArray($elementTexts);
20+
try {
21+
$this->record->save(true);
22+
$this->addOmekaApiImportRecordIdMap();
23+
} catch (Exception $e) {
24+
_log($e);
25+
}
26+
return $this->record;
27+
}
28+
29+
30+
/**
31+
* Process the element text data
32+
* @param array $responseData
33+
*/
34+
protected function elementTexts($responseData = null)
35+
{
36+
$elementTexts = array();
37+
if(!$responseData) {
38+
$responseData = $this->responseData;
39+
}
40+
41+
foreach($responseData['element_texts'] as $elTextData) {
42+
$elName = $elTextData['element']['name'];
43+
$elSet = $elTextData['element_set']['name'];
44+
$elTextInsertArray = array('text' => $elTextData['text'],
45+
'html' => $elTextData['html']
46+
);
47+
if (is_null($elTextInsertArray['text'])) {
48+
$elTextInsertArray['text'] = '';
49+
}
50+
$elementTexts[$elSet][$elName][] = $elTextInsertArray;
51+
52+
}
53+
return $elementTexts;
54+
}
55+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
class ApiImport_ResponseAdapter_Omeka_UserProfilesType extends ApiImport_ResponseAdapter_GenericAdapter
3+
{
4+
protected $resourceProperties = array('element_sets' => 'ElementSet');
5+
6+
public function import()
7+
{
8+
if(! $this->record) {
9+
$this->record = new $this->recordType;
10+
}
11+
$this->setFromResponseData();
12+
13+
// Rewrite required_element_ids and required_multielement_ids to
14+
// make sure they use the local ids.
15+
16+
$requiredElementIds = json_decode($this->record->required_element_ids, true);
17+
$localRequiredElementIds = array();
18+
foreach ($requiredElementIds as $id) {
19+
// data for getLocalResourceId is an array
20+
$data = array('id' => $id);
21+
$localRequiredElementIds[] = $this->getLocalResourceId($data, 'Element');
22+
}
23+
$this->record->required_element_ids = json_encode($localRequiredElementIds);
24+
25+
$requiredMultiElementIds = json_decode($this->record->require_multielement_ids, true);
26+
$localRequiredMultiElementIds = array();
27+
foreach ($requiredMultiElementIds as $id) {
28+
// data for getLocalResourceId is an array
29+
$data = array('id' => $id);
30+
$localRequiredMultiElementIds[] = $this->getLocalResourceId($data, 'UserProfilesMultiElement');
31+
}
32+
$this->record->required_multielement_ids = json_encode($localRequiredMultiElementIds);
33+
34+
try {
35+
$this->record->save(true);
36+
$this->addOmekaApiImportRecordIdMap();
37+
} catch (Exception $e) {
38+
_log($e);
39+
}
40+
return $this->record;
41+
}
42+
}

models/Api/UserProfilesProfile.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public function getRepresentation(Omeka_Record_AbstractRecord $profile)
66
{
77
$representation = array(
88
'id' => $profile->id,
9+
'url' => self::getResourceUrl("/user_profiles/{$profile->id}"),
910
'added' => self::getDate($profile->added),
1011
'modified' => self::getDate($profile->modified),
1112
'public' => (bool) $profile->public

0 commit comments

Comments
 (0)