|
| 1 | +<?php |
| 2 | + |
| 3 | +final class PhabricatorDashboardInstallController |
| 4 | + extends PhabricatorDashboardController { |
| 5 | + |
| 6 | + public function handleRequest(AphrontRequest $request) { |
| 7 | + $viewer = $request->getViewer(); |
| 8 | + $id = $request->getURIData('id'); |
| 9 | + |
| 10 | + $dashboard = id(new PhabricatorDashboardQuery()) |
| 11 | + ->setViewer($viewer) |
| 12 | + ->withIDs(array($id)) |
| 13 | + ->executeOne(); |
| 14 | + if (!$dashboard) { |
| 15 | + return new Aphront404Response(); |
| 16 | + } |
| 17 | + |
| 18 | + $cancel_uri = $this->getApplicationURI( |
| 19 | + 'view/'.$dashboard->getID().'/'); |
| 20 | + |
| 21 | + $home_app = new PhabricatorHomeApplication(); |
| 22 | + |
| 23 | + $options = array(); |
| 24 | + $options['home'] = array( |
| 25 | + 'personal' => |
| 26 | + array( |
| 27 | + 'capability' => PhabricatorPolicyCapability::CAN_VIEW, |
| 28 | + 'application' => $home_app, |
| 29 | + 'name' => pht('Personal Dashboard'), |
| 30 | + 'value' => 'personal', |
| 31 | + 'description' => pht('Places this dashboard as a menu item on home '. |
| 32 | + 'as a personal menu item. It will only be on your personal '. |
| 33 | + 'home.'), |
| 34 | + ), |
| 35 | + 'global' => |
| 36 | + array( |
| 37 | + 'capability' => PhabricatorPolicyCapability::CAN_EDIT, |
| 38 | + 'application' => $home_app, |
| 39 | + 'name' => pht('Global Dashboard'), |
| 40 | + 'value' => 'global', |
| 41 | + 'description' => pht('Places this dashboard as a menu item on home '. |
| 42 | + 'as a global menu item. It will be available to all users.'), |
| 43 | + ), |
| 44 | + ); |
| 45 | + |
| 46 | + |
| 47 | + $errors = array(); |
| 48 | + $v_name = null; |
| 49 | + if ($request->isFormPost()) { |
| 50 | + $menuitem = new PhabricatorDashboardProfileMenuItem(); |
| 51 | + $dashboard_phid = $dashboard->getPHID(); |
| 52 | + $home = new PhabricatorHomeApplication(); |
| 53 | + $v_name = $request->getStr('name'); |
| 54 | + $v_home = $request->getStr('home'); |
| 55 | + |
| 56 | + if ($v_home) { |
| 57 | + $application = $options['home'][$v_home]['application']; |
| 58 | + $capability = $options['home'][$v_home]['capability']; |
| 59 | + |
| 60 | + $can_edit_home = PhabricatorPolicyFilter::hasCapability( |
| 61 | + $viewer, |
| 62 | + $application, |
| 63 | + $capability); |
| 64 | + |
| 65 | + if (!$can_edit_home) { |
| 66 | + $errors[] = pht( |
| 67 | + 'You do not have permission to install a dashboard on home.'); |
| 68 | + } |
| 69 | + } else { |
| 70 | + $errors[] = pht( |
| 71 | + 'You must select a destination to install this dashboard.'); |
| 72 | + } |
| 73 | + |
| 74 | + $v_phid = $viewer->getPHID(); |
| 75 | + if ($v_home == 'global') { |
| 76 | + $v_phid = null; |
| 77 | + } |
| 78 | + |
| 79 | + if (!$errors) { |
| 80 | + $install = PhabricatorProfileMenuItemConfiguration::initializeNewItem( |
| 81 | + $home, |
| 82 | + $menuitem, |
| 83 | + $v_phid); |
| 84 | + |
| 85 | + $install->setMenuItemProperty('dashboardPHID', $dashboard_phid); |
| 86 | + $install->setMenuItemProperty('name', $v_name); |
| 87 | + $install->setMenuItemOrder(1); |
| 88 | + |
| 89 | + $xactions = array(); |
| 90 | + |
| 91 | + $editor = id(new PhabricatorProfileMenuEditor()) |
| 92 | + ->setActor($viewer) |
| 93 | + ->setContinueOnNoEffect(true) |
| 94 | + ->setContinueOnMissingFields(true) |
| 95 | + ->setContentSourceFromRequest($request); |
| 96 | + |
| 97 | + $editor->applyTransactions($install, $xactions); |
| 98 | + |
| 99 | + $view_uri = '/home/menu/view/'.$install->getID().'/'; |
| 100 | + |
| 101 | + return id(new AphrontRedirectResponse())->setURI($view_uri); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + $form = id(new AphrontFormView()) |
| 106 | + ->setUser($viewer) |
| 107 | + ->appendChild( |
| 108 | + id(new AphrontFormTextControl()) |
| 109 | + ->setLabel(pht('Menu Label')) |
| 110 | + ->setName('name') |
| 111 | + ->setValue($v_name)); |
| 112 | + |
| 113 | + $radio = id(new AphrontFormRadioButtonControl()) |
| 114 | + ->setLabel(pht('Home Menu')) |
| 115 | + ->setName('home'); |
| 116 | + |
| 117 | + foreach ($options['home'] as $type => $option) { |
| 118 | + $can_edit = PhabricatorPolicyFilter::hasCapability( |
| 119 | + $viewer, |
| 120 | + $option['application'], |
| 121 | + $option['capability']); |
| 122 | + if ($can_edit) { |
| 123 | + $radio->addButton( |
| 124 | + $option['value'], |
| 125 | + $option['name'], |
| 126 | + $option['description']); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + $form->appendChild($radio); |
| 131 | + |
| 132 | + return $this->newDialog() |
| 133 | + ->setTitle(pht('Install Dashboard')) |
| 134 | + ->setErrors($errors) |
| 135 | + ->setWidth(AphrontDialogView::WIDTH_FORM) |
| 136 | + ->appendChild($form->buildLayoutView()) |
| 137 | + ->addCancelButton($cancel_uri) |
| 138 | + ->addSubmitButton(pht('Install Dashboard')); |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments