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 = '
'.$this->trans('Contacts').' | ||||||
'. - ' '. - ' | '.$this->html_safe($contact->value('type')).' | '. - ($contact->value('source') == 'local' ? '' : $this->html_safe($contact->value('source'))).''. - ' | ' . $this->html_safe($name) . ' | ' . - ''.$this->html_safe($contact->value('email_address')).' | '.
- ''. - $this->html_safe($contact->value('phone_number')).' | '. - ''; - if (in_array($contact->value('type').':'.$contact->value('source'), $editable, true)) { - $res .= ''. - ''; + foreach ($contact as $c) { + $name = $c->value('display_name'); + if (!trim($name)) { + $name = $c->value('fn'); + } + + $res .= ' |
'. + ' '. + ' | '.$this->html_safe($c->value('type')).' | '. + ($c->value('source') == 'local' ? '' : $this->html_safe($c->value('source'))).''. + ' | ' . $this->html_safe($name) . ' | ' . + ''.$this->html_safe($c->value('email_address')).' | '.
+ ''. + $this->html_safe($c->value('phone_number')).' | '. + ''; + if (in_array($c->value('type').':'.$c->value('source'), $editable, true)) { + $res .= ''. + ''; + } + $res .= ''. + ''; + + $res .= ' |
'; + $res .= build_contact_detail($this, $c, $c->value('id')).' | '; + $res .= '||||||
'; - $res .= build_contact_detail($this, $contact, $id).' | '; - $res .= '||||||
'; $contactsPerPage = $per_page; - $totalContacts = count($contacts->dump()); + $totalContacts = count($contact); $totalPages = ceil($totalContacts / $contactsPerPage); $currentPage = $current_page; - if ($currentPage > 1) { $res .= 'Previous'; } - if ($currentPage < $totalPages) { + if ($currentPage <= $totalPages) { $res .= ' Next'; } $res .= ' |