SettingsController::defaultAvatar PHP Method

defaultAvatar() public method

Settings page for uploading, deleting and cropping the default avatar.
public defaultAvatar ( )
    public function defaultAvatar()
    {
        $this->permission('Garden.Community.Manage');
        $this->setHighlightRoute('dashboard/settings/avatars');
        $this->title(t('Default Avatar'));
        $this->addJsFile('avatars.js');
        $validation = new Gdn_Validation();
        $configurationModel = new Gdn_ConfigurationModel($validation);
        $this->Form->setModel($configurationModel);
        if (($avatar = c('Garden.DefaultAvatar')) && $this->isUploadedDefaultAvatar($avatar)) {
            //Get the image source so we can manipulate it in the crop module.
            $upload = new Gdn_UploadImage();
            $thumbnailSize = c('Garden.Thumbnail.Size', 40);
            $basename = changeBasename($avatar, "p%s");
            $source = $upload->copyLocal($basename);
            //Set up cropping.
            $crop = new CropImageModule($this, $this->Form, $thumbnailSize, $thumbnailSize, $source);
            $crop->saveButton = false;
            $crop->setExistingCropUrl(Gdn_UploadImage::url(changeBasename($avatar, "n%s")));
            $crop->setSourceImageUrl(Gdn_UploadImage::url(changeBasename($avatar, "p%s")));
            $this->setData('crop', $crop);
        } else {
            $this->setData('avatar', UserModel::getDefaultAvatarUrl());
        }
        if (!$this->Form->authenticatedPostBack()) {
            $this->Form->setData($configurationModel->Data);
        } else {
            if ($this->Form->save() !== false) {
                $upload = new Gdn_UploadImage();
                $newAvatar = false;
                $newUpload = false;
                if ($tmpAvatar = $upload->validateUpload('DefaultAvatar', false)) {
                    // New upload
                    $newUpload = true;
                    $thumbOptions = array('Crop' => true, 'SaveGif' => c('Garden.Thumbnail.SaveGif'));
                    $newAvatar = $this->saveDefaultAvatars($tmpAvatar, $thumbOptions);
                } else {
                    if ($avatar && $crop && $crop->isCropped()) {
                        // New thumbnail
                        $tmpAvatar = $source;
                        $thumbOptions = array('Crop' => true, 'SourceX' => $crop->getCropXValue(), 'SourceY' => $crop->getCropYValue(), 'SourceWidth' => $crop->getCropWidth(), 'SourceHeight' => $crop->getCropHeight());
                        $newAvatar = $this->saveDefaultAvatars($tmpAvatar, $thumbOptions);
                    }
                }
                if ($this->Form->errorCount() == 0) {
                    if ($newAvatar) {
                        $this->deleteDefaultAvatars($avatar);
                        $avatar = c('Garden.DefaultAvatar');
                        $thumbnailSize = c('Garden.Thumbnail.Size', 40);
                        // Update crop properties.
                        $basename = changeBasename($avatar, "p%s");
                        $source = $upload->copyLocal($basename);
                        $crop = new CropImageModule($this, $this->Form, $thumbnailSize, $thumbnailSize, $source);
                        $crop->saveButton = false;
                        $crop->setSize($thumbnailSize, $thumbnailSize);
                        $crop->setExistingCropUrl(Gdn_UploadImage::url(changeBasename($avatar, "n%s")));
                        $crop->setSourceImageUrl(Gdn_UploadImage::url(changeBasename($avatar, "p%s")));
                        $this->setData('crop', $crop);
                        // New uploads stay on the page to allow cropping. Otherwise, redirect to avatar settings page.
                        if (!$newUpload) {
                            redirect('/dashboard/settings/avatars');
                        }
                    }
                    $this->informMessage(t("Your settings have been saved."));
                }
            }
        }
        $this->render();
    }