<commit_before><?php

class ProductTag extends DataObject {

	private static $db = array(
		"Title" => "Varchar",
		"SortOrder" => "Int"
	);

	private static $has_one = array(
		"Group" => "ProductTagGroup"
	);

	private static $many_many = array(
		"Products" => "Product"
	);

	private static $summary_fields = array(
		"Group.Title" => "Group",
		"Title" => "Title",
	);

	private static $extensions = array(
		"URLSegmented"
	);

	private static $default_sort = "SortOrder";


	function NiceTitle() {
		return $this->Group()->Title.": ".$this->Title;
	}

	static function enable() {
		Object::add_extension('Product', 'ProductTag_ProductExtension');
		Object::add_extension('ProductCategory_Controller', 'ProductTag_ProductCategory_Controller_Extension');
	}

}

class ProductTagGroup extends DataObject {

	private static $db = array(
		"Title" => "Varchar",
		"SortOrder" => "Int"
	);

	private static $has_many = array(
		"Tags" => "ProductTag"
	);

	private static $default_sort = "SortOrder";


	public function getCMSFields() {
		$fields = parent::getCMSFields();

		$conf = GridFieldConfig_RelationEditor::create(500);
		$conf->addComponent(new GridFieldSortableRows("SortOrder"));

		$fields->removeByName("Tags");
		$fields->removeByName("SortOrder");

		$fields->addFieldToTab("Root.Main", new GridField("Tags", "Product Tags", $this->Tags(), $conf));

		return $fields;
	}

}

class ProductTag_ProductExtension extends DataExtension {

	private static $belongs_many_many = array(
		"Tags" => "ProductTag"
	);
	
	function updateProductCMSFields(FieldList $fields) {
		$fields->addFieldToTab("Root.Main", ListboxField::create("Tags", "Filter Tags")
			->setSource(ProductTag::get()->map("ID", "NiceTitle")->toArray())
			->setMultiple(true), 
		"Content");
	}

	function TagGroupIDs() {
		$groupIDs = $this->owner->Tags()->map("GroupID", "GroupID")->toArray();

		return $groupIDs;
	}

	function TagIDs() {
		$groupIDs = $this->owner->Tags()->map("ID", "ID")->toArray();
		return $groupIDs;
	}

}

class ProductTag_ProductCategory_Controller_Extension extends Extension {

	static $allowed_actions = array(
		"ProductFilterForm",
		"filterProducts"
	);


	function TagIDs() {
		$tags_map = $this->owner->Products()->map("ID", "TagIDs")->toArray();
		$tagIDs = array();
		foreach($tags_map as $_productID => $_tagMap) {
			foreach($_tagMap as $_tagID) {
				$tagIDs[$_tagID] = $_tagID;
			}
		}
		return $tagIDs;
	}

	function TagGroups() {
		$groups_map = $this->owner->Products()->map("ID", "TagGroupIDs")->toArray();
		$groupIDs = array();
		foreach($groups_map as $_productID => $_groupMap) {
			foreach($_groupMap as $_tagID => $_groupIDs) {
				$groupIDs[$_groupIDs] = $_groupIDs;
			}
		}
		if($groupIDs) {
			return ProductTagGroup::get()->byIDs($groupIDs);
		}
	}

	function ProductFilterForm() {
		if(!$this->owner->TagGroups()) {
			return false;
		}
		$currentFilters = $this->Filters();
		$tagIDs = $this->TagIDs();

		$fields = array();
		$fields[] = new LiteralField("hs", "<div class=\"sideHeader\">");
		$fields[] = new HeaderField("filterheader", "Refine results",3);
		$fields[] = new LiteralField("hp", "</div>");

		foreach($this->owner->TagGroups() as $group) {
			
			$field = CheckboxSetField::create("Filter[{$group->ID}]", $group->Title,
				$group->Tags()->byIDs($this->TagIDs())->map("ID", "Title")->toArray()
			);

			if(isset($currentFilters[$group->ID])) {
				$value = implode(",", (array)$currentFilters[$group->ID]);
				$field->setValue($value);
			}

			$fields[] = $field;
		}

		$fields = FieldList::create($fields);

		$actions = FieldList::create(
			FormAction::create("filterProducts", "Filter")
		);

		$form = Form::create($this->owner, __FUNCTION__, $fields, $actions);
		$form->setEncType(Form::ENC_TYPE_MULTIPART);<commit_msg>removed heading<commit_after><?php

class ProductTag extends DataObject {

	private static $db = array(
		"Title" => "Varchar",
		"SortOrder" => "Int"
	);

	private static $has_one = array(
		"Group" => "ProductTagGroup"
	);

	private static $many_many = array(
		"Products" => "Product"
	);

	private static $summary_fields = array(
		"Group.Title" => "Group",
		"Title" => "Title",
	);

	private static $extensions = array(
		"URLSegmented"
	);

	private static $default_sort = "SortOrder";


	function NiceTitle() {
		return $this->Group()->Title.": ".$this->Title;
	}

	static function enable() {
		Object::add_extension('Product', 'ProductTag_ProductExtension');
		Object::add_extension('ProductCategory_Controller', 'ProductTag_ProductCategory_Controller_Extension');
	}

}

class ProductTagGroup extends DataObject {

	private static $db = array(
		"Title" => "Varchar",
		"SortOrder" => "Int"
	);

	private static $has_many = array(
		"Tags" => "ProductTag"
	);

	private static $default_sort = "SortOrder";


	public function getCMSFields() {
		$fields = parent::getCMSFields();

		$conf = GridFieldConfig_RelationEditor::create(500);
		$conf->addComponent(new GridFieldSortableRows("SortOrder"));

		$fields->removeByName("Tags");
		$fields->removeByName("SortOrder");

		$fields->addFieldToTab("Root.Main", new GridField("Tags", "Product Tags", $this->Tags(), $conf));

		return $fields;
	}

}

class ProductTag_ProductExtension extends DataExtension {

	private static $belongs_many_many = array(
		"Tags" => "ProductTag"
	);
	
	function updateProductCMSFields(FieldList $fields) {
		$fields->addFieldToTab("Root.Main", ListboxField::create("Tags", "Filter Tags")
			->setSource(ProductTag::get()->map("ID", "NiceTitle")->toArray())
			->setMultiple(true), 
		"Content");
	}

	function TagGroupIDs() {
		$groupIDs = $this->owner->Tags()->map("GroupID", "GroupID")->toArray();

		return $groupIDs;
	}

	function TagIDs() {
		$groupIDs = $this->owner->Tags()->map("ID", "ID")->toArray();
		return $groupIDs;
	}

}

class ProductTag_ProductCategory_Controller_Extension extends Extension {

	static $allowed_actions = array(
		"ProductFilterForm",
		"filterProducts"
	);


	function TagIDs() {
		$tags_map = $this->owner->Products()->map("ID", "TagIDs")->toArray();
		$tagIDs = array();
		foreach($tags_map as $_productID => $_tagMap) {
			foreach($_tagMap as $_tagID) {
				$tagIDs[$_tagID] = $_tagID;
			}
		}
		return $tagIDs;
	}

	function TagGroups() {
		$groups_map = $this->owner->Products()->map("ID", "TagGroupIDs")->toArray();
		$groupIDs = array();
		foreach($groups_map as $_productID => $_groupMap) {
			foreach($_groupMap as $_tagID => $_groupIDs) {
				$groupIDs[$_groupIDs] = $_groupIDs;
			}
		}
		if($groupIDs) {
			return ProductTagGroup::get()->byIDs($groupIDs);
		}
	}

	function ProductFilterForm() {
		if(!$this->owner->TagGroups()) {
			return false;
		}
		$currentFilters = $this->Filters();
		$tagIDs = $this->TagIDs();

		$fields = array();
		

		foreach($this->owner->TagGroups() as $group) {
			
			$field = CheckboxSetField::create("Filter[{$group->ID}]", $group->Title,
				$group->Tags()->byIDs($this->TagIDs())->map("ID", "Title")->toArray()
			);

			if(isset($currentFilters[$group->ID])) {
				$value = implode(",", (array)$currentFilters[$group->ID]);
				$field->setValue($value);
			}

			$fields[] = $field;
		}

		$fields = FieldList::create($fields);

		$actions = FieldList::create(
			FormAction::create("filterProducts", "Filter")
		);

		$form = Form::create($this->owner, __FUNCTION__, $fields, $actions);
		$form->setEncType(Form::ENC_TYPE_MULTIPART);
{'fraction_non_alphanumeric': 0.18974844468487964, 'fraction_numerical': 0.0009467135515282662, 'mean_word_length': 4.883054892601432, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 127, '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': '5849743', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2458, 'n_words': 446}