<?php

/*
 * Copyright 2011 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

class ManiphestTaskCreateController extends ManiphestController {

  public function processRequest() {

    $request = $this->getRequest();
    $user = $request->getUser();

    $task = new ManiphestTask();

    $task->setPriority(ManiphestTaskPriority::PRIORITY_TRIAGE);

    $errors = array();
    $e_title = true;

    if ($request->isFormPost()) {
      $task->setTitle($request->getStr('title'));
      $task->setAuthorPHID($user->getPHID());
      $owner_tokenizer = $request->getArr('assigned_to');
      $task->setOwnerPHID(reset($owner_tokenizer));
      $task->setCCPHIDs($request->getArr('cc'));
      $task->setPriority($request->getInt('priority'));
      $task->setDescription($request->getStr('description'));

      if (!strlen($task->getTitle())) {
        $e_title = 'Required';
        $errors[] = 'Title is required.';
      }

      if (!$errors) {
        $transaction = new ManiphestTransaction();
        $transaction->setAuthorPHID($user->getPHID());
        $transaction->setTransactionType(ManiphestTransactionType::TYPE_STATUS);
        $transaction->setNewValue(ManiphestTaskStatus::STATUS_OPEN);

        $editor = new ManiphestTransactionEditor();
        $editor->applyTransaction($task, $transaction);

        return id(new AphrontRedirectResponse())
          ->setURI('/T'.$task->getID());
      }
    }

    $phids = array_merge(
      array($task->getOwnerPHID()),
      nonempty($task->getCCPHIDs(), array()));
    $phids = array_filter($phids);
    $phids = array_unique($phids);

    $handles = id(new PhabricatorObjectHandleData($phids))
      ->loadHandles($phids);

    $tvalues = mpull($handles, 'getFullName', 'getPHID');

    $error_view = null;
    if ($errors) {
      $error_view = new AphrontErrorView();
      $error_view->setErrors($errors);
      $error_view->setTitle('Form Errors');
    }

    $priority_map = ManiphestTaskPriority::getTaskPriorityMap();

    if ($task->getOwnerPHID()) {
      $assigned_value = array(
        $task->getOwnerPHID() => $handles[$task->getOwnerPHID()]->getFullName(),
      );
    } else {
      $assigned_value = array();
    }

    if ($task->getCCPHIDs()) {
      $cc_value = array_select_keys($tvalues, $task->getCCPHIDs());
    } else {
      $cc_value = array();
    }

    $form = new AphrontFormView();
    $form
      ->setUser($user)
      ->appendChild(
        id(new AphrontFormTextAreaControl())
          ->setLabel('Title')
          ->setName('title')
          ->setError($e_title)
          ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_SHORT)
          ->setValue($task->getTitle()))
      ->appendChild(
        id(new AphrontFormTokenizerControl())
          ->setLabel('Assigned To')
          ->setName('assigned_to')
          ->setValue($assigned_value)
          ->setDatasource('/typeahead/common/users/')
          ->setLimit(1))
      ->appendChild(
        id(new AphrontFormTokenizerControl())
          ->setLabel('CC')
          ->setName('cc')
          ->setValue($cc_value)
          ->setDatasource('/typeahead/common/mailable/'))
      ->appendChild(
        id(new AphrontFormSelectControl())
          ->setLabel('Priority')
          ->setName('priority')
          ->setOptions($priority_map)
          ->setValue($task->getPriority()))
      ->appendChild(
        id(new AphrontFormTextAreaControl())
          ->setLabel('Description')
          ->setName('description')
          ->setValue($task->getDescription()))
      ->appendChild(
        id(new AphrontFormSubmitControl())
          ->setValue('Create Task'));

    $panel = new AphrontPanelView();
    $panel->setWidth(AphrontPanelView::WIDTH_FULL);
    $panel->setHeader('Create New Task');
    $panel->appendChild($form);

    return $this->buildStandardPageResponse(
      array(
        $error_view,
        $panel,
      ),
      array(
        'title' => 'Create Task',
      ));
  }
}
