public function save() { jimport('joomla.filesystem.folder'); jimport('joomla.filesystem.file'); $mainframe = JFactory::getApplication(); $jinput = $mainframe->input; $id = JRequest::getInt('id', 0, 'POST'); $post = JRequest::get('POST'); $fields = $jinput->get('fields', '', 'NONE'); $name = $jinput->get('name', '', 'STRING'); $tmpParents = $jinput->get('parents', '', 'NONE'); $mainframe = JFactory::getApplication(); $task = JRequest::getCmd('task'); $isNew = $id == 0 ? true : false; $validated = true; $multiprofile = JTable::getInstance('MultiProfile', 'CTable'); $multiprofile->load($id); // Skip watermarking if it's the same location $skipWatermark = isset($multiprofile->watermark_location) && isset($post['watermark_location']) && $post['watermark_location'] == $multiprofile->watermark_location ? true : false; // Bind with form post $multiprofile->bind($post); // Can't have an empty name now can we? if (empty($name)) { $validated = false; $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_MULTIPROFILE_NAME_EMPTY'), 'error'); } $date = JFactory::getDate(); $isNew = $multiprofile->id == 0 ? true : false; if ($isNew) { $multiprofile->created = $date->toSql(); } // Store watermarks for profile types. $watermark = $jinput->files->get('watermark', '', 'NONE'); //JRequest::getVar( 'watermark' , '' , 'FILES'); if (!empty($watermark['tmp_name'])) { // Do not allow image size to exceed maximum width and height if (isset($watermark['name']) && !empty($watermark['name'])) { list($width, $height) = getimagesize($watermark['tmp_name']); /** * watermark can't large than 16px * @todo use define for min width & height instead fixed number here */ if ($width > 16 || $height > 16) { $validated = false; $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_MULTIPROFILE_WATERMARK_IMAGE_EXCEEDS_SIZE'), 'error'); } } } if ($validated) { $multiprofile->store(); // If image file is specified, we need to store the thumbnail. if (!empty($watermark['tmp_name'])) { if (isset($watermark['name']) && !empty($watermark['name'])) { if (!JFolder::exists(JPATH_ROOT . '/' . COMMUNITY_WATERMARKS_PATH)) { JFolder::create(JPATH_ROOT . '/' . COMMUNITY_WATERMARKS_PATH); } $watermarkFile = 'watermark_' . $multiprofile->id . CImageHelper::getExtension($watermark['type']); JFile::copy($watermark['tmp_name'], JPATH_ROOT . '/' . COMMUNITY_WATERMARKS_PATH . '/' . $watermarkFile); $multiprofile->watermark = CString::str_ireplace('/', '/', COMMUNITY_WATERMARKS_PATH) . '/' . $watermarkFile; $multiprofile->store(); } } // @rule: Create the watermarks folder if doesn't exists. if (!JFolder::exists(JPATH_ROOT . '/' . COMMUNITY_WATERMARKS_PATH)) { if (!JFolder::create(JPATH_ROOT . '/' . COMMUNITY_WATERMARKS_PATH)) { $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_MULTIPROFILE_UNABLE_TO_CREATE_WATERMARKS_FOLDER')); } } // @rule: Create original folder within watermarks to store original user photos. if (!JFolder::exists(JPATH_ROOT . '/' . COMMUNITY_WATERMARKS_PATH . '/original')) { if (!JFolder::create(JPATH_ROOT . '/' . COMMUNITY_WATERMARKS_PATH . '/original')) { $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_MULTIPROFILE_UNABLE_TO_CREATE_WATERMARKS_FOLDER')); } } if (!empty($watermark['tmp_name']) || !$isNew && !$skipWatermark) { if (isset($watermark['name']) && !empty($watermark['name'])) { $watermarkPath = $watermark['tmp_name']; $watermark_hash = md5($watermark['name'] . time()); } else { $watermarkPath = JPATH_ROOT . '/' . $multiprofile->watermark; $watermark_hash = $multiprofile->watermark_hash; } // Create default watermarks for avatar and thumbnails. // Generate filename $fileName = CImageHelper::getHashName($multiprofile->id . time()) . '.jpg'; $thumbFileName = 'thumb_' . $fileName; // Paths where the thumbnail and avatar should be saved. $thumbPath = JPATH_ROOT . '/' . COMMUNITY_WATERMARKS_PATH . '/' . $thumbFileName; $avatarPath = JPATH_ROOT . '/' . COMMUNITY_WATERMARKS_PATH . '/' . $fileName; // Copy existing default thumbnails into the path first. JFile::copy(JPATH_ROOT . '/' . DEFAULT_USER_THUMB, $thumbPath); JFile::copy(JPATH_ROOT . '/' . DEFAULT_USER_AVATAR, $avatarPath); $watermarkPath = $watermarkPath; list($watermarkWidth, $watermarkHeight) = getimagesize($watermarkPath); $oldDefaultAvatar = $multiprofile->avatar; $oldDefaultThumb = $multiprofile->thumb; // Avatar Properties $avatarInfo = getimagesize($avatarPath); $avatarWidth = $avatarInfo[0]; $avatarHeight = $avatarInfo[1]; $avatarMime = $avatarInfo['mime']; $avatarPosition = $this->_getPositions($multiprofile->watermark_location, $avatarWidth, $avatarHeight, $watermarkWidth, $watermarkHeight); CImageHelper::addWatermark($avatarPath, $avatarPath, $avatarMime, $watermarkPath, $avatarPosition->x, $avatarPosition->y); $multiprofile->avatar = CString::str_ireplace('/', '/', COMMUNITY_WATERMARKS_PATH) . '/' . $fileName; // Thumbnail properties. $thumbInfo = getimagesize($thumbPath); $thumbWidth = $thumbInfo[0]; $thumbHeight = $thumbInfo[1]; $thumbMime = $thumbInfo['mime']; $thumbPosition = $this->_getPositions($multiprofile->watermark_location, $thumbWidth, $thumbHeight, $watermarkWidth, $watermarkHeight); CImageHelper::addWatermark($thumbPath, $thumbPath, $thumbMime, $watermarkPath, $thumbPosition->x, $thumbPosition->y); $multiprofile->thumb = CString::str_ireplace('/', '/', COMMUNITY_WATERMARKS_PATH) . '/' . $thumbFileName; // Since the default thumbnail is used by current users, we need to update their existing values. $multiprofile->updateUserDefaultImage('avatar', $oldDefaultAvatar); $multiprofile->updateUserDefaultImage('thumb', $oldDefaultThumb); $multiprofile->watermark_hash = $watermark_hash; $multiprofile->store(); } // Since it would be very tedious to check if previous fields were enabled or disabled. // We delete all existing mapping and remap it again to ensure data integrity. if (!$isNew && empty($fields)) { $multiprofile->deleteChilds(); } if (!empty($fields)) { $parents = array(); // We need to unique the parents first. foreach ($fields as $id) { $customProfile = JTable::getInstance('Profiles', 'CommunityTable'); $customProfile->load($id); // Need to only $parent = $customProfile->getCurrentParentId(); if (in_array($parent, $tmpParents)) { $parents[] = $parent; } } $parents = array_unique($parents); $fields = array_merge($fields, $parents); $fieldTable = JTable::getInstance('MultiProfileFields', 'CTable'); $fieldTable->cleanField($multiprofile->id); foreach ($fields as $id) { $field = JTable::getInstance('MultiProfileFields', 'CTable'); $field->parent = $multiprofile->id; $field->field_id = $id; $field->store(); } } if ($isNew) { $message = JText::_('COM_COMMUNITY_MULTIPROFILE_CREATED_SUCCESSFULLY'); } else { $message = JText::_('COM_COMMUNITY_MULTIPROFILE_UPDATED_SUCCESSFULLY'); } switch ($task) { case 'apply': $link = 'index.php?option=com_community&view=multiprofile&layout=edit&id=' . $multiprofile->id; break; case 'save': default: $link = 'index.php?option=com_community&view=multiprofile'; break; } $mainframe->redirect($link, $message, 'message'); return; } $document = JFactory::getDocument(); $viewName = JRequest::getCmd('view', 'community'); // Get the view type $viewType = $document->getType(); // Get the view $view = $this->getView($viewName, $viewType); $view->setLayout('edit'); $model = $this->getModel('Profiles'); if ($model) { $view->setModel($model, $viewName); } $view->display(); }
/** * Return path to thumb image */ public function getThumbAvatar() { // @rule: Check if the current user's watermark matches the current system's watermark. $multiprofile = JTable::getInstance('MultiProfile', 'CTable'); $match = $multiprofile->isHashMatched($this->_profile_id, $this->_watermark_hash); if (!$match) { // @rule: Since the admin may have changed the watermark for the specific user profile type, we need to also update // the user's watermark as well. //CFactory::load( 'helpers' , 'image' ); $hashName = CImageHelper::getHashName($this->id . time()); $multiprofile->updateUserAvatar($this, $hashName); $multiprofile->updateUserThumb($this, $hashName); } if (JString::stristr($this->_thumb, 'default_thumb.jpg')) { $this->_thumb = ''; } // For user avatars that are stored in a remote location, we should return the proper path. // @rule: For default avatars and watermark avatars we don't want to alter the url behavior. // as it should be stored locally. if ($this->_storage != 'file' && !empty($this->_thumb) && JString::stristr($this->_thumb, 'images/watermarks') === false) { $storage = CStorage::getStorage($this->_storage); return $storage->getURI($this->_thumb); } if ($this->_thumb && file_exists(JPATH_ROOT . '/' . $this->_thumb)) { return JUri::root() . $this->_thumb; } $gender = $this->_getGender('male'); if (!strlen($gender)) { $gender = 'undefined'; } $td_path = "default-{$gender}-avatar"; if (file_exists(COMMUNITY_PATH_ASSETS . $td_path . '.png')) { return JUri::root() . str_replace(JPATH_ROOT, '', COMMUNITY_PATH_ASSETS) . "{$td_path}.png"; } if (file_exists(COMMUNITY_PATH_ASSETS . $td_path . '.jpg')) { return JUri::root() . str_replace(JPATH_ROOT, '', COMMUNITY_PATH_ASSETS) . "{$td_path}.jpg"; } if ($gender == 'undefined') { $gender = 'male'; } $thumb = CUrlHelper::avatarURI($this->_thumb, 'user-' . ucfirst($gender) . '-thumb.png'); return $thumb; }
/** * Return path to thumb image */ public function getThumbAvatar() { // @rule: Check if the current user's watermark matches the current system's watermark. $multiprofile =& JTable::getInstance('MultiProfile', 'CTable'); $match = $multiprofile->isHashMatched($this->_profile_id, $this->_watermark_hash); if (!$match) { // @rule: Since the admin may have changed the watermark for the specific user profile type, we need to also update // the user's watermark as well. CFactory::load('helpers', 'image'); $hashName = CImageHelper::getHashName($this->id . time()); $multiprofile->updateUserAvatar($this, $hashName); $multiprofile->updateUserThumb($this, $hashName); } if (JString::stristr($this->_thumb, 'default_thumb.jpg')) { $this->_thumb = ''; } // For user avatars that are stored in a remote location, we should return the proper path. // @rule: For default avatars and watermark avatars we don't want to alter the url behavior. // as it should be stored locally. if ($this->_storage != 'file' && !empty($this->_thumb) && JString::stristr($this->_thumb, 'images/watermarks') === false) { $storage = CStorage::getStorage($this->_storage); return $storage->getURI($this->_thumb); } CFactory::load('helpers', 'url'); $thumb = CUrlHelper::avatarURI($this->_thumb, 'user_thumb.png'); return $thumb; }
public function save() { jimport('joomla.filesystem.folder'); jimport('joomla.filesystem.file'); CFactory::load('helpers', 'image'); $id = JRequest::getInt('id', 0, 'POST'); $post = JRequest::get('POST'); $fields = JRequest::getVar('fields', ''); $name = JRequest::getVar('name', ''); $tmpParents = JRequest::getVar('parents', ''); $mainframe =& JFactory::getApplication(); $isNew = $id == 0 ? true : false; $multiprofile =& JTable::getInstance('MultiProfile', 'CTable'); $multiprofile->load($id); $multiprofile->bind($post); // Can't have an empty name now can we? if (empty($name)) { $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_MULTIPROFILE_NAME_EMPTY'), 'error'); $mainframe->redirect('index.php?option=com_community&view=multiprofile&layout=edit'); return; } $date =& JFactory::getDate(); $isNew = $multiprofile->id == 0; if ($isNew) { $multiprofile->created = $date->toMySQL(); } // Store watermarks for profile types. $watermark = JRequest::getVar('watermark', '', 'FILES'); // Do not allow image size to exceed maximum width and height if (isset($watermark['name']) && !empty($watermark['name'])) { list($width, $height) = getimagesize($watermark['tmp_name']); if ($width > 64 || $height > 64) { $mainframe->redirect('index.php?option=com_community&view=multiprofile&layout=edit', JText::_('COM_COMMUNITY_MULTIPROFILE_WATERMARK_IMAGE_EXCEEDS_SIZE'), 'error'); exit; } } $multiprofile->store(); // If image file is specified, we need to store the thumbnail. if (isset($watermark['name']) && !empty($watermark['name'])) { $watermarkFile = 'watermark_' . $multiprofile->id . CImageHelper::getExtension($watermark['type']); JFile::copy($watermark['tmp_name'], JPATH_ROOT . DS . COMMUNITY_WATERMARKS_PATH . DS . $watermarkFile); $multiprofile->watermark = CString::str_ireplace(DS, '/', COMMUNITY_WATERMARKS_PATH) . '/' . $watermarkFile; $multiprofile->store(); } // @rule: Create the watermarks folder if doesn't exists. if (!JFolder::exists(COMMUNITY_WATERMARKS_PATH)) { if (!JFolder::create(COMMUNITY_WATERMARKS_PATH)) { $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_MULTIPROFILE_UNABLE_TO_CREATE_WATERMARKS_FOLDER')); } } // @rule: Create original folder within watermarks to store original user photos. if (!JFolder::exists(COMMUNITY_WATERMARKS_PATH . DS . 'original')) { if (!JFolder::create(COMMUNITY_WATERMARKS_PATH . DS . 'original')) { $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_MULTIPROFILE_UNABLE_TO_CREATE_WATERMARKS_FOLDER')); } } // Create default watermarks for avatar and thumbnails. if (isset($watermark['name']) && !empty($watermark['name']) || !empty($multiprofile->watermark)) { CFactory::load('helpers', 'image'); // Generate filename $fileName = CImageHelper::getHashName($multiprofile->id . time()) . '.jpg'; $thumbFileName = 'thumb_' . $fileName; // Paths where the thumbnail and avatar should be saved. $thumbPath = JPATH_ROOT . DS . COMMUNITY_WATERMARKS_PATH . DS . $thumbFileName; $avatarPath = JPATH_ROOT . DS . COMMUNITY_WATERMARKS_PATH . DS . $fileName; // Copy existing default thumbnails into the path first. JFile::copy(JPATH_ROOT . DS . DEFAULT_USER_THUMB, $thumbPath); JFile::copy(JPATH_ROOT . DS . DEFAULT_USER_AVATAR, $avatarPath); $watermarkPath = $watermark['tmp_name']; list($watermarkWidth, $watermarkHeight) = getimagesize($watermarkPath); $oldDefaultAvatar = $multiprofile->avatar; $oldDefaultThumb = $multiprofile->thumb; // Avatar Properties $avatarInfo = getimagesize($avatarPath); $avatarWidth = $avatarInfo[0]; $avatarHeight = $avatarInfo[1]; $avatarMime = $avatarInfo['mime']; $avatarPosition = $this->_getPositions($multiprofile->watermark_location, $avatarWidth, $avatarHeight, $watermarkWidth, $watermarkHeight); CImageHelper::addWatermark($avatarPath, $avatarPath, 'image/jpg', $watermarkPath, $avatarPosition->x, $avatarPosition->y); $multiprofile->avatar = CString::str_ireplace(DS, '/', COMMUNITY_WATERMARKS_PATH) . '/' . $fileName; // Thumbnail properties. $thumbInfo = getimagesize($thumbPath); $thumbWidth = $thumbInfo[0]; $thumbHeight = $thumbInfo[1]; $thumbMime = $thumbInfo['mime']; $thumbPosition = $this->_getPositions($multiprofile->watermark_location, $thumbWidth, $thumbHeight, $watermarkWidth, $watermarkHeight); CImageHelper::addWatermark($thumbPath, $thumbPath, $thumbMime, $watermarkPath, $thumbPosition->x, $thumbPosition->y); $multiprofile->thumb = CString::str_ireplace(DS, '/', COMMUNITY_WATERMARKS_PATH) . '/' . $thumbFileName; // Since the default thumbnail is used by current users, we need to update their existing values. $multiprofile->updateUserDefaultImage('avatar', $oldDefaultAvatar); $multiprofile->updateUserDefaultImage('thumb', $oldDefaultThumb); $multiprofile->watermark_hash = md5($watermark['name'] . time()); $multiprofile->store(); } // Since it would be very tedious to check if previous fields were enabled or disabled. // We delete all existing mapping and remap it again to ensure data integrity. if (!$isNew && !empty($fields)) { $multiprofile->deleteChilds(); } if (!empty($fields)) { $parents = array(); // We need to unique the parents first. foreach ($fields as $id) { $customProfile =& JTable::getInstance('Profiles', 'CommunityTable'); $customProfile->load($id); // Need to only $parent = $customProfile->getCurrentParentId(); if (in_array($parent, $tmpParents)) { $parents[] = $parent; } } $parents = array_unique($parents); $fields = array_merge($fields, $parents); foreach ($fields as $id) { $field =& JTable::getInstance('MultiProfileFields', 'CTable'); $field->parent = $multiprofile->id; $field->field_id = $id; $field->store(); } } $message = JText::_('COM_COMMUNITY_MULTIPROFILE_UPDATED_SUCCESSFULLY'); if ($isNew) { $message = JText::_('COM_COMMUNITY_MULTIPROFILE_CREATED_SUCCESSFULLY'); } $mainframe->redirect('index.php?option=com_community&view=multiprofile', $message); }