1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\graphql\data\mutations; |
4
|
|
|
|
5
|
|
|
use GraphQLRelay\Relay; |
6
|
|
|
use DateTime; |
7
|
|
|
use EEM_Price; |
8
|
|
|
use EEM_Ticket; |
9
|
|
|
use EE_Ticket; |
10
|
|
|
use EventEspresso\core\domain\services\assets\EspressoEditorAssetManager; |
11
|
|
|
use EventEspresso\core\domain\services\converters\RestApiSpoofer; |
12
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
13
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
14
|
|
|
use EventEspresso\core\exceptions\ModelConfigurationException; |
15
|
|
|
use EventEspresso\core\exceptions\RestPasswordIncorrectException; |
16
|
|
|
use EventEspresso\core\exceptions\RestPasswordRequiredException; |
17
|
|
|
use EventEspresso\core\exceptions\UnexpectedEntityException; |
18
|
|
|
use EventEspresso\core\libraries\rest_api\RestException; |
19
|
|
|
use InvalidArgumentException; |
20
|
|
|
use ReflectionException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class TicketMutation |
24
|
|
|
* |
25
|
|
|
* @package Event Espresso |
26
|
|
|
* @author Manzoor Wani |
27
|
|
|
*/ |
28
|
|
|
class TicketMutation |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Maps the GraphQL input to a format that the model functions can use |
33
|
|
|
* |
34
|
|
|
* @param array $input Data coming from the GraphQL mutation query input |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public static function prepareFields(array $input) |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
$args = []; |
41
|
|
|
|
42
|
|
|
if (! empty($input['name'])) { |
43
|
|
|
$args['TKT_name'] = sanitize_text_field($input['name']); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (! empty($input['description'])) { |
47
|
|
|
$args['TKT_description'] = sanitize_text_field($input['description']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (! empty($input['price'])) { |
51
|
|
|
$args['TKT_price'] = floatval($input['price']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
if (! empty($input['startDate'])) { |
55
|
|
|
$args['TKT_start_date'] = new DateTime(sanitize_text_field($input['startDate'])); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
if (! empty($input['endDate'])) { |
59
|
|
|
$args['TKT_end_date'] = new DateTime(sanitize_text_field($input['endDate'])); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
if (! empty($input['datetimes'])) { |
63
|
|
|
$args['datetimes'] = array_map('sanitize_text_field', (array) $input['datetimes']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
if (! empty($input['prices'])) { |
67
|
|
|
$args['prices'] = array_map('sanitize_text_field', (array) $input['prices']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Likewise the other fields... |
71
|
|
|
|
72
|
|
|
return $args; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Sets the related datetimes for the given ticket. |
77
|
|
|
* |
78
|
|
|
* @param EE_Ticket $entity The Ticket instance. |
79
|
|
|
* @param array $datetimes Array of datetime IDs to relate. |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public static function setRelatedDatetimes($entity, array $datetimes) |
82
|
|
|
{ |
83
|
|
|
$relationName = 'Datetime'; |
84
|
|
|
// Remove all the existing related datetimes |
85
|
|
|
|
86
|
|
|
$entity->_remove_relations($relationName); |
87
|
|
|
// @todo replace loop with single query |
88
|
|
|
foreach ($datetimes as $ID) { |
89
|
|
|
$parts = Relay::fromGlobalId($ID); |
90
|
|
|
if (! empty($parts['id']) && absint($parts['id'])) { |
91
|
|
|
$entity->_add_relation_to( |
92
|
|
|
$parts['id'], |
93
|
|
|
$relationName |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Sets the related prices for the given ticket. |
101
|
|
|
* |
102
|
|
|
* @param EE_Ticket $entity The Ticket instance. |
103
|
|
|
* @param array $prices Array of entity IDs to relate. |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public static function setRelatedPrices($entity, array $prices) |
106
|
|
|
{ |
107
|
|
|
$relationName = 'Price'; |
108
|
|
|
// Remove all the existing related entities |
109
|
|
|
$entity->_remove_relations($relationName); |
110
|
|
|
|
111
|
|
|
// @todo replace loop with single query |
112
|
|
|
foreach ($prices as $ID) { |
113
|
|
|
$parts = Relay::fromGlobalId($ID); |
114
|
|
|
if (! empty($parts['id']) && absint($parts['id'])) { |
115
|
|
|
$entity->_add_relation_to( |
116
|
|
|
$parts['id'], |
117
|
|
|
$relationName |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param EE_Ticket $ticket_entity |
126
|
|
|
* @param EEM_Ticket $ticket_model |
127
|
|
|
* @throws DomainException |
128
|
|
|
* @throws EE_Error |
129
|
|
|
* @throws InvalidArgumentException |
130
|
|
|
* @throws InvalidDataTypeException |
131
|
|
|
* @throws InvalidInterfaceException |
132
|
|
|
* @throws ModelConfigurationException |
133
|
|
|
* @throws ReflectionException |
134
|
|
|
* @throws RestException |
135
|
|
|
* @throws UnexpectedEntityException |
136
|
|
|
* @since $VID:$ |
137
|
|
|
*/ |
138
|
|
|
public static function addDefaultPrices(EE_Ticket $ticket_entity, EEM_Ticket $ticket_model) |
139
|
|
|
{ |
140
|
|
|
$price_model = EEM_Price::instance(); |
141
|
|
|
$default_prices = $price_model->get_all_default_prices(); |
142
|
|
|
foreach ($default_prices as $default_price) { |
|
|
|
|
143
|
|
|
$default_price->save(); |
144
|
|
|
$default_price->_add_relation_to($ticket_entity, 'Ticket'); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.