Index: branches/5.2.x/core/units/categories/categories_event_handler.php =================================================================== diff -u -N -r16692 -r16713 --- branches/5.2.x/core/units/categories/categories_event_handler.php (.../categories_event_handler.php) (revision 16692) +++ branches/5.2.x/core/units/categories/categories_event_handler.php (.../categories_event_handler.php) (revision 16713) @@ -1,6 +1,6 @@ sortByDependencies($files); kUtil::setResourceLimit(); /** @var CategoriesItem $dummy */ @@ -2472,6 +2473,44 @@ } /** + * 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'] : ''; + $page_name = isset($meta['name']) ? $meta['name'] : '_Auto: ' . $template; + $files[$template]['sort_key'] = substr_count(($section ? $section . '||' : '') . $page_name, '||'); + } + + uasort($files, array($this, 'compareSMSTemplates')); + + return $files; + } + + /** + * Files array comparing. + * + * @param array $sms_template_a First item. + * @param array $sms_template_b Second item. + * + * @return integer + */ + public function compareSMSTemplates(array $sms_template_a, array $sms_template_b) + { + if ( $sms_template_a['sort_key'] == $sms_template_b['sort_key'] ) { + return 0; + } + + return $sms_template_a['sort_key'] < $sms_template_b['sort_key'] ? -1 : 1; + } + + /** * Processes OnMassMoveUp, OnMassMoveDown events * * @param kEvent $event