XF 2.1 XF Framework - User Upgrades/Downgrades

Jerr0w

Member
Hello,

I'm currently coding an external page with PHP and I need to be able to upgrade/downgrade users through the XF framework. Unfortunarely I can't find any references for user upgrade/downgrades.

Can someone share some code for User upgrades/downgrades through the XF framework? :) Currenty I'm only able to get the UserID by username. Thanks!
 

Jerr0w

Member
Bump.

I found this in XF/Admin/Controller/UserUpgrade.php

PHP:
public function actionManual(ParameterBag $params)
    {
        $upgrade = $this->assertUpgradeExists($params->user_upgrade_id);

        if ($this->isPost())
        {
            $username = $this->filter('username', 'str');
            $user = $this->em()->findOne('XF:User', ['username' => $username]);
            if (!$user)
            {
                return $this->error(\XF::phrase('requested_user_not_found'));
            }

            $endDate = $this->filter('end_type', 'str') == 'date'
                ? $this->filter('end_date', 'datetime')
                : 0;

            /** @var \XF\Service\User\Upgrade $upgradeService */
            $upgradeService = $this->service('XF:User\Upgrade', $upgrade, $user);
            $upgradeService->setEndDate($endDate);
            $upgradeService->ignoreUnpurchasable(true);
            $upgradeService->upgrade();

            return $this->redirect($this->buildLink('user-upgrades'));
        }
        else
        {
            if ($upgrade->length_unit)
            {
                $endDate = strtotime('+' . $upgrade->length_amount . ' ' . $upgrade->length_unit);
            }
            else
            {
                $endDate = false;
            }

            $viewParams = [
                'endDate' => $endDate,
                'upgrade' => $upgrade
            ];
            return $this->view('XF:UserUpgrade\Manual', 'user_upgrade_manual', $viewParams);
        }
    }

Unfortunately, I don't know how to use the class properly with the XenForo Framework.

I've loaded the Controller as followed:

PHP:
$app = XF::setupApp('XF\Admin\App');

Still need help. Would be appreciated..
 

Jerr0w

Member
Hello boys,

I've got it working. Unfortunately, no one could really help. I hope this code actually helps some people.

PHP:
  // Set XenForo directory
  $dir = '/var/www/public';

  // Require XenForo
  require($dir . '/src/XF.php');

  // Load XenForo
  XF::start($dir);
  $app = XF::setupApp('XF\Pub\App');

  // Get Upgrade Repository
  $app->repository('XF:UserUpgrade');

  // Get User Information
  $userFinder = \XF::finder('XF:User');
  $username = $userFinder->where('username', $subDetails['username'])->fetchOne();

  // Get UserUpgrade Information
  $upgradeFinder = \XF::finder('XF:UserUpgrade');
  $userUpgrade = $upgradeFinder->where('user_upgrade_id', $upgradeId)->fetchOne();

  // Get Active Upgrade Information
  $userUpgradeActiveFinder = \XF::Finder('XF:UserUpgradeActive');
  $activeUserUpgrades = $userUpgradeActiveFinder->where('user_id', $username->user_id)->fetch();

  // Upgrade User
  $upgradeService = $app->service('XF:User\Upgrade', $userUpgrade, $username);
  $upgradeService->setEndDate($timestamp);
  $upgradeService->ignoreUnpurchasable(true);
  $upgradeService->upgrade();

  // Downgrade User
  $downgradeService = $app->service('XF:User\Downgrade', $userUpgrade, $username);
  $downgradeService->setSendAlert(false);
  $downgradeService->downgrade();

  // - - - - - - - - - - - - - - - - - - -

  // Extend User Upgrade
  foreach ($activeUserUpgrades as &$activeUserUpgrade) {
    if (in_array($activeUserUpgrade->user_upgrade_id, $validUpgrades)) {
      $userUpgrade = $activeUserUpgrade->Upgrade;
      $expireDate = $activeUserUpgrade->end_date;
      break;
    }
  }
 
  $upgradeService = $app->service('XF:User\Upgrade', $userUpgrade, $username);
  $upgradeService->setEndDate($expiryDate + $monthInUnixtime /*FOR EXAMPLE*/);
  $upgradeService->ignoreUnpurchasable(true);
  $upgradeService->upgrade();
 

gotski

Active member
Hello boys,

I've got it working. Unfortunately, no one could really help. I hope this code actually helps some people.

PHP:
  // Set XenForo directory
  $dir = '/var/www/public';

  // Require XenForo
  require($dir . '/src/XF.php');

  // Load XenForo
  XF::start($dir);
  $app = XF::setupApp('XF\Pub\App');

  // Get Upgrade Repository
  $app->repository('XF:UserUpgrade');

  // Get User Information
  $userFinder = \XF::finder('XF:User');
  $username = $userFinder->where('username', $subDetails['username'])->fetchOne();

  // Get UserUpgrade Information
  $upgradeFinder = \XF::finder('XF:UserUpgrade');
  $userUpgrade = $upgradeFinder->where('user_upgrade_id', $upgradeId)->fetchOne();

  // Get Active Upgrade Information
  $userUpgradeActiveFinder = \XF::Finder('XF:UserUpgradeActive');
  $activeUserUpgrades = $userUpgradeActiveFinder->where('user_id', $username->user_id)->fetch();

  // Upgrade User
  $upgradeService = $app->service('XF:User\Upgrade', $userUpgrade, $username);
  $upgradeService->setEndDate($timestamp);
  $upgradeService->ignoreUnpurchasable(true);
  $upgradeService->upgrade();

  // Downgrade User
  $downgradeService = $app->service('XF:User\Downgrade', $userUpgrade, $username);
  $downgradeService->setSendAlert(false);
  $downgradeService->downgrade();

  // - - - - - - - - - - - - - - - - - - -

  // Extend User Upgrade
  foreach ($activeUserUpgrades as &$activeUserUpgrade) {
    if (in_array($activeUserUpgrade->user_upgrade_id, $validUpgrades)) {
      $userUpgrade = $activeUserUpgrade->Upgrade;
      $expireDate = $activeUserUpgrade->end_date;
      break;
    }
  }
 
  $upgradeService = $app->service('XF:User\Upgrade', $userUpgrade, $username);
  $upgradeService->setEndDate($expiryDate + $monthInUnixtime /*FOR EXAMPLE*/);
  $upgradeService->ignoreUnpurchasable(true);
  $upgradeService->upgrade();

Thank you! You saved a lot of my time.
 

Edman

Member
Hello boys,

I've got it working. Unfortunately, no one could really help. I hope this code actually helps some people.

PHP:
  // Set XenForo directory
  $dir = '/var/www/public';

  // Require XenForo
  require($dir . '/src/XF.php');

  // Load XenForo
  XF::start($dir);
  $app = XF::setupApp('XF\Pub\App');

  // Get Upgrade Repository
  $app->repository('XF:UserUpgrade');

  // Get User Information
  $userFinder = \XF::finder('XF:User');
  $username = $userFinder->where('username', $subDetails['username'])->fetchOne();

  // Get UserUpgrade Information
  $upgradeFinder = \XF::finder('XF:UserUpgrade');
  $userUpgrade = $upgradeFinder->where('user_upgrade_id', $upgradeId)->fetchOne();

  // Get Active Upgrade Information
  $userUpgradeActiveFinder = \XF::Finder('XF:UserUpgradeActive');
  $activeUserUpgrades = $userUpgradeActiveFinder->where('user_id', $username->user_id)->fetch();

  // Upgrade User
  $upgradeService = $app->service('XF:User\Upgrade', $userUpgrade, $username);
  $upgradeService->setEndDate($timestamp);
  $upgradeService->ignoreUnpurchasable(true);
  $upgradeService->upgrade();

  // Downgrade User
  $downgradeService = $app->service('XF:User\Downgrade', $userUpgrade, $username);
  $downgradeService->setSendAlert(false);
  $downgradeService->downgrade();

  // - - - - - - - - - - - - - - - - - - -

  // Extend User Upgrade
  foreach ($activeUserUpgrades as &$activeUserUpgrade) {
    if (in_array($activeUserUpgrade->user_upgrade_id, $validUpgrades)) {
      $userUpgrade = $activeUserUpgrade->Upgrade;
      $expireDate = $activeUserUpgrade->end_date;
      break;
    }
  }
 
  $upgradeService = $app->service('XF:User\Upgrade', $userUpgrade, $username);
  $upgradeService->setEndDate($expiryDate + $monthInUnixtime /*FOR EXAMPLE*/);
  $upgradeService->ignoreUnpurchasable(true);
  $upgradeService->upgrade();
Saved me today, thanks!!
 
Top