public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $this->phid);
     if (!$file) {
         return new Aphront404Response();
     }
     $author_child = null;
     if ($file->getAuthorPHID()) {
         $author = id(new PhabricatorUser())->loadOneWhere('phid = %s', $file->getAuthorPHID());
         if ($author) {
             $author_child = id(new AphrontFormStaticControl())->setLabel('Author')->setName('author')->setValue($author->getUserName());
         }
     }
     $form = new AphrontFormView();
     $submit = new AphrontFormSubmitControl();
     $form->setAction($file->getViewURI());
     if ($file->isViewableInBrowser()) {
         $submit->setValue('View File');
     } else {
         $submit->setValue('Download File');
     }
     if ($user->getPHID() == $file->getAuthorPHID() || $user->getIsAdmin()) {
         $submit->addCancelButton('/file/delete/' . $file->getID() . '/', 'Delete File');
     }
     $file_id = 'F' . $file->getID();
     $form->setUser($user);
     $form->appendChild(id(new AphrontFormStaticControl())->setLabel('Name')->setName('name')->setValue($file->getName()))->appendChild(id(new AphrontFormStaticControl())->setLabel('ID')->setName('id')->setValue($file_id)->setCaption('Download this file with: <tt>arc download ' . phutil_escape_html($file_id) . '</tt>'))->appendChild(id(new AphrontFormStaticControl())->setLabel('PHID')->setName('phid')->setValue($file->getPHID()))->appendChild($author_child)->appendChild(id(new AphrontFormStaticControl())->setLabel('Created')->setName('created')->setValue(phabricator_datetime($file->getDateCreated(), $user)))->appendChild(id(new AphrontFormStaticControl())->setLabel('Mime Type')->setName('mime')->setValue($file->getMimeType()))->appendChild(id(new AphrontFormStaticControl())->setLabel('Size')->setName('size')->setValue($file->getByteSize() . ' bytes'))->appendChild(id(new AphrontFormStaticControl())->setLabel('Engine')->setName('storageEngine')->setValue($file->getStorageEngine()))->appendChild(id(new AphrontFormStaticControl())->setLabel('Format')->setName('storageFormat')->setValue($file->getStorageFormat()))->appendChild(id(new AphrontFormStaticControl())->setLabel('Handle')->setName('storageHandle')->setValue($file->getStorageHandle()))->appendChild(id($submit));
     $panel = new AphrontPanelView();
     $panel->setHeader('File Info - ' . $file->getName());
     $panel->appendChild($form);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $xform_panel = null;
     $transformations = id(new PhabricatorTransformedFile())->loadAllWhere('originalPHID = %s', $file->getPHID());
     if ($transformations) {
         $transformed_phids = mpull($transformations, 'getTransformedPHID');
         $transformed_files = id(new PhabricatorFile())->loadAllWhere('phid in (%Ls)', $transformed_phids);
         $transformed_map = mpull($transformed_files, null, 'getPHID');
         $rows = array();
         foreach ($transformations as $transformed) {
             $phid = $transformed->getTransformedPHID();
             $rows[] = array(phutil_escape_html($transformed->getTransform()), phutil_render_tag('a', array('href' => $transformed_map[$phid]->getBestURI()), $phid));
         }
         $table = new AphrontTableView($rows);
         $table->setHeaders(array('Transform', 'File'));
         $xform_panel = new AphrontPanelView();
         $xform_panel->appendChild($table);
         $xform_panel->setWidth(AphrontPanelView::WIDTH_FORM);
         $xform_panel->setHeader('Transformations');
     }
     return $this->buildStandardPageResponse(array($panel, $xform_panel), array('title' => 'File Info - ' . $file->getName()));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $parent = null;
     $parent_id = null;
     if (!$this->id) {
         $is_create = true;
         $paste = new PhabricatorPaste();
         $parent_id = $request->getStr('parent');
         if ($parent_id) {
             // NOTE: If the Paste is forked from a paste which the user no longer
             // has permission to see, we still let them edit it.
             $parent = id(new PhabricatorPasteQuery())->setViewer($user)->withIDs(array($parent_id))->needContent(true)->execute();
             $parent = head($parent);
             if ($parent) {
                 $paste->setParentPHID($parent->getPHID());
             }
         }
         $paste->setAuthorPHID($user->getPHID());
     } else {
         $is_create = false;
         $paste = id(new PhabricatorPasteQuery())->setViewer($user)->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->withIDs(array($this->id))->executeOne();
         if (!$paste) {
             return new Aphront404Response();
         }
     }
     $text = null;
     $e_text = true;
     $errors = array();
     if ($request->isFormPost()) {
         if ($is_create) {
             $text = $request->getStr('text');
             if (!strlen($text)) {
                 $e_text = 'Required';
                 $errors[] = 'The paste may not be blank.';
             } else {
                 $e_text = null;
             }
         }
         $paste->setTitle($request->getStr('title'));
         $paste->setLanguage($request->getStr('language'));
         if (!$errors) {
             if ($is_create) {
                 $paste_file = PhabricatorFile::newFromFileData($text, array('name' => $paste->getTitle(), 'mime-type' => 'text/plain; charset=utf-8', 'authorPHID' => $user->getPHID()));
                 $paste->setFilePHID($paste_file->getPHID());
             }
             $paste->save();
             return id(new AphrontRedirectResponse())->setURI($paste->getURI());
         }
     } else {
         if ($is_create && $parent) {
             $paste->setTitle('Fork of ' . $parent->getFullName());
             $paste->setLanguage($parent->getLanguage());
             $text = $parent->getContent();
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = id(new AphrontErrorView())->setTitle('A fatal omission!')->setErrors($errors);
     }
     $form = new AphrontFormView();
     $form->setFlexible(true);
     $langs = array('' => '(Detect With Wizardly Powers)') + PhabricatorEnv::getEnvConfig('pygments.dropdown-choices');
     $form->setUser($user)->addHiddenInput('parent', $parent_id)->appendChild(id(new AphrontFormTextControl())->setLabel('Title')->setValue($paste->getTitle())->setName('title'))->appendChild(id(new AphrontFormSelectControl())->setLabel('Language')->setName('language')->setValue($paste->getLanguage())->setOptions($langs));
     if ($is_create) {
         $form->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Text')->setError($e_text)->setValue($text)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setCustomClass('PhabricatorMonospaced')->setName('text'));
     }
     /* TODO: Doesn't have any useful options yet.
          ->appendChild(
            id(new AphrontFormPolicyControl())
              ->setLabel('Visible To')
              ->setUser($user)
              ->setValue(
                $new_paste->getPolicy(PhabricatorPolicyCapability::CAN_VIEW))
              ->setName('policy'))
        */
     $submit = new AphrontFormSubmitControl();
     if (!$is_create) {
         $submit->addCancelButton($paste->getURI());
         $submit->setValue('Save Paste');
         $title = 'Edit ' . $paste->getFullName();
     } else {
         $submit->setValue('Create Paste');
         $title = 'Create Paste';
     }
     $form->appendChild($submit);
     $nav = $this->buildSideNavView();
     $nav->selectFilter('edit');
     $nav->appendChild(array(id(new PhabricatorHeaderView())->setHeader($title), $error_view, $form));
     return $this->buildApplicationPage($nav, array('title' => $title, 'device' => true));
 }
 public function handleRequest(AphrontRequest $request)
 {
     $user = $request->getUser();
     $id = $request->getURIData('id');
     if (!$id) {
         $is_create = true;
         $this->requireApplicationCapability(LegalpadCreateDocumentsCapability::CAPABILITY);
         $document = LegalpadDocument::initializeNewDocument($user);
         $body = id(new LegalpadDocumentBody())->setCreatorPHID($user->getPHID());
         $document->attachDocumentBody($body);
         $document->setDocumentBodyPHID(PhabricatorPHIDConstants::PHID_VOID);
     } else {
         $is_create = false;
         $document = id(new LegalpadDocumentQuery())->setViewer($user)->needDocumentBodies(true)->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->withIDs(array($id))->executeOne();
         if (!$document) {
             return new Aphront404Response();
         }
     }
     $e_title = true;
     $e_text = true;
     $title = $document->getDocumentBody()->getTitle();
     $text = $document->getDocumentBody()->getText();
     $v_signature_type = $document->getSignatureType();
     $v_preamble = $document->getPreamble();
     $v_require_signature = $document->getRequireSignature();
     $errors = array();
     $can_view = null;
     $can_edit = null;
     if ($request->isFormPost()) {
         $xactions = array();
         $title = $request->getStr('title');
         if (!strlen($title)) {
             $e_title = pht('Required');
             $errors[] = pht('The document title may not be blank.');
         } else {
             $xactions[] = id(new LegalpadTransaction())->setTransactionType(LegalpadTransaction::TYPE_TITLE)->setNewValue($title);
         }
         $text = $request->getStr('text');
         if (!strlen($text)) {
             $e_text = pht('Required');
             $errors[] = pht('The document may not be blank.');
         } else {
             $xactions[] = id(new LegalpadTransaction())->setTransactionType(LegalpadTransaction::TYPE_TEXT)->setNewValue($text);
         }
         $can_view = $request->getStr('can_view');
         $xactions[] = id(new LegalpadTransaction())->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)->setNewValue($can_view);
         $can_edit = $request->getStr('can_edit');
         $xactions[] = id(new LegalpadTransaction())->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)->setNewValue($can_edit);
         if ($is_create) {
             $v_signature_type = $request->getStr('signatureType');
             $xactions[] = id(new LegalpadTransaction())->setTransactionType(LegalpadTransaction::TYPE_SIGNATURE_TYPE)->setNewValue($v_signature_type);
         }
         $v_preamble = $request->getStr('preamble');
         $xactions[] = id(new LegalpadTransaction())->setTransactionType(LegalpadTransaction::TYPE_PREAMBLE)->setNewValue($v_preamble);
         $v_require_signature = $request->getBool('requireSignature', 0);
         if ($v_require_signature) {
             if (!$user->getIsAdmin()) {
                 $errors[] = pht('Only admins may require signature.');
             }
             $individual = LegalpadDocument::SIGNATURE_TYPE_INDIVIDUAL;
             if ($v_signature_type != $individual) {
                 $errors[] = pht('Only documents with signature type "individual" may require ' . 'signing to use Phabricator.');
             }
         }
         if ($user->getIsAdmin()) {
             $xactions[] = id(new LegalpadTransaction())->setTransactionType(LegalpadTransaction::TYPE_REQUIRE_SIGNATURE)->setNewValue($v_require_signature);
         }
         if (!$errors) {
             $editor = id(new LegalpadDocumentEditor())->setContentSourceFromRequest($request)->setContinueOnNoEffect(true)->setActor($user);
             $xactions = $editor->applyTransactions($document, $xactions);
             return id(new AphrontRedirectResponse())->setURI($this->getApplicationURI('view/' . $document->getID()));
         }
     }
     if ($errors) {
         // set these to what was specified in the form on post
         $document->setViewPolicy($can_view);
         $document->setEditPolicy($can_edit);
     }
     $form = id(new AphrontFormView())->setUser($user)->appendChild(id(new AphrontFormTextControl())->setID('document-title')->setLabel(pht('Title'))->setError($e_title)->setValue($title)->setName('title'));
     if ($is_create) {
         $form->appendChild(id(new AphrontFormSelectControl())->setLabel(pht('Who Should Sign?'))->setName(pht('signatureType'))->setValue($v_signature_type)->setOptions(LegalpadDocument::getSignatureTypeMap()));
         $show_require = true;
         $caption = pht('Applies only to documents individuals sign.');
     } else {
         $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Who Should Sign?'))->setValue($document->getSignatureTypeName()));
         $individual = LegalpadDocument::SIGNATURE_TYPE_INDIVIDUAL;
         $show_require = $document->getSignatureType() == $individual;
         $caption = null;
     }
     if ($show_require) {
         $form->appendChild(id(new AphrontFormCheckboxControl())->setDisabled(!$user->getIsAdmin())->setLabel(pht('Require Signature'))->addCheckbox('requireSignature', 'requireSignature', pht('Should signing this document be required to use Phabricator?'), $v_require_signature)->setCaption($caption));
     }
     $form->appendChild(id(new PhabricatorRemarkupControl())->setUser($user)->setID('preamble')->setLabel(pht('Preamble'))->setValue($v_preamble)->setName('preamble')->setCaption(pht('Optional help text for users signing this document.')))->appendChild(id(new PhabricatorRemarkupControl())->setUser($user)->setID('document-text')->setLabel(pht('Document Body'))->setError($e_text)->setValue($text)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setName('text'));
     $policies = id(new PhabricatorPolicyQuery())->setViewer($user)->setObject($document)->execute();
     $form->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_VIEW)->setPolicyObject($document)->setPolicies($policies)->setName('can_view'))->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_EDIT)->setPolicyObject($document)->setPolicies($policies)->setName('can_edit'));
     $crumbs = $this->buildApplicationCrumbs($this->buildSideNav());
     $submit = new AphrontFormSubmitControl();
     if ($is_create) {
         $submit->setValue(pht('Create Document'));
         $submit->addCancelButton($this->getApplicationURI());
         $title = pht('Create Document');
         $short = pht('Create');
     } else {
         $submit->setValue(pht('Save Document'));
         $submit->addCancelButton($this->getApplicationURI('view/' . $document->getID()));
         $title = pht('Edit Document');
         $short = pht('Edit');
         $crumbs->addTextCrumb($document->getMonogram(), $this->getApplicationURI('view/' . $document->getID()));
     }
     $form->appendChild($submit);
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($title)->setFormErrors($errors)->setForm($form);
     $crumbs->addTextCrumb($short);
     $preview = id(new PHUIRemarkupPreviewPanel())->setHeader(pht('Document Preview'))->setPreviewURI($this->getApplicationURI('document/preview/'))->setControlID('document-text')->addClass('phui-document-view');
     return $this->buildApplicationPage(array($crumbs, $form_box, $preview), array('title' => $title));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $parent = null;
     $parent_id = null;
     if (!$this->id) {
         $is_create = true;
         $paste = PhabricatorPaste::initializeNewPaste($user);
         $parent_id = $request->getStr('parent');
         if ($parent_id) {
             // NOTE: If the Paste is forked from a paste which the user no longer
             // has permission to see, we still let them edit it.
             $parent = id(new PhabricatorPasteQuery())->setViewer($user)->withIDs(array($parent_id))->needContent(true)->needRawContent(true)->execute();
             $parent = head($parent);
             if ($parent) {
                 $paste->setParentPHID($parent->getPHID());
                 $paste->setViewPolicy($parent->getViewPolicy());
             }
         }
         $paste->setAuthorPHID($user->getPHID());
         $paste->attachRawContent('');
     } else {
         $is_create = false;
         $paste = id(new PhabricatorPasteQuery())->setViewer($user)->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->withIDs(array($this->id))->needRawContent(true)->executeOne();
         if (!$paste) {
             return new Aphront404Response();
         }
     }
     $v_space = $paste->getSpacePHID();
     if ($is_create && $parent) {
         $v_title = pht('Fork of %s', $parent->getFullName());
         $v_language = $parent->getLanguage();
         $v_text = $parent->getRawContent();
         $v_space = $parent->getSpacePHID();
     } else {
         $v_title = $paste->getTitle();
         $v_language = $paste->getLanguage();
         $v_text = $paste->getRawContent();
     }
     $v_view_policy = $paste->getViewPolicy();
     $v_edit_policy = $paste->getEditPolicy();
     if ($is_create) {
         $v_projects = array();
     } else {
         $v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs($paste->getPHID(), PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
         $v_projects = array_reverse($v_projects);
     }
     $validation_exception = null;
     if ($request->isFormPost()) {
         $xactions = array();
         $v_text = $request->getStr('text');
         $v_title = $request->getStr('title');
         $v_language = $request->getStr('language');
         $v_view_policy = $request->getStr('can_view');
         $v_edit_policy = $request->getStr('can_edit');
         $v_projects = $request->getArr('projects');
         $v_space = $request->getStr('spacePHID');
         // NOTE: The author is the only editor and can always view the paste,
         // so it's impossible for them to choose an invalid policy.
         if ($is_create || $v_text !== $paste->getRawContent()) {
             $file = PhabricatorPasteEditor::initializeFileForPaste($user, $v_title, $v_text);
             $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorPasteTransaction::TYPE_CONTENT)->setNewValue($file->getPHID());
         }
         $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorPasteTransaction::TYPE_TITLE)->setNewValue($v_title);
         $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorPasteTransaction::TYPE_LANGUAGE)->setNewValue($v_language);
         $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)->setNewValue($v_view_policy);
         $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)->setNewValue($v_edit_policy);
         $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorTransactions::TYPE_SPACE)->setNewValue($v_space);
         $proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
         $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorTransactions::TYPE_EDGE)->setMetadataValue('edge:type', $proj_edge_type)->setNewValue(array('=' => array_fuse($v_projects)));
         $editor = id(new PhabricatorPasteEditor())->setActor($user)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true);
         try {
             $xactions = $editor->applyTransactions($paste, $xactions);
             return id(new AphrontRedirectResponse())->setURI($paste->getURI());
         } catch (PhabricatorApplicationTransactionValidationException $ex) {
             $validation_exception = $ex;
         }
     }
     $form = new AphrontFormView();
     $langs = array('' => pht('(Detect From Filename in Title)')) + PhabricatorEnv::getEnvConfig('pygments.dropdown-choices');
     $form->setUser($user)->addHiddenInput('parent', $parent_id)->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Title'))->setValue($v_title)->setName('title'))->appendChild(id(new AphrontFormSelectControl())->setLabel(pht('Language'))->setName('language')->setValue($v_language)->setOptions($langs));
     $policies = id(new PhabricatorPolicyQuery())->setViewer($user)->setObject($paste)->execute();
     $form->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_VIEW)->setPolicyObject($paste)->setPolicies($policies)->setValue($v_view_policy)->setSpacePHID($v_space)->setName('can_view'));
     $form->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_EDIT)->setPolicyObject($paste)->setPolicies($policies)->setValue($v_edit_policy)->setName('can_edit'));
     $form->appendControl(id(new AphrontFormTokenizerControl())->setLabel(pht('Projects'))->setName('projects')->setValue($v_projects)->setDatasource(new PhabricatorProjectDatasource()));
     $form->appendChild(id(new AphrontFormTextAreaControl())->setLabel(pht('Text'))->setValue($v_text)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setCustomClass('PhabricatorMonospaced')->setName('text'));
     $submit = new AphrontFormSubmitControl();
     if (!$is_create) {
         $submit->addCancelButton($paste->getURI());
         $submit->setValue(pht('Save Paste'));
         $title = pht('Edit %s', $paste->getFullName());
         $short = pht('Edit');
     } else {
         $submit->setValue(pht('Create Paste'));
         $title = pht('Create New Paste');
         $short = pht('Create');
     }
     $form->appendChild($submit);
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($title)->setForm($form);
     if ($validation_exception) {
         $form_box->setValidationException($validation_exception);
     }
     $crumbs = $this->buildApplicationCrumbs($this->buildSideNavView());
     if (!$is_create) {
         $crumbs->addTextCrumb('P' . $paste->getID(), '/P' . $paste->getID());
     }
     $crumbs->addTextCrumb($short);
     return $this->buildApplicationPage(array($crumbs, $form_box), array('title' => $title));
 }