View source
<?php
module_load_include('inc', 'commerce_bpc', 'commerce_bpc.hooks_list');
module_load_include('inc', 'commerce_bpc', 'commerce_bpc.tokens');
module_load_include('inc', 'commerce_bpc', 'commerce_bpc.settings');
function commerce_bpc_help($path, $arg) {
switch ($path) {
case 'admin/help#commerce_bpc':
return t('This module helps you create groups of related products quickly and easily.
To use it, you must have at least one Product Type that has one or more fields attached to it.
If you wish to attach the products all at once to a Display Node as part of the Bulk Creation process,
you must first have created a Display Node Type that has a commerce_product_reference field which can accept multiple products.');
}
}
function commerce_bpc_menu() {
$items['admin/commerce/products/add-bulk'] = array(
'title' => 'Bulk add products',
'description' => 'Add a new group of products for sale in bulk.',
'page callback' => 'commerce_bpc_add_page',
'access callback' => 'commerce_bpc_product_add_any_access',
'file' => 'commerce_bpc.pages.inc',
);
$items['admin/commerce/config/commerce_bpc'] = array(
'title' => 'Bulk product creation',
'description' => 'Settings for the bulk product creation functionality',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_bpc_pattern_settings_form',
),
'access arguments' => array(
'configure commerce bpc',
),
'file' => 'commerce_bpc.admin.inc',
'weight' => 1,
);
$items['admin/commerce/config/commerce_bpc/patterns'] = array(
'title' => 'Patterns',
'access arguments' => array(
'configure commerce bpc',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
foreach (commerce_product_types() as $type => $product_type) {
if (commerce_bpc_valid_product_type($type)) {
$items['admin/commerce/products/add-bulk/' . $type] = array(
'title' => 'Bulk add @name',
'title arguments' => array(
'@name' => $product_type['name'],
),
'description' => $product_type['description'],
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_bpc_create_bulk_form',
$type,
),
'file' => 'commerce_bpc.forms.inc',
'access callback' => 'commerce_product_access',
'access arguments' => array(
'create',
commerce_product_new($type),
),
);
$items['admin/commerce/products/types/' . $type . '/commerce_bpc'] = array(
'title' => 'Bulk product creation',
'description' => 'Settings for the bulk product creation functionality',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_bpc_pattern_settings_form',
$type,
),
'access arguments' => array(
'configure commerce bpc',
),
'file' => 'commerce_bpc.admin.inc',
'weight' => 10,
'type' => MENU_LOCAL_TASK,
);
$items['admin/commerce/products/types/' . $type . '/commerce_bpc/patterns'] = array(
'title' => 'Patterns',
'access arguments' => array(
'configure commerce bpc',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
}
}
return $items;
}
function commerce_bpc_permission() {
return array(
'configure commerce bpc' => array(
'title' => t('Configure Commerce bulk product creation'),
'description' => t('Allows users to update settings pertaining to Product bulk creation.'),
'restrict access' => TRUE,
),
);
}
function commerce_bpc_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if ($root_path == 'admin/commerce/products') {
$item = menu_get_item('admin/commerce/products/add-bulk');
if ($item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
}
function commerce_bpc_field_create_instance($instance) {
if ($instance['entity_type'] == 'commerce_product' && array_key_exists($instance['bundle'], commerce_product_types())) {
variable_set('menu_rebuild_needed', TRUE);
}
}
function commerce_bpc_field_update_instance($instance) {
if ($instance['entity_type'] == 'commerce_product') {
variable_set('menu_rebuild_needed', TRUE);
}
}
function commerce_bpc_field_delete_instance($instance) {
if ($instance['entity_type'] == 'commerce_product') {
variable_set('menu_rebuild_needed', TRUE);
}
}
function commerce_bpc_create_bulk_products($product_type, $combinations = array(), $static_values = array(), $extras = array()) {
$extras += array(
'uid' => 1,
);
$bulk_products = array();
foreach ($combinations as $combination) {
$bulk_products[] = commerce_bpc_create_product($product_type, $combination, $static_values, $extras);
}
return $bulk_products;
}
function commerce_bpc_create_product($product_type, $combination, $static_values, $extras) {
$form_state = array();
$form_state['values'] = $static_values;
$form = array();
$form['#parents'] = array();
$new_product = commerce_product_new($product_type);
$new_product->status = $extras['status'];
$new_product->uid = $extras['uid'];
$new_product->language = !empty($extras['language']) ? $extras['language'] : LANGUAGE_NONE;
$data = array();
$data['bulk_data'] = array(
'combination' => $combination,
'static_values' => $static_values,
'product_type' => $product_type,
);
$new_product->sku = token_replace($extras['sku_pattern'], $data, array(
'sanitize' => FALSE,
));
$new_product->title = token_replace($extras['title_pattern'], $data, array(
'sanitize' => FALSE,
));
foreach ($combination as $field => $value) {
$form_state['values'][$field] = $value;
}
field_attach_submit('commerce_product', $new_product, $form, $form_state);
commerce_product_save($new_product);
return $new_product->product_id;
}
function commerce_bpc_get_combinations($form, &$form_state) {
$combinations = array(
array(),
);
$hook = 'commerce_bpc_get_combinations';
foreach (module_implements($hook) as $module) {
$function = $module . '_' . $hook;
$function($form, $form_state, $combinations);
}
return $combinations;
}
function commerce_bpc_valid_product_type($type) {
$instances = field_info_instances('commerce_product', $type);
foreach ($instances as $field_name => $instance) {
if (commerce_bpc_is_combination_field($instance)) {
return TRUE;
}
}
return FALSE;
}
function commerce_bpc_product_add_any_access() {
if (user_access('administer commerce_product entities')) {
return TRUE;
}
foreach (commerce_product_types() as $type => $product_type) {
if (commerce_product_access('create', commerce_product_new($type)) && commerce_bpc_valid_product_type($type)) {
return TRUE;
}
}
return FALSE;
}
function commerce_bpc_form_field_ui_field_edit_form_alter(&$form, $form_state) {
$settings = $form['#instance'];
if ($form['#instance']['entity_type'] == 'commerce_product') {
$form['instance']['commerce_bpc'] = array(
'#type' => 'fieldset',
'#title' => t('Bulk product creation'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['instance']['commerce_bpc']['show_field'] = array(
'#type' => 'checkbox',
'#title' => t('Show this field on the bulk product creation form'),
'#default_value' => !isset($settings['commerce_bpc']['show_field']) ? TRUE : $settings['commerce_bpc']['show_field'],
);
if ($form['#field']['module'] == 'list') {
$form['instance']['commerce_bpc']['is_static'] = array(
'#type' => 'checkbox',
'#title' => t('Treat this field as static'),
'#default_value' => !isset($settings['commerce_bpc']['is_static']) ? FALSE : $settings['commerce_bpc']['is_static'],
'#description' => t('List fields can either be used to create combinations, or can be set to be static, in which case all bulk-created products will share the same value for the field.'),
);
}
}
}
function _commerce_bpc_get_value($array, $path, $default) {
$return = drupal_array_get_nested_value($array, $path, $key_exists);
if (!$key_exists) {
$return = $default;
}
return $return;
}
function _commerce_bpc_show_tokens($type, $avail_tokens = array()) {
$rows = array();
foreach ($avail_tokens as $token_name => $info) {
$rows[] = array(
'[' . $type . ':' . $token_name . ']',
$info['name'],
$info['description'],
);
}
$tokens = array(
'#type' => 'fieldset',
'#title' => t('Tokens'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$tokens_table = array(
'header' => array(
t('Token'),
t('Label'),
t('Desription'),
),
'rows' => $rows,
'attributes' => array(),
'caption' => t('Available Tokens'),
'colgroups' => array(),
'sticky' => FALSE,
'empty' => '',
);
$tokens['available_tokens'] = array(
'#type' => 'item',
'#title' => t('You may use the following tokens:'),
'#markup' => theme_table($tokens_table),
);
return $tokens;
}
function commerce_bpc_is_combination_field($instance) {
return in_array(TRUE, module_invoke_all('commerce_bpc_is_combination_field', $instance));
}
function commerce_bpc_next_destination() {
$destinations = !empty($_REQUEST['destinations']) ? $_REQUEST['destinations'] : array();
if (!empty($destinations)) {
unset($_REQUEST['destinations']);
$destination = array_shift($destinations);
if (!is_array($destination)) {
$destination = array(
$destination,
array(),
);
}
if (!empty($destinations)) {
$destination[1]['query']['destinations'] = $destinations;
}
return $destination;
}
else {
return 'admin/commerce/products';
}
}
function commerce_bpc_commerce_bpc_commerce_price_form_element_alter(&$form, &$form_state, &$path) {
$field_widget = drupal_array_get_nested_value($form, $path);
$lang = $field_widget['#language'];
$field_name = $field_widget[$lang]['#field_name'];
$form['product'][$field_name] = drupal_array_get_nested_value($form, $path);
drupal_array_set_nested_value($form, $path, NULL);
$path = array(
'product',
array(
'product',
$field_name,
),
);
if (!isset($form_state['commerce_bpc']['commerce_price']) || !in_array($field_name, $form_state['commerce_bpc']['commerce_price']['moved_fields'])) {
$form_state['commerce_bpc']['commerce_price']['moved_fields'][] = $field_name;
}
}
function commerce_bpc_commerce_bpc_submit_alter(&$form, &$form_state) {
if (isset($form_state['commerce_bpc']['commerce_price']['moved_fields'])) {
foreach ($form_state['commerce_bpc']['commerce_price']['moved_fields'] as $field_name) {
$form_state['values']['static_values'][$field_name] = $form_state['values'][$field_name];
unset($form_state['values'][$field_name]);
}
}
}