Skip to content

Commit

Permalink
Correct pagination while displaying contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed Jul 19, 2024
1 parent 4095dad commit 5514731
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
59 changes: 56 additions & 3 deletions modules/contacts/hm-contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,71 @@ 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'));
}

public function sort_callback($a, $b) {
return strcmp($a->value($this->sort_fld), $b->value($this->sort_fld));
}
Expand Down
17 changes: 7 additions & 10 deletions modules/contacts/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,20 +308,15 @@ protected function output() {
$res = '<div class="contact-group contact-group-effect-scale contact-group-theme-1">';
$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 .= '<input type="radio" name="contact-group" ' . ($tabIndex === 1 ? 'checked ' : '') . 'id="tab' . $tabIndex . '" class="' . ($tabIndex === 1 ? 'tab-content-first' : 'tab-content-' . $tabIndex) . '">';
$res .= '<label for="tab' . $tabIndex . '">' . $this->html_safe($group) . '</label>';
Expand All @@ -332,6 +327,8 @@ protected function output() {
$res .= '<ul>';

foreach ($contactGroups as $group => $groupContacts) {
exit(var_dump($group));

$res .= '<li class="tab-content '.($tabIndex === 1 ? 'tab-content-first' : 'tab-content-'.$tabIndex).' typography">';
$res .= '<table class="contact_list">';
$res .= '<tr><td colspan="7" class="contact_list_title"><div class="server_title">'.$this->trans('Contacts').'</div></td></tr>';
Expand Down

0 comments on commit 5514731

Please sign in to comment.