Index: core/units/categories/categories_event_handler.php =================================================================== --- core/units/categories/categories_event_handler.php +++ core/units/categories/categories_event_handler.php @@ -2359,6 +2359,7 @@ return; } + $files = $this->sortByDependencies($files); kUtil::setResourceLimit(); $dummy = $this->Application->recallObject($event->Prefix . '.rebuild', NULL, Array ('skip_autoload' => true)); @@ -2388,6 +2389,48 @@ } /** + * Sort structure files array by dependencies. + * + * @param array $files Files. + * + * @return array + */ + protected function sortByDependencies(array $files) + { + foreach ( $files as $template => $data ) { + $meta = unserialize($data['FileMetaInfo']); + $section = isset($meta['section']) && $meta['section'] ? array($meta['section']) : array(); + $section[] = isset($meta['name']) ? $meta['name'] : '_Auto: ' . $template; + $files[$template]['sort_key'] = implode('||', $section); + } + + uasort($files, array($this, 'sortCompare')); + + return $files; + } + + /** + * Files array comparing/ + * + * @param array $a First item. + * @param array $b Second item. + * + * @return integer + */ + public function sortCompare(array $a, array $b) + { + if ( $a['sort_key'] < $b['sort_key'] ) { + return -1; + } + + if ( $a['sort_key'] > $b['sort_key'] ) { + return 1; + } + + return 0; + } + + /** * Processes OnMassMoveUp, OnMassMoveDown events * * @param kEvent $event