Dashboard Subscriber Count

corey34

Active Member
I want to update the query that displays the total subscriber count in the customer dashboard. Currently, the way that count works is it displays all subscribers in all mailing lists, but we bill only on confirmed subscribers, so we'd like the count in the dashboard to display only confirmed subscribers, as well, to keep things consistent for the user.

Currently, we've added a line of code to the DashboardController.php file to make this modification, but obviously we'd prefer to build this as an extension so it doesn't get overwritten in a future upgrade. What's the best way to create the extension? Do I need to replicate the entire getGlanceStats() function in an extension to override the existing query, or is there a way to use an extension to append the line of code in question to the existing query? I'm still getting familiar with the Yii framework in general and the MailWizz framework in particular, so I'd appreciate any input you can offer.

Below is the code in question, with the last line displayed being the line I've added to change the parameters of the query.

PHP:
protected function getGlanceStats()
    {
        $customer    = Yii::app()->customer->getModel();
        $customer_id = (int)$customer->customer_id;
        $cacheKey    = md5('customer.'.$customer_id.'.dashboard.glanceStats');
        $cache       = Yii::app()->cache;

        if (($items = $cache->get($cacheKey))) {
            return $items;
        }
        
        $criteria = new CDbCriteria();
        $criteria->compare('t.customer_id', $customer_id);
        $criteria->addNotInCondition('t.status', array(Lists::STATUS_PENDING_DELETE));

        $subsCriteria = new CDbCriteria();
        $subsCriteria->addInCondition('t.list_id', $customer->getAllListsIdsNotMerged());
        $subsCriteria->addInCondition('t.status', array('confirmed'));
 
@corey34 - i think the best way for this is if i add a hook. SO in your dashboard controllers, both backend and customer area, you have this:
PHP:
// stats
$timelineItems = $this->getTimelineItems();
$glanceStats   = $this->getGlanceStats();

Let's change them like:
PHP:
// stats
$timelineItems = $this->getTimelineItems();

// 1.4.5
$appName     = Yii::app()->apps->getCurrentAppName();
$glanceStats = Yii::app()->hooks->applyFilters($appName . '_dashboard_glance_stats_list', array(), $this);
if (empty($glanceStats)) {
    $glanceStats = $this->getGlanceStats();
}

$keys = array('count', 'heading', 'icon', 'url');
foreach ($glanceStats as $index => $stat) {
    foreach ($keys as $key) {
        if (!array_key_exists($key, $stat)) {
            unset($glanceStats[$index]);
        }
    }  
}
//

Then, you can hook into it from an extension like:
PHP:
// for backend area use backend_dashboard_glance_stats_list instead.
Yii::app()->hooks->addFilter('customer_dashboard_glance_stats_list', function(array $items = array(), $controller){

    $customer    = Yii::app()->customer->getModel();
    $customer_id = (int)$customer->customer_id;
    $cacheKey    = md5('customer.'.$customer_id.'.dashboard.glanceStats');
    $cache       = Yii::app()->cache;

    if (($items = $cache->get($cacheKey))) {
        return $items;
    }

    $criteria = new CDbCriteria();
    $criteria->compare('t.customer_id', $customer_id);
    $criteria->addNotInCondition('t.status', array(Lists::STATUS_PENDING_DELETE));

    $subsCriteria = new CDbCriteria();
    $subsCriteria->addInCondition('t.list_id', $customer->getAllListsIdsNotMerged());
    $subsCriteria->addInCondition('t.status', array('confirmed'));

    $items = array(
        array(
            'count'     => Yii::app()->format->formatNumber(Campaign::model()->count($criteria)),
            'heading'   => Yii::t('dashboard', 'Campaigns'),
            'icon'      => IconHelper::make('ion-ios-email-outline'),
            'url'       => $controller->createUrl('campaigns/index'),
        ),
        array(
            'count'     => Yii::app()->format->formatNumber(Lists::model()->count($criteria)),
            'heading'   => Yii::t('dashboard', 'Lists'),
            'icon'      => IconHelper::make('ion ion-clipboard'),
            'url'       => $controller->createUrl('lists/index'),
        ),
        array(
            'count'     => Yii::app()->format->formatNumber(ListSubscriber::model()->count($subsCriteria)),
            'heading'   => Yii::t('dashboard', 'Subscribers'),
            'icon'      => IconHelper::make('ion-ios-people'),
            'url'       => $controller->createUrl('lists/all_subscribers'),
        ),
        array(
            'count'     => Yii::app()->format->formatNumber(CustomerEmailTemplate::model()->countByAttributes(array('customer_id' => $customer_id))),
            'heading'   => Yii::t('dashboard', 'Templates'),
            'icon'      => IconHelper::make('ion-ios-albums'),
            'url'       => $controller->createUrl('templates/index'),
        ),
    );

    $cache->set($cacheKey, $items, 600);

    return $items;

});

I have added these hooks in the app already so you'll be safe at upcoming upgrades.
 
Back
Top