1
votes

I've got a page type called PortfolioItemPage:

  <?php
    class PortfolioItemPage extends Page {

        private static $many_many = array(
            'MediaTypeTags' => 'MediaTypeTag'
        );

        public function getCMSFields() {
            $fields = parent::getCMSFields();

            if ($this->ID) {
                $fields->addFieldToTab('Root.Media Type Tags', CheckboxSetField::create(
                    'MediaTypeTags',
                    'Media Type Tags',
                    MediaTypeTag::get()->map()
                ));
            }

            return $fields;
        }
    }

I have a DataObject (MediaTypeTag) mapped to this page type as a separate tab. This is because I want to associated certain Media Type tags with each Portfolio Item page for sorting purposes. (I want to note that this is NOT a blog--this is a site a page of items that can be sorted/filtered).

I've become stuck with the functionality for sorting PortfolioItemPage base on the MediaTypeTags selected. I have jQuery in place that is pulling in the id of the Media Type tag selected and passing it to the server:

(function($) {

    $(document).ready(function() {
        var media = $('.media');

        media.each(function(e) {
            $(this).bind('click', function(e) {
                e.preventDefault();
                $(this).toggleClass('selectedTag');
                sendMediaTag($(this).attr('id'));
            }.bind($(this)));
        });

        function sendMediaTag(mediaTag){
            $.ajax({
                type: "POST",
                url: "/home/getByMediaTag/"+mediaTag,
                dataType: "json"
            }).done(function (response) {
                for(var i=0; i < response.length; i++){
                    //sort through the response items here
                }
            });
        }
    });

}(jQuery));

But when it comes to the getByMediaTag function, I'm not sure how to grab all the PortfolioItemPages by the MediaTypeTag id. There is a separate table in the database called PorfolioItemPage_MediaTypeTags that stores the PortfolioItemPage IDs with their corresponding MediaTypeTag IDs, but I'm not sure how to access that information. This is what I have so far:

//get all Portfolio Items by media tag(s)
public function getByMediaTag(){
    $mediaID = $this->getRequest()->param('ID');

    //get an array of PortfolioItemPage based on value of $mediaID

    $return = array();

    foreach($portfolioItems as $portfolioItem){
        $return[] = array(
            'thumbnail' => $portfolioItem->Thumbnail()->Link(),
            'name' => $portfolioItem->H1,
            'logo' => $portfolioItem->Logo()->Link(),
            'excerpt' => $portfolioItem->Excerpt,
            'id' => $portfolioItem->ID
        );
    }
    return json_encode($return);
}

Any ideas on how I can get an array of PortfolioItemPage items based on the MediaTypeTag id?

1
Does MediaTypeTag have the belongs_many_many to PortfolioItemPage? - wmk
That seems to have solved the issue, but it does add a PortfolioItemPage tab the the MediaTypeTag admin model, which I would rather hide. I suppose I can remove/hide this tab in some way? - Dejsa Cocan

1 Answers

1
votes

Based on the comments, this is what you are looking for.

class MediaTypeTag extends DataObject
{
    private static $db = array(
        'Title' => 'Varchar'
        // ...
    );

    private static $belongs_many_many = array(
        'PortfolioItemPages' => 'PortfolioItemPage'
    );

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->removeByName('PortfolioItemPages');

        return $fields;
    }
}

Besides that, you could just do something like this in your controller (not tested)

private static $allowed_actions = array(
    'getByMediaTag' => '->isAjax'
);

public function init()
{
    parent::init();

    Requirements::javascriptTemplate("{$this->ThemeDir()}/javascript/PortfolioItemPage.js", array(
        'Link' => $this->Link('getByMediaTag/')
    ));
}

protected function isAjax()
{
    return Director::is_ajax();
}

public function getByMediaTag()
{
    $mediaID = $this->getRequest()->param('ID');

    if(! $tag = MediaTypeTag::get()->ByID($mediaID))
        return false;

    return json_encode($tag->PortfolioItemPages()->toNestedArray());

}

Just to keep the ajax URL dynamic

//portfolioItemPage.js
$.ajax({
    type: "POST",
    url: "$Link"+mediaTag,
    dataType: "json"
}).done(function (response) {
    for(var i=0; i < response.length; i++){
        //sort through the response items here
    }
});

That would be a little cleaner way to do it.