Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba

Sorting products in category


Go to End
Reply


5 Posts   1475 Views

Avatar
GXG2010

Community Member, 4 Posts

23 February 2010 at 5:10am

Hi UncleCheese,

First of all I'd like to thank you for your effort to develop this module.
I used your module and I think is fantastic.

Now I have a little problem and I need a little help.
I have created a Product object:

class Product extends DataObject
{
static $db = array (
"Name" => "Text",
"Description" => "HTMLText",
);
static $has_one = array (
"Thumbnail" => "Image",
);
static $belongs_many_many = array (
'ProductHolderPage' => 'ProductHolderPage'
);

public function getCMSFields_forPopup()
{
$fields = new FieldSet();
$fields->push( new TextField('Name', 'Product name'));
$fields->push( new ImageField(
"Thumbnail",
"Upload product image",
null,
null,
null,
"assets/Products/"
));
$fields->push( new SimpleHTMLEditorField('Description','Product description', array ()));

return $fields;
}

}

and a ProductHolderPage

class ProductHolderPage extends Page
{
static $db = array (
);
static $has_one = array (
);
static $has_many = array (
);
static $many_many = array (
'Products' => 'Product'
);
static $icon = "themes/mpc/images/treeicons/productholder";
static $can_be_root = false;
static $allowed_children = "none";

function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeFieldFromTab("Root.Content.Main","Content");

# Products TAB
$manager = new ManyManyDataObjectManager(
$this,
'Products',
'Product',
array('Name' => 'Product name'),
'getCMSFields_forPopup'
);
$manager->setAddTitle('Product');
$fields->addFieldToTab("Root.Content.Products", $manager);

return $fields;
}

}

Now I need to sort the Products in each ProductHolderPage not to sort the Products list.
If I add the SortableDataObject::add_sortable_class('Product'); in the mysite/_config.php I can generally sort the Product's list but this is not useful in my case.

I need to add sortOrder to the ProductHolderPage_Products table and to manage somehow in CMS.
Is this possible?

Avatar
UncleCheese

Forum Moderator, 4096 Posts

23 February 2010 at 5:22am

You need SortableDataObject::add_many_many_sortable_relation()..

It worked in 2.3.3, but when 2.3.4 came out, there was an update to Object.php that broke this functionality. I put in a bug report, and as far as I know, 2.4 will include the necessary patch to make it work again.

See the thread "New Features" and read my post "many, many many_many new features"

Avatar
GXG2010

Community Member, 4 Posts

23 February 2010 at 11:34pm

Hi UncleCheese,

Thank you a lot! I still have trouble.

I have SS 2.3.3 and DataObjectManager last version.
I added in mysite/_config.php the line:

SortableDataObject::add_sortable_many_many_relation('ProductHolderPage', 'Products');

and I changed my class:

class ProductHolderPage extends Page
{
static $db = array (
);
static $has_one = array (
);
static $has_many = array (
);
static $many_many = array (
'Products' => 'Product'
);
static $icon = "themes/mpc/images/treeicons/productholder";
static $can_be_root = false;
static $allowed_children = "none";

function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeFieldFromTab("Root.Content.Main","Content");

# Products TAB
$manager = new ManyManyDataObjectManager(
$this,
'Products',
'Product',
array('Name' => 'Product name'),
'getCMSFields_forPopup'
);
$manager->setAddTitle('Product');
$fields->addFieldToTab("Root.Content.Products", $manager);
// $manager->setOnlyRelated(true);
// $manager->setPermissions(array('edit'));
// $manager->setMarkingPermission(false);
return $fields;
}

}

Everything it seems to be alright. I can check the "Allow drag & drop reordering" option and "Show only related records" but when I try to reorder the items, in the database the ProductHolderPage_Products table, SortOrder field, remains with the same values (0 zero default).

Do you have any idea what I'm missing here?
Thank you a lot for your help.

Avatar
UncleCheese

Forum Moderator, 4096 Posts

24 February 2010 at 5:21am

Edited: 24/02/2010 5:21am

The changes weren't forward compatible, so I reverted them for 2.3.4+. To get the sort functionality working in 2.3.3, you'll have to use the doSort() function from r337


class DataObjectManager_Controller extends Controller
{
  function dosort()
  {
    if(!empty($_POST) && is_array($_POST) && isset($this->urlParams['ID'])) {
      $className = $this->urlParams['ID'];
      if(stristr($className,"-") !== false) {
       list($ownerClass, $className) = explode("-",$className);
      }
      $many_many = ((is_numeric($this->urlParams['OtherID'])) && SortableDataObject::is_sortable_many_many($className));
      foreach($_POST as $group => $map) {
        if(substr($group, 0, 7) == "record-") {
          if($many_many) {
            $controllerID = $this->urlParams['OtherID'];          
            $candidates = singleton($ownerClass)->many_many();
            if(is_array($candidates)) {
              foreach($candidates as $name => $class)
                if($class == $className) {
                  $relationName = $name;
                  break;
                }
            }
            if(!isset($relationName)) return false;
            list($parentClass, $componentClass, $parentField, $componentField, $table) = singleton($ownerClass)->many_many($relationName);            
            foreach($map as $sort => $id)
              DB::query("UPDATE `$table` SET SortOrder = $sort WHERE {$className}ID = $id AND {$ownerClass}ID = $controllerID");
          }
          else {
            foreach($map as $sort => $id) {
              $obj = DataObject::get_by_id($className, $id);
              $obj->SortOrder = $sort;
              $obj->write();
            }           
          }
          break;
        }
      }
    }
  }
}

Avatar
GXG2010

Community Member, 4 Posts

25 February 2010 at 6:06am

Thank you, Thank you, Thank you!

Now everything is working perfectly. You are the best!