function wpshop_ajax_repair_default_datas() { global $wpdb; $output = ''; $container = ''; $result = ''; $selected_type = isset($_POST['type']) && !empty($_POST['type']) ? $_POST['type'] : null; $identifier = isset($_POST['identifier']) && !empty($_POST['identifier']) ? $_POST['identifier'] : null; switch ($selected_type) { case WPSHOP_NEWTYPE_IDENTIFIER_ENTITIES: $result = wpshop_entities::create_cpt_from_csv_file($identifier); break; case WPSHOP_DBT_ATTRIBUTE: $result = wpshop_entities::create_cpt_attributes_from_csv_file($identifier); break; } echo json_encode($result); die; }
/** * Create specific data in eav db model * * @param array $eav_content The complete array with all element to create into database * @param boolean $do_changes The current state of changes to do * * @return boolean If there are changes to do or not */ public static function add_content_to_eav($eav_content, $do_changes) { global $wpdb; /* Create entities if entites are set to be created for the current version */ if (isset($eav_content['entities']) && is_array($eav_content['entities']) && is_array($eav_content['entities']) && count($eav_content['entities']) > 0) { foreach ($eav_content['entities'] as $entity) { /* Creation de l'entité produit dans la table des posts */ wpshop_entities::create_cpt_from_csv_file($entity); } $do_changes = true; } /* Create attributes for a given entity if attributes are set to be created for current version */ if (!empty($eav_content['attributes']) && is_array($eav_content['attributes']) && is_array($eav_content['attributes']) && count($eav_content['attributes']) > 0) { foreach ($eav_content['attributes'] as $entity_code) { wpshop_entities::create_cpt_attributes_from_csv_file($entity_code); } $do_changes = true; } /* Create attribute groups for a given entity if attributes groups are set to be created for current version */ if (isset($eav_content['attribute_groups']) && is_array($eav_content['attribute_groups']) && count($eav_content['attribute_groups']) > 0) { foreach ($eav_content['attribute_groups'] as $entity_code => $attribute_set) { $entity_id = wpshop_entities::get_entity_identifier_from_code($entity_code); if ($entity_id > 0) { foreach ($attribute_set as $set_name => $set_groups) { $query = $wpdb->prepare("SELECT id FROM " . WPSHOP_DBT_ATTRIBUTE_SET . " WHERE entity_id = %d AND name = LOWER(%s)", $entity_id, wpshop_tools::slugify($set_name, array('noAccent', 'noSpaces', 'lowerCase'))); $attribute_set_id = $wpdb->get_var($query); if ($attribute_set_id <= 0) { $attribute_set_content = array('status' => 'valid', 'creation_date' => current_time('mysql', 0), 'entity_id' => $entity_id, 'name' => $set_name); if ($set_name == 'default') { $attribute_set_content['default_set'] = 'yes'; } $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_SET, $attribute_set_content); $attribute_set_id = $wpdb->insert_id; } if ($attribute_set_id > 0) { foreach ($set_groups as $set_group_infos) { $set_group_infos_details = $set_group_infos['details']; unset($set_group_infos['details']); /* Change an attribute set status if definition specify this param */ if (isset($set_group_infos['status'])) { $wpdb->update(WPSHOP_DBT_ATTRIBUTE_SET, array('last_update_date' => current_time('mysql', 0), 'status' => $set_group_infos['status']), array('id' => $attribute_set_id)); } $query = $wpdb->prepare("SELECT id FROM " . WPSHOP_DBT_ATTRIBUTE_GROUP . " WHERE attribute_set_id = %d AND code = LOWER(%s)", $attribute_set_id, $set_group_infos['code']); $attribute_set_section_id = $wpdb->get_var($query); if ($attribute_set_section_id <= 0) { $new_set_section_infos = $set_group_infos; $new_set_section_infos['status'] = isset($new_set_section_infos['status']) ? $new_set_section_infos['status'] : 'valid'; $new_set_section_infos['creation_date'] = current_time('mysql', 0); $new_set_section_infos['attribute_set_id'] = $attribute_set_id; $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_GROUP, $new_set_section_infos); $attribute_set_section_id = $wpdb->insert_id; } if ($attribute_set_section_id > 0 && (isset($set_group_infos_details) && is_array($set_group_infos_details) && count($set_group_infos_details) > 0)) { $query = $wpdb->prepare("SELECT MAX(position) AS position FROM " . WPSHOP_DBT_ATTRIBUTE_DETAILS . " WHERE entity_type_id = %d AND attribute_set_id = %d AND attribute_group_id = %d", $entity_id, $attribute_set_id, $attribute_set_section_id); $last_position = $wpdb->get_var($query); $position = (int) $last_position + 1; foreach ($set_group_infos_details as $attribute_code) { $query = $wpdb->prepare("SELECT * FROM " . WPSHOP_DBT_ATTRIBUTE . " WHERE code = %s AND entity_id = %d", $attribute_code, $entity_id); $attribute_id = $wpdb->get_row($query); if ($attribute_id->id > 0) { $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_DETAILS, array('status' => 'valid', 'creation_date' => current_time('mysql', 0), 'entity_type_id' => $entity_id, 'attribute_set_id' => $attribute_set_id, 'attribute_group_id' => $attribute_set_section_id, 'attribute_id' => $attribute_id->id, 'position' => $position)); $position++; } } } } } } } } $do_changes = true; } return $do_changes; }