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-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/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
37 define('MAX_ORIGINAL', 480);
42 * We use jCrop plugin for jQuery to crop the image after upload.
46 * @author Evan Prodromou <evan@status.net>
47 * @author Zach Copley <zach@status.net>
48 * @author Sarven Capadisli <csarven@status.net>
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50 * @link http://status.net/
52 class GrouplogoAction extends GroupDesignAction
55 var $imagefile = null;
64 function prepare($args)
66 parent::prepare($args);
68 if (!common_logged_in()) {
69 // TRANS: Client error displayed when trying to create a group while not logged in.
70 $this->clientError(_('You must be logged in to create a group.'));
74 $nickname_arg = $this->trimmed('nickname');
75 $nickname = common_canonical_nickname($nickname_arg);
77 // Permanent redirect on non-canonical nickname
79 if ($nickname_arg != $nickname) {
80 $args = array('nickname' => $nickname);
81 common_redirect(common_local_url('grouplogo', $args), 301);
86 // TRANS: Client error displayed when trying to change group logo settings without having a nickname.
87 $this->clientError(_('No nickname.'), 404);
91 $groupid = $this->trimmed('groupid');
94 $this->group = User_group::staticGet('id', $groupid);
96 $local = Local_group::staticGet('nickname', $nickname);
98 $this->group = User_group::staticGet('id', $local->group_id);
103 // TRANS: Client error displayed when trying to update logo settings for a non-existing group.
104 $this->clientError(_('No such group.'), 404);
108 $cur = common_current_user();
110 if (!$cur->isAdmin($this->group)) {
111 // TRANS: Client error displayed when trying to change group logo settings while not being a group admin.
112 $this->clientError(_('You must be an admin to edit the group.'), 403);
119 function handle($args)
121 parent::handle($args);
122 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
129 function showForm($msg = null, $success = false)
132 $this->success = $success;
140 * @return string Title of the page
144 // TRANS: Title for group logo settings page.
145 return _('Group logo');
149 * Instructions for use
151 * @return instructions for use
153 function getInstructions()
155 // TRANS: Instructions for group logo page.
156 // TRANS: %s is the maximum file size for that site.
157 return sprintf(_('You can upload a logo image for your group. The maximum file size is %s.'), ImageFile::maxFileSize());
161 * Content area of the page
163 * Shows a form for uploading an avatar.
167 function showContent()
169 if ($this->mode == 'crop') {
170 $this->showCropForm();
172 $this->showUploadForm();
176 function showUploadForm()
178 $user = common_current_user();
180 $profile = $user->getProfile();
183 common_log_db_error($user, 'SELECT', __FILE__);
184 // TRANS: Server error displayed coming across a request from a user without a profile.
185 $this->serverError(_('User without matching profile.'));
189 $original = $this->group->original_logo;
191 $this->elementStart('form', array('enctype' => 'multipart/form-data',
193 'id' => 'form_settings_avatar',
194 'class' => 'form_settings',
196 common_local_url('grouplogo',
197 array('nickname' => $this->group->nickname))));
198 $this->elementStart('fieldset');
199 // TRANS: Group logo form legend.
200 $this->element('legend', null, _('Group logo'));
201 $this->hidden('token', common_session_token());
203 $this->elementStart('ul', 'form_data');
205 $this->elementStart('li', array('id' => 'avatar_original',
206 'class' => 'avatar_view'));
207 // TRANS: Uploaded original file in group logo form.
208 $this->element('h2', null, _("Original"));
209 $this->elementStart('div', array('id'=>'avatar_original_view'));
210 $this->element('img', array('src' => $this->group->original_logo,
211 'alt' => $this->group->nickname));
212 $this->elementEnd('div');
213 $this->elementEnd('li');
216 if ($this->group->homepage_logo) {
217 $this->elementStart('li', array('id' => 'avatar_preview',
218 'class' => 'avatar_view'));
219 // TRANS: Header for preview of to be displayed group logo.
220 $this->element('h2', null, _("Preview"));
221 $this->elementStart('div', array('id'=>'avatar_preview_view'));
222 $this->element('img', array('src' => $this->group->homepage_logo,
223 'width' => AVATAR_PROFILE_SIZE,
224 'height' => AVATAR_PROFILE_SIZE,
225 'alt' => $this->group->nickname));
226 $this->elementEnd('div');
227 $this->elementEnd('li');
230 $this->elementStart('li', array ('id' => 'settings_attach'));
231 $this->element('input', array('name' => 'avatarfile',
233 'id' => 'avatarfile'));
234 $this->element('input', array('name' => 'MAX_FILE_SIZE',
236 'id' => 'MAX_FILE_SIZE',
237 'value' => ImageFile::maxFileSizeInt()));
238 $this->elementEnd('li');
239 $this->elementEnd('ul');
241 $this->elementStart('ul', 'form_actions');
242 $this->elementStart('li');
243 // TRANS: Submit button for uploading a group logo.
244 $this->submit('upload', _('Upload'));
245 $this->elementEnd('li');
246 $this->elementEnd('ul');
248 $this->elementEnd('fieldset');
249 $this->elementEnd('form');
253 function showCropForm()
255 $this->elementStart('form', array('method' => 'post',
256 'id' => 'form_settings_avatar',
257 'class' => 'form_settings',
259 common_local_url('grouplogo',
260 array('nickname' => $this->group->nickname))));
261 $this->elementStart('fieldset');
262 // TRANS: Legend for group logo settings fieldset.
263 $this->element('legend', null, _('Avatar settings'));
264 $this->hidden('token', common_session_token());
266 $this->elementStart('ul', 'form_data');
268 $this->elementStart('li',
269 array('id' => 'avatar_original',
270 'class' => 'avatar_view'));
271 // TRANS: Header for originally uploaded file before a crop on the group logo page.
272 $this->element('h2', null, _("Original"));
273 $this->elementStart('div', array('id'=>'avatar_original_view'));
274 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
275 'width' => $this->filedata['width'],
276 'height' => $this->filedata['height'],
277 'alt' => $this->group->nickname));
278 $this->elementEnd('div');
279 $this->elementEnd('li');
281 $this->elementStart('li',
282 array('id' => 'avatar_preview',
283 'class' => 'avatar_view'));
284 // TRANS: Header for the cropped group logo on the group logo page.
285 $this->element('h2', null, _("Preview"));
286 $this->elementStart('div', array('id'=>'avatar_preview_view'));
287 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
288 'width' => AVATAR_PROFILE_SIZE,
289 'height' => AVATAR_PROFILE_SIZE,
290 'alt' => $this->group->nickname));
291 $this->elementEnd('div');
293 foreach (array('avatar_crop_x', 'avatar_crop_y',
294 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
295 $this->element('input', array('name' => $crop_info,
297 'id' => $crop_info));
300 // TRANS: Button text for cropping an uploaded group logo.
301 $this->submit('crop', _('Crop'));
303 $this->elementEnd('li');
304 $this->elementEnd('ul');
305 $this->elementEnd('fieldset');
306 $this->elementEnd('form');
313 * We mux on the button name to figure out what the user actually wanted.
317 function handlePost()
321 $token = $this->trimmed('token');
322 if (!$token || $token != common_session_token()) {
323 // TRANS: Form validation error message.
324 $this->show_form(_('There was a problem with your session token. '.
325 'Try again, please.'));
329 if ($this->arg('upload')) {
331 } else if ($this->arg('crop')) {
334 // TRANS: Form validation error message when an unsupported argument is used.
335 $this->showForm(_('Unexpected form submission.'));
340 * Handle an image upload
342 * Does all the magic for handling an image upload, and crops the
347 function uploadLogo()
350 $imagefile = ImageFile::fromUpload('avatarfile');
351 } catch (Exception $e) {
352 $this->showForm($e->getMessage());
356 $type = $imagefile->preferredType();
357 $filename = Avatar::filename($this->group->id,
358 image_type_to_extension($type),
360 'group-temp-'.common_timestamp());
362 $filepath = Avatar::path($filename);
364 $imagefile->copyTo($filepath);
366 $filedata = array('filename' => $filename,
367 'filepath' => $filepath,
368 'width' => $imagefile->width,
369 'height' => $imagefile->height,
372 $_SESSION['FILEDATA'] = $filedata;
374 $this->filedata = $filedata;
376 $this->mode = 'crop';
378 // TRANS: Form instructions on the group logo page.
379 $this->showForm(_('Pick a square area of the image to be the logo.'),
384 * Handle the results of jcrop.
390 $filedata = $_SESSION['FILEDATA'];
393 // TRANS: Server error displayed trying to crop an uploaded group logo that is no longer present.
394 $this->serverError(_('Lost our file data.'));
398 // If image is not being cropped assume pos & dimentions of original
399 $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
400 $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
401 $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$filedata['width'];
402 $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height'];
403 $size = min($dest_w, $dest_h);
404 $size = ($size > MAX_ORIGINAL) ? MAX_ORIGINAL:$size;
406 $imagefile = new ImageFile($this->group->id, $filedata['filepath']);
407 $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
409 if ($this->group->setOriginal($filename)) {
410 @unlink($filedata['filepath']);
411 unset($_SESSION['FILEDATA']);
412 $this->mode = 'upload';
413 // TRANS: Form success message after updating a group logo.
414 $this->showForm(_('Logo updated.'), true);
416 // TRANS: Form failure message after failing to update a group logo.
417 $this->showForm(_('Failed updating logo.'));
421 function showPageNotice()
424 $this->element('div', ($this->success) ? 'success' : 'error',
427 $inst = $this->getInstructions();
428 $output = common_markup_to_html($inst);
430 $this->elementStart('div', 'instructions');
432 $this->elementEnd('div');
437 * Add the jCrop stylesheet
442 function showStylesheets()
444 parent::showStylesheets();
445 $this->cssLink('css/jquery.Jcrop.css','base','screen, projection, tv');
449 * Add the jCrop scripts
454 function showScripts()
456 parent::showScripts();
458 if ($this->mode == 'crop') {
459 $this->script('jcrop/jquery.Jcrop.min.js');
460 $this->script('jcrop/jquery.Jcrop.go.js');
463 $this->autofocus('avatarfile');
466 function showLocalNav()
468 $nav = new GroupNav($this, $this->group);