/**
	 * _ProcessJob_Send
	 * Send campaign for trigger email
	 *
	 * This function will return an associative array with the following value:
	 * - error => Boolean => Indicates whether or not the function is successful
	 * - halt => Boolean => Indicates whether or not to halt the operation
	 *
	 * @param Array $queue Queue record
	 * @param Array $trigger Trigger record
	 * @param Integer $recipientid Recipient ID
	 * @return Array Returns an array of the status (see comment above)
	 */
	private function _ProcessJob_Send($queue, $trigger, $recipientid)
	{
		$return = array(
			'error' => true,
			'halt' => false
		);

		// ----- Check if trigger has been previously send to the same email recently
			require_once(dirname(__FILE__) . '/subscribers.php');
			$subscribersapi = new Subscribers_API();
			$email = $subscribersapi->GetEmailForSubscriber($recipientid);
			if (!$email) {
				$this->_log('Unable to fetch email address from database');
				return $return;
			}

			$cutoff = (intval(time() / 86400) - 1) * 86400;

			$status = $this->Db->Query("
				SELECT		triggeremailsid
				FROM		[|PREFIX|]triggeremails_log
				WHERE		triggeremailsid = {$trigger['triggeremailsid']}
							AND action = 'send'
							AND timestamp > {$cutoff}
							AND note = '" . $this->Db->Quote($email) . "'
			");
			if (!$status) {
				$this->_log('Unable to check log');
				return $return;
			}

			$row = $this->Db->Fetch($status);
			$this->Db->FreeResult($status);

			// This particular email has been sent to this user before, so do not send it again
			if (!empty($row)) {
				$return['error'] = false;
				return $return;
			}
		// -----

		// Get newsletter record
		$newsletter = $this->_cacheNewsletterGet($trigger['triggeractions']['send']['newsletterid']);
		if ($newsletter === false) {
			$this->_log('Cannot get newsletter record with ID: ' . $trigger['triggeractions']['send']['newsletterid']);
			return $return;
		}

		// Process trigger queue only if newsletter is available and active
		if (empty($newsletter) || !$newsletter['active']) {
			$this->_log('Newsletter is marked as inactive');
			return $return;
		}

		// Send newsletter
		$result = $this->_send($trigger, $queue, $newsletter);

		// If it cannot be sent, return error
		if (!$result['result']) {
			$this->_log('Cannot send newsletter with queueid:' . $queue['queueid'] . ', and recipient:' . $recipientid);
			trigger_error('Trigger job error: Cannot send newsletter with queueid:' . $queue['queueid'] . ', and recipient:' . $recipientid);
			// Log failed sending
			$this->RecordLogActions($trigger['triggeremailsid'], $recipientid, 'send_failed', $result['email']);
            $return['halt'] = true;
			return $return;
		}

		// Record log
		if (!$this->RecordLogActions($trigger['triggeremailsid'], $recipientid, 'send', $result['email'])) {
			$this->_log('Cannot write log to the database... exitting');
			$return['halt'] = true;
			return $return;
		}

		// ----- Record statistic
			$tempFormat = $newsletter['format'];
			if ($tempFormat == 'b') {
				$tempFormat = 'm';
			}

			if ($this->_statsAPIObject->UpdateRecipient($trigger['statid'], $tempFormat) === false) {
				// Commit the transaction, as we do still want the record send to be recorded.
				$this->Db->CommitTransaction();
				$this->_log('Cannot update statistics... Exitting');
				$return['halt'] = true;
				return $return;
			}
		// -----

		$this->_log('Newsletter is sent (queueid:' . $queue['queueid'] . ', and recipient:' . $recipientid . ')');

		// ----- Trigger Event
			$tempEventData = new EventData_IEM_JOBSTRIGGEREMAILSAPI_PROCESSJOBSEND();
			$tempEventData->emailsent = true;
			$tempEventData->newsletter = $newsletter;
			$tempEventData->subscriberid = $recipientid;
			$tempEventData->listid = $result['listid'];
			$tempEventData->triggerrecord = $trigger;
			$tempEventData->trigger();

			unset($tempEventData);
		// -----

		$return['error'] = false;
		$return['halt'] = false;
		return $return;
	}
Esempio n. 2
0
	/**
	 * _getSubscriberIDSFromList
	 * Get all subsriber IDs with the same email address within a list
	 *
	 * @param Integer $subscriberid Subscriber ID
	 * @param Array $lists An array of list IDs
	 *
	 * @return Array|FALSE an array of return records that can be used to pass through Api::AddToQueue(), FALSE otherwise
	 */
	private function _getSubscriberIDSFromList($subscriberid, $lists)
	{
		if (!is_array($lists) || empty($lists)) {
			return array(array('subscriberid' => $subscriberid));
		}

		require_once(dirname(__FILE__) . '/subscribers.php');
		$subscribersapi = new Subscribers_API();

		$emailaddress = $subscribersapi->GetEmailForSubscriber($subscriberid);
		if (!$emailaddress) {
			return array(array('subscriberid' => $subscriberid));
		}

		$records = $subscribersapi->GetAllListsForEmailAddress($emailaddress, $lists);

		if (empty($records)) {
			return array(array('subscriberid' => $subscriberid));
		}

		return $records;
	}