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 protected function prepare(array $args=array())
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.'));
72 $nickname_arg = $this->trimmed('nickname');
73 $nickname = common_canonical_nickname($nickname_arg);
75 // Permanent redirect on non-canonical nickname
77 if ($nickname_arg != $nickname) {
78 $args = array('nickname' => $nickname);
79 common_redirect(common_local_url('grouplogo', $args), 301);
83 // TRANS: Client error displayed when trying to change group logo settings without providing a nickname.
84 $this->clientError(_('No nickname.'), 404);
87 $groupid = $this->trimmed('groupid');
90 $this->group = User_group::getKV('id', $groupid);
92 $local = Local_group::getKV('nickname', $nickname);
94 $this->group = User_group::getKV('id', $local->group_id);
99 // TRANS: Client error displayed when trying to update logo settings for a non-existing group.
100 $this->clientError(_('No such group.'), 404);
103 $cur = common_current_user();
105 if (!$cur->isAdmin($this->group)) {
106 // TRANS: Client error displayed when trying to change group logo settings while not being a group admin.
107 $this->clientError(_('You must be an admin to edit the group.'), 403);
113 protected function handle()
116 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
123 function showForm($msg = null, $success = false)
126 $this->success = $success;
134 * @return string Title of the page
138 // TRANS: Title for group logo settings page.
139 return _('Group logo');
143 * Instructions for use
145 * @return instructions for use
147 function getInstructions()
149 // TRANS: Instructions for group logo page.
150 // TRANS: %s is the maximum file size for that site.
151 return sprintf(_('You can upload a logo image for your group. The maximum file size is %s.'), ImageFile::maxFileSize());
155 * Content area of the page
157 * Shows a form for uploading an avatar.
161 function showContent()
163 if ($this->mode == 'crop') {
164 $this->showCropForm();
166 $this->showUploadForm();
170 function showUploadForm()
172 $user = common_current_user();
174 $profile = $user->getProfile();
177 common_log_db_error($user, 'SELECT', __FILE__);
178 // TRANS: Error message displayed when referring to a user without a profile.
179 $this->serverError(_('User has no profile.'));
182 $original = $this->group->original_logo;
184 $this->elementStart('form', array('enctype' => 'multipart/form-data',
186 'id' => 'form_settings_avatar',
187 'class' => 'form_settings',
189 common_local_url('grouplogo',
190 array('nickname' => $this->group->nickname))));
191 $this->elementStart('fieldset');
192 // TRANS: Group logo form legend.
193 $this->element('legend', null, _('Group logo'));
194 $this->hidden('token', common_session_token());
196 $this->elementStart('ul', 'form_data');
198 $this->elementStart('li', array('id' => 'avatar_original',
199 'class' => 'avatar_view'));
200 // TRANS: Uploaded original file in group logo form.
201 $this->element('h2', null, _('Original'));
202 $this->elementStart('div', array('id'=>'avatar_original_view'));
203 $this->element('img', array('src' => $this->group->original_logo,
204 'alt' => $this->group->nickname));
205 $this->elementEnd('div');
206 $this->elementEnd('li');
209 if ($this->group->homepage_logo) {
210 $this->elementStart('li', array('id' => 'avatar_preview',
211 'class' => 'avatar_view'));
212 // TRANS: Header for preview of to be displayed group logo.
213 $this->element('h2', null, _('Preview'));
214 $this->elementStart('div', array('id'=>'avatar_preview_view'));
215 $this->element('img', array('src' => $this->group->homepage_logo,
216 'width' => AVATAR_PROFILE_SIZE,
217 'height' => AVATAR_PROFILE_SIZE,
218 'alt' => $this->group->nickname));
219 $this->elementEnd('div');
220 $this->elementEnd('li');
223 $this->elementStart('li', array ('id' => 'settings_attach'));
224 $this->element('input', array('name' => 'MAX_FILE_SIZE',
226 'id' => 'MAX_FILE_SIZE',
227 'value' => ImageFile::maxFileSizeInt()));
228 $this->element('input', array('name' => 'avatarfile',
230 'id' => 'avatarfile'));
231 $this->elementEnd('li');
232 $this->elementEnd('ul');
234 $this->elementStart('ul', 'form_actions');
235 $this->elementStart('li');
236 // TRANS: Submit button for uploading a group logo.
237 $this->submit('upload', _('Upload'));
238 $this->elementEnd('li');
239 $this->elementEnd('ul');
241 $this->elementEnd('fieldset');
242 $this->elementEnd('form');
245 function showCropForm()
247 $this->elementStart('form', array('method' => 'post',
248 'id' => 'form_settings_avatar',
249 'class' => 'form_settings',
251 common_local_url('grouplogo',
252 array('nickname' => $this->group->nickname))));
253 $this->elementStart('fieldset');
254 // TRANS: Legend for group logo settings fieldset.
255 $this->element('legend', null, _('Avatar settings'));
256 $this->hidden('token', common_session_token());
258 $this->elementStart('ul', 'form_data');
260 $this->elementStart('li',
261 array('id' => 'avatar_original',
262 'class' => 'avatar_view'));
263 // TRANS: Header for originally uploaded file before a crop on the group logo page.
264 $this->element('h2', null, _('Original'));
265 $this->elementStart('div', array('id'=>'avatar_original_view'));
266 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
267 'width' => $this->filedata['width'],
268 'height' => $this->filedata['height'],
269 'alt' => $this->group->nickname));
270 $this->elementEnd('div');
271 $this->elementEnd('li');
273 $this->elementStart('li',
274 array('id' => 'avatar_preview',
275 'class' => 'avatar_view'));
276 // TRANS: Header for the cropped group logo on the group logo page.
277 $this->element('h2', null, _('Preview'));
278 $this->elementStart('div', array('id'=>'avatar_preview_view'));
279 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
280 'width' => AVATAR_PROFILE_SIZE,
281 'height' => AVATAR_PROFILE_SIZE,
282 'alt' => $this->group->nickname));
283 $this->elementEnd('div');
285 foreach (array('avatar_crop_x', 'avatar_crop_y',
286 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
287 $this->element('input', array('name' => $crop_info,
289 'id' => $crop_info));
292 // TRANS: Button text for cropping an uploaded group logo.
293 $this->submit('crop', _('Crop'));
295 $this->elementEnd('li');
296 $this->elementEnd('ul');
297 $this->elementEnd('fieldset');
298 $this->elementEnd('form');
304 * We mux on the button name to figure out what the user actually wanted.
308 function handlePost()
312 $token = $this->trimmed('token');
313 if (!$token || $token != common_session_token()) {
314 // TRANS: Form validation error message.
315 $this->show_form(_('There was a problem with your session token. '.
316 'Try again, please.'));
320 if ($this->arg('upload')) {
322 } else if ($this->arg('crop')) {
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);
394 $size = ($size > MAX_ORIGINAL) ? MAX_ORIGINAL:$size;
396 $imagefile = new ImageFile($this->group->id, $filedata['filepath']);
397 $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
399 if ($this->group->setOriginal($filename)) {
400 @unlink($filedata['filepath']);
401 unset($_SESSION['FILEDATA']);
402 $this->mode = 'upload';
403 // TRANS: Form success message after updating a group logo.
404 $this->showForm(_('Logo updated.'), true);
406 // TRANS: Form failure message after failing to update a group logo.
407 $this->showForm(_('Failed updating logo.'));
411 function showPageNotice()
414 $this->element('div', ($this->success) ? 'success' : 'error',
417 $inst = $this->getInstructions();
418 $output = common_markup_to_html($inst);
420 $this->elementStart('div', 'instructions');
422 $this->elementEnd('div');
427 * Add the jCrop stylesheet
431 function showStylesheets()
433 parent::showStylesheets();
434 $this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css','base','screen, projection, tv');
438 * Add the jCrop scripts
442 function showScripts()
444 parent::showScripts();
446 if ($this->mode == 'crop') {
447 $this->script('extlib/jquery-jcrop/jcrop.js');
448 $this->script('jcrop.go.js');
451 $this->autofocus('avatarfile');