XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
AvatarEditAction.class.php
1<?php
3 * @package user
4 * @author Kazuhisa Minato aka minahito, Core developer
5 * @version $Id: AvatarEditAction.class.php,v 1.3 2007/08/23 13:04:21 minahito Exp $
6 */
7
8if (!defined('XOOPS_ROOT_PATH')) {
9 exit();
10}
11
12require_once XOOPS_ROOT_PATH . '/core/XCube_PageNavigator.class.php';
13
14require_once XOOPS_MODULE_PATH . '/user/class/AbstractEditAction.class.php';
15require_once XOOPS_MODULE_PATH . '/user/forms/AvatarEditForm.class.php';
16require_once XOOPS_MODULE_PATH . '/user/forms/AvatarSelectForm.class.php';
17require_once XOOPS_MODULE_PATH . '/user/forms/AvatarFilterForm.class.php';
18
19/***
20 * @internal
21 * This action handles the uploaded avatar image file.
22 *
23 * Users who are allowed to upload, can upload custom avatars and select system
24 * avatars. So this action has to implement both of uploading and selecting.
25 * In the case of GET request, this action shows two forms to changing a avatar
26 * for specified user. One of form shows upload-form. Anther form shows the list
27 * of system avatars and the page navigator of the list.
28 *
29 * @see User_AvatarEditForm
30 * @see User_AvatarSelectForm
31 */
33{
34 /***
35 * @var int
36 */
37 public $mAvatarWidth = 0;
38
39 /***
40 * @var int
41 */
42 public $mAvatarHeight = 0;
43
44 /***
45 * @var int
46 */
47 public $mAvatarMaxfilesize = 0;
48
49 /***
50 * @var int
51 */
52 public $_mMinPost = 0;
53
54 /***
55 * @var bool
56 */
57 public $_mAllowUpload = false;
58
59 /***
60 * @var User_AvatarFilterForm
61 */
62 public $mFilter;
63
64 /***
65 * Preset avatar object collection.
66 */
67 public $mSystemAvatars = [];
68
69 /***
70 * Other action form for AvatarSelect.
71 * @var User_AvatarSelectForm
72 */
73 public $mAvatarSelectForm = null;
74
75 /***
76 * Fetch conditions from $moduleConfig and set these to member properties.
77 * And, by the member property mConfig of the base class, any member
78 * functions of this class can access $moduleConfig.
79 *
80 * @param $controller
81 * @param $xoopsUser
82 * @param $moduleConfig
83 * @todo The limit may be not completed, yet.
84 */
85 public function prepare(&$controller, &$xoopsUser, $moduleConfig)
86 {
87 $this->mAvatarWidth = $moduleConfig['avatar_width'];
88 $this->mAvatarHeight = $moduleConfig['avatar_height'];
89 $this->mAvatarMaxfilesize = $moduleConfig['avatar_maxsize'];
90
91 $this->_mMinPost = $moduleConfig['avatar_minposts'];
92 $this->_mAllowUpload = $moduleConfig['avatar_allow_upload'];
93
94 parent::prepare($controller, $xoopsUser, $moduleConfig);
95 }
96
97 public function _getId()
98 {
99 return isset($_REQUEST['uid']) ? (int)xoops_getrequest('uid') : 0;
100 }
101
102 public function &_getHandler()
103 {
104 $handler =& xoops_getmodulehandler('users', 'user');
105 return $handler;
106 }
107
115 protected function _getPagetitle()
116 {
117 return Legacy_Utils::getUserName($this->_getId());
118 }
119
127 protected function _getPageAction()
128 {
129 return _MD_USER_LANG_AVATAR_EDIT;
130 }
131
132 /***
133 * This class uses AvatarUploadForm class. It requests three condition
134 * which are width limit, height limit and filesize limit.
135 *
136 * @todo We may have to hand three parameters to constructor.
137 */
138 public function _setupActionForm()
139 {
140 $this->mActionForm =new User_AvatarEditForm();
141 $this->mActionForm->prepare($this->mAvatarWidth, $this->mAvatarHeight, $this->mAvatarMaxfilesize);
142 }
143
144 public function isEnableCreate()
145 {
146 return false;
147 }
148
149 /***
150 * Return true. This action should not be used by a guest user.
151 */
152 public function isSecure()
153 {
154 return true;
155 }
156
157 /***
158 * Check whether a current user can access this action.
159 * 1) A specified user has to exist.
160 * 2) A current user has to equal the specified user, or a current user has
161 * to be a administrator.
162 * @param $controller
163 * @param $xoopsUser
164 * @param $moduleConfig
165 * @return bool
166 */
167 public function hasPermission(&$controller, &$xoopsUser, $moduleConfig)
168 {
169 if (!is_object($this->mObject)) {
170 return false;
171 }
172
173 if ($controller->mRoot->mContext->mUser->isInRole('Module.user.Admin')) {
174 return true;
175 } elseif ($this->mObject->get('uid') == $xoopsUser->get('uid')) {
176 // Check count of the system avatar.
177 $handler =& xoops_getmodulehandler('avatar', 'user');
178 $criteria =new Criteria('avatar_type', 'S');
179 if ($handler->getCount($criteria) > 0) {
180 return true;
181 }
182
183 return ($this->mObject->get('posts') >= $this->_mMinPost);
184 }
185
186 return false;
187 }
188
189 /***
190 * This override method looks like the same method of ListAction, and tries
191 * to get system avatars. After, it will call base class.
192 * @param $controller
193 * @param $xoopsUser
194 * @return int
195 */
196 public function getDefaultView(&$controller, &$xoopsUser)
197 {
198 $navi =new XCube_PageNavigator(XOOPS_URL . '/edituser.php?op=avatarform&amp;uid=' . $xoopsUser->get('uid'), XCUBE_PAGENAVI_START);
199 $handler =& xoops_getmodulehandler('avatar', 'user');
200
201 $this->mSystemAvatars[] =& $handler->createNoavatar();
202
203 $this->mFilter =new User_AvatarFilterForm($navi, $handler);
204 $this->mFilter->fetch();
205
206 $criteria = $this->mFilter->getCriteria();
207 $t_avatarArr =& $handler->getObjects($criteria);
208 foreach (array_keys($t_avatarArr) as $key) {
209 $this->mSystemAvatars[] =& $t_avatarArr[$key];
210 }
211
212 $this->mAvatarSelectForm =new User_AvatarSelectForm();
213 $this->mAvatarSelectForm->prepare();
214
215 $this->mAvatarSelectForm->load($this->mObject);
216
217 return parent::getDefaultView($controller, $xoopsUser);
218 }
219
220 public function execute(&$controller, &$xoopsUser)
221 {
222 if (null == $this->mObject) {
223 return USER_FRAME_VIEW_ERROR;
224 }
225
226 if ($this->_mMinPost > 0 && $this->mObject->get('posts') < $this->_mMinPost) {
227 return USER_FRAME_VIEW_ERROR;
228 }
229
230 $this->mActionForm->load($this->mObject);
231
232 $this->mActionForm->fetch();
233 $this->mActionForm->validate();
234
235 if ($this->mActionForm->hasError()) {
236 return $this->getDefaultView($controller, $xoopsUser);
237 }
238
239 $this->mActionForm->update($this->mObject);
240
241 return $this->_doExecute() ? USER_FRAME_VIEW_SUCCESS
242 : USER_FRAME_VIEW_ERROR;
243 }
244
245 /***
246 * 1) Save avatar file which has been uploaded.
247 * 2) If old avatar file exists, remove it.
248 * 3) Insert a data to DB with calling base class method.
249 */
250 public function _doExecute()
251 {
252 if (null != $this->mActionForm->mFormFile) {
253 if (!$this->mActionForm->mFormFile->saveAs(XOOPS_UPLOAD_PATH)) {
254 return false;
255 }
256 }
257
258 $this->_resize();
259
260 if (null != $this->mActionForm->mOldAvatarFilename && 'blank.gif' != $this->mActionForm->mOldAvatarFilename and null != $this->mActionForm->mFormFile) {
261 $avatarHandler =& xoops_getmodulehandler('avatar', 'user');
262 $criteria =new Criteria('avatar_file', $this->mActionForm->mOldAvatarFilename);
263 $avatarArr =& $avatarHandler->getObjects($criteria);
264 if ((is_countable($avatarArr) ? count($avatarArr) : 0) > 0 && is_object($avatarArr[0]) && 'C' == $avatarArr[0]->get('avatar_type')) {
265 $avatarHandler->delete($avatarArr[0]);
266 }
267 }
268
269 if (parent::_doExecute()) {
270 $avatar =& $this->mActionForm->createAvatar();
271 if (null != $avatar) {
272 $avatar->set('avatar_name', $this->mObject->get('uname'));
273 $avatarHandler =& xoops_getmodulehandler('avatar', 'user');
274 $avatarHandler->insert($avatar);
275
276 $linkHandler =& xoops_getmodulehandler('avatar_user_link', 'user');
277 $linkHandler->deleteAllByUser($this->mObject);
278
279 $link =& $linkHandler->create();
280 $link->set('user_id', $this->mObject->get('uid'));
281 $link->set('avatar_id', $avatar->get('avatar_id'));
282
283 $linkHandler->insert($link);
284 }
285
286 return true;
287 } else {
288 return false;
289 }
290 }
291
299 public function _resize()
300 {
301 $source = null;
302 //resize requires GD library
303 if (! function_exists('imagecreatefromjpeg') || ! function_exists('imagecreatefrompng') || ! function_exists('imagecreatefromgif')) {
304 return;
305 }
306
307 if (! $formFile = $this->mActionForm->mFormFile) {
308 return;
309 }
310 $filePath = XOOPS_UPLOAD_PATH.'/'.$formFile->getFileName();
311 [$width, $height, $type, $attr]=getimagesize($filePath);
312 switch ($type) {
313 case 2:
314 $source = imagecreatefromjpeg($filePath);
315 break;
316 case 1:
317 $source = imagecreatefromgif($filePath);
318 break;
319 case 3:
320 $source = imagecreatefrompng($filePath);
321 break;
322 }
323 $size = $this->_getResizedSize($width, $height, $this->mAvatarWidth, $this->mAvatarHeight);
324 $result = function_exists('imagecreatetruecolor') ? imagecreatetruecolor($size['width'], $size['height']) : imagecreate($size['width'], $size['height']);
325 if (!imagecopyresampled($result, $source, 0, 0, 0, 0, $size['width'], $size['height'], $width, $height)) {
326 die(); // TODO redirect message
327 }
328 switch ($type) {
329 case 2:
330 imagejpeg($result, $filePath);
331 break;
332 case 1:
333 imagegif($result, $filePath);
334 break;
335 case 3:
336 imagepng($result, $filePath);
337 break;
338 }
339 }
340
351 public function _getResizedSize(/*** int ***/ $width, /*** int ***/ $height, /*** int ***/ $maxWidth, /*** int ***/ $maxHeight)
352 {
353 if (min($width, $height, $maxWidth, $maxHeight) < 1) {
354 echo $width .'/'. $height .'/'. $maxWidth .'/'. $maxHeight;
355 die();
356 }
357 $scale = min($maxWidth / $width, $maxHeight / $height, 1);
358 return ['width' => (int)($width * $scale), 'height' => (int)($height * $scale)];
359 }
360
361 public function executeViewInput(&$controller, &$xoopsUser, &$render)
362 {
363 $render->setTemplateName('user_avatar_edit.html');
364 $render->setAttribute('actionForm', $this->mActionForm);
365 $render->setAttribute('thisUser', $this->mObject);
366
367 $render->setAttribute('allowUpload', $this->_mAllowUpload && $this->mObject->get('posts') >= $this->_mMinPost);
368 $render->setAttribute('avatarWidth', $this->mAvatarWidth);
369 $render->setAttribute('avatarHeight', $this->mAvatarHeight);
370 $render->setAttribute('avatarMaxfilesize', $this->mAvatarMaxfilesize);
371
372 $render->setAttribute('pageNavi', $this->mFilter->mNavi);
373 $render->setAttribute('systemAvatars', $this->mSystemAvatars);
374 $render->setAttribute('avatarSelectForm', $this->mAvatarSelectForm);
375 }
376
377 public function executeViewSuccess(&$controller, &$xoopsUser, &$render)
378 {
379 $controller->executeForward(XOOPS_URL . '/userinfo.php?uid=' . $this->mActionForm->get('uid'));
380 }
381
382 public function executeViewError(&$controller, &$xoopsUser, &$render)
383 {
384 $controller->executeRedirect(XOOPS_URL . '/userinfo.php?uid=' . $this->mActionForm->get('uid'), 1, _MD_USER_ERROR_DBUPDATE_FAILED);
385 }
386}
static getUserName( $uid)
_getResizedSize( $width, $height, $maxWidth, $maxHeight)