|
| 1 | +<?php |
| 2 | + |
| 3 | +final class PhabricatorFileUICurtainAttachController |
| 4 | + extends PhabricatorFileController { |
| 5 | + |
| 6 | + public function handleRequest(AphrontRequest $request) { |
| 7 | + $viewer = $request->getViewer(); |
| 8 | + |
| 9 | + $object_phid = $request->getURIData('objectPHID'); |
| 10 | + $file_phid = $request->getURIData('filePHID'); |
| 11 | + |
| 12 | + $object = id(new PhabricatorObjectQuery()) |
| 13 | + ->setViewer($viewer) |
| 14 | + ->withPHIDs(array($object_phid)) |
| 15 | + ->executeOne(); |
| 16 | + if (!$object) { |
| 17 | + return new Aphront404Response(); |
| 18 | + } |
| 19 | + |
| 20 | + $attachment = id(new PhabricatorFileAttachmentQuery()) |
| 21 | + ->setViewer($viewer) |
| 22 | + ->withObjectPHIDs(array($object->getPHID())) |
| 23 | + ->withFilePHIDs(array($file_phid)) |
| 24 | + ->needFiles(true) |
| 25 | + ->withVisibleFiles(true) |
| 26 | + ->executeOne(); |
| 27 | + if (!$attachment) { |
| 28 | + return new Aphront404Response(); |
| 29 | + } |
| 30 | + |
| 31 | + $file = $attachment->getFile(); |
| 32 | + $file_phid = $file->getPHID(); |
| 33 | + |
| 34 | + $handles = $viewer->loadHandles( |
| 35 | + array( |
| 36 | + $object_phid, |
| 37 | + $file_phid, |
| 38 | + )); |
| 39 | + |
| 40 | + $object_handle = $handles[$object_phid]; |
| 41 | + $file_handle = $handles[$file_phid]; |
| 42 | + $cancel_uri = $object_handle->getURI(); |
| 43 | + |
| 44 | + $dialog = $this->newDialog() |
| 45 | + ->setViewer($viewer) |
| 46 | + ->setTitle(pht('Attach File')) |
| 47 | + ->addCancelButton($object_handle->getURI(), pht('Close')); |
| 48 | + |
| 49 | + $file_link = phutil_tag('strong', array(), $file_handle->renderLink()); |
| 50 | + $object_link = phutil_tag('strong', array(), $object_handle->renderLink()); |
| 51 | + |
| 52 | + if ($attachment->isPolicyAttachment()) { |
| 53 | + $body = pht( |
| 54 | + 'The file %s is already attached to the object %s.', |
| 55 | + $file_link, |
| 56 | + $object_link); |
| 57 | + |
| 58 | + return $dialog->appendParagraph($body); |
| 59 | + } |
| 60 | + |
| 61 | + if (!$request->isDialogFormPost()) { |
| 62 | + $dialog->appendRemarkup( |
| 63 | + pht( |
| 64 | + '(WARNING) This file is referenced by this object, but '. |
| 65 | + 'not formally attached to it. Users who can see the object may '. |
| 66 | + 'not be able to see the file.')); |
| 67 | + |
| 68 | + $dialog->appendParagraph( |
| 69 | + pht( |
| 70 | + 'Do you want to attach the file %s to the object %s?', |
| 71 | + $file_link, |
| 72 | + $object_link)); |
| 73 | + |
| 74 | + $dialog->addSubmitButton(pht('Attach File')); |
| 75 | + |
| 76 | + return $dialog; |
| 77 | + } |
| 78 | + |
| 79 | + if (!$request->getBool('confirm')) { |
| 80 | + $dialog->setTitle(pht('Confirm File Attachment')); |
| 81 | + |
| 82 | + $dialog->addHiddenInput('confirm', 1); |
| 83 | + |
| 84 | + $dialog->appendRemarkup( |
| 85 | + pht( |
| 86 | + '(IMPORTANT) If you attach this file to this object, any user who '. |
| 87 | + 'has permission to view the object will be able to view and '. |
| 88 | + 'download the file!')); |
| 89 | + |
| 90 | + $dialog->appendParagraph( |
| 91 | + pht( |
| 92 | + 'Really attach the file %s to the object %s, allowing any user '. |
| 93 | + 'who can view the object to view and download the file?', |
| 94 | + $file_link, |
| 95 | + $object_link)); |
| 96 | + |
| 97 | + $dialog->addSubmitButton(pht('Grant Permission')); |
| 98 | + |
| 99 | + return $dialog; |
| 100 | + } |
| 101 | + |
| 102 | + if (!($object instanceof PhabricatorApplicationTransactionInterface)) { |
| 103 | + $dialog->appendParagraph( |
| 104 | + pht( |
| 105 | + 'This object (of class "%s") does not implement the required '. |
| 106 | + 'interface ("%s"), so files can not be manually attached to it.', |
| 107 | + get_class($object), |
| 108 | + 'PhabricatorApplicationTransactionInterface')); |
| 109 | + |
| 110 | + return $dialog; |
| 111 | + } |
| 112 | + |
| 113 | + $editor = $object->getApplicationTransactionEditor() |
| 114 | + ->setActor($viewer) |
| 115 | + ->setContentSourceFromRequest($request) |
| 116 | + ->setContinueOnNoEffect(true) |
| 117 | + ->setContinueOnMissingFields(true); |
| 118 | + |
| 119 | + $template = $object->getApplicationTransactionTemplate(); |
| 120 | + |
| 121 | + $xactions = array(); |
| 122 | + |
| 123 | + $xactions[] = id(clone $template) |
| 124 | + ->setTransactionType(PhabricatorTransactions::TYPE_FILE) |
| 125 | + ->setNewValue( |
| 126 | + array( |
| 127 | + $file_phid => PhabricatorFileAttachment::MODE_ATTACH, |
| 128 | + )); |
| 129 | + |
| 130 | + $editor->applyTransactions($object, $xactions); |
| 131 | + |
| 132 | + return $this->newRedirect() |
| 133 | + ->setURI($cancel_uri); |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments