|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/force/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/software/force/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\craft\salesforce\controllers; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\helpers\Json; |
|
13
|
|
|
use flipbox\craft\ember\controllers\AbstractController; |
|
14
|
|
|
use flipbox\craft\salesforce\criteria\ObjectCriteria; |
|
15
|
|
|
use flipbox\craft\salesforce\criteria\UrlCriteria; |
|
16
|
|
|
use flipbox\craft\salesforce\events\DownloadDocumentEvent; |
|
17
|
|
|
use yii\web\NotFoundHttpException; |
|
18
|
|
|
use yii\web\UnauthorizedHttpException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Flipbox Factory <[email protected]> |
|
22
|
|
|
* @since 1.2.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class DocumentsController extends AbstractController |
|
25
|
|
|
{ |
|
26
|
|
|
const EVENT_DOWNLOAD_DOCUMENT = 'downloadDocument'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string|null $id |
|
30
|
|
|
* @return \craft\web\Response|\yii\console\Response |
|
31
|
|
|
* @throws NotFoundHttpException |
|
32
|
|
|
* @throws UnauthorizedHttpException |
|
33
|
|
|
* @throws \yii\web\BadRequestHttpException |
|
34
|
|
|
* @throws \yii\web\HttpException |
|
35
|
|
|
* @throws \yii\web\RangeNotSatisfiableHttpException |
|
36
|
|
|
*/ |
|
37
|
|
|
public function actionDownload(string $id = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$id = $id ?? Craft::$app->getRequest()->getRequiredBodyParam('id'); |
|
40
|
|
|
$inline = (bool) Craft::$app->getRequest()->getParam('inline', false); |
|
41
|
|
|
|
|
42
|
|
|
$result = (new ObjectCriteria()) |
|
43
|
|
|
->setObject('ContentVersion') |
|
44
|
|
|
->setId($id) |
|
45
|
|
|
->read(); |
|
46
|
|
|
|
|
47
|
|
|
if ($result->getStatusCode() !== 200) { |
|
48
|
|
|
throw new NotFoundHttpException( |
|
49
|
|
|
Craft::t( |
|
50
|
|
|
'salesforce', |
|
51
|
|
|
'The document could not be found.' |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$object = Json::decodeIfJson( |
|
57
|
|
|
$result->getBody()->getContents() |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$result = (new UrlCriteria()) |
|
61
|
|
|
->setUrl($object['VersionData']) |
|
62
|
|
|
->read(); |
|
63
|
|
|
|
|
64
|
|
|
if ($result->getStatusCode() !== 200) { |
|
65
|
|
|
throw new NotFoundHttpException( |
|
66
|
|
|
Craft::t( |
|
67
|
|
|
'salesforce', |
|
68
|
|
|
'The document contents could not be found.' |
|
69
|
|
|
) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$event = new DownloadDocumentEvent([ |
|
74
|
|
|
'object' => $object, |
|
75
|
|
|
'fileName' => $object['Title'] . '.' . strtolower($object['FileExtension']), |
|
76
|
|
|
'fileContents' => $result->getBody()->getContents() |
|
77
|
|
|
]); |
|
78
|
|
|
|
|
79
|
|
|
$this->trigger( |
|
80
|
|
|
self::EVENT_DOWNLOAD_DOCUMENT, |
|
81
|
|
|
$event |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
if (!$event->canDownload) { |
|
85
|
|
|
throw new UnauthorizedHttpException( |
|
86
|
|
|
Craft::t( |
|
87
|
|
|
'salesforce', |
|
88
|
|
|
$event->unauthorizedMessage |
|
|
|
|
|
|
89
|
|
|
) |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return Craft::$app->getResponse()->sendContentAsFile( |
|
94
|
|
|
$event->fileContents, |
|
95
|
|
|
$event->fileName, |
|
96
|
|
|
[ |
|
97
|
|
|
'inline' => $inline |
|
98
|
|
|
] |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: