~ubuntu-branches/ubuntu/vivid/phabricator/vivid-proposed

« back to all changes in this revision

Viewing changes to phabricator/src/applications/settings/panel/PhabricatorDisplayPreferencesSettingsPanel.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2015-01-29 00:15:58 UTC
  • mfrom: (0.16.1) (0.15.1) (0.12.2) (2.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20150129001558-na84707j70qqla7z
Tags: 0~git20150129-1
* New snapshot release
* restricted access to local config file (closes: #775479)
* moved local config file to /var/lib/phabricator (closes: #775478)
* switched mysql-server dependency to recommends (closes: #773536)
* use /run instead of /var/run (closes: #775803)
* prevent package reinstall from overwritting local changes (closes: #776288)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
final class PhabricatorDisplayPreferencesSettingsPanel
 
4
  extends PhabricatorSettingsPanel {
 
5
 
 
6
  public function getPanelKey() {
 
7
    return 'display';
 
8
  }
 
9
 
 
10
  public function getPanelName() {
 
11
    return pht('Display Preferences');
 
12
  }
 
13
 
 
14
  public function getPanelGroup() {
 
15
    return pht('Application Settings');
 
16
  }
 
17
 
 
18
  public function processRequest(AphrontRequest $request) {
 
19
    $user = $request->getUser();
 
20
    $preferences = $user->loadPreferences();
 
21
 
 
22
    $pref_monospaced   = PhabricatorUserPreferences::PREFERENCE_MONOSPACED;
 
23
    $pref_editor       = PhabricatorUserPreferences::PREFERENCE_EDITOR;
 
24
    $pref_multiedit    = PhabricatorUserPreferences::PREFERENCE_MULTIEDIT;
 
25
    $pref_titles       = PhabricatorUserPreferences::PREFERENCE_TITLES;
 
26
    $pref_monospaced_textareas =
 
27
      PhabricatorUserPreferences::PREFERENCE_MONOSPACED_TEXTAREAS;
 
28
 
 
29
    $errors = array();
 
30
    $e_editor = null;
 
31
    if ($request->isFormPost()) {
 
32
      $monospaced = $request->getStr($pref_monospaced);
 
33
 
 
34
      // Prevent the user from doing stupid things.
 
35
      $monospaced = preg_replace('/[^a-z0-9 ,".]+/i', '', $monospaced);
 
36
 
 
37
      $preferences->setPreference($pref_titles, $request->getStr($pref_titles));
 
38
      $preferences->setPreference($pref_editor, $request->getStr($pref_editor));
 
39
      $preferences->setPreference(
 
40
        $pref_multiedit,
 
41
        $request->getStr($pref_multiedit));
 
42
      $preferences->setPreference($pref_monospaced, $monospaced);
 
43
      $preferences->setPreference(
 
44
        $pref_monospaced_textareas,
 
45
        $request->getStr($pref_monospaced_textareas));
 
46
 
 
47
      $editor_pattern = $preferences->getPreference($pref_editor);
 
48
      if (strlen($editor_pattern)) {
 
49
        $ok = PhabricatorHelpEditorProtocolController::hasAllowedProtocol(
 
50
          $editor_pattern);
 
51
        if (!$ok) {
 
52
          $allowed_key = 'uri.allowed-editor-protocols';
 
53
          $allowed_protocols = PhabricatorEnv::getEnvConfig($allowed_key);
 
54
 
 
55
          $proto_names = array();
 
56
          foreach (array_keys($allowed_protocols) as $protocol) {
 
57
            $proto_names[] = $protocol.'://';
 
58
          }
 
59
 
 
60
          $errors[] = pht(
 
61
            'Editor link has an invalid or missing protocol. You must '.
 
62
            'use a whitelisted editor protocol from this list: %s. To '.
 
63
            'add protocols, update %s.',
 
64
            implode(', ', $proto_names),
 
65
            phutil_tag('tt', array(), $allowed_key));
 
66
 
 
67
          $e_editor = pht('Invalid');
 
68
        }
 
69
      }
 
70
 
 
71
      if (!$errors) {
 
72
        $preferences->save();
 
73
        return id(new AphrontRedirectResponse())
 
74
          ->setURI($this->getPanelURI('?saved=true'));
 
75
      }
 
76
    }
 
77
 
 
78
    $example_string = <<<EXAMPLE
 
79
// This is what your monospaced font currently looks like.
 
80
function helloWorld() {
 
81
  alert("Hello world!");
 
82
}
 
83
EXAMPLE;
 
84
 
 
85
    $editor_doc_link = phutil_tag(
 
86
      'a',
 
87
      array(
 
88
        'href' => PhabricatorEnv::getDoclink(
 
89
          'User Guide: Configuring an External Editor'),
 
90
      ),
 
91
      pht('User Guide: Configuring an External Editor'));
 
92
 
 
93
    $font_default = PhabricatorEnv::getEnvConfig('style.monospace');
 
94
 
 
95
    $pref_monospaced_textareas_value = $preferences
 
96
      ->getPreference($pref_monospaced_textareas);
 
97
    if (!$pref_monospaced_textareas_value) {
 
98
      $pref_monospaced_textareas_value = 'disabled';
 
99
    }
 
100
 
 
101
    $editor_instructions = pht('Link to edit files in external editor. '.
 
102
      '%%f is replaced by filename, %%l by line number, %%r by repository '.
 
103
      'callsign, %%%% by literal %%. For documentation, see: %s',
 
104
      $editor_doc_link);
 
105
 
 
106
    $form = id(new AphrontFormView())
 
107
      ->setUser($user)
 
108
      ->appendChild(
 
109
        id(new AphrontFormSelectControl())
 
110
          ->setLabel(pht('Page Titles'))
 
111
          ->setName($pref_titles)
 
112
          ->setValue($preferences->getPreference($pref_titles))
 
113
          ->setOptions(
 
114
            array(
 
115
              'glyph' =>
 
116
              pht("In page titles, show Tool names as unicode glyphs: ".
 
117
                "\xE2\x9A\x99"),
 
118
              'text' =>
 
119
              pht('In page titles, show Tool names as plain text: '.
 
120
                '[Differential]'),
 
121
            )))
 
122
      ->appendChild(
 
123
        id(new AphrontFormTextControl())
 
124
        ->setLabel(pht('Editor Link'))
 
125
        ->setName($pref_editor)
 
126
        ->setCaption($editor_instructions)
 
127
        ->setError($e_editor)
 
128
        ->setValue($preferences->getPreference($pref_editor)))
 
129
      ->appendChild(
 
130
        id(new AphrontFormSelectControl())
 
131
        ->setLabel(pht('Edit Multiple Files'))
 
132
        ->setName($pref_multiedit)
 
133
        ->setOptions(array(
 
134
          '' => pht('Supported (paths separated by spaces)'),
 
135
          'disable' => pht('Not Supported'),
 
136
        ))
 
137
        ->setValue($preferences->getPreference($pref_multiedit)))
 
138
      ->appendChild(
 
139
        id(new AphrontFormTextControl())
 
140
        ->setLabel(pht('Monospaced Font'))
 
141
        ->setName($pref_monospaced)
 
142
        // Check plz
 
143
        ->setCaption(hsprintf(
 
144
          '%s<br />(%s: %s)',
 
145
          pht('Overrides default fonts in tools like Differential.'),
 
146
          pht('Default'),
 
147
          $font_default))
 
148
        ->setValue($preferences->getPreference($pref_monospaced)))
 
149
      ->appendChild(
 
150
        id(new AphrontFormMarkupControl())
 
151
        ->setValue(phutil_tag(
 
152
          'pre',
 
153
          array('class' => 'PhabricatorMonospaced'),
 
154
          $example_string)))
 
155
      ->appendChild(
 
156
        id(new AphrontFormRadioButtonControl())
 
157
        ->setLabel(pht('Monospaced Textareas'))
 
158
        ->setName($pref_monospaced_textareas)
 
159
        ->setValue($pref_monospaced_textareas_value)
 
160
        ->addButton('enabled', pht('Enabled'),
 
161
          pht('Show all textareas using the monospaced font defined above.'))
 
162
        ->addButton('disabled', pht('Disabled'), null));
 
163
 
 
164
    $form->appendChild(
 
165
      id(new AphrontFormSubmitControl())
 
166
        ->setValue(pht('Save Preferences')));
 
167
 
 
168
    $form_box = id(new PHUIObjectBoxView())
 
169
      ->setHeaderText(pht('Display Preferences'))
 
170
      ->setFormErrors($errors)
 
171
      ->setFormSaved($request->getStr('saved') === 'true')
 
172
      ->setForm($form);
 
173
 
 
174
    return array(
 
175
      $form_box,
 
176
    );
 
177
  }
 
178
}