1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\graphql\data\mutations; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use EE_Datetime; |
7
|
|
|
use EE_Error; |
8
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
9
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
10
|
|
|
use GraphQLRelay\Relay; |
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
use ReflectionException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DatetimeMutation |
16
|
|
|
* |
17
|
|
|
* @package Event Espresso |
18
|
|
|
* @author Manzoor Wani |
19
|
|
|
*/ |
20
|
|
|
class DatetimeMutation |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Maps the GraphQL input to a format that the model functions can use |
25
|
|
|
* |
26
|
|
|
* @param array $input Data coming from the GraphQL mutation query input |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public static function prepareFields(array $input) |
30
|
|
|
{ |
31
|
|
|
$args = []; |
32
|
|
|
|
33
|
|
|
if (! empty($input['eventId'])) { |
34
|
|
|
$args['EVT_ID'] = absint($input['eventId']); |
35
|
|
View Code Duplication |
} elseif (! empty($input['event'])) { |
36
|
|
|
$parts = Relay::fromGlobalId($input['event']); |
37
|
|
|
$args['EVT_ID'] = (! empty($parts['id']) && is_int($parts['id'])) ? $parts['id'] : null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (! empty($input['name'])) { |
41
|
|
|
$args['DTT_name'] = sanitize_text_field($input['name']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (! empty($input['description'])) { |
45
|
|
|
$args['DTT_description'] = sanitize_text_field($input['description']); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
if (! empty($input['startDate'])) { |
49
|
|
|
$args['DTT_EVT_start'] = new DateTime(sanitize_text_field($input['startDate'])); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
if (! empty($input['endDate'])) { |
53
|
|
|
$args['DTT_EVT_end'] = new DateTime(sanitize_text_field($input['endDate'])); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
if (! empty($input['tickets'])) { |
57
|
|
|
$args['tickets'] = array_map('sanitize_text_field', (array) $input['tickets']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (array_key_exists('isTrashed', $input)) { |
61
|
|
|
$args['DTT_deleted'] = (bool) $input['isTrashed']; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Likewise the other fields... |
65
|
|
|
|
66
|
|
|
return $args; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Sets the related tickets for the given datetime. |
72
|
|
|
* |
73
|
|
|
* @param EE_Datetime $entity The datetime instance. |
74
|
|
|
* @param array $tickets Array of ticket IDs to relate. |
75
|
|
|
* @throws EE_Error |
76
|
|
|
* @throws InvalidDataTypeException |
77
|
|
|
* @throws InvalidInterfaceException |
78
|
|
|
* @throws InvalidArgumentException |
79
|
|
|
* @throws ReflectionException |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public static function setRelatedTickets($entity, array $tickets) |
82
|
|
|
{ |
83
|
|
|
$relationName = 'Ticket'; |
84
|
|
|
// Remove all the existing related tickets |
85
|
|
|
$entity->_remove_relations($relationName); |
86
|
|
|
|
87
|
|
|
foreach ($tickets as $ID) { |
88
|
|
|
$parts = Relay::fromGlobalId($ID); |
89
|
|
|
if (! empty($parts['id']) && absint($parts['id'])) { |
90
|
|
|
$entity->_add_relation_to( |
91
|
|
|
$parts['id'], |
92
|
|
|
$relationName |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|