Merge pull request #1728 from mailpoet/stats-notifications
Stats notifications [ MAILPOET-1571]
This commit is contained in:
@ -5,4 +5,23 @@ if(!defined('ABSPATH')) exit;
|
||||
|
||||
class NewsletterLink extends Model {
|
||||
public static $_table = MP_NEWSLETTER_LINKS_TABLE;
|
||||
|
||||
static function findTopLinkForNewsletter(Newsletter $newsletter) {
|
||||
$link = self::selectExpr('links.*')
|
||||
->selectExpr('count(*)', 'clicksCount')
|
||||
->tableAlias('links')
|
||||
->innerJoin(StatisticsClicks::$_table,
|
||||
array('clicks.link_id', '=', 'links.id'),
|
||||
'clicks')
|
||||
->where('newsletter_id', $newsletter->id())
|
||||
->groupBy('links.id')
|
||||
->orderByDesc('clicksCount')
|
||||
->limit(1)
|
||||
->findOne();
|
||||
if(!$link) {
|
||||
return null;
|
||||
}
|
||||
return $link;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -37,6 +37,15 @@ class ScheduledTask extends Model {
|
||||
);
|
||||
}
|
||||
|
||||
/** @return StatsNotification */
|
||||
function statsNotification() {
|
||||
return $this->hasOne(
|
||||
StatsNotification::class,
|
||||
'task_id',
|
||||
'id'
|
||||
);
|
||||
}
|
||||
|
||||
function pause() {
|
||||
$this->set('status', self::STATUS_PAUSED);
|
||||
$this->save();
|
||||
|
42
lib/Models/StatsNotification.php
Normal file
42
lib/Models/StatsNotification.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Models;
|
||||
|
||||
class StatsNotification extends Model {
|
||||
public static $_table = MP_STATS_NOTIFICATIONS_TABLE;
|
||||
|
||||
/** @return Newsletter */
|
||||
public function newsletter() {
|
||||
return $this->hasOne(
|
||||
Newsletter::class,
|
||||
'id',
|
||||
'newsletter_id'
|
||||
);
|
||||
}
|
||||
|
||||
/** @return StatsNotification */
|
||||
static function createOrUpdate($data = array()) {
|
||||
$model = null;
|
||||
|
||||
if(isset($data['id']) && (int)$data['id'] > 0) {
|
||||
$model = static::findOne((int)$data['id']);
|
||||
}
|
||||
|
||||
if(!$model && isset($data['task_id']) && $data['newsletter_id']) {
|
||||
$model = self::where('newsletter_id', $data['newsletter_id'])
|
||||
->where('task_id', $data['task_id'])
|
||||
->findOne();
|
||||
}
|
||||
|
||||
if(!$model) {
|
||||
$model = static::create();
|
||||
$model->hydrate($data);
|
||||
} else {
|
||||
unset($data['id']);
|
||||
$model->set($data);
|
||||
}
|
||||
|
||||
return $model->save();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user