From 4f7d91a5cc92853e35d937ba6295e3dc20ca6e29 Mon Sep 17 00:00:00 2001 From: Civiware Solutions Date: Fri, 17 Aug 2018 05:45:21 +0530 Subject: [PATCH] Added code changes --- CRM/EntityTemplates/BAO/EntityTemplates.php | 75 ++++++++++++- CRM/EntityTemplates/Page/EntityTemplates.php | 3 +- CRM/EntityTemplates/Utils.php | 106 ++++++++++++++++++ api/v3/EntityTemplates.php | 2 +- entitytemplates.php | 66 ++++++++++- .../CRM/EntityTemplates/EntityTemplate.tpl | 25 +++++ .../EntityTemplates/Page/EntityTemplates.tpl | 18 ++- 7 files changed, 282 insertions(+), 13 deletions(-) create mode 100644 CRM/EntityTemplates/Utils.php create mode 100644 templates/CRM/EntityTemplates/EntityTemplate.tpl diff --git a/CRM/EntityTemplates/BAO/EntityTemplates.php b/CRM/EntityTemplates/BAO/EntityTemplates.php index 9f6af8c..2de2512 100644 --- a/CRM/EntityTemplates/BAO/EntityTemplates.php +++ b/CRM/EntityTemplates/BAO/EntityTemplates.php @@ -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; + } + } diff --git a/CRM/EntityTemplates/Page/EntityTemplates.php b/CRM/EntityTemplates/Page/EntityTemplates.php index 4134d54..d25a3e0 100644 --- a/CRM/EntityTemplates/Page/EntityTemplates.php +++ b/CRM/EntityTemplates/Page/EntityTemplates.php @@ -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 = ''; diff --git a/CRM/EntityTemplates/Utils.php b/CRM/EntityTemplates/Utils.php new file mode 100644 index 0000000..7ca844a --- /dev/null +++ b/CRM/EntityTemplates/Utils.php @@ -0,0 +1,106 @@ +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); + } + } + +} diff --git a/api/v3/EntityTemplates.php b/api/v3/EntityTemplates.php index e72338d..421ec82 100644 --- a/api/v3/EntityTemplates.php +++ b/api/v3/EntityTemplates.php @@ -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'); } /** diff --git a/entitytemplates.php b/entitytemplates.php index c05b325..1c7bede 100644 --- a/entitytemplates.php +++ b/entitytemplates.php @@ -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.'); + } + } + } +} diff --git a/templates/CRM/EntityTemplates/EntityTemplate.tpl b/templates/CRM/EntityTemplates/EntityTemplate.tpl new file mode 100644 index 0000000..bf163dc --- /dev/null +++ b/templates/CRM/EntityTemplates/EntityTemplate.tpl @@ -0,0 +1,25 @@ +{if $form.entity_template_title} + + + + + +
{$form.entity_template_title.label}{$form.entity_template_title.html}
+{else} + + + + + +
{$form.entity_template_id.label}{$form.entity_template_id.html}
+{/if} +{literal} + +{/literal} diff --git a/templates/CRM/EntityTemplates/Page/EntityTemplates.tpl b/templates/CRM/EntityTemplates/Page/EntityTemplates.tpl index 32f9eef..0909ea0 100644 --- a/templates/CRM/EntityTemplates/Page/EntityTemplates.tpl +++ b/templates/CRM/EntityTemplates/Page/EntityTemplates.tpl @@ -9,14 +9,20 @@ {ts}Title{/ts} - {foreach from=$rows item=row} - - {$row.title} - {$row.links} + {if $rows} + {foreach from=$rows item=row} + + {$row.title} + {$row.links} + + {/foreach} + {else} + + {ts}None found.{/ts} - {/foreach} + {/if} - {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}