<commit_before><?php

/**
 * Newsletter administration section
 *
 * @package newsletter
 */

class NewsletterAdmin extends ModelAdmin {

	static $url_segment = 'newsletter';
	static $menu_title = 'Newsletter';

	public static $managed_models = array(
		"Newsletter" => array('title' => 'Newsletters'),
		"Newsletter_Sent" => array('title' => 'Sent Newsletters'),
		"MailingList" => array('title' => 'Mailing Lists'),
		"Recipient" => array('title' => 'All Recipients')
	);

	// We keep the legacy code below there for developing purposes, 
	// it might be helpful for us to copy / paste. we should clean it up before 1.0.1 release.

	/** 
	 * @var which will be used to seperator "send items" into 2 groups, e.g. "most recent number 5", "older". 
	 */
	static $most_recent_seperator = 5;
	
	/** 
	 * @var array Array of template paths to check 
	 */	
	static $template_paths = null; //could be customised in _config.php

	public function init() {
		parent::init();
		Requirements::javascript(CMS_DIR . '/javascript/SilverStripeNavigator.js');
		Requirements::css('newsletter/css/NewsletterAdmin.css');

	}

	public function getEditForm($id = null, $fields = null) {
		$form = parent::getEditForm($id, $fields);

		//custom handling of the newsletter modeladmin with a specialized action menu for the detail form
		if ($this->modelClass == "Newsletter" || $this->modelClass == "Newsletter_Sent") {
			$config = $form->Fields()->first()->getConfig();
			$config->removeComponentsByType('GridFieldDetailForm')
				->addComponents(new NewsletterGridFieldDetailForm());
			if ($this->modelClass == "Newsletter_Sent") {
				$config->removeComponentsByType('GridFieldAddNewButton');
			}
			$config->getComponentByType('GridFieldDataColumns')
				->setFieldCasting(array(
					"Template" => "Boolean->Nice",
					"Content" => "HTMLText->LimitSentences",
			));
		}
		if($this->modelClass == "Recipient") {
			$config = $form->Fields()->first()->getConfig();
			$config->removeComponentsByType('GridFieldDetailForm')
				->addComponents(new RecipientGridFieldDetailForm());
			$config->getComponentByType('GridFieldDataColumns')
				->setFieldCasting(array(
					"Blacklisted" => "Boolean->Nice",
					"Verified" => "Boolean->Nice",
			));
		}
		return $form;
	}

	/**
	 * looked-up the email template_paths. 
	 * if not set, will look up both theme folder and project folder
	 * in both cases, email folder exsits or Email folder exists
	 * return an array containing all folders pointing to the bunch of email templates
	 *
	 * @return array
	 */
	public static function template_paths() {
		if(!isset(self::$template_paths)) {
			if(class_exists('SiteConfig') && ($config = SiteConfig::current_site_config()) && $config->Theme) {
				$theme = $config->Theme;
			} elseif(SSViewer::current_custom_theme()) {
				$theme = SSViewer::current_custom_theme();
			} else if(SSViewer::current_theme()){
				$theme = SSViewer::current_theme();
			} else {
				$theme = false;
			}

			if($theme) {
				if(file_exists("../".THEMES_DIR."/".$theme."/templates/email")){
					self::$template_paths[] = THEMES_DIR."/".$theme."/templates/email";
				}
				
				if(file_exists("../".THEMES_DIR."/".$theme."/templates/Email")){
					self::$template_paths[] = THEMES_DIR."/".$theme."/templates/Email";
				}
			}

			$project = project();
			
			if(file_exists("../". $project . '/templates/email')){
				self::$template_paths[] = $project . '/templates/email';
			}
			
			if(file_exists("../". $project . '/templates/Email')){
				self::$template_paths[] = $project . '/templates/Email';
			}
		}
		else {
			if(is_string(self::$template_paths)) {
				self::$template_paths = array(self::$template_paths);
			}
		}
		return self::$template_paths;
	}

	public function getList() {
		$list = parent::getList();
		if ($this->modelClass == "Newsletter"){
			$statusFilter = array("Draft", "Sending");

			//using a editform detail request, that should allow Newsletter_Sent objects as well as regular Newsletters
			if (!empty($_REQUEST['url'])) {
				if (strpos($_REQUEST['url'],'/EditForm/field/Newsletter/item/') !== false) {
					$statusFilter[] = "Sent";
				}
			}

		} else {
			$statusFilter = array("Sent");
		}

		$list->addFilter(array("Status" => $statusFilter));

		return $list;
	}

	/**
	 * @return SearchContext<commit_msg>BUG fixing filtering so it only applies for Newsletter and Newsletter_Sent classes in the model admin<commit_after><?php

/**
 * Newsletter administration section
 *
 * @package newsletter
 */

class NewsletterAdmin extends ModelAdmin {

	static $url_segment = 'newsletter';
	static $menu_title = 'Newsletter';

	public static $managed_models = array(
		"Newsletter" => array('title' => 'Newsletters'),
		"Newsletter_Sent" => array('title' => 'Sent Newsletters'),
		"MailingList" => array('title' => 'Mailing Lists'),
		"Recipient" => array('title' => 'All Recipients')
	);

	// We keep the legacy code below there for developing purposes, 
	// it might be helpful for us to copy / paste. we should clean it up before 1.0.1 release.

	/** 
	 * @var which will be used to seperator "send items" into 2 groups, e.g. "most recent number 5", "older". 
	 */
	static $most_recent_seperator = 5;
	
	/** 
	 * @var array Array of template paths to check 
	 */	
	static $template_paths = null; //could be customised in _config.php

	public function init() {
		parent::init();
		Requirements::javascript(CMS_DIR . '/javascript/SilverStripeNavigator.js');
		Requirements::css('newsletter/css/NewsletterAdmin.css');

	}

	public function getEditForm($id = null, $fields = null) {
		$form = parent::getEditForm($id, $fields);

		//custom handling of the newsletter modeladmin with a specialized action menu for the detail form
		if ($this->modelClass == "Newsletter" || $this->modelClass == "Newsletter_Sent") {
			$config = $form->Fields()->first()->getConfig();
			$config->removeComponentsByType('GridFieldDetailForm')
				->addComponents(new NewsletterGridFieldDetailForm());
			if ($this->modelClass == "Newsletter_Sent") {
				$config->removeComponentsByType('GridFieldAddNewButton');
			}
			$config->getComponentByType('GridFieldDataColumns')
				->setFieldCasting(array(
					"Template" => "Boolean->Nice",
					"Content" => "HTMLText->LimitSentences",
			));
		}
		if($this->modelClass == "Recipient") {
			$config = $form->Fields()->first()->getConfig();
			$config->removeComponentsByType('GridFieldDetailForm');
			$config->getComponentByType('GridFieldDataColumns')
				->setFieldCasting(array(
					"Blacklisted" => "Boolean->Nice",
					"Verified" => "Boolean->Nice",
			));
		}
		return $form;
	}

	/**
	 * looked-up the email template_paths. 
	 * if not set, will look up both theme folder and project folder
	 * in both cases, email folder exsits or Email folder exists
	 * return an array containing all folders pointing to the bunch of email templates
	 *
	 * @return array
	 */
	public static function template_paths() {
		if(!isset(self::$template_paths)) {
			if(class_exists('SiteConfig') && ($config = SiteConfig::current_site_config()) && $config->Theme) {
				$theme = $config->Theme;
			} elseif(SSViewer::current_custom_theme()) {
				$theme = SSViewer::current_custom_theme();
			} else if(SSViewer::current_theme()){
				$theme = SSViewer::current_theme();
			} else {
				$theme = false;
			}

			if($theme) {
				if(file_exists("../".THEMES_DIR."/".$theme."/templates/email")){
					self::$template_paths[] = THEMES_DIR."/".$theme."/templates/email";
				}
				
				if(file_exists("../".THEMES_DIR."/".$theme."/templates/Email")){
					self::$template_paths[] = THEMES_DIR."/".$theme."/templates/Email";
				}
			}

			$project = project();
			
			if(file_exists("../". $project . '/templates/email')){
				self::$template_paths[] = $project . '/templates/email';
			}
			
			if(file_exists("../". $project . '/templates/Email')){
				self::$template_paths[] = $project . '/templates/Email';
			}
		}
		else {
			if(is_string(self::$template_paths)) {
				self::$template_paths = array(self::$template_paths);
			}
		}
		return self::$template_paths;
	}

	public function getList() {
		$list = parent::getList();
		if ($this->modelClass == "Newsletter" || $this->modelClass == "Newsletter_Sent" ){
			if ($this->modelClass == "Newsletter") {
				$statusFilter = array("Draft", "Sending");

				//using a editform detail request, that should allow Newsletter_Sent objects as well as regular Newsletters
				if (!empty($_REQUEST['url'])) {
					if (strpos($_REQUEST['url'],'/EditForm/field/Newsletter/item/') !== false) {
						$statusFilter[] = "Sent";
					}
				}
			} else {
				$statusFilter = array("Sent");
			}

			$list->addFilter(array("Status" => $statusFilter));
		}



		return $list;
	}

	/**
	 * @return SearchContext
{'fraction_non_alphanumeric': 0.16816885339418142, 'fraction_numerical': 0.0013690815744438105, 'mean_word_length': 3.9330332020258862, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 84, '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': '5610526', 'n_tokens_mistral': 3150, 'n_tokens_neox': 2692, 'n_words': 609}