📄 user.class.php
字号:
<?php /** * User class * Generated on Sat, 25 Feb 2006 17:37:12 +0100 by DataObject generation tool * * @http://www.projectpier.org/ */ class User extends BaseUser { /** * Cached project permission values. Two level array. First level are projects (project ID) and * second are permissions associated with permission name * * @var array */ private $project_permissions_cache = array(); /** * Associative array. Key is project ID and value is true if user has access to that project * * @var array */ private $is_project_user_cache = array(); /** * True if user is member of owner company. This value is read on first request and cached * * @var boolean */ private $is_member_of_owner_company = null; /** * Cached is_administrator value. First time value is requested it will be checked and cached. * After that every request will return cached value * * @var boolean */ private $is_administrator = null; /** * Cached is_account_owner value. Value is retrived on first requests * * @var boolean */ private $is_account_owner = null; /** * Cached value of all projects * * @var array */ private $projects; /** * Cached value of active projects * * @var array */ private $active_projects; /** * Cached value of finished projects * * @var array */ private $finished_projects; /** * Array of all active milestons * * @var array */ private $all_active_milestons; /** * Cached late milestones * * @var array */ private $late_milestones; /** * Cached today milestones * * @var array */ private $today_milestones; /** * Cached array of new objects * * @var array */ private $whats_new; /** * canSeeCompany() method will cache its result here (company_id => visible as bool) * * @var array */ private $visible_companies = array(); /** * Construct user object * * @param void * @return User */ function __construct() { parent::__construct(); $this->addProtectedAttribute('password', 'salt', 'session_lifetime', 'token', 'twister', 'last_login', 'last_visit', 'last_activity'); } // __construct /** * Check if this user is member of specific company * * @access public * @param Company $company * @return boolean */ function isMemberOf(Company $company) { return $this->getCompanyId() == $company->getId(); } // isMemberOf /** * Usualy we check if user is member of owner company so this is the shortcut method * * @param void * @return boolean */ function isMemberOfOwnerCompany() { if (is_null($this->is_member_of_owner_company)) { $this->is_member_of_owner_company = $this->isMemberOf(owner_company()); } return $this->is_member_of_owner_company; } // isMemberOfOwnerCompany /** * Check if this user is part of specific project * * @param Project $project * @return boolean */ function isProjectUser(Project $project) { if (!isset($this->is_project_user_cache[$project->getId()])) { $project_user = ProjectUsers::findById(array( 'project_id' => $project->getId(), 'user_id' => $this->getId()) ); // findById $this->is_project_user_cache[$project->getId()] = $project_user instanceof ProjectUser; } // if return $this->is_project_user_cache[$project->getId()]; } // isProjectUser /** * Check if this of specific company website. If must be member of that company and is_admin flag set to true * * @param void * @return boolean */ function isAdministrator() { if (is_null($this->is_administrator)) { $this->is_administrator = $this->isAccountOwner() || ($this->isMemberOfOwnerCompany() && $this->getIsAdmin()); } // if return $this->is_administrator; } // isAdministrator /** * Account owner is user account that was created when company website is created * * @param void * @return boolean */ function isAccountOwner() { if (is_null($this->is_account_owner)) { $this->is_account_owner = $this->isMemberOfOwnerCompany() && (owner_company()->getCreatedById() == $this->getId()); } // if return $this->is_account_owner; } // isAccountOwner /** * Check if this user have specific project permission. $permission is the name of table field that holds the value * * @param Project $project * @param string $permission Name of the field where the permission value is stored. There are set of constants * in ProjectUser that hold field names (ProjectUser::CAN_MANAGE_MESSAGES ...) * @return boolean */ function hasProjectPermission(Project $project, $permission, $use_cache = true) { if ($use_cache) { if (isset($this->project_permissions_cache[$project->getId()]) && isset($this->project_permissions_cache[$project->getId()][$permission])) { return $this->project_permissions_cache[$project->getId()][$permission]; } // if } // if $project_user = ProjectUsers::findById(array('project_id' => $project->getId(), 'user_id' => $this->getId())); if (!($project_user instanceof ProjectUser)) { if ($use_cache) { $this->project_permissions_cache[$project->getId()][$permission] = false; } // if return false; } // if $getter_method = 'get' . Inflector::camelize($permission); $project_user_methods = get_class_methods('ProjectUser'); $value = in_array($getter_method, $project_user_methods) ? $project_user->$getter_method() : false; if ($use_cache) { $this->project_permissions_cache[$project->getId()][$permission] = $value; } return $value; } // hasProjectPermission /** * This function will check if this user have all project permissions * * @param Project $project * @param boolean $use_cache * @return boolean */ function hasAllProjectPermissions(Project $project, $use_cache = true) { $permissions = ProjectUsers::getPermissionColumns(); if (is_array($permissions)) { foreach ($permissions as $permission) { if (!$this->hasProjectPermission($project, $permission, $use_cache)) { return false; } } // foreach } // if return true; } // hasAllProjectPermissions // --------------------------------------------------- // Retrive // --------------------------------------------------- /** * Return owner company * * @access public * @param void * @return Company */ function getCompany() { return Companies::findById($this->getCompanyId()); } // getCompany /** * Return all projects that this user is member of * * @access public * @param void * @return array */ function getProjects() { if (is_null($this->projects)) { $this->projects = ProjectUsers::getProjectsByUser($this); } // if return $this->projects; } // getProjects /** * Return array of active projects that this user have access * * @access public * @param void * @return array */ function getActiveProjects() { if (is_null($this->active_projects)) { $this->active_projects = ProjectUsers::getProjectsByUser($this, '`completed_on` = ' . DB::escape(EMPTY_DATETIME)); } // if return $this->active_projects; } // getActiveProjects /** * Return array of finished projects * * @access public * @param void * @return array */ function getFinishedProjects() { if (is_null($this->finished_projects)) { $this->finished_projects = ProjectUsers::getProjectsByUser($this, '`completed_on` > ' . DB::escape(EMPTY_DATETIME)); } // if return $this->finished_projects; } // getFinishedProjects /** * Return all active milestones assigned to this user * * @param void * @return array */ function getActiveMilestones() { if (is_null($this->all_active_milestons)) { $this->all_active_milestons = ProjectMilestones::getActiveMilestonesByUser($this); } // if return $this->all_active_milestons; } // getActiveMilestones /** * Return late milestones that this user have access to * * @access public * @param void * @return array */ function getLateMilestones() { if (is_null($this->late_milestones)) { $this->late_milestones = ProjectMilestones::getLateMilestonesByUser($this); } // if return $this->late_milestones; } // getLateMilestones /** * Return today milestones that this user have access to * * @access public * @param void * @return array */ function getTodayMilestones() { if (is_null($this->today_milestones)) { $this->today_milestones = ProjectMilestones::getTodayMilestonesByUser($this); } // if return $this->today_milestones; } // getTodayMilestones /** * Return display name for this account. If there is no display name set username will be used * * @access public * @param void * @return string */ function getDisplayName() { $display = parent::getDisplayName(); return trim($display) == '' ? $this->getUsername() : $display; } // getDisplayName /** * Returns true if we have title value set * * @access public * @param void * @return boolean */ function hasTitle() { return trim($this->getTitle()) <> ''; } // hasTitle // --------------------------------------------------- // IMs // --------------------------------------------------- /** * Return true if this user have at least one IM address * * @access public * @param void * @return boolean */ function hasImValue() { return UserImValues::count('`user_id` = ' . DB::escape($this->getId())); } // hasImValue /** * Return all IM values * * @access public * @param void * @return array */ function getImValues() { return UserImValues::getByUser($this); } // getImValues /** * Return value of specific IM. This function will return null if IM is not found * * @access public * @param ImType $im_type * @return string */ function getImValue(ImType $im_type) { $im_value = UserImValues::findById(array('user_id' => $this->getId(), 'im_type_id' => $im_type->getId())); return $im_value instanceof UserImValue && (trim($im_value->getValue()) <> '') ? $im_value->getValue() : null; } // getImValue /** * Return default IM value. If value was not found NULL is returned * * @access public * @param void * @return string */ function getDefaultImValue() { $default_im_type = $this->getDefaultImType(); return $this->getImValue($default_im_type); } // getDefaultImValue /** * Return default user IM type. If there is no default user IM type NULL is returned * * @access public * @param void * @return ImType */ function getDefaultImType() { return UserImValues::getDefaultUserImType($this); } // getDefaultImType /** * Clear all IM values * * @access public * @param void * @return boolean */ function clearImValues() { return UserImValues::instance()->clearByUser($this); } // clearImValues // --------------------------------------------------- // Avatars // --------------------------------------------------- /** * Set user avatar from $source file * * @param string $source Source file * @param integer $max_width Max avatar widht * @param integer $max_height Max avatar height * @param boolean $save Save user object when done * @return string */ function setAvatar($source, $max_width = 50, $max_height = 50, $save = true) { if (!is_readable($source)) { return false; } do { $temp_file = ROOT . '/cache/' . sha1(uniqid(rand(), true)); } while (is_file($temp_file)); try { Env::useLibrary('simplegd'); $image = new SimpleGdImage($source); $thumb = $image->scale($max_width, $max_height, SimpleGdImage::BOUNDARY_DECREASE_ONLY, false); $thumb->saveAs($temp_file, IMAGETYPE_PNG); $public_filename = PublicFiles::addFile($temp_file, 'png'); if ($public_filename) { $this->setAvatarFile($public_filename); if ($save) { $this->save(); } // if } // if $result = true; } catch(Exception $e) { $result = false; } // try // Cleanup if (!$result && $public_filename) { PublicFiles::deleteFile($public_filename); } // if @unlink($temp_file); return $result; } // setAvatar /** * Delete avatar * * @param void * @return null */ function deleteAvatar() { if ($this->hasAvatar()) { PublicFiles::deleteFile($this->getAvatarFile()); $this->setAvatarFile(''); } // if } // deleteAvatar /** * Return path to the avatar file. This function just generates the path, does not check if file really exists * * @access public * @param void * @return string */ function getAvatarPath() { return PublicFiles::getFilePath($this->getAvatarFile()); } // getAvatarPath /** * Return URL of avatar * * @access public * @param void * @return string */ function getAvatarUrl() { return $this->hasAvatar() ? PublicFiles::getFileUrl($this->getAvatarFile()) : get_image_url('avatar.gif'); } // getAvatarUrl /** * Check if this user has uploaded avatar * * @access public * @param void * @return boolean */ function hasAvatar() { return (trim($this->getAvatarFile()) <> '') && is_file($this->getAvatarPath()); } // hasAvatar // --------------------------------------------------- // Utils // ---------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -