Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / settings / panel / PhabricatorEmailPreferencesSettingsPanel.php
blob50d64072f57ab4095935b65425413dfd8fb20510
1 <?php
3 final class PhabricatorEmailPreferencesSettingsPanel
4 extends PhabricatorSettingsPanel {
6 public function getPanelKey() {
7 return 'emailpreferences';
10 public function getPanelName() {
11 return pht('Email Preferences');
14 public function getPanelMenuIcon() {
15 return 'fa-envelope-open-o';
18 public function getPanelGroupKey() {
19 return PhabricatorSettingsEmailPanelGroup::PANELGROUPKEY;
22 public function isManagementPanel() {
23 if ($this->getUser()->getIsMailingList()) {
24 return true;
27 return false;
30 public function isTemplatePanel() {
31 return true;
34 public function processRequest(AphrontRequest $request) {
35 $viewer = $this->getViewer();
36 $user = $this->getUser();
38 $preferences = $this->getPreferences();
40 $value_email = PhabricatorEmailTagsSetting::VALUE_EMAIL;
42 $errors = array();
43 if ($request->isFormPost()) {
44 $new_tags = $request->getArr('mailtags');
45 $mailtags = $preferences->getPreference('mailtags', array());
46 $all_tags = $this->getAllTags($user);
48 foreach ($all_tags as $key => $label) {
49 $mailtags[$key] = (int)idx($new_tags, $key, $value_email);
52 $this->writeSetting(
53 $preferences,
54 PhabricatorEmailTagsSetting::SETTINGKEY,
55 $mailtags);
57 return id(new AphrontRedirectResponse())
58 ->setURI($this->getPanelURI('?saved=true'));
61 $mailtags = $preferences->getSettingValue(
62 PhabricatorEmailTagsSetting::SETTINGKEY);
64 $form = id(new AphrontFormView())
65 ->setUser($viewer);
67 $form->appendRemarkupInstructions(
68 pht(
69 'You can adjust **Application Settings** here to customize when '.
70 'you are emailed and notified.'.
71 "\n\n".
72 "| Setting | Effect\n".
73 "| ------- | -------\n".
74 "| Email | You will receive an email and a notification, but the ".
75 "notification will be marked \"read\".\n".
76 "| Notify | You will receive an unread notification only.\n".
77 "| Ignore | You will receive nothing.\n".
78 "\n\n".
79 'If an update makes several changes (like adding CCs to a task, '.
80 'closing it, and adding a comment) you will receive the strongest '.
81 'notification any of the changes is configured to deliver.'.
82 "\n\n".
83 'These preferences **only** apply to objects you are connected to '.
84 '(for example, Revisions where you are a reviewer or tasks you are '.
85 'CC\'d on). To receive email alerts when other objects are created, '.
86 'configure [[ /herald/ | Herald Rules ]].'));
88 $editors = $this->getAllEditorsWithTags($user);
90 // Find all the tags shared by more than one application, and put them
91 // in a "common" group.
92 $all_tags = array();
93 foreach ($editors as $editor) {
94 foreach ($editor->getMailTagsMap() as $tag => $name) {
95 if (empty($all_tags[$tag])) {
96 $all_tags[$tag] = array(
97 'count' => 0,
98 'name' => $name,
101 $all_tags[$tag]['count'];
105 $common_tags = array();
106 foreach ($all_tags as $tag => $info) {
107 if ($info['count'] > 1) {
108 $common_tags[$tag] = $info['name'];
112 // Build up the groups of application-specific options.
113 $tag_groups = array();
114 foreach ($editors as $editor) {
115 $tag_groups[] = array(
116 $editor->getEditorObjectsDescription(),
117 array_diff_key($editor->getMailTagsMap(), $common_tags),
121 // Sort them, then put "Common" at the top.
122 $tag_groups = isort($tag_groups, 0);
123 if ($common_tags) {
124 array_unshift($tag_groups, array(pht('Common'), $common_tags));
127 // Finally, build the controls.
128 foreach ($tag_groups as $spec) {
129 list($label, $map) = $spec;
130 $control = $this->buildMailTagControl($label, $map, $mailtags);
131 $form->appendChild($control);
134 $form
135 ->appendChild(
136 id(new AphrontFormSubmitControl())
137 ->setValue(pht('Save Preferences')));
139 $form_box = id(new PHUIObjectBoxView())
140 ->setHeaderText(pht('Email Preferences'))
141 ->setFormErrors($errors)
142 ->setBackground(PHUIObjectBoxView::WHITE_CONFIG)
143 ->setForm($form);
145 return $form_box;
148 private function getAllEditorsWithTags(PhabricatorUser $user = null) {
149 $editors = id(new PhutilClassMapQuery())
150 ->setAncestorClass('PhabricatorApplicationTransactionEditor')
151 ->setFilterMethod('getMailTagsMap')
152 ->execute();
154 foreach ($editors as $key => $editor) {
155 // Remove editors for applications which are not installed.
156 $app = $editor->getEditorApplicationClass();
157 if ($app !== null && $user !== null) {
158 if (!PhabricatorApplication::isClassInstalledForViewer($app, $user)) {
159 unset($editors[$key]);
164 return $editors;
167 private function getAllTags(PhabricatorUser $user = null) {
168 $tags = array();
169 foreach ($this->getAllEditorsWithTags($user) as $editor) {
170 $tags += $editor->getMailTagsMap();
172 return $tags;
175 private function buildMailTagControl(
176 $control_label,
177 array $tags,
178 array $prefs) {
180 $value_email = PhabricatorEmailTagsSetting::VALUE_EMAIL;
181 $value_notify = PhabricatorEmailTagsSetting::VALUE_NOTIFY;
182 $value_ignore = PhabricatorEmailTagsSetting::VALUE_IGNORE;
184 $content = array();
185 foreach ($tags as $key => $label) {
186 $select = AphrontFormSelectControl::renderSelectTag(
187 (int)idx($prefs, $key, $value_email),
188 array(
189 $value_email => pht("\xE2\x9A\xAB Email"),
190 $value_notify => pht("\xE2\x97\x90 Notify"),
191 $value_ignore => pht("\xE2\x9A\xAA Ignore"),
193 array(
194 'name' => 'mailtags['.$key.']',
197 $content[] = phutil_tag(
198 'div',
199 array(
200 'class' => 'psb',
202 array(
203 $select,
204 ' ',
205 $label,
209 $control = new AphrontFormStaticControl();
210 $control->setLabel($control_label);
211 $control->setValue($content);
213 return $control;