-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNote.php
130 lines (111 loc) · 3.72 KB
/
Note.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Sugarcrm\REST\Endpoint;
use MRussell\REST\Exception\Endpoint\InvalidData;
use GuzzleHttp\Promise\Utils;
use GuzzleHttp\Psr7\Response;
use Sugarcrm\REST\Endpoint\Abstracts\AbstractSugarBeanEndpoint;
use Sugarcrm\REST\Endpoint\Traits\ParseFilesTrait;
use Sugarcrm\REST\Endpoint\Traits\NoteAttachmentsTrait;
/**
* Metadata Endpoint provides access to the defined Metadata of the system
* @package Sugarcrm\REST\Endpoint
*/
class Note extends SugarBean
{
use NoteAttachmentsTrait {
resetAttachments as private resetAttachmentsProp;
}
use ParseFilesTrait;
public const NOTE_ACTION_MULTI_ATTACH = 'multiAttach';
public const NOTES_FILE_FIELD = 'filename';
public const NOTES_ATTACHMENTS_FIELD = 'attachments';
protected array $_actions = [
self::NOTE_ACTION_MULTI_ATTACH => 'POST',
];
protected string $_beanName = 'Notes';
/**
* @inheritDoc
* Add in handling for Multi Attachment Action, since it is multiple requests
*/
protected function configureURL(array $urlArgs): string
{
if ($this->getCurrentAction() === self::NOTE_ACTION_MULTI_ATTACH) {
//Set ID Var to temp - :module/temp
$urlArgs[self::MODEL_ID_VAR] = 'temp';
//Set action to file - :module/temp/file
$urlArgs[self::MODEL_ACTION_VAR] = self::BEAN_ACTION_FILE;
//Set action arg1 to filename - :module/temp/file/filename
$urlArgs[self::BEAN_ACTION_ARG1_VAR] = self::NOTES_FILE_FIELD;
}
return parent::configureURL($urlArgs);
}
/**
* Pass in an array of files, to attach to current(or new) Bean
* @return $this
*/
public function multiAttach(array $files, bool $async = true): self
{
$parsed = $this->parseFiles($files);
if (!empty($parsed)) {
$this->setCurrentAction(self::NOTE_ACTION_MULTI_ATTACH);
$promises = [];
foreach ($parsed as $file) {
$this->setFile(self::NOTES_FILE_FIELD, $file['path'], [
'filename' => $file['name'],
]);
$this->_upload = true;
if ($async) {
$promises[] = $this->asyncExecute()->getPromise();
} else {
$this->execute(); // @codeCoverageIgnore
}
}
if ($async) {
$responses = Utils::unwrap($promises);
}
$this->save();
}
return $this;
}
/**
* @return AbstractSugarBeanEndpoint|Note|void
*/
public function clear(): static
{
$this->resetAttachments();
return parent::clear();
}
/**
* Reset the attachments link to default blank values
* @return $this
*/
public function resetAttachments(): static
{
$this->resetAttachmentsProp();
$this->getData()->offsetUnset($this->getAttachmentsLinkField());
return $this;
}
protected function parseResponse(Response $response): void
{
parent::parseResponse($response);
if ($response->getStatusCode() == 200) {
switch ($this->getCurrentAction()) {
case self::MODEL_ACTION_UPDATE:
case self::MODEL_ACTION_CREATE:
$this->resetAttachments();
break;
case self::NOTE_ACTION_MULTI_ATTACH:
$this->parseAttachmentUploadResponse($this->getResponseBody());
break;
}
}
}
/**
* @throws InvalidData
*/
protected function configurePayload(): mixed
{
$data = parent::configurePayload();
return $this->configureAttachmentsPayload($data);
}
}