<commit_before><?php

/**
 * In this file we store all generic functions that we will be using to communicate with CampaignMonitor
 *
 * @author <NAME> <<EMAIL>>
 */
class FrontendMailmotorCMHelper
{
	/**
	 * Checks if a group exists by its CampaignMonitor ID
	 *
	 * @param string $id The id of the group on Campaign Monitor.
	 * @return bool
	 */
	public static function existsGroupByCampaignMonitorID($id)
	{
		return (bool) FrontendModel::getDB()->getVar(
			'SELECT COUNT(mg.id)
			 FROM mailmotor_groups AS mg
			 INNER JOIN mailmotor_campaignmonitor_ids AS mci ON mci.other_id = mg.id
			 WHERE mci.cm_id = ? AND mci.type = ?',
			array($id, 'list')
		);
	}

	/**
	 * Inserts a record into the mailmotor_campaignmonitor_ids table
	 *
	 * @param string $type The type for the item.
	 * @param string $otherId The id of the item.
	 * @return string
	 */
	public static function getCampaignMonitorID($type, $otherId)
	{
		return FrontendModel::getDB()->getVar(
			'SELECT cm_id
			 FROM mailmotor_campaignmonitor_ids
			 WHERE type = ? AND other_id = ?',
			array($type, $otherId)
		);
	}

	/**
	 * Returns the client ID from the settings
	 *
	 * @return string
	 */
	public static function getClientID()
	{
		return (string) FrontendModel::getModuleSetting('mailmotor', 'cm_client_id');
	}

	/**
	 * Returns the CampaignMonitor object
	 *
	 * @param int[optional] $listId The default list id to use.
	 * @return CampaignMonitor
	 */
	public static function getCM($listId = null)
	{
		// campaignmonitor reference exists
		if(!Spoon::exists('campaignmonitor'))
		{
			// check if the CampaignMonitor class exists
			if(!SpoonFile::exists(PATH_LIBRARY . '/external/campaignmonitor.php'))
			{
				// the class doesn't exist, so throw an exception
				throw new SpoonFileException('The CampaignMonitor wrapper class is not found. Please locate and place it in /library/external');
			}

			// require CampaignMonitor class
			require_once 'external/campaignmonitor.php';

			// set login data
			$url = FrontendModel::getModuleSetting('mailmotor', 'cm_url');
			$username = FrontendModel::getModuleSetting('mailmotor', 'cm_username');
			$password = FrontendModel::getModuleSetting('mailmotor', 'cm_password');

			// init CampaignMonitor object
			$cm = new CampaignMonitor($url, $username, $password, 5, self::getClientId());

			// set CampaignMonitor object reference
			Spoon::set('campaignmonitor', $cm);

			// get the default list ID
			$listId = (!empty($listId)) ? $listId : self::getDefaultListID();

			// set the default list ID
			$cm->setListId($listId);
		}

		// return the CampaignMonitor object
		return Spoon::get('campaignmonitor');
	}

	/**
	 * Returns the default list ID
	 *
	 * @return string
	 */
	public static function getDefaultListID()
	{
		// fetch default group ID
		$groupId = FrontendMailmotorModel::getDefaultGroupID();

		// fetch the CM ID for this group
		return self::getCampaignMonitorID('list', $groupId);
	}

	/**
	 * Subscribes an e-mail address and send him/her to CampaignMonitor
	 *
	 * @param string $email The e-mail address to subscribe.
	 * @param string[optional] $groupId The id of the group to subscribe to.
	 * @return bool
	 */
	public static function subscribe($email, $groupId = null)
	{
		// get objects
		$db = FrontendModel::getDB(true);
		$cm = self::getCM();

		// set groupID
		$groupId = !empty($groupId) ? $groupId : FrontendMailmotorModel::getDefaultGroupID();

		// group ID found
		if(FrontendMailmotorModel::existsGroup($groupId) && $cm->subscribe($email, $email))
		{
			// set variables
			$subscriber['email'] = $email;
			$subscriber['source'] = 'website';
			$subscriber['created_on'] = FrontendModel::getUTCDate('Y-m-d H:i:s');

			// insert/update the user
			$db->execute(
				'INSERT INTO mailmotor_addresses(email, source, created_on)
				 VALUES (?, ?, ?)
				 ON DUPLICATE KEY UPDATE source = ?, created_on = ?',
				array(
					$subscriber['email'],
					$subscriber['source'],
					$subscriber['created_on'],
					$subscriber['source'],
					$subscriber['created_on']
				)
			);<commit_msg>subscribe to the given groupId<commit_after><?php

/**
 * In this file we store all generic functions that we will be using to communicate with CampaignMonitor
 *
 * @author <NAME> <<EMAIL>>
 */
class FrontendMailmotorCMHelper
{
	/**
	 * Checks if a group exists by its CampaignMonitor ID
	 *
	 * @param string $id The id of the group on Campaign Monitor.
	 * @return bool
	 */
	public static function existsGroupByCampaignMonitorID($id)
	{
		return (bool) FrontendModel::getDB()->getVar(
			'SELECT COUNT(mg.id)
			 FROM mailmotor_groups AS mg
			 INNER JOIN mailmotor_campaignmonitor_ids AS mci ON mci.other_id = mg.id
			 WHERE mci.cm_id = ? AND mci.type = ?',
			array($id, 'list')
		);
	}

	/**
	 * Inserts a record into the mailmotor_campaignmonitor_ids table
	 *
	 * @param string $type The type for the item.
	 * @param string $otherId The id of the item.
	 * @return string
	 */
	public static function getCampaignMonitorID($type, $otherId)
	{
		return FrontendModel::getDB()->getVar(
			'SELECT cm_id
			 FROM mailmotor_campaignmonitor_ids
			 WHERE type = ? AND other_id = ?',
			array($type, $otherId)
		);
	}

	/**
	 * Returns the client ID from the settings
	 *
	 * @return string
	 */
	public static function getClientID()
	{
		return (string) FrontendModel::getModuleSetting('mailmotor', 'cm_client_id');
	}

	/**
	 * Returns the CampaignMonitor object
	 *
	 * @param int[optional] $listId The default list id to use.
	 * @return CampaignMonitor
	 */
	public static function getCM($listId = null)
	{
		// campaignmonitor reference exists
		if(!Spoon::exists('campaignmonitor'))
		{
			// check if the CampaignMonitor class exists
			if(!SpoonFile::exists(PATH_LIBRARY . '/external/campaignmonitor.php'))
			{
				// the class doesn't exist, so throw an exception
				throw new SpoonFileException('The CampaignMonitor wrapper class is not found. Please locate and place it in /library/external');
			}

			// require CampaignMonitor class
			require_once 'external/campaignmonitor.php';

			// set login data
			$url = FrontendModel::getModuleSetting('mailmotor', 'cm_url');
			$username = FrontendModel::getModuleSetting('mailmotor', 'cm_username');
			$password = FrontendModel::getModuleSetting('mailmotor', 'cm_password');

			// init CampaignMonitor object
			$cm = new CampaignMonitor($url, $username, $password, 5, self::getClientId());

			// set CampaignMonitor object reference
			Spoon::set('campaignmonitor', $cm);

			// get the default list ID
			$listId = (!empty($listId)) ? $listId : self::getDefaultListID();

			// set the default list ID
			$cm->setListId($listId);
		}

		// return the CampaignMonitor object
		return Spoon::get('campaignmonitor');
	}

	/**
	 * Returns the default list ID
	 *
	 * @return string
	 */
	public static function getDefaultListID()
	{
		// fetch default group ID
		$groupId = FrontendMailmotorModel::getDefaultGroupID();

		// fetch the CM ID for this group
		return self::getCampaignMonitorID('list', $groupId);
	}

	/**
	 * Subscribes an e-mail address and send him/her to CampaignMonitor
	 *
	 * @param string $email The e-mail address to subscribe.
	 * @param string[optional] $groupId The id of the group to subscribe to.
	 * @return bool
	 */
	public static function subscribe($email, $groupId = null)
	{
		// get objects
		$db = FrontendModel::getDB(true);
		$cm = self::getCM();

		// set groupID
		$groupId = !empty($groupId) ? $groupId : FrontendMailmotorModel::getDefaultGroupID();
		
		// get campaign monitor list id
		$listId = self::getCampaignMonitorID('list', $groupId);

		// group ID found
		if(FrontendMailmotorModel::existsGroup($groupId) && $cm->subscribe($email, $email, array(), true, $listId))
		{
			// set variables
			$subscriber['email'] = $email;
			$subscriber['source'] = 'website';
			$subscriber['created_on'] = FrontendModel::getUTCDate('Y-m-d H:i:s');

			// insert/update the user
			$db->execute(
				'INSERT INTO mailmotor_addresses(email, source, created_on)
				 VALUES (?, ?, ?)
				 ON DUPLICATE KEY UPDATE source = ?, created_on = ?',
				array(
					$subscriber['email'],
					$subscriber['source'],
					$subscriber['created_on'],
					$subscriber['source'],
					$subscriber['created_on']
				)
			);
{'fraction_non_alphanumeric': 0.12849903474903476, 'fraction_numerical': 0.00024131274131274132, 'mean_word_length': 3.755593803786575, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2880089', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2462, 'n_words': 721}