[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/conpherence/controller/ -> ConpherenceUpdateController.php (source)

   1  <?php
   2  
   3  final class ConpherenceUpdateController
   4    extends ConpherenceController {
   5  
   6    private $conpherenceID;
   7  
   8    public function setConpherenceID($conpherence_id) {
   9      $this->conpherenceID = $conpherence_id;
  10      return $this;
  11    }
  12    public function getConpherenceID() {
  13      return $this->conpherenceID;
  14    }
  15    public function willProcessRequest(array $data) {
  16      $this->setConpherenceID(idx($data, 'id'));
  17    }
  18  
  19    public function processRequest() {
  20      $request = $this->getRequest();
  21      $user = $request->getUser();
  22      $conpherence_id = $this->getConpherenceID();
  23      if (!$conpherence_id) {
  24        return new Aphront404Response();
  25      }
  26  
  27      $conpherence = id(new ConpherenceThreadQuery())
  28        ->setViewer($user)
  29        ->withIDs(array($conpherence_id))
  30        ->needFilePHIDs(true)
  31        ->executeOne();
  32  
  33      $action = $request->getStr('action', ConpherenceUpdateActions::METADATA);
  34  
  35      $latest_transaction_id = null;
  36      $response_mode = $request->isAjax() ? 'ajax' : 'redirect';
  37      $error_view = null;
  38      $e_file = array();
  39      $errors = array();
  40      $delete_draft = false;
  41      $xactions = array();
  42      if ($request->isFormPost() || ($action == ConpherenceUpdateActions::LOAD)) {
  43        $editor = id(new ConpherenceEditor())
  44          ->setContinueOnNoEffect($request->isContinueRequest())
  45          ->setContentSourceFromRequest($request)
  46          ->setActor($user);
  47  
  48        switch ($action) {
  49          case ConpherenceUpdateActions::DRAFT:
  50            $draft = PhabricatorDraft::newFromUserAndKey(
  51              $user,
  52              $conpherence->getPHID());
  53            $draft->setDraft($request->getStr('text'));
  54            $draft->replaceOrDelete();
  55            return new AphrontAjaxResponse();
  56          case ConpherenceUpdateActions::MESSAGE:
  57            $message = $request->getStr('text');
  58            $xactions = $editor->generateTransactionsFromText(
  59              $user,
  60              $conpherence,
  61              $message);
  62            $delete_draft = true;
  63            break;
  64          case ConpherenceUpdateActions::ADD_PERSON:
  65            $person_phids = $request->getArr('add_person');
  66            if (!empty($person_phids)) {
  67              $xactions[] = id(new ConpherenceTransaction())
  68                ->setTransactionType(
  69                  ConpherenceTransactionType::TYPE_PARTICIPANTS)
  70                ->setNewValue(array('+' => $person_phids));
  71            }
  72            break;
  73          case ConpherenceUpdateActions::REMOVE_PERSON:
  74            if (!$request->isContinueRequest()) {
  75              // do nothing; we'll display a confirmation dialogue instead
  76              break;
  77            }
  78            $person_phid = $request->getStr('remove_person');
  79            if ($person_phid && $person_phid == $user->getPHID()) {
  80              $xactions[] = id(new ConpherenceTransaction())
  81                ->setTransactionType(
  82                  ConpherenceTransactionType::TYPE_PARTICIPANTS)
  83                ->setNewValue(array('-' => array($person_phid)));
  84              $response_mode = 'go-home';
  85            }
  86            break;
  87          case ConpherenceUpdateActions::NOTIFICATIONS:
  88            $notifications = $request->getStr('notifications');
  89            $participant = $conpherence->getParticipant($user->getPHID());
  90            $participant->setSettings(array('notifications' => $notifications));
  91            $participant->save();
  92            $result = pht(
  93              'Updated notification settings to "%s".',
  94              ConpherenceSettings::getHumanString($notifications));
  95            return id(new AphrontAjaxResponse())
  96              ->setContent($result);
  97            break;
  98          case ConpherenceUpdateActions::METADATA:
  99            $updated = false;
 100            // all metadata updates are continue requests
 101            if (!$request->isContinueRequest()) {
 102              break;
 103            }
 104  
 105            $title = $request->getStr('title');
 106            if ($title != $conpherence->getTitle()) {
 107              $xactions[] = id(new ConpherenceTransaction())
 108                ->setTransactionType(ConpherenceTransactionType::TYPE_TITLE)
 109                ->setNewValue($title);
 110              $updated = true;
 111              $response_mode = 'redirect';
 112            }
 113            if (!$updated) {
 114              $errors[] = pht(
 115                'That was a non-update. Try cancel.');
 116            }
 117            break;
 118          case ConpherenceUpdateActions::LOAD:
 119            $updated = false;
 120            $response_mode = 'ajax';
 121            break;
 122          default:
 123            throw new Exception('Unknown action: '.$action);
 124            break;
 125        }
 126  
 127        if ($xactions || ($action == ConpherenceUpdateActions::LOAD)) {
 128          if ($xactions) {
 129            try {
 130              $xactions = $editor->applyTransactions($conpherence, $xactions);
 131              if ($delete_draft) {
 132                $draft = PhabricatorDraft::newFromUserAndKey(
 133                  $user,
 134                  $conpherence->getPHID());
 135                $draft->delete();
 136              }
 137            } catch (PhabricatorApplicationTransactionNoEffectException $ex) {
 138              return id(new PhabricatorApplicationTransactionNoEffectResponse())
 139                ->setCancelURI($this->getApplicationURI($conpherence_id.'/'))
 140                ->setException($ex);
 141            }
 142          }
 143  
 144          switch ($response_mode) {
 145            case 'ajax':
 146              $latest_transaction_id = $request->getInt('latest_transaction_id');
 147              $content = $this->loadAndRenderUpdates(
 148                $action,
 149                $conpherence_id,
 150                $latest_transaction_id);
 151              return id(new AphrontAjaxResponse())
 152                ->setContent($content);
 153              break;
 154            case 'go-home':
 155              return id(new AphrontRedirectResponse())
 156                ->setURI($this->getApplicationURI());
 157              break;
 158            case 'redirect':
 159            default:
 160              return id(new AphrontRedirectResponse())
 161                ->setURI($this->getApplicationURI($conpherence->getID().'/'));
 162              break;
 163          }
 164        }
 165      }
 166  
 167      if ($errors) {
 168        $error_view = id(new AphrontErrorView())
 169          ->setErrors($errors);
 170      }
 171  
 172      switch ($action) {
 173        case ConpherenceUpdateActions::ADD_PERSON:
 174          $dialogue = $this->renderAddPersonDialogue($conpherence);
 175          break;
 176        case ConpherenceUpdateActions::REMOVE_PERSON:
 177          $dialogue = $this->renderRemovePersonDialogue($conpherence);
 178          break;
 179        case ConpherenceUpdateActions::METADATA:
 180        default:
 181          $dialogue = $this->renderMetadataDialogue($conpherence, $error_view);
 182          break;
 183      }
 184  
 185      return id(new AphrontDialogResponse())
 186        ->setDialog($dialogue
 187          ->setUser($user)
 188          ->setWidth(AphrontDialogView::WIDTH_FORM)
 189          ->setSubmitURI($this->getApplicationURI('update/'.$conpherence_id.'/'))
 190          ->addSubmitButton()
 191          ->addCancelButton($this->getApplicationURI($conpherence->getID().'/')));
 192  
 193    }
 194  
 195    private function renderAddPersonDialogue(
 196      ConpherenceThread $conpherence) {
 197  
 198      $request = $this->getRequest();
 199      $user = $request->getUser();
 200      $add_person = $request->getStr('add_person');
 201  
 202      $form = id(new PHUIFormLayoutView())
 203        ->setUser($user)
 204        ->setFullWidth(true)
 205        ->appendChild(
 206          id(new AphrontFormTokenizerControl())
 207          ->setName('add_person')
 208          ->setUser($user)
 209          ->setDatasource(new PhabricatorPeopleDatasource()));
 210  
 211      require_celerity_resource('conpherence-update-css');
 212      return id(new AphrontDialogView())
 213        ->setTitle(pht('Add Participants'))
 214        ->addHiddenInput('action', 'add_person')
 215        ->appendChild($form);
 216      }
 217  
 218    private function renderRemovePersonDialogue(
 219      ConpherenceThread $conpherence) {
 220  
 221      $request = $this->getRequest();
 222      $user = $request->getUser();
 223      $remove_person = $request->getStr('remove_person');
 224      $participants = $conpherence->getParticipants();
 225      $message = pht(
 226        'Are you sure you want to remove yourself from this conpherence? ');
 227      if (count($participants) == 1) {
 228        $message .= pht(
 229          'The conpherence will be inaccessible forever and ever.');
 230      } else {
 231        $message .= pht(
 232          'Someone else in the conpherence can add you back later.');
 233      }
 234      $body = phutil_tag(
 235        'p',
 236        array(
 237        ),
 238        $message);
 239  
 240      require_celerity_resource('conpherence-update-css');
 241      return id(new AphrontDialogView())
 242        ->setTitle(pht('Remove Participants'))
 243        ->addHiddenInput('action', 'remove_person')
 244        ->addHiddenInput('__continue__', true)
 245        ->addHiddenInput('remove_person', $remove_person)
 246        ->appendChild($body);
 247    }
 248  
 249    private function renderMetadataDialogue(
 250      ConpherenceThread $conpherence,
 251      $error_view) {
 252  
 253      $form = id(new PHUIFormLayoutView())
 254        ->appendChild($error_view)
 255        ->appendChild(
 256          id(new AphrontFormTextControl())
 257          ->setLabel(pht('Title'))
 258          ->setName('title')
 259          ->setValue($conpherence->getTitle()));
 260  
 261      require_celerity_resource('conpherence-update-css');
 262      return id(new AphrontDialogView())
 263        ->setTitle(pht('Update Conpherence'))
 264        ->addHiddenInput('action', 'metadata')
 265        ->addHiddenInput('__continue__', true)
 266        ->appendChild($form);
 267    }
 268  
 269    private function loadAndRenderUpdates(
 270      $action,
 271      $conpherence_id,
 272      $latest_transaction_id) {
 273  
 274      $need_widget_data = false;
 275      $need_transactions = false;
 276      switch ($action) {
 277        case ConpherenceUpdateActions::METADATA:
 278        case ConpherenceUpdateActions::LOAD:
 279          $need_transactions = true;
 280          break;
 281        case ConpherenceUpdateActions::MESSAGE:
 282        case ConpherenceUpdateActions::ADD_PERSON:
 283          $need_transactions = true;
 284          $need_widget_data = true;
 285          break;
 286        case ConpherenceUpdateActions::REMOVE_PERSON:
 287        case ConpherenceUpdateActions::NOTIFICATIONS:
 288        default:
 289          break;
 290  
 291      }
 292      $user = $this->getRequest()->getUser();
 293      $conpherence = id(new ConpherenceThreadQuery())
 294        ->setViewer($user)
 295        ->setAfterTransactionID($latest_transaction_id)
 296        ->needWidgetData($need_widget_data)
 297        ->needTransactions($need_transactions)
 298        ->withIDs(array($conpherence_id))
 299        ->executeOne();
 300  
 301      if ($need_transactions) {
 302        $data = $this->renderConpherenceTransactions($conpherence);
 303      } else {
 304        $data = array();
 305      }
 306      $rendered_transactions = idx($data, 'transactions');
 307      $new_latest_transaction_id = idx($data, 'latest_transaction_id');
 308  
 309      $widget_uri = $this->getApplicationURI('update/'.$conpherence->getID().'/');
 310      $nav_item = null;
 311      $header = null;
 312      $people_widget = null;
 313      $file_widget = null;
 314      switch ($action) {
 315        case ConpherenceUpdateActions::METADATA:
 316          $header = $this->buildHeaderPaneContent($conpherence);
 317          $nav_item = id(new ConpherenceThreadListView())
 318            ->setUser($user)
 319            ->setBaseURI($this->getApplicationURI())
 320            ->renderSingleThread($conpherence);
 321          break;
 322        case ConpherenceUpdateActions::MESSAGE:
 323          $file_widget = id(new ConpherenceFileWidgetView())
 324            ->setUser($this->getRequest()->getUser())
 325            ->setConpherence($conpherence)
 326            ->setUpdateURI($widget_uri);
 327          break;
 328        case ConpherenceUpdateActions::ADD_PERSON:
 329          $people_widget = id(new ConpherencePeopleWidgetView())
 330            ->setUser($user)
 331            ->setConpherence($conpherence)
 332            ->setUpdateURI($widget_uri);
 333          break;
 334        case ConpherenceUpdateActions::REMOVE_PERSON:
 335        case ConpherenceUpdateActions::NOTIFICATIONS:
 336        default:
 337          break;
 338      }
 339  
 340      $people_html = null;
 341      if ($people_widget) {
 342        $people_html = hsprintf('%s', $people_widget->render());
 343      }
 344      $content = array(
 345        'transactions' => hsprintf('%s', $rendered_transactions),
 346        'latest_transaction_id' => $new_latest_transaction_id,
 347        'nav_item' => hsprintf('%s', $nav_item),
 348        'conpherence_phid' => $conpherence->getPHID(),
 349        'header' => hsprintf('%s', $header),
 350        'file_widget' => $file_widget ? $file_widget->render() : null,
 351        'people_widget' => $people_html,
 352      );
 353  
 354      return $content;
 355    }
 356  
 357  }


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