<commit_before><?php

class Newsletter extends DataObject {
	
	/**
	 * Returns a FieldSet with which to create the CMS editing form.
	 * You can use the extend() method of FieldSet to create customised forms for your other
	 * data objects.
	 */
	function getCMSFields($controller = null) {
		require_once("forms/Form.php");

		$group = DataObject::get_by_id("Group", $this->Parent()->GroupID);
		$sent_status_report = $this->renderWith("Newsletter_SentStatusReport");
		$ret = new FieldSet(
			new TabSet("Root",
				$mailTab = new Tab("Newsletter",
					new TextField("Subject", "Subject", $this->Subject),
					new HtmlEditorField("Content", "Content")
				),
				$sentToTab = new Tab("Sent Status Report",
					new LiteralField("Sent Status Report", $sent_status_report)
				)
			)
		);
		
		if( $this->Status != 'Draft' ) {
			$mailTab->push( new ReadonlyField("SendDate", "Sent at", $this->SendDate) );
		} 
		
		
		return $ret;
	}

	/**
	 * Returns a DataObject listing the recipients for the given status for this newsletter
	 *
	 * @param string $result 3 possible values: "Sent", (mail() returned TRUE), "Failed" (mail() returned FALSE), or "Bounced" ({@see $email_bouncehandler}).
	 */
	function SentRecipients($result) {
		$SQL_result = Convert::raw2sql($result);
		return DataObject::get("Newsletter_SentRecipient",array("ParentID='".$this->ID."'", "Result='".$SQL_result."'"));
	}
	
	/**
	 * Returns a DataObjectSet containing the subscribers who have never been sent this Newsletter
	 *
	 */
	function UnsentSubscribers() {
		// Get a list of everyone who has been sent this newsletter
		$sent_recipients = DataObject::get("Newsletter_SentRecipient","ParentID='".$this->ID."'");
		// If this Newsletter has not been sent to anyone yet, $sent_recipients will be null
		if ($sent_recipients != null) {
			$sent_recipients_array = $sent_recipients->toNestedArray('MemberID');
		} else { 
			$sent_recipients_array = array();
		}

		// Get a list of all the subscribers to this newsletter
		$subscribers = DataObject::get( 'Member', "`GroupID`='".$this->Parent()->GroupID."'", null, "INNER JOIN `Group_Members` ON `MemberID`=`Member`.`ID`" );
		// If this Newsletter has no subscribers, $subscribers will be null
		if ($subscribers != null) {
			$subscribers_array = $subscribers->toNestedArray();
		} else { 
			$subscribers_array = array();
		}

		// Get list of subscribers who have not been sent this newsletter:
		$unsent_subscribers_array = array_diff_key($subscribers_array, $sent_recipients_array);

		// Create new data object set containing the subscribers who have not been sent this newsletter:
		$unsent_subscribers = new DataObjectSet();
		foreach($unsent_subscribers_array as $key => $data) {
			$record = array(
				'ID' => $data['ID'],
				'FirstName' => $data['FirstName'],
				'Surname' => $data['Surname'],
				'Email' => $data['Email']
			);
			$unsent_subscribers->push(new ArrayData($record));
		}
	
		return $unsent_subscribers;	<commit_msg>elofgren: Remove 6 lines of code by directly passing the unsent_subscribers_array data to push(new ArrayData?($data)) instead of pointlessly creating a new array.  (merged from branches/gsoc)<commit_after><?php

class Newsletter extends DataObject {
	
	/**
	 * Returns a FieldSet with which to create the CMS editing form.
	 * You can use the extend() method of FieldSet to create customised forms for your other
	 * data objects.
	 */
	function getCMSFields($controller = null) {
		require_once("forms/Form.php");

		$group = DataObject::get_by_id("Group", $this->Parent()->GroupID);
		$sent_status_report = $this->renderWith("Newsletter_SentStatusReport");
		$ret = new FieldSet(
			new TabSet("Root",
				$mailTab = new Tab("Newsletter",
					new TextField("Subject", "Subject", $this->Subject),
					new HtmlEditorField("Content", "Content")
				),
				$sentToTab = new Tab("Sent Status Report",
					new LiteralField("Sent Status Report", $sent_status_report)
				)
			)
		);
		
		if( $this->Status != 'Draft' ) {
			$mailTab->push( new ReadonlyField("SendDate", "Sent at", $this->SendDate) );
		} 
		
		
		return $ret;
	}

	/**
	 * Returns a DataObject listing the recipients for the given status for this newsletter
	 *
	 * @param string $result 3 possible values: "Sent", (mail() returned TRUE), "Failed" (mail() returned FALSE), or "Bounced" ({@see $email_bouncehandler}).
	 */
	function SentRecipients($result) {
		$SQL_result = Convert::raw2sql($result);
		return DataObject::get("Newsletter_SentRecipient",array("ParentID='".$this->ID."'", "Result='".$SQL_result."'"));
	}
	
	/**
	 * Returns a DataObjectSet containing the subscribers who have never been sent this Newsletter
	 *
	 */
	function UnsentSubscribers() {
		// Get a list of everyone who has been sent this newsletter
		$sent_recipients = DataObject::get("Newsletter_SentRecipient","ParentID='".$this->ID."'");
		// If this Newsletter has not been sent to anyone yet, $sent_recipients will be null
		if ($sent_recipients != null) {
			$sent_recipients_array = $sent_recipients->toNestedArray('MemberID');
		} else { 
			$sent_recipients_array = array();
		}

		// Get a list of all the subscribers to this newsletter
		$subscribers = DataObject::get( 'Member', "`GroupID`='".$this->Parent()->GroupID."'", null, "INNER JOIN `Group_Members` ON `MemberID`=`Member`.`ID`" );
		// If this Newsletter has no subscribers, $subscribers will be null
		if ($subscribers != null) {
			$subscribers_array = $subscribers->toNestedArray();
		} else { 
			$subscribers_array = array();
		}

		// Get list of subscribers who have not been sent this newsletter:
		$unsent_subscribers_array = array_diff_key($subscribers_array, $sent_recipients_array);

		// Create new data object set containing the subscribers who have not been sent this newsletter:
		$unsent_subscribers = new DataObjectSet();
		foreach($unsent_subscribers_array as $key => $data) {
			$unsent_subscribers->push(new ArrayData($data));
		}
	
		return $unsent_subscribers;	
{'fraction_non_alphanumeric': 0.13979574753055415, 'fraction_numerical': 0.0008371002846140967, 'mean_word_length': 4.391696750902527, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '1659920', 'n_tokens_mistral': 2098, 'n_tokens_neox': 1820, 'n_words': 522}