diff --git a/modules/contacts/hm-contacts.php b/modules/contacts/hm-contacts.php index 825395572..f7ee78d29 100644 --- a/modules/contacts/hm-contacts.php +++ b/modules/contacts/hm-contacts.php @@ -142,13 +142,66 @@ public function reset() { $this->data = array(); } - public function page($page, $size) { + public function page($page, $size, $data = null) { if ($page < 1) { return array(); } - return array_slice($this->data, (($page - 1)*$size), $size, true); + if ($data === null) { + $data = $this->data; + } + return array_slice($data, (($page - 1)*$size), $size, true); } + public function group_by($column = 'group') { + if (!is_string($column) && !is_int($column) && !is_float($column) && !is_callable($column) ) { + trigger_error('group_by(): The key should be a string, an integer, or a callback', E_USER_ERROR); + return null; + } + $func = (!is_string($column) && is_callable($column) ? $column : null); + $_key = $column; + // Load the new array, splitting by the target key + $grouped = []; + foreach ($this->data as $value) { + + $column = null; + if (is_callable($func)) { + $column = call_user_func($func, $value); + } elseif (is_object($value)) { + $reflection = new ReflectionClass($value); + $property = $reflection->getProperty('data'); + $property->setAccessible(true); // Make the private property accessible + // Get the value of the data property + $contact = $property->getValue($value); + $column = isset($contact[$_key]) ? $contact[$_key] : 'Personal Addresses'; + } elseif (isset($value[$_key])) { + $column = $value[$_key]; + } + if ($column === null) { + continue; + } + $grouped[$column][] = $value; + } + // Recursively build a nested grouping if more parameters are supplied + // Each grouped array value is grouped according to the next sequential key + if (func_num_args() > 2) { + $args = func_get_args(); + foreach ($grouped as $key => $value) { + $params = array_merge([ $value ], array_slice($args, 2, func_num_args())); + $grouped[$key] = call_user_func_array('group_by', $params); + } + } + return $grouped; + } + + public function paginate_grouped($column, $page, $size) { + $grouped = $this->group_by($column); + $paginated = []; + foreach ($grouped as $key => $group) { + $paginated[$key] = $this->page($page, $size, $group); + } + return $paginated; + } + public function sort($fld) { $this->sort_fld = $fld; uasort($this->data, array($this, 'sort_callback')); diff --git a/modules/contacts/modules.php b/modules/contacts/modules.php index 63f7b8de0..7d5ff48b9 100644 --- a/modules/contacts/modules.php +++ b/modules/contacts/modules.php @@ -308,20 +308,15 @@ protected function output() { $res = '
'; $tabIndex = 1; $contactGroups = []; - if ($contacts) { - foreach ($contacts->page($current_page, $per_page) as $id => $contact) { - $group = $contact->value('group'); - if (!$group || empty($group)) { - $group = 'Personal Addresses'; // Set the group to "Personal Addresses" when it's null or empty - } - if (!array_key_exists($group, $contactGroups)) { - $contactGroups[$group] = []; + foreach ($contacts->paginate_grouped('group', $current_page, $per_page) as $key => $contact) { + if (!array_key_exists($key, $contactGroups)) { + $contactGroups[$key] = []; } - $contactGroups[$group][] = $contact; + $contactGroups[$key][] = $contact; } } - + foreach ($contactGroups as $group => $groupContacts) { $res .= ''; $res .= ''; @@ -332,54 +327,55 @@ protected function output() { $res .= '