37 public $mAvatarWidth = 0;
42 public $mAvatarHeight = 0;
47 public $mAvatarMaxfilesize = 0;
52 public $_mMinPost = 0;
57 public $_mAllowUpload =
false;
67 public $mSystemAvatars = [];
73 public $mAvatarSelectForm =
null;
85 public function prepare(&$controller, &$xoopsUser, $moduleConfig)
87 $this->mAvatarWidth = $moduleConfig[
'avatar_width'];
88 $this->mAvatarHeight = $moduleConfig[
'avatar_height'];
89 $this->mAvatarMaxfilesize = $moduleConfig[
'avatar_maxsize'];
91 $this->_mMinPost = $moduleConfig[
'avatar_minposts'];
92 $this->_mAllowUpload = $moduleConfig[
'avatar_allow_upload'];
94 parent::prepare($controller, $xoopsUser, $moduleConfig);
99 return isset($_REQUEST[
'uid']) ? (int)xoops_getrequest(
'uid') : 0;
104 $handler =& xoops_getmodulehandler(
'users',
'user');
129 return _MD_USER_LANG_AVATAR_EDIT;
141 $this->mActionForm->prepare($this->mAvatarWidth, $this->mAvatarHeight, $this->mAvatarMaxfilesize);
152 public function isSecure()
167 public function hasPermission(&$controller, &$xoopsUser, $moduleConfig)
169 if (!is_object($this->mObject)) {
173 if ($controller->mRoot->mContext->mUser->isInRole(
'Module.user.Admin')) {
175 } elseif ($this->mObject->get(
'uid') == $xoopsUser->get(
'uid')) {
177 $handler =& xoops_getmodulehandler(
'avatar',
'user');
178 $criteria =
new Criteria(
'avatar_type',
'S');
179 if ($handler->getCount($criteria) > 0) {
183 return ($this->mObject->get(
'posts') >= $this->_mMinPost);
196 public function getDefaultView(&$controller, &$xoopsUser)
198 $navi =
new XCube_PageNavigator(XOOPS_URL .
'/edituser.php?op=avatarform&uid=' . $xoopsUser->get(
'uid'), XCUBE_PAGENAVI_START);
199 $handler =& xoops_getmodulehandler(
'avatar',
'user');
201 $this->mSystemAvatars[] =& $handler->createNoavatar();
203 $this->mFilter =
new User_AvatarFilterForm($navi, $handler);
204 $this->mFilter->fetch();
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];
212 $this->mAvatarSelectForm =
new User_AvatarSelectForm();
213 $this->mAvatarSelectForm->prepare();
215 $this->mAvatarSelectForm->load($this->mObject);
217 return parent::getDefaultView($controller, $xoopsUser);
220 public function execute(&$controller, &$xoopsUser)
222 if (
null == $this->mObject) {
223 return USER_FRAME_VIEW_ERROR;
226 if ($this->_mMinPost > 0 && $this->mObject->get(
'posts') < $this->_mMinPost) {
227 return USER_FRAME_VIEW_ERROR;
230 $this->mActionForm->load($this->mObject);
232 $this->mActionForm->fetch();
233 $this->mActionForm->validate();
235 if ($this->mActionForm->hasError()) {
236 return $this->getDefaultView($controller, $xoopsUser);
239 $this->mActionForm->update($this->mObject);
241 return $this->
_doExecute() ? USER_FRAME_VIEW_SUCCESS
242 : USER_FRAME_VIEW_ERROR;
252 if (
null != $this->mActionForm->mFormFile) {
253 if (!$this->mActionForm->mFormFile->saveAs(XOOPS_UPLOAD_PATH)) {
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]);
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);
276 $linkHandler =& xoops_getmodulehandler(
'avatar_user_link',
'user');
277 $linkHandler->deleteAllByUser($this->mObject);
279 $link =& $linkHandler->create();
280 $link->set(
'user_id', $this->mObject->get(
'uid'));
281 $link->set(
'avatar_id', $avatar->get(
'avatar_id'));
283 $linkHandler->insert($link);
303 if (! function_exists(
'imagecreatefromjpeg') || ! function_exists(
'imagecreatefrompng') || ! function_exists(
'imagecreatefromgif')) {
307 if (! $formFile = $this->mActionForm->mFormFile) {
310 $filePath = XOOPS_UPLOAD_PATH.
'/'.$formFile->getFileName();
311 [$width, $height, $type, $attr]=getimagesize($filePath);
314 $source = imagecreatefromjpeg($filePath);
317 $source = imagecreatefromgif($filePath);
320 $source = imagecreatefrompng($filePath);
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)) {
330 imagejpeg($result, $filePath);
333 imagegif($result, $filePath);
336 imagepng($result, $filePath);
353 if (min($width, $height, $maxWidth, $maxHeight) < 1) {
354 echo $width .
'/'. $height .
'/'. $maxWidth .
'/'. $maxHeight;
357 $scale = min($maxWidth / $width, $maxHeight / $height, 1);
358 return [
'width' => (int)($width * $scale),
'height' => (int)($height * $scale)];
361 public function executeViewInput(&$controller, &$xoopsUser, &$render)
363 $render->setTemplateName(
'user_avatar_edit.html');
364 $render->setAttribute(
'actionForm', $this->mActionForm);
365 $render->setAttribute(
'thisUser', $this->mObject);
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);
372 $render->setAttribute(
'pageNavi', $this->mFilter->mNavi);
373 $render->setAttribute(
'systemAvatars', $this->mSystemAvatars);
374 $render->setAttribute(
'avatarSelectForm', $this->mAvatarSelectForm);
377 public function executeViewSuccess(&$controller, &$xoopsUser, &$render)
379 $controller->executeForward(XOOPS_URL .
'/userinfo.php?uid=' . $this->mActionForm->get(
'uid'));
382 public function executeViewError(&$controller, &$xoopsUser, &$render)
384 $controller->executeRedirect(XOOPS_URL .
'/userinfo.php?uid=' . $this->mActionForm->get(
'uid'), 1, _MD_USER_ERROR_DBUPDATE_FAILED);