2017-09-15 14:36:37 +03:00
|
|
|
<?php
|
2016-07-09 10:02:25 +03:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2020-03-11 12:10:59 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
|
2016-07-09 10:02:25 +03:00
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.2.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
namespace EA\Engine\Api\V1\Processors;
|
2016-07-09 10:02:25 +03:00
|
|
|
|
2016-07-10 11:59:39 +03:00
|
|
|
/**
|
|
|
|
* Sort Processor
|
|
|
|
*
|
2017-09-15 14:36:37 +03:00
|
|
|
* This class will sort the response array with the provided GET parameters. Make sure that the
|
2016-07-10 11:59:39 +03:00
|
|
|
* response parameter is a sequential array and not a single entry by the time this processor is
|
|
|
|
* executed.
|
2020-05-12 21:49:15 +03:00
|
|
|
*
|
|
|
|
* @deprecated
|
2016-07-10 11:59:39 +03:00
|
|
|
*/
|
2016-07-09 14:14:08 +03:00
|
|
|
class Sort implements ProcessorsInterface {
|
2016-07-10 00:03:27 +03:00
|
|
|
/**
|
2016-07-10 11:59:39 +03:00
|
|
|
* Supports up to 3 columns for sorting.
|
|
|
|
*
|
2017-09-15 14:36:37 +03:00
|
|
|
* Example:
|
2016-07-10 11:59:39 +03:00
|
|
|
* http://ea-installation.com/api/v1/appointments?sort=-start,+notes,-hash
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2016-07-10 00:03:27 +03:00
|
|
|
* @param array &$response The response array to be processed.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public static function process(array &$response)
|
|
|
|
{
|
|
|
|
if ( ! isset($_GET['sort']) || empty($response))
|
|
|
|
{
|
|
|
|
return;
|
2016-07-10 00:03:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$sort = explode(',', (string)$_GET['sort']);
|
|
|
|
|
2020-11-16 12:54:59 +03:00
|
|
|
$sort_direction1 = substr($sort[0], 0, 1) === '-' ? SORT_DESC : SORT_ASC;
|
2016-07-10 00:03:27 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if (isset($sort[1]))
|
|
|
|
{
|
2020-11-16 12:54:59 +03:00
|
|
|
$sort_direction2 = substr($sort[1], 0, 1) === '-' ? SORT_DESC : SORT_ASC;
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-11-16 12:54:59 +03:00
|
|
|
$sort_direction2 = NULL;
|
2016-07-10 00:03:27 +03:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if (isset($sort[2]))
|
|
|
|
{
|
2020-11-16 12:54:59 +03:00
|
|
|
$sort_direction3 = substr($sort[2], 0, 1) === '-' ? SORT_DESC : SORT_ASC;
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-11-16 12:54:59 +03:00
|
|
|
$sort_direction3 = NULL;
|
2016-07-10 00:03:27 +03:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
foreach ($response as $index => $entry)
|
|
|
|
{
|
2020-11-16 12:54:59 +03:00
|
|
|
$sort_order1[$index] = $entry[substr($sort[0], 1)];
|
2016-07-10 00:03:27 +03:00
|
|
|
|
2020-11-16 12:54:59 +03:00
|
|
|
if ($sort_direction2)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-11-16 12:54:59 +03:00
|
|
|
$sort_order2[$index] = $entry[substr($sort[1], 1)];
|
2016-07-10 00:03:27 +03:00
|
|
|
}
|
|
|
|
|
2020-11-16 12:54:59 +03:00
|
|
|
if ($sort_direction3)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-11-16 12:54:59 +03:00
|
|
|
$sort_order3[$index] = $entry[substr($sort[2], 1)];
|
2016-07-10 00:03:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$arguments = [
|
2020-11-16 12:54:59 +03:00
|
|
|
&$sort_order1,
|
|
|
|
&$sort_direction1
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2016-07-10 00:03:27 +03:00
|
|
|
|
2020-11-16 12:54:59 +03:00
|
|
|
if ($sort_direction2)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-11-16 12:54:59 +03:00
|
|
|
$arguments[] = $sort_order2;
|
|
|
|
$arguments[] = $sort_direction2;
|
2016-07-10 00:03:27 +03:00
|
|
|
}
|
|
|
|
|
2020-11-16 12:54:59 +03:00
|
|
|
if ($sort_direction3)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-11-16 12:54:59 +03:00
|
|
|
$arguments[] = $sort_order3;
|
|
|
|
$arguments[] = $sort_direction3;
|
2016-07-10 00:03:27 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 11:37:11 +03:00
|
|
|
$arguments[] = &$response;
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
call_user_func_array('array_multisort', $arguments);
|
2016-07-09 14:14:08 +03:00
|
|
|
}
|
2016-07-09 10:02:25 +03:00
|
|
|
}
|