public function generateCode()
{
// get a map of "real attribute properties to properties used in setup resource array
$realToSetupKeyLegend = $this->_getKeyMapping();
// swap keys from above
$data = $this->attribute->getData();
$keysLegend = array_keys($realToSetupKeyLegend);
$newData = array();
foreach ($data as $key => $value) {
if (in_array($key, $keysLegend)) {
$key = $realToSetupKeyLegend[$key];
}
$newData[$key] = $value;
}
// unset items from model that we don't need and would be discarded by
// resource script anyways
unset($newData['attribute_id']);
unset($newData['attribute_code']);
unset($newData['entity_type_id']);
// chuck a few warnings out there for things that were a little murky
if ($newData['attribute_model']) {
$this->warnings[] = '<warning>WARNING, value detected in attribute_model. We\'ve never seen a value ' . 'there before and this script doesn\'t handle it. Caution, etc. </warning>';
}
if ($newData['is_used_for_price_rules']) {
$this->warnings[] = '<error>WARNING, non false value detected in is_used_for_price_rules. ' . 'The setup resource migration scripts may not support this (per 1.7.0.1)</error>';
}
//load values for attributes (if any exist)
$newData['option'] = $this->getOptions($this->attribute);
//get text for script
$arrayCode = var_export($newData, true);
//generate script using simpnle string concatenation, making
//a single tear fall down the cheek of a CS professor
$script = "<?php\n\$setup = new Mage_Catalog_Model_Resource_Setup('core_setup');\n\n\$attr = {$arrayCode};\n\$setup->addAttribute('catalog_product', '" . $this->attribute->getAttributeCode() . "', \$attr);\n ";
$attributeLabels = $this->getAttributeLabels($this->attribute);
$attributeLabelsCode = var_export($attributeLabels, true);
$labelsScript = "\n\$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', '" . $this->attribute->getAttributeCode() . "');\n\$attribute->setStoreLabels({$attributeLabelsCode});\n\$attribute->save();\n";
$script .= $labelsScript;
return $script;
}