[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class AphrontFormPolicyControl extends AphrontFormControl { 4 5 private $object; 6 private $capability; 7 private $policies; 8 9 public function setPolicyObject(PhabricatorPolicyInterface $object) { 10 $this->object = $object; 11 return $this; 12 } 13 14 public function setPolicies(array $policies) { 15 assert_instances_of($policies, 'PhabricatorPolicy'); 16 $this->policies = $policies; 17 return $this; 18 } 19 20 public function setCapability($capability) { 21 $this->capability = $capability; 22 23 $labels = array( 24 PhabricatorPolicyCapability::CAN_VIEW => pht('Visible To'), 25 PhabricatorPolicyCapability::CAN_EDIT => pht('Editable By'), 26 PhabricatorPolicyCapability::CAN_JOIN => pht('Joinable By'), 27 ); 28 29 if (isset($labels[$capability])) { 30 $label = $labels[$capability]; 31 } else { 32 $capobj = PhabricatorPolicyCapability::getCapabilityByKey($capability); 33 if ($capobj) { 34 $label = $capobj->getCapabilityName(); 35 } else { 36 $label = pht('Capability "%s"', $capability); 37 } 38 } 39 40 $this->setLabel($label); 41 42 return $this; 43 } 44 45 protected function getCustomControlClass() { 46 return 'aphront-form-control-policy'; 47 } 48 49 protected function getOptions() { 50 $capability = $this->capability; 51 52 $options = array(); 53 foreach ($this->policies as $policy) { 54 if ($policy->getPHID() == PhabricatorPolicies::POLICY_PUBLIC) { 55 // Never expose "Public" for capabilities which don't support it. 56 $capobj = PhabricatorPolicyCapability::getCapabilityByKey($capability); 57 if (!$capobj || !$capobj->shouldAllowPublicPolicySetting()) { 58 continue; 59 } 60 } 61 $policy_short_name = id(new PhutilUTF8StringTruncator()) 62 ->setMaximumGlyphs(28) 63 ->truncateString($policy->getName()); 64 65 $options[$policy->getType()][$policy->getPHID()] = array( 66 'name' => $policy_short_name, 67 'full' => $policy->getName(), 68 'icon' => $policy->getIcon(), 69 ); 70 } 71 72 // If we were passed several custom policy options, throw away the ones 73 // which aren't the value for this capability. For example, an object might 74 // have a custom view pollicy and a custom edit policy. When we render 75 // the selector for "Can View", we don't want to show the "Can Edit" 76 // custom policy -- if we did, the menu would look like this: 77 // 78 // Custom 79 // Custom Policy 80 // Custom Policy 81 // 82 // ...where one is the "view" custom policy, and one is the "edit" custom 83 // policy. 84 85 $type_custom = PhabricatorPolicyType::TYPE_CUSTOM; 86 if (!empty($options[$type_custom])) { 87 $options[$type_custom] = array_select_keys( 88 $options[$type_custom], 89 array($this->getValue())); 90 } 91 92 // If there aren't any custom policies, add a placeholder policy so we 93 // render a menu item. This allows the user to switch to a custom policy. 94 95 if (empty($options[$type_custom])) { 96 $placeholder = new PhabricatorPolicy(); 97 $placeholder->setName(pht('Custom Policy...')); 98 $options[$type_custom][$this->getCustomPolicyPlaceholder()] = array( 99 'name' => $placeholder->getName(), 100 'full' => $placeholder->getName(), 101 'icon' => $placeholder->getIcon(), 102 ); 103 } 104 105 $options = array_select_keys( 106 $options, 107 array( 108 PhabricatorPolicyType::TYPE_GLOBAL, 109 PhabricatorPolicyType::TYPE_USER, 110 PhabricatorPolicyType::TYPE_CUSTOM, 111 PhabricatorPolicyType::TYPE_PROJECT, 112 )); 113 114 return $options; 115 } 116 117 protected function renderInput() { 118 if (!$this->object) { 119 throw new Exception(pht('Call setPolicyObject() before rendering!')); 120 } 121 if (!$this->capability) { 122 throw new Exception(pht('Call setCapability() before rendering!')); 123 } 124 125 $policy = $this->object->getPolicy($this->capability); 126 if (!$policy) { 127 // TODO: Make this configurable. 128 $policy = PhabricatorPolicies::POLICY_USER; 129 } 130 131 if (!$this->getValue()) { 132 $this->setValue($policy); 133 } 134 135 $control_id = celerity_generate_unique_node_id(); 136 $input_id = celerity_generate_unique_node_id(); 137 138 $caret = phutil_tag( 139 'span', 140 array( 141 'class' => 'caret', 142 )); 143 144 $input = phutil_tag( 145 'input', 146 array( 147 'type' => 'hidden', 148 'id' => $input_id, 149 'name' => $this->getName(), 150 'value' => $this->getValue(), 151 )); 152 153 $options = $this->getOptions(); 154 155 $order = array(); 156 $labels = array(); 157 foreach ($options as $key => $values) { 158 $order[$key] = array_keys($values); 159 $labels[$key] = PhabricatorPolicyType::getPolicyTypeName($key); 160 } 161 162 $flat_options = array_mergev($options); 163 164 $icons = array(); 165 foreach (igroup($flat_options, 'icon') as $icon => $ignored) { 166 $icons[$icon] = id(new PHUIIconView()) 167 ->setIconFont($icon); 168 } 169 170 171 Javelin::initBehavior( 172 'policy-control', 173 array( 174 'controlID' => $control_id, 175 'inputID' => $input_id, 176 'options' => $flat_options, 177 'groups' => array_keys($options), 178 'order' => $order, 179 'icons' => $icons, 180 'labels' => $labels, 181 'value' => $this->getValue(), 182 'customPlaceholder' => $this->getCustomPolicyPlaceholder(), 183 )); 184 185 $selected = idx($flat_options, $this->getValue(), array()); 186 $selected_icon = idx($selected, 'icon'); 187 $selected_name = idx($selected, 'name'); 188 189 return phutil_tag( 190 'div', 191 array( 192 ), 193 array( 194 javelin_tag( 195 'a', 196 array( 197 'class' => 'grey button dropdown has-icon policy-control', 198 'href' => '#', 199 'mustcapture' => true, 200 'sigil' => 'policy-control', 201 'id' => $control_id, 202 ), 203 array( 204 $caret, 205 javelin_tag( 206 'span', 207 array( 208 'sigil' => 'policy-label', 209 'class' => 'phui-button-text', 210 ), 211 array( 212 idx($icons, $selected_icon), 213 $selected_name, 214 )), 215 )), 216 $input, 217 )); 218 219 return AphrontFormSelectControl::renderSelectTag( 220 $this->getValue(), 221 $this->getOptions(), 222 array( 223 'name' => $this->getName(), 224 'disabled' => $this->getDisabled() ? 'disabled' : null, 225 'id' => $this->getID(), 226 )); 227 } 228 229 private function getCustomPolicyPlaceholder() { 230 return 'custom:placeholder'; 231 } 232 233 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |