]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/designsettings.php
L10n consistency tweak.
[quix0rs-gnu-social.git] / lib / designsettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Change user password
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    Sarven Capadisli <csarven@status.net>
25  * @author    Zach Copley <zach@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('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * Base class for setting a user or group design
37  *
38  * Shows the design setting form and also handles some things like saving
39  * background images, and fetching a default design
40  *
41  * @category Settings
42  * @package  StatusNet
43  * @author   Zach Copley <zach@status.net>
44  * @author   Sarven Capadisli <csarven@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  */
48
49 class DesignSettingsAction extends SettingsAction
50 {
51     var $submitaction = null;
52
53     /**
54      * Title of the page
55      *
56      * @return string Title of the page
57      */
58     function title()
59     {
60         // TRANS: Page title for profile design page.
61         return _('Profile design');
62     }
63
64     /**
65      * Instructions for use
66      *
67      * @return instructions for use
68      */
69     function getInstructions()
70     {
71         // TRANS: Instructions for profile design page.
72         return _('Customize the way your profile looks ' .
73         'with a background image and a colour palette of your choice.');
74     }
75
76     /**
77      * Shows the design settings form
78      *
79      * @param Design $design a working design to show
80      *
81      * @return nothing
82      */
83     function showDesignForm($design)
84     {
85         $form = new DesignForm($this, $design, $this->selfUrl());
86         $form->show();
87
88     }
89
90     /**
91      * Handle a post
92      *
93      * Validate input and save changes. Reload the form with a success
94      * or error message.
95      *
96      * @return void
97      */
98     function handlePost()
99     {
100         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
101
102             // Workaround for PHP returning empty $_POST and $_FILES when POST
103             // length > post_max_size in php.ini
104
105             if (empty($_FILES)
106                 && empty($_POST)
107                 && ($_SERVER['CONTENT_LENGTH'] > 0)
108             ) {
109                 // TRANS: Form validation error in design settings form. POST should remain untranslated.
110                 $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
111                           'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
112                           intval($_SERVER['CONTENT_LENGTH']));
113
114                 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
115                 return;
116             }
117         }
118
119         // CSRF protection
120         $token = $this->trimmed('token');
121         if (!$token || $token != common_session_token()) {
122             // TRANS: Client error displayed when the session token does not match or is not given.
123             $this->showForm(_('There was a problem with your session token. '.
124                               'Try again, please.'));
125             return;
126         }
127
128         if ($this->arg('save')) {
129             $this->saveDesign();
130         } else if ($this->arg('defaults')) {
131             $this->restoreDefaults();
132         } else {
133             // TRANS: Unknown form validation error in design settings form.
134             $this->showForm(_('Unexpected form submission.'));
135         }
136     }
137
138     /**
139      * Add the Farbtastic stylesheet
140      *
141      * @return void
142      */
143     function showStylesheets()
144     {
145         parent::showStylesheets();
146         $this->cssLink('js/farbtastic/farbtastic.css',null,'screen, projection, tv');
147     }
148
149     /**
150      * Add the Farbtastic scripts
151      *
152      * @return void
153      */
154     function showScripts()
155     {
156         parent::showScripts();
157
158         $this->script('farbtastic/farbtastic.js');
159         $this->script('userdesign.go.js');
160
161         $this->autofocus('design_background-image_file');
162     }
163
164     /**
165      * Save the background image, if any, and set its disposition
166      *
167      * @param Design $design a working design to attach the img to
168      *
169      * @return nothing
170      */
171     function saveBackgroundImage($design)
172     {
173         // Now that we have a Design ID we can add a file to the design.
174         // XXX: This is an additional DB hit, but figured having the image
175         // associated with the Design rather than the User was worth
176         // it. -- Zach
177
178         if (array_key_exists('design_background-image_file', $_FILES) &&
179           $_FILES['design_background-image_file']['error'] == UPLOAD_ERR_OK) {
180
181             $filepath = null;
182
183             try {
184                 $imagefile = ImageFile::fromUpload('design_background-image_file');
185             } catch (Exception $e) {
186                 $this->showForm($e->getMessage());
187                 return;
188             }
189
190             $filename = Design::filename($design->id,
191                                          image_type_to_extension($imagefile->type),
192                                          common_timestamp());
193
194             $filepath = Design::path($filename);
195
196             move_uploaded_file($imagefile->filepath, $filepath);
197
198             // delete any old backround img laying around
199
200             if (isset($design->backgroundimage)) {
201                 @unlink(Design::path($design->backgroundimage));
202             }
203
204             $original = clone($design);
205
206             $design->backgroundimage = $filename;
207
208             // default to on, no tile
209
210             $design->setDisposition(true, false, false);
211
212             $result = $design->update($original);
213
214             if ($result === false) {
215                 common_log_db_error($design, 'UPDATE', __FILE__);
216                 // TRANS: Error message displayed if design settings could not be saved.
217                 $this->showForm(_('Could not update your design.'));
218                 return;
219             }
220         }
221     }
222
223     /**
224      * Restore the user or group design to system defaults
225      *
226      * @return nothing
227      */
228     function restoreDefaults()
229     {
230         $design = $this->getWorkingDesign();
231
232         if (!empty($design)) {
233
234             $result = $design->delete();
235
236             if ($result === false) {
237                 common_log_db_error($design, 'DELETE', __FILE__);
238                 // TRANS: Error message displayed if design settings could not be saved after clicking "Use defaults".
239                 $this->showForm(_('Could not update your design.'));
240                 return;
241             }
242         }
243
244         // TRANS: Success message displayed if design settings were saved after clicking "Use defaults".
245         $this->showForm(_('Design defaults restored.'), true);
246     }
247 }