Completed
Branch add-event-dom-data (6e412b)
by
unknown
32:54 queued 23:24
created

EventConnectionResolver::get_query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\graphql\connection_resolvers;
4
5
use EE_Datetime;
6
use EE_Error;
7
use EEM_Datetime;
8
use EEM_Event;
9
use EE_Event;
10
use EventEspresso\core\exceptions\InvalidDataTypeException;
11
use EventEspresso\core\exceptions\InvalidInterfaceException;
12
use InvalidArgumentException;
13
use WPGraphQL\Model\Post;
14
15
/**
16
 * Class EventConnectionResolver
17
 *
18
 */
19
class EventConnectionResolver extends AbstractConnectionResolver
20
{
21
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
22
    public function get_loader_name()
23
    {
24
        return 'espresso_event';
25
    }
26
27
    /**
28
     * @return EEM_Event
29
     * @throws EE_Error
30
     * @throws InvalidArgumentException
31
     * @throws InvalidDataTypeException
32
     * @throws InvalidInterfaceException
33
     */
34
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
35
    public function get_query()
36
    {
37
        return EEM_Event::instance();
38
    }
39
40
    /**
41
     * Return an array of item IDs from the query
42
     *
43
     * @return array
44
     */
45
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
46
    public function get_ids()
47
    {
48
        $results = $this->query->get_col($this->query_args);
49
50
        return ! empty($results) ? $results : [];
51
    }
52
53
    /**
54
     * Here, we map the args from the input, then we make sure that we're only querying
55
     * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers
56
     * handle batch resolution of the posts.
57
     *
58
     * @return array
59
     * @throws EE_Error
60
     * @throws InvalidArgumentException
61
     * @throws InvalidDataTypeException
62
     * @throws InvalidInterfaceException
63
     */
64
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
65
    public function get_query_args()
66
    {
67
        $input_fields = [];
68
        $query_args   = [
69
            'limit'                    => $this->getLimit(),
70
            'default_where_conditions' => 'minimum',
71
        ];
72
        $where_params = [
73
            'status' => ['IN', ['publish', EEM_Event::sold_out]],
74
        ];
75
76
        /**
77
         * Collect the input_fields and sanitize them to prepare them for sending to the Query
78
         */
79
        if (! empty($this->args['where'])) {
80
            $input_fields = $this->sanitizeInputFields($this->args['where']);
81
            // Use the proper operator.
82
            if (! empty($input_fields['EVT_ID']) && is_array($input_fields['EVT_ID'])) {
83
                $input_fields['EVT_ID'] = ['in', $input_fields['EVT_ID']];
84
            }
85
        }
86
87
        /**
88
         * Determine where we're at in the Graph and adjust the query context appropriately.
89
         *
90
         * For example, if we're querying for datetime as a field of event query, this will automatically
91
         * set the query to pull datetimes that belong to that event.
92
         * We can set more cases for other source types.
93
         */
94
        if (is_object($this->source)) {
95
            switch (true) {
96
                // It's surely an event
97
                case $this->source instanceof Post:
98
                    $where_params['EVT_ID'] = $this->source->ID;
99
                    break;
100
                case $this->source instanceof EE_Event:
101
                    $where_params['EVT_ID'] = $this->source->ID();
102
                    break;
103
                case $this->source instanceof EE_Datetime:
104
                    $where_params['Datetime.EVT_ID'] = $this->source->ID();
105
                    break;
106
            }
107
        }
108
109
        /**
110
         * Merge the input_fields with the default query_args
111
         */
112
        if (! empty($input_fields)) {
113
            $where_params = array_merge($where_params, $input_fields);
114
        }
115
116
        [$query_args, $where_params] = $this->mapOrderbyInputArgs($query_args, $where_params, 'EVT_ID');
117
118
        $search = isset($this->args['where']) ? $this->getSearchKeywords($this->args['where']) : '';
119
120
        if (! empty($search)) {
121
            // use OR operator to search in any of the fields
122
            $where_params['OR'] = [
123
                'EVT_name'        => ['LIKE', '%' . $search . '%'],
124
                'EVT_description' => ['LIKE', '%' . $search . '%'],
125
            ];
126
        }
127
128
        if (! empty($this->args['where']['upcoming'])) {
129
            $where_params['Datetime.DTT_EVT_start'] = [
130
                '>',
131
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'),
132
            ];
133
        }
134
135
        if (! empty($this->args['where']['active'])) {
136
            $where_params['Datetime.DTT_EVT_start'] = [
137
                '<',
138
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'),
139
            ];
140
        }
141
142
        if (! empty($this->args['where']['expired'])) {
143
            $where_params['Datetime.DTT_EVT_end'] = [
144
                '<',
145
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'),
146
            ];
147
        }
148
149
        $where_params = apply_filters(
150
            'FHEE__EventEspresso_core_domain_services_graphql_connection_resolvers__event_where_params',
151
            $where_params,
152
            $this->source,
153
            $this->args
154
        );
155
156
        $query_args[] = $where_params;
157
158
        /**
159
         * Return the $query_args
160
         */
161
        return apply_filters(
162
            'FHEE__EventEspresso_core_domain_services_graphql_connection_resolvers__event_query_args',
163
            $query_args,
164
            $this->source,
165
            $this->args
166
        );
167
    }
168
169
170
    /**
171
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model
172
     * friendly keys.
173
     *
174
     * @param array $where_args
175
     * @return array
176
     */
177
    public function sanitizeInputFields(array $where_args)
178
    {
179
        $arg_mapping = [
180
            'datetime'     => 'Datetime.DTT_ID',
181
            'datetimeIn'   => 'Datetime.DTT_ID',
182
            'datetimeIdIn' => 'Datetime.DTT_ID',
183
            'datetimeId'   => 'Datetime.DTT_ID', // priority.
184
        ];
185
        return $this->sanitizeWhereArgsForInputFields(
186
            $where_args,
187
            $arg_mapping,
188
            ['datetime', 'datetimeIn']
189
        );
190
    }
191
}
192