diff --git a/includes/commerce_product.inline_entity_form.inc b/includes/commerce_product.inline_entity_form.inc index 6a2137c..7e7687b 100644 --- a/includes/commerce_product.inline_entity_form.inc +++ b/includes/commerce_product.inline_entity_form.inc @@ -131,7 +131,6 @@ class CommerceProductInlineEntityFormController extends EntityInlineEntityFormCo */ public function settingsForm($field, $instance) { $form = parent::settingsForm($field, $instance); - $form['autogenerate_title'] = array( '#type' => 'checkbox', '#title' => t('Auto generate the product title'), @@ -144,6 +143,49 @@ class CommerceProductInlineEntityFormController extends EntityInlineEntityFormCo '#default_value' => $this->settings['use_variation_language'], ); + // Bulk product variation creation. + $fields = field_info_instances('commerce_product', $instance['bundle']); + + foreach ($fields as $name => $field) { + if ($field['widget']['module'] === 'options') { + $options_settings[$name] = $field['label']; + } + if ($field['widget']['module'] === 'image') { + // Todo: Maybe it's more than image, we have to figure out this. + $options_pair[$name] = $field['label']; + } + } + + $form['bulk_variation_creation'] = array( + '#type' => 'checkbox', + '#title' => t('Bulk product variation creation.'), + '#default_value' => $this->settings['bulk_variation_creation'], + ); + $form['bulk_variation_creation_settings'] = array( + '#type' => 'checkboxes', + '#title' => t('Settings bulk operation.'), + '#description' => t('The type of element used to select an option if used on an Add to Cart form.'), + '#options' => $options_settings, + '#default_value' => $this->settings['bulk_variation_creation_settings'], + '#states' => array( + 'visible' => array( + ':input[name="instance[widget][settings][type_settings][bulk_variation_creation]"]' => array('checked' => TRUE), + ), + ), + ); + $form['bulk_variation_creation_pair'] = array( + '#type' => 'checkboxes', + '#title' => t('Settings bulk operation pair.'), + '#description' => t('The type of element used to select an option if used on an Add to Cart form.'), + '#options' => $options_pair, + '#default_value' => $this->settings['bulk_variation_creation_pair'], + '#states' => array( + 'visible' => array( + ':input[name="instance[widget][settings][type_settings][bulk_variation_creation]"]' => array('checked' => TRUE), + ), + ), + ); + return $form; } diff --git a/inline_entity_form.module b/inline_entity_form.module index be41f13..fffc53d 100644 --- a/inline_entity_form.module +++ b/inline_entity_form.module @@ -781,6 +781,7 @@ function inline_entity_form_entity_form($controller, $entity_form, &$form_state) $entity_form['#entity'] = inline_entity_form_create_entity($entity_form['#entity_type'], $form_settings['bundle'], $entity_form['#parent_language']); $entity_form['#title'] = t('Add new @type_singular', array('@type_singular' => $labels['singular'])); $save_label = t('Create @type_singular', array('@type_singular' => $labels['singular'])); + $bulk_label = t('Bulk action: Creates @type_plural', array('@type_plural' => $labels['plural'])); } // Retrieve the form provided by the controller. @@ -812,11 +813,30 @@ function inline_entity_form_entity_form($controller, $entity_form, &$form_state) 'wrapper' => 'inline-entity-form-' . $entity_form['#ief_id'], ), ); + if ($controller->settings['bulk_variation_creation']) { + $form_state['bulk_variation_creation'] = array( + 'settings' => $controller->settings['bulk_variation_creation_settings'], + 'pair' => $controller->settings['bulk_variation_creation_pair'], + ); + $entity_form['actions']['ief_' . $entity_form['#op'] . '_bulk'] = array( + '#type' => 'submit', + '#value' => $bulk_label, + '#name' => 'ief-' . $entity_form['#op'] . '-bulk-' . $delta, + '#limit_validation_errors' => array($entity_form['#parents']), + '#ajax' => array( + 'callback' => 'inline_entity_form_get_element', + 'wrapper' => 'inline-entity-form-' . $entity_form['#ief_id'], + ), + ); + } // Add the appropriate submit handlers and their related data. if ($entity_form['#op'] == 'add') { $entity_form['actions']['ief_add_save']['#submit'] = array('inline_entity_form_close_form'); $entity_form['actions']['ief_add_cancel']['#submit'] = array('inline_entity_form_close_form'); + if ($controller->settings['bulk_variation_creation']) { + $entity_form['actions']['ief_add_bulk']['#submit'] = array('inline_entity_form_close_form'); + } } else { $entity_form['actions']['ief_edit_save']['#submit'] = array('inline_entity_form_close_row_form'); @@ -857,7 +877,7 @@ function inline_entity_form_entity_form($controller, $entity_form, &$form_state) function inline_entity_form_process_entity_form(&$entity_form, &$form_state) { // Only react on submissions triggered by the main submit buttons. $triggering_element_name = end($form_state['triggering_element']['#array_parents']); - if (empty($form_state['triggering_element']['#ief_submit_all']) && !in_array($triggering_element_name, array('ief_add_save', 'ief_edit_save'))) { + if (empty($form_state['triggering_element']['#ief_submit_all']) && !in_array($triggering_element_name, array('ief_add_save', 'ief_add_bulk', 'ief_edit_save'))) { return; } @@ -865,10 +885,74 @@ function inline_entity_form_process_entity_form(&$entity_form, &$form_state) { $instance = $form_state['inline_entity_form'][$ief_id]['instance']; // Instantiate the controller and validate the form. $controller = inline_entity_form_get_controller($instance); - $controller->entityFormValidate($entity_form, $form_state); + // $controller->entityFormValidate($entity_form, $form_state); + + // When is a bulk operation. + $clicked_button = $form_state['clicked_button']['#name']; + if ($instance['widget']['settings']['type_settings']['bulk_variation_creation'] && preg_match('@^ief-add-bulk-+@', $clicked_button)) { + + // Bulk variation creation settings. + $bvc_settings = $instance['widget']['settings']['type_settings']['bulk_variation_creation_settings']; + // Bulk variation creation pair. + $bvc_pair = $instance['widget']['settings']['type_settings']['bulk_variation_creation_pair']; + + foreach ($bvc_settings as $value) { + $operations[] = array( + 'field' => $value, + 'values' => $entity_form[$value][LANGUAGE_NONE]['#value'], + 'volume' => count($entity_form[$value][LANGUAGE_NONE]['#value']), + ); + } + uasort($operations, '_inline_entity_form_bulk_settings_cmp'); + + $operations = _inline_entity_form_bulk_value_fixing($operations); + + if (is_array($operations) && !form_get_errors() && !$form_state['rebuild']) { + + foreach ($operations as $item) { + + $bulk_entity_form = $entity_form; + $bulk_entity_form = array_replace_recursive_unique($bulk_entity_form, $item); + + // Read handler. + $bulk_form_state = $form_state; + $form_state_fiel_form = $bulk_form_state['values'][$instance['field_name']][LANGUAGE_NONE]['form']; + $form_state_fiel_form = _form_state_bulk_replace($form_state_fiel_form, $item); + $bulk_form_state['values'][$instance['field_name']][LANGUAGE_NONE]['form'] = $form_state_fiel_form; + + $controller->entityFormValidate($bulk_entity_form, $bulk_form_state); + + $controller->entityFormSubmit($bulk_entity_form, $bulk_form_state); + + $entity = $bulk_entity_form['#entity']; + + if ($bulk_entity_form['#op'] == 'add') { + // Determine the correct weight of the new element. + $weight = 0; + if (!empty($form_state['inline_entity_form'][$ief_id]['entities'])) { + $weight = max(array_keys($form_state['inline_entity_form'][$ief_id]['entities'])) + 1; + } + // Add the entity to form state, mark it for saving, and close the form. + $form_state['inline_entity_form'][$ief_id]['entities'][] = array( + 'entity' => clone $bulk_entity_form['#entity'], + 'weight' => $weight, + 'form' => NULL, + 'needs_save' => TRUE, + ); + + $bulk_entity_form = NULL; + } + } + } + + // I don't find other way to stop it. + return; + } // If validation passed, execute the submission handler. if (!form_get_errors() && !$form_state['rebuild']) { + $controller->entityFormValidate($entity_form, $form_state); + $controller->entityFormSubmit($entity_form, $form_state); $entity = $entity_form['#entity']; @@ -1483,3 +1567,117 @@ function inline_entity_form_pre_render_add_fieldset_markup($form) { return $form; } + +/** + * Comparation function helper for bulk creations process. + */ +function _inline_entity_form_bulk_settings_cmp($a, $b) { + if ($a['volume'] == $b['volume']) { + return 0; + } + + return ($a['volume'] < $b['volume']) ? 1 : -1; +} + +/** + * Function to fix each value per each variation. + */ +function _inline_entity_form_bulk_value_fixing($operations, array &$ret, $child = 0) { + + // This entry was weird. + if (empty($operations) && !is_array($operations)) { + return; + } + $end_level = FALSE; + foreach ($operations as $value) { + array_shift($operations); + + // The controler child tell me when is a full top item. + if (is_array($value['values']) && !$end_level) { + foreach ($value['values'] as $kvalues) { + + if (!empty($ret) && $child === 1) { + + $current_ret = end($ret); + $current_ret_key = key($ret); + + $ret[$current_ret_key] = $current_ret + array( + $value['field'] => array( + LANGUAGE_NONE => array('#value' => array($kvalues => $kvalues)), + ), + ); + + $child = 2; + } + elseif (!empty($ret) && $child === 2) { + $ret[] = $current_ret + array( + $value['field'] => array( + LANGUAGE_NONE => array('#value' => array($kvalues => $kvalues)), + ), + ); + } + else { + $ret[] = array( + $value['field'] => array( + LANGUAGE_NONE => array('#value' => array($kvalues => $kvalues)), + ), + ); + } + + if (!empty($operations) && is_array($operations)) { + _inline_entity_form_bulk_value_fixing($operations, $ret, 1); + } + } + } + + $end_level = TRUE; + } + + return $ret; +} + +/** + * Replace the array with a unique value. + */ +function array_replace_recursive_unique(array $array1, array &$array2) { + $merged = $array1; + + foreach ($array2 as $key => $value) { + if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { + $merged[$key] = array_replace_recursive_unique($merged[$key], $value); + } + else { + // Deleting the current state, to make it unique. + $merged = array(); + + $merged[$key] = $value; + } + } + + return $merged; +} + +/** + * Form state override helper in the bulk process. + */ +function _form_state_bulk_replace($array1, array &$array2) { + $merged = $array1; + + foreach ($array2 as $key => $value) { + if (is_array($value[LANGUAGE_NONE]) && is_array($merged) && !empty($merged[$key][LANGUAGE_NONE]) && is_array($merged[$key][LANGUAGE_NONE])) { + $merged[$key][LANGUAGE_NONE] = _form_state_bulk_replace($merged[$key][LANGUAGE_NONE], $value[LANGUAGE_NONE]); + } + if (is_array($value[LANGUAGE_NONE]) && !empty($merged->{$key}[LANGUAGE_NONE]) && is_array($merged->{$key}[LANGUAGE_NONE])) { + $merged->{$key}[LANGUAGE_NONE] = _form_state_bulk_replace($merged->{$key}[LANGUAGE_NONE], $value[LANGUAGE_NONE]); + } + else { + foreach ($merged as $mkey => $mvalue) { + if (!array_intersect($mvalue, $value)) { + unset($merged[$mkey]); + } + } + } + } + + return $merged; +}