Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't sort by last_contact_at or updated_at #41

Closed
REJack opened this issue Jul 9, 2021 · 1 comment
Closed

Can't sort by last_contact_at or updated_at #41

REJack opened this issue Jul 9, 2021 · 1 comment

Comments

@REJack
Copy link

REJack commented Jul 9, 2021

I've created a custom resource to search with sort/order, sadly it can't sort by last_contact_at or updated_at.

We use Zammad Cloud.

EDIT: Is there any chance to get this working? I have seen that other users should rebuild the serach indexes, but I don't know how I could do this 😄 .

EDIT2: I use the resoruce with this line of code

$client->resource( \App\Zammad\Resource\TicketCustom::class )->search('last_contact_at:*', null, null, 'last_contact_at', 'asc' );
<?php

namespace App\Zammad\Resource;

use ZammadAPIClient\Exception\AlreadyFetchedObjectException;
use ZammadAPIClient\ResourceType;

class TicketCustom extends \ZammadAPIClient\Resource\Ticket
{
    const URLS = [
        'get'    => 'tickets/{object_id}',
        'all'    => 'tickets',
        'create' => 'tickets',
        'update' => 'tickets/{object_id}',
        'delete' => 'tickets/{object_id}',
        'search' => 'tickets/search',
    ];

    /**
     * Fetches TicketArticle objects of this Ticket object.
     *
     * @return Array of TicketArticle objects   Returns array of ZammadAPIClient\Resource\TicketArticle objects or an empty array.
     */
    public function getTicketArticles()
    {
        $this->clearError();

        if ( empty( $this->getID() ) ) {
            return [];
        }

        $ticket_articles = $this->getClient()->resource( ResourceType::TICKET_ARTICLE )->getForTicket( $this->getID() );
        if ( !is_array($ticket_articles) ) {
            $this->setError( $ticket_articles->getError() );
            return [];
        }

        return $ticket_articles;
    }
    
    /**
     * Fetches object data for searched objects of this type.
     * This method will be used internally and automatically by search() to automate pagination
     * to retrieve all available objects, ignoring the server side limit of fetchable objects.
     *
     * @return mixed                        Returns array of ZammadAPIClient\Resource\... objects
     *                                          or this object on failure.
     */
    private function searchWithoutPagination($search_term)
    {
        $page             = 1;
        $objects_per_page = 100;
        $objects          = [];
        $objects_of_page  = [];

        do {
            $objects_of_page = $this->search( $search_term, $page, $objects_per_page );
            if ( !is_array($objects_of_page) ) {
                return $this;
            }

            $objects = array_merge( $objects, $objects_of_page );

            $is_last_page = count($objects_of_page) < $objects_per_page
                || !count($objects_of_page);

            $page++;
        } while ( !$is_last_page );

        return $objects;
    }

    /**
     * Fetches object data for given search term.
     * Pagination available.
     *
     * @param string  $search_term          Search term.
     * @param integer $page                 Page of objects, optional, if given, $objects_per_page must also be given.
     * @param integer $objects_per_page     Number of objects per page, optional, if given, $page must also be given.
     *
     * @return mixed                        Returns array of ZammadAPIClient\Resource\... objects
     *                                          or this object on failure.
     */
    public function search( $search_term, $page = null, $objects_per_page = null, $sort_by = null, $order_by = null )
    {
        if ( !empty( $this->getValues() ) ) {
            throw new AlreadyFetchedObjectException('Object already contains values, search() not possible, use a new object');
        }

        if ( isset($page) && $page <= 0 ) {
            throw new \RuntimeException('Parameter page must be a > 0');
        }
        if ( isset($objects_per_page) && $objects_per_page <= 0 ) {
            throw new \RuntimeException('Parameter objects_per_page must be a > 0');
        }
        if (
            ( isset($page) && !isset($objects_per_page) )
            || ( !isset($page) && isset($objects_per_page) )
        ) {
            throw new \RuntimeException('Parameters page and objects_per_page must both be given');
        }

        if ( !isset($page) || !isset($objects_per_page) ) {
            return $this->searchWithoutPagination($search_term);
        }

        $url_parameters = [
            'expand' => true,
            'query'  => $search_term,
        ];

        if (isset($sort_by)) {
            $url_parameters['sort_by'] = $sort_by;
        }
        if (isset($order_by)) {
            $url_parameters['order_by'] = $order_by;
        }
        if ( isset($page) && isset($objects_per_page) ) {
            $url_parameters['page']     = $page;
            $url_parameters['per_page'] = $objects_per_page;
        }

        $url      = $this->getURL('search');
        $response = $this->getClient()->get(
            $url,
            $url_parameters
        );

        if ( $response->hasError() ) {
            $this->setError( $response->getError() );
            return $this;
        }

        $this->clearError();

        // Return array of resource objects if no $object_id was given.
        // Note: the resource object (this object) used to execute get() will be left empty in this case.
        $objects = [];
        foreach ( $response->getData() as $object_data ) {
            $object = $this->getClient()->resource( get_class($this) );
            $object->setRemoteData($object_data);
            $objects[] = $object;
        }

        return $objects;
    }
}
@REJack
Copy link
Author

REJack commented Jul 12, 2021

row back, row back 😄 or like we germans say "ZURÜCK RUDERN!!!", I don't know what caused that but after the Weekend it worked 😅.

I will create a PR soon with my enhancement to change the order/sort within the search as extra arguments.

@REJack REJack closed this as completed Jul 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant