From 347454480bddfc391be4de5be14a32cdbc755542 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 18 Feb 2021 10:41:28 -0500 Subject: [PATCH] Add 'readonly' attribute to schema fields For core fields, applies to primaryKey fields and fields tagged . For custom fields, this comes through via the 'is_view' property. This will help autogenerated forms to know whether a field is appropriate to show to the user. --- CRM/Contact/DAO/Contact.php | 8 ++++++- CRM/Core/CodeGen/Specification.php | 3 +-- Civi/Api4/Generic/BasicGetFieldsAction.php | 5 +++++ Civi/Api4/Service/Spec/FieldSpec.php | 22 +++++++++++++++++++ .../Spec/Provider/CustomValueSpecProvider.php | 2 ++ Civi/Api4/Service/Spec/SpecFormatter.php | 2 ++ .../crmSearchActionUpdate.ctrl.js | 10 ++++----- xml/schema/Contact/Contact.xml | 7 ++++-- xml/templates/dao.tpl | 3 +++ 9 files changed, 52 insertions(+), 10 deletions(-) diff --git a/CRM/Contact/DAO/Contact.php b/CRM/Contact/DAO/Contact.php index 95f45151b5..1ffa441fb1 100644 --- a/CRM/Contact/DAO/Contact.php +++ b/CRM/Contact/DAO/Contact.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Contact/Contact.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:d72db3da5f0a3646c35296c43880b425) + * (GenCodeChecksum:cfed69c9e09acada46340971415ec767) */ /** @@ -465,6 +465,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'html' => [ 'type' => 'Number', ], + 'readonly' => TRUE, 'add' => '1.1', ], 'contact_type' => [ @@ -490,6 +491,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'labelColumn' => 'label', 'condition' => 'parent_id IS NULL', ], + 'readonly' => TRUE, 'add' => '1.1', ], 'contact_sub_type' => [ @@ -699,6 +701,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'html' => [ 'type' => 'Text', ], + 'readonly' => TRUE, 'add' => '1.1', ], 'display_name' => [ @@ -717,6 +720,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'html' => [ 'type' => 'Text', ], + 'readonly' => TRUE, 'add' => '1.1', ], 'nick_name' => [ @@ -869,6 +873,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', 'localizable' => 0, + 'readonly' => TRUE, 'add' => '1.1', ], 'api_key' => [ @@ -1528,6 +1533,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'html' => [ 'label' => ts("Modified Date"), ], + 'readonly' => TRUE, 'add' => '4.3', ], ]; diff --git a/CRM/Core/CodeGen/Specification.php b/CRM/Core/CodeGen/Specification.php index 015b56560c..0cf1843286 100644 --- a/CRM/Core/CodeGen/Specification.php +++ b/CRM/Core/CodeGen/Specification.php @@ -317,8 +317,6 @@ class CRM_Core_CodeGen_Specification { $field['cols'] = isset($fieldXML->html) ? $this->value('cols', $fieldXML->html) : NULL; break; - break; - case 'datetime': $field['sqlType'] = $field['phpType'] = $type; $field['crmType'] = 'CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME'; @@ -377,6 +375,7 @@ class CRM_Core_CodeGen_Specification { } $field['headerPattern'] = $this->value('headerPattern', $fieldXML); $field['dataPattern'] = $this->value('dataPattern', $fieldXML); + $field['readonly'] = $this->value('readonly', $fieldXML); $field['uniqueName'] = $this->value('uniqueName', $fieldXML); $field['uniqueTitle'] = $this->value('uniqueTitle', $fieldXML); $field['serialize'] = $this->value('serialize', $fieldXML); diff --git a/Civi/Api4/Generic/BasicGetFieldsAction.php b/Civi/Api4/Generic/BasicGetFieldsAction.php index 7f4faf7ae9..49af587ffb 100644 --- a/Civi/Api4/Generic/BasicGetFieldsAction.php +++ b/Civi/Api4/Generic/BasicGetFieldsAction.php @@ -125,6 +125,7 @@ class BasicGetFieldsAction extends BasicGetAction { 'title' => empty($field['name']) ? NULL : ucwords(str_replace('_', ' ', $field['name'])), 'entity' => $this->getEntityName(), 'required' => FALSE, + 'readonly' => FALSE, 'options' => !empty($field['pseudoconstant']), 'data_type' => \CRM_Utils_Array::value('type', $field, 'String'), ], array_flip($fields)); @@ -295,6 +296,10 @@ class BasicGetFieldsAction extends BasicGetAction { 'name' => 'entity', 'data_type' => 'String', ], + [ + 'name' => 'readonly', + 'data_type' => 'Boolean', + ], ]; } diff --git a/Civi/Api4/Service/Spec/FieldSpec.php b/Civi/Api4/Service/Spec/FieldSpec.php index 0d503ad748..3aa807a572 100644 --- a/Civi/Api4/Service/Spec/FieldSpec.php +++ b/Civi/Api4/Service/Spec/FieldSpec.php @@ -112,6 +112,11 @@ class FieldSpec { */ protected $columnName; + /** + * @var bool + */ + protected $readonly = FALSE; + /** * Aliases for the valid data types * @@ -361,6 +366,23 @@ class FieldSpec { return $this; } + /** + * @return bool + */ + public function getreadonly() { + return $this->readonly; + } + + /** + * @param bool $readonly + * @return $this + */ + public function setreadonly($readonly) { + $this->readonly = (bool) $readonly; + + return $this; + } + /** * @return string|NULL */ diff --git a/Civi/Api4/Service/Spec/Provider/CustomValueSpecProvider.php b/Civi/Api4/Service/Spec/Provider/CustomValueSpecProvider.php index 19c4cdbb6c..5e61b2a54f 100644 --- a/Civi/Api4/Service/Spec/Provider/CustomValueSpecProvider.php +++ b/Civi/Api4/Service/Spec/Provider/CustomValueSpecProvider.php @@ -32,12 +32,14 @@ class CustomValueSpecProvider implements Generic\SpecProviderInterface { if ($action !== 'create') { $idField = new FieldSpec('id', $spec->getEntity(), 'Integer'); $idField->setTitle(ts('Custom Value ID')); + $idField->setreadonly(TRUE); $spec->addFieldSpec($idField); } $entityField = new FieldSpec('entity_id', $spec->getEntity(), 'Integer'); $entityField->setTitle(ts('Entity ID')); $entityField->setRequired($action === 'create'); $entityField->setFkEntity('Contact'); + $entityField->setreadonly(TRUE); $spec->addFieldSpec($entityField); } diff --git a/Civi/Api4/Service/Spec/SpecFormatter.php b/Civi/Api4/Service/Spec/SpecFormatter.php index 9e00ba09f5..a860e60b0b 100644 --- a/Civi/Api4/Service/Spec/SpecFormatter.php +++ b/Civi/Api4/Service/Spec/SpecFormatter.php @@ -67,6 +67,7 @@ class SpecFormatter { $field->setHelpPre($data['help_pre'] ?? NULL); $field->setHelpPost($data['help_post'] ?? NULL); $field->setOptions(self::customFieldHasOptions($data)); + $field->setreadonly($data['is_view']); } else { $name = $data['name'] ?? NULL; @@ -75,6 +76,7 @@ class SpecFormatter { $field->setTitle($data['title'] ?? NULL); $field->setLabel($data['html']['label'] ?? NULL); $field->setOptions(!empty($data['pseudoconstant'])); + $field->setreadonly(!empty($data['readonly'])); } $field->setSerialize($data['serialize'] ?? NULL); $field->setDefaultValue($data['default'] ?? NULL); diff --git a/ext/search/ang/crmSearchActions/crmSearchActionUpdate.ctrl.js b/ext/search/ang/crmSearchActions/crmSearchActionUpdate.ctrl.js index 0a3af265ea..b55b605d27 100644 --- a/ext/search/ang/crmSearchActions/crmSearchActionUpdate.ctrl.js +++ b/ext/search/ang/crmSearchActions/crmSearchActionUpdate.ctrl.js @@ -11,8 +11,11 @@ this.add = null; this.fields = null; - crmApi4(model.entity, 'getFields', {action: 'update', loadOptions: ['id', 'name', 'label', 'description', 'color', 'icon']}) - .then(function(fields) { + crmApi4(model.entity, 'getFields', { + action: 'update', + loadOptions: ['id', 'name', 'label', 'description', 'color', 'icon'], + where: [["readonly", "=", false]], + }).then(function(fields) { ctrl.fields = fields; }); @@ -58,9 +61,6 @@ if (fieldInUse(item.name)) { formatted.disabled = true; } - if (item.name !== 'id') { - result.push(formatted); - } }, []); return {results: results}; }; diff --git a/xml/schema/Contact/Contact.xml b/xml/schema/Contact/Contact.xml index b89166a2e9..e539c2805c 100644 --- a/xml/schema/Contact/Contact.xml +++ b/xml/schema/Contact/Contact.xml @@ -48,6 +48,7 @@ Select + true 1.1 3.1 null @@ -208,7 +209,7 @@ Text 30 - + true true Name used for sorting different contact types 1.1 @@ -227,7 +228,7 @@ Text 30 - + true true Formatted name representing preferred format for display/print/other output. 1.1 @@ -344,6 +345,7 @@ 1.1 1.5 true + true index_hash @@ -878,6 +880,7 @@ + true 4.3 diff --git a/xml/templates/dao.tpl b/xml/templates/dao.tpl index e9e94f70c3..c5bf31fb01 100644 --- a/xml/templates/dao.tpl +++ b/xml/templates/dao.tpl @@ -201,6 +201,9 @@ class {$table.className} extends CRM_Core_DAO {ldelim} {/if} {if $field.pseudoconstant} 'pseudoconstant' => {$field.pseudoconstant|@print_array}, +{/if} +{if $field.readonly || $field.name === $table.primaryKey.name} + 'readonly' => TRUE, {/if} 'add' => {if $field.add}'{$field.add}'{else}NULL{/if}, ), -- 2.25.1