3 * Laconica, 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@controlyourself.ca>
25 * @author Zach Copley <zach@controlyourself.ca>
26 * @copyright 2008-2009 Control Yourself, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://laconi.ca/
31 if (!defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
40 * We use jQuery to crop the image after upload.
44 * @author Evan Prodromou <evan@controlyourself.ca>
45 * @author Zach Copley <zach@controlyourself.ca>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://laconi.ca/
50 class AvatarsettingsAction extends AccountSettingsAction
55 * @return string Title of the page
64 * Instructions for use
66 * @return instructions for use
69 function getInstructions()
71 return _('Set your personal avatar.');
75 * Content area of the page
77 * Shows a form for uploading an avatar.
82 function showContent()
84 $user = common_current_user();
86 $profile = $user->getProfile();
89 common_log_db_error($user, 'SELECT', __FILE__);
90 $this->serverError(_('User without matching profile'));
94 $original = $profile->getOriginalAvatar();
96 $this->elementStart('form', array('enctype' => 'multipart/form-data',
100 common_local_url('avatarsettings')));
101 $this->hidden('token', common_session_token());
104 $this->elementStart('div',
105 array('id' => 'avatar_original',
106 'class' => 'avatar_view'));
107 $this->element('h3', null, _("Original:"));
108 $this->elementStart('div', array('id'=>'avatar_original_view'));
109 $this->element('img', array('src' => $original->url,
110 'class' => 'avatar original',
111 'width' => $original->width,
112 'height' => $original->height,
113 'alt' => $user->nickname));
114 $this->elementEnd('div');
115 $this->elementEnd('div');
118 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
121 $this->elementStart('div',
122 array('id' => 'avatar_preview',
123 'class' => 'avatar_view'));
124 $this->element('h3', null, _("Preview:"));
125 $this->elementStart('div', array('id'=>'avatar_preview_view'));
126 $this->element('img', array('src' => $original->url,//$avatar->url,
127 'class' => 'avatar profile',
128 'width' => AVATAR_PROFILE_SIZE,
129 'height' => AVATAR_PROFILE_SIZE,
130 'alt' => $user->nickname));
131 $this->elementEnd('div');
132 $this->elementEnd('div');
134 foreach (array('avatar_crop_x', 'avatar_crop_y',
135 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
136 $this->element('input', array('name' => $crop_info,
138 'id' => $crop_info));
140 $this->submit('crop', _('Crop'));
143 $this->element('input', array('name' => 'MAX_FILE_SIZE',
145 'id' => 'MAX_FILE_SIZE',
146 'value' => MAX_AVATAR_SIZE));
148 $this->elementStart('p');
150 $this->element('input', array('name' => 'avatarfile',
152 'id' => 'avatarfile'));
153 $this->elementEnd('p');
155 $this->submit('upload', _('Upload'));
156 $this->elementEnd('form');
163 * We mux on the button name to figure out what the user actually wanted.
168 function handlePost()
172 $token = $this->trimmed('token');
173 if (!$token || $token != common_session_token()) {
174 $this->show_form(_('There was a problem with your session token. '.
175 'Try again, please.'));
179 if ($this->arg('upload')) {
180 $this->uploadAvatar();
181 } else if ($this->arg('crop')) {
184 $this->showForm(_('Unexpected form submission.'));
189 * Handle an image upload
191 * Does all the magic for handling an image upload, and crops the
197 function uploadAvatar()
200 $imagefile = ImageFile::fromUpload('avatarfile');
201 } catch (Exception $e) {
202 $this->showForm($e->getMessage());
206 $user = common_current_user();
208 $profile = $user->getProfile();
210 if ($profile->setOriginal($imagefile->filename)) {
211 $this->showForm(_('Avatar updated.'), true);
213 $this->showForm(_('Failed updating avatar.'));
216 $imagefile->unlink();
220 * Handle the results of jcrop.
225 function cropAvatar()
227 $user = common_current_user();
229 $profile = $user->getProfile();
231 $x = $this->arg('avatar_crop_x');
232 $y = $this->arg('avatar_crop_y');
233 $w = $this->arg('avatar_crop_w');
234 $h = $this->arg('avatar_crop_h');
236 if ($profile->crop_avatars($x, $y, $w, $h)) {
237 $this->showForm(_('Avatar updated.'), true);
239 $this->showForm(_('Failed updating avatar.'));
244 * Add the jCrop stylesheet
249 function showStylesheets()
251 parent::showStylesheets();
253 common_path('js/jcrop/jquery.Jcrop.css?version='.LACONICA_VERSION);
255 $this->element('link', array('rel' => 'stylesheet',
256 'type' => 'text/css',
257 'href' => $jcropStyle,
258 'media' => 'screen, projection, tv'));
262 * Add the jCrop scripts
267 function showScripts()
269 parent::showScripts();
271 $jcropPack = common_path('js/jcrop/jquery.Jcrop.pack.js');
272 $jcropGo = common_path('js/jcrop/jquery.Jcrop.go.js');
274 $this->element('script', array('type' => 'text/javascript',
275 'src' => $jcropPack));
276 $this->element('script', array('type' => 'text/javascript',