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('STATUSNET') && !defined('LACONICA')) {
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 GroupAction
55 var $imagefile = null;
63 function prepare($args)
65 parent::prepare($args);
67 if (!common_logged_in()) {
68 // TRANS: Client error displayed when trying to create a group while not logged in.
69 $this->clientError(_('You must be logged in to create a group.'));
73 $nickname_arg = $this->trimmed('nickname');
74 $nickname = common_canonical_nickname($nickname_arg);
76 // Permanent redirect on non-canonical nickname
78 if ($nickname_arg != $nickname) {
79 $args = array('nickname' => $nickname);
80 common_redirect(common_local_url('grouplogo', $args), 301);
85 // TRANS: Client error displayed when trying to change group logo settings without providing a nickname.
86 $this->clientError(_('No nickname.'), 404);
90 $groupid = $this->trimmed('groupid');
93 $this->group = User_group::staticGet('id', $groupid);
95 $local = Local_group::staticGet('nickname', $nickname);
97 $this->group = User_group::staticGet('id', $local->group_id);
102 // TRANS: Client error displayed when trying to update logo settings for a non-existing group.
103 $this->clientError(_('No such group.'), 404);
107 $cur = common_current_user();
109 if (!$cur->isAdmin($this->group)) {
110 // TRANS: Client error displayed when trying to change group logo settings while not being a group admin.
111 $this->clientError(_('You must be an admin to edit the group.'), 403);
118 function handle($args)
120 parent::handle($args);
121 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
128 function showForm($msg = null, $success = false)
131 $this->success = $success;
139 * @return string Title of the page
143 // TRANS: Title for group logo settings page.
144 return _('Group logo');
148 * Instructions for use
150 * @return instructions for use
152 function getInstructions()
154 // TRANS: Instructions for group logo page.
155 // TRANS: %s is the maximum file size for that site.
156 return sprintf(_('You can upload a logo image for your group. The maximum file size is %s.'), ImageFile::maxFileSize());
160 * Content area of the page
162 * Shows a form for uploading an avatar.
166 function showContent()
168 if ($this->mode == 'crop') {
169 $this->showCropForm();
171 $this->showUploadForm();
175 function showUploadForm()
177 $user = common_current_user();
179 $profile = $user->getProfile();
182 common_log_db_error($user, 'SELECT', __FILE__);
183 // TRANS: Error message displayed when referring to a user without a profile.
184 $this->serverError(_('User has no profile.'));
188 $original = $this->group->original_logo;
190 $this->elementStart('form', array('enctype' => 'multipart/form-data',
192 'id' => 'form_settings_avatar',
193 'class' => 'form_settings',
195 common_local_url('grouplogo',
196 array('nickname' => $this->group->nickname))));
197 $this->elementStart('fieldset');
198 // TRANS: Group logo form legend.
199 $this->element('legend', null, _('Group logo'));
200 $this->hidden('token', common_session_token());
202 $this->elementStart('ul', 'form_data');
204 $this->elementStart('li', array('id' => 'avatar_original',
205 'class' => 'avatar_view'));
206 // TRANS: Uploaded original file in group logo form.
207 $this->element('h2', null, _('Original'));
208 $this->elementStart('div', array('id'=>'avatar_original_view'));
209 $this->element('img', array('src' => $this->group->original_logo,
210 'alt' => $this->group->nickname));
211 $this->elementEnd('div');
212 $this->elementEnd('li');
215 if ($this->group->homepage_logo) {
216 $this->elementStart('li', array('id' => 'avatar_preview',
217 'class' => 'avatar_view'));
218 // TRANS: Header for preview of to be displayed group logo.
219 $this->element('h2', null, _('Preview'));
220 $this->elementStart('div', array('id'=>'avatar_preview_view'));
221 $this->element('img', array('src' => $this->group->homepage_logo,
222 'width' => AVATAR_PROFILE_SIZE,
223 'height' => AVATAR_PROFILE_SIZE,
224 'alt' => $this->group->nickname));
225 $this->elementEnd('div');
226 $this->elementEnd('li');
229 $this->elementStart('li', array ('id' => 'settings_attach'));
230 $this->element('input', array('name' => 'MAX_FILE_SIZE',
232 'id' => 'MAX_FILE_SIZE',
233 'value' => ImageFile::maxFileSizeInt()));
234 $this->element('input', array('name' => 'avatarfile',
236 'id' => 'avatarfile'));
237 $this->elementEnd('li');
238 $this->elementEnd('ul');
240 $this->elementStart('ul', 'form_actions');
241 $this->elementStart('li');
242 // TRANS: Submit button for uploading a group logo.
243 $this->submit('upload', _('Upload'));
244 $this->elementEnd('li');
245 $this->elementEnd('ul');
247 $this->elementEnd('fieldset');
248 $this->elementEnd('form');
251 function showCropForm()
253 $this->elementStart('form', array('method' => 'post',
254 'id' => 'form_settings_avatar',
255 'class' => 'form_settings',
257 common_local_url('grouplogo',
258 array('nickname' => $this->group->nickname))));
259 $this->elementStart('fieldset');
260 // TRANS: Legend for group logo settings fieldset.
261 $this->element('legend', null, _('Avatar settings'));
262 $this->hidden('token', common_session_token());
264 $this->elementStart('ul', 'form_data');
266 $this->elementStart('li',
267 array('id' => 'avatar_original',
268 'class' => 'avatar_view'));
269 // TRANS: Header for originally uploaded file before a crop on the group logo page.
270 $this->element('h2', null, _('Original'));
271 $this->elementStart('div', array('id'=>'avatar_original_view'));
272 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
273 'width' => $this->filedata['width'],
274 'height' => $this->filedata['height'],
275 'alt' => $this->group->nickname));
276 $this->elementEnd('div');
277 $this->elementEnd('li');
279 $this->elementStart('li',
280 array('id' => 'avatar_preview',
281 'class' => 'avatar_view'));
282 // TRANS: Header for the cropped group logo on the group logo page.
283 $this->element('h2', null, _('Preview'));
284 $this->elementStart('div', array('id'=>'avatar_preview_view'));
285 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
286 'width' => AVATAR_PROFILE_SIZE,
287 'height' => AVATAR_PROFILE_SIZE,
288 'alt' => $this->group->nickname));
289 $this->elementEnd('div');
291 foreach (array('avatar_crop_x', 'avatar_crop_y',
292 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
293 $this->element('input', array('name' => $crop_info,
295 'id' => $crop_info));
298 // TRANS: Button text for cropping an uploaded group logo.
299 $this->submit('crop', _('Crop'));
301 $this->elementEnd('li');
302 $this->elementEnd('ul');
303 $this->elementEnd('fieldset');
304 $this->elementEnd('form');
310 * We mux on the button name to figure out what the user actually wanted.
314 function handlePost()
318 $token = $this->trimmed('token');
319 if (!$token || $token != common_session_token()) {
320 // TRANS: Form validation error message.
321 $this->show_form(_('There was a problem with your session token. '.
322 'Try again, please.'));
326 if ($this->arg('upload')) {
328 } else if ($this->arg('crop')) {
331 // TRANS: Form validation error message when an unsupported argument is used.
332 $this->showForm(_('Unexpected form submission.'));
337 * Handle an image upload
339 * Does all the magic for handling an image upload, and crops the
344 function uploadLogo()
347 $imagefile = ImageFile::fromUpload('avatarfile');
348 } catch (Exception $e) {
349 $this->showForm($e->getMessage());
353 $type = $imagefile->preferredType();
354 $filename = Avatar::filename($this->group->id,
355 image_type_to_extension($type),
357 'group-temp-'.common_timestamp());
359 $filepath = Avatar::path($filename);
361 $imagefile->copyTo($filepath);
363 $filedata = array('filename' => $filename,
364 'filepath' => $filepath,
365 'width' => $imagefile->width,
366 'height' => $imagefile->height,
369 $_SESSION['FILEDATA'] = $filedata;
371 $this->filedata = $filedata;
373 $this->mode = 'crop';
375 // TRANS: Form instructions on the group logo page.
376 $this->showForm(_('Pick a square area of the image to be the logo.'),
381 * Handle the results of jcrop.
387 $filedata = $_SESSION['FILEDATA'];
390 // TRANS: Server error displayed trying to crop an uploaded group logo that is no longer present.
391 $this->serverError(_('Lost our file data.'));
395 // If image is not being cropped assume pos & dimentions of original
396 $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
397 $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
398 $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$filedata['width'];
399 $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height'];
400 $size = min($dest_w, $dest_h);
401 $size = ($size > MAX_ORIGINAL) ? MAX_ORIGINAL:$size;
403 $imagefile = new ImageFile($this->group->id, $filedata['filepath']);
404 $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
406 if ($this->group->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.'));
418 function showPageNotice()
421 $this->element('div', ($this->success) ? 'success' : 'error',
424 $inst = $this->getInstructions();
425 $output = common_markup_to_html($inst);
427 $this->elementStart('div', 'instructions');
429 $this->elementEnd('div');
434 * Add the jCrop stylesheet
438 function showStylesheets()
440 parent::showStylesheets();
441 $this->cssLink('css/jquery.Jcrop.css','base','screen, projection, tv');
445 * Add the jCrop scripts
449 function showScripts()
451 parent::showScripts();
453 if ($this->mode == 'crop') {
454 $this->script('jcrop/jquery.Jcrop.min.js');
455 $this->script('jcrop/jquery.Jcrop.go.js');
458 $this->autofocus('avatarfile');