You are here

public function RulesContainerPlugin::sortChildren in Rules 7.2

Sorts all child elements by their weight.

Parameters

bool $deep: If enabled a deep sort is performed, thus the whole element tree below this element is sorted.

1 call to RulesContainerPlugin::sortChildren()
Rule::sortChildren in includes/rules.plugins.inc
Sorts all child elements by their weight.
1 method overrides RulesContainerPlugin::sortChildren()
Rule::sortChildren in includes/rules.plugins.inc
Sorts all child elements by their weight.

File

includes/rules.core.inc, line 2399
Rules base classes and interfaces needed for any rule evaluation.

Class

RulesContainerPlugin
Base class for ContainerPlugins like Rules, Logical Operations or Loops.

Code

public function sortChildren($deep = FALSE) {

  // Make sure the array order is kept in case two children have the same
  // weight by ensuring later children would have higher weights.
  foreach (array_values($this->children) as $i => $child) {
    $child->weight += $i / 1000;
  }
  usort($this->children, array(
    'RulesPlugin',
    'compare',
  ));

  // Fix up the weights afterwards to be unique integers.
  foreach (array_values($this->children) as $i => $child) {
    $child->weight = $i;
  }
  if ($deep) {
    foreach (new ParentIterator($this
      ->getIterator()) as $child) {
      $child
        ->sortChildren(TRUE);
    }
  }
  $this
    ->resetInternalCache();
}