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/
53 class GrouplogoAction extends GroupDesignAction
56 var $imagefile = null;
65 function prepare($args)
67 parent::prepare($args);
69 if (!common_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 $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 $this->clientError(_('No such group.'), 404);
106 $cur = common_current_user();
108 if (!$cur->isAdmin($this->group)) {
109 $this->clientError(_('You must be an admin to edit the group.'), 403);
116 function handle($args)
118 parent::handle($args);
119 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
126 function showForm($msg = null, $success = false)
129 $this->success = $success;
137 * @return string Title of the page
142 return _('Group logo');
146 * Instructions for use
148 * @return instructions for use
151 function getInstructions()
153 return sprintf(_('You can upload a logo image for your group. The maximum file size is %s.'), ImageFile::maxFileSize());
157 * Content area of the page
159 * Shows a form for uploading an avatar.
164 function showContent()
166 if ($this->mode == 'crop') {
167 $this->showCropForm();
169 $this->showUploadForm();
173 function showUploadForm()
175 $user = common_current_user();
177 $profile = $user->getProfile();
180 common_log_db_error($user, 'SELECT', __FILE__);
181 $this->serverError(_('User without matching profile.'));
185 $original = $this->group->original_logo;
187 $this->elementStart('form', array('enctype' => 'multipart/form-data',
189 'id' => 'form_settings_avatar',
190 'class' => 'form_settings',
192 common_local_url('grouplogo',
193 array('nickname' => $this->group->nickname))));
194 $this->elementStart('fieldset');
195 $this->element('legend', null, _('Group logo'));
196 $this->hidden('token', common_session_token());
198 $this->elementStart('ul', 'form_data');
200 $this->elementStart('li', array('id' => 'avatar_original',
201 'class' => 'avatar_view'));
202 $this->element('h2', null, _("Original"));
203 $this->elementStart('div', array('id'=>'avatar_original_view'));
204 $this->element('img', array('src' => $this->group->original_logo,
205 'alt' => $this->group->nickname));
206 $this->elementEnd('div');
207 $this->elementEnd('li');
210 if ($this->group->homepage_logo) {
211 $this->elementStart('li', array('id' => 'avatar_preview',
212 'class' => 'avatar_view'));
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' => 'avatarfile',
226 'id' => 'avatarfile'));
227 $this->element('input', array('name' => 'MAX_FILE_SIZE',
229 'id' => 'MAX_FILE_SIZE',
230 'value' => ImageFile::maxFileSizeInt()));
231 $this->elementEnd('li');
232 $this->elementEnd('ul');
234 $this->elementStart('ul', 'form_actions');
235 $this->elementStart('li');
236 $this->submit('upload', _('Upload'));
237 $this->elementEnd('li');
238 $this->elementEnd('ul');
240 $this->elementEnd('fieldset');
241 $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 $this->element('legend', null, _('Avatar settings'));
255 $this->hidden('token', common_session_token());
257 $this->elementStart('ul', 'form_data');
259 $this->elementStart('li',
260 array('id' => 'avatar_original',
261 'class' => 'avatar_view'));
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 $this->element('h2', null, _("Preview"));
275 $this->elementStart('div', array('id'=>'avatar_preview_view'));
276 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
277 'width' => AVATAR_PROFILE_SIZE,
278 'height' => AVATAR_PROFILE_SIZE,
279 'alt' => $this->group->nickname));
280 $this->elementEnd('div');
282 foreach (array('avatar_crop_x', 'avatar_crop_y',
283 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
284 $this->element('input', array('name' => $crop_info,
286 'id' => $crop_info));
289 $this->submit('crop', _('Crop'));
291 $this->elementEnd('li');
292 $this->elementEnd('ul');
293 $this->elementEnd('fieldset');
294 $this->elementEnd('form');
301 * 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 $this->show_form(_('There was a problem with your session token. '.
313 'Try again, please.'));
317 if ($this->arg('upload')) {
319 } else if ($this->arg('crop')) {
322 $this->showForm(_('Unexpected form submission.'));
327 * Handle an image upload
329 * Does all the magic for handling an image upload, and crops the
335 function uploadLogo()
338 $imagefile = ImageFile::fromUpload('avatarfile');
339 } catch (Exception $e) {
340 $this->showForm($e->getMessage());
344 $filename = Avatar::filename($this->group->id,
345 image_type_to_extension($imagefile->type),
347 'group-temp-'.common_timestamp());
349 $filepath = Avatar::path($filename);
351 move_uploaded_file($imagefile->filepath, $filepath);
353 $filedata = array('filename' => $filename,
354 'filepath' => $filepath,
355 'width' => $imagefile->width,
356 'height' => $imagefile->height,
357 'type' => $imagefile->type);
359 $_SESSION['FILEDATA'] = $filedata;
361 $this->filedata = $filedata;
363 $this->mode = 'crop';
365 $this->showForm(_('Pick a square area of the image to be the logo.'),
370 * Handle the results of jcrop.
377 $filedata = $_SESSION['FILEDATA'];
380 $this->serverError(_('Lost our file data.'));
384 // If image is not being cropped assume pos & dimentions of original
385 $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
386 $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
387 $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$filedata['width'];
388 $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height'];
389 $size = min($dest_w, $dest_h);
390 $size = ($size > MAX_ORIGINAL) ? MAX_ORIGINAL:$size;
392 $imagefile = new ImageFile($this->group->id, $filedata['filepath']);
393 $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
395 if ($this->group->setOriginal($filename)) {
396 @unlink($filedata['filepath']);
397 unset($_SESSION['FILEDATA']);
398 $this->mode = 'upload';
399 $this->showForm(_('Logo updated.'), true);
401 $this->showForm(_('Failed updating logo.'));
405 function showPageNotice()
408 $this->element('div', ($this->success) ? 'success' : 'error',
411 $inst = $this->getInstructions();
412 $output = common_markup_to_html($inst);
414 $this->elementStart('div', 'instructions');
416 $this->elementEnd('div');
421 * Add the jCrop stylesheet
426 function showStylesheets()
428 parent::showStylesheets();
429 $this->cssLink('css/jquery.Jcrop.css','base','screen, projection, tv');
433 * Add the jCrop scripts
438 function showScripts()
440 parent::showScripts();
442 if ($this->mode == 'crop') {
443 $this->script('jcrop/jquery.Jcrop.min.js');
444 $this->script('jcrop/jquery.Jcrop.go.js');
447 $this->autofocus('avatarfile');
450 function showLocalNav()
452 $nav = new GroupNav($this, $this->group);