Commit 8f9488bf authored by Binny Thomas's avatar Binny Thomas Committed by Binny Thomas
Browse files

Issue #3310598 by binnythomas: Port the Top Affiliate users block to Drupal 9

parent eae1d7b3
Loading
Loading
Loading
Loading
+63 −1
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@
namespace Drupal\uber_affiliate\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Link;

/**
 * Displays the top affiliates.
@@ -14,10 +16,70 @@ use Drupal\Core\Block\BlockBase;
 */
class UberAffiliateBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    // Todo check permissions if user_access('view affiliate overviews') || user_access('administer affiliate settings') || user_access('view any affiliate info')) {
    //  Then call affiliate_top_users_block_content();
      return "Affiliates Block";
    return $this->affiliate_top_users_block_content();
  }

  /**
   * Return block dataa
   */
  private function affiliate_top_users_block_content() {
    $limit = \Drupal::state()->get('affiliate_module_top_users_count_block', 10);
    $time = time();
    $time_interval = \Drupal::state()->get('affiliate_module_top_users_period_interval', 259200);
    $time_limit = $time - $time_interval;
   // $results = \Drupal::database()->query("SELECT COUNT(DISTINCT(ac.clickid)) AS clicks, u.uid AS uid, u.name AS name, u.data AS data, aa.payouts_owed AS payouts_owed
    // FROM {affiliate_clicks} ac INNER JOIN {users} u ON ac.uid = u.uid INNER JOIN {affiliate} aa ON u.uid = aa.uid WHERE aa.active = 1 AND ac.click_time > :time_limit
    // GROUP BY uid ORDER BY clicks DESC", array(":time_limit" => $time_limit));
    $database = \Drupal::database();
    $query = $database->select('affiliate_clicks', 'ac');
    $query->innerjoin('users_field_data', 'u');
    $query->innerjoin('affiliate', 'aa');
    $query->condition('u.uid', 'aa.uid', '=');
    $query->condition('u.uid', 'ac.uid', '=');
    $query->condition('aa.active', 1, '=');
    $query->condition('ac.click_time', $time_limit, '>');
    $query->fields('u', ['uid', 'name']);
    $query->fields('aa', ['payouts_owed']);
    $results = $query->execute();

    $results_count = count($results);
    $rows = array();
    $rank = 1;
    if ($results && !empty($results)) {
      foreach ($results as $result) {
        $userdata = unserialize($result->data);
        $homepage = (isset($userdata['affiliate_homepage']) && !empty($userdata['affiliate_homepage'])) ? check_url($userdata['affiliate_homepage']) : '';
        $username = SafeMarkup::checkPlain($result->name);
        $name = (!empty($homepage)) ? Link::fromTextAndUrl($username, $homepage) : Link::fromTextAndUrl($username,  \Drupal::service('path.alias_manager')->getAliasByPath('user/' . $result->uid));
        $rows[] = array(
          array('data' => t('#@rank', array('@rank' => $rank++))),
          array('data' => $name),
        );

      }
    }
    if (empty($rows)) {
      $rows[] = array(array('data' => t('No affiliate activity found for this time period.'), 'colspan' => '2'));
    }

    $output = [
      '#theme' => 'table',
      '#rows' => $rows,
    ];

    if ($results_count && $results_count > $limit) {
      $output .= [
        '#theme' => 'more_link',
        array('url' => \Drupal::service('path.alias_manager')->getAliasByPath('affiliate/top-users'), 'title' => t('More')),
      ];
    }
    return $output;
  }

}
+0 −34
Original line number Diff line number Diff line
@@ -832,37 +832,3 @@ function affiliate_top_users_page() {
  return $output;
}
function affiliate_top_users_block_content() {
  $limit = \Drupal::state()->get('affiliate_module_top_users_count_block', 10);
  $time = time();
  $time_interval = \Drupal::state()->get('affiliate_module_top_users_period_interval', 259200);
  $time_limit = $time - $time_interval;
  $results = \Drupal::database()->query("SELECT COUNT(DISTINCT(ac.clickid)) AS clicks, u.uid AS uid, u.name AS name, u.data AS data, aa.payouts_owed AS payouts_owed FROM {affiliate_clicks} ac INNER JOIN {users} u ON ac.uid = u.uid INNER JOIN {affiliate} aa ON u.uid = aa.uid WHERE aa.active = 1 AND ac.click_time > :time_limit GROUP BY uid ORDER BY clicks DESC", array(":time_limit" => $time_limit));
  $results_count = count($results);
  $rows = array();
  $rank = 1;
  if ($results && !empty($results)) {
    foreach ($results as $result) {
      $userdata = unserialize($result->data);
      $homepage = (isset($userdata['affiliate_homepage']) && !empty($userdata['affiliate_homepage'])) ? check_url($userdata['affiliate_homepage']) : '';
      $username = check_plain($result->name);
      $name = (!empty($homepage)) ? l($username, $homepage) : l($username, drupal_get_path_alias('user/' . $result->uid));
      $rows[] = array(
        array('data' => t('#@rank', array('@rank' => $rank++))),
        array('data' => $name),
      );
      /*
      $rows[] = $name;
      */
    }
  }
  if (empty($rows)) {
    $rows[] = array(array('data' => t('No affiliate activity found for this time period.'), 'colspan' => '2'));
  }
  $output = theme('table', array('rows' => $rows));
  //  $output = theme('item_list', array('items' => $rows, 'title' => '', 'type' => 'ol', 'attributes' => array('class' => 'affiliate-top-users-ol')));
  if ($results_count && $results_count > $limit) {
    $output .= theme('more_link', array('url' => drupal_get_path_alias('affiliate/top-users'), 'title' => t('More')));
  }
  return $output;
}
+5 −4
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;

module_load_include('inc', 'uber_affiliate');

@@ -42,7 +43,7 @@ function uber_affiliate_user_presave(&$edit, $account, $category) {

/**
 * Implements hook_user_view().
 */
 *
function uber_affiliate_user_view($account) {
  $user = \Drupal::currentUser();
  $user->hasPermission('access administration menu');
@@ -59,7 +60,7 @@ function uber_affiliate_user_view($account) {
      $account->content["affiliate"]["affiliate_homepage"] =  array(
        "#type" => "user_profile_item",
        "#title" => t("Web site"),
        "#markup" => l($homepage, $homepage, array("attributes" => array("title" => t("Visit !username's web site", array("!username" => format_username($account)))))),
        "#markup" => l($homepage, $homepage, array("attributes" => array("title" => t("Visit !username's web site", array("!username" => $account))))),
        "#attributes" => array("class" => array("affiliate-profile", "affiliate-profile-homepage")),
      );
    }
@@ -67,12 +68,12 @@ function uber_affiliate_user_view($account) {
      $account->content['affiliate']['affiliate_history'] =  array(
        '#type' => 'user_profile_item',
        '#title' => t('Affiliate history'),
        '#markup' => l(t('View affiliate history'), "user/$account->uid/affiliate", array('attributes' => array('title' => t('View affiliate information and history for !username', array('!username' => format_username($account)))))),
        '#markup' => Link::fromTextAndUrl(t('View affiliate history'), "user/$account->uid/affiliate", array('attributes' => array('title' => t('View affiliate information and history for !username', array('!username' => $account))))),
        '#attributes' => array('class' => array('affiliate-profile', 'affiliate-profile-history')),
      );
    }
  }
}
}*/

/**
 * Implements hook_form_FORM_ID_alter().