Documentation on GridFieldSortableObject

Sort order now starts at 1

Cleaned up SQL query generation for many_many relationships
This commit is contained in:
UndefinedOffset 2012-05-09 15:52:41 -03:00
parent b10961873c
commit 410b3a91a8
2 changed files with 51 additions and 16 deletions

View File

@ -1,4 +1,7 @@
<?php
/**
* @package extensions
*/
class GridFieldSortableObject extends DataExtension {
public static $db=array(
'SortOrder'=>'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)) {

View File

@ -132,10 +132,10 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
for($sort=0;$sort<count($data['Items']);$sort++) {
$id=intval($data['Items'][$sort]);
if($many_many) {
DB::query("UPDATE \"$table\" SET \"SortOrder\" = $sort WHERE \"{$className}ID\" = $id AND \"{$ownerClass}ID\" = $controllerID");
DB::query('UPDATE "'.$table.'" SET "SortOrder" = '.($sort+1).' WHERE "'.$className.'ID" = $id AND "'.$ownerClass.'ID" = '.$controllerID);
}else {
$obj=DataObject::get_by_id($className, $id);
$obj->SortOrder=$sort;
$obj->SortOrder=$sort+1;
$obj->write();
}
}