/**
  * @param Repository $repository
  * @param $contentTypeName
  * @param $recordId
  * @param array $options currently only one option available: levels = array containing menu levels, that should get expanded
  * @return Record[]
  */
 public static function getExpandedMenu(Repository $repository, $contentTypeName, $recordId, $options = [])
 {
     $path = self::getBreadcrumb($repository, $contentTypeName, $recordId);
     $repository->selectContentType($contentTypeName);
     $result = [];
     // Add all same level pages within path
     $i = 0;
     /**
      * @var int $id
      * @var Record $record
      */
     foreach ($path as $id => $record) {
         $i++;
         $expand = true;
         if (array_key_exists('levels', $options)) {
             $expand = false;
             if (in_array($i, $options['levels'])) {
                 $expand = true;
             }
         }
         if ($expand) {
             $records = $repository->getSortedRecords($record->getParent(), false, 1);
             foreach ($records as $record) {
                 $result[$record->getId()] = $record;
             }
         } else {
             $result[$record->getId()] = $record;
         }
     }
     // Add children
     $records = $repository->getSortedRecords($id, false, 1);
     foreach ($records as $record) {
         $result[$record->getId()] = $record;
     }
     foreach ($result as $record) {
         $record->setRepository($repository);
     }
     return RecordsSorter::sortRecords($result);
 }
 public function getSortedRecords($parentId, $includeParent = false, $depth = null, $height = 0)
 {
     $records = $this->getRecords();
     return RecordsSorter::sortRecords($records, $parentId, $includeParent, $depth, $height);
 }
 public function testSortingTree()
 {
     //        A
     //     B    C
     //   D  E     F
     //          G   H
     //
     $cmdl = 'name
     @sortable';
     $records = [];
     $records[1] = RecordFactory::instance()->createRecordFromCMDL($cmdl, ['name' => 'A', 'position' => 1, 'parent' => 0])->setId(1);
     $records[2] = RecordFactory::instance()->createRecordFromCMDL($cmdl, ['name' => 'B', 'position' => 1, 'parent' => 1])->setId(2);
     $records[3] = RecordFactory::instance()->createRecordFromCMDL($cmdl, ['name' => 'C', 'position' => 1, 'parent' => 1])->setId(3);
     $records[4] = RecordFactory::instance()->createRecordFromCMDL($cmdl, ['name' => 'D', 'position' => 1, 'parent' => 2])->setId(4);
     $records[5] = RecordFactory::instance()->createRecordFromCMDL($cmdl, ['name' => 'E', 'position' => 2, 'parent' => 2])->setId(5);
     $records[6] = RecordFactory::instance()->createRecordFromCMDL($cmdl, ['name' => 'F', 'position' => 1, 'parent' => 3])->setId(6);
     $records[7] = RecordFactory::instance()->createRecordFromCMDL($cmdl, ['name' => 'G', 'position' => 1, 'parent' => 6])->setId(7);
     $records[8] = RecordFactory::instance()->createRecordFromCMDL($cmdl, ['name' => 'H', 'position' => 2, 'parent' => 6])->setId(8);
     $subset = RecordsSorter::sortRecords($records);
     $this->assertEquals([1, 2, 4, 5, 3, 6, 7, 8], array_keys($subset));
     $subset = RecordsSorter::sortRecords($records, 0, false, 1);
     $this->assertEquals([1], array_keys($subset));
     $subset = RecordsSorter::sortRecords($records, 0, false, 2);
     $this->assertEquals([1, 2, 3], array_keys($subset));
     $subset = RecordsSorter::sortRecords($records, 2, false, 1);
     // B
     $this->assertEquals([4, 5], array_keys($subset));
     $subset = RecordsSorter::sortRecords($records, 3, false, 1);
     // C
     $this->assertEquals([6], array_keys($subset));
     $subset = RecordsSorter::sortRecords($records, 3, false, 2);
     // C
     $this->assertEquals([6, 7, 8], array_keys($subset));
     $subset = RecordsSorter::sortRecords($records, 0, true, 1);
     $this->assertEquals([1], array_keys($subset));
     $subset = RecordsSorter::sortRecords($records, 2, true, 1);
     // B
     $this->assertEquals([2, 4, 5], array_keys($subset));
 }