Skip to content

Commit

Permalink
Initial first couple of API methods /api/item_relations returns ItemR…
Browse files Browse the repository at this point in the history
…elations and POST works (without complete validation of records)
  • Loading branch information
root committed Oct 6, 2014
1 parent 3bd35a7 commit 3eaf7c0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
48 changes: 47 additions & 1 deletion ItemRelationsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,52 @@ class ItemRelationsPlugin extends Omeka_Plugin_AbstractPlugin
protected $_filters = array(
'admin_items_form_tabs',
'admin_navigation_main',
'api_extend_items',
'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('id', 'subject_id', 'object_id', 'propery_id'),
);
return $apiResources;
}
public function filterApiExtendItems($extend, $args)
{
$item = $args['record'];
// For one resource:
$resourceId = array("id" => 21); //$this->_db->getTable('YourResource')->findByItemId($item->id);
$extend['your_resources'] = array(
'id' => 1,
'url' => Omeka_Record_Api_AbstractRecordAdapter::getResourceUrl("/item_relations/{$resourceId->id}"),
'resource' => 'your_resources',
);

/* Or, for multiple resources:
$extend['your_resources'] = array(
'count' => 10,
'url' => Omeka_Record_Api_AbstractRecordAdapter::getResourceUrl("/your_resources?item={$item->id}"),
'resource' => 'your_resources',
);*/
echo var_dump($extend);
echo "<br>";
return $extend;
}
/**
* @var array Options and their default values.
*/
Expand Down Expand Up @@ -236,11 +280,13 @@ public function hookInitialize()
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');
}

/**
Expand Down
30 changes: 30 additions & 0 deletions models/Api/ItemRelationsRelation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php


class Api_ItemRelationsRelation extends Omeka_Record_Api_AbstractRecordAdapter
{
// Get the REST representation of a record.
public function getRepresentation(Omeka_Record_AbstractRecord $record)
{
return $record;
// Return a PHP array, representing the passed record.
}

// Set data to a record during a POST request.
public function setPostData(Omeka_Record_AbstractRecord $record, $data)
{
//echo "data";
//echo var_dump($data);
//echo $data -> subject_item_id;
// Set properties directly to a new record.a
$record->subject_item_id = $data->subject_item_id;
$record->object_item_id = $data->object_item_id;
$record->property_id = $data->property_id;
}

// Set data to a record during a PUT request.
public function setPutData(Omeka_Record_AbstractRecord $record, $data)
{
// Set properties directly to an existing record.
}
}
24 changes: 22 additions & 2 deletions models/ItemRelationsRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Item Relations Relation model.
*/
class ItemRelationsRelation extends Omeka_Record_AbstractRecord
class ItemRelationsRelation extends Omeka_Record_AbstractRecord implements Zend_Acl_Resource_Interface
{
/**
* @var int
Expand All @@ -29,6 +29,17 @@ class ItemRelationsRelation extends Omeka_Record_AbstractRecord
* @var int
*/
public $object_item_id;
protected function _validate()
{
//echo "Validating";
if (empty($this->subject_item_id)) {
$this->addError('subject_item_id', __('Relation requires subject item ID.'));
}
// An item must exist.
if (!$this->getTable('Item')->exists($this->subject_item_id)) {
$this->addError('subject_item_id', __('Relation requires a valid subject item ID.'));
}
}

/**
* Get a textual representation of the property for this relation.
Expand All @@ -42,7 +53,16 @@ public function getPropertyText()
$property->local_part = $this->property_local_part;
$property->label = $this->property_label;
$property->vocabulary_namespace_prefix = $this->vocabulary_namespace_prefix;

return $property->getText();
}
/**
/* Identify ItemRelationsRelation records as relating to the ItemRelationsRelations ACL resource.
*
* @return string
*/
public function getResourceId()
{
return 'Relations';
}

}

0 comments on commit 3eaf7c0

Please sign in to comment.