Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / files / controller / PhabricatorFileDetachController.php
blob146eb428748e84e6b56ac942f72c3e1c766368e5
1 <?php
3 final class PhabricatorFileDetachController
4 extends PhabricatorFileController {
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $request->getViewer();
9 $object_phid = $request->getURIData('objectPHID');
10 $file_phid = $request->getURIData('filePHID');
12 $object = id(new PhabricatorObjectQuery())
13 ->setViewer($viewer)
14 ->withPHIDs(array($object_phid))
15 ->executeOne();
16 if (!$object) {
17 return new Aphront404Response();
20 $handles = $viewer->loadHandles(
21 array(
22 $object_phid,
23 $file_phid,
24 ));
26 $object_handle = $handles[$object_phid];
27 $file_handle = $handles[$file_phid];
28 $cancel_uri = $file_handle->getURI();
30 $dialog = $this->newDialog()
31 ->setViewer($viewer)
32 ->setTitle(pht('Detach File'))
33 ->addCancelButton($cancel_uri, pht('Close'));
35 $file_link = phutil_tag('strong', array(), $file_handle->renderLink());
36 $object_link = phutil_tag('strong', array(), $object_handle->renderLink());
38 $attachment = id(new PhabricatorFileAttachmentQuery())
39 ->setViewer($viewer)
40 ->withObjectPHIDs(array($object->getPHID()))
41 ->withFilePHIDs(array($file_phid))
42 ->needFiles(true)
43 ->withVisibleFiles(true)
44 ->executeOne();
45 if (!$attachment) {
46 $body = pht(
47 'The file %s is not attached to the object %s.',
48 $file_link,
49 $object_link);
51 return $dialog->appendParagraph($body);
54 $mode_reference = PhabricatorFileAttachment::MODE_REFERENCE;
55 if ($attachment->getAttachmentMode() === $mode_reference) {
56 $body = pht(
57 'The file %s is referenced by the object %s, but not attached to '.
58 'it, so it can not be detached.',
59 $file_link,
60 $object_link);
62 return $dialog->appendParagraph($body);
65 if (!$attachment->canDetach()) {
66 $body = pht(
67 'The file %s can not be detached from the object %s.',
68 $file_link,
69 $object_link);
71 return $dialog->appendParagraph($body);
74 if (!$request->isDialogFormPost()) {
75 $dialog->appendParagraph(
76 pht(
77 'Detach the file %s from the object %s?',
78 $file_link,
79 $object_link));
81 $dialog->addSubmitButton(pht('Detach File'));
83 return $dialog;
86 if (!($object instanceof PhabricatorApplicationTransactionInterface)) {
87 $dialog->appendParagraph(
88 pht(
89 'This object (of class "%s") does not implement the required '.
90 'interface ("%s"), so files can not be manually detached from it.',
91 get_class($object),
92 'PhabricatorApplicationTransactionInterface'));
94 return $dialog;
97 $editor = $object->getApplicationTransactionEditor()
98 ->setActor($viewer)
99 ->setContentSourceFromRequest($request)
100 ->setContinueOnNoEffect(true)
101 ->setContinueOnMissingFields(true);
103 $template = $object->getApplicationTransactionTemplate();
105 $xactions = array();
107 $xactions[] = id(clone $template)
108 ->setTransactionType(PhabricatorTransactions::TYPE_FILE)
109 ->setNewValue(
110 array(
111 $file_phid => PhabricatorFileAttachment::MODE_DETACH,
114 $editor->applyTransactions($object, $xactions);
116 return $this->newRedirect()
117 ->setURI($cancel_uri);