/** * * @param unknown_type $attributeSetId * @param unknown_type $currentPageCode * @param unknown_type $itemToEdit * @param unknown_type $outputType * @return Ambigous <multitype:, string> */ public static function entities_attribute_box($attributeSetId, $currentPageCode, $itemToEdit, $outputType = 'box') { $box = $box['box'] = $box['boxContent'] = $box['generalTabContent'] = array(); /* Get the attribute set details in order to build the product interface */ $productAttributeSetDetails = wpshop_attributes_set::getAttributeSetDetails($attributeSetId, "'valid'"); $attribute_specification = array('page_code' => $currentPageCode, 'element_identifier' => $itemToEdit, 'field_id' => $currentPageCode . '_' . $itemToEdit . '_'); if (count($productAttributeSetDetails) > 0) { /* Read the attribute list in order to output */ $shortcodes_attr = ''; $shortcodes_to_display = false; $attribute_set_id_is_present = false; foreach ($productAttributeSetDetails as $productAttributeSetDetail) { $shortcodes = $currentTabContent = ''; $output_nb = 0; if (count($productAttributeSetDetail['attribut']) >= 1) { foreach ($productAttributeSetDetail['attribut'] as $attribute) { if (!empty($attribute->id)) { if ($attribute->code == 'product_attribute_set_id') { $attribute_set_id_is_present = true; } /** Generic part for attribute field output */ $value = wpshop_attributes::getAttributeValueForEntityInSet($attribute->data_type, $attribute->id, wpshop_entities::get_entity_identifier_from_code($currentPageCode), $itemToEdit, array('intrinsic' => $attribute->is_intrinsic, 'backend_input' => $attribute->backend_input)); $product_meta = get_post_meta($itemToEdit, '_wpshop_product_metadata', true); /** Check if value is empty and get value in meta if not empty */ $value = empty($value) && !empty($product_meta[$attribute->code]) ? $product_meta[$attribute->code] : (!empty($value) ? $value : null); /* Manage specific field as the attribute_set_id in product form */ if ($attribute->code == 'product_attribute_set_id') { $value = empty($value) ? $attributeSetId : $value; } $attribute_specification['current_value'] = $value; $attribute_output_def = wpshop_attributes::display_attribute($attribute->code, 'admin', $attribute_specification); if ($attribute_output_def['field_definition']['type'] != 'hidden' && $attribute->code != 'product_attribute_set_id') { $currentTabContent .= $attribute_output_def['field']; $shortcode_code_def = array(); $shortcode_code_def['attribute_' . str_replace('-', '_', sanitize_title($attribute_output_def['field_definition']['label']))]['main_code'] = 'wpshop_att_val'; $shortcode_code_def['attribute_' . str_replace('-', '_', sanitize_title($attribute_output_def['field_definition']['label']))]['attrs_exemple']['type'] = $attribute->data_type; $shortcode_code_def['attribute_' . str_replace('-', '_', sanitize_title($attribute_output_def['field_definition']['label']))]['attrs_exemple']['attid'] = $attribute->id; $shortcode_code_def['attribute_' . str_replace('-', '_', sanitize_title($attribute_output_def['field_definition']['label']))]['attrs_exemple']['pid'] = $itemToEdit; ob_start(); wps_shortcodes_ctr::output_shortcode('attribute_' . str_replace('-', '_', sanitize_title($attribute_output_def['field_definition']['label'])), $shortcode_code_def, 'wpshop_product_shortcode_display wpshop_product_attribute_shortcode_display wpshop_product_attribute_shortcode_display_' . str_replace('-', '_', sanitize_title($attribute_output_def['field_definition']['label'])) . ' wpshop_cls'); $shortcodes .= '<li class="wpshop_cls" >' . sprintf(__('Insertion code for the attribute %s for this product', 'wpshop'), '<span>' . __($attribute_output_def['field_definition']['label'], 'wpshop') . '</span>') . ob_get_contents() . '</li>'; ob_end_clean(); } else { if ($attribute->code == 'product_attribute_set_id') { $attribute_output_def['field_definition']['type'] = 'hidden'; } $currentTabContent .= wpshop_form::check_input_type($attribute_output_def['field_definition'], $attribute_output_def['field_definition']['input_domain']); } $output_nb++; } } $shortcode_code['attributes_set']['main_code'] = 'wpshop_att_group'; $shortcode_code['attributes_set']['attrs_exemple']['pid'] = $itemToEdit; $shortcode_code['attributes_set']['attrs_exemple']['sid'] = $productAttributeSetDetail['id']; ob_start(); wps_shortcodes_ctr::output_shortcode('attributes_set', $shortcode_code, 'wpshop_product_shortcode_display wpshop_product_attribute_group_shortcode_display wpshop_product_attribute_group_shortcode_display_' . str_replace('-', '_', sanitize_title($productAttributeSetDetail['name'])) . ' cls'); $attribute_group_display = sprintf(__('Insertion code for attribute group %s for this product', 'wpshop'), '<span>' . $productAttributeSetDetail['name'] . '</span>') . ob_get_contents() . '<ul class="" >' . $shortcodes . '</ul>'; ob_end_clean(); if (WPSHOP_PRODUCT_SHORTCODE_DISPLAY_TYPE == 'each-box') { $currentTabContent .= '<div class="wpshop_cls" ><strong>' . __('Shortcodes', 'wpshop') . '</strong> - <a href="#" class="show-hide-shortcodes">' . __('Display', 'wpshop') . '</a><div class="wpshop_product_shortcode_display wpshop_product_shortcode_display_container wpshopHide" >' . $attribute_group_display . '</div></div>'; } else { $shortcodes_attr .= $attribute_group_display; } if ($output_nb <= 0) { $currentTabContent = __('Nothing avaiblable here. You can go in attribute management interface in order to add content here.', 'wpshop'); } } if ($output_nb > 0) { $shortcodes_to_display = true; if ($outputType == 'box') { $box['box'][$productAttributeSetDetail['code']] = $productAttributeSetDetail['name']; $box['box'][$productAttributeSetDetail['code'] . '_backend_display_type'] = $productAttributeSetDetail['backend_display_type']; $box['boxContent'][$productAttributeSetDetail['code']] = ' <div id="wpshop_' . $currentPageCode . '_' . wpshop_tools::slugify($productAttributeSetDetail['code'], array('noAccent')) . '_form" >' . $currentTabContent . ' </div><div class="wpshop_cls" ></div>'; } else { if ($outputType == 'column') { $currentTabContent = str_replace('wpshop_form_input_element', 'wpshop_form_input_column', $currentTabContent); $currentTabContent = str_replace('wpshop_form_label', 'wpshop_form_label_column', $currentTabContent); $box['columnTitle'][$productAttributeSetDetail['code']] = __($productAttributeSetDetail['name'], 'wpshop'); $box['columnContent'][$productAttributeSetDetail['code']] = $currentTabContent; } } } } if (!$attribute_set_id_is_present) { /* Get attribute definition */ $attribute_def = wpshop_attributes::getElement('product_attribute_set_id', "'valid'", 'code'); /* Get attribute input definition */ $input = wpshop_attributes::get_attribute_field_definition($attribute_def, $attributeSetId, array_merge($attribute_specification, array('input_class' => ' wpshop_attributes_display', 'from' => 'admin'))); $input['type'] = 'hidden'; $box['boxMore'] = wpshop_form::check_input_type($input, $input['input_domain']); } /* Ajout de la boite permettant d'ajouter des valeurs aux attributs de type liste deroulante a la volee */ $dialog_title = __('New value for attribute', 'wpshop'); $dialog_identifier = 'wpshop_new_attribute_option_value_add'; $dialog_input_identifier = 'wpshop_new_attribute_option_value'; ob_start(); include WPSHOP_TEMPLATES_DIR . 'admin/add_new_element_dialog.tpl.php'; $box['boxMore'] .= ob_get_contents(); ob_end_clean(); $box['boxMore'] .= '<input type="hidden" name="wpshop_attribute_type_select_code" value="" id="wpshop_attribute_type_select_code" />'; if ($shortcodes_to_display) { switch (WPSHOP_PRODUCT_SHORTCODE_DISPLAY_TYPE) { case 'fixed-tab': case 'movable-tab': if ($outputType == 'box') { $box['box']['shortcode'] = __('Product Shortcodes', 'wpshop'); $box['boxContent']['shortcode'] = $shortcodes_attr; $box['box']['shortcode_backend_display_type'] = WPSHOP_PRODUCT_SHORTCODE_DISPLAY_TYPE; } else { $box['columnTitle']['shortcode'] = __('Product Shortcodes', 'wpshop'); $box['columnContent']['shortcode'] = $shortcodes_attr; } break; } } } return $box; }
/** * Define the different message and action after an action is send through the element interface */ function elementAction() { global $wpdb, $initialEavData; $pageMessage = $actionResult = ''; /* Start definition of output message when action is doing on another page */ /************ CHANGE THE FIELD NAME TO TAKE TO DISPLAY *************/ /****************************************************************************/ $action = isset($_REQUEST['action']) ? wpshop_tools::varSanitizer($_REQUEST['action']) : 'add'; $saveditem = isset($_REQUEST['saveditem']) ? wpshop_tools::varSanitizer($_REQUEST['saveditem']) : ''; if ($action != '' && $action == 'saveok' && $saveditem > 0) { $editedElement = self::getElement($saveditem); $pageMessage = '<img src="' . WPSHOP_SUCCES_ICON . '" alt="action success" class="wpshopPageMessage_Icon" />' . sprintf(__('%s succesfully saved', 'wpshop'), '<span class="bold" >' . $editedElement->code . '</span>'); } elseif ($action != '' && $action == 'deleteok' && $saveditem > 0) { $editedElement = self::getElement($saveditem, "'deleted'"); $pageMessage = '<img src="' . WPSHOP_SUCCES_ICON . '" alt="action success" class="wpshopPageMessage_Icon" />' . sprintf(__('%s succesfully deleted', 'wpshop'), '<span class="bold" >' . $editedElement->code . '</span>'); } /* Define the database operation type from action launched by the user */ $_REQUEST[self::getDbTable()]['default_value'] = str_replace('"', "'", $_REQUEST[self::getDbTable()]['default_value']); /************************* GENERIC **************************/ /*************************************************************************/ $pageAction = isset($_REQUEST[self::getDbTable() . '_action']) ? wpshop_tools::varSanitizer($_REQUEST[self::getDbTable() . '_action']) : ''; $id = isset($_REQUEST[self::getDbTable()]['id']) ? wpshop_tools::varSanitizer($_REQUEST[self::getDbTable()]['id']) : ''; if ($pageAction != '' && ($pageAction == 'edit' || $pageAction == 'editandcontinue')) { if (current_user_can('wpshop_edit_attributes')) { $_REQUEST[self::getDbTable()]['last_update_date'] = date('Y-m-d H:i:s'); if ($pageAction == 'delete') { if (current_user_can('wpshop_delete_attributes')) { $_REQUEST[self::getDbTable()]['status'] = 'deleted'; } else { $actionResult = 'userNotAllowedForActionDelete'; } } $actionResult = wpshop_database::update($_REQUEST[self::getDbTable()], $id, self::getDbTable()); } else { $actionResult = 'userNotAllowedForActionEdit'; } } elseif ($pageAction != '' && $pageAction == 'delete') { if (current_user_can('wpshop_delete_attributes')) { $_REQUEST[self::getDbTable()]['last_update_date'] = date('Y-m-d H:i:s'); $_REQUEST[self::getDbTable()]['status'] = 'deleted'; $actionResult = wpshop_database::update($_REQUEST[self::getDbTable()], $id, self::getDbTable()); } else { $actionResult = 'userNotAllowedForActionDelete'; } } elseif ($pageAction != '' && ($pageAction == 'save' || $pageAction == 'saveandcontinue' || $pageAction == 'add')) { if (current_user_can('wpshop_add_attributes')) { $_REQUEST[self::getDbTable()]['creation_date'] = date('Y-m-d H:i:s'); if (trim($_REQUEST[self::getDbTable()]['code']) == '') { $_REQUEST[self::getDbTable()]['code'] = $_REQUEST[self::getDbTable()]['frontend_label']; } $_REQUEST[self::getDbTable()]['code'] = wpshop_tools::slugify(str_replace("\\'", "_", str_replace('\\"', "_", $_REQUEST[self::getDbTable()]['code'])), array('noAccent', 'noSpaces', 'lowerCase', 'noPunctuation')); $code_exists = self::getElement($_REQUEST[self::getDbTable()]['code'], "'valid', 'moderated', 'deleted'", 'code'); if ((is_object($code_exists) || is_array($code_exists)) && count($code_exists) > 0) { $_REQUEST[self::getDbTable()]['code'] = $_REQUEST[self::getDbTable()]['code'] . '_' . (count($code_exists) + 1); } $actionResult = wpshop_database::save($_REQUEST[self::getDbTable()], self::getDbTable()); $id = $wpdb->insert_id; } else { $actionResult = 'userNotAllowedForActionAdd'; } } /* When an action is launched and there is a result message */ /************ CHANGE THE FIELD NAME TO TAKE TO DISPLAY *************/ /************ CHANGE ERROR MESSAGE FOR SPECIFIC CASE *************/ /****************************************************************************/ if ($actionResult != '') { $elementIdentifierForMessage = '<span class="bold" >' . $_REQUEST[self::getDbTable()]['frontend_label'] . '</span>'; if ($actionResult == 'error') { $pageMessage .= '<img src="' . WPSHOP_ERROR_ICON . '" alt="action error" class="wpshopPageMessage_Icon" />' . sprintf(__('An error occured while saving %s', 'wpshop'), $elementIdentifierForMessage); if (WPSHOP_DEBUG_MODE) { $pageMessage .= '<br/>' . $wpdb->last_error; } } elseif ($actionResult == 'done' || $actionResult == 'nothingToUpdate') { /* CHANGE HERE FOR SPECIFIC CASE */ /*****************************************************************************************************************/ /************************* CHANGE FOR SPECIFIC ACTION FOR CURRENT ELEMENT ******************/ /*****************************************************************************************************************/ /***********************************************************************************/ /************************* GENERIC ****************************/ /***********************************************************************************/ $pageMessage .= '<img src="' . WPSHOP_SUCCES_ICON . '" alt="action success" class="wpshopPageMessage_Icon" />' . sprintf(__('%s succesfully saved', 'wpshop'), $elementIdentifierForMessage); if ($pageAction == 'edit' || $pageAction == 'save') { wpshop_tools::wpshop_safe_redirect(admin_url('admin.php?page=' . self::getListingSlug() . "&action=saveok&saveditem=" . $id)); } elseif ($pageAction == 'add') { wpshop_tools::wpshop_safe_redirect(admin_url('admin.php?page=' . self::getListingSlug() . "&action=edit&id=" . $id)); } elseif ($pageAction == 'delete') { wpshop_tools::wpshop_safe_redirect(admin_url('admin.php?page=' . self::getListingSlug() . "&action=deleteok&saveditem=" . $id)); } } elseif ($actionResult == 'userNotAllowedForActionEdit' || $actionResult == 'userNotAllowedForActionAdd' || $actionResult == 'userNotAllowedForActionDelete') { $pageMessage .= '<img src="' . WPSHOP_ERROR_ICON . '" alt="action error" class="wpshopPageMessage_Icon" />' . __('You are not allowed to do this action', 'wpshop'); } } self::setMessage($pageMessage); }
/** * Update 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 update_eav_content($eav_content, $do_changes) { /* Update attributes fo a given entity if attributes are set to be updated for current version */ if (isset($eav_content['attributes']) && is_array($eav_content['attributes']) && count($eav_content['attributes']) > 0) { foreach ($eav_content['attributes'] as $entity_code => $attribute_definition) { foreach ($attribute_definition as $attribute_def) { $option_list_for_attribute = ''; if (isset($attribute_def['backend_input_values'])) { $option_list_for_attribute = $attribute_def['backend_input_values']; unset($attribute_def['backend_input_values']); } /* Get entity identifier from code */ $attribute_def['entity_id'] = wpshop_entities::get_entity_identifier_from_code($entity_code); $attribute_def['status'] = $attribute_def['attribute_status']; unset($attribute_def['attribute_status']); $attribute_def['last_update_date'] = current_time('mysql', 0); $wpdb->update(WPSHOP_DBT_ATTRIBUTE, $attribute_def, array('code' => $attribute_def['code'])); $attribute_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . WPSHOP_DBT_ATTRIBUTE . " WHERE code = %s", $attribute_def['code'])); /* Insert option values if there are some to add for the current attribute */ if ($option_list_for_attribute != '' && is_array($option_list_for_attribute)) { foreach ($option_list_for_attribute as $option_code => $option_value) { $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS, array('status' => 'valid', 'creation_date' => current_time('mysql', 0), 'attribute_id' => $attribute_id, 'label' => substr($option_code, 0, 2) != '__' ? $option_value : __(substr($option_code, 2), 'wpshop'), 'value' => $option_value)); if ($option_code == $attribute_def['default_value']) { $wpdb->update(WPSHOP_DBT_ATTRIBUTE, array('last_update_date' => current_time('mysql', 0), 'default_value' => $wpdb->insert_id), array('id' => $attribute_id, 'default_value' => $option_code)); } } } } } $do_changes = true; } /* Update attribute groups fo a given entity if attributes groups are set to be updated for current version */ if (is_array($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) { $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_SET, array('status' => 'valid', 'creation_date' => current_time('mysql', 0), 'entity_id' => $entity_id, 'name' => $set_name)); $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; } else { $new_set_section_infos = $set_group_infos; $new_set_section_infos['last_update_date'] = current_time('mysql', 0); $wpdb->update(WPSHOP_DBT_ATTRIBUTE_GROUP, $new_set_section_infos, array('id' => $attribute_set_section_id)); } if ($attribute_set_section_id > 0 && (isset($set_group_infos_details) && is_array($set_group_infos_details))) { if (count($set_group_infos_details) <= 0) { $wpdb->update(WPSHOP_DBT_ATTRIBUTE_DETAILS, array('last_update_date' => current_time('mysql', 0), 'status' => 'deleted'), array('entity_type_id' => $entity_id, 'attribute_set_id' => $attribute_set_id, 'attribute_group_id' => $attribute_set_section_id)); } else { $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 id FROM " . WPSHOP_DBT_ATTRIBUTE . " WHERE code = %s AND entity_id = %d", $attribute_code, $entity_id); $attribute_id = $wpdb->get_var($query); if ($attribute_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, 'position' => $position)); $position++; } } } } } } } } } $do_changes = true; } return $do_changes; }
</script>'; break; case 'attribute_set': switch ($elementType) { case 'attributeSetSection': switch ($action) { case 'saveNewAttributeSetSection': $attributeSetSectionName = wpshop_tools::varSanitizer($_REQUEST['attributeSetSectionName']); $attributeSetInfos = array(); $attributeSetInfos['id'] = ''; $attributeSetInfos['status'] = 'valid'; $attributeSetInfos['attribute_set_id'] = $elementIdentifier; $query = $wpdb->prepare("SELECT MAX(position) + 1 AS LAST_GROUP_POSITION FROM " . WPSHOP_DBT_ATTRIBUTE_GROUP . " WHERE attribute_set_id = %s", $elementIdentifier); $attributeSetInfos['position'] = $wpdb->get_var($query); $attributeSetInfos['creation_date'] = date('Y-m-d H:i:s'); $attributeSetInfos['code'] = wpshop_tools::slugify($attributeSetSectionName, array('noAccent', 'noSpaces', 'lowerCase')); $attributeSetInfos['name'] = $attributeSetSectionName; $more_script = ''; $attributeSetSectionCreation = wpshop_database::save($attributeSetInfos, WPSHOP_DBT_ATTRIBUTE_GROUP); if ($attributeSetSectionCreation == 'done') { $attributeSetSectionCreation_Result = '<img src=\'' . WPSHOP_SUCCES_ICON . '\' alt=\'action_success\' class=\'wpshopPageMessage_Icon\' />' . __('New section has been created successfully', 'wpshop'); $more_script = 'wpshop_go_to("#attribute_group_' . $wpdb->insert_id . '")'; } else { $attributeSetSectionCreation_Result = '<img src=\'' . WPSHOP_ERROR_ICON . '\' alt=\'action_error\' class=\'wpshopPageMessage_Icon\' />' . __('An error occured while saving new section', 'wpshop'); } echo wpshop_attributes_set::attributeSetDetailsManagement($elementIdentifier) . '<script type="text/javascript" >wpshop(document).ready(function(){ jQuery("#wpshop_new_set_section_add").dialog("close");jQuery("#wpshop_new_set_section_add").children("img").hide(); });wpshopShowMessage("' . $attributeSetSectionCreation_Result . '");hideShowMessage(5000);' . $more_script . '</script>'; break; case 'editAttributeSetSection': $attributeSetSectionName = wpshop_tools::varSanitizer($_REQUEST['attributeSetSectionName']); $attributeSetSectionId = wpshop_tools::varSanitizer($_REQUEST['attributeSetSectionId']); $attributeSetSectionDefault = wpshop_tools::varSanitizer($_REQUEST['attributeSetSectionDefault']);