/** * Build the output for an attribute field * * @param object $attribute The complete definition for an attribute * @param string $attribute_value Optionnal The current value for the attribute * @param array $specific_argument Optionnal The different parameters used for filter output * @return array The definition for the field used to display an attribute */ public static function get_attribute_field_definition($attribute, $attribute_value = '', $specific_argument = array()) { global $wpdb; $wpshop_price_attributes = unserialize(WPSHOP_ATTRIBUTE_PRICES); $wpshop_weight_attributes = unserialize(WPSHOP_ATTRIBUTE_WEIGHT); $input_def = array(); $input_def['option'] = $input_def['field_container_class'] = ''; $attributeInputDomain = !empty($specific_argument['field_custom_name_prefix']) || !empty($specific_argument['field_custom_name_prefix']) && $specific_argument['field_custom_name_prefix'] == 'empty' ? $specific_argument['field_custom_name_prefix'] : (!empty($specific_argument['page_code']) ? $specific_argument['page_code'] . '_' : '') . 'attribute[' . $attribute->data_type . ']'; $input_def['input_domain'] = $attributeInputDomain; $input_def['id'] = (!empty($specific_argument) && !empty($specific_argument['field_id']) ? $specific_argument['field_id'] . '_' : '') . 'attribute_' . $attribute->id; $input_def['intrinsec'] = $attribute->is_intrinsic; $input_def['name'] = $attribute->code; $input_def['type'] = wpshop_tools::defineFieldType($attribute->data_type, $attribute->frontend_input, $attribute->frontend_verification); $input_def['label'] = $attribute->frontend_label; $attribute_default_value = stripslashes($attribute->default_value); $input_def['value'] = $attribute_default_value; $input_def['default_value'] = $attribute_default_value; $input_def['is_unique'] = $attribute->is_unique; $input_def['_need_verification'] = $attribute->_need_verification; $input_def['required'] = $attribute->is_required; $input_def['frontend_verification'] = $attribute->frontend_verification; $input_def['data_type'] = $attribute->data_type; $input_def['data_type_to_use'] = $attribute->data_type_to_use; $input_def['backend_type'] = $attribute->backend_input; $input_def['frontend_type'] = $attribute->frontend_input; $input_def['is_used_in_quick_add_form'] = $attribute->is_used_in_quick_add_form; if (!empty($attribute_value) && !is_object($attribute_value)) { $input_def['value'] = $attribute_value; } else { if (!empty($attribute_value->value)) { $input_def['value'] = stripslashes($attribute_value->value); } else { if (!empty($specific_argument['element_identifier']) && empty($attribute_value) && get_post_status($specific_argument['element_identifier']) != 'auto-draft') { $input_def['value'] = ''; } } } $input_def['options'] = ''; $input_more_class = !empty($specific_argument['input_class']) ? $specific_argument['input_class'] : ''; if ($attribute->data_type == 'datetime') { $date_config = unserialize($attribute->default_value); if (($date_config['default_value'] == '' || $date_config['default_value'] == 'date_of_current_day') && $date_config['default_value'] == 'date_of_current_day') { $input_def['value'] = date('Y-m-d'); } else { /** Modification due to a message on eoxia forum: http://www.eoxia.com/forums/topic/bug-attribut-de-type-date-dans-fiche-produit-admin/ */ $input_def['value'] = !empty($attribute_value->value) && is_string($attribute_value->value) ? str_replace(" 00:00:00", "", $attribute_value->value) : (!empty($attribute_value) && is_string($attribute_value) ? str_replace(" 00:00:00", "", $attribute_value) : ''); } $input_more_class .= ' wpshop_input_datetime '; $field_script = '<script type="text/javascript" > jQuery(document).ready(function(){ wpshop("#' . $input_def['id'] . '").datepicker(); wpshop("#' . $input_def['id'] . '").datepicker("option", "dateFormat", "yy-mm-dd"); wpshop("#' . $input_def['id'] . '").datepicker("option", "changeMonth", true); wpshop("#' . $input_def['id'] . '").datepicker("option", "changeYear", true); wpshop("#' . $input_def['id'] . '").datepicker("option", "yearRange", "-90:+10"); wpshop("#' . $input_def['id'] . '").datepicker("option", "navigationAsDateFormat", true); wpshop("#' . $input_def['id'] . '").val("' . str_replace(" 00:00:00", "", $input_def['value']) . '");'; if (!empty($date_config['field_options']['attribute_type_date_options_available_date_past_futur'])) { if (!empty($date_config['field_options']['attribute_type_date_options_available_date_past_futur']['minDate'][0])) { $field_script .= ' wpshop("#' . $input_def['id'] . '").datepicker("option", "minDate", "' . $date_config['field_options']['attribute_type_date_options_available_date_past_futur']['minDate'][0] . '");'; } if (!empty($date_config['field_options']['attribute_type_date_options_available_date_past_futur']['maxDate'][0])) { $field_script .= ' wpshop("#' . $input_def['id'] . '").datepicker("option", "maxDate", "' . $date_config['field_options']['attribute_type_date_options_available_date_past_futur']['maxDate'][0] . '");'; } } $script_options = $script_options_params = array(); if (!empty($date_config['field_options']['attribute_type_date_options_day_to_show'])) { $day_to_show_list = ' '; foreach ($date_config['field_options']['attribute_type_date_options_day_to_show'] as $day_to_show) { $day_to_show_list .= '(date.getDay() == ' . $day_to_show . ') || '; } $script_options[] = '( ' . substr($day_to_show_list, 0, -4) . ' )'; } if (!empty($date_config['field_options']['attribute_type_date_options_available_date_type'][0])) { if (!empty($date_config['field_options']['attribute_type_date_options_available_date'])) { $available_date = ' '; foreach ($date_config['field_options']['attribute_type_date_options_available_date'] as $avalaible_date_list) { if (!empty($avalaible_date_list)) { $available_date .= '"' . $avalaible_date_list . '",'; } } $script_options_params[] = 'var dates = [' . substr($available_date, 0, -1) . ']'; $script_options[] = '(jQuery.inArray(dmy, dates) ' . ($date_config['field_options']['attribute_type_date_options_available_date_type'][0] == 'available' ? '!=' : '==') . ' -1)'; } } if (!empty($script_options)) { $field_script .= ' wpshop("#' . $input_def['id'] . '").datepicker("option", "beforeShowDay", function(date){ ' . implode(' ', $script_options_params) . '; var Y = date.getFullYear(); var M = (date.getMonth()+1); if( M < 10) { M = "0" + M; } var D = date.getDate(); if( D < 10) { D = "0" + D; } dmy = Y + "-" + M + "-" + D; if ( ' . implode(' && ', $script_options) . ' ) { return [true, ""]; } else { return [false,""]; } });'; } $field_script .= ' }); </script>'; $input_def['options'] .= $field_script; } if (in_array($attribute->backend_input, array('multiple-select', 'select', 'radio', 'checkbox'))) { $input_more_class .= !empty($specific_argument['no_chosen']) ? '' : ' chosen_select '; $input_def['type'] = !empty($specific_argument['from']) && $specific_argument['from'] == 'frontend' || !is_admin() && empty($specific_argument['from']) ? $attribute->frontend_input : $attribute->backend_input; $input_def['valueToPut'] = 'index'; $select_display = self::get_select_output($attribute, $specific_argument); if (empty($input_def['options_label']) && !empty($specific_argument) && (!empty($specific_argument['from']) && $specific_argument['from'] == 'frontend')) { $input_def['options_label']['original'] = true; } $input_def['options'] .= $select_display['more_input']; $input_def['possible_value'] = !empty($select_display) && !empty($select_display['possible_value']) ? $select_display['possible_value'] : ''; if (!is_admin()) { $input_def['options'] .= '<input type="hidden" value="' . str_replace("\\", "", $input_def['value']) . '" name="wpshop_product_attribute_' . $attribute->code . '_current_value" id="wpshop_product_attribute_' . $attribute->code . '_current_value" />'; } if (in_array($attribute->backend_input, array('multiple-select', 'checkbox')) && is_admin() && (empty($specific_argument['from']) || $specific_argument['from'] != 'frontend')) { $input_def['options'] .= wpshop_display::display_template_element('select_list_multiple_bulk_action', array('CURRENT_ATTRIBUTE_ID' => $input_def['id'], 'CURRENT_ATTRIBUTE_CODE' => $attribute->code), array(), 'admin'); } } $input_def['label_pointer'] = 'for="' . $input_def['id'] . '"'; if ($input_def['type'] == 'radio' || $input_def['type'] == 'checkbox') { $input_def['label_pointer'] = ''; } /* * Specifc treatment for price attributes */ if (WPSHOP_PRODUCT_PRICE_PILOT == 'HT' && $attribute->code == WPSHOP_PRODUCT_PRICE_TTC) { $input_def['option'] .= ' readonly="readonly" '; $input_more_class .= ' wpshop_prices_readonly'; } elseif (WPSHOP_PRODUCT_PRICE_PILOT == 'TTC' && $attribute->code == WPSHOP_PRODUCT_PRICE_HT) { $input_def['option'] .= ' readonly="readonly" '; $input_more_class .= ' wpshop_prices_readonly'; } if ($attribute->code == WPSHOP_PRODUCT_PRICE_TAX_AMOUNT) { $input_def['option'] .= ' readonly="readonly" '; $input_more_class .= ' wpshop_prices_readonly'; } $input_def['label'] = str_replace("\\", "", $input_def['label']); // $input_def['value'] = str_replace("\\", "", $input_def['value']); $input_def['option'] .= ' class="wpshop_product_attribute_' . $attribute->code . $input_more_class . ' ' . (is_admin() ? $attribute->backend_css_class : $attribute->frontend_css_class) . (!empty($attribute->frontend_verification) ? ' wps_attr_verification_' . $attribute->frontend_verification : '') . '" '; $input_def['title'] = !empty($attribute->frontend_help_message) ? ' title="' . $attribute->frontend_help_message . '" ' : ''; if ($attribute->is_intrinsic == 'yes' && (!empty($input_def['value']) || $input_def['value'] > 0)) { $input_def['option'] .= ' readonly="readonly" '; } /* * Add the unit to the attribute if attribute configuration is set to yes */ if ($attribute->is_requiring_unit == 'yes') { if (in_array($attribute->code, $wpshop_price_attributes) || WPSHOP_COST_OF_POSTAGE == $attribute->code) { $input_def['options'] .= ' <span class="attribute_currency" id="attribute_currency_' . $attribute->id . '" >' . wpshop_tools::wpshop_get_currency() . '</span>'; } elseif (in_array($attribute->code, $wpshop_weight_attributes)) { $weight_defaut_unity_option = get_option('wpshop_shop_default_weight_unity'); $query = $wpdb->prepare('SELECT name FROM ' . WPSHOP_DBT_ATTRIBUTE_UNIT . ' WHERE id=%d', $weight_defaut_unity_option); $unity = $wpdb->get_var($query); $input_def['options'] .= ' <span class="attribute_weight" id="attribute_weight_' . $attribute->id . '" >' . __($unity, 'wpshop') . '</span>'; } else { unset($unit_input_def); $unit_input_def['possible_value'] = wpshop_attributes_unit::get_unit_list_for_group($attribute->_unit_group_id); $unit_input_def['type'] = 'select'; $unit_input_def['option'] = ' class="wpshop_attribute_unit_input chosen_select" '; $unit_input_def['id'] = (!empty($specific_argument['page_code']) ? $specific_argument['page_code'] : null) . '_' . (!empty($specific_argument['element_identifier']) ? $specific_argument['element_identifier'] : null) . '_unit_attribute_' . $attribute->id; $unit_input_def['name'] = $attribute->code; $unit_input_def['value'] = !empty($attribute_value->unit_id) ? $attribute_value->unit_id : ''; if ($unit_input_def['value'] == '') { if ($attribute->_default_unit > 0) { $unit_input_def['value'] = $attribute->_default_unit; } else { $unit_input_def['value'] = wpshop_attributes_unit::get_default_unit_for_group($attribute->_unit_group_id); } } $input_def['options'] .= wpshop_form::check_input_type($unit_input_def, $attributeInputDomain . '[unit]'); } } /* * Add indication on postage cost tax */ if ($attribute->code == WPSHOP_COST_OF_POSTAGE) { $input_def['options'] .= ' <span class="attribute_currency" >' . __('ATI', 'wpshop') . '</span>'; } /* * Create the field output */ if (is_admin() && $attribute->data_type == 'datetime' && $attribute->is_user_defined == 'yes' && (empty($specific_argument['from']) || $specific_argument['from'] != 'frontend')) { $input_def['output'] = sprintf(__('You select this field to be defined by final customer into frontend part. To change this behaviour you have to change attribute option "%s"', 'wpshop'), __('is_user_defined', 'wpshop')); $input_def['options'] = ''; $input_def['label_pointer'] = ''; $input_def['option'] = substr($input_def['option'], 0, -2) . ' wpshop_attributes_is_user_defined_admin_field "'; $input_def['field_container_class'] .= 'wpshop_attributes_is_user_defined_admin_container'; } else { $input_def['output'] = wpshop_form::check_input_type($input_def, $attributeInputDomain); } return $input_def; }
/** * Load comboBox of unit or group of unit */ function wpshop_ajax_load_attribute_unit_list() { check_ajax_referer('wpshop_load_attribute_unit_list', 'wpshop_ajax_nonce'); $response = ''; $current_group = isset($_POST['current_group']) && !empty($_POST['current_group']) ? $_POST['current_group'] : null; $selected_list = isset($_POST['selected_list']) && !empty($_POST['selected_list']) ? $_POST['selected_list'] : null; $group = wpshop_tools::varSanitizer($current_group); $selected_list = wpshop_tools::varSanitizer($selected_list); if (!empty($group) && !empty($selected_list)) { /* Test if we want display the group unit list OR the unit list */ if ($selected_list == 'group unit') { $list = wpshop_attributes_unit::get_unit_group(); } else { $list = wpshop_attributes_unit::get_unit_list_for_group($group); } foreach ($list as $unit) { $response .= '<option value="' . $unit->id . '" ' . ($current_group == $unit->id && $selected_list == 'group unit' ? 'selected="selected"' : '') . '>' . $unit->name . '</option>'; } $result = array(true, $response); } else { $result = array(false, __('Incorrect order request', 'wpshop')); } echo json_encode($result); die; }