Path: blob/master/src/applications/metamta/applicationpanel/PhabricatorMetaMTAApplicationEmailPanel.php
12256 views
<?php12final class PhabricatorMetaMTAApplicationEmailPanel3extends PhabricatorApplicationConfigurationPanel {45public function getPanelKey() {6return 'email';7}89public function shouldShowForApplication(10PhabricatorApplication $application) {11return $application->supportsEmailIntegration();12}1314public function buildConfigurationPagePanel() {15$viewer = $this->getViewer();16$application = $this->getApplication();1718$table = $this->buildEmailTable($is_edit = false, null);1920$can_edit = PhabricatorPolicyFilter::hasCapability(21$viewer,22$application,23PhabricatorPolicyCapability::CAN_EDIT);2425$header = id(new PHUIHeaderView())26->setHeader(pht('Application Emails'))27->addActionLink(28id(new PHUIButtonView())29->setTag('a')30->setText(pht('Edit Application Emails'))31->setIcon('fa-pencil')32->setHref($this->getPanelURI())33->setDisabled(!$can_edit)34->setWorkflow(!$can_edit));353637$box = id(new PHUIObjectBoxView())38->setHeader($header)39->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)40->setTable($table);4142return $box;43}4445public function handlePanelRequest(46AphrontRequest $request,47PhabricatorController $controller) {48$viewer = $request->getViewer();49$application = $this->getApplication();5051$path = $request->getURIData('path');52if (strlen($path)) {53return new Aphront404Response();54}5556$uri = new PhutilURI($request->getPath());5758$new = $request->getStr('new');59$edit = $request->getInt('edit');60$delete = $request->getInt('delete');6162if ($new) {63return $this->returnNewAddressResponse($request, $uri, $application);64}6566if ($edit) {67return $this->returnEditAddressResponse($request, $uri, $edit);68}6970if ($delete) {71return $this->returnDeleteAddressResponse($request, $uri, $delete);72}7374$table = $this->buildEmailTable(75$is_edit = true,76$request->getInt('id'));7778$form = id(new AphrontFormView())79->setUser($viewer);8081$crumbs = $controller->buildPanelCrumbs($this);82$crumbs->addTextCrumb(pht('Edit Application Emails'));83$crumbs->setBorder(true);8485$header = id(new PHUIHeaderView())86->setHeader(pht('Edit Application Emails: %s', $application->getName()))87->setSubheader($application->getAppEmailBlurb())88->setHeaderIcon('fa-pencil');8990$icon = id(new PHUIIconView())91->setIcon('fa-plus');92$button = new PHUIButtonView();93$button->setText(pht('Add New Address'));94$button->setTag('a');95$button->setHref($uri->alter('new', 'true'));96$button->setIcon($icon);97$button->addSigil('workflow');98$header->addActionLink($button);99100$object_box = id(new PHUIObjectBoxView())101->setHeaderText(pht('Emails'))102->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)103->setTable($table);104105$title = $application->getName();106$view = id(new PHUITwoColumnView())107->setHeader($header)108->setFooter($object_box);109110return $controller->buildPanelPage(111$this,112$title,113$crumbs,114$view);115}116117private function returnNewAddressResponse(118AphrontRequest $request,119PhutilURI $uri,120PhabricatorApplication $application) {121122$viewer = $request->getUser();123$email_object =124PhabricatorMetaMTAApplicationEmail::initializeNewAppEmail($viewer)125->setApplicationPHID($application->getPHID());126127return $this->returnSaveAddressResponse(128$request,129$uri,130$email_object,131$is_new = true);132}133134private function returnEditAddressResponse(135AphrontRequest $request,136PhutilURI $uri,137$email_object_id) {138139$viewer = $request->getUser();140$email_object = id(new PhabricatorMetaMTAApplicationEmailQuery())141->setViewer($viewer)142->withIDs(array($email_object_id))143->requireCapabilities(144array(145PhabricatorPolicyCapability::CAN_VIEW,146PhabricatorPolicyCapability::CAN_EDIT,147))148->executeOne();149if (!$email_object) {150return new Aphront404Response();151}152153return $this->returnSaveAddressResponse(154$request,155$uri,156$email_object,157$is_new = false);158}159160private function returnSaveAddressResponse(161AphrontRequest $request,162PhutilURI $uri,163PhabricatorMetaMTAApplicationEmail $email_object,164$is_new) {165166$viewer = $request->getUser();167168$config_default =169PhabricatorMetaMTAApplicationEmail::CONFIG_DEFAULT_AUTHOR;170171$e_email = true;172$v_email = $email_object->getAddress();173$e_space = null;174$v_space = $email_object->getSpacePHID();175$v_default = $email_object->getConfigValue($config_default);176177$validation_exception = null;178if ($request->isDialogFormPost()) {179$e_email = null;180181$v_email = trim($request->getStr('email'));182$v_space = $request->getStr('spacePHID');183$v_default = $request->getArr($config_default);184$v_default = nonempty(head($v_default), null);185186$type_address =187PhabricatorMetaMTAApplicationEmailTransaction::TYPE_ADDRESS;188$type_space = PhabricatorTransactions::TYPE_SPACE;189$type_config =190PhabricatorMetaMTAApplicationEmailTransaction::TYPE_CONFIG;191192$key_config = PhabricatorMetaMTAApplicationEmailTransaction::KEY_CONFIG;193194$xactions = array();195196$xactions[] = id(new PhabricatorMetaMTAApplicationEmailTransaction())197->setTransactionType($type_address)198->setNewValue($v_email);199200$xactions[] = id(new PhabricatorMetaMTAApplicationEmailTransaction())201->setTransactionType($type_space)202->setNewValue($v_space);203204$xactions[] = id(new PhabricatorMetaMTAApplicationEmailTransaction())205->setTransactionType($type_config)206->setMetadataValue($key_config, $config_default)207->setNewValue($v_default);208209$editor = id(new PhabricatorMetaMTAApplicationEmailEditor())210->setActor($viewer)211->setContentSourceFromRequest($request)212->setContinueOnNoEffect(true);213214try {215$editor->applyTransactions($email_object, $xactions);216217return id(new AphrontRedirectResponse())->setURI(218$uri->alter('highlight', $email_object->getID()));219} catch (PhabricatorApplicationTransactionValidationException $ex) {220$validation_exception = $ex;221$e_email = $ex->getShortMessage($type_address);222$e_space = $ex->getShortMessage($type_space);223}224}225226if ($v_default) {227$v_default = array($v_default);228} else {229$v_default = array();230}231232$form = id(new AphrontFormView())233->setUser($viewer)234->appendChild(235id(new AphrontFormTextControl())236->setLabel(pht('Email'))237->setName('email')238->setValue($v_email)239->setError($e_email));240241if (PhabricatorSpacesNamespaceQuery::getViewerSpacesExist($viewer)) {242$form->appendControl(243id(new AphrontFormSelectControl())244->setLabel(pht('Space'))245->setName('spacePHID')246->setValue($v_space)247->setError($e_space)248->setOptions(249PhabricatorSpacesNamespaceQuery::getSpaceOptionsForViewer(250$viewer,251$v_space)));252}253254$form255->appendControl(256id(new AphrontFormTokenizerControl())257->setDatasource(new PhabricatorPeopleDatasource())258->setLabel(pht('Default Author'))259->setName($config_default)260->setLimit(1)261->setValue($v_default)262->setCaption(263pht(264'Used if the "From:" address does not map to a user account. '.265'Setting a default author will allow anyone on the public '.266'internet to create objects by sending email to '.267'this address.')));268269if ($is_new) {270$title = pht('New Address');271} else {272$title = pht('Edit Address');273}274275$dialog = id(new AphrontDialogView())276->setUser($viewer)277->setWidth(AphrontDialogView::WIDTH_FORM)278->setTitle($title)279->setValidationException($validation_exception)280->appendForm($form)281->addSubmitButton(pht('Save'))282->addCancelButton($uri);283284if ($is_new) {285$dialog->addHiddenInput('new', 'true');286}287288return id(new AphrontDialogResponse())->setDialog($dialog);289}290291private function returnDeleteAddressResponse(292AphrontRequest $request,293PhutilURI $uri,294$email_object_id) {295296$viewer = $this->getViewer();297298$email_object = id(new PhabricatorMetaMTAApplicationEmailQuery())299->setViewer($viewer)300->withIDs(array($email_object_id))301->requireCapabilities(302array(303PhabricatorPolicyCapability::CAN_VIEW,304PhabricatorPolicyCapability::CAN_EDIT,305))306->executeOne();307if (!$email_object) {308return new Aphront404Response();309}310311if ($request->isDialogFormPost()) {312$engine = new PhabricatorDestructionEngine();313$engine->destroyObject($email_object);314return id(new AphrontRedirectResponse())->setURI($uri);315}316317$dialog = id(new AphrontDialogView())318->setUser($viewer)319->addHiddenInput('delete', $email_object_id)320->setTitle(pht('Delete Address'))321->appendParagraph(pht(322'Are you sure you want to delete this email address?'))323->addSubmitButton(pht('Delete'))324->addCancelButton($uri);325326return id(new AphrontDialogResponse())->setDialog($dialog);327}328329private function buildEmailTable($is_edit, $highlight) {330$viewer = $this->getViewer();331$application = $this->getApplication();332$uri = new PhutilURI($this->getPanelURI());333334$emails = id(new PhabricatorMetaMTAApplicationEmailQuery())335->setViewer($viewer)336->withApplicationPHIDs(array($application->getPHID()))337->execute();338339$rowc = array();340$rows = array();341foreach ($emails as $email) {342343$button_edit = javelin_tag(344'a',345array(346'class' => 'button small button-grey',347'href' => $uri->alter('edit', $email->getID()),348'sigil' => 'workflow',349),350pht('Edit'));351352$button_remove = javelin_tag(353'a',354array(355'class' => 'button small button-grey',356'href' => $uri->alter('delete', $email->getID()),357'sigil' => 'workflow',358),359pht('Delete'));360361if ($highlight == $email->getID()) {362$rowc[] = 'highlighted';363} else {364$rowc[] = null;365}366367$space_phid = PhabricatorSpacesNamespaceQuery::getObjectSpacePHID($email);368if ($space_phid) {369$email_space = $viewer->renderHandle($space_phid);370} else {371$email_space = null;372}373374$default_author_phid = $email->getDefaultAuthorPHID();375if (!$default_author_phid) {376$default_author = phutil_tag('em', array(), pht('None'));377} else {378$default_author = $viewer->renderHandle($default_author_phid);379}380381$rows[] = array(382$email_space,383$email->getAddress(),384$default_author,385$button_edit,386$button_remove,387);388}389390$table = id(new AphrontTableView($rows))391->setNoDataString(pht('No application emails created yet.'));392$table->setHeaders(393array(394pht('Space'),395pht('Email'),396pht('Default'),397pht('Edit'),398pht('Delete'),399));400$table->setColumnClasses(401array(402'',403'',404'wide',405'action',406'action',407));408$table->setRowClasses($rowc);409$table->setColumnVisibility(410array(411PhabricatorSpacesNamespaceQuery::getViewerSpacesExist($viewer),412true,413true,414$is_edit,415$is_edit,416));417418return $table;419}420421}422423424