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 jCrop plugin for jQuery to crop the image after upload.
44 * @author Evan Prodromou <evan@controlyourself.ca>
45 * @author Zach Copley <zach@controlyourself.ca>
46 * @author Sarven Capadisli <csarven@controlyourself.ca>
47 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48 * @link http://laconi.ca/
51 class AvatarsettingsAction extends AccountSettingsAction
56 * @return string Title of the page
65 * Instructions for use
67 * @return instructions for use
70 function getInstructions()
72 return _('Set your personal avatar.');
76 * Content area of the page
78 * Shows a form for uploading an avatar.
83 function showContent()
85 $user = common_current_user();
87 $profile = $user->getProfile();
90 common_log_db_error($user, 'SELECT', __FILE__);
91 $this->serverError(_('User without matching profile'));
95 $original = $profile->getOriginalAvatar();
97 $this->elementStart('form', array('enctype' => 'multipart/form-data',
99 'id' => 'form_settings_avatar',
100 'class' => 'form_settings',
102 common_local_url('avatarsettings')));
103 $this->elementStart('fieldset');
104 $this->element('legend', null, _('Avatar settings'));
105 $this->hidden('token', common_session_token());
107 $this->elementStart('ul', 'form_data');
109 $this->elementStart('li',
110 array('id' => 'avatar_original',
111 'class' => 'avatar_view'));
112 $this->element('h2', null, _("Original"));
113 $this->elementStart('div', array('id'=>'avatar_original_view'));
114 $this->element('img', array('src' => $original->url,
115 'width' => $original->width,
116 'height' => $original->height,
117 'alt' => $user->nickname));
118 $this->elementEnd('div');
119 $this->elementEnd('li');
122 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
125 $this->elementStart('li',
126 array('id' => 'avatar_preview',
127 'class' => 'avatar_view'));
128 $this->element('h2', null, _("Preview"));
129 $this->elementStart('div', array('id'=>'avatar_preview_view'));
130 $this->element('img', array('src' => $original->url,//$avatar->url,
131 'width' => AVATAR_PROFILE_SIZE,
132 'height' => AVATAR_PROFILE_SIZE,
133 'alt' => $user->nickname));
134 $this->elementEnd('div');
136 foreach (array('avatar_crop_x', 'avatar_crop_y',
137 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
138 $this->element('input', array('name' => $crop_info,
140 'id' => $crop_info));
142 $this->submit('crop', _('Crop'));
144 $this->elementEnd('li');
147 $this->elementStart('li', array ('id' => 'settings_attach'));
148 $this->element('input', array('name' => 'avatarfile',
150 'id' => 'avatarfile'));
151 $this->element('input', array('name' => 'MAX_FILE_SIZE',
153 'id' => 'MAX_FILE_SIZE',
154 'value' => MAX_AVATAR_SIZE));
155 $this->elementEnd('li');
156 $this->elementEnd('ul');
158 $this->elementStart('ul', 'form_actions');
159 $this->elementStart('li');
160 $this->submit('upload', _('Upload'));
161 $this->elementEnd('li');
162 $this->elementEnd('ul');
164 $this->elementEnd('fieldset');
165 $this->elementEnd('form');
172 * We mux on the button name to figure out what the user actually wanted.
177 function handlePost()
181 $token = $this->trimmed('token');
182 if (!$token || $token != common_session_token()) {
183 $this->show_form(_('There was a problem with your session token. '.
184 'Try again, please.'));
188 if ($this->arg('upload')) {
189 $this->uploadAvatar();
190 } else if ($this->arg('crop')) {
193 $this->showForm(_('Unexpected form submission.'));
198 * Handle an image upload
200 * Does all the magic for handling an image upload, and crops the
206 function uploadAvatar()
209 $imagefile = ImageFile::fromUpload('avatarfile');
210 } catch (Exception $e) {
211 $this->showForm($e->getMessage());
215 $user = common_current_user();
217 $profile = $user->getProfile();
219 if ($profile->setOriginal($imagefile->filename)) {
220 $this->showForm(_('Avatar updated.'), true);
222 $this->showForm(_('Failed updating avatar.'));
225 $imagefile->unlink();
229 * Handle the results of jcrop.
234 function cropAvatar()
236 $user = common_current_user();
238 $profile = $user->getProfile();
240 $x = $this->arg('avatar_crop_x');
241 $y = $this->arg('avatar_crop_y');
242 $w = $this->arg('avatar_crop_w');
243 $h = $this->arg('avatar_crop_h');
245 if ($profile->crop_avatars($x, $y, $w, $h)) {
246 $this->showForm(_('Avatar updated.'), true);
248 $this->showForm(_('Failed updating avatar.'));
253 * Add the jCrop stylesheet
258 function showStylesheets()
260 parent::showStylesheets();
262 common_path('theme/base/css/jquery.Jcrop.css?version='.LACONICA_VERSION);
264 $this->element('link', array('rel' => 'stylesheet',
265 'type' => 'text/css',
266 'href' => $jcropStyle,
267 'media' => 'screen, projection, tv'));
271 * Add the jCrop scripts
276 function showScripts()
278 parent::showScripts();
280 $jcropPack = common_path('js/jcrop/jquery.Jcrop.pack.js');
281 $jcropGo = common_path('js/jcrop/jquery.Jcrop.go.js');
283 $this->element('script', array('type' => 'text/javascript',
284 'src' => $jcropPack));
285 $this->element('script', array('type' => 'text/javascript',