Skip to content

@vbuck /Blogcategory.php forked from anonymous/Blogcategory.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
class MyPackage_Wordpress_Block_Catalog_Category_Helper_Blogcategory extends Varien_Data_Form_Element_Select {
/**
* Attaches javascript helper library to element output.
*
* @return mixed|string The final element HTML output
*/
public function getAfterElementHtml() {
$html=parent::getAfterElementHtml();
$html.=
'<script language="text/javascript">
/**
* Creates dependency of categories drop-down on blog drop-down by
* loading categories based on blog selection.
*
* @todo [low] Move code into standalone JS file and initialize with parameters.
*/
window.WPSourceHelper=({
cache: null,
config: {
currentBlog: null,
currentCategory: null,
defaultCategories: null
},
sources: {
blog: null,
blogCategory: null
},
init: function() {
// Initialize sources, config, cache
this.sources.blog = $(document.getElementsByClassName("wp-blog-child")[0]),
this.sources.blogCategory = $("'.$this->getHtmlId().'"),
this.config.currentBlog = this.sources.blog.value,
this.config.currentCategory = "'.$this->getEscapedValue().'",
this.config.defaultCategories = Object.clone(this.sources.blogCategory.options),
this.cache = new Object(),
self = this;
// Disable the categories source
this.sources.blogCategory.disable();
// Listen for blog selection
this.sources.blog.observe("change",function() {
self.config.currentBlog=this.value;
self.fetchCategories(
this,
function(response) {
self.populateSource(self.sources.blogCategory,response);
}
);
});
// Persist config category data
this.sources.blogCategory.observe("change",function() {
self.config.currentCategory=this.value;
});
// Auto-load categories if a blog is already set
if(this.config.currentBlog!="-1") {
this.fetchCategories(
this.sources.blog,
function(response) {
self.populateSource(self.sources.blogCategory,response);
}
);
}
return this;
},
fetchCategories: function(source,callback) {
if(typeof source!="object") {
alert("Cannot fetch the blog categories: no source specified.");
return false;
}
// Bail if `None` was selected
if(this.config.currentBlog=="-1") {
this.resetSource(this.sources.blogCategory);
this.sources.blogCategory.disable();
return false;
}
// Fetch from cache if available
if(typeof this.cache[this.config.currentBlog]=="object") {
if(typeof callback=="function") {
callback(this.cache[this.config.currentBlog]);
return this;
}
}
// Prep the request URL
var url="'.Mage::getUrl('wordpress/adminhtml_source/index',array('_current'=>true)).'";
url=url.replace(/\/key\//,"/blog_id/"+this.config.currentBlog+"/key/");
// Ask the controller to fetch from the database
new Ajax.Request(url,{
onSuccess: function(transport) {
var data=(transport.responseText).evalJSON();
self.cache[self.config.currentBlog]=data;
if(typeof callback=="function")
callback(data);
},
onFailure: function() {
alert("Failed to fetch the blog categories: bad server response.");
}
});
return this;
},
newOption: function(attributes) {
var option=document.createElement("option");
option.value=attributes.value;
option.text=attributes.label;
if(this.config.currentCategory==attributes.value)
option.selected="selected";
return option;
},
populateSource: function(target,data) {
if(typeof target!="object") {
alert("Cannot populate the blog categories: no target specified.");
return false;
}
this.resetSource(target,this.config.cachedCategories);
for(var i=0;i<data.length;i++)
target.appendChild(this.newOption(data[i]));
this.sources.blogCategory.enable();
return this;
},
resetSource: function(source) {
if(typeof source!="object")
return false;
// Empty the source
source.length=0;
var options=this.config.defaultCategories;
for(var i=0;i<options.length;i++)
source.appendChild(options[i]);
return this;
}
});
window.WPSourceHelper.init();
</script>';
return $html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.