Commit e8f6bba4 authored by KevinLevie's avatar KevinLevie
Browse files

Add new ParamsProvider setup

parent 95b20606
......@@ -7,12 +7,15 @@
* @license AGPL-3.0
*/
abstract class CRM_Civiconfig_ParamsProvider {
/**
* Returns params needed to create entities.
*
* @param string $entityType CiviCRM entity type to create params for
*
* If you want to get your configuration from something else than the
* json-files in the resource directory, just overload this class, and use
* it in Config.php.
* If you want to get your configuration from something else than JSON files,
* just overload this class and call CRM_Civiconfig_Config::updateConfig($yourCustomParamsProvider).
*/
public abstract function getParamsArray();
public abstract function getParamsArray($entityType);
}
<?php
/**
* Params provider that uses the json files in the resources directory.
*
* Params provider that uses JSON files in a resources directory.
* @author Johan Vervloet (Chirojeugd-Vlaanderen vzw) <helpdesk@chiro.be>
* @date 6 Jun 2016
* @license AGPL-3.0
*
* This class is now initialised once, and reused for getting the parameters for each entity type.
* It's injected into CRM_Civiconfig_Config, to make it easier to implement alternative ParamsProviders.
* The constructor now only checks if the directory exists: getParamsArray tries to find and parse the JSON file.
* @author Kevin Levie <kevin.levie@civicoop.org>
*/
class CRM_Civiconfig_ParamsProvider_ResourcesDir extends CRM_Civiconfig_ParamsProvider {
protected $_resourcesPath = NULL;
protected $_jsonFile = NULL;
/**
* Constructor.
*
* @param string $entityType - CiviCRM entity type to create params for.
* @param string $path - Location of the resources dir.
*
* If $path is empty, the default location will be
* (extensionsDir)/org.iida.civiconfig/resources/
*
* @param string $path Resources directory
* @throws \CRM_Civiconfig_Exception Thrown if the JSON resource directory doesn't exist
*/
function __construct($entityType, $path = NULL) {
// TODO: Check whether $entity is actually a CiviCRM entity type.
// TODO: Check whether $path exists.
if ($path != NULL) {
if (substr($path, -1) != '/') {
// Add trailing slash if it's not there.
$path .= '/';
}
$this->_resourcesPath = $path;
}
else {
$settings = civicrm_api3('Setting', 'Getsingle', array());
$this->_resourcesPath = $settings['extensionsDir'].'/org.iida.civiconfig/resources/';
function __construct($path) {
// Path now always required: check if it exists
if(!file_exists($path) || !is_dir($path)) {
throw new \CRM_Civiconfig_Exception("Civiconfig resource directory does not exist: {$path}.");
}
// Convert camelcase to underscore separated, and add an 's'.
// TODO: this will not work as expected with UFField, UFGroup, UFJoin, UFMatch:
$fileName = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $entityType)) . 's.json';
$this->_jsonFile = $this->_resourcesPath . $fileName;
if (!file_exists($this->_jsonFile)) {
throw new Exception("Could not load $entityType configuration file for extension,
contact your system administrator!");
}
// Resolve relative paths, and always add a trailing slash
$this->_resourcesPath = realpath($path) . '/';
}
/**
* Returns params to create entities.
* Returns params to create entities for $entityType.
*
* @param string $entityType CiviCRM entity type to create params for
* @return array $params Parameters
* @throws \CRM_Civiconfig_EntityException Thrown if json_decode returns false
*/
public function getParamsArray() {
$membershipTypesJson = file_get_contents($this->_jsonFile);
return json_decode($membershipTypesJson, true);
public function getParamsArray($entityType) {
// Get file name: convert camelcase to underscore separated, and add an 's'.
// TODO: this will not work as expected with UFField, UFGroup, UFJoin, UFMatch:
$fileName = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $entityType)) . 's.json';
$jsonFile = $this->_resourcesPath . $fileName;
// Check if file exists
if (!is_readable($jsonFile)) {
throw new \CRM_Civiconfig_EntityException("No readable JSON file for entity type '$entityType'.");
}
// Read file and try to parse JSON
$jsonData = file_get_contents($jsonFile);
$params = json_decode($jsonData, true);
if($params === null || !is_array($params)) {
throw new \CRM_Civiconfig_EntityException("Could not parse JSON file '{$this->_jsonFile}'.");
}
return $params;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment