Projet

Général

Profil

Paste
Télécharger (8,47 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / product / includes / commerce_product.forms.inc @ 9d13637e

1
<?php
2

    
3
/**
4
 * @file
5
 * Forms for creating, editing, and deleting products.
6
 */
7

    
8

    
9
/**
10
 * Form callback: create or edit a product.
11
 *
12
 * @param $product
13
 *   The product object to edit or for a create form an empty product object
14
 *     with only a product type defined.
15
 */
16
function commerce_product_product_form($form, &$form_state, $product) {
17
  // Ensure this include file is loaded when the form is rebuilt from the cache.
18
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'commerce_product') . '/includes/commerce_product.forms.inc';
19

    
20
  // Add the default field elements.
21
  // TODO: Update description to include the acceptable product tokens.
22
  $form['sku'] = array(
23
    '#type' => 'textfield',
24
    '#title' => t('Product SKU'),
25
    '#description' => t('Supply a unique identifier for this product using letters, numbers, hyphens, and underscores. Commas may not be used.'),
26
    '#default_value' => $product->sku,
27
    '#maxlength' => 128,
28
    '#required' => TRUE,
29
    '#weight' => -10,
30
  );
31

    
32
  $form['title'] = array(
33
    '#type' => 'textfield',
34
    '#title' => t('Title'),
35
    '#default_value' => $product->title,
36
    '#maxlength' => 255,
37
    '#required' => TRUE,
38
    '#weight' => -5,
39
  );
40

    
41
  // Add the field related form elements.
42
  $form_state['commerce_product'] = $product;
43

    
44
  $langcode = entity_language('commerce_product', $product);
45

    
46
  if (empty($langcode)) {
47
    $langcode = LANGUAGE_NONE;
48
  }
49

    
50
  field_attach_form('commerce_product', $product, $form, $form_state, $langcode);
51

    
52
  $form['status'] = array(
53
    '#type' => 'radios',
54
    '#title' => t('Status'),
55
    '#description' => t('Disabled products cannot be added to shopping carts and may be hidden in administrative product lists.'),
56
    '#options' => array(
57
      '1' => t('Active'),
58
      '0' => t('Disabled'),
59
    ),
60
    '#default_value' => $product->status,
61
    '#required' => TRUE,
62
    '#weight' => 35,
63
  );
64

    
65
  // Load the product type to get the default revision setting.
66
  $product_type = commerce_product_type_load($product->type);
67

    
68
  // When updating a product, do not collapse the Change History fieldset if the
69
  // product type is configured to create a new revision by default.
70
  $form['change_history'] = array(
71
    '#type' => 'fieldset',
72
    '#title' => t('Change history'),
73
    '#collapsible' => TRUE,
74
    '#collapsed' => empty($product->product_id) || empty($product_type['revision']),
75
    '#weight' => 350,
76
  );
77
  if (!empty($product->product_id)) {
78
    $form['change_history']['revision'] = array(
79
      '#type' => 'checkbox',
80
      '#title' => t('Create new revision on update'),
81
      '#description' => t('If an update log message is entered, a revision will be created even if this is unchecked.'),
82
      '#default_value' => $product_type['revision'],
83
      '#access' => user_access('administer commerce_product entities'),
84
    );
85
  }
86
  $form['change_history']['log'] = array(
87
    '#type' => 'textarea',
88
    '#title' => !empty($product->product_id) ? t('Update log message') : t('Creation log message'),
89
    '#rows' => 4,
90
    '#description' => t('Provide an explanation of the changes you are making. This will provide a meaningful history of changes to this product.'),
91
  );
92

    
93
  $form['actions'] = array(
94
    '#type' => 'actions',
95
    '#weight' => 400,
96
  );
97

    
98
  // Simply use default language
99
  $form['language'] = array(
100
    '#type' => 'value',
101
    '#value' => $langcode,
102
  );
103

    
104
  // We add the form's #submit array to this button along with the actual submit
105
  // handler to preserve any submit handlers added by a form callback_wrapper.
106
  $submit = array();
107

    
108
  if (!empty($form['#submit'])) {
109
    $submit += $form['#submit'];
110
  }
111

    
112
  $form['actions']['submit'] = array(
113
    '#type' => 'submit',
114
    '#value' => t('Save product'),
115
    '#submit' => array_merge($submit, array('commerce_product_product_form_submit')),
116
  );
117

    
118
  // We append the validate handler to #validate in case a form callback_wrapper
119
  // is used to add validate handlers earlier.
120
  $form['#validate'][] = 'commerce_product_product_form_validate';
121

    
122
  return $form;
123
}
124

    
125
/**
126
 * Validation callback for commerce_product_product_form().
127
 */
128
function commerce_product_product_form_validate($form, &$form_state) {
129
  $product = $form_state['commerce_product'];
130

    
131
  // TODO: Resolve workflow issues pertaining to token replacement in SKUs.
132
  // Perform token replacement on the entered SKU.
133
  // $sku = commerce_product_replace_sku_tokens($form_state['values']['sku'], $product);
134

    
135
  // Until the above is resolved, simply use the SKU as entered with no tokens.
136
  $sku = $form_state['values']['sku'];
137

    
138
  // If invalid tokens were specified, throw an error.
139
  if ($sku === FALSE) {
140
    form_set_error('sku', t('The SKU contains invalid tokens.'));
141
  }
142
  else {
143
    // Ensure the proposed SKU is unique or reused only during product updates.
144
    $query = new EntityFieldQuery();
145

    
146
    $query
147
      ->entityCondition('entity_type', 'commerce_product')
148
      ->propertyCondition('sku', $sku);
149

    
150
    $result = $query->execute();
151

    
152
    if (!empty($result)) {
153
      $product_id = key($result['commerce_product']);
154

    
155
      if ($product_id != $product->product_id) {
156
        form_set_error('sku', t('This SKU is <a href="!url">already in use</a> and must be unique. Please supply another value.', array('!url' => url('admin/commerce/products/' . $product_id))));
157
      }
158
    }
159

    
160
    // Validate the SKU for invalid characters.
161
    if (!commerce_product_validate_sku($sku)) {
162
      form_set_error('sku', t('The SKU %sku contains invalid characters.', array('%sku' => $sku)));
163
    }
164

    
165
    // Trim leading and trailing whitespace from the SKU.
166
    form_set_value($form['sku'], trim($sku), $form_state);
167
  }
168

    
169
  // Notify field widgets to validate their data.
170
  field_attach_form_validate('commerce_product', $product, $form, $form_state);
171
}
172

    
173
/**
174
 * Submit callback for commerce_product_product_form().
175
 */
176
function commerce_product_product_form_submit($form, &$form_state) {
177
  global $user;
178

    
179
  $product = &$form_state['commerce_product'];
180

    
181
  // Save default parameters back into the $product object.
182
  $product->sku = $form_state['values']['sku'];
183
  $product->title = $form_state['values']['title'];
184
  $product->status = $form_state['values']['status'];
185
  $product->language = $form_state['values']['language'];
186

    
187
  // Set the product's uid if it's being created at this time.
188
  if (empty($product->product_id)) {
189
    $product->uid = $user->uid;
190
  }
191

    
192
  // Trigger a new revision if the checkbox was enabled or a log message supplied.
193
  if ((user_access('administer commerce_product entities') && !empty($form_state['values']['revision'])) ||
194
    (!user_access('administer commerce_product entities') && !empty($form['change_history']['revision']['#default_value'])) ||
195
    !empty($form_state['values']['log'])) {
196
    $product->revision = TRUE;
197
    $product->log = $form_state['values']['log'];
198
  }
199

    
200
  // Notify field widgets.
201
  field_attach_submit('commerce_product', $product, $form, $form_state);
202

    
203
  // Save the product.
204
  commerce_product_save($product);
205

    
206
  // Redirect based on the button clicked.
207
  drupal_set_message(t('Product saved.'));
208
}
209

    
210
/**
211
 * Form callback: confirmation form for deleting a product.
212
 *
213
 * @param $product
214
 *   The product object to be deleted.
215
 *
216
 * @see confirm_form()
217
 */
218
function commerce_product_product_delete_form($form, &$form_state, $product) {
219
  $form_state['product'] = $product;
220

    
221
  // Ensure this include file is loaded when the form is rebuilt from the cache.
222
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'commerce_product') . '/includes/commerce_product.forms.inc';
223

    
224
  $form['#submit'][] = 'commerce_product_product_delete_form_submit';
225

    
226
  $content = entity_view('commerce_product', array($product->product_id => $product));
227

    
228
  $form = confirm_form($form,
229
    t('Are you sure you want to delete %title?', array('%title' => $product->title)),
230
    '',
231
    drupal_render($content) . '<p>' . t('Deleting this product cannot be undone.', array('@sku' => $product->sku)) . '</p>',
232
    t('Delete'),
233
    t('Cancel'),
234
    'confirm'
235
  );
236

    
237
  return $form;
238
}
239

    
240
/**
241
 * Submit callback for commerce_product_product_delete_form().
242
 */
243
function commerce_product_product_delete_form_submit($form, &$form_state) {
244
  $product = $form_state['product'];
245

    
246
  if (commerce_product_delete($product->product_id)) {
247
    drupal_set_message(t('%title has been deleted.', array('%title' => $product->title)));
248
    watchdog('commerce_product', 'Deleted product %title (SKU: @sku).', array('%title' => $product->title, '@sku' => $product->sku), WATCHDOG_NOTICE);
249
  }
250
  else {
251
    drupal_set_message(t('%title could not be deleted.', array('%title' => $product->title)), 'error');
252
  }
253
}