function getAttributeCsv($fileName) { // $csv = array_map("str_getcsv", file($fileName,FILE_SKIP_EMPTY_LINES)); $file = fopen($fileName, "r"); while (!feof($file)) { $csv[] = fgetcsv($file, 0, '|'); } $keys = array_shift($csv); foreach ($csv as $i => $row) { $csv[$i] = array_combine($keys, $row); } foreach ($csv as $row) { $labelText = $row['frontend_label']; $attributeCode = $row['attribute_code']; if ($row['_options'] != "") { $options = explode(";", $row['_options']); } else { $options = -1; } if ($row['apply_to'] != "") { $productTypes = explode(",", $row['apply_to']); } else { $productTypes = -1; } unset($row['frontend_label'], $row['attribute_code'], $row['_options'], $row['apply_to'], $row['attribute_id'], $row['entity_type_id'], $row['search_weight']); createAttribute($labelText, $attributeCode, $row, $productTypes, -1, $options); } }
function createAttributes($attributes, $entityTypeId, $defaultAttribute, $group) { array_map(function ($name) use($attributes, $entityTypeId, $defaultAttribute, $group) { return createAttribute($name, $attributes[$name], $entityTypeId, $defaultAttribute, $group); }, array_keys($attributes)); }
if ($set_skeleton !== -1) { $modelGroup = Mage::getModel('eav/entity_attribute_group'); $modelGroup->setAttributeGroupName($set_name); $modelGroup->setAttributeSetId($set_id); $model->initFromSkeleton($set_skeleton); $groups = $model->getGroups(); $groups[] = $modelGroup; $model->setGroups($groups); $model->save(); $group_id = $modelGroup->getId(); $values = array("is_required" => 1); $set = array("SetID" => $set_id, "GroupID" => $group_id); createAttribute("skinned_front", "skinned_front", $values, -1, $set); createAttribute("skinned_back", "skinned_back", $values, -1, $set); createAttribute("skinned_left", "skinned_left", $values, -1, $set); createAttribute("skinned_right", "skinned_right", $values, -1, $set); } } function createAttribute($labelText, $attributeCode, $values = -1, $productTypes = -1, $setInfo = -1) { $labelText = trim($labelText); $attributeCode = trim($attributeCode); if ($labelText == '' || $attributeCode == '') { echo "Can't import the attribute with an empty label or code. LABEL= [{$labelText}] CODE= [{$attributeCode}]"; return false; } if ($values === -1) { $values = array(); } if ($productTypes === -1) { $productTypes = array();
$error = ""; //Did the user just submit changes to the attribute? if (!is_null($_POST) && count($_POST) > 0) { $isNewAttribute = is_null($_REQUEST) || !array_key_exists("id", $_REQUEST) || $_REQUEST["id"] == ''; $newCode = stripString1($_POST["attributeCode"]); $newName = stripString2($_POST["attributeName"]); $newDescription = stripString2($_POST["attributeDescription"]); if ($newCode == '') { die("An attribute must have a code"); } if ($newName == '') { die("An attribute must have a name"); } //Is this a new attribute? if ($isNewAttribute) { $result = createAttribute($newCode, $newName, $newDescription); $attributeID = intval($result[0]); $error = $result[1]; } else { $attributeID = intval($_POST["id"]); if ($newCode != $_POST["oldAttributeCode"] || $newName != $_POST["oldAttributeName"] || $newDescription != $_POST["oldAttributeDescription"]) { $error = updateAttribute($attributeID, $newCode, $newName, $newDescription); } } $labelChanges = array(); for ($i = 0; $i < $maxLabels; $i++) { //If nothing changed, skip this label $code = emptyAsNull(stripString1($_POST["labelCode"][$i])); $nameChange = $code != null && $_POST["oldLabelName"][$i] == '' || $_POST["labelName"][$i] != $_POST["oldLabelName"][$i]; $descChange = $_POST["labelDescription"][$i] != $_POST["oldLabelDescription"][$i]; $delete = checkDelete($code);