Skip to content

Commit

Permalink
Afform - Rename blocks and joins for clarity
Browse files Browse the repository at this point in the history
Renames the ambiguously named "block" and "join" afform properties,
and changes the naming convention to "af{type}-title-munged".
  • Loading branch information
colemanw committed Aug 23, 2021
1 parent 994b284 commit 461bcb7
Show file tree
Hide file tree
Showing 26 changed files with 500 additions and 47 deletions.
14 changes: 7 additions & 7 deletions ext/afform/admin/Civi/Api4/Action/Afform/LoadAdminData.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function _run(\Civi\Api4\Generic\Result $result) {
case 'block':
$info['definition'] = $this->definition + [
'title' => '',
'block' => $this->entity,
'entity_type' => $this->entity,
'layout' => [],
];
break;
Expand Down Expand Up @@ -123,8 +123,8 @@ public function _run(\Civi\Api4\Generic\Result $result) {
if ($embeddedForm['type'] === 'block') {
$info['blocks'][$blockTag] = $embeddedForm;
}
if (!empty($embeddedForm['join'])) {
$entities = array_unique(array_merge($entities, [$embeddedForm['join']]));
if (!empty($embeddedForm['join_entity'])) {
$entities = array_unique(array_merge($entities, [$embeddedForm['join_entity']]));
}
$scanBlocks($embeddedForm['layout']);
}
Expand Down Expand Up @@ -152,7 +152,7 @@ public function _run(\Civi\Api4\Generic\Result $result) {
}

if ($info['definition']['type'] === 'block') {
$blockEntity = $info['definition']['join'] ?? $info['definition']['block'];
$blockEntity = $info['definition']['join_entity'] ?? $info['definition']['entity_type'];
if ($blockEntity !== '*') {
$entities[] = $blockEntity;
}
Expand Down Expand Up @@ -191,7 +191,7 @@ public function _run(\Civi\Api4\Generic\Result $result) {
if (!$newForm) {
$scanBlocks($info['definition']['layout']);
}
$this->loadAvailableBlocks($entities, $info, [['join', 'IS NULL']]);
$this->loadAvailableBlocks($entities, $info, [['join_entity', 'IS NULL']]);
}

// Optimization - since contact fields are a combination of these three,
Expand Down Expand Up @@ -237,10 +237,10 @@ private function loadAvailableBlocks($entities, &$info, $where = []) {
}
if ($entities) {
$blockInfo = Afform::get($this->checkPermissions)
->addSelect('name', 'title', 'block', 'join', 'directive_name', 'repeat')
->addSelect('name', 'title', 'entity_type', 'join_entity', 'directive_name', 'repeat')
->setWhere($where)
->addWhere('type', '=', 'block')
->addWhere('block', 'IN', $entities)
->addWhere('entity_type', 'IN', $entities)
->addWhere('directive_name', 'NOT IN', array_keys($info['blocks']))
->execute();
$info['blocks'] = array_merge(array_values($info['blocks']), (array) $blockInfo);
Expand Down
2 changes: 1 addition & 1 deletion ext/afform/admin/ang/afGuiEditor/afGuiEditor.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

else if (editor.getFormType() === 'block') {
editor.layout['#children'] = editor.afform.layout;
editor.blockEntity = editor.afform.join || editor.afform.block;
editor.blockEntity = editor.afform.join_entity || editor.afform.entity_type;
$scope.entities[editor.blockEntity] = backfillEntityDefaults({
type: editor.blockEntity,
name: editor.blockEntity,
Expand Down
8 changes: 4 additions & 4 deletions ext/afform/admin/ang/afGuiEditor/afGuiEntity.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@
$scope.blockTitles.length = 0;
_.each(afGui.meta.blocks, function(block, directive) {
if ((!search || _.contains(directive, search) || _.contains(block.name.toLowerCase(), search) || _.contains(block.title.toLowerCase(), search)) &&
(block.block === '*' || block.block === ctrl.entity.type || (ctrl.entity.type === 'Contact' && block.block === ctrl.entity.data.contact_type)) &&
(block.entity_type === '*' || block.entity_type === ctrl.entity.type || (ctrl.entity.type === 'Contact' && block.entity_type === ctrl.entity.data.contact_type)) &&
block.name !== ctrl.editor.getAfform().name
) {
var item = {"#tag": block.join ? "div" : directive};
if (block.join) {
item['af-join'] = block.join;
var item = {"#tag": block.join_entity ? "div" : directive};
if (block.join_entity) {
item['af-join'] = block.join_entity;
item['#children'] = [{"#tag": directive}];
}
if (block.repeat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
};

_.each(afGui.meta.blocks, function(blockInfo, directive) {
if (directive === ctrl.node['#tag'] || (blockInfo.join && blockInfo.join === ctrl.getFieldEntityType())) {
if (directive === ctrl.node['#tag'] || (blockInfo.join_entity && blockInfo.join_entity === ctrl.getFieldEntityType())) {
block.options.push({
id: directive,
text: blockInfo.title
Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/CRM/Afform/AfformScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private function appendFilePaths(&$formPaths, $parent, $priority) {
* @return mixed|string
* Ex: '/var/www/sites/default/files/civicrm/afform'.
*/
private function getSiteLocalPath() {
public function getSiteLocalPath() {
// TODO Allow a setting override.
// return Civi::paths()->getPath(Civi::settings()->get('afformPath'));
return Civi::paths()->getPath('[civicrm.files]/ang');
Expand Down
53 changes: 53 additions & 0 deletions ext/afform/core/CRM/Afform/Upgrader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
use CRM_Afform_ExtensionUtil as E;

/**
* Collection of upgrade steps.
*/
class CRM_Afform_Upgrader extends CRM_Afform_Upgrader_Base {

/**
* Alter names of blocks and joins
*
* @return TRUE on success
* @throws Exception
*/
public function upgrade_1000() {
$this->ctx->log->info('Applying update 1000');
$scanner = new CRM_Afform_AfformScanner();
$localDir = $scanner->getSiteLocalPath();

$replacements = [
'afjoin-address-default>' => 'afblock-contact-address>',
'afjoin-email-default>' => 'afblock-contact-email>',
'afjoin-i-m-default>' => 'afblock-contact-i-m>',
'afjoin-phone-default>' => 'afblock-contact-phone>',
'afjoin-website-default>' => 'afblock-contact-website>',
'afjoin-custom-' => 'afblock-custom-',
];

foreach (glob("$localDir/*." . $scanner::LAYOUT_FILE) as $fileName) {
$html = file_get_contents($fileName);
$html = str_replace(array_keys($replacements), array_values($replacements), $html);
file_put_contents($fileName, $html);
}

foreach (glob("$localDir/*." . $scanner::METADATA_FILE) as $fileName) {
$meta = json_decode(file_get_contents($fileName), TRUE);
$newKeys = ['join' => 'join_entity', 'block' => 'entity_type'];
foreach ($newKeys as $oldKey => $newKey) {
if (isset($meta[$oldKey])) {
$meta[$newKey] = $meta[$oldKey];
unset($meta[$oldKey]);
}
}
if (!empty($meta['entity_type'])) {
$meta['type'] = 'block';
}
file_put_contents($fileName, json_encode($meta, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

return TRUE;
}

}
Loading

0 comments on commit 461bcb7

Please sign in to comment.