/**
  * Allows to create attributes for a custom post type from a csv file, allowing to create default attributes or import new attributes
  *
  * @param string $identifier The custom post type identifier to create attributes for. This identifier is unique into database
  *
  * @return array The different response element for the request. $result: Boolean representing if creation is OK / $container: Where the result must be placed into output code / $output: The html content to output
  */
 public static function create_cpt_attributes_from_csv_file($identifier, $custom_file = '')
 {
     global $wpdb;
     $output = $container = '';
     $result = true;
     $container = 'wpshop_cpt_' . $identifier . ' ul.wpshop_tools_default_datas_repair_attribute_container';
     $excluded_column = array('available_values');
     $file_uri = !empty($custom_file) ? $custom_file : WPSHOP_TEMPLATES_DIR . 'default_datas/' . $identifier . '-attributes.csv';
     if (is_file($file_uri)) {
         $entity_id = wpshop_entities::get_entity_identifier_from_code($identifier);
         $csv_file_default_data = file($file_uri, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
         $db_field_definition = explode(";", $csv_file_default_data[0]);
         $code_column = null;
         foreach ($db_field_definition as $column_index => $column_name) {
             if ($column_name == 'code') {
                 $code_column = $column_index;
                 continue;
             }
         }
         unset($csv_file_default_data[0]);
         if (!empty($code_column) || $code_column == 0) {
             foreach ($csv_file_default_data as $line_index => $line_content) {
                 $attribute_definition = explode(";", $line_content);
                 $query = $wpdb->prepare("SELECT id FROM " . WPSHOP_DBT_ATTRIBUTE . " WHERE code = %s AND entity_id = %d", $attribute_definition[$code_column], $entity_id);
                 $attribute_identifier = $wpdb->get_var($query);
                 if (empty($attribute_identifier)) {
                     $attribute_def = array();
                     $attribute_values = $default_value = null;
                     foreach ($db_field_definition as $column_index => $column_name) {
                         $column_name = trim($column_name);
                         if (!empty($column_name) && !in_array($column_name, $excluded_column)) {
                             $column_value = $attribute_definition[$column_index];
                             switch ($column_name) {
                                 case 'frontend_label':
                                     $column_value = __($column_value, 'wpshop');
                                     break;
                             }
                             $attribute_def[$column_name] = !empty($attribute_definition[$column_index]) ? $column_value : '';
                         } else {
                             switch ($column_name) {
                                 case 'available_values':
                                     $attribute_values = $attribute_definition[$column_index];
                                     break;
                             }
                         }
                         switch ($column_name) {
                             case 'default_value':
                                 $default_value = __($attribute_definition[$column_index], 'wpshop');
                                 break;
                         }
                     }
                     $attribute_def['entity_id'] = $entity_id;
                     $wpdb->insert(WPSHOP_DBT_ATTRIBUTE, $attribute_def);
                     $last_attribute_id = $wpdb->insert_id;
                     /**	Create values for select element	*/
                     if (!empty($attribute_values)) {
                         $list_of_values_to_create = explode(',', $attribute_values);
                         if (!empty($list_of_values_to_create)) {
                             foreach ($list_of_values_to_create as $value) {
                                 $value_element = explode('!:!:!', $value);
                                 $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS, array('status' => 'valid', 'creation_date' => current_time('mysql', 0), 'attribute_id' => $last_attribute_id, 'label' => __($value_element[0], 'wpshop'), 'value' => __(!empty($value_element[1]) ? $value_element[1] : strtolower($value_element[0]), 'wpshop')));
                                 if ($default_value == (!empty($value_element[1]) ? $value_element[1] : strtolower($value_element[0]))) {
                                     $wpdb->update(WPSHOP_DBT_ATTRIBUTE, array('last_update_date' => current_time('mysql', 0), 'default_value' => $wpdb->insert_id), array('id' => $last_attribute_id, 'default_value' => $default_value));
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $check_cpt = wpshop_entities::check_default_cpt_attributes($identifier, array(), false, $custom_file);
     $output = $check_cpt[1];
     return array($result, $container, $output);
     die;
 }