Commit 83db82c8 authored by BjoernE's avatar BjoernE 👾 Committed by jaapjansma

API import/export/update via files

parent a6fa8af2
......@@ -236,6 +236,23 @@
return $status;
}
/**
* Returns the status of the form processor.
* @See CRM_FormProcessor_Status for possible values.
*
* @param string $formProcessorName
* The name of the form processor.
* @param integer $status
* The new status
*/
public static function setStatus($formProcessorName, $status) {
$sql = "UPDATE `civicrm_form_processor_instance` SET `status` = %1 WHERE `name` = %2";
$params[1] = array($status, 'Integer');
$params[2] = array($formProcessorName, 'String');
CRM_Core_DAO::executeQuery($sql, $params);
}
/**
* Updates the status and source file of the form processor.
* @See CRM_FormProcessor_Status for possible status values.
......
......@@ -3,7 +3,7 @@
use CRM_FormProcessor_ExtensionUtil as E;
/**
* FormProcessorInstance.Validatename API
* FormProcessorInstance.Export API
*
* @param array $params
* @return array API result descriptor
......@@ -14,12 +14,24 @@ use CRM_FormProcessor_ExtensionUtil as E;
function civicrm_api3_form_processor_instance_export($params) {
$exporter = new \Civi\FormProcessor\Exporter\ExportToJson();
$returnValues = array();
$returnValues['export'] = $exporter->export($params['id']);
$returnValues['export'] = (array) $exporter->export($params['id']);
if (!empty($params['to_file'])) {
// set export date
$data = $returnValues['export'];
$data['export_timestamp'] = date('Y-m-d H:i:s');
$data['export_system_url'] = CIVICRM_UF_BASEURL;
// if the path is a directory, use the name
if (is_dir($params['to_file'])) {
$params['to_file'] = $params['to_file'] . DIRECTORY_SEPARATOR . $data['name'] . '.json';
}
file_put_contents($params['to_file'], json_encode($data, JSON_PRETTY_PRINT));
}
return $returnValues;
}
/**
* FormProcessorInstance.Validatename API specification (optional)
* FormProcessorInstance.Export API specification (optional)
* This is used for documentation and validation.
*
* @param array $spec description of fields supported by this API call
......@@ -32,4 +44,10 @@ function _civicrm_api3_form_processor_instance_export_spec(&$spec) {
'type' => CRM_Utils_Type::T_INT,
'api.required' => true
);
$spec['to_file'] = array(
'title' => E::ts('To File'),
'type' => CRM_Utils_Type::T_STRING,
'description' => E::ts('If a file path is given, the JSON configuration will be written there.'),
'api.required' => false
);
}
\ No newline at end of file
......@@ -24,7 +24,45 @@ function _civicrm_api3_form_processor_instance_Import_spec(&$spec) {
function civicrm_api3_form_processor_instance_Import($params) {
$exporter = new \Civi\FormProcessor\Exporter\ExportToJson();
$returnValues = array();
if (empty($params['file'])) {
// default behaviour
$returnValues['import'] = $exporter->importFromExtensions();
$returnValues['is_error'] = 0;
} else {
// caller provided a file to import:
$data = json_decode(file_get_contents($params['file']), true);
if (!empty($data['name'])) {
$status = \CRM_FormProcessor_BAO_FormProcessorInstance::getStatus($data['name']);
if ($status != \CRM_FormProcessor_Status::IN_CODE) {
// it's already overridden -> reset so we can override again
\CRM_FormProcessor_BAO_FormProcessorInstance::setStatus($data['name'], \CRM_FormProcessor_Status::IN_CODE);
}
$returnValues['import'] = $exporter->import($data, $params['file']);
$returnValues['import']['original_status'] = $status; // we might have adjusted it, so we know better
$returnValues['is_error'] = 0;
} else {
$returnValues = civicrm_api3_create_error("The file '{$params['file']}' doesn't contain a form processor specification.");
}
}
return $returnValues;
}
/**
* FormProcessorInstance.Import API specification (optional)
* This is used for documentation and validation.
*
* @param array $spec description of fields supported by this API call
* @return void
* @see http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
*/
function _civicrm_api3_form_processor_instance_export_spec(&$spec) {
$spec['file'] = array(
'title' => E::ts('Specification File'),
'type' => CRM_Utils_Type::T_STRING,
'description' => E::ts('If a file path is given, the JSON configuration will be imported from there'),
'api.required' => false
);
}
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