<commit_before><?php
/**
 * This component provides a checkbox which when checked enables drag-and-drop re-ordering of elements displayed in a {@link GridField}
 *
 * @package forms
 */
class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator {
	protected $sortColumn;
	protected $disable_selection=true;
	protected $append_to_top=false;

	/**
	 * @param String $sortColumn Column that should be used to update the sort information
	 * @param bool   $disableSelection
	 */
	public function __construct($sortColumn, $disableSelection = true) {
		$this->sortColumn = $sortColumn;
		$this->disable_selection = $disableSelection;
	}
	
	/**
	 * Returns a map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
	 * @param GridField $gridField Grid Field Reference
	 * @return Array Map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
	 */
	public function getHTMLFragments($gridField) {
		$dataList = $gridField->getList();

		if(class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
			return array();
		}
		
		$state = $gridField->State->GridFieldSortableRows;
		if(!is_bool($state->sortableToggle)) {
			$state->sortableToggle = false;
		}
		
		//Ensure user can edit
		if(!singleton($gridField->getModelClass())->canEdit()){
			return array();
		}
		
		
		//Sort order toggle
		$sortOrderToggle = GridField_FormAction::create(
			$gridField,
			'sortablerows-toggle',
			'sorttoggle',
			'sortableRowsToggle',
			null
		)->addExtraClass('sortablerows-toggle');
		
		
		$sortOrderSave = GridField_FormAction::create(
			$gridField,
			'sortablerows-savesort',
			'savesort',
			'saveGridRowSort',
			null
		)->addExtraClass('sortablerows-savesort');
		
		
		//Sort to Page Action
		$sortToPage = GridField_FormAction::create(
			$gridField,
			'sortablerows-sorttopage',
			'sorttopage',
			'sortToPage',
			null
		)->addExtraClass('sortablerows-sorttopage');
		
		
		$data = array('SortableToggle' => $sortOrderToggle,
					'SortOrderSave' => $sortOrderSave,
					'SortToPage' => $sortToPage,
					'Checked' => ($state->sortableToggle == true ? ' checked = "checked"':''),
					'List' => $dataList);
		
		$forTemplate = new ArrayData($data);
		
		
		//Inject Requirements
		$custom = Config::inst()->get('GridFieldSortableRows', 'Base');
		$base = $custom ?: SORTABLE_GRIDFIELD_BASE;

		Requirements::css($base . '/css/GridFieldSortableRows.css');
		Requirements::javascript($base . '/javascript/GridFieldSortableRows.js');
		
		
		$args = array('Colspan' => count($gridField->getColumns()), 'ID' => $gridField->ID(), 'DisableSelection' => $this->disable_selection);
		
		
		$fragments=array('header' => $forTemplate->renderWith('GridFieldSortableRows', $args));
		
		if($gridField->getConfig()->getComponentByType('GridFieldPaginator')) {
			$fragments['after']=$forTemplate->renderWith('GridFieldSortableRows_paginator');
		}
		
		return $fragments;
	}
	
	/**
	 * Manipulate the datalist as needed by this grid modifier.
	 * @param GridField $gridField Grid Field Reference
	 * @param SS_List $dataList Data List to adjust
	 * @return DataList Modified Data List
	 */
	public function getManipulatedData(GridField $gridField, SS_List $dataList) {
		//Detect and correct items with a sort column value of 0 (push to bottom)
		$this->fixSortColumn($gridField, $dataList);
		
		
		$headerState = $gridField->State->GridFieldSortableHeader;
		$state = $gridField->State->GridFieldSortableRows;
		if ((!is_bool($state->sortableToggle) || $state->sortableToggle==false) && $headerState && !empty($headerState->SortColumn)) {
			return $dataList->sort($this->sortColumn);
		}
		
		if ($state->sortableToggle == true) {
			$gridField->getConfig()->removeComponentsByType('GridFieldFilterHeader');
			$gridField->getConfig()->removeComponentsByType('GridFieldSortableHeader');
		}
		
		return $dataList->sort($this->sortColumn);
	}
	
	/**
	 * Sets if new records should be appended to the top or the bottom of the list
	 * @param bool $value Boolean true to append to the top false to append to the bottom
	 * @return GridFieldSortableRows Returns the current instance
	 */
	public function setAppendToTop($value) {
		$this->append_to_top=$value;
		return $this;
	}

	/**
	 * @param bool $value Boolean true to disable selection of table contents false to enable selection
	 * @return GridFieldSortableRows Returns the current instance
	 */
	public function setDisableSelection($value){
		$this->disable_selection = $value;
		return $this;
	}
	
	/**
	 * Detects and corrects items with a sort column value of 0, by appending them to the bottom of the list
	 * @param GridField $gridField Grid Field Reference
	 * @param SS_List $dataList Data List of items to be checked
	 */
	protected function fixSortColumn($gridField, SS_List $dataList) {
		if(class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
			return;
		}
		
		$list=clone $dataList;
		$list=$list->alterDataQuery(function($query, SS_List $tmplist) {
			$query->limit(array());
			return $query;
		});
		
		$many_many = ($list instanceof ManyManyList);
		if (!$many_many) {
			$sng=singleton($gridField->getModelClass());
			$fieldType=$sng->db($this->sortColumn);
			if(!$fieldType || !($fieldType=='Int' || is_subclass_of('Int', $fieldType))) {
				if(is_array($fieldType)) {
					user_error('Sort column '.$this->sortColumn.' could not be found in '.$gridField->getModelClass().'\'s ancestry', E_USER_ERROR);<commit_msg>Fixing bug "must be an Int, column is of type int"<commit_after><?php
/**
 * This component provides a checkbox which when checked enables drag-and-drop re-ordering of elements displayed in a {@link GridField}
 *
 * @package forms
 */
class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator {
	protected $sortColumn;
	protected $disable_selection=true;
	protected $append_to_top=false;

	/**
	 * @param String $sortColumn Column that should be used to update the sort information
	 * @param bool   $disableSelection
	 */
	public function __construct($sortColumn, $disableSelection = true) {
		$this->sortColumn = $sortColumn;
		$this->disable_selection = $disableSelection;
	}
	
	/**
	 * Returns a map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
	 * @param GridField $gridField Grid Field Reference
	 * @return Array Map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
	 */
	public function getHTMLFragments($gridField) {
		$dataList = $gridField->getList();

		if(class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
			return array();
		}
		
		$state = $gridField->State->GridFieldSortableRows;
		if(!is_bool($state->sortableToggle)) {
			$state->sortableToggle = false;
		}
		
		//Ensure user can edit
		if(!singleton($gridField->getModelClass())->canEdit()){
			return array();
		}
		
		
		//Sort order toggle
		$sortOrderToggle = GridField_FormAction::create(
			$gridField,
			'sortablerows-toggle',
			'sorttoggle',
			'sortableRowsToggle',
			null
		)->addExtraClass('sortablerows-toggle');
		
		
		$sortOrderSave = GridField_FormAction::create(
			$gridField,
			'sortablerows-savesort',
			'savesort',
			'saveGridRowSort',
			null
		)->addExtraClass('sortablerows-savesort');
		
		
		//Sort to Page Action
		$sortToPage = GridField_FormAction::create(
			$gridField,
			'sortablerows-sorttopage',
			'sorttopage',
			'sortToPage',
			null
		)->addExtraClass('sortablerows-sorttopage');
		
		
		$data = array('SortableToggle' => $sortOrderToggle,
					'SortOrderSave' => $sortOrderSave,
					'SortToPage' => $sortToPage,
					'Checked' => ($state->sortableToggle == true ? ' checked = "checked"':''),
					'List' => $dataList);
		
		$forTemplate = new ArrayData($data);
		
		
		//Inject Requirements
		$custom = Config::inst()->get('GridFieldSortableRows', 'Base');
		$base = $custom ?: SORTABLE_GRIDFIELD_BASE;

		Requirements::css($base . '/css/GridFieldSortableRows.css');
		Requirements::javascript($base . '/javascript/GridFieldSortableRows.js');
		
		
		$args = array('Colspan' => count($gridField->getColumns()), 'ID' => $gridField->ID(), 'DisableSelection' => $this->disable_selection);
		
		
		$fragments=array('header' => $forTemplate->renderWith('GridFieldSortableRows', $args));
		
		if($gridField->getConfig()->getComponentByType('GridFieldPaginator')) {
			$fragments['after']=$forTemplate->renderWith('GridFieldSortableRows_paginator');
		}
		
		return $fragments;
	}
	
	/**
	 * Manipulate the datalist as needed by this grid modifier.
	 * @param GridField $gridField Grid Field Reference
	 * @param SS_List $dataList Data List to adjust
	 * @return DataList Modified Data List
	 */
	public function getManipulatedData(GridField $gridField, SS_List $dataList) {
		//Detect and correct items with a sort column value of 0 (push to bottom)
		$this->fixSortColumn($gridField, $dataList);
		
		
		$headerState = $gridField->State->GridFieldSortableHeader;
		$state = $gridField->State->GridFieldSortableRows;
		if ((!is_bool($state->sortableToggle) || $state->sortableToggle==false) && $headerState && !empty($headerState->SortColumn)) {
			return $dataList->sort($this->sortColumn);
		}
		
		if ($state->sortableToggle == true) {
			$gridField->getConfig()->removeComponentsByType('GridFieldFilterHeader');
			$gridField->getConfig()->removeComponentsByType('GridFieldSortableHeader');
		}
		
		return $dataList->sort($this->sortColumn);
	}
	
	/**
	 * Sets if new records should be appended to the top or the bottom of the list
	 * @param bool $value Boolean true to append to the top false to append to the bottom
	 * @return GridFieldSortableRows Returns the current instance
	 */
	public function setAppendToTop($value) {
		$this->append_to_top=$value;
		return $this;
	}

	/**
	 * @param bool $value Boolean true to disable selection of table contents false to enable selection
	 * @return GridFieldSortableRows Returns the current instance
	 */
	public function setDisableSelection($value){
		$this->disable_selection = $value;
		return $this;
	}
	
	/**
	 * Detects and corrects items with a sort column value of 0, by appending them to the bottom of the list
	 * @param GridField $gridField Grid Field Reference
	 * @param SS_List $dataList Data List of items to be checked
	 */
	protected function fixSortColumn($gridField, SS_List $dataList) {
		if(class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
			return;
		}
		
		$list=clone $dataList;
		$list=$list->alterDataQuery(function($query, SS_List $tmplist) {
			$query->limit(array());
			return $query;
		});
		
		$many_many = ($list instanceof ManyManyList);
		if (!$many_many) {
			$sng=singleton($gridField->getModelClass());
			$fieldType=$sng->db($this->sortColumn);
			if(!$fieldType || !(strtolower($fieldType) == 'int' || is_subclass_of('Int', $fieldType))) {
				if(is_array($fieldType)) {
					user_error('Sort column '.$this->sortColumn.' could not be found in '.$gridField->getModelClass().'\'s ancestry', E_USER_ERROR);
{'fraction_non_alphanumeric': 0.13205528310298706, 'fraction_numerical': 0.00035666518056174767, 'mean_word_length': 4.605197401299351, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 115, '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': '1867387', 'n_tokens_mistral': 3793, 'n_tokens_neox': 3316, 'n_words': 850}