public
Created — forked from /Blogcategory.php

  • Download Gist
Blogcategory.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
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;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.