Added code changes

This commit is contained in:
CiviWare Solutions 2018-08-17 05:45:21 +05:30
parent 334c120a35
commit 4f7d91a5cc
7 changed files with 282 additions and 13 deletions

View File

@ -21,9 +21,9 @@ class CRM_EntityTemplates_BAO_EntityTemplates extends CRM_Core_DAO_EntityTemplat
* @param array $params
*
* @throws Exception
* @return CRM_EntityTemplates_BAO_EntityTemplates|CRM_Core_Error
* @return CRM_EntityTemplates_BAO_EntityTemplates|CRM_Core_Error|null
*/
public static function create($params) {
public static function add($params) {
if (empty($params['entity_table'])) {
throw new CRM_Core_Exception(ts('Entity Table is mandatory.'));
@ -40,9 +40,9 @@ class CRM_EntityTemplates_BAO_EntityTemplates extends CRM_Core_DAO_EntityTemplat
}
if (empty($params['form_values'])) {
return;
return NULL;
}
$params['form_values'] = serialize($params['form_values']);
$params['form_values'] = json_encode($params['form_values']);
if (!empty($params['id'])) {
CRM_Utils_Hook::pre('edit', 'EntityTemplates', $params['id'], $params);
@ -65,4 +65,71 @@ class CRM_EntityTemplates_BAO_EntityTemplates extends CRM_Core_DAO_EntityTemplat
return $entityTemplates;
}
/**
* 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;
}
}

View File

@ -31,7 +31,7 @@ class CRM_EntityTemplates_Page_EntityTemplates extends CRM_Core_Page_Basic {
CRM_Core_Action::UPDATE => [
'name' => ts('Edit'),
'url' => '%%url%%',
'qs' => '%%query%%&isTemplate=1&templateId=%%id%%',
'qs' => '%%query%%&templateId=%%id%%',
'title' => ts('Edit template'),
],
CRM_Core_Action::DELETE => [
@ -74,6 +74,7 @@ class CRM_EntityTemplates_Page_EntityTemplates extends CRM_Core_Page_Basic {
);
}
list($url, $query) = explode('?', $optionValueName);
$query .= "&entityTemplate={$entityType}";
$entityTypeOptions = '';
foreach ($entityTypes as $key => $value) {
$extra = '';

View File

@ -0,0 +1,106 @@
<?php
class CRM_EntityTemplates_Utils {
/**
* Build all the data structures needed to build the form.
*
* @param string $formName
* @param object $form
*/
public static function preProcess($formName, &$form) {
if (CRM_Utils_Request::retrieve('snippet', 'String')
|| !($form->getVar('_action') & CRM_Core_Action::ADD)
) {
return;
}
$form->_entityTemplate = CRM_Utils_Request::retrieve('entityTemplate', 'String', $form);
$form->_entityTemplateId = CRM_Utils_Request::retrieve('templateId', 'Integer', $form);
$contactType = NULL;
if ('CRM_Contact_Form_Contact' == $formName) {
$contactType = $form->getVar('_contactType');
}
$form->_entityTemplateValues = CRM_EntityTemplates_BAO_EntityTemplates::getTemplateValues(
$formName,
$form->_entityTemplate,
$form->_entityTemplateId,
$contactType
);
}
/**
* Build the form object.
*
* @param string $formName
* @param object $form
*/
public static function buildForm($formName, &$form) {
if (!empty($form->_entityTemplateValues)) {
if ($form->_entityTemplate) {
$form->add('hidden', 'entity_template', $form->_entityTemplate);
if ($form->_entityTemplateId) {
$form->add('hidden', 'entity_template_id', $form->_entityTemplateId);
}
$form->add('text', 'entity_template_title', ts('Template Title'), [], TRUE);
}
else {
$entities = CRM_EntityTemplates_BAO_EntityTemplates::getEntityTemplates($form->_entityTemplateValues['value']);
if (!empty($entities)) {
$form->add('select', 'entity_template_id', ts('Templates'), $entities, FALSE, ['placeholder' => ts('- select -'), 'class' => 'crm-select2']);
list($url, $query) = explode('?', $form->_entityTemplateValues['name']);
$url = CRM_Utils_System::url($url, $query);
$form->assign('redirectUrl', $url);
}
}
if ($form->_entityTemplateId) {
$formValues = CRM_EntityTemplates_BAO_EntityTemplates::getFormValues($form->_entityTemplateId);
$formValues['entity_template_id'] = $form->_entityTemplateId;
$form->setDefaults($formValues);
}
CRM_Core_Region::instance('page-body')->add([
'template' => 'CRM/EntityTemplates/EntityTemplate.tpl',
]);
}
}
/**
* Add entity template.
*
* @param string $objectName
* @param array $params
*/
public static function addTemplate($objectName, $params) {
if (CRM_Utils_Array::value('entity_template', $params) == $objectName) {
$id = NULL;
if (!empty($params['entity_template_id'])) {
$id = $params['entity_template_id'];
}
// unset entity template fields
unset(
$params['is_entity_template'],
$params['entity_template_id'],
$params['qfKey'],
$params['entryURL']
);
$entityParams = [
'entity_table' => $objectName,
'title' => CRM_Utils_Array::value('entity_template_title', $params, rand()),
'form_values' => $params,
];
if ($id) {
$entityParams['id'] = $id;
}
civicrm_api3('EntityTemplates', 'create', $entityParams);
// print message
CRM_Core_Session::setStatus(ts('Entity template saved successfully'), ts('Entity template'), 'success');
// redirect to list page
$url = CRM_Utils_System::url('civicrm/entity/templates', "reset=1&entityType={$objectName}");
CRM_Utils_System::redirect($url);
}
}
}

View File

@ -25,7 +25,7 @@ function civicrm_api3_entity_templates_create($params) {
* Array of retrieved EntityTemplates property values.
*/
function civicrm_api3_entity_templates_get($params) {
return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'EntityTemplates');
}
/**

View File

@ -175,7 +175,7 @@ function entitytemplates_civicrm_entityTypes(&$entityTypes) {
_entitytemplates_civix_civicrm_entityTypes($entityTypes);
$entityTypes[] = [
'name' => 'EntityTemplates',
'class' => 'CRM_Core_DAO_EntityTemplates',
'class' => 'CRM_EntityTemplates_BAO_EntityTemplates',
'table' => 'civicrm_entity_templates',
];
}
@ -197,3 +197,67 @@ function entitytemplates_civicrm_navigationMenu(&$menu) {
]);
_entitytemplates_civix_navigationMenu($menu);
}
/**
* Implements hook_civicrm_preProcess().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
*
*/
function entitytemplates_civicrm_preProcess($formName, &$form) {
CRM_EntityTemplates_Utils::preProcess($formName, $form);
}
/**
* Implements hook_civicrm_buildForm().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildForm
*
*/
function entitytemplates_civicrm_buildForm($formName, &$form) {
if ($formName == 'CRM_Admin_Form_Options' && $form->getVar('_gName') == 'entity_template_for') {
$form->add(
'text',
'name',
ts('Url'),
CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'name')
);
}
CRM_EntityTemplates_Utils::buildForm($formName, $form);
}
/**
* Implements hook_civicrm_pre().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_pre
*
*/
function entitytemplates_civicrm_pre($op, $objectName, $id, &$params) {
if ($op == 'create') {
CRM_EntityTemplates_Utils::addTemplate($objectName, $params);
}
}
/**
* Implements hook_civicrm_validateForm().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_validateForm
*
*/
function entitytemplates_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ($form->_entityTemplate) {
if (!empty($fields['entity_template_title'])) {
$params = [
'title' => $fields['entity_template_title'],
'entity_table' => $form->_entityTemplate,
];
if ($form->_entityTemplateId) {
$params['id'] = ['NOT IN' => [$form->_entityTemplateId]];
}
$count = civicrm_api3('EntityTemplates', 'getcount', $params);
if ($count) {
$errors['entity_template_title'] = ts('Title already exists.');
}
}
}
}

View File

@ -0,0 +1,25 @@
{if $form.entity_template_title}
<table class='entity_template_title form-layout-compressed'>
<tr>
<td class='label'>{$form.entity_template_title.label}</td>
<td>{$form.entity_template_title.html}</td>
</tr>
</table>
{else}
<table class='entity_template_id form-layout-compressed'>
<tr>
<td class='label'>{$form.entity_template_id.label}</td>
<td>{$form.entity_template_id.html}</td>
</tr>
</table>
{/if}
{literal}
<script type="text/javascript">
CRM.$(function($) {
$($('table.entity_template_title, table.entity_template_id')).insertAfter('div.crm-submit-buttons:first');
$('#entity_template_id').change(function() {
window.location.href = '{/literal}{$redirectUrl}{literal}&templateId=' + $(this).val();
});
});
</script>
{/literal}

View File

@ -9,14 +9,20 @@
<th>{ts}Title{/ts}</th>
<th></th>
</thead>
{if $rows}
{foreach from=$rows item=row}
<tr id="EntityTemplates-{$row.id}" class="crm-entity {cycle values="odd-row,even-row"}">
<td class='EntityTemplates-title'>{$row.title}</td>
<td class='EntityTemplates-links'>{$row.links}</td>
</tr>
{/foreach}
{else}
<tr>
<td style='text-align: center;' colspan="2">{ts}None found.{/ts}</td>
</tr>
{/if}
</table>
{crmButton p=$url q="`$query`&isTemplate=1" class="cancel" icon="times"}{ts}Add Template{/ts}{/crmButton}
{crmButton p=$url q="`$query`" class="cancel" icon="times"}{ts}Add Template{/ts}{/crmButton}
{crmButton p="civicrm" q="reset=1" class="cancel" icon="times"}{ts}Done{/ts}{/crmButton}
</div>