From 070e887d7a3886e4aed58eef0255717872d0bc1f Mon Sep 17 00:00:00 2001
From: jucallme <jucallme@gmail.com>
Date: Thu, 15 Mar 2012 17:16:23 -0300
Subject: [PATCH 1/2] initial pass at adding lineitems to the mix here

---
 includes/entity_types/commerce_line_item.inc |  141 ++++++++++++++++++++++++++
 inline_entity_form.module                    |   12 ++-
 2 files changed, 152 insertions(+), 1 deletions(-)
 create mode 100644 includes/entity_types/commerce_line_item.inc

diff --git a/includes/entity_types/commerce_line_item.inc b/includes/entity_types/commerce_line_item.inc
new file mode 100644
index 0000000..b95b1e3
--- /dev/null
+++ b/includes/entity_types/commerce_line_item.inc
@@ -0,0 +1,141 @@
+<?php
+
+/**
+ * @file
+ * CTools plugin. Provides functionality for inline managing commerce line items.
+ */
+
+$plugin = array(
+  'table' => 'inline_entity_form_commerce_line_item_build_table',
+  'form' => 'inline_entity_form_commerce_line_item_form',
+  'form validate' => 'inline_entity_form_commerce_line_item_form_validate',
+  'form submit' => 'inline_entity_form_commerce_line_item_form_submit',
+  'delete form' => 'inline_entity_form_commerce_line_item_delete_form',
+  'add label' => t('Add line item variation'),
+  'save label' => t('Save variation'),
+);
+
+/**
+ * IEF table callback: Returns the table used to identify existing line items.
+ * Additional elements (weight, actions) are appended to this table later on.
+ */
+function inline_entity_form_commerce_line_item_build_table($line_items) {
+  $table['#header'] = array(
+    array('data' => t('Line item label')),
+    array('data' => t('ID')),
+  );
+
+  $table['#rows'] = array();
+  foreach ($line_items as $line_item) {
+    $table['#rows'][] = array(
+      array('data' => check_plain($line_item->line_item_label), 'class' => array('inline-entity-form-line-item-label')),
+      array('data' => check_plain($line_item->line_item_id), 'class' => array('inline-entity-form-line-item-id')),
+    );
+  }
+
+  return $table;
+}
+
+/**
+ * IEF add/edit form callback: Returns the line item form to be embedded.
+ *
+ * When adding data to $form_state it should be noted that there can be several
+ * IEF widgets on one master form, each with several form rows, leading to
+ * possible key collisions if the keys are not prefixed with $parents.
+ */
+function inline_entity_form_commerce_line_item_form($form, &$form_state, $parents, $line_item) {
+  // Field API relies on #parents when invoked on subforms, using it to find
+  // the field values in $form_state.
+  $form['#parents'] = $parents;
+
+  $language = !empty($line_item->language) ? $line_item->language : LANGUAGE_NONE;
+  $form['line_item_label'] = array(
+     '#type' => 'textfield',
+     '#title' => t('Line item label'),
+     '#description' => t('Supply the line item label to be used for this line item.'),
+     '#default_value' => $line_item->line_item_label,
+     '#maxlength' => 128,
+     '#required' => TRUE,
+     '#fieldset' => 'line_item_details',
+   );
+  field_attach_form('commerce_line_item', $line_item, $form, $form_state, $language);
+
+  // Arrange attributes.
+  $attributes = _inline_entity_form_commerce_line_item_attributes($line_item->type);
+  if (empty($attributes)) {
+    // Hide the fieldset, it will be empty.
+    $form['line_item_attributes']['#access'] = FALSE;
+  }
+  else {
+    foreach ($attributes as $field_name) {
+      $form[$field_name]['#fieldset'] = 'line_item_attributes';
+    }
+  }
+
+  return $form;
+}
+
+/**
+ * IEF add/edit form validation callback.
+ */
+function inline_entity_form_commerce_line_item_form_validate($form, &$form_state, $parents, $line_item) {
+  $parents_path = implode('][', $parents);
+  $line_item_values = drupal_array_get_nested_value($form_state['values'], $parents);
+  $line_item_label = trim($line_item_values['line_item_label']);
+
+  // Trim leading and trailing whitespace from the line item label.
+  drupal_array_set_nested_value($form_state['values'], array_merge($parents, array('line_item_label')), $line_item_label);
+
+  field_attach_form_validate('commerce_line_item', $line_item, $form, $form_state);
+}
+
+/**
+ * IEF add/edit form submit callback: Modifies the passed-in line item before it
+ * is saved.
+ */
+function inline_entity_form_commerce_line_item_form_submit($form, &$form_state, $parents, $line_item) {
+  $line_item_values = drupal_array_get_nested_value($form_state['values'], $parents);
+  $line_item->line_item_label = $line_item_values['line_item_label'];
+  $line_item->line_item_id = $line_item_values['line_item_id'];
+  field_attach_submit('commerce_line_item', $line_item, $form, $form_state);
+}
+
+/**
+ * IEF delete form callback: Returns the confirmation message.
+ */
+function inline_entity_form_commerce_line_item_delete_form($form, $form_state, $parents, $line_item) {
+  $form['message'] = array(
+    '#markup' => '<div>' . t('Are you sure you want to delete %title?', array('%title' => $line_item->title)) . '</div>',
+  );
+
+  return $form;
+}
+
+/**
+ * Returns a list of field names that are used as attributes for the given
+ * line item type.
+ *
+ * Copied from commerce_cart.module
+ */
+function _inline_entity_form_commerce_line_item_attributes($type) {
+  $attributes = array();
+  // Loop through all the field instances on that line item type.
+  foreach (field_info_instances('commerce_line_item', $type) as $name => $instance) {
+    // A field qualifies if it is single value, required and uses a widget
+    // with a definite set of options. For the sake of simplicity, this is
+    // currently restricted to fields defined by the options module.
+    $field = field_info_field($instance['field_name']);
+
+    // Get the array of Cart settings pertaining to this instance.
+    $commerce_cart_settings = commerce_cart_field_instance_attribute_settings($instance);
+
+    // If the instance is of a field type that is eligible to function as
+    // a line item attribute field and if its attribute field settings
+    // specify that this functionality is enabled...
+    if (commerce_cart_field_attribute_eligible($field) && $commerce_cart_settings['attribute_field']) {
+      $attributes[] = $field['field_name'];
+    }
+  }
+
+  return $attributes;
+}
diff --git a/inline_entity_form.module b/inline_entity_form.module
index 48e8f9f..08ddad3 100644
--- a/inline_entity_form.module
+++ b/inline_entity_form.module
@@ -59,7 +59,7 @@ function inline_entity_form_field_widget_info() {
 
   $widgets['inline_entity_form'] = array(
     'label' => t('Inline entity form'),
-    'field types' => array('commerce_product_reference'),
+    'field types' => array('commerce_product_reference', 'commerce_line_item_reference'),
     'settings' => array(),
     'behaviors' => array(
       'multiple values' => FIELD_BEHAVIOR_CUSTOM,
@@ -97,6 +97,16 @@ function inline_entity_form_settings($field, $instance) {
     }
   }
 
+
+  if ($field['type'] == 'commerce_line_item_reference') {
+    //#TODO figgure out what needs to be se for the bundles key here....
+    // right now its a hack.
+    // dsm($instance);
+    $settings['entity_type'] = 'commerce_line_item';
+    $settings['column'] = 'line_item_id';
+    $settings['bundles'] = array();
+  }
+
   // By default, allow entities of all bundles to be created.
   if (empty($settings['bundles'])) {
     $info = entity_get_info($settings['entity_type']);
-- 
1.7.5.4


From 034742b6bbe799ee789a6eb9dd5f761433b280e7 Mon Sep 17 00:00:00 2001
From: jucallme <jucallme@gmail.com>
Date: Fri, 16 Mar 2012 18:52:27 -0300
Subject: [PATCH 2/2] more clean up

---
 includes/entity_types/commerce_line_item.inc |   15 ++++++++++-----
 inline_entity_form.module                    |    5 ++++-
 theme/inline_entity_form.css                 |    2 +-
 3 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/includes/entity_types/commerce_line_item.inc b/includes/entity_types/commerce_line_item.inc
index b95b1e3..b394cf9 100644
--- a/includes/entity_types/commerce_line_item.inc
+++ b/includes/entity_types/commerce_line_item.inc
@@ -21,7 +21,7 @@ $plugin = array(
  */
 function inline_entity_form_commerce_line_item_build_table($line_items) {
   $table['#header'] = array(
-    array('data' => t('Line item label')),
+    array('data' => t('Line item label'), 'class' => array('ief-line-item-header')),
     array('data' => t('ID')),
   );
 
@@ -48,6 +48,12 @@ function inline_entity_form_commerce_line_item_form($form, &$form_state, $parent
   // the field values in $form_state.
   $form['#parents'] = $parents;
 
+  $form['#pre_render'][] = 'inline_entity_form_pre_render_add_fieldset_markup';
+  $form['line_item_details'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Line item details'),
+    '#attributes' => array('class' => array('ief-line_item-details')),
+  );
   $language = !empty($line_item->language) ? $line_item->language : LANGUAGE_NONE;
   $form['line_item_label'] = array(
      '#type' => 'textfield',
@@ -64,11 +70,11 @@ function inline_entity_form_commerce_line_item_form($form, &$form_state, $parent
   $attributes = _inline_entity_form_commerce_line_item_attributes($line_item->type);
   if (empty($attributes)) {
     // Hide the fieldset, it will be empty.
-    $form['line_item_attributes']['#access'] = FALSE;
+    $form['line_item_details']['#access'] = FALSE;
   }
   else {
     foreach ($attributes as $field_name) {
-      $form[$field_name]['#fieldset'] = 'line_item_attributes';
+      $form[$field_name]['#fieldset'] = 'line_item_details';
     }
   }
 
@@ -96,7 +102,6 @@ function inline_entity_form_commerce_line_item_form_validate($form, &$form_state
 function inline_entity_form_commerce_line_item_form_submit($form, &$form_state, $parents, $line_item) {
   $line_item_values = drupal_array_get_nested_value($form_state['values'], $parents);
   $line_item->line_item_label = $line_item_values['line_item_label'];
-  $line_item->line_item_id = $line_item_values['line_item_id'];
   field_attach_submit('commerce_line_item', $line_item, $form, $form_state);
 }
 
@@ -125,7 +130,7 @@ function _inline_entity_form_commerce_line_item_attributes($type) {
     // with a definite set of options. For the sake of simplicity, this is
     // currently restricted to fields defined by the options module.
     $field = field_info_field($instance['field_name']);
-
+    $attributes[] = $field['field_name'];
     // Get the array of Cart settings pertaining to this instance.
     $commerce_cart_settings = commerce_cart_field_instance_attribute_settings($instance);
 
diff --git a/inline_entity_form.module b/inline_entity_form.module
index 08ddad3..3ffc192 100644
--- a/inline_entity_form.module
+++ b/inline_entity_form.module
@@ -102,9 +102,12 @@ function inline_entity_form_settings($field, $instance) {
     //#TODO figgure out what needs to be se for the bundles key here....
     // right now its a hack.
     // dsm($instance);
+    $bundles = array();
+    $bundles[] = $instance['bundle'];
+
     $settings['entity_type'] = 'commerce_line_item';
     $settings['column'] = 'line_item_id';
-    $settings['bundles'] = array();
+    $settings['bundles'] = $bundles;
   }
 
   // By default, allow entities of all bundles to be created.
diff --git a/theme/inline_entity_form.css b/theme/inline_entity_form.css
index 07ff3b0..93d3526 100644
--- a/theme/inline_entity_form.css
+++ b/theme/inline_entity_form.css
@@ -7,7 +7,7 @@
   border-left: none;
 }
 
-.ief-product-header {
+.ief-product-header, .ief-line-item-header {
   border-left: none;
 }
 
-- 
1.7.5.4

