private function buildPropertySectionView(HeraldRule $rule)
 {
     $viewer = $this->getRequest()->getUser();
     $view = id(new PHUIPropertyListView())->setUser($viewer);
     $view->addProperty(pht('Rule Type'), idx(HeraldRuleTypeConfig::getRuleTypeMap(), $rule->getRuleType()));
     if ($rule->isPersonalRule()) {
         $view->addProperty(pht('Author'), $viewer->renderHandle($rule->getAuthorPHID()));
     }
     $adapter = HeraldAdapter::getAdapterForContentType($rule->getContentType());
     if ($adapter) {
         $view->addProperty(pht('Applies To'), idx(HeraldAdapter::getEnabledAdapterMap($viewer), $rule->getContentType()));
         if ($rule->isObjectRule()) {
             $view->addProperty(pht('Trigger Object'), $viewer->renderHandle($rule->getTriggerObjectPHID()));
         }
     }
     return $view;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $content_type_map = HeraldAdapter::getEnabledAdapterMap($user);
     $rule_type_map = HeraldRuleTypeConfig::getRuleTypeMap();
     if ($this->id) {
         $id = $this->id;
         $rule = id(new HeraldRuleQuery())->setViewer($user)->withIDs(array($id))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
         if (!$rule) {
             return new Aphront404Response();
         }
         $cancel_uri = $this->getApplicationURI("rule/{$id}/");
     } else {
         $rule = new HeraldRule();
         $rule->setAuthorPHID($user->getPHID());
         $rule->setMustMatchAll(1);
         $content_type = $request->getStr('content_type');
         $rule->setContentType($content_type);
         $rule_type = $request->getStr('rule_type');
         if (!isset($rule_type_map[$rule_type])) {
             $rule_type = HeraldRuleTypeConfig::RULE_TYPE_PERSONAL;
         }
         $rule->setRuleType($rule_type);
         $adapter = HeraldAdapter::getAdapterForContentType($rule->getContentType());
         if (!$adapter->supportsRuleType($rule->getRuleType())) {
             throw new Exception(pht("This rule's content type does not support the selected rule " . "type."));
         }
         if ($rule->isObjectRule()) {
             $rule->setTriggerObjectPHID($request->getStr('targetPHID'));
             $object = id(new PhabricatorObjectQuery())->setViewer($user)->withPHIDs(array($rule->getTriggerObjectPHID()))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
             if (!$object) {
                 throw new Exception(pht('No valid object provided for object rule!'));
             }
             if (!$adapter->canTriggerOnObject($object)) {
                 throw new Exception(pht('Object is of wrong type for adapter!'));
             }
         }
         $cancel_uri = $this->getApplicationURI();
     }
     if ($rule->isGlobalRule()) {
         $this->requireApplicationCapability(HeraldManageGlobalRulesCapability::CAPABILITY);
     }
     $adapter = HeraldAdapter::getAdapterForContentType($rule->getContentType());
     $local_version = id(new HeraldRule())->getConfigVersion();
     if ($rule->getConfigVersion() > $local_version) {
         throw new Exception(pht('This rule was created with a newer version of Herald. You can not ' . 'view or edit it in this older version. Upgrade your Phabricator ' . 'deployment.'));
     }
     // Upgrade rule version to our version, since we might add newly-defined
     // conditions, etc.
     $rule->setConfigVersion($local_version);
     $rule_conditions = $rule->loadConditions();
     $rule_actions = $rule->loadActions();
     $rule->attachConditions($rule_conditions);
     $rule->attachActions($rule_actions);
     $e_name = true;
     $errors = array();
     if ($request->isFormPost() && $request->getStr('save')) {
         list($e_name, $errors) = $this->saveRule($adapter, $rule, $request);
         if (!$errors) {
             $id = $rule->getID();
             $uri = $this->getApplicationURI("rule/{$id}/");
             return id(new AphrontRedirectResponse())->setURI($uri);
         }
     }
     $must_match_selector = $this->renderMustMatchSelector($rule);
     $repetition_selector = $this->renderRepetitionSelector($rule, $adapter);
     $handles = $this->loadHandlesForRule($rule);
     require_celerity_resource('herald-css');
     $content_type_name = $content_type_map[$rule->getContentType()];
     $rule_type_name = $rule_type_map[$rule->getRuleType()];
     $form = id(new AphrontFormView())->setUser($user)->setID('herald-rule-edit-form')->addHiddenInput('content_type', $rule->getContentType())->addHiddenInput('rule_type', $rule->getRuleType())->addHiddenInput('save', 1)->appendChild(javelin_tag('input', array('type' => 'hidden', 'name' => 'rule', 'sigil' => 'rule')))->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Rule Name'))->setName('name')->setError($e_name)->setValue($rule->getName()));
     $trigger_object_control = false;
     if ($rule->isObjectRule()) {
         $trigger_object_control = id(new AphrontFormStaticControl())->setValue(pht('This rule triggers for %s.', $handles[$rule->getTriggerObjectPHID()]->renderLink()));
     }
     $form->appendChild(id(new AphrontFormMarkupControl())->setValue(pht('This %s rule triggers for %s.', phutil_tag('strong', array(), $rule_type_name), phutil_tag('strong', array(), $content_type_name))))->appendChild($trigger_object_control)->appendChild(id(new PHUIFormInsetView())->setTitle(pht('Conditions'))->setRightButton(javelin_tag('a', array('href' => '#', 'class' => 'button green', 'sigil' => 'create-condition', 'mustcapture' => true), pht('New Condition')))->setDescription(pht('When %s these conditions are met:', $must_match_selector))->setContent(javelin_tag('table', array('sigil' => 'rule-conditions', 'class' => 'herald-condition-table'), '')))->appendChild(id(new PHUIFormInsetView())->setTitle(pht('Action'))->setRightButton(javelin_tag('a', array('href' => '#', 'class' => 'button green', 'sigil' => 'create-action', 'mustcapture' => true), pht('New Action')))->setDescription(pht('Take these actions %s this rule matches:', $repetition_selector))->setContent(javelin_tag('table', array('sigil' => 'rule-actions', 'class' => 'herald-action-table'), '')))->appendChild(id(new AphrontFormSubmitControl())->setValue(pht('Save Rule'))->addCancelButton($cancel_uri));
     $this->setupEditorBehavior($rule, $handles, $adapter);
     $title = $rule->getID() ? pht('Edit Herald Rule') : pht('Create Herald Rule');
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($title)->setFormErrors($errors)->setForm($form);
     $crumbs = $this->buildApplicationCrumbs()->addTextCrumb($title);
     return $this->buildApplicationPage(array($crumbs, $form_box), array('title' => pht('Edit Rule')));
 }
 public function willSaveAction(HeraldRule $rule, HeraldAction $action)
 {
     $target = $action->getTarget();
     if (is_array($target)) {
         $target = array_keys($target);
     }
     $author_phid = $rule->getAuthorPHID();
     $rule_type = $rule->getRuleType();
     if ($rule_type == HeraldRuleTypeConfig::RULE_TYPE_PERSONAL) {
         switch ($action->getAction()) {
             case self::ACTION_EMAIL:
             case self::ACTION_ADD_CC:
             case self::ACTION_REMOVE_CC:
             case self::ACTION_AUDIT:
             case self::ACTION_ASSIGN_TASK:
             case self::ACTION_ADD_REVIEWERS:
             case self::ACTION_ADD_BLOCKING_REVIEWERS:
                 // For personal rules, force these actions to target the rule owner.
                 $target = array($author_phid);
                 break;
             case self::ACTION_FLAG:
                 // Make sure flag color is valid; set to blue if not.
                 $color_map = PhabricatorFlagColor::getColorNameMap();
                 if (empty($color_map[$target])) {
                     $target = PhabricatorFlagColor::COLOR_BLUE;
                 }
                 break;
             case self::ACTION_BLOCK:
             case self::ACTION_NOTHING:
                 break;
             default:
                 throw new HeraldInvalidActionException(pht('Unrecognized action type "%s"!', $action->getAction()));
         }
     }
     $action->setTarget($target);
 }
 /**
  * Load rules for the "Another Herald rule..." condition dropdown, which
  * allows one rule to depend upon the success or failure of another rule.
  */
 private function loadRulesThisRuleMayDependUpon(HeraldRule $rule)
 {
     // Any rule can depend on a global rule.
     $all_rules = id(new HeraldRuleQuery())->withRuleTypes(array(HeraldRuleTypeConfig::RULE_TYPE_GLOBAL))->withContentTypes(array($rule->getContentType()))->execute();
     if ($rule->getRuleType() == HeraldRuleTypeConfig::RULE_TYPE_PERSONAL) {
         // Personal rules may depend upon your other personal rules.
         $all_rules += id(new HeraldRuleQuery())->withRuleTypes(array(HeraldRuleTypeConfig::RULE_TYPE_PERSONAL))->withContentTypes(array($rule->getContentType()))->withAuthorPHIDs(array($rule->getAuthorPHID()))->execute();
     }
     // A rule can not depend upon itself.
     unset($all_rules[$rule->getID()]);
     return $all_rules;
 }