2014-02-13 65 views
0

我在修改一個Concrete5插件,當用戶的訂閱即將到期時向用戶發送警告郵件。它的工作原理是先篩選出已通知的用戶,然後選擇截至今天的訂閱距離過期日期(expirationDate)爲給定天數(emailOnExpirationDays)的用戶。Concrete5 Filter SQL

我的問題是,我需要能夠通知用戶3次而不是一次。我想在30天前,5天前和1天前通知他們。我的第一個障礙是隻篩選5天內通知的用戶(未在25天前故意通知)。我如何修改下面的PHP和SQL來完成此操作?代碼註釋掉了我嘗試過的東西。有關過濾功能的信息是here

收集在這裏實例:

$expiringTxns = new LMemTxnList(); 
$expiringTxns->filterByProductWithExpirationEmailsThatShouldBeWarned(); 

......

public function filterByProductWithExpirationEmailsThatShouldBeWarned() { 
    $this->setupProductExpirationQueries();//displays all with account 

    //we haven't already sent out an expiration warning or, for that matter, a notice 
    $this->filter('notifiedDate', null); 
    $this->filter('warnedDate', null); //Caitlin commented out to allow more than one warning 
    //$this->filter('warnedDate', '!= BETWEEN DATE_SUB(NOW(), INTERVAL 5 DAY) AND NOW()'); 


    // the emailOnExpirationDays has been properly configured 
    $this->filter('emailOnExpirationDays', null, '!='); 
    $this->filter('emailOnExpirationDays', 0, '>'); 

    //the expirationDate - warning is between 5 days ago (don't want to send a bunch of emails) and now 
    // the warning code should be getting run after the notice code, so we shouldn't have to worry about 3 day warnings and sending warning and notice at the same time 
    $this->filter(null, 'DATE_SUB(expirationDate, INTERVAL emailOnExpirationDays DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 5 DAY) AND NOW()'); 
    //$this->filter(null, 'DATE_SUB(expirationDate, INTERVAL 30 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 5 DAY) AND NOW()'); 

} 


protected function setupProductExpirationQueries() { 
    $this->addToQuery(' INNER JOIN LMemProducts ON txns.productID = LMemProducts.ID'); 

    // the expiration date exists (some products don't expire) 
    $this->filter('expirationDate', null, '!='); 

    // we're supposed to send emails 
    $this->filter('emailOnExpiration', true); 

    $this->filter('emailOnExpirationDays', null, '!='); 
    $this->filter('emailOnExpirationDays', 0, '!='); 
} 

}

回答

0

下面的SQL 應該爲你工作:

(
    (DATE_SUB(expirationDate, 30 DAYS) BETWEEN DATE_SUB(NOW(), 3 DAYS) AND NOW()) 
    OR 
    (DATE_SUB(expirationDate, 5 DAYS) BETWEEN DATE_SUB(NOW(), 3 DAYS) AND NOW()) 
    OR 
    (DATE_SUB(expirationDate, 1 DAYS) BETWEEN DATE_SUB(NOW(), 3 DAYS) AND NOW()) 
) 
AND 
(DATE_ADD(warnedDate, 4 DAYS) <= NOW()) 

基本上,您正在檢查到期日期減去警告期是否在3天前和現在之間。這樣可以使作業中的小(3天)閃爍不再運行。具體來說,如果到期日期是3月15日,它將在2月15日(假設是30天)和2月18日之間返回。但是我們不希望在這段時間內多次發送(如果這項工作可能每天運行一次以上,這將是一個潛在的問題),因此您還要檢查warnedDate是否至少在4天前。這與你的30/5/1時期相得益彰。

要它放入filterBy ...警告()函數,你會刪除$this->filter('warnedDate', null);線,然後一個與DATE_SUB,然後你會添加SQL「直接」在:

$this->filter(null, "(
    (DATE_SUB(expirationDate, 30 DAYS) BETWEEN DATE_SUB(NOW(), 3 DAYS) AND NOW()) 
    OR 
    (DATE_SUB(expirationDate, 5 DAYS) BETWEEN DATE_SUB(NOW(), 3 DAYS) AND NOW()) 
    OR 
    (DATE_SUB(expirationDate, 1 DAYS) BETWEEN DATE_SUB(NOW(), 3 DAYS) AND NOW()) 
) 
AND 
(DATE_ADD(warnedDate, 4 DAYS) <= NOW())"); 
+0

謝謝你的真棒!現在要弄清楚如何測試它。應該很有趣哈哈 – CaitlinHavener