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 if (!empty($this->group->homepage_logo)) {
215 // TRANS: Button on group logo upload page to delete current group logo.
216 $this->submit('delete', _('Delete'));
218 $this->elementEnd('li');
221 $this->elementStart('li', array ('id' => 'settings_attach'));
222 $this->element('input', array('name' => 'MAX_FILE_SIZE',
224 'id' => 'MAX_FILE_SIZE',
225 'value' => ImageFile::maxFileSizeInt()));
226 $this->element('input', array('name' => 'avatarfile',
228 'id' => 'avatarfile'));
229 $this->elementEnd('li');
230 $this->elementEnd('ul');
232 $this->elementStart('ul', 'form_actions');
233 $this->elementStart('li');
234 // TRANS: Submit button for uploading a group logo.
235 $this->submit('upload', _('Upload'));
236 $this->elementEnd('li');
237 $this->elementEnd('ul');
239 $this->elementEnd('fieldset');
240 $this->elementEnd('form');
243 function showCropForm()
245 $this->elementStart('form', array('method' => 'post',
246 'id' => 'form_settings_avatar',
247 'class' => 'form_settings',
249 common_local_url('grouplogo',
250 array('nickname' => $this->group->nickname))));
251 $this->elementStart('fieldset');
252 // TRANS: Legend for group logo settings fieldset.
253 $this->element('legend', null, _('Avatar settings'));
254 $this->hidden('token', common_session_token());
256 $this->elementStart('ul', 'form_data');
258 $this->elementStart('li',
259 array('id' => 'avatar_original',
260 'class' => 'avatar_view'));
261 // TRANS: Header for originally uploaded file before a crop on the group logo page.
262 $this->element('h2', null, _('Original'));
263 $this->elementStart('div', array('id'=>'avatar_original_view'));
264 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
265 'width' => $this->filedata['width'],
266 'height' => $this->filedata['height'],
267 'alt' => $this->group->nickname));
268 $this->elementEnd('div');
269 $this->elementEnd('li');
271 $this->elementStart('li',
272 array('id' => 'avatar_preview',
273 'class' => 'avatar_view'));
274 // TRANS: Header for the cropped group logo on the group logo page.
275 $this->element('h2', null, _('Preview'));
276 $this->elementStart('div', array('id'=>'avatar_preview_view'));
277 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
278 'width' => AVATAR_PROFILE_SIZE,
279 'height' => AVATAR_PROFILE_SIZE,
280 'alt' => $this->group->nickname));
281 $this->elementEnd('div');
283 foreach (array('avatar_crop_x', 'avatar_crop_y',
284 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
285 $this->element('input', array('name' => $crop_info,
287 'id' => $crop_info));
290 // TRANS: Button text for cropping an uploaded group logo.
291 $this->submit('crop', _('Crop'));
293 $this->elementEnd('li');
294 $this->elementEnd('ul');
295 $this->elementEnd('fieldset');
296 $this->elementEnd('form');
302 * We mux on the button name to figure out what the user actually wanted.
306 function handlePost()
310 $token = $this->trimmed('token');
311 if (!$token || $token != common_session_token()) {
312 // TRANS: Form validation error message.
313 $this->show_form(_('There was a problem with your session token. '.
314 'Try again, please.'));
318 if ($this->arg('upload')) {
320 } else if ($this->arg('crop')) {
322 } else if ($this->arg('delete')) {
325 // TRANS: Form validation error message when an unsupported argument is used.
326 $this->showForm(_('Unexpected form submission.'));
331 * Handle an image upload
333 * Does all the magic for handling an image upload, and crops the
338 function uploadLogo()
341 $imagefile = ImageFile::fromUpload('avatarfile');
342 } catch (Exception $e) {
343 $this->showForm($e->getMessage());
347 $type = $imagefile->preferredType();
348 $filename = Avatar::filename($this->group->id,
349 image_type_to_extension($type),
351 'group-temp-'.common_timestamp());
353 $filepath = Avatar::path($filename);
355 $imagefile->copyTo($filepath);
357 $filedata = array('filename' => $filename,
358 'filepath' => $filepath,
359 'width' => $imagefile->width,
360 'height' => $imagefile->height,
363 $_SESSION['FILEDATA'] = $filedata;
365 $this->filedata = $filedata;
367 $this->mode = 'crop';
369 // TRANS: Form instructions on the group logo page.
370 $this->showForm(_('Pick a square area of the image to be the logo.'),
375 * Handle the results of jcrop.
381 $filedata = $_SESSION['FILEDATA'];
384 // TRANS: Server error displayed trying to crop an uploaded group logo that is no longer present.
385 $this->serverError(_('Lost our file data.'));
388 // If image is not being cropped assume pos & dimentions of original
389 $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
390 $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
391 $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$filedata['width'];
392 $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height'];
393 $size = min($dest_w, $dest_h, common_config('avatar', 'maxsize'));
394 $box = array('width' => $size, 'height' => $size,
395 'x' => $dest_x, 'y' => $dest_y,
396 'w' => $dest_w, 'h' => $dest_h);
398 $profile = $this->group->getProfile();
400 $imagefile = new ImageFile(null, $filedata['filepath']);
401 $filename = Avatar::filename($profile->getID(), image_type_to_extension($imagefile->preferredType()),
402 $size, common_timestamp());
404 $imagefile->resizeTo(Avatar::path($filename), $box);
406 if ($profile->setOriginal($filename)) {
407 @unlink($filedata['filepath']);
408 unset($_SESSION['FILEDATA']);
409 $this->mode = 'upload';
410 // TRANS: Form success message after updating a group logo.
411 $this->showForm(_('Logo updated.'), true);
413 // TRANS: Form failure message after failing to update a group logo.
414 $this->showForm(_('Failed updating logo.'));
419 * Get rid of the current group logo.
423 function deleteLogo()
425 $orig = clone($this->group);
426 Avatar::deleteFromProfile($this->group->getProfile());
427 @unlink(Avatar::path(basename($this->group->original_logo)));
428 @unlink(Avatar::path(basename($this->group->homepage_logo)));
429 @unlink(Avatar::path(basename($this->group->stream_logo)));
430 @unlink(Avatar::path(basename($this->group->mini_logo)));
431 $this->group->original_logo=User_group::defaultLogo(AVATAR_PROFILE_SIZE);
432 $this->group->homepage_logo=User_group::defaultLogo(AVATAR_PROFILE_SIZE);
433 $this->group->stream_logo=User_group::defaultLogo(AVATAR_STREAM_SIZE);
434 $this->group->mini_logo=User_group::defaultLogo(AVATAR_MINI_SIZE);
435 $this->group->update($orig);
437 // TRANS: Success message for deleting the group logo.
438 $this->showForm(_('Logo deleted.'));
441 function showPageNotice()
444 $this->element('div', ($this->success) ? 'success' : 'error',
447 $inst = $this->getInstructions();
448 $output = common_markup_to_html($inst);
450 $this->elementStart('div', 'instructions');
452 $this->elementEnd('div');
457 * Add the jCrop stylesheet
461 function showStylesheets()
463 parent::showStylesheets();
464 $this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css','base','screen, projection, tv');
468 * Add the jCrop scripts
472 function showScripts()
474 parent::showScripts();
476 if ($this->mode == 'crop') {
477 $this->script('extlib/jquery-jcrop/jcrop.js');
478 $this->script('jcrop.go.js');
481 $this->autofocus('avatarfile');