3 * StatusNet, the distributed open-source microblogging tool
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.
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.
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/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @author Zach Copley <zach@status.net>
26 * @copyright 2008-2011 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/
31 if (!defined('GNUSOCIAL')) { exit(1); }
36 * We use jCrop plugin for jQuery to crop the image after upload.
40 * @author Evan Prodromou <evan@status.net>
41 * @author Zach Copley <zach@status.net>
42 * @author Sarven Capadisli <csarven@status.net>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
46 class GrouplogoAction extends GroupAction
49 var $imagefile = null;
57 protected function prepare(array $args=array())
59 parent::prepare($args);
61 if (!common_logged_in()) {
62 // TRANS: Client error displayed when trying to create a group while not logged in.
63 $this->clientError(_('You must be logged in to create a group.'));
66 $nickname_arg = $this->trimmed('nickname');
67 $nickname = common_canonical_nickname($nickname_arg);
69 // Permanent redirect on non-canonical nickname
71 if ($nickname_arg != $nickname) {
72 $args = array('nickname' => $nickname);
73 common_redirect(common_local_url('grouplogo', $args), 301);
77 // TRANS: Client error displayed when trying to change group logo settings without providing a nickname.
78 $this->clientError(_('No nickname.'), 404);
81 $groupid = $this->trimmed('groupid');
84 $this->group = User_group::getKV('id', $groupid);
86 $local = Local_group::getKV('nickname', $nickname);
88 $this->group = User_group::getKV('id', $local->group_id);
93 // TRANS: Client error displayed when trying to update logo settings for a non-existing group.
94 $this->clientError(_('No such group.'), 404);
97 $cur = common_current_user();
99 if (!$cur->isAdmin($this->group)) {
100 // TRANS: Client error displayed when trying to change group logo settings while not being a group admin.
101 $this->clientError(_('You must be an admin to edit the group.'), 403);
107 protected function handle()
110 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
117 function showForm($msg = null, $success = false)
120 $this->success = $success;
128 * @return string Title of the page
132 // TRANS: Title for group logo settings page.
133 return _('Group logo');
137 * Instructions for use
139 * @return instructions for use
141 function getInstructions()
143 // TRANS: Instructions for group logo page.
144 // TRANS: %s is the maximum file size for that site.
145 return sprintf(_('You can upload a logo image for your group. The maximum file size is %s.'), ImageFile::maxFileSize());
149 * Content area of the page
151 * Shows a form for uploading an avatar.
155 function showContent()
157 if ($this->mode == 'crop') {
158 $this->showCropForm();
160 $this->showUploadForm();
164 function showUploadForm()
166 $user = common_current_user();
168 $profile = $user->getProfile();
171 common_log_db_error($user, 'SELECT', __FILE__);
172 // TRANS: Error message displayed when referring to a user without a profile.
173 $this->serverError(_('User has no profile.'));
176 $original = $this->group->original_logo;
178 $this->elementStart('form', array('enctype' => 'multipart/form-data',
180 'id' => 'form_settings_avatar',
181 'class' => 'form_settings',
183 common_local_url('grouplogo',
184 array('nickname' => $this->group->nickname))));
185 $this->elementStart('fieldset');
186 // TRANS: Group logo form legend.
187 $this->element('legend', null, _('Group logo'));
188 $this->hidden('token', common_session_token());
190 $this->elementStart('ul', 'form_data');
192 $this->elementStart('li', array('id' => 'avatar_original',
193 'class' => 'avatar_view'));
194 // TRANS: Uploaded original file in group logo form.
195 $this->element('h2', null, _('Original'));
196 $this->elementStart('div', array('id'=>'avatar_original_view'));
197 $this->element('img', array('src' => $this->group->original_logo,
198 'alt' => $this->group->nickname));
199 $this->elementEnd('div');
200 $this->elementEnd('li');
203 if ($this->group->homepage_logo) {
204 $this->elementStart('li', array('id' => 'avatar_preview',
205 'class' => 'avatar_view'));
206 // TRANS: Header for preview of to be displayed group logo.
207 $this->element('h2', null, _('Preview'));
208 $this->elementStart('div', array('id'=>'avatar_preview_view'));
209 $this->element('img', array('src' => $this->group->homepage_logo,
210 'width' => AVATAR_PROFILE_SIZE,
211 'height' => AVATAR_PROFILE_SIZE,
212 'alt' => $this->group->nickname));
213 $this->elementEnd('div');
214 $this->elementEnd('li');
217 $this->elementStart('li', array ('id' => 'settings_attach'));
218 $this->element('input', array('name' => 'MAX_FILE_SIZE',
220 'id' => 'MAX_FILE_SIZE',
221 'value' => ImageFile::maxFileSizeInt()));
222 $this->element('input', array('name' => 'avatarfile',
224 'id' => 'avatarfile'));
225 $this->elementEnd('li');
226 $this->elementEnd('ul');
228 $this->elementStart('ul', 'form_actions');
229 $this->elementStart('li');
230 // TRANS: Submit button for uploading a group logo.
231 $this->submit('upload', _('Upload'));
232 $this->elementEnd('li');
233 $this->elementEnd('ul');
235 $this->elementEnd('fieldset');
236 $this->elementEnd('form');
239 function showCropForm()
241 $this->elementStart('form', array('method' => 'post',
242 'id' => 'form_settings_avatar',
243 'class' => 'form_settings',
245 common_local_url('grouplogo',
246 array('nickname' => $this->group->nickname))));
247 $this->elementStart('fieldset');
248 // TRANS: Legend for group logo settings fieldset.
249 $this->element('legend', null, _('Avatar settings'));
250 $this->hidden('token', common_session_token());
252 $this->elementStart('ul', 'form_data');
254 $this->elementStart('li',
255 array('id' => 'avatar_original',
256 'class' => 'avatar_view'));
257 // TRANS: Header for originally uploaded file before a crop on the group logo page.
258 $this->element('h2', null, _('Original'));
259 $this->elementStart('div', array('id'=>'avatar_original_view'));
260 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
261 'width' => $this->filedata['width'],
262 'height' => $this->filedata['height'],
263 'alt' => $this->group->nickname));
264 $this->elementEnd('div');
265 $this->elementEnd('li');
267 $this->elementStart('li',
268 array('id' => 'avatar_preview',
269 'class' => 'avatar_view'));
270 // TRANS: Header for the cropped group logo on the group logo page.
271 $this->element('h2', null, _('Preview'));
272 $this->elementStart('div', array('id'=>'avatar_preview_view'));
273 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
274 'width' => AVATAR_PROFILE_SIZE,
275 'height' => AVATAR_PROFILE_SIZE,
276 'alt' => $this->group->nickname));
277 $this->elementEnd('div');
279 foreach (array('avatar_crop_x', 'avatar_crop_y',
280 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
281 $this->element('input', array('name' => $crop_info,
283 'id' => $crop_info));
286 // TRANS: Button text for cropping an uploaded group logo.
287 $this->submit('crop', _('Crop'));
289 $this->elementEnd('li');
290 $this->elementEnd('ul');
291 $this->elementEnd('fieldset');
292 $this->elementEnd('form');
298 * We mux on the button name to figure out what the user actually wanted.
302 function handlePost()
306 $token = $this->trimmed('token');
307 if (!$token || $token != common_session_token()) {
308 // TRANS: Form validation error message.
309 $this->show_form(_('There was a problem with your session token. '.
310 'Try again, please.'));
314 if ($this->arg('upload')) {
316 } else if ($this->arg('crop')) {
319 // TRANS: Form validation error message when an unsupported argument is used.
320 $this->showForm(_('Unexpected form submission.'));
325 * Handle an image upload
327 * Does all the magic for handling an image upload, and crops the
332 function uploadLogo()
335 $imagefile = ImageFile::fromUpload('avatarfile');
336 } catch (Exception $e) {
337 $this->showForm($e->getMessage());
341 $type = $imagefile->preferredType();
342 $filename = Avatar::filename($this->group->id,
343 image_type_to_extension($type),
345 'group-temp-'.common_timestamp());
347 $filepath = Avatar::path($filename);
349 $imagefile->copyTo($filepath);
351 $filedata = array('filename' => $filename,
352 'filepath' => $filepath,
353 'width' => $imagefile->width,
354 'height' => $imagefile->height,
357 $_SESSION['FILEDATA'] = $filedata;
359 $this->filedata = $filedata;
361 $this->mode = 'crop';
363 // TRANS: Form instructions on the group logo page.
364 $this->showForm(_('Pick a square area of the image to be the logo.'),
369 * Handle the results of jcrop.
375 $filedata = $_SESSION['FILEDATA'];
378 // TRANS: Server error displayed trying to crop an uploaded group logo that is no longer present.
379 $this->serverError(_('Lost our file data.'));
382 // If image is not being cropped assume pos & dimentions of original
383 $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
384 $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
385 $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$filedata['width'];
386 $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height'];
387 $size = min($dest_w, $dest_h, common_config('avatar', 'maxsize'));
388 $box = array('width' => $size, 'height' => $size,
389 'x' => $dest_x, 'y' => $dest_y,
390 'w' => $dest_w, 'h' => $dest_h);
392 $profile = $this->group->getProfile();
394 $imagefile = new ImageFile(null, $filedata['filepath']);
395 $filename = Avatar::filename($profile->getID(), image_type_to_extension($imagefile->preferredType()),
396 $size, common_timestamp());
398 $imagefile->resizeTo(Avatar::path($filename), $box);
400 if ($profile->setOriginal($filename)) {
401 @unlink($filedata['filepath']);
402 unset($_SESSION['FILEDATA']);
403 $this->mode = 'upload';
404 // TRANS: Form success message after updating a group logo.
405 $this->showForm(_('Logo updated.'), true);
407 // TRANS: Form failure message after failing to update a group logo.
408 $this->showForm(_('Failed updating logo.'));
412 function showPageNotice()
415 $this->element('div', ($this->success) ? 'success' : 'error',
418 $inst = $this->getInstructions();
419 $output = common_markup_to_html($inst);
421 $this->elementStart('div', 'instructions');
423 $this->elementEnd('div');
428 * Add the jCrop stylesheet
432 function showStylesheets()
434 parent::showStylesheets();
435 $this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css','base','screen, projection, tv');
439 * Add the jCrop scripts
443 function showScripts()
445 parent::showScripts();
447 if ($this->mode == 'crop') {
448 $this->script('extlib/jquery-jcrop/jcrop.js');
449 $this->script('jcrop.go.js');
452 $this->autofocus('avatarfile');