<commit_before><?php
/**
 * Provides an interface for attaching multiple files associated with
 * a Page or DataObject. Files can be chosen from exting assets in {@link KickAssetAdmin}
 *
 * @package KickAssets
 * @author UncleCheese <<EMAIL>>
 */

class MultipleFileAttachmentField extends KickAssetField {


	
	/**
	 * @var boolean A simple template variable that states whether this is a multiple
	 *	 		upload field or not
	 */	
	public $Multi = true;
	
	

	/**
	 * @var string The template used to render the file list
	 */
	public $AttachedFilesTemplate = "KickAssetFieldFiles_Multi";	
	


	/**
	 * Sets the value of the form field based on data from the request. Gets the list
	 * of file IDs by running the $name() against the object. File relation can be
	 * has_many or many_many.
	 *
	 * @param string The value to set
	 * @param array The array of form data
	 */	
	public function setValue($value = null, $data = null) {
		if(!is_array($value)) {
			if(!$value && $data && $data instanceof DataObject && $data->hasMethod($this->name)) {
				$funcName = $this->name;
				if($obj = $data->$funcName()) {
					if($obj instanceof DataObjectSet) {
						$value = $obj->column('ID');
					}
				}
			}
		}
		parent::setValue($value, $data);
	}



	/**
	 * Refreshes the file list. If passed an array of IDs in the request, 
	 * it augments the list with those files.
	 *
	 * @param SS_HTTPRequest
	 * @return SSViewer
	 */	
	public function refresh(SS_HTTPRequest $r) {
		if($r->requestVar('ids')) {
			$ids = $r->requestVar('ids');
			$files = new DataObjectSet();
			if($set = DataObject::get("File", "\"File\".ID IN (".implode(',',$ids).")")) {
				foreach($set as $file) {
					$this->processFile($file);
					$files->push($file);					
				}
				$files->merge($this->Files());
			}
			else {
				die("File $id doesn't exist");
			}
		}
		else {
			$files = $this->Files();
		}
		return $this->customise(array(
			'Files' => $files
		))->renderWith($this->AttachedFilesTemplate);
	}



	/**
	 * Gets all the attached files.
	 *
	 * @return DataObjectSet
	 */
	public function Files() {
		if($val = $this->Value()) {
			if(is_array($val)) {
				$list = implode(',', $val);
				if($files = DataObject::get("File", "\"File\".\"ID\" IN (".Convert::raw2sql($list).")")) {
					$ret = new DataObjectSet();
					foreach($files as $file) {
						$this->processFile($file);
						$ret->push($file);
					}
					return $ret;<commit_msg>BUGFIX: MultipleFileAttachmentField showing duplicates after subsequent attachments<commit_after><?php
/**
 * Provides an interface for attaching multiple files associated with
 * a Page or DataObject. Files can be chosen from exting assets in {@link KickAssetAdmin}
 *
 * @package KickAssets
 * @author UncleCheese <<EMAIL>>
 */

class MultipleFileAttachmentField extends KickAssetField {


	
	/**
	 * @var boolean A simple template variable that states whether this is a multiple
	 *	 		upload field or not
	 */	
	public $Multi = true;
	
	

	/**
	 * @var string The template used to render the file list
	 */
	public $AttachedFilesTemplate = "KickAssetFieldFiles_Multi";	
	


	/**
	 * Sets the value of the form field based on data from the request. Gets the list
	 * of file IDs by running the $name() against the object. File relation can be
	 * has_many or many_many.
	 *
	 * @param string The value to set
	 * @param array The array of form data
	 */	
	public function setValue($value = null, $data = null) {
		if(!is_array($value)) {
			if(!$value && $data && $data instanceof DataObject && $data->hasMethod($this->name)) {
				$funcName = $this->name;
				if($obj = $data->$funcName()) {
					if($obj instanceof DataObjectSet) {
						$value = $obj->column('ID');
					}
				}
			}
		}
		parent::setValue($value, $data);
	}



	/**
	 * Refreshes the file list. If passed an array of IDs in the request, 
	 * it augments the list with those files.
	 *
	 * @param SS_HTTPRequest
	 * @return SSViewer
	 */	
	public function refresh(SS_HTTPRequest $r) {
		if($r->requestVar('ids')) {
			$ids = array_unique($r->requestVar('ids'));
			$files = new DataObjectSet();
			if($set = DataObject::get("File", "\"File\".ID IN (".implode(',',$ids).")")) {
				foreach($set as $file) {
					$this->processFile($file);
					$files->push($file);					
				}
				$files->merge($this->Files());
				$files->removeDuplicates();
			}
			else {
				die("File $id doesn't exist");
			}
		}
		else {
			$files = $this->Files();
		}
		return $this->customise(array(
			'Files' => $files
		))->renderWith($this->AttachedFilesTemplate);
	}



	/**
	 * Gets all the attached files.
	 *
	 * @return DataObjectSet
	 */
	public function Files() {
		if($val = $this->Value()) {
			if(is_array($val)) {
				$list = implode(',', $val);
				if($files = DataObject::get("File", "\"File\".\"ID\" IN (".Convert::raw2sql($list).")")) {
					$ret = new DataObjectSet();
					foreach($files as $file) {
						$this->processFile($file);
						$ret->push($file);
					}
					return $ret;
{'fraction_non_alphanumeric': 0.1610509426393903, 'fraction_numerical': 0.00040112314480545525, 'mean_word_length': 3.131731565865783, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 46, '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': '6702080', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1567, 'n_words': 456}