示例#1
0
	/**
	 * _GetSearchInfo
	 * Get search info for segment.
	 *
	 * @param Mixed $segments IDs of the segments (Integer|Array)
	 * @return Mixed Returns an array of list id used by segment, FALSE otherwise
	 *
	 * @access private
	 */
	function _GetSearchInfo($segments)
	{
		if (!is_array($segments)) {
			$segments = array($segments);
		}

		$lists = array();
		$rules = array();
		$api = new Segment_API();
		foreach ($segments as $id) {
			$status = $api->Load(intval($id));
			if ($status === false) {
				return false;
			}

			$temp = $api->GetMailingListUsed();
			if (is_array($temp) && !is_null($temp)) {
				$lists = array_merge($lists, $temp);
			}

			$temp = $api->GetRules();
			if (is_array($temp) && !is_null($temp)) {
				$temp = array('type' => 'group', 'connector' => 'or', 'rules' => $temp);
				$rules = array_merge($rules, array($temp));
			}
		}
		unset($api);

		return array('Lists' => $lists, 'Rules' => $rules);
	}
	/**
	 * GetSubscribersFromSegment
	 * Get subscribers from segment. This is esentially the same functions as Subscribers::GetSubscribers(),
	 * but it uses segment's information to fetch subscribers instead of search filter.
	 *
	 * @param Array $segmentIDs An array of segments that we want to fetch subscribers ID from
	 * @param Boolean $countonly Whether to only do a count or get the list of subscribers as well.
	 * @param Array $queuedetails If this is not an empty array, the subscribers returned from the query are put directly into this queue (based on the array fields).
	 *
	 * @return Mixed This will return the count only if that is set to true. Otherwise this will return an array of data including the count and the subscriber list.
	 *
	 * @uses SENDSTUDIO_TABLEPREFIX
	 * @uses Segment_API
	 * @uses Segment_API::Load()
	 * @uses Segment_API::GetSearchInfo()
	 * @uses Segment_API::ReplaceLists()
	 * @uses Segment_API::ReplaceRules()
	 * @uses Segment_API::GetSubscribersCount()
	 * @uses Segment_API::GetSubscribersQueryString()
	 * @uses Db::Query()
	 * @uses Db::FetchOne()
	 * @uses Db::Fetch()
	 */
	function GetSubscribersFromSegment($segmentIDs, $countonly = false, $queuedetails = null, $sortdetails = array())
	{
		$return = array('count' => 0, 'subscriberlist' => 0, 'lists' => array());

		if (empty($sortdetails)) {
			$sortdetails = array('SortBy' => 'emailaddress', 'Direction' => 'asc', 'Max' => 100);
		}

		require_once(dirname(__FILE__) . '/segment.php');

		$count = 0;
		$lists = array();
		$selectQueries = array();
		foreach ($segmentIDs as $id) {
			$segmentAPI = new Segment_API();

			// Cannot load segment
			$status = $segmentAPI->Load($id);
			if (!$status) {
				return array();
			}

			// Get lists that are used in this segment
			$tempLists = $segmentAPI->GetMailingListUsed();
			$lists = array_merge($lists, $tempLists);

			// Get count
			$count += $segmentAPI->GetSubscribersCount(0, true);

			// Get query
			$tempQuery = $segmentAPI->GetSubscribersQueryString(true);
			$selectQueries[] = preg_replace('/^SELECT .*? FROM?/i', '', $tempQuery);
		}
		unset($segmentAPI);

		checksize($count, (isset($sortdetails['Max'])? $sortdetails['Max'] : 100), $countonly);
		$return['count'] = $count;
		$return['lists'] = $lists;

		if (empty($selectQueries)) {
			return array();
		}

		if ($countonly) {
			return $return;
		}

		if (empty($queuedetails)) {
			$temp = 'SELECT DISTINCT subscribers.subscriberid AS subscriberid FROM ';
			$selectQuery = $temp . implode(" UNION {$temp}", $selectQueries);
		} else {
			$queueID = intval($queuedetails['queueid']);
			$queueType = $this->Db->Quote($queuedetails['queuetype']);
			$queueOwnerID = intval($queuedetails['ownerid']);

			$temp = "SELECT DISTINCT {$queueID}, '{$queueType}', {$queueOwnerID}, subscribers.subscriberid, 0 FROM ";

			$selectQuery = 'INSERT INTO [|PREFIX|]queues (queueid, queuetype, ownerid, recipient, processed)';
			$selectQuery .= $temp . implode(" UNION {$temp}", $selectQueries);
		}

		setmax($sortdetails, $selectQuery);
		$selectQuery = preg_replace('/l.subscribedate/', 'subscribers.subscribedate', $selectQuery);

		$search_result = $this->Db->Query($selectQuery);
		if (!$search_result) {
			trigger_error(__CLASS__ . '::' . __METHOD__ . " -- Unable to query database with the following query string: {$selectQuery}", E_USER_NOTICE);
			return array();
		}

		if (!empty($queuedetails)) {
			return array();
		}

		$subscriber_results = array();
		while ($row = $this->Db->Fetch($search_result)) {
			$subscriber_results[] = $row;
		}
		$return['subscriberlist'] = $subscriber_results;

		return $return;
	}