Skip to content

Commit

Permalink
Enhance metadata for OptionGroups
Browse files Browse the repository at this point in the history
Extracted from civicrm#25180
  • Loading branch information
aydun committed Dec 16, 2022
1 parent 0a7b0cf commit e5a78fc
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 7 deletions.
35 changes: 31 additions & 4 deletions CRM/Core/DAO/OptionGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Core/OptionGroup.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:6d1ca6dc8aa693e7b26bb19b731b0b55)
* (GenCodeChecksum:8e1e98e1424ca35cb2bca88efee38f4b)
*/

/**
Expand Down Expand Up @@ -37,6 +37,16 @@ class CRM_Core_DAO_OptionGroup extends CRM_Core_DAO {
*/
public static $_log = TRUE;

/**
* Paths for accessing this entity in the UI.
*
* @var string[]
*/
protected static $_paths = [
'add' => 'civicrm/admin/options/add?action=add&reset=1',
'update' => 'civicrm/admin/options/update?action=update&reset=1&id=[id]',
];

/**
* Option Group ID
*
Expand Down Expand Up @@ -174,12 +184,15 @@ public static function &fields() {
'entity' => 'OptionGroup',
'bao' => 'CRM_Core_BAO_OptionGroup',
'localizable' => 0,
'html' => [
'type' => 'Text',
],
'add' => '1.5',
],
'title' => [
'name' => 'title',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Option Group title'),
'title' => ts('Option Group Title'),
'description' => ts('Option Group title.'),
'maxlength' => 255,
'size' => CRM_Utils_Type::HUGE,
Expand All @@ -188,6 +201,9 @@ public static function &fields() {
'entity' => 'OptionGroup',
'bao' => 'CRM_Core_BAO_OptionGroup',
'localizable' => 1,
'html' => [
'type' => 'Text',
],
'add' => '1.5',
],
'description' => [
Expand All @@ -200,6 +216,9 @@ public static function &fields() {
'entity' => 'OptionGroup',
'bao' => 'CRM_Core_BAO_OptionGroup',
'localizable' => 1,
'html' => [
'type' => 'Text',
],
'add' => '1.5',
],
'data_type' => [
Expand All @@ -222,7 +241,7 @@ public static function &fields() {
'is_reserved' => [
'name' => 'is_reserved',
'type' => CRM_Utils_Type::T_BOOLEAN,
'title' => ts('Option Group Is Reserved?'),
'title' => ts('Option Group Is Reserved'),
'description' => ts('Is this a predefined system option group (i.e. it can not be deleted)?'),
'required' => TRUE,
'where' => 'civicrm_option_group.is_reserved',
Expand All @@ -231,12 +250,16 @@ public static function &fields() {
'entity' => 'OptionGroup',
'bao' => 'CRM_Core_BAO_OptionGroup',
'localizable' => 0,
'html' => [
'type' => 'CheckBox',
'label' => ts("Reserved"),
],
'add' => '1.5',
],
'is_active' => [
'name' => 'is_active',
'type' => CRM_Utils_Type::T_BOOLEAN,
'title' => ts('Option Group Is Active?'),
'title' => ts('Option Group Is Active'),
'description' => ts('Is this option group active?'),
'required' => TRUE,
'where' => 'civicrm_option_group.is_active',
Expand All @@ -263,6 +286,10 @@ public static function &fields() {
'entity' => 'OptionGroup',
'bao' => 'CRM_Core_BAO_OptionGroup',
'localizable' => 0,
'html' => [
'type' => 'CheckBox',
'label' => ts("Locked"),
],
'add' => '4.5',
],
'option_value_fields' => [
Expand Down
90 changes: 90 additions & 0 deletions Civi/Api4/Service/Spec/Provider/OptionGroupGetSpecProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

namespace Civi\Api4\Service\Spec\Provider;

use Civi\Api4\Service\Spec\RequestSpec;
use Civi\Api4\Service\Spec\FieldSpec;

/**
* @service
* @internal
*/
class OptionGroupGetSpecProvider extends \Civi\Core\Service\AutoService implements Generic\SpecProviderInterface {

/**
* @param \Civi\Api4\Service\Spec\RequestSpec $spec
*/
public function modifySpec(RequestSpec $spec) {
$field = new FieldSpec('edit_link', 'OptionGroup', 'String');
$field->setLabel(ts('Edit link'))
->setTitle(ts('Edit link'))
->setDescription(ts('Link for editing this OptionGroup'))
->setColumnName('name')
->setReadonly(TRUE)
->setSqlRenderer([__CLASS__, 'getEditLink']);
$spec->addFieldSpec($field);

$field = new FieldSpec('add_link', 'OptionGroup', 'String');
$field->setLabel(ts('Add link'))
->setTitle(ts('Add link'))
->setDescription(ts('Link for editing this OptionGroup'))
->setColumnName('name')
->setReadonly(TRUE)
->setSqlRenderer([__CLASS__, 'getAddLink']);
$spec->addFieldSpec($field);
}

/**
* @param string $entity
* @param string $action
*
* @return bool
*/
public function applies($entity, $action) {
return $entity === 'OptionGroup' && $action === 'get';
}

/**
* Generate SQL for age field
* @param array $field
* @return string
*
* If there is explicit handling defined in the menus, use that
* otherwise, use the new SK edit
*/
public static function getEditLink(array $field): string {
return "COALESCE(
(SELECT path FROM `civicrm_menu` WHERE path = CONCAT('civicrm/admin/options/', {$field['sql_name']})),
CONCAT('civicrm/admin/options/edit#/?gid=', `a`.`id`)
)";
}

/**
* Generate SQL for age field
* @param array $field
* @return string
*
* If there is explicit handling defined in the menus, use that
* Otherwise go to the old form using the 'add' alias to avoid being directed to SK
*/
public static function getAddLink(array $field): string {
return "CONCAT(
COALESCE(
(SELECT path FROM `civicrm_menu` WHERE path = CONCAT('civicrm/admin/options/', {$field['sql_name']})),
'civicrm/admin/options/add'
),
'?action=add&gid=', `a`.`id`
)";
}

}
27 changes: 24 additions & 3 deletions xml/schema/Core/OptionGroup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<name>civicrm_option_group</name>
<add>1.5</add>
<log>true</log>
<paths>
<add>civicrm/admin/options/add?action=add&amp;reset=1</add>
<update>civicrm/admin/options/update?action=update&amp;reset=1&amp;id=[id]</update>
</paths>
<field>
<name>id</name>
<title>Option Group ID</title>
Expand All @@ -29,15 +33,21 @@
<required>true</required>
<comment>Option group name. Used as selection key by class properties which lookup options in civicrm_option_value.</comment>
<add>1.5</add>
<html>
<type>Text</type>
</html>
</field>
<field>
<name>title</name>
<title>Option Group title</title>
<title>Option Group Title</title>
<type>varchar</type>
<length>255</length>
<localizable>true</localizable>
<comment>Option Group title.</comment>
<add>1.5</add>
<html>
<type>Text</type>
</html>
</field>
<field>
<name>description</name>
Expand All @@ -46,6 +56,9 @@
<localizable>true</localizable>
<comment>Option group description.</comment>
<add>1.5</add>
<html>
<type>Text</type>
</html>
</field>
<field>
<name>data_type</name>
Expand All @@ -60,16 +73,20 @@
</field>
<field>
<name>is_reserved</name>
<title>Option Group Is Reserved?</title>
<title>Option Group Is Reserved</title>
<type>boolean</type>
<default>1</default>
<required>true</required>
<comment>Is this a predefined system option group (i.e. it can not be deleted)?</comment>
<add>1.5</add>
<html>
<type>CheckBox</type>
<label>Reserved</label>
</html>
</field>
<field>
<name>is_active</name>
<title>Option Group Is Active?</title>
<title>Option Group Is Active</title>
<type>boolean</type>
<default>1</default>
<required>true</required>
Expand All @@ -88,6 +105,10 @@
<required>true</required>
<comment>A lock to remove the ability to add new options via the UI.</comment>
<add>4.5</add>
<html>
<type>CheckBox</type>
<label>Locked</label>
</html>
</field>
<field>
<name>option_value_fields</name>
Expand Down

0 comments on commit e5a78fc

Please sign in to comment.