-
-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework the listeners so that they are called on the basis of each ent…
…itiy rather than just once. Also extract weight function out and make improvements following Coleman's review and add tests
- Loading branch information
1 parent
6a03450
commit 0ee3c91
Showing
4 changed files
with
158 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?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 | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
/** | ||
* | ||
* @package CRM | ||
* @copyright CiviCRM LLC https://civicrm.org/licensing | ||
*/ | ||
class CRM_Afform_Utils { | ||
|
||
public static function getEntityWeights($formEntities, $entityValues) { | ||
$entityWeights = $entityMapping = $entitiesToBeProcessed = []; | ||
foreach ($formEntities as $entityName => $entity) { | ||
$entityWeights[$entityName] = 1; | ||
$entityMapping[$entityName] = $entity['type']; | ||
foreach ($entityValues[$entity['type']][$entityName] as $index => $vals) { | ||
foreach ($vals as $field => $value) { | ||
if (array_key_exists($value, $entityWeights)) { | ||
$entityWeights[$entityName] = max((int) $entityWeights[$entityName], (int) ($entityWeights[$value] + 1)); | ||
} | ||
else { | ||
if (!array_key_exists($value, $entitiesToBeProcessed)) { | ||
$entitiesToBeProcessed[$value] = [$entityName]; | ||
} | ||
else { | ||
$entitiesToBeProcessed[$value][] = $entityName; | ||
} | ||
} | ||
} | ||
} | ||
// If any other entities have been processed that relied on this entity lets now alter their weights based on this entity's weight. | ||
if (array_key_exists($entityName, $entitiesToBeProcessed)) { | ||
foreach ($entitiesToBeProcessed[$entityName] as $dependentEntity) { | ||
$entityWeights[$dependentEntity] = max((int) $entityWeights[$dependentEntity], (int) ($entityWeights[$entityName] + 1)); | ||
} | ||
} | ||
} | ||
// Numerically sort the weights now that we have them set | ||
asort($entityWeights); | ||
return $entityWeights; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters