Differential D21840 Diff 52055 src/applications/files/controller/PhabricatorFileDetachController.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/controller/PhabricatorFileDetachController.php
- This file was copied from src/applications/files/controller/PhabricatorFileUICurtainAttachController.php.
<?php | <?php | ||||
final class PhabricatorFileUICurtainAttachController | final class PhabricatorFileDetachController | ||||
extends PhabricatorFileController { | extends PhabricatorFileController { | ||||
public function handleRequest(AphrontRequest $request) { | public function handleRequest(AphrontRequest $request) { | ||||
$viewer = $request->getViewer(); | $viewer = $request->getViewer(); | ||||
$object_phid = $request->getURIData('objectPHID'); | $object_phid = $request->getURIData('objectPHID'); | ||||
$file_phid = $request->getURIData('filePHID'); | $file_phid = $request->getURIData('filePHID'); | ||||
$object = id(new PhabricatorObjectQuery()) | $object = id(new PhabricatorObjectQuery()) | ||||
->setViewer($viewer) | ->setViewer($viewer) | ||||
->withPHIDs(array($object_phid)) | ->withPHIDs(array($object_phid)) | ||||
->executeOne(); | ->executeOne(); | ||||
if (!$object) { | if (!$object) { | ||||
return new Aphront404Response(); | return new Aphront404Response(); | ||||
} | } | ||||
$attachment = id(new PhabricatorFileAttachmentQuery()) | |||||
->setViewer($viewer) | |||||
->withObjectPHIDs(array($object->getPHID())) | |||||
->withFilePHIDs(array($file_phid)) | |||||
->needFiles(true) | |||||
->withVisibleFiles(true) | |||||
->executeOne(); | |||||
if (!$attachment) { | |||||
return new Aphront404Response(); | |||||
} | |||||
$file = $attachment->getFile(); | |||||
$file_phid = $file->getPHID(); | |||||
$handles = $viewer->loadHandles( | $handles = $viewer->loadHandles( | ||||
array( | array( | ||||
$object_phid, | $object_phid, | ||||
$file_phid, | $file_phid, | ||||
)); | )); | ||||
$object_handle = $handles[$object_phid]; | $object_handle = $handles[$object_phid]; | ||||
$file_handle = $handles[$file_phid]; | $file_handle = $handles[$file_phid]; | ||||
$cancel_uri = $object_handle->getURI(); | $cancel_uri = $file_handle->getURI(); | ||||
$dialog = $this->newDialog() | $dialog = $this->newDialog() | ||||
->setViewer($viewer) | ->setViewer($viewer) | ||||
->setTitle(pht('Attach File')) | ->setTitle(pht('Detach File')) | ||||
->addCancelButton($object_handle->getURI(), pht('Close')); | ->addCancelButton($cancel_uri, pht('Close')); | ||||
$file_link = phutil_tag('strong', array(), $file_handle->renderLink()); | $file_link = phutil_tag('strong', array(), $file_handle->renderLink()); | ||||
$object_link = phutil_tag('strong', array(), $object_handle->renderLink()); | $object_link = phutil_tag('strong', array(), $object_handle->renderLink()); | ||||
if ($attachment->isPolicyAttachment()) { | $attachment = id(new PhabricatorFileAttachmentQuery()) | ||||
->setViewer($viewer) | |||||
->withObjectPHIDs(array($object->getPHID())) | |||||
->withFilePHIDs(array($file_phid)) | |||||
->needFiles(true) | |||||
->withVisibleFiles(true) | |||||
->executeOne(); | |||||
if (!$attachment) { | |||||
$body = pht( | $body = pht( | ||||
'The file %s is already attached to the object %s.', | 'The file %s is not attached to the object %s.', | ||||
$file_link, | $file_link, | ||||
$object_link); | $object_link); | ||||
return $dialog->appendParagraph($body); | return $dialog->appendParagraph($body); | ||||
} | } | ||||
if (!$request->isDialogFormPost()) { | $mode_reference = PhabricatorFileAttachment::MODE_REFERENCE; | ||||
$dialog->appendRemarkup( | if ($attachment->getAttachmentMode() === $mode_reference) { | ||||
pht( | $body = pht( | ||||
'(WARNING) This file is referenced by this object, but '. | 'The file %s is referenced by the object %s, but not attached to '. | ||||
'not formally attached to it. Users who can see the object may '. | 'it, so it can not be detached.', | ||||
'not be able to see the file.')); | |||||
$dialog->appendParagraph( | |||||
pht( | |||||
'Do you want to attach the file %s to the object %s?', | |||||
$file_link, | $file_link, | ||||
$object_link)); | $object_link); | ||||
$dialog->addSubmitButton(pht('Attach File')); | |||||
return $dialog; | return $dialog->appendParagraph($body); | ||||
} | } | ||||
if (!$request->getBool('confirm')) { | if (!$attachment->canDetach()) { | ||||
$dialog->setTitle(pht('Confirm File Attachment')); | $body = pht( | ||||
'The file %s can not be detached from the object %s.', | |||||
$dialog->addHiddenInput('confirm', 1); | $file_link, | ||||
$object_link); | |||||
$dialog->appendRemarkup( | return $dialog->appendParagraph($body); | ||||
pht( | } | ||||
'(IMPORTANT) If you attach this file to this object, any user who '. | |||||
'has permission to view the object will be able to view and '. | |||||
'download the file!')); | |||||
if (!$request->isDialogFormPost()) { | |||||
$dialog->appendParagraph( | $dialog->appendParagraph( | ||||
pht( | pht( | ||||
'Really attach the file %s to the object %s, allowing any user '. | 'Detach the file %s from the object %s?', | ||||
'who can view the object to view and download the file?', | |||||
$file_link, | $file_link, | ||||
$object_link)); | $object_link)); | ||||
$dialog->addSubmitButton(pht('Grant Permission')); | $dialog->addSubmitButton(pht('Detach File')); | ||||
return $dialog; | return $dialog; | ||||
} | } | ||||
if (!($object instanceof PhabricatorApplicationTransactionInterface)) { | if (!($object instanceof PhabricatorApplicationTransactionInterface)) { | ||||
$dialog->appendParagraph( | $dialog->appendParagraph( | ||||
pht( | pht( | ||||
'This object (of class "%s") does not implement the required '. | 'This object (of class "%s") does not implement the required '. | ||||
'interface ("%s"), so files can not be manually attached to it.', | 'interface ("%s"), so files can not be manually detached from it.', | ||||
get_class($object), | get_class($object), | ||||
'PhabricatorApplicationTransactionInterface')); | 'PhabricatorApplicationTransactionInterface')); | ||||
return $dialog; | return $dialog; | ||||
} | } | ||||
$editor = $object->getApplicationTransactionEditor() | $editor = $object->getApplicationTransactionEditor() | ||||
->setActor($viewer) | ->setActor($viewer) | ||||
->setContentSourceFromRequest($request) | ->setContentSourceFromRequest($request) | ||||
->setContinueOnNoEffect(true) | ->setContinueOnNoEffect(true) | ||||
->setContinueOnMissingFields(true); | ->setContinueOnMissingFields(true); | ||||
$template = $object->getApplicationTransactionTemplate(); | $template = $object->getApplicationTransactionTemplate(); | ||||
$xactions = array(); | $xactions = array(); | ||||
$xactions[] = id(clone $template) | $xactions[] = id(clone $template) | ||||
->setTransactionType(PhabricatorTransactions::TYPE_FILE) | ->setTransactionType(PhabricatorTransactions::TYPE_FILE) | ||||
->setNewValue( | ->setNewValue( | ||||
array( | array( | ||||
$file_phid => PhabricatorFileAttachment::MODE_ATTACH, | $file_phid => PhabricatorFileAttachment::MODE_DETACH, | ||||
)); | )); | ||||
$editor->applyTransactions($object, $xactions); | $editor->applyTransactions($object, $xactions); | ||||
return $this->newRedirect() | return $this->newRedirect() | ||||
->setURI($cancel_uri); | ->setURI($cancel_uri); | ||||
} | } | ||||
} | } |