@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator

Add Merchants to Phortune

Summary:
Ref T2787. Currently, you add payment providers (Stripe, Paypal, etc) in global configuration.

Generally, this approach is cumbersome, limiting, and often hard for users to figure out. It also doesn't provide a natural way to segment payment receivers or provide web access to administrative payment functions like issuing refunds, canceling orders, etc. I think that stuff definitely needs to be in the web UI, and the rule for access to it can't reasonably just be "all administrators" in a lot of reasonable cases.

The only real advantage is that it prevents an attacker from adjusting settings and pointing something at an account they control. But this attack can be mitigated through notifications, some sort of CLI-only merchant lock, payment accounts being relatively identifiable, etc.

So introduce "merchants", which are basically payable entities. An individual merchant will have attached Paypal, Stripe, etc., accounts, and access rules. When you buy something in an application, the merchant to pay is also specified. They also provide an umbrella for dealing with permissions down the line.

This may get a //little// cumbersome because if there are several merchants your saved card information is not shared across them. I think that will be fine in the normal case (most installs will have only one merchant). Even if it isn't and we leave providers global, I think introducing this is the right call from a web UI / permissions point of view. I'll play around with it in the next couple of diffs and figure out exactly where the line goes.

Test Plan: Listed, created, edited, viewed merchants.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T2787

Differential Revision: https://secure.phabricator.com/D10648

+806
+10
resources/sql/autopatches/20141006.phortunemerchant.sql
··· 1 + CREATE TABLE {$NAMESPACE}_phortune.phortune_merchant ( 2 + id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 3 + phid VARCHAR(64) NOT NULL COLLATE utf8_bin, 4 + name VARCHAR(255) NOT NULL COLLATE utf8_bin, 5 + viewPolicy VARCHAR(64) NOT NULL COLLATE utf8_bin, 6 + editPolicy VARCHAR(64) NOT NULL COLLATE utf8_bin, 7 + dateCreated INT UNSIGNED NOT NULL, 8 + dateModified INT UNSIGNED NOT NULL, 9 + UNIQUE KEY `key_phid` (phid) 10 + ) ENGINE=InnoDB, COLLATE=utf8_bin;
+19
resources/sql/autopatches/20141006.phortunemerchantx.sql
··· 1 + CREATE TABLE {$NAMESPACE}_phortune.phortune_merchanttransaction ( 2 + id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 3 + phid VARCHAR(64) COLLATE utf8_bin NOT NULL, 4 + authorPHID VARCHAR(64) COLLATE utf8_bin NOT NULL, 5 + objectPHID VARCHAR(64) COLLATE utf8_bin NOT NULL, 6 + viewPolicy VARCHAR(64) COLLATE utf8_bin NOT NULL, 7 + editPolicy VARCHAR(64) COLLATE utf8_bin NOT NULL, 8 + commentPHID VARCHAR(64) COLLATE utf8_bin DEFAULT NULL, 9 + commentVersion INT UNSIGNED NOT NULL, 10 + transactionType VARCHAR(32) COLLATE utf8_bin NOT NULL, 11 + oldValue LONGTEXT COLLATE utf8_bin NOT NULL, 12 + newValue LONGTEXT COLLATE utf8_bin NOT NULL, 13 + contentSource LONGTEXT COLLATE utf8_bin NOT NULL, 14 + metadata LONGTEXT COLLATE utf8_bin NOT NULL, 15 + dateCreated INT UNSIGNED NOT NULL, 16 + dateModified INT UNSIGNED NOT NULL, 17 + UNIQUE KEY `key_phid` (`phid`), 18 + KEY `key_object` (`objectPHID`) 19 + ) ENGINE=InnoDB, COLLATE utf8_general_ci;
+27
src/__phutil_library_map__.php
··· 2573 2573 'PhortuneDAO' => 'applications/phortune/storage/PhortuneDAO.php', 2574 2574 'PhortuneErrCode' => 'applications/phortune/constants/PhortuneErrCode.php', 2575 2575 'PhortuneLandingController' => 'applications/phortune/controller/PhortuneLandingController.php', 2576 + 'PhortuneMerchant' => 'applications/phortune/storage/PhortuneMerchant.php', 2577 + 'PhortuneMerchantCapability' => 'applications/phortune/capability/PhortuneMerchantCapability.php', 2578 + 'PhortuneMerchantController' => 'applications/phortune/controller/PhortuneMerchantController.php', 2579 + 'PhortuneMerchantEditController' => 'applications/phortune/controller/PhortuneMerchantEditController.php', 2580 + 'PhortuneMerchantEditor' => 'applications/phortune/editor/PhortuneMerchantEditor.php', 2581 + 'PhortuneMerchantListController' => 'applications/phortune/controller/PhortuneMerchantListController.php', 2582 + 'PhortuneMerchantPHIDType' => 'applications/phortune/phid/PhortuneMerchantPHIDType.php', 2583 + 'PhortuneMerchantQuery' => 'applications/phortune/query/PhortuneMerchantQuery.php', 2584 + 'PhortuneMerchantSearchEngine' => 'applications/phortune/query/PhortuneMerchantSearchEngine.php', 2585 + 'PhortuneMerchantTransaction' => 'applications/phortune/storage/PhortuneMerchantTransaction.php', 2586 + 'PhortuneMerchantTransactionQuery' => 'applications/phortune/query/PhortuneMerchantTransactionQuery.php', 2587 + 'PhortuneMerchantViewController' => 'applications/phortune/controller/PhortuneMerchantViewController.php', 2576 2588 'PhortuneMonthYearExpiryControl' => 'applications/phortune/control/PhortuneMonthYearExpiryControl.php', 2577 2589 'PhortuneMultiplePaymentProvidersException' => 'applications/phortune/exception/PhortuneMultiplePaymentProvidersException.php', 2578 2590 'PhortuneNoPaymentProviderException' => 'applications/phortune/exception/PhortuneNoPaymentProviderException.php', ··· 5608 5620 'PhortuneDAO' => 'PhabricatorLiskDAO', 5609 5621 'PhortuneErrCode' => 'PhortuneConstants', 5610 5622 'PhortuneLandingController' => 'PhortuneController', 5623 + 'PhortuneMerchant' => array( 5624 + 'PhortuneDAO', 5625 + 'PhabricatorPolicyInterface', 5626 + ), 5627 + 'PhortuneMerchantCapability' => 'PhabricatorPolicyCapability', 5628 + 'PhortuneMerchantController' => 'PhortuneController', 5629 + 'PhortuneMerchantEditController' => 'PhortuneMerchantController', 5630 + 'PhortuneMerchantEditor' => 'PhabricatorApplicationTransactionEditor', 5631 + 'PhortuneMerchantListController' => 'PhortuneMerchantController', 5632 + 'PhortuneMerchantPHIDType' => 'PhabricatorPHIDType', 5633 + 'PhortuneMerchantQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 5634 + 'PhortuneMerchantSearchEngine' => 'PhabricatorApplicationSearchEngine', 5635 + 'PhortuneMerchantTransaction' => 'PhabricatorApplicationTransaction', 5636 + 'PhortuneMerchantTransactionQuery' => 'PhabricatorApplicationTransactionQuery', 5637 + 'PhortuneMerchantViewController' => 'PhortuneMerchantController', 5611 5638 'PhortuneMonthYearExpiryControl' => 'AphrontFormControl', 5612 5639 'PhortuneMultiplePaymentProvidersException' => 'Exception', 5613 5640 'PhortuneNoPaymentProviderException' => 'Exception',
+14
src/applications/phortune/application/PhabricatorPhortuneApplication.php
··· 60 60 ), 61 61 'provider/(?P<digest>[^/]+)/(?P<action>[^/]+)/' 62 62 => 'PhortuneProviderController', 63 + 'merchant/' => array( 64 + '(?:query/(?P<queryKey>[^/]+)/)?' => 'PhortuneMerchantListController', 65 + 'edit/(?:(?P<id>\d+)/)?' => 'PhortuneMerchantEditController', 66 + '(?P<id>\d+)/' => 'PhortuneMerchantViewController', 67 + ), 68 + ), 69 + ); 70 + } 71 + 72 + protected function getCustomCapabilities() { 73 + return array( 74 + PhortuneMerchantCapability::CAPABILITY => array( 75 + 'caption' => pht('Merchant accounts can receive payments.'), 76 + 'default' => PhabricatorPolicies::POLICY_ADMIN, 63 77 ), 64 78 ); 65 79 }
+17
src/applications/phortune/capability/PhortuneMerchantCapability.php
··· 1 + <?php 2 + 3 + final class PhortuneMerchantCapability 4 + extends PhabricatorPolicyCapability { 5 + 6 + const CAPABILITY = 'phortune.merchant'; 7 + 8 + public function getCapabilityName() { 9 + return pht('Can Create Merchants'); 10 + } 11 + 12 + public function describeCapabilityRejection() { 13 + return pht( 14 + 'You do not have permission to create Phortune merchant accounts.'); 15 + } 16 + 17 + }
+13
src/applications/phortune/controller/PhortuneMerchantController.php
··· 1 + <?php 2 + 3 + abstract class PhortuneMerchantController 4 + extends PhortuneController { 5 + 6 + public function buildApplicationCrumbs() { 7 + $crumbs = parent::buildApplicationCrumbs(); 8 + $crumbs->addTextCrumb( 9 + pht('Merchants'), 10 + $this->getApplicationURI('merchant/')); 11 + return $crumbs; 12 + } 13 + }
+155
src/applications/phortune/controller/PhortuneMerchantEditController.php
··· 1 + <?php 2 + 3 + final class PhortuneMerchantEditController 4 + extends PhortuneMerchantController { 5 + 6 + private $id; 7 + 8 + public function willProcessRequest(array $data) { 9 + $this->id = idx($data, 'id'); 10 + } 11 + 12 + public function processRequest() { 13 + $request = $this->getRequest(); 14 + $viewer = $request->getUser(); 15 + 16 + if ($this->id) { 17 + $merchant = id(new PhortuneMerchantQuery()) 18 + ->setViewer($viewer) 19 + ->withIDs(array($this->id)) 20 + ->requireCapabilities( 21 + array( 22 + PhabricatorPolicyCapability::CAN_VIEW, 23 + PhabricatorPolicyCapability::CAN_EDIT, 24 + )) 25 + ->executeOne(); 26 + if (!$merchant) { 27 + return new Aphront404Response(); 28 + } 29 + $is_new = false; 30 + } else { 31 + $this->requireApplicationCapability( 32 + PhortuneMerchantCapability::CAPABILITY); 33 + 34 + $merchant = PhortuneMerchant::initializeNewMerchant($viewer); 35 + $is_new = true; 36 + } 37 + 38 + if ($is_new) { 39 + $title = pht('Create Merchant'); 40 + $button_text = pht('Create Merchant'); 41 + $cancel_uri = $this->getApplicationURI('merchant/'); 42 + } else { 43 + $title = pht( 44 + 'Edit Merchant %d %s', 45 + $merchant->getID(), 46 + $merchant->getName()); 47 + $button_text = pht('Save Changes'); 48 + $cancel_uri = $this->getApplicationURI( 49 + '/merchant/'.$merchant->getID().'/'); 50 + } 51 + 52 + $e_name = true; 53 + $v_name = $merchant->getName(); 54 + 55 + $validation_exception = null; 56 + if ($request->isFormPost()) { 57 + $v_name = $request->getStr('name'); 58 + $v_view = $request->getStr('viewPolicy'); 59 + $v_edit = $request->getStr('editPolicy'); 60 + 61 + $type_name = PhortuneMerchantTransaction::TYPE_NAME; 62 + $type_view = PhabricatorTransactions::TYPE_VIEW_POLICY; 63 + $type_edit = PhabricatorTransactions::TYPE_EDIT_POLICY; 64 + 65 + $xactions = array(); 66 + 67 + $xactions[] = id(new PhortuneMerchantTransaction()) 68 + ->setTransactionType($type_name) 69 + ->setNewValue($v_name); 70 + 71 + $xactions[] = id(new PhortuneMerchantTransaction()) 72 + ->setTransactionType($type_view) 73 + ->setNewValue($v_view); 74 + 75 + $xactions[] = id(new PhortuneMerchantTransaction()) 76 + ->setTransactionType($type_edit) 77 + ->setNewValue($v_edit); 78 + 79 + $editor = id(new PhortuneMerchantEditor()) 80 + ->setActor($viewer) 81 + ->setContentSourceFromRequest($request) 82 + ->setContinueOnNoEffect(true); 83 + 84 + try { 85 + $editor->applyTransactions($merchant, $xactions); 86 + 87 + $id = $merchant->getID(); 88 + $merchant_uri = $this->getApplicationURI("merchant/{$id}/"); 89 + return id(new AphrontRedirectResponse())->setURI($merchant_uri); 90 + } catch (PhabricatorApplicationTransactionValidationException $ex) { 91 + $validation_exception = $ex; 92 + 93 + $e_name = $ex->getShortMessage($type_name); 94 + 95 + $merchant->setViewPolicy($v_view); 96 + $merchant->setEditPolicy($v_edit); 97 + } 98 + } 99 + 100 + $policies = id(new PhabricatorPolicyQuery()) 101 + ->setViewer($viewer) 102 + ->setObject($merchant) 103 + ->execute(); 104 + 105 + $form = id(new AphrontFormView()) 106 + ->setUser($viewer) 107 + ->appendChild( 108 + id(new AphrontFormTextControl()) 109 + ->setName('name') 110 + ->setLabel(pht('Name')) 111 + ->setValue($v_name) 112 + ->setError($e_name)) 113 + ->appendChild( 114 + id(new AphrontFormPolicyControl()) 115 + ->setName('viewPolicy') 116 + ->setPolicyObject($merchant) 117 + ->setCapability(PhabricatorPolicyCapability::CAN_VIEW) 118 + ->setPolicies($policies)) 119 + ->appendChild( 120 + id(new AphrontFormPolicyControl()) 121 + ->setName('editPolicy') 122 + ->setPolicyObject($merchant) 123 + ->setCapability(PhabricatorPolicyCapability::CAN_EDIT) 124 + ->setPolicies($policies)) 125 + ->appendChild( 126 + id(new AphrontFormSubmitControl()) 127 + ->setValue($button_text) 128 + ->addCancelButton($cancel_uri)); 129 + 130 + $crumbs = $this->buildApplicationCrumbs(); 131 + if ($is_new) { 132 + $crumbs->addTextCrumb(pht('Create Merchant')); 133 + } else { 134 + $crumbs->addTextCrumb( 135 + pht('Merchant %d', $merchant->getID()), 136 + $this->getApplicationURI('/merchant/'.$merchant->getID().'/')); 137 + $crumbs->addTextCrumb(pht('Edit')); 138 + } 139 + 140 + $box = id(new PHUIObjectBoxView()) 141 + ->setValidationException($validation_exception) 142 + ->setHeaderText($title) 143 + ->appendChild($form); 144 + 145 + return $this->buildApplicationPage( 146 + array( 147 + $crumbs, 148 + $box, 149 + ), 150 + array( 151 + 'title' => $title, 152 + )); 153 + } 154 + 155 + }
+58
src/applications/phortune/controller/PhortuneMerchantListController.php
··· 1 + <?php 2 + 3 + final class PhortuneMerchantListController 4 + extends PhortuneMerchantController { 5 + 6 + private $queryKey; 7 + 8 + public function shouldAllowPublic() { 9 + return true; 10 + } 11 + 12 + public function willProcessRequest(array $data) { 13 + $this->queryKey = idx($data, 'queryKey'); 14 + } 15 + 16 + public function processRequest() { 17 + $request = $this->getRequest(); 18 + $controller = id(new PhabricatorApplicationSearchController($request)) 19 + ->setQueryKey($this->queryKey) 20 + ->setSearchEngine(new PhortuneMerchantSearchEngine()) 21 + ->setNavigation($this->buildSideNavView()); 22 + 23 + return $this->delegateToController($controller); 24 + } 25 + 26 + public function buildSideNavView() { 27 + $viewer = $this->getRequest()->getUser(); 28 + 29 + $nav = new AphrontSideNavFilterView(); 30 + $nav->setBaseURI(new PhutilURI($this->getApplicationURI())); 31 + 32 + id(new PhortuneMerchantSearchEngine()) 33 + ->setViewer($viewer) 34 + ->addNavigationItems($nav->getMenu()); 35 + 36 + $nav->selectFilter(null); 37 + 38 + return $nav; 39 + } 40 + 41 + public function buildApplicationCrumbs() { 42 + $crumbs = parent::buildApplicationCrumbs(); 43 + 44 + $can_create = $this->hasApplicationCapability( 45 + PhortuneMerchantCapability::CAPABILITY); 46 + 47 + $crumbs->addAction( 48 + id(new PHUIListItemView()) 49 + ->setName(pht('Create Merchant')) 50 + ->setHref($this->getApplicationURI('merchant/edit/')) 51 + ->setIcon('fa-plus-square') 52 + ->setWorkflow(!$can_create) 53 + ->setDisabled(!$can_create)); 54 + 55 + return $crumbs; 56 + } 57 + 58 + }
+101
src/applications/phortune/controller/PhortuneMerchantViewController.php
··· 1 + <?php 2 + 3 + final class PhortuneMerchantViewController 4 + extends PhortuneMerchantController { 5 + 6 + private $id; 7 + 8 + public function willProcessRequest(array $data) { 9 + $this->id = $data['id']; 10 + } 11 + 12 + public function processRequest() { 13 + $request = $this->getRequest(); 14 + $viewer = $request->getUser(); 15 + 16 + $merchant = id(new PhortuneMerchantQuery()) 17 + ->setViewer($viewer) 18 + ->withIDs(array($this->id)) 19 + ->executeOne(); 20 + if (!$merchant) { 21 + return new Aphront404Response(); 22 + } 23 + 24 + $crumbs = $this->buildApplicationCrumbs(); 25 + $crumbs->addTextCrumb(pht('Merchant %d', $merchant->getID())); 26 + 27 + $title = pht( 28 + 'Merchant %d %s', 29 + $merchant->getID(), 30 + $merchant->getName()); 31 + 32 + $header = id(new PHUIHeaderView()) 33 + ->setObjectName(pht('Merchant %d', $merchant->getID())) 34 + ->setHeader($merchant->getName()) 35 + ->setUser($viewer) 36 + ->setPolicyObject($merchant); 37 + 38 + $properties = $this->buildPropertyListView($merchant); 39 + $actions = $this->buildActionListView($merchant); 40 + $properties->setActionList($actions); 41 + 42 + $box = id(new PHUIObjectBoxView()) 43 + ->setHeader($header) 44 + ->appendChild($properties); 45 + 46 + $xactions = id(new PhortuneMerchantTransactionQuery()) 47 + ->setViewer($viewer) 48 + ->withObjectPHIDs(array($merchant->getPHID())) 49 + ->execute(); 50 + 51 + $timeline = id(new PhabricatorApplicationTransactionView()) 52 + ->setUser($viewer) 53 + ->setObjectPHID($merchant->getPHID()) 54 + ->setTransactions($xactions); 55 + 56 + return $this->buildApplicationPage( 57 + array( 58 + $crumbs, 59 + $box, 60 + $timeline, 61 + ), 62 + array( 63 + 'title' => $title, 64 + )); 65 + } 66 + 67 + private function buildPropertyListView(PhortuneMerchant $merchant) { 68 + $viewer = $this->getRequest()->getUser(); 69 + 70 + $view = id(new PHUIPropertyListView()) 71 + ->setUser($viewer) 72 + ->setObject($merchant); 73 + 74 + return $view; 75 + } 76 + 77 + private function buildActionListView(PhortuneMerchant $merchant) { 78 + $viewer = $this->getRequest()->getUser(); 79 + $id = $merchant->getID(); 80 + 81 + $can_edit = PhabricatorPolicyFilter::hasCapability( 82 + $viewer, 83 + $merchant, 84 + PhabricatorPolicyCapability::CAN_EDIT); 85 + 86 + $view = id(new PhabricatorActionListView()) 87 + ->setUser($viewer) 88 + ->setObject($merchant); 89 + 90 + $view->addAction( 91 + id(new PhabricatorActionView()) 92 + ->setName(pht('Edit Merchant')) 93 + ->setIcon('fa-pencil') 94 + ->setDisabled(!$can_edit) 95 + ->setWorkflow(!$can_edit) 96 + ->setHref($this->getApplicationURI("merchant/edit/{$id}/"))); 97 + 98 + return $view; 99 + } 100 + 101 + }
+102
src/applications/phortune/editor/PhortuneMerchantEditor.php
··· 1 + <?php 2 + 3 + final class PhortuneMerchantEditor 4 + extends PhabricatorApplicationTransactionEditor { 5 + 6 + public function getEditorApplicationClass() { 7 + return 'PhabricatorPhortuneApplication'; 8 + } 9 + 10 + public function getEditorObjectsDescription() { 11 + return pht('Phortune Merchants'); 12 + } 13 + 14 + public function getTransactionTypes() { 15 + $types = parent::getTransactionTypes(); 16 + 17 + $types[] = PhortuneMerchantTransaction::TYPE_NAME; 18 + $types[] = PhabricatorTransactions::TYPE_VIEW_POLICY; 19 + $types[] = PhabricatorTransactions::TYPE_EDIT_POLICY; 20 + 21 + return $types; 22 + } 23 + 24 + protected function getCustomTransactionOldValue( 25 + PhabricatorLiskDAO $object, 26 + PhabricatorApplicationTransaction $xaction) { 27 + switch ($xaction->getTransactionType()) { 28 + case PhortuneMerchantTransaction::TYPE_NAME: 29 + return $object->getName(); 30 + } 31 + 32 + return parent::getCustomTransactionOldValue($object, $xaction); 33 + } 34 + 35 + protected function getCustomTransactionNewValue( 36 + PhabricatorLiskDAO $object, 37 + PhabricatorApplicationTransaction $xaction) { 38 + 39 + switch ($xaction->getTransactionType()) { 40 + case PhortuneMerchantTransaction::TYPE_NAME: 41 + return $xaction->getNewValue(); 42 + } 43 + 44 + return parent::getCustomTransactionNewValue($object, $xaction); 45 + } 46 + 47 + protected function applyCustomInternalTransaction( 48 + PhabricatorLiskDAO $object, 49 + PhabricatorApplicationTransaction $xaction) { 50 + 51 + switch ($xaction->getTransactionType()) { 52 + case PhortuneMerchantTransaction::TYPE_NAME: 53 + $object->setName($xaction->getNewValue()); 54 + return; 55 + } 56 + 57 + return parent::applyCustomInternalTransaction($object, $xaction); 58 + } 59 + 60 + protected function applyCustomExternalTransaction( 61 + PhabricatorLiskDAO $object, 62 + PhabricatorApplicationTransaction $xaction) { 63 + 64 + switch ($xaction->getTransactionType()) { 65 + case PhortuneMerchantTransaction::TYPE_NAME: 66 + return; 67 + } 68 + 69 + return parent::applyCustomExternalTransaction($object, $xaction); 70 + } 71 + 72 + protected function validateTransaction( 73 + PhabricatorLiskDAO $object, 74 + $type, 75 + array $xactions) { 76 + 77 + $errors = parent::validateTransaction($object, $type, $xactions); 78 + 79 + switch ($type) { 80 + case PhortuneMerchantTransaction::TYPE_NAME: 81 + $missing = $this->validateIsEmptyTextField( 82 + $object->getName(), 83 + $xactions); 84 + 85 + if ($missing) { 86 + $error = new PhabricatorApplicationTransactionValidationError( 87 + $type, 88 + pht('Required'), 89 + pht('Merchant name is required.'), 90 + nonempty(last($xactions), null)); 91 + 92 + $error->setIsMissingFieldError(true); 93 + $errors[] = $error; 94 + } 95 + break; 96 + } 97 + 98 + return $errors; 99 + } 100 + 101 + 102 + }
+38
src/applications/phortune/phid/PhortuneMerchantPHIDType.php
··· 1 + <?php 2 + 3 + final class PhortuneMerchantPHIDType extends PhabricatorPHIDType { 4 + 5 + const TYPECONST = 'PMRC'; 6 + 7 + public function getTypeName() { 8 + return pht('Phortune Merchant'); 9 + } 10 + 11 + public function newObject() { 12 + return new PhortuneMerchant(); 13 + } 14 + 15 + protected function buildQueryForObjects( 16 + PhabricatorObjectQuery $query, 17 + array $phids) { 18 + 19 + return id(new PhortuneMerchantQuery()) 20 + ->withPHIDs($phids); 21 + } 22 + 23 + public function loadHandles( 24 + PhabricatorHandleQuery $query, 25 + array $handles, 26 + array $objects) { 27 + 28 + foreach ($handles as $phid => $handle) { 29 + $merchant = $objects[$phid]; 30 + 31 + $id = $merchant->getID(); 32 + 33 + $handle->setName(pht('Merchant %d', $id)); 34 + $handle->setURI("/phortune/merchant/{$id}/"); 35 + } 36 + } 37 + 38 + }
+60
src/applications/phortune/query/PhortuneMerchantQuery.php
··· 1 + <?php 2 + 3 + final class PhortuneMerchantQuery 4 + extends PhabricatorCursorPagedPolicyAwareQuery { 5 + 6 + private $ids; 7 + private $phids; 8 + 9 + public function withIDs(array $ids) { 10 + $this->ids = $ids; 11 + return $this; 12 + } 13 + 14 + public function withPHIDs(array $phids) { 15 + $this->phids = $phids; 16 + return $this; 17 + } 18 + 19 + protected function loadPage() { 20 + $table = new PhortuneMerchant(); 21 + $conn = $table->establishConnection('r'); 22 + 23 + $rows = queryfx_all( 24 + $conn, 25 + 'SELECT * FROM %T %Q %Q %Q', 26 + $table->getTableName(), 27 + $this->buildWhereClause($conn), 28 + $this->buildOrderClause($conn), 29 + $this->buildLimitClause($conn)); 30 + 31 + return $table->loadAllFromArray($rows); 32 + } 33 + 34 + private function buildWhereClause(AphrontDatabaseConnection $conn) { 35 + $where = array(); 36 + 37 + if ($this->ids !== null) { 38 + $where[] = qsprintf( 39 + $conn, 40 + 'id IN (%Ld)', 41 + $this->ids); 42 + } 43 + 44 + if ($this->phids !== null) { 45 + $where[] = qsprintf( 46 + $conn, 47 + 'phid IN (%Ls)', 48 + $this->phids); 49 + } 50 + 51 + $where[] = $this->buildPagingClause($conn); 52 + 53 + return $this->formatWhereClause($where); 54 + } 55 + 56 + public function getQueryApplicationClass() { 57 + return 'PhabricatorPhortuneApplication'; 58 + } 59 + 60 + }
+79
src/applications/phortune/query/PhortuneMerchantSearchEngine.php
··· 1 + <?php 2 + 3 + final class PhortuneMerchantSearchEngine 4 + extends PhabricatorApplicationSearchEngine { 5 + 6 + public function getResultTypeDescription() { 7 + return pht('Phortune Merchants'); 8 + } 9 + 10 + public function buildSavedQueryFromRequest(AphrontRequest $request) { 11 + $saved = new PhabricatorSavedQuery(); 12 + 13 + return $saved; 14 + } 15 + 16 + public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { 17 + $query = id(new PhortuneMerchantQuery()); 18 + 19 + return $query; 20 + } 21 + 22 + public function buildSearchForm( 23 + AphrontFormView $form, 24 + PhabricatorSavedQuery $saved_query) {} 25 + 26 + protected function getURI($path) { 27 + return '/phortune/merchant/'.$path; 28 + } 29 + 30 + public function getBuiltinQueryNames() { 31 + $names = array( 32 + 'all' => pht('All Merchants'), 33 + ); 34 + 35 + return $names; 36 + } 37 + 38 + public function buildSavedQueryFromBuiltin($query_key) { 39 + 40 + $query = $this->newSavedQuery(); 41 + $query->setQueryKey($query_key); 42 + 43 + switch ($query_key) { 44 + case 'all': 45 + return $query; 46 + } 47 + 48 + return parent::buildSavedQueryFromBuiltin($query_key); 49 + } 50 + 51 + protected function getRequiredHandlePHIDsForResultList( 52 + array $merchants, 53 + PhabricatorSavedQuery $query) { 54 + return array(); 55 + } 56 + 57 + protected function renderResultList( 58 + array $merchants, 59 + PhabricatorSavedQuery $query, 60 + array $handles) { 61 + assert_instances_of($merchants, 'PhortuneMerchant'); 62 + 63 + $viewer = $this->requireViewer(); 64 + 65 + $list = new PHUIObjectItemListView(); 66 + $list->setUser($viewer); 67 + foreach ($merchants as $merchant) { 68 + $item = id(new PHUIObjectItemView()) 69 + ->setObjectName(pht('Merchant %d', $merchant->getID())) 70 + ->setHeader($merchant->getName()) 71 + ->setHref('/phortune/merchant/'.$merchant->getID().'/') 72 + ->setObject($merchant); 73 + 74 + $list->addItem($item); 75 + } 76 + 77 + return $list; 78 + } 79 + }
+10
src/applications/phortune/query/PhortuneMerchantTransactionQuery.php
··· 1 + <?php 2 + 3 + final class PhortuneMerchantTransactionQuery 4 + extends PhabricatorApplicationTransactionQuery { 5 + 6 + public function getTemplateApplicationTransaction() { 7 + return new PhortuneMerchantTransaction(); 8 + } 9 + 10 + }
+58
src/applications/phortune/storage/PhortuneMerchant.php
··· 1 + <?php 2 + 3 + final class PhortuneMerchant extends PhortuneDAO 4 + implements PhabricatorPolicyInterface { 5 + 6 + protected $name; 7 + protected $viewPolicy; 8 + protected $editPolicy; 9 + 10 + public static function initializeNewMerchant(PhabricatorUser $actor) { 11 + return id(new PhortuneMerchant()) 12 + ->setViewPolicy(PhabricatorPolicies::getMostOpenPolicy()) 13 + ->setEditPolicy($actor->getPHID()); 14 + } 15 + 16 + public function getConfiguration() { 17 + return array( 18 + self::CONFIG_AUX_PHID => true, 19 + self::CONFIG_COLUMN_SCHEMA => array( 20 + 'name' => 'text255', 21 + ), 22 + ) + parent::getConfiguration(); 23 + } 24 + 25 + public function generatePHID() { 26 + return PhabricatorPHID::generateNewPHID( 27 + PhortuneMerchantPHIDType::TYPECONST); 28 + } 29 + 30 + 31 + /* -( PhabricatorPolicyInterface )----------------------------------------- */ 32 + 33 + 34 + public function getCapabilities() { 35 + return array( 36 + PhabricatorPolicyCapability::CAN_VIEW, 37 + PhabricatorPolicyCapability::CAN_EDIT, 38 + ); 39 + } 40 + 41 + public function getPolicy($capability) { 42 + switch ($capability) { 43 + case PhabricatorPolicyCapability::CAN_VIEW: 44 + return $this->getViewPolicy(); 45 + case PhabricatorPolicyCapability::CAN_EDIT: 46 + return $this->getEditPolicy(); 47 + } 48 + } 49 + 50 + public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { 51 + return false; 52 + } 53 + 54 + public function describeAutomaticCapability($capability) { 55 + return null; 56 + } 57 + 58 + }
+45
src/applications/phortune/storage/PhortuneMerchantTransaction.php
··· 1 + <?php 2 + 3 + final class PhortuneMerchantTransaction 4 + extends PhabricatorApplicationTransaction { 5 + 6 + const TYPE_NAME = 'merchant:name'; 7 + 8 + public function getApplicationName() { 9 + return 'phortune'; 10 + } 11 + 12 + public function getApplicationTransactionType() { 13 + return PhortuneMerchantPHIDType::TYPECONST; 14 + } 15 + 16 + public function getApplicationTransactionCommentObject() { 17 + return null; 18 + } 19 + 20 + public function getTitle() { 21 + $author_phid = $this->getAuthorPHID(); 22 + 23 + $old = $this->getOldValue(); 24 + $new = $this->getNewValue(); 25 + 26 + switch ($this->getTransactionType()) { 27 + case self::TYPE_NAME: 28 + if ($old === null) { 29 + return pht( 30 + '%s created this merchant.', 31 + $this->renderHandleLink($author_phid)); 32 + } else { 33 + return pht( 34 + '%s renamed this merchant from "%s" to "%s".', 35 + $this->renderHandleLink($author_phid), 36 + $old, 37 + $new); 38 + } 39 + break; 40 + } 41 + 42 + return parent::getTitle(); 43 + } 44 + 45 + }