View source
<?php
function commerce_product_ui_menu() {
$items = array();
$items['admin/commerce/products/add'] = array(
'title' => 'Add a product',
'description' => 'Add a new product for sale.',
'page callback' => 'commerce_product_ui_add_page',
'access callback' => 'commerce_product_ui_product_add_any_access',
'weight' => 10,
'file' => 'includes/commerce_product_ui.products.inc',
);
foreach (commerce_product_types() as $type => $product_type) {
$items['admin/commerce/products/add/' . strtr($type, array(
'_' => '-',
))] = array(
'title' => 'Create !name',
'title arguments' => array(
'!name' => $product_type['name'],
),
'description' => $product_type['description'],
'page callback' => 'commerce_product_ui_product_form_wrapper',
'page arguments' => array(
commerce_product_new($type),
),
'access callback' => 'commerce_product_access',
'access arguments' => array(
'create',
commerce_product_new($type),
),
'file' => 'includes/commerce_product_ui.products.inc',
);
}
$items['admin/commerce/products/%commerce_product'] = array(
'title callback' => 'commerce_product_ui_product_title',
'title arguments' => array(
3,
),
'page callback' => 'commerce_product_ui_product_form_wrapper',
'page arguments' => array(
3,
),
'access callback' => 'commerce_product_access',
'access arguments' => array(
'update',
3,
),
'weight' => 0,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'includes/commerce_product_ui.products.inc',
);
$items['admin/commerce/products/%commerce_product/edit'] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['admin/commerce/products/%commerce_product/delete'] = array(
'title' => 'Delete',
'page callback' => 'commerce_product_ui_product_delete_form_wrapper',
'page arguments' => array(
3,
),
'access callback' => 'commerce_product_access',
'access arguments' => array(
'delete',
3,
),
'type' => MENU_LOCAL_TASK,
'weight' => 20,
'context' => MENU_CONTEXT_INLINE,
'file' => 'includes/commerce_product_ui.products.inc',
);
$items['admin/commerce/products/types'] = array(
'title' => 'Product types',
'description' => 'Manage products types for your store.',
'page callback' => 'commerce_product_ui_types_overview',
'access arguments' => array(
'administer product types',
),
'type' => MENU_LOCAL_TASK,
'weight' => 0,
'file' => 'includes/commerce_product_ui.types.inc',
);
$items['admin/commerce/products/types/add'] = array(
'title' => 'Add product type',
'page callback' => 'commerce_product_ui_product_type_form_wrapper',
'page arguments' => array(
commerce_product_ui_product_type_new(),
),
'access arguments' => array(
'administer product types',
),
'type' => MENU_LOCAL_ACTION,
'file' => 'includes/commerce_product_ui.types.inc',
);
foreach (commerce_product_types() as $type => $product_type) {
$type_arg = strtr($type, '_', '-');
$items['admin/commerce/products/types/' . $type_arg] = array(
'title' => $product_type['name'],
'page callback' => 'commerce_product_ui_product_type_form_wrapper',
'page arguments' => array(
$type,
),
'access arguments' => array(
'administer product types',
),
'file' => 'includes/commerce_product_ui.types.inc',
);
if ($product_type['module'] == 'commerce_product_ui') {
$items['admin/commerce/products/types/' . $type_arg . '/edit'] = array(
'title' => 'Edit',
'access callback' => 'commerce_product_ui_product_type_update_access',
'access arguments' => array(
$type,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['admin/commerce/products/types/' . $type_arg . '/delete'] = array(
'title' => 'Delete',
'page callback' => 'commerce_product_ui_product_type_delete_form_wrapper',
'page arguments' => array(
$type,
),
'access callback' => 'commerce_product_ui_product_type_update_access',
'access arguments' => array(
$type,
),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 10,
'file' => 'includes/commerce_product_ui.types.inc',
);
}
}
return $items;
}
function commerce_product_ui_product_title($product) {
return t('Product: @sku', array(
'@sku' => $product->sku,
));
}
function commerce_product_ui_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))) {
return TRUE;
}
}
return FALSE;
}
function commerce_product_ui_product_type_update_access($type) {
$product_type = commerce_product_type_load($type);
if ($product_type['module'] == 'commerce_product_ui') {
return user_access('administer product types');
}
return FALSE;
}
function commerce_product_ui_menu_alter(&$items) {
foreach (commerce_product_types() as $type => $product_type) {
$type_arg = strtr($type, '_', '-');
$items['admin/commerce/products/types/' . $type_arg . '/fields']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
$items['admin/commerce/products/types/' . $type_arg . '/display']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
}
}
function commerce_product_ui_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if ($root_path == 'admin/commerce/products') {
$item = menu_get_item('admin/commerce/products/add');
if ($item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
}
function commerce_product_ui_admin_menu_map() {
$type_args = array();
foreach (array_keys(commerce_product_types()) as $type) {
$type_args[] = strtr($type, '_', '-');
}
$map['admin/commerce/products/types/%'] = array(
'parent' => 'admin/commerce/products/types',
'arguments' => array(
array(
'%' => $type_args,
),
),
);
return $map;
}
function commerce_product_ui_help($path, $arg) {
switch ($path) {
case 'admin/commerce/products/types/add':
return '<p>' . t('Individual product types can have different fields assigned to them.') . '</p>';
}
if ($arg[1] == 'commerce' && $arg[2] == 'products' && $arg[3] == 'add' && $arg[4]) {
$product_type = commerce_product_type_load($arg[4]);
return !empty($product_type['help']) ? '<p>' . filter_xss_admin($product_type['help']) . '</p>' : '';
}
elseif ($arg[1] == 'commerce' && $arg[2] == 'products' && is_numeric($arg[3])) {
$product = commerce_product_load($arg[3]);
$product_type = commerce_product_type_load($product->type);
return !empty($product_type['help']) ? '<p>' . filter_xss_admin($product_type['help']) . '</p>' : '';
}
}
function commerce_product_ui_theme() {
return array(
'product_add_list' => array(
'variables' => array(
'content' => array(),
),
'file' => 'includes/commerce_product_ui.products.inc',
),
'product_type_admin_overview' => array(
'variables' => array(
'type' => NULL,
),
'file' => 'includes/commerce_product_ui.types.inc',
),
);
}
function commerce_product_ui_entity_info_alter(&$entity_info) {
$entity_info['commerce_product']['uri callback'] = 'commerce_product_ui_product_uri';
$entity_info['commerce_product']['translation']['entity_translation'] += array(
'base path' => 'admin/commerce/products/%commerce_product',
);
foreach ($entity_info['commerce_product']['bundles'] as $type => &$bundle) {
$bundle['admin'] = array(
'path' => 'admin/commerce/products/types/' . strtr($type, '_', '-'),
'access arguments' => array(
'administer product types',
),
);
}
}
function commerce_product_ui_product_uri($product) {
$uri = commerce_product_uri($product);
if (!empty($uri)) {
return $uri;
}
if (commerce_product_access('update', $product)) {
return array(
'path' => 'admin/commerce/products/' . $product->product_id,
);
}
return NULL;
}
function commerce_product_ui_commerce_product_type_info() {
return db_query('SELECT * FROM {commerce_product_type}')
->fetchAllAssoc('type', PDO::FETCH_ASSOC);
}
function commerce_product_ui_product_type_new() {
return array(
'type' => '',
'name' => '',
'description' => '',
'help' => '',
'revision' => 1,
);
}
function commerce_product_ui_product_type_save($product_type, $configure = TRUE, $skip_reset = FALSE) {
$op = drupal_write_record('commerce_product_type', $product_type, empty($product_type['is_new']) ? 'type' : array());
if (!empty($product_type['is_new']) && $op !== FALSE) {
field_attach_create_bundle('commerce_product', $product_type['type']);
if ($configure) {
commerce_product_configure_product_type($product_type['type']);
}
module_invoke_all('commerce_product_type_insert', $product_type, $skip_reset);
}
elseif ($op !== FALSE) {
module_invoke_all('commerce_product_type_update', $product_type, $skip_reset);
}
if (!$skip_reset) {
commerce_product_types_reset();
variable_set('menu_rebuild_needed', TRUE);
}
return $op;
}
function commerce_product_ui_product_type_delete($type, $skip_reset = FALSE) {
$product_type = commerce_product_type_load($type);
db_delete('commerce_product_type')
->condition('type', $type)
->execute();
if (!$skip_reset) {
commerce_product_types_reset();
variable_set('menu_rebuild_needed', TRUE);
}
field_attach_delete_bundle('commerce_product', $type);
module_invoke_all('commerce_product_type_delete', $product_type, $skip_reset);
}
function commerce_product_ui_validate_product_type_unique($type) {
if ($match_id = db_query('SELECT type FROM {commerce_product_type} WHERE type = :type', array(
':type' => $type,
))
->fetchField()) {
return FALSE;
}
return TRUE;
}
function commerce_product_ui_forms($form_id, $args) {
$forms = array();
$forms['commerce_product_ui_product_form'] = array(
'callback' => 'commerce_product_product_form',
);
$forms['commerce_product_ui_product_delete_form'] = array(
'callback' => 'commerce_product_product_delete_form',
);
return $forms;
}
function commerce_product_ui_form_commerce_product_ui_product_form_alter(&$form, &$form_state) {
$product = $form_state['commerce_product'];
$form['actions']['submit']['#submit'][] = 'commerce_product_ui_product_form_submit';
if (empty($product->product_id)) {
$form['actions']['save_continue'] = array(
'#type' => 'submit',
'#value' => t('Save and add another'),
'#submit' => $form['actions']['submit']['#submit'],
'#suffix' => l(t('Cancel'), 'admin/commerce/products'),
'#weight' => 45,
);
}
else {
$form['actions']['submit']['#suffix'] = l(t('Cancel'), 'admin/commerce/products');
}
}
function commerce_product_ui_product_form_submit($form, &$form_state) {
$array_parents = $form_state['triggering_element']['#array_parents'];
$submit_element = array_pop($array_parents);
if ($submit_element == 'save_continue') {
$form_state['redirect'] = 'admin/commerce/products/add/' . strtr($form_state['commerce_product']->type, array(
'_' => '-',
));
}
elseif (arg(2) == 'products' && arg(3) == 'add') {
$form_state['redirect'] = 'admin/commerce/products';
}
}
function commerce_product_ui_form_commerce_product_ui_product_delete_form_alter(&$form, &$form_state) {
$form['actions']['cancel']['#href'] = 'admin/commerce/products';
$form['#submit'][] = 'commerce_product_ui_product_delete_form_submit';
}
function commerce_product_ui_product_delete_form_submit($form, &$form_state) {
$form_state['redirect'] = 'admin/commerce/products';
}
function commerce_product_ui_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'commerce_product_ui') . '/includes/views',
);
}
function commerce_product_ui_set_breadcrumb($product_types = FALSE) {
}