2018-08-13 21:59:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class CRM_EntityTemplates_BAO_EntityTemplates extends CRM_Core_DAO_EntityTemplates {
|
|
|
|
|
|
|
|
/**
|
2018-08-13 22:42:25 +00:00
|
|
|
* Build Entity option list.
|
2018-08-13 21:59:23 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getEntityTypes() {
|
|
|
|
$result = civicrm_api3('OptionValue', 'get', [
|
|
|
|
'return' => ["value"],
|
|
|
|
'option_group_id' => "entity_template_for",
|
|
|
|
'options' => ['limit' => 0],
|
|
|
|
]);
|
|
|
|
return array_column($result['values'], 'value', 'value');
|
|
|
|
}
|
|
|
|
|
2018-08-13 22:42:25 +00:00
|
|
|
/**
|
|
|
|
* Create Entity Template.
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*
|
|
|
|
* @throws Exception
|
2018-08-17 00:15:21 +00:00
|
|
|
* @return CRM_EntityTemplates_BAO_EntityTemplates|CRM_Core_Error|null
|
2018-08-13 22:42:25 +00:00
|
|
|
*/
|
2018-08-17 00:15:21 +00:00
|
|
|
public static function add($params) {
|
2018-08-13 21:59:23 +00:00
|
|
|
|
|
|
|
if (empty($params['entity_table'])) {
|
|
|
|
throw new CRM_Core_Exception(ts('Entity Table is mandatory.'));
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$optionValueName = civicrm_api3('OptionValue', 'getvalue', [
|
|
|
|
'option_group_id' => "entity_template_for",
|
|
|
|
'value' => $params['entity_table'],
|
|
|
|
'return' => 'name',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
catch (CiviCRM_API3_Exception $e) {
|
|
|
|
throw new CRM_Core_Exception(ts('Invalid Entity Type.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($params['form_values'])) {
|
2018-08-17 00:15:21 +00:00
|
|
|
return NULL;
|
2018-08-13 21:59:23 +00:00
|
|
|
}
|
2018-08-17 00:15:21 +00:00
|
|
|
$params['form_values'] = json_encode($params['form_values']);
|
2018-08-13 21:59:23 +00:00
|
|
|
|
|
|
|
if (!empty($params['id'])) {
|
|
|
|
CRM_Utils_Hook::pre('edit', 'EntityTemplates', $params['id'], $params);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CRM_Utils_Hook::pre('create', 'EntityTemplates', NULL, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
$entityTemplates = new CRM_EntityTemplates_BAO_EntityTemplates();
|
|
|
|
$entityTemplates->copyValues($params);
|
|
|
|
$entityTemplates->save();
|
|
|
|
|
|
|
|
if (!empty($params['id'])) {
|
|
|
|
CRM_Utils_Hook::post('edit', 'EntityTemplates', $entityTemplates->id, $entityTemplates);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CRM_Utils_Hook::post('create', 'EntityTemplates', $entityTemplates->id, $entityTemplates);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $entityTemplates;
|
|
|
|
}
|
|
|
|
|
2018-08-17 00:15:21 +00:00
|
|
|
/**
|
|
|
|
* Get Form values.
|
|
|
|
*
|
|
|
|
* @param int $templateId
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getFormValues($templateId) {
|
|
|
|
$formValues = civicrm_api3('EntityTemplates', 'getvalue', [
|
|
|
|
'id' => $templateId,
|
|
|
|
'return' => 'form_values',
|
|
|
|
]);
|
|
|
|
return json_decode($formValues, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all entity template for entity type.
|
|
|
|
*
|
|
|
|
* @param string $entityType
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getEntityTemplates($entityType) {
|
|
|
|
$templates = civicrm_api3('EntityTemplates', 'get', [
|
|
|
|
'return' => ['id', 'title'],
|
|
|
|
'entity_table' => $entityType,
|
|
|
|
]);
|
|
|
|
return array_column($templates['values'], 'title', 'id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create Entity Template.
|
|
|
|
*
|
|
|
|
* @param string $formName
|
|
|
|
* @param string $entityTemplate *
|
|
|
|
* @param int $entityTemplateId
|
|
|
|
* @param string $contactType
|
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public static function getTemplateValues(
|
|
|
|
$formName,
|
|
|
|
$entityTemplate,
|
|
|
|
$entityTemplateId,
|
|
|
|
$contactType
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
$params = [
|
|
|
|
'option_group_id' => "entity_template_for",
|
|
|
|
];
|
|
|
|
if ($entityTemplate || $contactType) {
|
|
|
|
$params['value'] = $entityTemplate ? $entityTemplate : $contactType;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$params['description'] = $formName;
|
|
|
|
}
|
|
|
|
$result = civicrm_api3('OptionValue', 'getsingle', $params);
|
|
|
|
|
|
|
|
if ($formName == $result['description']) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (CiviCRM_API3_Exception $e) {
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-08-13 21:59:23 +00:00
|
|
|
}
|