Skip to content

Commit

Permalink
Added support for Template Restriction settings
Browse files Browse the repository at this point in the history
The SDK now supports all restrictions functions.

* Get template restrictions
* Update template restrictions
  • Loading branch information
kevin39 committed Apr 30, 2014
1 parent f314bf5 commit d00a3bc
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
16 changes: 16 additions & 0 deletions examples/getTemplateRestrictions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
require_once('../src/PassSlot.php');

$appKey ='<YOUR APP KEY>';
$templateId = '<TEMPLATE ID>';

try {
$engine = PassSlot::start($appKey);
$pass = $engine->saveTemplateRestrictions($templateId);

var_dump($pass);
} catch (PassSlotApiException $e) {
echo "Something went wrong:\n";
echo $e;
}

16 changes: 16 additions & 0 deletions examples/saveTemplateRestrictions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
require_once('../src/PassSlot.php');

$appKey ='<YOUR APP KEY>';
$templateId = '<TEMPLATE ID>';

try {
$engine = PassSlot::start($appKey);
$pass = $engine->saveTemplateRestrictions($templateId);

var_dump($pass);
} catch (PassSlotApiException $e) {
echo "Something went wrong:\n";
echo $e;
}

72 changes: 71 additions & 1 deletion src/PassSlot.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ public function deleteTemplateImages($templateId, $type="", $resolution="")
* Create or update the image with the given type and resolution of
* a pass template
*
* @param object $templateId Template Identifier
* @param int $templateId Template Identifier
* @param string $type Image type
* @param string $resolution Image resolution
* @param string $image Image file name
Expand Down Expand Up @@ -505,6 +505,56 @@ public function saveTemplateImage($templateId, $type, $resolution, $image)
return $this -> _restCall('POST', $resource, $content, true);
}

/**
* Updates the pass template distributions restrictions
*
* @param int $templateId Template identifier
* @param int $quantityRestriction Quantity restriction
* @param int $redemptionRestriction Redemption restriction
* @param string $passwordProtection Password restriction
* @param string $dateRestriction Date (ISO 8601 format required)
* @param bool $sharingRestriction Sharing restriction
*/
public function saveTemplateRestrictions($templateId, $quantityRestriction=null, $redemptionRestriction=null, $passwordProtection=null, $dateRestriction=null, $sharingRestriction=false)
{
if($quantityRestriction != null && !is_numeric($quantityRestriction))
{
user_error('Invalid value for $quantityRestriction', E_USER_ERROR);
}
if($redemptionRestriction != null && !is_numeric($redemptionRestriction))
{
user_error('Invalid value for $redemptionRestriction', E_USER_ERROR);
}
if(!is_bool($sharingRestriction))
{
user_error('Invalid value for $sharingRestriction', E_USER_ERROR);
}
if($dateRestriction != null && !$this->_validateDate($dateRestriction))
{
user_error('Invalid value for $dateRestriction, must use an ISO 8601 string.', E_USER_ERROR);
}
$resource = sprintf("/templates/%u/restrictions", $templateId);
$content = json_encode(array(
"quantityRestriction" => $quantityRestriction,
"redemptionRestriction" => $redemptionRestriction,
"passwordProtection" => $passwordProtection,
"dateRestriction" => $dateRestriction,
"sharingRestriction" => $sharingRestriction
));
return $this -> _restCall('PUT', $resource, $content, true);
}

/**
* Returns the pass template distributions restrictions
*
* @param int $templateId Template identifier
*/
public function getTemplateRestrictions($templateId)
{
$resource = sprintf("/templates/%u/restrictions", $templateId);
return $this -> _restCall('GET', $resource);
}

/**
* Prepares the values and image for the pass and creates it
*
Expand Down Expand Up @@ -630,6 +680,26 @@ private function _restCall($httpMethod, $resource, $content = null, $multipart =

return $response;
}

/**
* Check if a date match the ISO 8601 specs
*
* @param string $date
* @return boolean
*/
private function _validateDate($date)
{
if (preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/', $date, $parts) == true) {
$time = gmmktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]);

$input_time = strtotime($date);
if ($input_time === false) return false;

return $input_time == $time;
} else {
return false;
}
}

}

Expand Down

0 comments on commit d00a3bc

Please sign in to comment.