Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #159 from compucorp/develop
Browse files Browse the repository at this point in the history
HC-58: Fix Problems Scheduling Unlimited Resources
  • Loading branch information
MiyaNoctem authored Nov 28, 2017
2 parents efdd03f + 198f945 commit 685328c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
66 changes: 66 additions & 0 deletions CRM/Booking/APIWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* API Wrapper that pre processes and post processes API Calls.
*/
class CRM_Booking_APIWrapper implements API_Wrapper {
/**
* Alter the parameters of the api request.
*
* @param array $apiRequest
*
* @return array
*/
public function fromApiInput($apiRequest) {
$this->fixParametersArray($apiRequest['params']);

return $apiRequest;
}

/**
* alter the result before returning it to the caller.
*
* @param array $apiRequest
* @param array $result
*
* @return array
*/
public function toApiOutput($apiRequest, $result) {
return $result;
}

/**
* Fixes parameters array so that if chained API calls are made, any chained
* fields with '$value.' are moved to the end of the array.
*
* @param array $params
*/
private function fixParametersArray(&$params) {
$chainedValues = array();

foreach ($params as $parameter => &$value) {
if (stripos($parameter, 'api.') === 0 && is_array($value)) {
$allChainedValues = TRUE;

foreach ($value as $chainedParameter => $chainedValue) {
if (stripos($chainedValue, '$value.') !== 0) {
$allChainedValues = FALSE;
}
}

if ($allChainedValues) {
$value['sequential'] = 0;
}

$this->fixParametersArray($value);
}
elseif (stripos($value, '$value.') === 0) {
unset($params[$parameter]);
$chainedValues[$parameter] = $value;
}
}

$params = array_merge($params, $chainedValues);
}

}
12 changes: 12 additions & 0 deletions booking.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,15 @@ function booking_civicrm_alterAPIPermissions($entity, $action, &$params, &$permi
}

}

/**
* Implements hook_civicrm_apiWrappers()
*
* @param array $wrappers
* @param array $apiRequest
*/
function booking_civicrm_apiWrappers(&$wrappers, $apiRequest) {
if ($apiRequest['entity'] == 'Resource' && $apiRequest['action'] == 'get') {
$wrappers[] = new CRM_Booking_APIWrapper();
}
}

0 comments on commit 685328c

Please sign in to comment.