[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/dashboard/controller/ -> PhabricatorDashboardEditController.php (source)

   1  <?php
   2  
   3  final class PhabricatorDashboardEditController
   4    extends PhabricatorDashboardController {
   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        $dashboard = id(new PhabricatorDashboardQuery())
  18          ->setViewer($viewer)
  19          ->withIDs(array($this->id))
  20          ->needPanels(true)
  21          ->requireCapabilities(
  22            array(
  23              PhabricatorPolicyCapability::CAN_VIEW,
  24              PhabricatorPolicyCapability::CAN_EDIT,
  25            ))
  26          ->executeOne();
  27        if (!$dashboard) {
  28          return new Aphront404Response();
  29        }
  30  
  31        $is_new = false;
  32      } else {
  33        if (!$request->getStr('edit')) {
  34          if ($request->isFormPost()) {
  35            switch ($request->getStr('template')) {
  36              case 'empty':
  37                break;
  38              default:
  39                return $this->processBuildTemplateRequest($request);
  40            }
  41          } else {
  42            return $this->processTemplateRequest($request);
  43          }
  44        }
  45  
  46        $dashboard = PhabricatorDashboard::initializeNewDashboard($viewer);
  47  
  48        $is_new = true;
  49      }
  50  
  51      $crumbs = $this->buildApplicationCrumbs();
  52  
  53      if ($is_new) {
  54        $title = pht('Create Dashboard');
  55        $header = pht('Create Dashboard');
  56        $button = pht('Create Dashboard');
  57        $cancel_uri = $this->getApplicationURI();
  58  
  59        $crumbs->addTextCrumb('Create Dashboard');
  60      } else {
  61        $id = $dashboard->getID();
  62        $cancel_uri = $this->getApplicationURI('manage/'.$id.'/');
  63  
  64        $title = pht('Edit Dashboard %d', $dashboard->getID());
  65        $header = pht('Edit Dashboard "%s"', $dashboard->getName());
  66        $button = pht('Save Changes');
  67  
  68        $crumbs->addTextCrumb(pht('Dashboard %d', $id), $cancel_uri);
  69        $crumbs->addTextCrumb(pht('Edit'));
  70      }
  71  
  72      $v_name = $dashboard->getName();
  73      $v_layout_mode = $dashboard->getLayoutConfigObject()->getLayoutMode();
  74      $e_name = true;
  75  
  76      $validation_exception = null;
  77      if ($request->isFormPost() && $request->getStr('edit')) {
  78        $v_name = $request->getStr('name');
  79        $v_layout_mode = $request->getStr('layout_mode');
  80        $v_view_policy = $request->getStr('viewPolicy');
  81        $v_edit_policy = $request->getStr('editPolicy');
  82  
  83        $xactions = array();
  84  
  85        $type_name = PhabricatorDashboardTransaction::TYPE_NAME;
  86        $type_layout_mode = PhabricatorDashboardTransaction::TYPE_LAYOUT_MODE;
  87        $type_view_policy = PhabricatorTransactions::TYPE_VIEW_POLICY;
  88        $type_edit_policy = PhabricatorTransactions::TYPE_EDIT_POLICY;
  89  
  90        $xactions[] = id(new PhabricatorDashboardTransaction())
  91          ->setTransactionType($type_name)
  92          ->setNewValue($v_name);
  93        $xactions[] = id(new PhabricatorDashboardTransaction())
  94          ->setTransactionType($type_layout_mode)
  95          ->setNewValue($v_layout_mode);
  96        $xactions[] = id(new PhabricatorDashboardTransaction())
  97          ->setTransactionType($type_view_policy)
  98          ->setNewValue($v_view_policy);
  99        $xactions[] = id(new PhabricatorDashboardTransaction())
 100          ->setTransactionType($type_edit_policy)
 101          ->setNewValue($v_edit_policy);
 102  
 103        try {
 104          $editor = id(new PhabricatorDashboardTransactionEditor())
 105            ->setActor($viewer)
 106            ->setContinueOnNoEffect(true)
 107            ->setContentSourceFromRequest($request)
 108            ->applyTransactions($dashboard, $xactions);
 109  
 110          $uri = $this->getApplicationURI('manage/'.$dashboard->getID().'/');
 111  
 112          return id(new AphrontRedirectResponse())->setURI($uri);
 113        } catch (PhabricatorApplicationTransactionValidationException $ex) {
 114          $validation_exception = $ex;
 115  
 116          $e_name = $validation_exception->getShortMessage($type_name);
 117  
 118          $dashboard->setViewPolicy($v_view_policy);
 119          $dashboard->setEditPolicy($v_edit_policy);
 120        }
 121      }
 122  
 123      $policies = id(new PhabricatorPolicyQuery())
 124        ->setViewer($viewer)
 125        ->setObject($dashboard)
 126        ->execute();
 127  
 128      $layout_mode_options =
 129        PhabricatorDashboardLayoutConfig::getLayoutModeSelectOptions();
 130      $form = id(new AphrontFormView())
 131        ->setUser($viewer)
 132        ->addHiddenInput('edit', true)
 133        ->appendChild(
 134          id(new AphrontFormTextControl())
 135            ->setLabel(pht('Name'))
 136            ->setName('name')
 137            ->setValue($v_name)
 138            ->setError($e_name))
 139        ->appendChild(
 140          id(new AphrontFormPolicyControl())
 141            ->setName('viewPolicy')
 142            ->setPolicyObject($dashboard)
 143            ->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
 144            ->setPolicies($policies))
 145        ->appendChild(
 146          id(new AphrontFormPolicyControl())
 147            ->setName('editPolicy')
 148            ->setPolicyObject($dashboard)
 149            ->setCapability(PhabricatorPolicyCapability::CAN_EDIT)
 150            ->setPolicies($policies))
 151        ->appendChild(
 152          id(new AphrontFormSelectControl())
 153            ->setLabel(pht('Layout Mode'))
 154            ->setName('layout_mode')
 155            ->setValue($v_layout_mode)
 156            ->setOptions($layout_mode_options))
 157        ->appendChild(
 158          id(new AphrontFormSubmitControl())
 159            ->setValue($button)
 160            ->addCancelButton($cancel_uri));
 161  
 162      $box = id(new PHUIObjectBoxView())
 163        ->setHeaderText($header)
 164        ->setForm($form)
 165        ->setValidationException($validation_exception);
 166  
 167      return $this->buildApplicationPage(
 168        array(
 169          $crumbs,
 170          $box,
 171        ),
 172        array(
 173          'title' => $title,
 174        ));
 175    }
 176  
 177    private function processTemplateRequest(AphrontRequest $request) {
 178      $viewer = $request->getUser();
 179  
 180      $template_control = id(new AphrontFormRadioButtonControl())
 181        ->setName(pht('template'))
 182        ->setValue($request->getStr('template', 'empty'))
 183        ->addButton(
 184          'empty',
 185          pht('Empty'),
 186          pht('Start with a blank canvas.'))
 187        ->addButton(
 188          'simple',
 189          pht('Simple Template'),
 190          pht(
 191            'Start with a simple dashboard with a welcome message, a feed of '.
 192            'recent events, and a few starter panels.'));
 193  
 194      $form = id(new AphrontFormView())
 195        ->setUser($viewer)
 196        ->appendRemarkupInstructions(
 197          pht('Choose a dashboard template to start with.'))
 198        ->appendChild($template_control);
 199  
 200      return $this->newDialog()
 201        ->setTitle(pht('Create Dashboard'))
 202        ->setWidth(AphrontDialogView::WIDTH_FORM)
 203        ->appendChild($form->buildLayoutView())
 204        ->addCancelButton('/dashboard/')
 205        ->addSubmitButton(pht('Continue'));
 206    }
 207  
 208    private function processBuildTemplateRequest(AphrontRequest $request) {
 209      $viewer = $request->getUser();
 210      $template = $request->getStr('template');
 211  
 212      $bare_panel = PhabricatorDashboardPanel::initializeNewPanel($viewer);
 213      $panel_phids = array();
 214  
 215      switch ($template) {
 216        case 'simple':
 217          $v_name = pht('New Simple Dashboard');
 218  
 219          $welcome_panel = $this->newPanel(
 220            $request,
 221            $viewer,
 222            'text',
 223            pht('Welcome'),
 224            array(
 225              'text' => pht(
 226                "This is a simple template dashboard. You can edit this panel ".
 227                "to change this text and replace it with a welcome message, or ".
 228                "leave this placeholder text as-is to give your dashboard a ".
 229                "rustic, authentic feel.".
 230                "\n\n".
 231                "You can drag, remove, add, and edit panels to customize the ".
 232                "rest of this dashboard to show the information you want.".
 233                "\n\n".
 234                "To install this dashboard on the home page, use the ".
 235                "**Install Dashboard** action link above."),
 236            ));
 237          $panel_phids[] = $welcome_panel->getPHID();
 238  
 239          $feed_panel = $this->newPanel(
 240            $request,
 241            $viewer,
 242            'query',
 243            pht('Recent Activity'),
 244            array(
 245              'class' => 'PhabricatorFeedSearchEngine',
 246              'key' => 'all',
 247            ));
 248          $panel_phids[] = $feed_panel->getPHID();
 249  
 250          $task_panel = $this->newPanel(
 251            $request,
 252            $viewer,
 253            'query',
 254            pht('Recent Tasks'),
 255            array(
 256              'class' => 'ManiphestTaskSearchEngine',
 257              'key' => 'all',
 258            ));
 259          $panel_phids[] = $task_panel->getPHID();
 260  
 261          $commit_panel = $this->newPanel(
 262            $request,
 263            $viewer,
 264            'query',
 265            pht('Recent Commits'),
 266            array(
 267              'class' => 'PhabricatorCommitSearchEngine',
 268              'key' => 'all',
 269            ));
 270          $panel_phids[] = $commit_panel->getPHID();
 271  
 272          $mode_2_and_1 = PhabricatorDashboardLayoutConfig::MODE_THIRDS_AND_THIRD;
 273          $layout = id(new PhabricatorDashboardLayoutConfig())
 274            ->setLayoutMode($mode_2_and_1)
 275            ->setPanelLocation(0, $welcome_panel->getPHID())
 276            ->setPanelLocation(0, $task_panel->getPHID())
 277            ->setPanelLocation(0, $commit_panel->getPHID())
 278            ->setPanelLocation(1, $feed_panel->getPHID());
 279  
 280          break;
 281        default:
 282          throw new Exception(pht('Unknown dashboard template %s!', $template));
 283      }
 284  
 285      // Create the dashboard.
 286  
 287      $dashboard = PhabricatorDashboard::initializeNewDashboard($viewer)
 288        ->setLayoutConfigFromObject($layout);
 289  
 290      $xactions = array();
 291  
 292      $xactions[] = id(new PhabricatorDashboardTransaction())
 293        ->setTransactionType(PhabricatorDashboardTransaction::TYPE_NAME)
 294        ->setNewValue($v_name);
 295  
 296      $xactions[] = id(new PhabricatorDashboardTransaction())
 297        ->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
 298        ->setMetadataValue(
 299          'edge:type',
 300          PhabricatorEdgeConfig::TYPE_DASHBOARD_HAS_PANEL)
 301        ->setNewValue(
 302          array(
 303            '+' => array_fuse($panel_phids),
 304          ));
 305  
 306      $editor = id(new PhabricatorDashboardTransactionEditor())
 307        ->setActor($viewer)
 308        ->setContinueOnNoEffect(true)
 309        ->setContentSourceFromRequest($request)
 310        ->applyTransactions($dashboard, $xactions);
 311  
 312      $manage_uri = $this->getApplicationURI('manage/'.$dashboard->getID().'/');
 313  
 314      return id(new AphrontRedirectResponse())
 315        ->setURI($manage_uri);
 316    }
 317  
 318    private function newPanel(
 319      AphrontRequest $request,
 320      PhabricatorUser $viewer,
 321      $type,
 322      $name,
 323      array $properties) {
 324  
 325      $panel = PhabricatorDashboardPanel::initializeNewPanel($viewer)
 326        ->setPanelType($type)
 327        ->setProperties($properties);
 328  
 329      $xactions = array();
 330  
 331      $xactions[] = id(new PhabricatorDashboardPanelTransaction())
 332        ->setTransactionType(PhabricatorDashboardPanelTransaction::TYPE_NAME)
 333        ->setNewValue($name);
 334  
 335      $editor = id(new PhabricatorDashboardPanelTransactionEditor())
 336        ->setActor($viewer)
 337        ->setContinueOnNoEffect(true)
 338        ->setContentSourceFromRequest($request)
 339        ->applyTransactions($panel, $xactions);
 340  
 341      return $panel;
 342    }
 343  
 344  
 345  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1