diff --git a/code/extensions/GridFieldSortableObject.php b/code/extensions/GridFieldSortableObject.php index 62ab8f0..7e1e425 100644 --- a/code/extensions/GridFieldSortableObject.php +++ b/code/extensions/GridFieldSortableObject.php @@ -1,4 +1,7 @@ 'Int' @@ -8,12 +11,22 @@ class GridFieldSortableObject extends DataExtension { protected static $many_many_sortable_relations = array(); protected static $sort_dir = "ASC"; - + /** + * Sets the direction of the sort, by default it is ASC + * @param {string} $dir Sort direction ASC or DESC + */ public static function set_sort_dir($dir) { + if(strtoupper($dir)!='ASC' && strtoupper($dir)!='DESC') { + user_error('Sort direction must be ASC or DESC', E_USER_ERROR); + } + self::$sort_dir=$dir; } - - + + /** + * Makes a class sortable + * @param {string} $className Name of the DataObject to extend + */ public static function add_sortable_class($className) { if(!self::is_sortable_class($className)) { Object::add_extension($className, 'GridFieldSortableObject'); @@ -21,7 +34,12 @@ class GridFieldSortableObject extends DataExtension { self::$sortable_classes[]=$className; } } - + + /** + * Makes a many_many relationship sortable + * @param {string} $ownerClass Name of the owner class of the relationship + * @param {string} $componentName Name of the relationship + */ public static function add_sortable_many_many_relation($ownerClass, $componentName) { list($parentClass, $componentClass, $parentField, $componentField, $table)=singleton($ownerClass)->many_many($componentName); @@ -40,25 +58,31 @@ class GridFieldSortableObject extends DataExtension { self::add_sortable_class($componentClass); } - public static function remove_sortable_class($class) { - Object::remove_extension($class, 'GridFieldSortableObject'); - } - - public static function is_sortable_class($classname) { + /** + * Checks to see if a given DataObject class is sortable or not + * @param {string} $className Name of the DataObject to check + */ + public static function is_sortable_class($className) { if(in_array($classname, self::$sortable_classes)) { return true; } foreach(self::$sortable_classes as $class) { - if(is_subclass_of($classname, $class)) { + if(is_subclass_of($className, $class)) { return true; } } - return Object::has_extension($classname, 'GridFieldSortableObject'); + return Object::has_extension($className, 'GridFieldSortableObject'); } + /** + * Checks to see if a given many_many relationship is sortable or not + * @param {string} $componentClass Name of the component's class + * @param {string} $parentClass Name of the owner class of the relationship + * @return {bool} Returns boolean true if the many_many relationship is sortable + */ public static function is_sortable_many_many($componentClass, $parentClass=null) { $map=self::$many_many_sortable_relations; if($parentClass===null) { @@ -73,14 +97,22 @@ class GridFieldSortableObject extends DataExtension { } - public static function get_join_tables($classname) { - if(isset(self::$many_many_sortable_relations[$classname])) { - return self::$many_many_sortable_relations[$classname]; + /** + * Gets the join tables for the given class name + * @param {string} $className Name of the DataObject to fetch for + */ + public static function get_join_tables($className) { + if(isset(self::$many_many_sortable_relations[$className])) { + return self::$many_many_sortable_relations[$className]; } return false; } + /** + * Modifies the SQL appending the SortOrder with the direction to the orderby statement + * @param {SQLQuery} $query SQL Query to adjust + */ public function augmentSQL(SQLQuery &$query) { if(empty($query->select) || $query->delete || in_array("COUNT(*)", $query->select) || in_array("count(*)", $query->select)) { return; @@ -118,6 +150,9 @@ class GridFieldSortableObject extends DataExtension { } } + /** + * Sets the sort order on the DataObject when its being written to the database for the first time + */ public function onBeforeWrite() { if(!$this->owner->ID) { if($peers=DataList::create($this->owner->class)) { diff --git a/code/forms/GridFieldSortableRows.php b/code/forms/GridFieldSortableRows.php index 12df673..bb3fa17 100644 --- a/code/forms/GridFieldSortableRows.php +++ b/code/forms/GridFieldSortableRows.php @@ -132,10 +132,10 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP for($sort=0;$sort