]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/urlsettings.php
shortenLinks _after_ media upload to be consistent with api
[quix0rs-gnu-social.git] / actions / urlsettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Miscellaneous settings
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Settings
23  * @package   StatusNet
24  * @author    Robin Millette <millette@status.net>
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * Miscellaneous settings actions
35  *
36  * Currently this just manages URL shortening.
37  *
38  * @category Settings
39  * @package  StatusNet
40  * @author   Robin Millette <millette@status.net>
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45 class UrlsettingsAction extends SettingsAction
46 {
47     /**
48      * Title of the page
49      *
50      * @return string Title of the page
51      */
52     function title()
53     {
54         // TRANS: Title of URL settings tab in profile settings.
55         return _('URL settings');
56     }
57
58     /**
59      * Instructions for use
60      *
61      * @return instructions for use
62      */
63     function getInstructions()
64     {
65         // TRANS: Instructions for tab "Other" in user profile settings.
66         return _('Manage various other options.');
67     }
68
69     function showScripts()
70     {
71         parent::showScripts();
72         $this->autofocus('urlshorteningservice');
73     }
74
75     /**
76      * Content area of the page
77      *
78      * Shows a form for uploading an avatar.
79      *
80      * @return void
81      */
82     function showContent()
83     {
84         $user = $this->scoped->getUser();
85
86         $this->elementStart('form', array('method' => 'post',
87                                           'id' => 'form_settings_other',
88                                           'class' => 'form_settings',
89                                           'action' =>
90                                           common_local_url('urlsettings')));
91         $this->elementStart('fieldset');
92         $this->hidden('token', common_session_token());
93         $this->elementStart('ul', 'form_data');
94
95         $shorteners = array();
96
97         Event::handle('GetUrlShorteners', array(&$shorteners));
98
99         $services = array();
100
101         foreach ($shorteners as $name => $value)
102         {
103             $services[$name] = $name;
104             if ($value['freeService']) {
105                 // TRANS: Used as a suffix for free URL shorteners in a dropdown list in the tab "Other" of a
106                 // TRANS: user's profile settings. This message has one space at the beginning. Use your
107                 // TRANS: language's word separator here if it has one (most likely a single space).
108                 $services[$name] .= _(' (free service)');
109             }
110         }
111
112         // Include default values
113
114         // TRANS: Default value for URL shortening settings.
115         $services['none']     = _('[none]');
116         // TRANS: Default value for URL shortening settings.
117         $services['internal'] = _('[internal]');
118
119         if ($services) {
120             asort($services);
121
122             $this->elementStart('li');
123             // TRANS: Label for dropdown with URL shortener services.
124             $this->dropdown('urlshorteningservice', _('Shorten URLs with'),
125                             // TRANS: Tooltip for for dropdown with URL shortener services.
126                             $services, _('Automatic shortening service to use.'),
127                             false, $user->urlshorteningservice);
128             $this->elementEnd('li');
129         }
130         $this->elementStart('li');
131         $this->input('maxurllength',
132                      // TRANS: Field label in URL settings in profile.
133                      _('URL longer than'),
134                      (!is_null($this->arg('maxurllength'))) ?
135                      $this->arg('maxurllength') : User_urlshortener_prefs::maxUrlLength($user),
136                      // TRANS: Field title in URL settings in profile.
137                      _('URLs longer than this will be shortened, -1 means never shorten because a URL is long.'));
138         $this->elementEnd('li');
139         $this->elementStart('li');
140         $this->input('maxnoticelength',
141                      // TRANS: Field label in URL settings in profile.
142                      _('Text longer than'),
143                      (!is_null($this->arg('maxnoticelength'))) ?
144                      $this->arg('maxnoticelength') : User_urlshortener_prefs::maxNoticeLength($user),
145                      // TRANS: Field title in URL settings in profile.
146                      _('URLs in notices longer than this will always be shortened, -1 means only shorten if the full post exceeds maximum length.'));
147         $this->elementEnd('li');
148         $this->elementEnd('ul');
149         // TRANS: Button text for saving "Other settings" in profile.
150         $this->submit('save', _m('BUTTON','Save'));
151         $this->elementEnd('fieldset');
152         $this->elementEnd('form');
153     }
154
155     protected function doPost()
156     {
157         $urlshorteningservice = $this->trimmed('urlshorteningservice');
158
159         if (!is_null($urlshorteningservice) && strlen($urlshorteningservice) > 50) {
160             // TRANS: Form validation error for form "Other settings" in user profile.
161             throw new ClientException(_('URL shortening service is too long (maximum 50 characters).'));
162         }
163
164         $maxurllength = $this->trimmed('maxurllength');
165
166         if (!Validate::number($maxurllength, array('min' => -1))) {
167             // TRANS: Client exception thrown when the maximum URL settings value is invalid in profile URL settings.
168             throw new ClientException(_('Invalid number for maximum URL length.'));
169         }
170
171         $maxnoticelength = $this->trimmed('maxnoticelength');
172
173         if (!Validate::number($maxnoticelength, array('min' => -1))) {
174             // TRANS: Client exception thrown when the maximum notice length settings value is invalid in profile URL settings.
175             throw new ClientException(_('Invalid number for maximum notice length.'));
176         }
177
178         $user = $this->scoped->getUser();
179
180         $user->query('BEGIN');
181
182         $original = clone($user);
183
184         $user->urlshorteningservice = $urlshorteningservice;
185
186         $result = $user->update($original);
187
188         if ($result === false) {
189             common_log_db_error($user, 'UPDATE', __FILE__);
190             $user->query('ROLLBACK');
191             // TRANS: Server error displayed when "Other" settings in user profile could not be updated on the server.
192             throw new ServerException(_('Could not update user.'));
193         }
194
195         $prefs = User_urlshortener_prefs::getPrefs($user);
196         $orig  = null;
197
198         if (!$prefs instanceof User_urlshortener_prefs) {
199             $prefs = new User_urlshortener_prefs();
200
201             $prefs->user_id = $user->id;
202             $prefs->created = common_sql_now();
203         } else {
204             $orig = clone($prefs);
205         }
206
207         $prefs->urlshorteningservice = $urlshorteningservice;
208         $prefs->maxurllength         = $maxurllength;
209         $prefs->maxnoticelength      = $maxnoticelength;
210
211         if ($orig instanceof User_urlshortener_prefs) {
212             $result = $prefs->update($orig);
213         } else {
214             $result = $prefs->insert();
215         }
216
217         if ($result === null) {
218             $user->query('ROLLBACK');
219             // TRANS: Server exception thrown in profile URL settings when preferences could not be saved.
220             throw new ServerException(_('Error saving user URL shortening preferences.'));
221         }
222
223         $user->query('COMMIT');
224
225         // TRANS: Confirmation message after saving preferences.
226         return _('Preferences saved.');
227     }
228 }