/** * * Generates HTML with drop-down roles menu * and a Shred button if current Viewer has necessary * permissions * * @param Registry $Registry * @param User $User user whose profile is being viewed now * @return string html fragment with Form and button */ public static function getHtml(Registry $Registry, User $User) { $oACL = $Registry->Acl; $options = ''; $shredButton = ''; $token = ''; $uid = $User->getUid(); $role = $Registry->Viewer->getRoleId(); d('role: ' . $role); if ($oACL->isAllowed($role, null, 'change_user_role')) { d('change_user_role is allowed'); $userRole = $User->getRoleId(); $roles = $oACL->getRegisteredRoles(); $token = Form::generateToken(); foreach ($roles as $roleName => $val) { $selected = $roleName === $userRole ? ' selected' : ''; $options .= "\n" . vsprintf('<option value="%1$s"%2$s>%1$s</option>', array($roleName, $selected)); } } if ($oACL->isAllowed($role, null, 'shred_user')) { d('getting shred button'); $shredButton = '<div class="fl cb"><input type="button" class="ajax btn_shred rounded4" value="@@Shred User@@" id="shred' . $uid . '"></div>'; } if (empty($options) && empty($shredButton)) { return ''; } return \tplSelectrole::parse(array($token, $uid, $options, $shredButton), false); }
/** * Parse the avatar file in $tempPath, * by creating small square image from it, * save into file system and then add path to new avatar * in User object as 'avatar' element * * * @param User $User * @param unknown_type $tempPath * @throws \Lampcms\Exception * * @return object $this */ public function addAvatar(User $User, $tempPath) { d('$tempPath: ' . $tempPath); if (empty($tempPath)) { d('no avatar to add'); return $this; } $size = $this->Registry->Ini->AVATAR_SQUARE_SIZE; $avatarDir = LAMPCMS_DATA_DIR . 'img' . DS . 'avatar' . DS . 'sqr' . DS; d('$avatarDir: ' . $avatarDir); $savePath = Path::prepare($User->getUid(), $avatarDir); d('$savePath: ' . $savePath); /** * Create avatar and save it * with compression level of 80% (small compression) */ //try{ $ImgParser = \Lampcms\Image\Editor::factory($this->Registry)->loadImage($tempPath)->makeSquare($size); $savePath .= $ImgParser->getExtension(); $ImgParser->save($avatarDir . $savePath, null, 80); d('avatar saved to ' . $savePath); //} catch(\Lampcms\ImageException $e){ //e('ImageException caught in: '.$e->getFile().' on line: '.$e->getLine().' error: '.$e->getMessage()); //throw new \Lampcms\Exception('Unable to process your avatar image at this time'); //} /** * Now remove tempPath file */ @\unlink($tempPath); /** * Now add the path to avatar * to user object * save() is not invoked on User object here! * Either rely on auto-save (may not work in case User is * actually the Viewer object) or call save() * from a function that invoked this method */ $User['avatar'] = $savePath; return $this; }
/** * Approve pending resource * * @param User $Moderator User object of user who approved this Question * * @return mixed true if status was changed|int status code of question */ public function setApprovedStatus(\Lampcms\User $Moderator) { $status = $this->offsetGet(Schema::RESOURCE_STATUS_ID); if ($status === Schema::PENDING) { $this->offsetSet(Schema::RESOURCE_STATUS_ID, Schema::POSTED); $this->offsetSet(Schema::APPROVED_BY_ID, $Moderator->getUid()); $this->offsetSet(Schema::APPROVED_BY_USERNAME, $Moderator->getDisplayName()); $this->offsetSet(Schema::APPROVED_TIMESTAMP, time()); $this->touch(true); $this->save(); return true; } return $status; }
/** * * Adds a_edited array of data to Question * * @param User $user * @param string $reason reason for editing * * @return object $this */ public function setEdited(User $user, $reason = '') { if (!empty($reason)) { $reason = \strip_tags((string) $reason); } $aEdited = $this->offsetGet('a_edited'); if (empty($aEdited) || !is_array($aEdited)) { $aEdited = array(); } $aEdited[] = array('username' => $user->getDisplayName(), 'i_uid' => $user->getUid(), 'av' => $user->getAvatarSrc(), 'reason' => $reason, 'i_ts' => time(), 'hts' => date('F j, Y g:i a T')); parent::offsetSet('a_edited', $aEdited); return $this; }
/** * Process follow user request * * @param \Lampcms\User|Object $User object of type User user who follows * @param int $userid id user being followed (followee) * * @throws \InvalidArgumentException * @return object $this */ public function followUser(User $User, $userid) { if (!is_int($userid)) { throw new \InvalidArgumentException('$userid must be an integer'); } $aFollowed = $User['a_f_u']; d('$aFollowed: ' . print_r($aFollowed, 1)); if (in_array($userid, $aFollowed)) { e('User ' . $User->getUid() . ' is already following $userid ' . $userid); return $this; } $this->Registry->Dispatcher->post($User, 'onBeforeUserFollow', array('uid' => $userid)); $aFollowed[] = $userid; $User['a_f_u'] = $aFollowed; $User['i_f_u'] = count($aFollowed); $User->save(); $this->Registry->Dispatcher->post($User, 'onUserFollow', array('uid' => $userid)); $this->Registry->Mongo->USERS->update(array('_id' => $userid), array('$inc' => array('i_flwrs' => 1))); return $this; }
/** * Logic: For guests dont check question owner id * For others: insert into QUESTION_VIEWS first as a way * to test for duplicates. * Duplicates are: same uid and same qid * If no duplicate then also increase count of views for this * question * * @todo try to run this as post-echo method via runLater * callback. This is not really resource intensive, but still... * it checks for duplicate, checks viewer ID, etc... * This also runs on every page view, and also since we use fsync when * updating via MongoDoc object, it does require disk write. * * * @return object $this */ public function increaseViews(\Lampcms\User $Viewer, $inc = 1) { if (!\is_int($inc)) { throw new \InvalidArgumentException('Param $inc must be an integer. was: ' . gettype($inc)); } /** * @todo Don't count question owner view * For this we must be able to get Viewer from Registry * * Filter out duplicate views */ $viewerId = $Viewer->getUid(); /** * If guest, then there * will be a problem if we at least don't check * for same session_id */ $viewerId = 0 === $viewerId ? session_id() : $viewerId; $ownerID = $this->offsetGet('i_uid'); d('$viewerId: ' . $viewerId . ' $ownerID: ' . $ownerID); if ($viewerId === $ownerID) { d('viewing own question'); return $this; } $iViews = $this->offsetGet('i_views'); /** * If this is the first view, we will cheat a little * and set the views to 2 * There will never be just 1 view, and this way we don't * have to worry about the plural suffix */ if (0 === $iViews && 1 === $inc) { $inc = 2; } $collViews = $this->getRegistry()->Mongo->QUESTION_VIEWS; $collViews->ensureIndex(array('uid' => 1, 'qid' => 1), array('unique' => true)); $qid = (int) $this->offsetGet('_id'); try { $collViews->insert(array('qid' => $qid, 'uid' => $viewerId, 'i_ts' => time()), array('safe' => true)); parent::offsetSet('i_views', $iViews + (int) $inc); /** * If new value is NOT 1 then set * vw_s (plural suffix) to 's' otherwise * must set to empty string because * by default it's already set to 's' */ $this->offsetSet('vw_s', 's'); } catch (\MongoException $e) { d('duplicate view for qid ' . $qid . ' uid: ' . $viewerId); } return $this; }
/** * Check if this user has same userID * as user object passed to this method * * @param \Lampcms\User $User * * @internal param \Lampcms\User $user another User object * * @return bool true if User object passed here has the same user id */ public function equals(User $User) { return $User->getUid() === $this->getUid(); }