Jump to:

5453 Posts in 1674 Topics by 1198 members

Customising the CMS

SilverStripe Forums » Customising the CMS » ModelAdmin: Load next DO with Next Button

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

Page: 1
Go to End
Author Topic: 718 Views
  • danzzz
    Avatar
    Community Member
    175 Posts

    ModelAdmin: Load next DO with Next Button Link to this post

    hi,

    with the help of this tutorial:
    http://www.ssbits.com/tutorials/2011/add-a-duplicate-button-to-model-admin/

    I created a "Next" Button at the bottom of model admin.

    The button calls the function "nextdataobject".

    public function nextdataobject($data, $form, $request) {
       
       $NextRecord = DataObject::get_by_id('SomeDataObject', $this->currentRecord->ID + 1);
       $this->currentRecord = $NextRecord;
       
       if(Director::is_ajax()) {
    return new SS_HTTPResponse(
    Convert::array2json(array(
    'html' => $this->EditForm()->forAjaxTemplate(),
    'message' => _t('ModelAdmin.PUBLISHED',"Next DataObject was loaded.")
    )),
    200
    );
    } else {
    Director::redirectBack();
    }
       
    }

    This works only if there are no breaks in the ID's. Because I just do a + 1 on the ID it could
    be that one is missing cause of deletion. How to get just the next DO?

    $this->nextRecord or something else would be great, does anyone have an idea?

    But there is one more problem, the DataObject has a "has_one" relationship to an image.
    If I click on "Next" it loads everything correct, but not the images ... it loads the images
    of the last DataObject. Any idea why and how to solve?

    Thx

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: ModelAdmin: Load next DO with Next Button Link to this post

    best I could recomend is to persist the search query into the session...

    class MyAdmin_CollectionController extends ModelAdmin_CollectionController {
       function getSearchQuery($searchCriteria){
          $query = parent::getSearchQuery($searchCriteria);
          Session::set('LastQuery',$query);
          return $query;
       }
    }

    then modify your code to run this same query and grab the next one in the returned DataObjectSet after the $this->ID

    please let me know if you get this running - I'd be interested to add this functionality for some of my users.

  • danzzz
    Avatar
    Community Member
    175 Posts

    Re: ModelAdmin: Load next DO with Next Button Link to this post

    hi swaiba,

    is your solution for the "select next ID" problem or for the problem with the relations and images?

    at the moment I use this working way for the problem selecting the next ID:

    $NextRecord = DataObject::get_one('SomeDataObject', "ID > ".$this->currentRecord->ID, true, "ID ASC");
    $this->currentRecord = $NextRecord;

    I just changed the DataObject::get_by_id to DataObject::get_one. with a filter and ordering.

    But still have no idea how to solve the problem with the relations ... if I click "Next" it loads the next row, but without
    the correct relations. Each row has_one Image, and if I click "Next" always the image of the previous row shows ...
    alse other has_many or many_many relations are no there.

    I tried to solve this with your idea this way ...

    $query = Session::get('LastQuery');
    $NextRecord = DataObject::get_one('SomeDataObject', "ID > ".$this->currentRecord->ID, true, "ID ASC");
    $query->where = array('SomeDataObjecet.ID = '.$NextRecord->ID);
    $result = $query->execute();
    $this->currentRecord = singleton('SomeDataObject')->buildDataObjectSet($result)->First();

    ... but with no success in case of the relations.

    EDIT: the relations work, only the Images dont ... if I dump the query, the the "logoID" and "screenshotID" are correct (the IDs of the next DO) but the pics of the prv. DO shows ...

  • danzzz
    Avatar
    Community Member
    175 Posts

    Re: ModelAdmin: Load next DO with Next Button Link to this post

    ok ....

    the image problem is only when I use uploadify.
    If I use standard upload it works

    here the complete function which is working:

    public function nextdataobject($data, $form, $request) {
       
       $NextRecord = DataObject::get_one('SomeDataObject', "ID > ".$this->currentRecord->ID, true, "ID ASC");
       $this->currentRecord = $NextRecord;
       
       if(Director::is_ajax()) {
    return new SS_HTTPResponse(
    Convert::array2json(array(
    'html' => $this->EditForm()->forAjaxTemplate(),
    'message' => _t('ModelAdmin.PUBLISHED',"Next DataObject loaded.")
    )),
    200
    );
    } else {
    Director::redirectBack();
    }
       
    }

    so finaly, changed has onle the $NextRecord = DataObject....... and I switched back from an uploadify field to a normal ImageField.

    @unclecheese: andy ideas to get this working with uploadify to?

    EDIT: muahaha, what a day :-|

    this solution only work if you dont have any search criteria, so let's add the code for the SearchQuery manipulation again ...

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: ModelAdmin: Load next DO with Next Button Link to this post

    Hey Danzzz,

    Just had a ticket with a client requesting this - did you get any further with your coding than is recorded here?

    718 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.