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?