/**
 * Variation uniq item creation
 */
function ajax_new_single_variation_definition()
{
    check_ajax_referer('wpshop_variation_management', 'wpshop_ajax_nonce');
    $output = '';
    $current_post_id = isset($_POST['current_post_id']) ? wpshop_tools::varSanitizer($_POST['current_post_id']) : null;
    /*	Get the list of values of the attribute to affect to a variation	*/
    $attribute_for_variation = wpshop_attributes::get_variation_available_attribute_display($current_post_id, 'single');
    $output = $attribute_for_variation[0];
    /**	Display specific element for variation	*/
    $tpl_component['ADMIN_VARIATION_SPECIFIC_DEFINITION_CONTAINER_CLASS'] = '';
    $tpl_component['VARIATION_IDENTIFIER'] = 'new';
    $tpl_component['VARIATION_DEFINITION'] = wpshop_attributes::get_variation_attribute(array('input_class' => ' new_variation_specific_values', 'field_name' => wpshop_products::current_page_variation_code . '[' . $tpl_component['VARIATION_IDENTIFIER'] . ']', 'page_code' => wpshop_products::current_page_variation_code, 'field_id' => wpshop_products::current_page_variation_code . '_' . $tpl_component['VARIATION_IDENTIFIER'], 'variation_dif_values' => ''));
    $output .= wpshop_display::display_template_element('wpshop_admin_variation_item_specific_def', $tpl_component, array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT => $current_post_id, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION => $tpl_component['VARIATION_IDENTIFIER']), 'admin');
    $tpl_component = array();
    $tpl_component['ADMIN_VARIATION_SINGLE_CREATION_FORM_CONTENT'] = $output;
    $tpl_component['ADMIN_VARIATION_CREATION_FORM_HEAD_PRODUCT_ID'] = $current_post_id;
    $tpl_component['ADMIN_VARIATION_CREATION_FORM_HEAD_NOUNCE'] = wp_create_nonce("wpshop_variation_management");
    $tpl_component['ADMIN_VARIATION_CREATION_FORM_ACTION'] = 'add_new_single_variation';
    echo wpshop_display::display_template_element('wpshop_admin_new_single_variation_form', $tpl_component, array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT => $current_post_id, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION => ''), 'admin');
    die;
}
 /**
  * Affichage des variations d'un produit dans l'administration
  *
  * @param integer $head_product L'identifiant du produit dont on veut afficher les variations
  * @return string Le code html permettant l'affichage des variations dans l'interface d'édition du produit
  */
 public static function display_variation_admin($head_product)
 {
     $output = '';
     $productCurrency = wpshop_tools::wpshop_get_currency();
     /*	Récupération de la liste des variations pour le produit en cours d'édition	*/
     $variations = self::get_variation($head_product);
     $price_piloting = get_option('wpshop_shop_price_piloting');
     /*	Affichage de la liste des variations pour le produit en cours d'édition	*/
     if (!empty($variations) && is_array($variations)) {
         $existing_variation_list = wpshop_display::display_template_element('wpshop_admin_existing_variation_controller', array(), array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT => $head_product), 'admin');
         foreach ($variations as $variation) {
             $tpl_component = array();
             $tpl_component['ADMIN_EXISTING_VARIATIONS_CLASS'] = ' wpshop_variation_' . self::currentPageCode;
             $tpl_component['VARIATION_IDENTIFIER'] = $variation['post']->ID;
             $tpl_component['VARIATION_DETAIL'] = '  ';
             $p = !empty($variation['variation_dif']['product_price']) || !empty($variation['variation_dif']['price_ht']) ? !empty($price_piloting) && $price_piloting == 'HT' && !empty($variation['variation_dif']['price_ht']) ? $variation['variation_dif']['price_ht'] : !empty($variation['variation_dif']['product_price']) ? $variation['variation_dif']['product_price'] : 0 : 0;
             $tpl_component['VARIATION_DETAIL_PRICE'] = number_format($p, 2, '.', '') . ' ' . $productCurrency . ' ' . (!empty($price_piloting) && $price_piloting == 'HT' ? __('ET', 'wpshop') : __('ATI', 'wpshop'));
             if (!empty($price_piloting) && $price_piloting == 'HT') {
             } else {
             }
             $post_obj = $variation['post'];
             $parent_product_infos = wpshop_products::get_parent_variation($post_obj->ID);
             if (!empty($parent_product_infos)) {
                 $parent_post = $parent_product_infos['parent_post'];
                 $product_option_postmeta = get_post_meta($parent_post->ID, '_wpshop_variation_defining', true);
                 if (!empty($product_option_postmeta['options']['price_behaviour']) && !empty($product_option_postmeta['options']['price_behaviour'][0]) && $product_option_postmeta['options']['price_behaviour'][0] == 'addition') {
                     if (!empty($price_piloting) && $price_piloting == 'HT') {
                         $product_price = (!empty($variation['variation_dif']['price_ht']) ? $variation['variation_dif']['price_ht'] : 0) + $parent_product_infos['parent_post_meta']['price_ht'];
                     } else {
                         $product_price = (!empty($variation['variation_dif']['product_price']) ? $variation['variation_dif']['product_price'] : 0) + $parent_product_infos['parent_post_meta']['product_price'];
                     }
                     $tpl_component['VARIATION_DETAIL_SALE_PRICE_INDICATION'] = __('Variation price combined with the parent product price', 'wpshop');
                 } else {
                     if (!empty($price_piloting) && $price_piloting == 'HT') {
                         $product_price = !empty($variation['variation_dif']['price_ht']) ? $variation['variation_dif']['price_ht'] : 0;
                     } else {
                         $product_price = !empty($variation['variation_dif']['product_price']) ? $variation['variation_dif']['product_price'] : 0;
                     }
                     $tpl_component['VARIATION_DETAIL_SALE_PRICE_INDICATION'] = __('Only variation\'s price is used', 'wpshop');
                 }
                 $product_price = number_format(str_replace(',', '.', $product_price), 2, '.', '') . ' ' . $productCurrency . ' ' . (!empty($price_piloting) && $price_piloting == 'HT' ? __('ET', 'wpshop') : __('ATI', 'wpshop'));
                 $tpl_component['VARIATION_DETAIL_SALE_PRICE'] = $product_price;
             }
             if (!empty($variation['variation_def'])) {
                 foreach ($variation['variation_def'] as $variation_key => $variation_value) {
                     if (!empty($variation_value)) {
                         $attribute_def_for_variation = wpshop_attributes::getElement($variation_key, "'valid'", 'code');
                         $tpl_component['VARIATION_DETAIL'] .= '<input type="hidden" name="' . self::current_page_variation_code . '[' . $variation['post']->ID . '][attribute][' . $attribute_def_for_variation->data_type . '][' . $variation_key . ']" value="' . $variation_value . '" />' . wpshop_display::display_template_element('wpshop_admin_variation_item_def_header', array('VARIATION_ATTRIBUTE_CODE' => $attribute_def_for_variation->frontend_label, 'VARIATION_ATTRIBUTE_CODE_VALUE' => stripslashes(wpshop_attributes::get_attribute_type_select_option_info($variation_value, 'label', $attribute_def_for_variation->data_type_to_use, true))), array(), 'admin');
                         $tpl_component['VARIATION_IMAGE_CHOICE'] = '';
                         /** Define Link image to variation interface **/
                         if (!empty($product_option_postmeta) && !empty($product_option_postmeta['attributes']) && !empty($product_option_postmeta['variation_type']) && (count($product_option_postmeta['attributes']) == 1 && $product_option_postmeta['variation_type'] == 'single' || $product_option_postmeta['variation_type'] == 'combined')) {
                             $pictures = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => $head_product));
                             $media_id_data = get_post_meta($head_product, '_wps_product_media', true);
                             if (!empty($media_id_data)) {
                                 $medias_ids = explode(',', $media_id_data);
                                 if (!empty($medias_ids)) {
                                     foreach ($medias_ids as $media_id) {
                                         if (!empty($media_id)) {
                                             $pictures[] = get_post($media_id);
                                         }
                                     }
                                 }
                             }
                             $pictures_data = '';
                             if (!empty($pictures)) {
                                 $selected_picture = get_post_meta($variation['post']->ID, '_wps_variation_attached_picture', true);
                                 $done_picture = array();
                                 foreach ($pictures as $picture) {
                                     if (!in_array($picture->ID, $done_picture)) {
                                         $checked = !empty($selected_picture) && $selected_picture == $picture->ID ? 'checked="checked"' : '';
                                         $pictures_data .= wpshop_display::display_template_element('wpshop_admin_variation_picture_choice_element', array('PICTURE_CHOICE_VARIATION_ID' => $picture->ID, 'PRODUCT_VARIATION_ID' => $variation['post']->ID, 'PICTURE_CHOICE_SELECTED' => $checked, 'PICTURE_CHOICE_VARIATION_IMG' => wp_get_attachment_image($picture->ID, 'thumbnail')), array(), 'admin');
                                         $done_picture[] = $picture->ID;
                                     }
                                 }
                             }
                             $tpl_component['VARIATION_IMAGE_CHOICE'] = wpshop_display::display_template_element('wpshop_admin_variation_picture_choice_container', array('PICTURE_CHOICE_CONTAINER_CONTENT' => $pictures_data), array(), 'admin');
                         }
                     }
                 }
             }
             $tpl_component['VARIATION_DETAIL'] = substr($tpl_component['VARIATION_DETAIL'], 0, -2);
             $tpl_component['ADMIN_VARIATION_SPECIFIC_DEFINITION_CONTAINER_CLASS'] = ' wpshopHide';
             $tpl_component['VARIATION_DEFINITION'] = wpshop_attributes::get_variation_attribute(array('input_class' => ' ', 'field_name' => wpshop_products::current_page_variation_code . '[' . $variation['post']->ID . ']', 'page_code' => self::current_page_variation_code, 'field_id' => self::current_page_variation_code . '_' . $variation['post']->ID, 'variation_dif_values' => !empty($variation['variation_dif']) ? $variation['variation_dif'] : array()));
             $tpl_component['VARIATION_DEFINITION_CONTENT'] = wpshop_display::display_template_element('wpshop_admin_variation_item_specific_def', $tpl_component, array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT => $head_product, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION => $variation['post']->ID), 'admin');
             /*	Add the variation definition to output	*/
             $existing_variation_list .= wpshop_display::display_template_element('wpshop_admin_variation_item_def', $tpl_component, array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT => $head_product, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION => $variation['post']->ID), 'admin');
         }
         $output .= wpshop_display::display_template_element('wpshop_admin_existing_variation_list', array('ADMIN_EXISTING_VARIATIONS_CONTAINER_CLASS' => '', 'ADMIN_EXISTING_VARIATIONS_CONTAINER' => $existing_variation_list), array(), 'admin');
         /*	Reset de la liste des résultats pour éviter les comportements indésirables	*/
         wp_reset_query();
     } else {
         $output = __('No variation found for this product. Please use button above for create one', 'wpshop');
     }
     return $output;
 }