<commit_before><?php

final class PhabricatorCalendarEventEditController
  extends PhabricatorCalendarController {

  private $id;

  public function willProcessRequest(array $data) {
    $this->id = idx($data, 'id');
  }

  public function isCreate() {
    return !$this->id;
  }

  public function processRequest() {
    $request = $this->getRequest();
    $user = $request->getUser();
    $user_phid = $user->getPHID();
    $error_name = true;
    $validation_exception = null;

    $start_time = id(new AphrontFormDateControl())
      ->setUser($user)
      ->setName('start')
      ->setLabel(pht('Start'))
      ->setInitialTime(AphrontFormDateControl::TIME_START_OF_DAY);

    $end_time = id(new AphrontFormDateControl())
      ->setUser($user)
      ->setName('end')
      ->setLabel(pht('End'))
      ->setInitialTime(AphrontFormDateControl::TIME_END_OF_DAY);

    if ($this->isCreate()) {
      $event = PhabricatorCalendarEvent::initializeNewCalendarEvent($user);
      $end_value = $end_time->readValueFromRequest($request);
      $start_value = $start_time->readValueFromRequest($request);
      $submit_label = pht('Create');
      $page_title = pht('Create Event');
      $redirect = 'created';
      $subscribers = array();
      $invitees = array($user_phid);
      $cancel_uri = $this->getApplicationURI();
    } else {
      $event = id(new PhabricatorCalendarEventQuery())
        ->setViewer($user)
        ->withIDs(array($this->id))
        ->requireCapabilities(
          array(
            PhabricatorPolicyCapability::CAN_VIEW,
            PhabricatorPolicyCapability::CAN_EDIT,
          ))
        ->executeOne();
      if (!$event) {
        return new Aphront404Response();
      }

      $end_time->setValue($event->getDateTo());
      $start_time->setValue($event->getDateFrom());
      $submit_label = pht('Update');
      $page_title   = pht('Update Event');

      $subscribers = PhabricatorSubscribersQuery::loadSubscribersForPHID(
        $event->getPHID());

      $invitees = array();
      foreach ($event->getInvitees() as $invitee) {
        if ($invitee->isUninvited()) {
          continue;
        } else {
          $invitees[] = $invitee->getInviteePHID();
        }
      }

      $cancel_uri = '/'.$event->getMonogram();
    }

    $errors = array();
    if ($request->isFormPost()) {
      $xactions = array();
      $name = $request->getStr('name');
      $type = $request->getInt('status');
      $start_value = $start_time->readValueFromRequest($request);
      $end_value = $end_time->readValueFromRequest($request);
      $description = $request->getStr('description');
      $subscribers = $request->getArr('subscribers');

      $invitees = $request->getArr('invitees');
      $new_invitees = $this->getNewInviteeList($invitees, $event);
      $status_attending = PhabricatorCalendarEventInvitee::STATUS_ATTENDING;
      if ($this->isCreate()) {
        $status = idx($new_invitees, $user->getPHID());
        if ($status) {
          $new_invitees[$user->getPHID()] = $status_attending;
        }
      }

      if ($start_time->getError()) {
        $errors[] = pht('Invalid start time; reset to default.');
      }
      if ($end_time->getError()) {
        $errors[] = pht('Invalid end time; reset to default.');
      }
      if (!$errors) {
        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorCalendarEventTransaction::TYPE_NAME)
          ->setNewValue($name);

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorCalendarEventTransaction::TYPE_START_DATE)
          ->setNewValue($start_value);

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorCalendarEventTransaction::TYPE_END_DATE)
          ->setNewValue($end_value);

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorCalendarEventTransaction::TYPE_STATUS)
          ->setNewValue($type);

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorTransactions::TYPE_SUBSCRIBERS)
          ->setNewValue(array('=' => array_fuse($subscribers)));

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorCalendarEventTransaction::TYPE_INVITE)
          ->setNewValue($new_invitees);

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorCalendarEventTransaction::TYPE_DESCRIPTION)
          ->setNewValue($description);

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
          ->setNewValue($request->getStr('viewPolicy'));

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)
          ->setNewValue($request->getStr('editPolicy'));

        $editor = id(new PhabricatorCalendarEventEditor())
          ->setActor($user)
          ->setContentSourceFromRequest($request)
          ->setContinueOnNoEffect(true);

        try {
          $xactions = $editor->applyTransactions($event, $xactions);
          $response = id(new AphrontRedirectResponse());
          return $response->setURI('/E'.$event->getID());
        } catch (PhabricatorApplicationTransactionValidationException $ex) {
          $validation_exception = $ex;
          $error_name = $ex
            ->getShortMessage(PhabricatorCalendarEventTransaction::TYPE_NAME);
        }
      }
    }

    $error_view = null;
    if ($errors) {
      $error_view = id(new PHUIInfoView())
        ->setTitle(pht('Status can not be set!'))
        ->setErrors($errors);
    }

    $name = id(new AphrontFormTextControl())
      ->setLabel(pht('Name'))
      ->setName('name')
      ->setValue($event->getName())
      ->setError($error_name);

    $status_select = id(new AphrontFormSelectControl())
      ->setLabel(pht('Status'))
      ->setName('status')
      ->setValue($event->getStatus())
      ->setOptions($event->getStatusOptions());

    $description = id(new AphrontFormTextAreaControl())
      ->setLabel(pht('Description'))
      ->setName('description')
      ->setValue($event->getDescription());

    $current_policies = id(new PhabricatorPolicyQuery())
      ->setViewer($user)
      ->setObject($event)
      ->execute();
    $view_policies = id(new AphrontFormPolicyControl())
      ->setUser($user)
      ->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
      ->setPolicyObject($event)
      ->setPolicies($current_policies)
      ->setName('viewPolicy');
    $edit_policies = id(new AphrontFormPolicyControl())
      ->setUser($user)
      ->setCapability(PhabricatorPolicyCapability::CAN_EDIT)<commit_msg>Calendar event update form submission should not lose date (except selected dates) when form has error.<commit_after><?php

final class PhabricatorCalendarEventEditController
  extends PhabricatorCalendarController {

  private $id;

  public function willProcessRequest(array $data) {
    $this->id = idx($data, 'id');
  }

  public function isCreate() {
    return !$this->id;
  }

  public function processRequest() {
    $request = $this->getRequest();
    $user = $request->getUser();
    $user_phid = $user->getPHID();
    $error_name = true;
    $validation_exception = null;

    $start_time = id(new AphrontFormDateControl())
      ->setUser($user)
      ->setName('start')
      ->setLabel(pht('Start'))
      ->setInitialTime(AphrontFormDateControl::TIME_START_OF_DAY);

    $end_time = id(new AphrontFormDateControl())
      ->setUser($user)
      ->setName('end')
      ->setLabel(pht('End'))
      ->setInitialTime(AphrontFormDateControl::TIME_END_OF_DAY);

    if ($this->isCreate()) {
      $event = PhabricatorCalendarEvent::initializeNewCalendarEvent($user);
      $end_value = $end_time->readValueFromRequest($request);
      $start_value = $start_time->readValueFromRequest($request);
      $submit_label = pht('Create');
      $page_title = pht('Create Event');
      $redirect = 'created';
      $subscribers = array();
      $invitees = array($user_phid);
      $cancel_uri = $this->getApplicationURI();
    } else {
      $event = id(new PhabricatorCalendarEventQuery())
        ->setViewer($user)
        ->withIDs(array($this->id))
        ->requireCapabilities(
          array(
            PhabricatorPolicyCapability::CAN_VIEW,
            PhabricatorPolicyCapability::CAN_EDIT,
          ))
        ->executeOne();
      if (!$event) {
        return new Aphront404Response();
      }

      $end_time->setValue($event->getDateTo());
      $start_time->setValue($event->getDateFrom());
      $submit_label = pht('Update');
      $page_title   = pht('Update Event');

      $subscribers = PhabricatorSubscribersQuery::loadSubscribersForPHID(
        $event->getPHID());

      $invitees = array();
      foreach ($event->getInvitees() as $invitee) {
        if ($invitee->isUninvited()) {
          continue;
        } else {
          $invitees[] = $invitee->getInviteePHID();
        }
      }

      $cancel_uri = '/'.$event->getMonogram();
    }

    $errors = array();
    $name = $event->getName();
    $description = $event->getDescription();
    $type = $event->getStatus();

    $current_policies = id(new PhabricatorPolicyQuery())
      ->setViewer($user)
      ->setObject($event)
      ->execute();

    if ($request->isFormPost()) {
      $xactions = array();
      $name = $request->getStr('name');
      $type = $request->getInt('status');
      $start_value = $start_time->readValueFromRequest($request);
      $end_value = $end_time->readValueFromRequest($request);
      $description = $request->getStr('description');
      $subscribers = $request->getArr('subscribers');
      $edit_policy = $request->getStr('editPolicy');
      $view_policy = $request->getStr('viewPolicy');

      $invitees = $request->getArr('invitees');
      $new_invitees = $this->getNewInviteeList($invitees, $event);
      $status_attending = PhabricatorCalendarEventInvitee::STATUS_ATTENDING;
      if ($this->isCreate()) {
        $status = idx($new_invitees, $user->getPHID());
        if ($status) {
          $new_invitees[$user->getPHID()] = $status_attending;
        }
      }

      if ($start_time->getError()) {
        $errors[] = pht('Invalid start time; reset to default.');
      }
      if ($end_time->getError()) {
        $errors[] = pht('Invalid end time; reset to default.');
      }
      if (!$errors) {
        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorCalendarEventTransaction::TYPE_NAME)
          ->setNewValue($name);

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorCalendarEventTransaction::TYPE_START_DATE)
          ->setNewValue($start_value);

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorCalendarEventTransaction::TYPE_END_DATE)
          ->setNewValue($end_value);

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorCalendarEventTransaction::TYPE_STATUS)
          ->setNewValue($type);

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorTransactions::TYPE_SUBSCRIBERS)
          ->setNewValue(array('=' => array_fuse($subscribers)));

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorCalendarEventTransaction::TYPE_INVITE)
          ->setNewValue($new_invitees);

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(
            PhabricatorCalendarEventTransaction::TYPE_DESCRIPTION)
          ->setNewValue($description);

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
          ->setNewValue($request->getStr('viewPolicy'));

        $xactions[] = id(new PhabricatorCalendarEventTransaction())
          ->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)
          ->setNewValue($request->getStr('editPolicy'));

        $editor = id(new PhabricatorCalendarEventEditor())
          ->setActor($user)
          ->setContentSourceFromRequest($request)
          ->setContinueOnNoEffect(true);

        try {
          $xactions = $editor->applyTransactions($event, $xactions);
          $response = id(new AphrontRedirectResponse());
          return $response->setURI('/E'.$event->getID());
        } catch (PhabricatorApplicationTransactionValidationException $ex) {
          $validation_exception = $ex;
          $error_name = $ex
            ->getShortMessage(PhabricatorCalendarEventTransaction::TYPE_NAME);

          $event->setViewPolicy($view_policy);
          $event->setEditPolicy($edit_policy);
        }
      } else {
        $event->setViewPolicy($view_policy);
        $event->setEditPolicy($edit_policy);
      }
    }

    $error_view = null;
    if ($errors) {
      $error_view = id(new PHUIInfoView())
        ->setTitle(pht('Status can not be set!'))
        ->setErrors($errors);
    }

    $name = id(new AphrontFormTextControl())
      ->setLabel(pht('Name'))
      ->setName('name')
      ->setValue($name)
      ->setError($error_name);

    $status_select = id(new AphrontFormSelectControl())
      ->setLabel(pht('Status'))
      ->setName('status')
      ->setValue($type)
      ->setOptions($event->getStatusOptions());

    $description = id(new AphrontFormTextAreaControl())
      ->setLabel(pht('Description'))
      ->setName('description')
      ->setValue($description);

    $view_policies = id(new AphrontFormPolicyControl())
      ->setUser($user)
      ->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
      ->setPolicyObject($event)
      ->setPolicies($current_policies)
      ->setName('viewPolicy');
    $edit_policies = id(new AphrontFormPolicyControl())
      ->setUser($user)
      ->setCapability(PhabricatorPolicyCapability::CAN_EDIT)
{'fraction_non_alphanumeric': 0.14505296683514504, 'fraction_numerical': 0.0004154261580004154, 'mean_word_length': 3.084841628959276, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 209, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7324090', 'n_tokens_mistral': 4312, 'n_tokens_neox': 4261, 'n_words': 609}