Пример #1
0
 public function loadUserProfile()
 {
     if ($this->profile) {
         return $this->profile;
     }
     $profile_dao = new PhabricatorUserProfile();
     $this->profile = $profile_dao->loadOneWhere('userPHID = %s', $this->getPHID());
     if (!$this->profile) {
         $profile_dao->setUserPHID($this->getPHID());
         $this->profile = $profile_dao;
     }
     return $this->profile;
 }
 protected function didFilterPage(array $users)
 {
     if ($this->needProfile) {
         $user_list = mpull($users, null, 'getPHID');
         $profiles = new PhabricatorUserProfile();
         $profiles = $profiles->loadAllWhere('userPHID IN (%Ls)', array_keys($user_list));
         $profiles = mpull($profiles, null, 'getUserPHID');
         foreach ($user_list as $user_phid => $user) {
             $profile = idx($profiles, $user_phid);
             if (!$profile) {
                 $profile = PhabricatorUserProfile::initializeNewProfile($user);
             }
             $user->attachUserProfile($profile);
         }
     }
     if ($this->needBadges) {
         $awards = id(new PhabricatorBadgesAwardQuery())->setViewer($this->getViewer())->withRecipientPHIDs(mpull($users, 'getPHID'))->execute();
         $awards = mgroup($awards, 'getRecipientPHID');
         foreach ($users as $user) {
             $user_awards = idx($awards, $user->getPHID(), array());
             $badge_phids = mpull($user_awards, 'getBadgePHID');
             $user->attachBadgePHIDs($badge_phids);
         }
     }
     if ($this->needAvailability) {
         $rebuild = array();
         foreach ($users as $user) {
             $cache = $user->getAvailabilityCache();
             if ($cache !== null) {
                 $user->attachAvailability($cache);
             } else {
                 $rebuild[] = $user;
             }
         }
         if ($rebuild) {
             $this->rebuildAvailabilityCache($rebuild);
         }
     }
     $this->fillUserCaches($users);
     return $users;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $profile = id(new PhabricatorUserProfile())->loadOneWhere('userPHID = %s', $user->getPHID());
     if (!$profile) {
         $profile = new PhabricatorUserProfile();
         $profile->setUserPHID($user->getPHID());
     }
     $supported_formats = PhabricatorFile::getTransformableImageFormats();
     $e_image = null;
     $errors = array();
     if ($request->isFormPost()) {
         $profile->setTitle($request->getStr('title'));
         $profile->setBlurb($request->getStr('blurb'));
         if (!empty($_FILES['image'])) {
             $err = idx($_FILES['image'], 'error');
             if ($err != UPLOAD_ERR_NO_FILE) {
                 $file = PhabricatorFile::newFromPHPUpload($_FILES['image'], array('authorPHID' => $user->getPHID()));
                 $okay = $file->isTransformableImage();
                 if ($okay) {
                     $xformer = new PhabricatorImageTransformer();
                     // Generate the large picture for the profile page.
                     $large_xformed = $xformer->executeProfileTransform($file, $width = 280, $min_height = 140, $max_height = 420);
                     $profile->setProfileImagePHID($large_xformed->getPHID());
                     // Generate the small picture for comments, etc.
                     $small_xformed = $xformer->executeProfileTransform($file, $width = 50, $min_height = 50, $max_height = 50);
                     $user->setProfileImagePHID($small_xformed->getPHID());
                 } else {
                     $e_image = 'Not Supported';
                     $errors[] = 'This server only supports these image formats: ' . implode(', ', $supported_formats) . '.';
                 }
             }
         }
         if (!$errors) {
             $user->save();
             $profile->save();
             $response = id(new AphrontRedirectResponse())->setURI('/settings/page/profile/?saved=true');
             return $response;
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     } else {
         if ($request->getStr('saved')) {
             $error_view = new AphrontErrorView();
             $error_view->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
             $error_view->setTitle('Changes Saved');
             $error_view->appendChild('<p>Your changes have been saved.</p>');
             $error_view = $error_view->render();
         }
     }
     $file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $user->getProfileImagePHID());
     if ($file) {
         $img_src = $file->getBestURI();
     } else {
         $img_src = null;
     }
     $profile_uri = PhabricatorEnv::getURI('/p/' . $user->getUsername() . '/');
     $form = new AphrontFormView();
     $form->setUser($request->getUser())->setAction('/settings/page/profile/')->setEncType('multipart/form-data')->appendChild(id(new AphrontFormTextControl())->setLabel('Title')->setName('title')->setValue($profile->getTitle())->setCaption('Serious business title.'))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Profile URI')->setValue(phutil_render_tag('a', array('href' => $profile_uri), phutil_escape_html($profile_uri))))->appendChild('<p class="aphront-form-instructions">Write something about yourself! ' . 'Make sure to include <strong>important information</strong> like ' . 'your favorite pokemon and which Starcraft race you play.</p>')->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Blurb')->setName('blurb')->setValue($profile->getBlurb()))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Profile Image')->setValue(phutil_render_tag('img', array('src' => $img_src))))->appendChild(id(new AphrontFormFileControl())->setLabel('Change Image')->setName('image')->setError($e_image)->setCaption('Supported formats: ' . implode(', ', $supported_formats)))->appendChild(id(new AphrontFormSubmitControl())->setValue('Save')->addCancelButton('/p/' . $user->getUsername() . '/'));
     $panel = new AphrontPanelView();
     $panel->setHeader('Edit Profile Details');
     $panel->appendChild($form);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     return id(new AphrontNullView())->appendChild(array($error_view, $panel));
 }
Пример #4
0
 protected function didFilterPage(array $users)
 {
     if ($this->needProfile) {
         $user_list = mpull($users, null, 'getPHID');
         $profiles = new PhabricatorUserProfile();
         $profiles = $profiles->loadAllWhere('userPHID IN (%Ls)', array_keys($user_list));
         $profiles = mpull($profiles, null, 'getUserPHID');
         foreach ($user_list as $user_phid => $user) {
             $profile = idx($profiles, $user_phid);
             if (!$profile) {
                 $profile = new PhabricatorUserProfile();
                 $profile->setUserPHID($user_phid);
             }
             $user->attachUserProfile($profile);
         }
     }
     if ($this->needProfileImage) {
         $rebuild = array();
         foreach ($users as $user) {
             $image_uri = $user->getProfileImageCache();
             if ($image_uri) {
                 // This user has a valid cache, so we don't need to fetch any
                 // data or rebuild anything.
                 $user->attachProfileImageURI($image_uri);
                 continue;
             }
             // This user's cache is invalid or missing, so we're going to rebuild
             // it.
             $rebuild[] = $user;
         }
         if ($rebuild) {
             $file_phids = mpull($rebuild, 'getProfileImagePHID');
             $file_phids = array_filter($file_phids);
             if ($file_phids) {
                 // NOTE: We're using the omnipotent user here because older profile
                 // images do not have the 'profile' flag, so they may not be visible
                 // to the executing viewer. At some point, we could migrate to add
                 // this flag and then use the real viewer, or just use the real
                 // viewer after enough time has passed to limit the impact of old
                 // data. The consequence of missing here is that we cache a default
                 // image when a real image exists.
                 $files = id(new PhabricatorFileQuery())->setParentQuery($this)->setViewer(PhabricatorUser::getOmnipotentUser())->withPHIDs($file_phids)->execute();
                 $files = mpull($files, null, 'getPHID');
             } else {
                 $files = array();
             }
             foreach ($rebuild as $user) {
                 $image_phid = $user->getProfileImagePHID();
                 if (isset($files[$image_phid])) {
                     $image_uri = $files[$image_phid]->getBestURI();
                 } else {
                     $image_uri = PhabricatorUser::getDefaultProfileImageURI();
                 }
                 $user->writeProfileImageCache($image_uri);
                 $user->attachProfileImageURI($image_uri);
             }
         }
     }
     if ($this->needAvailability) {
         $rebuild = array();
         foreach ($users as $user) {
             $cache = $user->getAvailabilityCache();
             if ($cache !== null) {
                 $user->attachAvailability($cache);
             } else {
                 $rebuild[] = $user;
             }
         }
         if ($rebuild) {
             $this->rebuildAvailabilityCache($rebuild);
         }
     }
     return $users;
 }
Пример #5
0
 protected function didFilterPage(array $users)
 {
     if ($this->needProfile) {
         $user_list = mpull($users, null, 'getPHID');
         $profiles = new PhabricatorUserProfile();
         $profiles = $profiles->loadAllWhere('userPHID IN (%Ls)', array_keys($user_list));
         $profiles = mpull($profiles, null, 'getUserPHID');
         foreach ($user_list as $user_phid => $user) {
             $profile = idx($profiles, $user_phid);
             if (!$profile) {
                 $profile = new PhabricatorUserProfile();
                 $profile->setUserPHID($user_phid);
             }
             $user->attachUserProfile($profile);
         }
     }
     if ($this->needProfileImage) {
         $user_profile_file_phids = mpull($users, 'getProfileImagePHID');
         $user_profile_file_phids = array_filter($user_profile_file_phids);
         if ($user_profile_file_phids) {
             $files = id(new PhabricatorFileQuery())->setParentQuery($this)->setViewer($this->getViewer())->withPHIDs($user_profile_file_phids)->execute();
             $files = mpull($files, null, 'getPHID');
         } else {
             $files = array();
         }
         foreach ($users as $user) {
             $image_phid = $user->getProfileImagePHID();
             if (isset($files[$image_phid])) {
                 $profile_image_uri = $files[$image_phid]->getBestURI();
             } else {
                 $profile_image_uri = PhabricatorUser::getDefaultProfileImageURI();
             }
             $user->attachProfileImageURI($profile_image_uri);
         }
     }
     if ($this->needStatus) {
         $user_list = mpull($users, null, 'getPHID');
         $statuses = id(new PhabricatorCalendarEvent())->loadCurrentStatuses(array_keys($user_list));
         foreach ($user_list as $phid => $user) {
             $status = idx($statuses, $phid);
             if ($status) {
                 $user->attachStatus($status);
             }
         }
     }
     return $users;
 }
 public function processRequest(AphrontRequest $request)
 {
     $user = $request->getUser();
     $profile = id(new PhabricatorUserProfile())->loadOneWhere('userPHID = %s', $user->getPHID());
     if (!$profile) {
         $profile = new PhabricatorUserProfile();
         $profile->setUserPHID($user->getPHID());
     }
     $supported_formats = PhabricatorFile::getTransformableImageFormats();
     $e_image = null;
     $errors = array();
     if ($request->isFormPost()) {
         $profile->setTitle($request->getStr('title'));
         $profile->setBlurb($request->getStr('blurb'));
         $sex = $request->getStr('sex');
         $sexes = array(PhutilPerson::SEX_MALE, PhutilPerson::SEX_FEMALE);
         if (in_array($sex, $sexes)) {
             $user->setSex($sex);
         } else {
             $user->setSex(null);
         }
         // Checked in runtime.
         $user->setTranslation($request->getStr('translation'));
         $default_image = $request->getExists('default_image');
         if ($default_image) {
             $profile->setProfileImagePHID(null);
             $user->setProfileImagePHID(null);
         } else {
             if (!empty($_FILES['image'])) {
                 $err = idx($_FILES['image'], 'error');
                 if ($err != UPLOAD_ERR_NO_FILE) {
                     $file = PhabricatorFile::newFromPHPUpload($_FILES['image'], array('authorPHID' => $user->getPHID()));
                     $okay = $file->isTransformableImage();
                     if ($okay) {
                         $xformer = new PhabricatorImageTransformer();
                         // Generate the large picture for the profile page.
                         $large_xformed = $xformer->executeProfileTransform($file, $width = 280, $min_height = 140, $max_height = 420);
                         $profile->setProfileImagePHID($large_xformed->getPHID());
                         // Generate the small picture for comments, etc.
                         $small_xformed = $xformer->executeProfileTransform($file, $width = 50, $min_height = 50, $max_height = 50);
                         $user->setProfileImagePHID($small_xformed->getPHID());
                     } else {
                         $e_image = 'Not Supported';
                         $errors[] = 'This server only supports these image formats: ' . implode(', ', $supported_formats) . '.';
                     }
                 }
             }
         }
         if (!$errors) {
             $user->save();
             $profile->save();
             $response = id(new AphrontRedirectResponse())->setURI($this->getPanelURI('?saved=true'));
             return $response;
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     } else {
         if ($request->getStr('saved')) {
             $error_view = new AphrontErrorView();
             $error_view->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
             $error_view->setTitle('Changes Saved');
             $error_view->appendChild('<p>Your changes have been saved.</p>');
             $error_view = $error_view->render();
         }
     }
     $img_src = $user->loadProfileImageURI();
     $profile_uri = PhabricatorEnv::getURI('/p/' . $user->getUsername() . '/');
     $sexes = array(PhutilPerson::SEX_UNKNOWN => 'Unknown', PhutilPerson::SEX_MALE => 'Male', PhutilPerson::SEX_FEMALE => 'Female');
     $translations = array();
     $symbols = id(new PhutilSymbolLoader())->setType('class')->setAncestorClass('PhabricatorTranslation')->setConcreteOnly(true)->selectAndLoadSymbols();
     foreach ($symbols as $symbol) {
         $class = $symbol['name'];
         $translations[$class] = newv($class, array())->getName();
     }
     asort($translations);
     $default = PhabricatorEnv::newObjectFromConfig('translation.provider');
     $translations = array('' => 'Server Default (' . $default->getName() . ')') + $translations;
     $form = new AphrontFormView();
     $form->setUser($request->getUser())->setEncType('multipart/form-data')->appendChild(id(new AphrontFormTextControl())->setLabel('Title')->setName('title')->setValue($profile->getTitle())->setCaption('Serious business title.'))->appendChild(id(new AphrontFormSelectControl())->setOptions($sexes)->setLabel('Sex')->setName('sex')->setValue($user->getSex()))->appendChild(id(new AphrontFormSelectControl())->setOptions($translations)->setLabel('Translation')->setName('translation')->setValue($user->getTranslation()))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Profile URI')->setValue(phutil_render_tag('a', array('href' => $profile_uri), phutil_escape_html($profile_uri))))->appendChild('<p class="aphront-form-instructions">Write something about yourself! ' . 'Make sure to include <strong>important information</strong> like ' . 'your favorite Pokemon and which Starcraft race you play.</p>')->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Blurb')->setName('blurb')->setValue($profile->getBlurb()))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Profile Image')->setValue(phutil_render_tag('img', array('src' => $img_src))))->appendChild(id(new AphrontFormImageControl())->setLabel('Change Image')->setName('image')->setError($e_image)->setCaption('Supported formats: ' . implode(', ', $supported_formats)))->appendChild(id(new AphrontFormSubmitControl())->setValue('Save')->addCancelButton('/p/' . $user->getUsername() . '/'));
     $panel = new AphrontPanelView();
     $panel->setHeader('Edit Profile Details');
     $panel->appendChild($form);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     return array($error_view, $panel);
 }