Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Afform - Add action modes for joins #31480

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ext/afform/admin/ang/afGuiEditor.css
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ body.af-gui-dragging {
white-space: nowrap;
}

#afGuiEditor .dropdown-menu li .checkbox-inline label {
font-weight: normal;
}
#afGuiEditor .dropdown-menu li > * > label {
font-weight: normal;
cursor: pointer;
Expand Down
13 changes: 13 additions & 0 deletions ext/afform/admin/ang/afGuiEditor/elements/afGuiContainer-menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@
</div>
</div>
</li>
<li ng-click="$event.stopPropagation()" ng-if="$ctrl.isJoin()">
<div class="form-inline af-gui-field-select-in-dropdown">
<div class="form-group">
<label>{{:: ts('Allow:') }}</label>
<div class="checkbox-inline">
<label><input type="checkbox" ng-model="$ctrl.node.actions.update" ng-change="$ctrl.onChangeUpdateAction()">{{:: ts('Update') }}</label>
</div>
<div class="checkbox-inline">
<label><input type="checkbox" ng-model="$ctrl.node.actions.delete" ng-disabled="!$ctrl.node.actions.update">{{:: ts('Delete') }}</label>
</div>
</div>
</div>
</li>
<li><af-gui-menu-item-style node="$ctrl.node"></af-gui-menu-item-style></li>
<li><af-gui-menu-item-border node="$ctrl.node"></af-gui-menu-item-border></li>
<li><af-gui-menu-item-background node="$ctrl.node"></af-gui-menu-item-background></li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
return 'layout' in block;
};

this.isJoin = function() {
return !!ctrl.join;
};

$scope.getSetChildren = function(val) {
var collection = block.layout || (ctrl.node && ctrl.node['#children']);
return arguments.length ? (collection = val) : collection;
Expand Down Expand Up @@ -255,7 +259,17 @@
}
};

this.onChangeUpdateAction = function() {
if (!ctrl.node.actions.update) {
ctrl.node.actions.delete = false;
}
};

function initializeBlockContainer() {
// Set defaults for 'actions'
if (!('actions' in ctrl.node)) {
ctrl.node.actions = {update: true, delete: true};
}

// Cancel the below $watch expressions if already set
_.each(block.listeners, function(deregister) {
Expand Down
1 change: 1 addition & 0 deletions ext/afform/core/CRM/Afform/ArrayHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class CRM_Afform_ArrayHtml {
'*' => 'text',
'af-fieldset' => 'text',
'data' => 'js',
'actions' => 'js',
],
'af-entity' => [
'#selfClose' => TRUE,
Expand Down
31 changes: 28 additions & 3 deletions ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ public function loadEntity(array $entity, array $values, string $mode = 'update'
if (!empty($result[$key])) {
$data = ['fields' => $result[$key]];
foreach ($entity['joins'] ?? [] as $joinEntity => $join) {
$data['joins'][$joinEntity] = $this->loadJoins($joinEntity, $join, $entity, $entityId, $index);
$joinAllowedAction = self::getJoinAllowedAction($entity, $joinEntity);
if ($joinAllowedAction['update']) {
$data['joins'][$joinEntity] = $this->loadJoins($joinEntity, $entity, $entityId, $index);
}
}
$this->_entityValues[$entity['name']][$index] = $data;
}
Expand All @@ -251,8 +254,9 @@ public function loadEntity(array $entity, array $values, string $mode = 'update'
/**
* Finds all joins after loading an entity.
*/
public function loadJoins($joinEntity, $join, $afEntity, $entityId, $index): array {
public function loadJoins(string $joinEntity, array $afEntity, $entityId, $index): array {
$joinIdField = CoreUtil::getIdFieldName($joinEntity);
$join = $afEntity['joins'][$joinEntity];
$multipleLocationBlocks = is_array($join['data']['location_type_id'] ?? NULL);
$limit = 1;
// Repeating blocks - set limit according to `max`, if set, otherwise 0 for unlimited
Expand Down Expand Up @@ -462,9 +466,13 @@ protected static function getJoinWhereClause(FormDataModel $formDataModel, strin
}

protected static function getFkField($mainEntity, $otherEntity): ?array {
$fkEntities = [$otherEntity];
if ($otherEntity !== 'Contact' && CoreUtil::isContact($otherEntity)) {
$fkEntities[] = 'Contact';
}
foreach (self::getEntityFields($mainEntity) as $field) {
if ($field['type'] === 'Field' && empty($field['custom_field_id']) &&
($field['fk_entity'] === $otherEntity || in_array($otherEntity, $field['dfk_entities'] ?? [], TRUE))
(in_array($field['fk_entity'], $fkEntities, TRUE) || array_intersect($fkEntities, $field['dfk_entities'] ?? []))
) {
return $field;
}
Expand Down Expand Up @@ -732,4 +740,21 @@ protected static function getNestedKey(array $values) {
return is_array($firstValue) && $firstValue ? array_keys($firstValue)[0] : NULL;
}

/**
* Function to get allowed action of a join entity
*
* @param array $mainEntity
* @param string $joinEntityName
*
* @return array{update: bool, delete: bool}
*/
public static function getJoinAllowedAction(array $mainEntity, string $joinEntityName) {
$actions = ["update" => TRUE, "delete" => TRUE];
if (array_key_exists('actions', $mainEntity['joins'][$joinEntityName])) {
$actions = array_merge($actions, $mainEntity['joins'][$joinEntityName]['actions']);
}

return $actions;
}

}
50 changes: 37 additions & 13 deletions ext/afform/core/Civi/Api4/Action/Afform/Submit.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,17 +486,18 @@ private static function saveRelationshipByValues(array $relationship, array $ent
*/
protected static function saveJoins(AfformSubmitEvent $event, $index, $entityId, $joins) {
$mainEntity = $event->getFormDataModel()->getEntity($event->getEntityName());
foreach ($joins as $joinEntityName => $join) {
$values = self::filterEmptyJoins($mainEntity, $joinEntityName, $join);
foreach ($joins as $joinEntityName => $joinValues) {
$values = self::filterEmptyJoins($mainEntity, $joinEntityName, $joinValues);
$whereClause = self::getJoinWhereClause($event->getFormDataModel(), $event->getEntityName(), $joinEntityName, $entityId);
$mainIdField = CoreUtil::getIdFieldName($mainEntity['type']);
$joinIdField = CoreUtil::getIdFieldName($joinEntityName);
$joinAllowedAction = self::getJoinAllowedAction($mainEntity, $joinEntityName);

// Forward FK e.g. Event.loc_block_id => LocBlock
$forwardFkField = self::getFkField($mainEntity['type'], $joinEntityName);
if ($forwardFkField && $values) {
// Add id to values for update op, but only if id is not already on the form
if ($whereClause && empty($mainEntity['joins'][$joinEntityName]['fields'][$joinIdField])) {
if ($whereClause && $joinAllowedAction['update'] && empty($mainEntity['joins'][$joinEntityName]['fields'][$joinIdField])) {
$values[0][$joinIdField] = $whereClause[0][2];
}
$result = civicrm_api4($joinEntityName, 'save', [
Expand All @@ -514,21 +515,44 @@ protected static function saveJoins(AfformSubmitEvent $event, $index, $entityId,
}

// Reverse FK e.g. Contact <= Email.contact_id
// TODO: REPLACE works for creating or updating contacts, but different logic would be needed if
// the contact was being auto-updated via a dedupe rule; in that case we would not want to
// delete any existing records.
elseif ($values) {
$result = civicrm_api4($joinEntityName, 'replace', [
// Disable permission checks because the main entity has already been vetted
'checkPermissions' => FALSE,
'where' => $whereClause,
'records' => $values,
]);
// In update mode, set ids of existing values
if ($joinAllowedAction['update']) {
$existingJoinValues = $event->getApiRequest()->loadJoins($joinEntityName, $mainEntity, $entityId, $index);
foreach ($existingJoinValues as $joinIndex => $existingJoin) {
if (!empty($existingJoin[$joinIdField]) && !empty($values[$joinIndex])) {
$values[$joinIndex][$joinIdField] = $existingJoin[$joinIdField];
}
}
}
else {
foreach ($values as $key => $value) {
unset($values[$key][$joinIdField]);
}
}
// Use REPLACE action if update+delete are both allowed (only need to check for 'delete' as it implies 'update')
if ($joinAllowedAction['delete']) {
$result = civicrm_api4($joinEntityName, 'replace', [
// Disable permission checks because the main entity has already been vetted
'checkPermissions' => FALSE,
'where' => $whereClause,
'records' => $values,
]);
}
else {
$fkField = self::getFkField($joinEntityName, $mainEntity['type']);
$result = civicrm_api4($joinEntityName, 'save', [
// Disable permission checks because the main entity has already been vetted
'checkPermissions' => FALSE,
'defaults' => [$fkField['name'] => $entityId],
'records' => $values,
]);
}
$indexedResult = array_combine(array_keys($values), (array) $result);
$event->setJoinIds($index, $joinEntityName, $indexedResult);
}
// REPLACE doesn't work if there are no records, have to use DELETE
else {
elseif ($joinAllowedAction['delete']) {
try {
civicrm_api4($joinEntityName, 'delete', [
// Disable permission checks because the main entity has already been vetted
Expand Down
Loading