/** * Generates events for one subscription * @param AW_Sarp_Model_Subscription $Subscription * @param object $woDelete [optional] * @return AW_Sarp_Model_Alert */ public function generateSubscriptionEvents(AW_Sarp_Model_Subscription $Subscription, $woDelete = null) { // Purge events before if (!$Subscription->getId()) { return $this; } if (!$woDelete) { if ($Subscription->getIsNew() && $this->getType() == self::TYPE_NEW_SUBSCRIPTION) { $this->getResource()->getWriteAdapter()->delete($this->getResource()->getTable('sarp/alert_event'), 'subscription_id=' . $Subscription->getId() . " AND alert_id=" . $this->getId()); } elseif ($this->getType() == self::TYPE_DATE_EXPIRE) { $_alertCollection = Mage::getModel('sarp/alert_event')->getCollection(); $_alertCollection->getSelect()->where('subscription_id = ?', $Subscription->getId())->where('alert_id = ?', $this->getId()); $_alertCollection->load(); if (sizeof($_alertCollection) > 0) { return $this; } } elseif ($this->getType() != self::TYPE_NEW_SUBSCRIPTION) { $this->getResource()->getWriteAdapter()->delete($this->getResource()->getTable('sarp/alert_event'), 'subscription_id=' . $Subscription->getId() . " AND alert_id=" . $this->getId()); } } // Don't generate events for disabled alerts if ($this->getStatus() == self::STATUS_DISABLED) { return $this; } // Generate dates $suffix = $this->getTimeIsAfter() ? 1 : -1; $timeAmount = $suffix * $this->getTimeMultiplier() * $this->getTimeAmount(); // hours switch ($this->getType()) { case self::TYPE_DATE_START: if ($Subscription->isActive()) { $date = $Subscription->getDateStart(); $date = $this->_getDate($timeAmount, $date); } break; case self::TYPE_DATE_EXPIRE: if (($Subscription->getIsExpiring() || $Subscription->isActive()) && !$Subscription->isInfinite()) { $date = $Subscription->getDateExpire(); $date = $this->_getDate($timeAmount, $date); } break; case self::TYPE_DELIVERY: if ($Subscription->isActive()) { $date = $Subscription->getFlatNextDeliveryDate(); $date = new Zend_Date($date, AW_Sarp_Model_Subscription::DB_DATE_FORMAT); $date = $this->_getDate($timeAmount, $date); } break; case self::TYPE_NEW_SUBSCRIPTION: if ($Subscription->getIsNew()) { $date = new Zend_Date(); $date = $this->_getDate($timeAmount, $date); } else { } break; case self::TYPE_SUSPENDED: if ($Subscription->getIsSuspending()) { $date = new Zend_Date(); $date = $this->_getDate($timeAmount, $date); } else { } break; case self::TYPE_ACTIVATION: if ($Subscription->getIsReactivated()) { $date = new Zend_Date(); $date = $this->_getDate($timeAmount, $date); } else { } break; case self::TYPE_CANCEL_SUBSCRIPTION: if ($Subscription->getIsCancelling()) { $date = new Zend_Date(); $date = $this->_getDate($timeAmount, $date); } else { } break; default: throw new AW_Sarp_Exception("Alert for events type '{$this->getType()}' are not supported"); } if (isset($date) && $date instanceof Zend_Date && $date->compare(new Zend_Date()) > 0) { $Event = Mage::getModel('sarp/alert_event')->setSubscriptionId($Subscription->getId())->setRecipient($this->getRecipientAddress($Subscription))->setAlertId($this->getId())->setDate($date->toString(AW_Sarp_Model_Subscription::DB_DATETIME_FORMAT))->save(); } return $this; }