| Current Path : /var/www/cesa.co.za/php/includes/ |
| Current File : /var/www/cesa.co.za/php/includes/CCEStudentDisplayOrder.php |
<?php
/**
* Standard CCEStudent list ordering: KnownAs, else FirstName, then Surname.
*/
final class CCEStudentDisplayOrder
{
/**
* @param string $tableAlias SQL table alias (letters, numbers, underscore only)
*/
public static function sqlOrderByDisplayName($tableAlias = 'CCEStudents'): string
{
$alias = preg_replace('/[^A-Za-z0-9_]/', '', $tableAlias);
if ($alias === '') {
$alias = 'CCEStudents';
}
return 'CASE WHEN ' . $alias . '.KnownAs IS NULL OR TRIM(' . $alias . '.KnownAs) = \'\''
. ' THEN ' . $alias . '.FirstName ELSE ' . $alias . '.KnownAs END, '
. $alias . '.Surname';
}
public static function sqlOrderByDisplayNameLookup(): string
{
return 'CASE WHEN `KnownAs` IS NULL OR TRIM(`KnownAs`) = \'\' THEN `FirstName` ELSE `KnownAs` END, `Surname`';
}
/**
* @param string $tableAlias
* @param string $direction Asc|Desc|ASC|DESC
*/
public static function sqlOrderByFragment($tableAlias = 'CCEStudents', $direction = 'ASC'): string
{
return ' ORDER BY ' . self::sqlOrderByDisplayName($tableAlias) . ' ' . self::normaliseDirection($direction);
}
/**
* For PHPMaker CCEStudents lookup SQL fragments.
*
* @param string $direction Asc|Desc|ASC|DESC or empty
*/
public static function sqlOrderByLookupFragment($direction = 'ASC'): string
{
$dir = self::normaliseDirection($direction);
return ' ORDER BY ' . self::sqlOrderByDisplayNameLookup() . ($dir !== '' ? ' ' . $dir : '');
}
/**
* @param array<string, mixed> $row
*/
public static function getSortPrimaryName(array $row, string $knownAsKey = 'KnownAs', string $firstNameKey = 'FirstName'): string
{
$knownAs = trim((string) ($row[$knownAsKey] ?? ''));
if ($knownAs !== '') {
return $knownAs;
}
return trim((string) ($row[$firstNameKey] ?? ''));
}
/**
* @param array<string, mixed> $row
*/
public static function getSortSurname(array $row, string $surnameKey = 'Surname'): string
{
return trim((string) ($row[$surnameKey] ?? ''));
}
/**
* @param array<int, array<string, mixed>> $rows
*/
public static function sortRows(
array &$rows,
string $knownAsKey = 'KnownAs',
string $firstNameKey = 'FirstName',
string $surnameKey = 'Surname'
): void {
if (count($rows) <= 1) {
return;
}
usort(
$rows,
static function (array $left, array $right) use ($knownAsKey, $firstNameKey, $surnameKey): int {
return self::compareRows($left, $right, $knownAsKey, $firstNameKey, $surnameKey);
}
);
}
/**
* @param array<string, mixed> $left
* @param array<string, mixed> $right
*/
public static function compareRows(
array $left,
array $right,
string $knownAsKey = 'KnownAs',
string $firstNameKey = 'FirstName',
string $surnameKey = 'Surname'
): int {
$cmp = strcasecmp(
self::getSortPrimaryName($left, $knownAsKey, $firstNameKey),
self::getSortPrimaryName($right, $knownAsKey, $firstNameKey)
);
if ($cmp !== 0) {
return $cmp;
}
return strcasecmp(self::getSortSurname($left, $surnameKey), self::getSortSurname($right, $surnameKey));
}
/**
* @param object $left
* @param object $right
*/
public static function compareObjects($left, $right): int
{
$cmp = strcasecmp(self::getObjectSortPrimaryName($left), self::getObjectSortPrimaryName($right));
if ($cmp !== 0) {
return $cmp;
}
return strcasecmp(self::getObjectSortSurname($left), self::getObjectSortSurname($right));
}
/**
* @param object $object
*/
public static function getObjectSortPrimaryName($object): string
{
if (is_object($object) && method_exists($object, 'getKnownAs')) {
$knownAs = trim((string) $object->getKnownAs());
if ($knownAs !== '') {
return $knownAs;
}
}
if (is_object($object) && method_exists($object, 'getFirstName')) {
return trim((string) $object->getFirstName());
}
return '';
}
/**
* @param object $object
*/
public static function getObjectSortSurname($object): string
{
if (is_object($object) && method_exists($object, 'getSurname')) {
return trim((string) $object->getSurname());
}
return '';
}
private static function normaliseDirection($direction): string
{
$direction = strtoupper(trim((string) $direction));
if ($direction === 'DESC' || $direction === 'ASC') {
return $direction;
}
if (stripos((string) $direction, 'DESC') !== false) {
return 'DESC';
}
return 'ASC';
}
}