private function getRoutingRuleMap()
 {
     if ($this->routingMap === null) {
         $map = array();
         $routing = $this->getParam('routing', array());
         foreach ($routing as $route) {
             $phids = $route['phids'];
             if ($phids === null) {
                 $phids = array('default');
             }
             foreach ($phids as $phid) {
                 $new_rule = $route['routingRule'];
                 $current_rule = idx($map, $phid);
                 if ($current_rule === null) {
                     $is_stronger = true;
                 } else {
                     $is_stronger = PhabricatorMailRoutingRule::isStrongerThan($new_rule, $current_rule);
                 }
                 if ($is_stronger) {
                     $map[$phid] = array('rule' => $new_rule, 'reason' => $route['reasonPHID']);
                 }
             }
         }
         $this->routingMap = $map;
     }
     return $this->routingMap;
 }
 private function buildDeliveryProperties(PhabricatorMetaMTAMail $mail)
 {
     $viewer = $this->getViewer();
     $properties = id(new PHUIPropertyListView())->setUser($viewer);
     $actors = $mail->getDeliveredActors();
     $reasons = null;
     if (!$actors) {
         if ($mail->getStatus() == PhabricatorMailOutboundStatus::STATUS_QUEUE) {
             $delivery = $this->renderEmptyMessage(pht('This message has not been delivered yet, so delivery information ' . 'is not available.'));
         } else {
             $delivery = $this->renderEmptyMessage(pht('This is an older message that predates recording delivery ' . 'information, so none is available.'));
         }
     } else {
         $actor = idx($actors, $viewer->getPHID());
         if (!$actor) {
             $delivery = phutil_tag('em', array(), pht('This message was not delivered to you.'));
         } else {
             $deliverable = $actor['deliverable'];
             if ($deliverable) {
                 $delivery = pht('Delivered');
             } else {
                 $delivery = pht('Voided');
             }
             $reasons = id(new PHUIStatusListView());
             $reason_codes = $actor['reasons'];
             if (!$reason_codes) {
                 $reason_codes = array(PhabricatorMetaMTAActor::REASON_NONE);
             }
             $icon_yes = 'fa-check green';
             $icon_no = 'fa-times red';
             foreach ($reason_codes as $reason) {
                 $target = phutil_tag('strong', array(), PhabricatorMetaMTAActor::getReasonName($reason));
                 if (PhabricatorMetaMTAActor::isDeliveryReason($reason)) {
                     $icon = $icon_yes;
                 } else {
                     $icon = $icon_no;
                 }
                 $item = id(new PHUIStatusItemView())->setIcon($icon)->setTarget($target)->setNote(PhabricatorMetaMTAActor::getReasonDescription($reason));
                 $reasons->addItem($item);
             }
         }
     }
     $properties->addProperty(pht('Delivery'), $delivery);
     if ($reasons) {
         $properties->addProperty(pht('Reasons'), $reasons);
         $properties->addProperty(null, $this->renderEmptyMessage(pht('Delivery reasons are listed from weakest to strongest.')));
     }
     $properties->addSectionHeader(pht('Routing Rules'), 'fa-paper-plane-o');
     $map = $mail->getDeliveredRoutingMap();
     $routing_detail = null;
     if ($map === null) {
         if ($mail->getStatus() == PhabricatorMailOutboundStatus::STATUS_QUEUE) {
             $routing_result = $this->renderEmptyMessage(pht('This message has not been sent yet, so routing rules have ' . 'not been computed.'));
         } else {
             $routing_result = $this->renderEmptyMessage(pht('This is an older message which predates routing rules.'));
         }
     } else {
         $rule = idx($map, $viewer->getPHID());
         if ($rule === null) {
             $rule = idx($map, 'default');
         }
         if ($rule === null) {
             $routing_result = $this->renderEmptyMessage(pht('No routing rules applied when delivering this message to you.'));
         } else {
             $rule_const = $rule['rule'];
             $reason_phid = $rule['reason'];
             switch ($rule_const) {
                 case PhabricatorMailRoutingRule::ROUTE_AS_NOTIFICATION:
                     $routing_result = pht('This message was routed as a notification because it ' . 'matched %s.', $viewer->renderHandle($reason_phid)->render());
                     break;
                 case PhabricatorMailRoutingRule::ROUTE_AS_MAIL:
                     $routing_result = pht('This message was routed as an email because it matched %s.', $viewer->renderHandle($reason_phid)->render());
                     break;
                 default:
                     $routing_result = pht('Unknown routing rule "%s".', $rule_const);
                     break;
             }
         }
         $routing_rules = $mail->getDeliveredRoutingRules();
         if ($routing_rules) {
             $rules = array();
             foreach ($routing_rules as $rule) {
                 $phids = idx($rule, 'phids');
                 if ($phids === null) {
                     $rules[] = $rule;
                 } else {
                     if (in_array($viewer->getPHID(), $phids)) {
                         $rules[] = $rule;
                     }
                 }
             }
             // Reorder rules by strength.
             foreach ($rules as $key => $rule) {
                 $const = $rule['routingRule'];
                 $phids = $rule['phids'];
                 if ($phids === null) {
                     $type = 'A';
                 } else {
                     $type = 'B';
                 }
                 $rules[$key]['strength'] = sprintf('~%s%08d', $type, PhabricatorMailRoutingRule::getRuleStrength($const));
             }
             $rules = isort($rules, 'strength');
             $routing_detail = id(new PHUIStatusListView());
             foreach ($rules as $rule) {
                 $const = $rule['routingRule'];
                 $phids = $rule['phids'];
                 $name = PhabricatorMailRoutingRule::getRuleName($const);
                 $icon = PhabricatorMailRoutingRule::getRuleIcon($const);
                 $color = PhabricatorMailRoutingRule::getRuleColor($const);
                 if ($phids === null) {
                     $kind = pht('Global');
                 } else {
                     $kind = pht('Personal');
                 }
                 $target = array($kind, ': ', $name);
                 $target = phutil_tag('strong', array(), $target);
                 $item = id(new PHUIStatusItemView())->setTarget($target)->setNote($viewer->renderHandle($rule['reasonPHID']))->setIcon($icon, $color);
                 $routing_detail->addItem($item);
             }
         }
     }
     $properties->addProperty(pht('Effective Rule'), $routing_result);
     if ($routing_detail !== null) {
         $properties->addProperty(pht('All Matching Rules'), $routing_detail);
         $properties->addProperty(null, $this->renderEmptyMessage(pht('Matching rules are listed from weakest to strongest.')));
     }
     return $properties;
 }