Projet

Général

Profil

Paste
Télécharger (4,14 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / line_item / includes / views / handlers / commerce_line_item_handler_field_edit_quantity.inc @ b858700c

1
<?php
2

    
3
/**
4
 * @file
5
 * Field handler to present a form field to change quantity of a line item. It's
6
 * a dummy handler, most part of the implementation is done via post render
7
 * hook.
8
 */
9

    
10
/**
11
 * Field handler to present a field to change quantity of a line item.
12
 */
13
class commerce_line_item_handler_field_edit_quantity extends views_handler_field {
14

    
15
  function construct() {
16
    parent::construct();
17
    $this->additional_fields['line_item_id'] = 'line_item_id';
18
    $this->additional_fields['quantity'] = 'quantity';
19

    
20
    // Set real_field in order to make it generate a field_alias.
21
    $this->real_field = 'quantity';
22
  }
23

    
24
  function render($values) {
25
    return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
26
  }
27

    
28
  /**
29
   * Returns the form which replaces the placeholder from render().
30
   */
31
  function views_form(&$form, &$form_state) {
32
    // The view is empty, abort.
33
    if (empty($this->view->result)) {
34
      return;
35
    }
36

    
37
    $form[$this->options['id']] = array(
38
      '#tree' => TRUE,
39
    );
40
    // At this point, the query has already been run, so we can access the results
41
    // in order to get the base key value (for example, nid for nodes).
42
    foreach ($this->view->result as $row_id => $row) {
43
      $line_item_id = $this->get_value($row, 'line_item_id');
44
      $quantity = $this->get_value($row, 'quantity');
45

    
46
      $form[$this->options['id']][$row_id] = array(
47
        '#type' => 'textfield',
48
        '#datatype' => 'integer',
49
        '#default_value' => round($quantity),
50
        '#size' => 4,
51
        '#maxlength' => max(4, strlen($quantity)),
52
        '#line_item_id' => $line_item_id,
53
        '#attributes' => array(
54
          'title' => $this->options['label'],
55
        ),
56
      );
57
    }
58
  }
59

    
60
  function views_form_validate($form, &$form_state) {
61
    $field_name = $this->options['id'];
62
    foreach (element_children($form[$field_name]) as $row_id) {
63
      // Ensure the quantity is actually a numeric value.
64
      if (!is_numeric($form_state['values'][$field_name][$row_id]) || $form_state['values'][$field_name][$row_id] < 0) {
65
        form_set_error($field_name . '][' . $row_id, t('You must specify a positive number for the quantity'));
66
      }
67

    
68
      // If the custom data type attribute of the quantity element is integer,
69
      // ensure we only accept whole number values.
70
      if ($form[$field_name][$row_id]['#datatype'] == 'integer' &&
71
        (int) $form_state['values'][$field_name][$row_id] != $form_state['values'][$field_name][$row_id]) {
72
        form_set_error($field_name . '][' . $row_id, t('You must specify a whole number for the quantity.'));
73
      }
74
    }
75
  }
76

    
77
  function views_form_submit($form, &$form_state) {
78
    $field_name = $this->options['id'];
79
    $deleted_line_items = array();
80
    $updated_line_items = array();
81

    
82
    foreach (element_children($form[$field_name]) as $row_id) {
83
      $line_item_id = $form[$field_name][$row_id]['#line_item_id'];
84

    
85
      // If the line item hasn't been deleted...
86
      if ($line_item = commerce_line_item_load($line_item_id)) {
87
        $form_quantity = $form_state['values'][$field_name][$row_id];
88

    
89
        // If the quantity on the form is different...
90
        if ($form_quantity != $line_item->quantity) {
91
          // If the quantity specified is 0, flag the line item for deletion.
92
          if ($form_quantity == 0) {
93
            $deleted_line_items[] = $line_item_id;
94
          }
95
          else {
96
            // Otherwise queue the line item quantity update.
97
            $updated_line_items[$line_item_id] = $form_quantity;
98
          }
99
        }
100
      }
101
    }
102

    
103
    // Process the deletes first.
104
    foreach ($deleted_line_items as $line_item_id) {
105
      $order = commerce_order_load($form_state['order']->order_id);
106
      commerce_cart_order_product_line_item_delete($order, $line_item_id);
107
    }
108

    
109
    // Then process the quantity updates.
110
    foreach ($updated_line_items as $line_item_id => $quantity) {
111
      // Load the line item and update it.
112
      $line_item = commerce_line_item_load($line_item_id);
113
      $line_item->quantity = $quantity;
114
      commerce_line_item_save($line_item);
115
      entity_get_controller('commerce_line_item')->resetCache(array($line_item->line_item_id));
116
    }
117
  }
118
}