View source
<?php
class commerce_reorder_handler_field_commerce_reorder_button extends views_handler_field {
function option_definition() {
$options = parent::option_definition();
$options['reorder_button_label'] = array(
'default' => 'Reorder',
'translatable' => TRUE,
);
$options['redirect'] = array(
'default' => TRUE,
);
$options['redirect_url'] = array(
'default' => 'cart',
);
$profile_types = array_keys(commerce_customer_profile_types());
foreach ($profile_types as $type) {
$options['copy_profiles'][$type] = FALSE;
}
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['reorder_button_label'] = array(
'#type' => 'textfield',
'#title' => t('Reorder button label'),
'#default_value' => $this->options['reorder_button_label'],
);
$form['redirect'] = array(
'#type' => 'checkbox',
'#title' => t('Redirect after reorder'),
'#default_value' => $this->options['redirect'],
);
$form['redirect_url'] = array(
'#type' => 'textfield',
'#title' => t('Target URL to redirect after reorder'),
'#default_value' => $this->options['redirect_url'],
'#description' => t('Tokens are supported. For instance you may use <strong></strong><code>checkout/[site:current-cart-order:order-id]/review</code></strong> to go directly to the review step.'),
'#dependency' => array(
'edit-options-redirect' => array(
TRUE,
),
),
);
$types = commerce_customer_profile_types();
$copy_profile_options = array();
foreach ($types as $type => $type_data) {
$copy_profile_options[$type] = t($type_data['name']);
}
$form['copy_profiles'] = array(
'#type' => 'checkboxes',
'#title' => t('Copy customer profiles to the new order'),
'#description' => t('The selected profiles, if present on the existing order, will be copied to the new order.'),
'#options' => $copy_profile_options,
'#default_value' => $this->options['copy_profiles'],
);
}
function construct() {
parent::construct();
$this->additional_fields['order_id'] = 'order_id';
$this->real_field = 'order_id';
}
function render($values) {
return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
}
function views_form(&$form, &$form_state) {
if (empty($this->view->result)) {
return;
}
$form[$this->options['id']] = array(
'#tree' => TRUE,
);
foreach ($this->view->result as $row_id => $row) {
$order_id = $this
->get_value($row, 'order_id');
$form[$this->options['id']][$row_id] = array(
'#type' => 'submit',
'#value' => isset($this->options['reorder_button_label']) ? $this->options['reorder_button_label'] : t('Reorder'),
'#name' => 'reorder-line-item-' . $row_id,
'#order_id' => $order_id,
);
}
}
function views_form_submit($form, &$form_state) {
$field_name = $this->options['id'];
foreach (element_children($form[$field_name]) as $row_id) {
if ($form_state['triggering_element']['#name'] == 'reorder-line-item-' . $row_id) {
$order = commerce_order_load($form[$field_name][$row_id]['#order_id']);
commerce_reorder_helper($order, null, array(
'copy_profiles' => $this->options['copy_profiles'],
));
}
}
if (strpos($form_state['triggering_element']['#name'], 'reorder-line-item-') === 0) {
drupal_set_message(t('Order copied to your cart.'));
if ($this->options['redirect']) {
$url = isset($this->options['redirect_url']) ? $this->options['redirect_url'] : 'cart';
$url = token_replace($url, array(
'commerce-order' => $order,
));
drupal_goto(check_url($url));
}
}
}
}