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 $this->group = User_group::staticGet('nickname', $nickname);
99 $this->clientError(_('No such group'), 404);
103 $cur = common_current_user();
105 if (!$cur->isAdmin($this->group)) {
106 $this->clientError(_('You must be an admin to edit the group'), 403);
113 function handle($args)
115 parent::handle($args);
116 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
123 function showForm($msg = null, $success = false)
126 $this->success = $success;
134 * @return string Title of the page
139 return _('Group logo');
143 * Instructions for use
145 * @return instructions for use
148 function getInstructions()
150 return sprintf(_('You can upload a logo image for your group. The maximum file size is %s.'), ImageFile::maxFileSize());
154 * Content area of the page
156 * 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 $this->serverError(_('User without matching 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 $this->element('legend', null, _('Group logo'));
193 $this->hidden('token', common_session_token());
195 $this->elementStart('ul', 'form_data');
197 $this->elementStart('li', array('id' => 'avatar_original',
198 'class' => 'avatar_view'));
199 $this->element('h2', null, _("Original"));
200 $this->elementStart('div', array('id'=>'avatar_original_view'));
201 $this->element('img', array('src' => $this->group->original_logo,
202 'alt' => $this->group->nickname));
203 $this->elementEnd('div');
204 $this->elementEnd('li');
207 if ($this->group->homepage_logo) {
208 $this->elementStart('li', array('id' => 'avatar_preview',
209 'class' => 'avatar_view'));
210 $this->element('h2', null, _("Preview"));
211 $this->elementStart('div', array('id'=>'avatar_preview_view'));
212 $this->element('img', array('src' => $this->group->homepage_logo,
213 'width' => AVATAR_PROFILE_SIZE,
214 'height' => AVATAR_PROFILE_SIZE,
215 'alt' => $this->group->nickname));
216 $this->elementEnd('div');
217 $this->elementEnd('li');
220 $this->elementStart('li', array ('id' => 'settings_attach'));
221 $this->element('input', array('name' => 'avatarfile',
223 'id' => 'avatarfile'));
224 $this->element('input', array('name' => 'MAX_FILE_SIZE',
226 'id' => 'MAX_FILE_SIZE',
227 'value' => ImageFile::maxFileSizeInt()));
228 $this->elementEnd('li');
229 $this->elementEnd('ul');
231 $this->elementStart('ul', 'form_actions');
232 $this->elementStart('li');
233 $this->submit('upload', _('Upload'));
234 $this->elementEnd('li');
235 $this->elementEnd('ul');
237 $this->elementEnd('fieldset');
238 $this->elementEnd('form');
242 function showCropForm()
244 $this->elementStart('form', array('method' => 'post',
245 'id' => 'form_settings_avatar',
246 'class' => 'form_settings',
248 common_local_url('grouplogo',
249 array('nickname' => $this->group->nickname))));
250 $this->elementStart('fieldset');
251 $this->element('legend', null, _('Avatar settings'));
252 $this->hidden('token', common_session_token());
254 $this->elementStart('ul', 'form_data');
256 $this->elementStart('li',
257 array('id' => 'avatar_original',
258 'class' => 'avatar_view'));
259 $this->element('h2', null, _("Original"));
260 $this->elementStart('div', array('id'=>'avatar_original_view'));
261 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
262 'width' => $this->filedata['width'],
263 'height' => $this->filedata['height'],
264 'alt' => $this->group->nickname));
265 $this->elementEnd('div');
266 $this->elementEnd('li');
268 $this->elementStart('li',
269 array('id' => 'avatar_preview',
270 'class' => 'avatar_view'));
271 $this->element('h2', null, _("Preview"));
272 $this->elementStart('div', array('id'=>'avatar_preview_view'));
273 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
274 'width' => AVATAR_PROFILE_SIZE,
275 'height' => AVATAR_PROFILE_SIZE,
276 'alt' => $this->group->nickname));
277 $this->elementEnd('div');
279 foreach (array('avatar_crop_x', 'avatar_crop_y',
280 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
281 $this->element('input', array('name' => $crop_info,
283 'id' => $crop_info));
286 $this->submit('crop', _('Crop'));
288 $this->elementEnd('li');
289 $this->elementEnd('ul');
290 $this->elementEnd('fieldset');
291 $this->elementEnd('form');
298 * We mux on the button name to figure out what the user actually wanted.
303 function handlePost()
307 $token = $this->trimmed('token');
308 if (!$token || $token != common_session_token()) {
309 $this->show_form(_('There was a problem with your session token. '.
310 'Try again, please.'));
314 if ($this->arg('upload')) {
316 } else if ($this->arg('crop')) {
319 $this->showForm(_('Unexpected form submission.'));
324 * Handle an image upload
326 * Does all the magic for handling an image upload, and crops the
332 function uploadLogo()
335 $imagefile = ImageFile::fromUpload('avatarfile');
336 } catch (Exception $e) {
337 $this->showForm($e->getMessage());
341 $filename = Avatar::filename($this->group->id,
342 image_type_to_extension($imagefile->type),
344 'group-temp-'.common_timestamp());
346 $filepath = Avatar::path($filename);
348 move_uploaded_file($imagefile->filepath, $filepath);
350 $filedata = array('filename' => $filename,
351 'filepath' => $filepath,
352 'width' => $imagefile->width,
353 'height' => $imagefile->height,
354 'type' => $imagefile->type);
356 $_SESSION['FILEDATA'] = $filedata;
358 $this->filedata = $filedata;
360 $this->mode = 'crop';
362 $this->showForm(_('Pick a square area of the image to be the logo.'),
367 * Handle the results of jcrop.
374 $filedata = $_SESSION['FILEDATA'];
377 $this->serverError(_('Lost our file data.'));
381 // If image is not being cropped assume pos & dimentions of original
382 $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
383 $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
384 $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$filedata['width'];
385 $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height'];
386 $size = min($dest_w, $dest_h);
387 $size = ($size > MAX_ORIGINAL) ? MAX_ORIGINAL:$size;
389 $imagefile = new ImageFile($this->group->id, $filedata['filepath']);
390 $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
392 if ($this->group->setOriginal($filename)) {
393 @unlink($filedata['filepath']);
394 unset($_SESSION['FILEDATA']);
395 $this->mode = 'upload';
396 $this->showForm(_('Logo updated.'), true);
398 $this->showForm(_('Failed updating logo.'));
402 function showPageNotice()
405 $this->element('div', ($this->success) ? 'success' : 'error',
408 $inst = $this->getInstructions();
409 $output = common_markup_to_html($inst);
411 $this->elementStart('div', 'instructions');
413 $this->elementEnd('div');
418 * Add the jCrop stylesheet
423 function showStylesheets()
425 parent::showStylesheets();
426 $this->cssLink('css/jquery.Jcrop.css','base','screen, projection, tv');
430 * Add the jCrop scripts
435 function showScripts()
437 parent::showScripts();
439 if ($this->mode == 'crop') {
440 $this->script('js/jcrop/jquery.Jcrop.min.js');
441 $this->script('js/jcrop/jquery.Jcrop.go.js');
444 $this->autofocus('avatarfile');
447 function showLocalNav()
449 $nav = new GroupNav($this, $this->group);