Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / advagg / advagg_bundler / advagg_bundler.advagg.inc
1 <?php
2
3 /**
4  * @file
5  * Advanced aggregation bundler module.
6  */
7
8 /**
9  * Implements hook_advagg_aggregate_grouping_alter().
10  */
11 function advagg_bundler_advagg_aggregate_grouping_alter(&$groups, $type) {
12   // If bundler is disabled then do not do any more processing.
13   if (!advagg_bundler_enabled($type)) {
14     return;
15   }
16
17   $config = \Drupal::config('advagg_bundler.settings');
18   $max = $config->get('max_' . $type);
19   $logic = $config->get($type . '_logic');
20   if ($logic == 1) {
21     $file_info = \Drupal::service('state.advagg.files');
22   }
23   $modifiable = [];
24   $counter = 0;
25   foreach ($groups as $key => $group) {
26     if (isset($group['type'], $group['preprocess']) && $group['type'] == 'file' && $group['preprocess']) {
27       $modifiable[$key] = $group;
28       $modifiable[$key]['order'] = $key;
29       if ($logic == 0) {
30         $modifiable[$key]['counter'] = count($group['items']);
31         $counter += count($group['items']);
32       }
33       elseif ($logic == 1) {
34         $file_info = \Drupal::service('state.advagg.files');
35         $modifiable[$key]['counter'] = 0;
36         foreach ($modifiable[$key]['items'] as &$asset) {
37           $asset['counter'] = $file_info->get($asset['data'])['filesize'];
38           $counter += $asset['counter'];
39           $modifiable[$key]['counter'] += $asset['counter'];
40         }
41       }
42     }
43   }
44
45   // If more bundles than $max return. $groups is already set to least
46   // possible number of groups with current sort. Enabling sort external first
47   // may help decrease number of bundles.
48   if (count($modifiable) > $max || !$modifiable) {
49     return;
50   }
51
52   $target_count = ceil($counter / $max);
53   $final_groups = [];
54   $bundles = 1;
55   foreach ($groups as $key => $group) {
56     if (!isset($modifiable[$key]) || $bundles == $max) {
57       $final_groups[] = $group;
58       continue;
59     }
60     $splits = round($modifiable[$key]['counter'] / $target_count);
61     if ($splits < 2) {
62       $final_groups[] = $group;
63       $bundles++;
64       continue;
65     }
66     if ($logic == 0) {
67       $chunks = array_chunk($modifiable[$key]['items'], $target_count);
68     }
69     elseif ($logic == 1) {
70       $target_size = $modifiable[$key]['counter'] / $splits;
71       $chunks = [0 => []];
72       $i = 0;
73       $current_chunk_size = 0;
74       foreach ($modifiable[$key]['items'] as $asset) {
75         $current_chunk_size += $asset['counter'];
76         $chunks[$i][] = $asset;
77         if ($current_chunk_size > $target_size) {
78           $i++;
79           $chunks[$i] = [];
80           $current_chunk_size = 0;
81         }
82       }
83     }
84     foreach ($chunks as $chunk) {
85       if (!$chunk) {
86         continue;
87       }
88       $group['items'] = $chunk;
89       $final_groups[] = $group;
90       $bundles++;
91     }
92   }
93
94   $groups = $final_groups;
95 }