/[drupal]/contributions/modules/ec_live_subproducts/ec_live_subproducts.module
ViewVC logotype

Diff of /contributions/modules/ec_live_subproducts/ec_live_subproducts.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1 by brmassa, Wed Nov 15 00:46:54 2006 UTC revision 1.2 by brmassa, Wed Aug 1 14:42:19 2007 UTC
# Line 0  Line 1 
1    <?php
2    // $Id: ec_live_subproducts.module,v 1.1.1.2.2.5.2.14 2007/06/14 02:56:19 brmassa Exp $
3    /*******************************************************************************
4                         D R U P A L    M O D U L E
5    ********************************************************************************
6    Module Name        : E-Commerce Live Subproducts
7    Original Author    : Bruno Massa http://drupal.org/user/67164
8    Project Page       : http://drupal.org/project/ec_live_subproducts
9    Support Queue      : http://drupal.org/project/issues/ec_live_subproducts
10    
11    *******************************************************************************/
12    /**
13     * @file
14     * Automaticly create subproducts based on subproducts
15     * @todo
16     * - doxygen documentation
17     * - attribute of attribute: make possible to create an attribute that has attributes
18     * - dynamic price: change each attribute's surcharge when another attribute is selected
19     * - Site recommends: some sort of "DRUPAL_SITE recommends it attribute"
20     * - non required attribute: some generic "none" option on a variation
21     * - update process
22     */
23    
24    /**
25     * Implementation of hook_access(): only allow authorized user to create
26     * modify and delete this node type
27     * @param op the operation to be made: create, update, delete or something else
28     * @param node object
29     */
30    function ec_live_subproducts_attribute_access($op, &$node) {
31      if ($op == "create" or $op == "update" or $op == "delete") {
32        return user_access("manage attributes and variations");
33      }
34    } // ec_live_subproducts_attribute_access
35    
36    /**
37     * View all variations and attributes
38     */
39    function ec_live_subproducts_attribute_admin() {
40      $form = array();
41      $current_variation = NULL;
42      $attributes = ec_live_subproducts_attribute_get(NULL, NULL, NULL, 50);
43      if (!empty($attributes)) {
44        foreach ($attributes as $attribute) {
45          $form_temp = array();
46          if ($current_variation != $attribute["tid"]) {
47            $current_variation = $attribute["tid"];
48            $form_temp["variation"] = array(
49              "#type" => "item",
50              "#value" => l($attribute["name"], "admin/content/taxonomy/edit/term/". $attribute["tid"], NULL,
51                "destination=admin/store/attributes") ." - ". l(t("Create attribute"),
52                "node/add/ec_live_subproducts-attribute", NULL, "destination=admin/store/attributes&variation=". $attribute["tid"]));
53            $form[] = $form_temp;
54            $form_temp = array();
55          }
56          if (!empty($attribute["nid"])) {
57            $form_temp["attribute"] = array(
58              "#type" => "item",
59              "#value" => "-- ". l($attribute["title"], "node/". $attribute["nid"] ."/edit", NULL,
60                "destination=admin/store/attributes&variation=". $attribute["tid"]));
61            $form_temp["pricevariation"] = array(
62              "#type" => "item",
63              "#value" => payment_format($attribute["pricevariation"]));
64            $form_temp["stock"] = array(
65              "#type" => "item",
66              "#value" => $attribute["stock"]);
67            $form_temp["sku"] = array(
68              "#type" => "item",
69              "#value" => $attribute["sku"]);
70            $form[] = $form_temp;
71          }
72        }
73      }
74      return $form;
75    } // ec_live_subproducts_attribute_admin
76    
77    /**
78     * Implementation of hook_delete().
79     *
80     * Delete attribute-specific information from the table
81     *
82     * @param &$node
83     *   Object, the node object
84     */
85    function ec_live_subproducts_attribute_delete(&$node) {
86      // delete all subproducts that has this attribute
87      $pparent_sql = db_query("SELECT DISTINCT pnid FROM {ec_ls_pattribute} WHERE nid = %d", $node->nid);
88      while ($pparent = db_fetch_object($pparent_sql)) {
89        node_delete($pparent->pnid);
90      }
91      // delete all remaining references
92      db_query("DELETE FROM {ec_ls_attribute} WHERE nid = %d", $node->nid);
93      db_query("DELETE FROM {ec_ls_bpattribute} WHERE nid = %d", $node->nid);
94    } // ec_live_subproducts_attribute_delete
95    
96    /**
97     * Implementation of hook_form(): create the node type form
98     * @param node object
99     */
100    function ec_live_subproducts_attribute_form(&$node) {
101      $form["title"] = array(
102        "#type"           => "textfield",
103        "#title"          => t("Attribute"),
104        "#description"    => t("The attribute name"),
105        "#default_value"  => isset($node->title) ? $node->title : "",
106        "#weight"         => -5,
107        "#required"       => TRUE);
108      $form["attribute"] = array(
109        "#type"           => "fieldset",
110        "#title"          => t("Attribute"),
111        "#weight"         => -4);
112      $form["attribute"]["pricevariation"] = array(
113        "#type"           => "textfield",
114        "#title"          => t("Price Variation"),
115        "#description"    => t("The inclusion of this attribute will incur a price variation on final subproduct? (can be a discount)"),
116        "#default_value"  => isset($node->pricevariation) ? $node->pricevariation : "0.00",
117        "#required"       => TRUE);
118      $form["attribute"]["stock"] = array(
119        "#type"           => "textfield",
120        "#title"          => t("Stock"),
121        "#description"    => t("The amount of this attribute on stock. -1 for unlimited."),
122        "#default_value"  => isset($node->stock) ? $node->stock : -1,
123        "#required"       => TRUE);
124      $form["attribute"]["sku"] = array(
125        "#type"           => "textfield",
126        "#title"          => t("SKU"),
127        "#description"    => t("The attribute SKU"),
128        "#default_value"  => isset($node->sku) ? $node->sku : "");
129      $form["attribute"]["body"] = array(
130        "#type"           => "textarea",
131        "#title"          => t("Description"),
132        "#default_value"  => isset($node->body) ? $node->body : "",
133        "#rows"           => 5,
134        "#description"    => t("The attribute description"));
135      $form["attribute"]["format"] = isset($node->format) ? filter_form($node->format) : filter_form();
136      $form["attribute"]["attr_weight"] = array(
137        "#type"           => "weight",
138        "#title"          => t("Weight"),
139        "#default_value"  => isset($node->attr_weight) ? $node->attr_weight : 0);
140      return $form;
141    } // ec_live_subproducts_attribute_form
142    
143    /**
144     * Get all attributes
145     *
146     * @param $id
147     *   number, parent variation ID
148     * @param $type
149     *   string, the search type
150     *   baseproduct = get all attributes from a given parent subproduct
151     *   product     = get all attributes from a given subproduct
152     *   attribute   = get a single attribute
153     * @return
154     *   object, all attributes and theirs fields or NULL
155     */
156    function ec_live_subproducts_attribute_get($id = NULL, $type = NULL, $only_variation = NULL, $pager = NULL) {
157      static $variations = array();
158      if (empty($variations[$id][$type][$only_variation][$pager])) {
159        $sql["sqlselect"][]   = " td.*, sv.* ";
160        $sql["args"]["vid"]   = variable_get("ec_live_subproducts_variation_vid", 0);
161        $sql["sqlwhere"][]    = " td.vid = %d ";
162        $sql["sqljoin"][]     = " INNER JOIN {ec_ls_variation} sv ON sv.tid = td.tid ";
163        if     (empty($only_variation)) {
164          $sql["sqlselect"][] = ", n.title, n.body AS attribute_description, n.format, a.* ";
165          $sql["sqljoin"][]   = " LEFT JOIN {term_node} tn ON tn.tid = td.tid
166            LEFT JOIN {node_revisions} n ON n.nid = tn.nid
167            LEFT JOIN {ec_ls_attribute} a ON a.nid = tn.nid ";
168          $tablesort[] = ",attr_weight , n.title";
169        }
170        if     (!empty($id) and $type == "baseproduct") {
171          $sql["sqljoin"][]   = " INNER JOIN {ec_ls_bpattribute} ba ON ba.nid = a.nid ";
172          $sql["sqlwhere"][]  = " AND ba.pnid  = %d";
173          $sql["args"][]      = $id;
174        }
175        elseif (!empty($id) and $type == "product") {
176          $sql["sqljoin"][]   = " INNER JOIN {ec_ls_pattribute} pa ON pa.nid = a.nid" ;
177          $sql["sqlwhere"][]  = " AND pa.pnid  = %d";
178          $sql["args"][]      = $id;
179        }
180        elseif (!empty($id) and $type == "variation") {
181          $sql["sqlwhere"][]  = " AND td.nid  = %d";
182          $sql["args"][]      = $id;
183        }
184        $tablesort = " ORDER BY td.weight, td.name ASC ". implode($tablesort);
185        $sql_final =  "SELECT ". implode("", $sql["sqlselect"]) .
186          " FROM {term_data} td ". implode("", $sql["sqljoin"]) .
187          " WHERE ". implode("", $sql["sqlwhere"]) . $tablesort;
188        $sql_final = db_rewrite_sql($sql_final);
189        if (!empty($pager)) {
190          $result = pager_query($sql_final, $pager, 0, NULL, $sql["args"]);
191        }
192        else {
193          $result = db_query($sql_final, $sql["args"]);
194        }
195        $var = array();
196        $var[] = "";
197        while ($variation = db_fetch_array($result)) {
198          $var[] = $variation;
199        }
200        unset($var[0]);
201        if (is_array($var)) {
202          $variations[$id][$type][$only_variation][$tablesort][$pager] = $var;
203        }
204      }
205      return $variations[$id][$type][$only_variation][$tablesort][$pager];
206    } // ec_live_subproducts_attribute_get
207    
208    /**
209     * Implementation of hook_insert().
210     *
211     * Save attribute-specific information into the table
212     *
213     * @param node object
214     */
215    function ec_live_subproducts_attribute_insert(&$node) {
216      $categ = $forbid = array();
217      $categories = db_query("SELECT categ_data.name FROM {term_node} categ
218        INNER JOIN {term_data} categ_data ON categ.tid = categ_data.tid
219        WHERE categ_data.vid = %d and categ.nid = %d", variable_get("ec_ls_combination_cat", 0), $node->nid);
220      while ($category = db_fetch_array($categories)) {
221        array_push($categ, $category["name"]);
222      }
223      $categ = implode(",", array_unique($categ));
224      $categories = db_query("SELECT categ_data.name FROM {term_node} categ
225        INNER JOIN {term_data} categ_data ON categ.tid = categ_data.tid
226        WHERE categ_data.vid = %d and categ.nid = %d", variable_get("ec_ls_combination_forbid", 0), $node->nid);
227      while ($category = db_fetch_array($categories)) {
228        array_push($forbid, $category["name"]);
229      }
230      $forbid = implode(",", array_unique($forbid));
231      db_query("INSERT INTO {ec_ls_attribute} (nid, pricevariation, stock, sku, category, forbidden, attr_weight)
232        VALUES (%d, %d, %d, '%s', '%s', '%s', %d)", $node->nid, $node->pricevariation, $node->stock, $node->sku,
233        $categ, $forbid, $node->attr_weight);
234    } // ec_live_subproducts_attribute_insert
235    
236    /**
237     * Implemenation of hook_load
238     * @param node object to load additional information for
239     * @return object with attribute fields
240     */
241    function ec_live_subproducts_attribute_load(&$node) {
242      return db_fetch_object(db_query("SELECT pricevariation, stock, sku, attr_weight FROM {ec_ls_attribute} WHERE nid = %d", $node->nid));
243    } // ec_live_subproducts_attribute_load
244    
245    /**
246     *
247     */
248    function ec_live_subproducts_attribute_surcharge($attribute, $show_price = TRUE, $show_name = TRUE) {
249      $output = "";
250      if (!empty($show_name)) {
251        $output .= $attribute["title"];
252      }
253      if (!empty($show_price) and $attribute["pricevariation"] != 0) {
254        $output .= " (". payment_format($attribute["pricevariation"]) .")";
255      }
256      return $output;
257    } // ec_live_subproducts_attribute_surcharge
258    
259    /**
260     * Implementation of hook_update, which saves updated attribute-specific
261     * information into the table
262     * @param node object
263     */
264    function ec_live_subproducts_attribute_update(&$node) {
265      $categ = $forbid = array();
266      $categories = db_query("SELECT categ_data.name FROM {term_node} categ
267        INNER JOIN {term_data} categ_data ON categ.tid = categ_data.tid
268        WHERE categ_data.vid = %d and categ.nid = %d", variable_get("ec_ls_combination_cat", 0), $node->nid);
269      while ($category = db_fetch_array($categories)) {
270        array_push($categ, $category["name"]);
271      }
272      $categ = implode(",", array_unique($categ));
273      $categories = db_query("SELECT categ_data.name FROM {term_node} categ
274        INNER JOIN {term_data} categ_data ON categ.tid = categ_data.tid
275        WHERE categ_data.vid = %d and categ.nid = %d", variable_get("ec_ls_combination_forbid", 0), $node->nid);
276      while ($category = db_fetch_array($categories)) {
277        array_push($forbid, $category["name"]);
278      }
279      $forbid = implode(",", array_unique($forbid));
280      db_query("UPDATE {ec_ls_attribute} SET pricevariation = %d, stock = %d, sku = '%s', category = '%s',
281        forbidden = '%s', attr_weight = %d WHERE nid = %d", $node->pricevariation, $node->stock, $node->sku, $categ,
282        $forbid, $node->attr_weight, $node->nid);
283      // Subproducts that have this attribute should be deleted
284      // since updating each and every one can be a high demanding
285      // process.
286      $pparent_sql = db_query("SELECT DISTINCT pnid FROM {ec_ls_pattribute} WHERE nid = %d", $node->nid);
287      while ($pparent = db_fetch_object($pparent_sql)) {
288        node_delete($pparent->pnid);
289      }
290    } // ec_live_subproducts_attribute_update
291    
292    /**
293     * Validate attribute forms
294     * @param $form_id form id
295     * @param $form form values
296     */
297    function ec_live_subproducts_attribute_validate($form_id, &$form) {
298      // price variation and stock must be numbers
299      if (!empty($form["attribute"]["pricevariation"]) and !is_numeric($form["attribute"]["pricevariation"]["#value"])) {
300        form_set_error("pricevariation", t("The price variation must be numeric."));
301      }
302      if (!empty($form["attribute"]["stock"]) and !is_numeric($form["attribute"]["stock"]["#value"])) {
303        form_set_error("stock", t("The stock must be numeric."));
304      }
305    } // ec_live_subproducts_attribute_validate
306    
307    /**
308     * Implementation of hook_view, add our node specific information
309     * @param node object to display
310     * @param boolean is this a teaser or full node?
311     * @param boolean is this displaying on its own page
312     * @return node
313     */
314    function ec_live_subproducts_attribute_view($node, $teaser = FALSE, $page = FALSE) {
315      $node = node_prepare($node, $teaser);
316      $node->content["ec_live_subproducts_attribute"] = array(
317         "#value" => theme("ec_live_subproducts_attribute_view", $node),
318         "#weight" => -1,
319       );
320      return $node;
321    } // ec_live_subproducts_attribute_view
322    
323    /**
324     *  Add the combinated product to cart
325     * @param node a node object representing a parent product
326     * @param data an object with properties representing, in this case, subproduct attributes
327     * @return boolean indicating success or failure in setting a subproduct
328     */
329    function ec_live_subproducts_cart_add_item(&$node, $data) {
330      $priceupdate = array();
331      if (!empty($_POST["priceupdate"]) and !empty($_POST["variations"]) ) {
332        while (list($variation, $attribute) = ($_POST["variations"])) {
333          array_push($priceupdate, $variation .",". $attribute);
334        }
335        drupal_goto($_REQUEST["q"], "priceupdate=". implode(";", $priceupdate));
336      }
337      // Create the subproduct if it doesnt exist
338      $newnode = ec_live_subproducts_subproduct_set($node, $_POST["variations"]);
339      if (!empty($newnode) and $node = node_load($newnode)) {
340        return TRUE;
341      }
342      else {
343        return FALSE;
344      }
345    } // ec_live_subproducts_cart_add_item
346    
347    /**
348     * Find all the combinations of a given set of arrays.
349     *
350     * This helper function is used to generate all the possible combinations
351     * of a set of attributes. In this way, we generate lists of subproducts.
352     *
353     * @param $array
354     *   Structured array of attributes.
355     * @param $start
356     *   Used internally, should not be passed in.
357     * @param $value
358     *   Used internally, should not be passed in.
359     */
360    function ec_live_subproducts_combine(&$array, $start = 0, $value = array()) {
361      global $_ec_live_subproducts_combination_results;
362      global $_ec_live_subproducts_restore;
363      global $_ec_live_subproducts_restore_2;
364      global $_ec_live_subproducts_total;
365      $keys = array_keys($array);
366      $number = count($keys) - 1;
367      foreach ($array[$keys[$start]] as $value[$start]) {
368        if (!$_ec_live_subproducts_total) {
369          return $results;
370        }
371        $_ec_live_subproducts_restore[$keys[$start]]++;
372        if ( $_ec_live_subproducts_restore[$keys[$start]] < $_ec_live_subproducts_restore_2[$keys[$start]] ) {
373          continue;
374        }
375        if ($start < $number) {
376          ec_live_subproducts_combine($array, $start + 1, $value);
377        }
378        else {
379          if ( $_ec_live_subproducts_restore[$keys[$start]] == $_ec_live_subproducts_restore_2[$keys[$start]] ) {
380            continue;
381          }
382          $values = array();
383          for ($i = 0; $i <= $number; $i++) {
384            $values[$keys[$i]] = $value[$i] ;
385          }
386          $_ec_live_subproducts_combination_results[] = $values;
387          --$_ec_live_subproducts_total;
388        }
389      }
390    } // ec_live_subproducts_combine
391    
392    /**
393     * Implementation of hook_cron().
394     */
395    function ec_live_subproducts_cron() {
396      return;
397      global $_ec_live_subproducts_combination_results;
398      global $_ec_live_subproducts_restore;
399      global $_ec_live_subproducts_restore_2;
400      global $_ec_live_subproducts_total;
401      $_ec_live_subproducts_total = variable_get("ec_live_subproducts_combination_cron", 50);
402      if (!$pproducts = ec_live_subproducts_pproduct_get()) {
403        return;
404      }
405      foreach ($pproducts as $pproduct) {
406        $_ec_live_subproducts_restore = array();
407        $_ec_live_subproducts_combination_results = array();
408        $_ec_live_subproducts_restore_2 = array();
409        $_ec_live_subproducts_restore_temp = explode(";", $pproduct->pproduct["children_creation"]);
410        foreach ($_ec_live_subproducts_restore_temp as $temp) {
411          $temp2 = explode(":", $temp);
412          $_ec_live_subproducts_restore_2[$temp2[0]] = $temp2[1];
413        }
414        $options = array();
415        foreach ($pproduct->pproduct_attributes as $attribute) {
416          $options[$attribute["tid"]][] = $attribute["nid"];
417        }
418        if (empty($options)) {
419          continue;
420        }
421        ec_live_subproducts_combine($options);
422        if (empty($_ec_live_subproducts_combination_results)) {
423          continue;
424        }
425        $temp = "";
426        foreach ($_ec_live_subproducts_restore as $index => $value) {
427          $temp .= $index .":". $value .";";
428        }
429        db_query("UPDATE {ec_ls_pproduct} SET children_creation = '%s' WHERE nid = %d", $temp, $pproduct->nid);
430        foreach ($_ec_live_subproducts_combination_results as $combination) {
431          $temp = $pproduct;
432          ec_live_subproducts_subproduct_set($temp, $combination, TRUE);
433        }
434      }
435    } // ec_live_subproducts_cron
436    
437    /**
438     * Implementation of hook_help()
439     * Display help text for the module
440     */
441    function ec_live_subproducts_help($section) {
442      switch ($section) {
443        case "admin/store/attributes":
444          // list all attributes
445          return "<p>". t("All variations and attributes and their main options. You can !create_variations.",
446            array("!create_variations" => l(t("Create Variations"),
447            "admin/content/taxonomy/". variable_get("ec_live_subproducts_variation_vid", 0) ."/add/term",
448            NULL, "destination=admin/store/attributes"))) ."</p>";
449        case "admin/store/pproducts":
450          // list all pproducts
451          return "<p>". t("Every parent product that has subproducts. During cron, new combinations are made, but you can !update_manually.",
452            array("!update_manually" => l(t("update manually"), "admin/store/pproducts", NULL, "update=true"))) ."</p>";
453        case "node/add/ec-live-subproducts-attribute":
454          // creating an attribute
455          return "<p>". t("Some special products can have changeable attributes.") ."</p>";
456      }
457      // creating an attribute
458      if (preg_match("|node/\d+/attribute|", $section)) {
459        return "<p>". t("Select which attributes this product will have. Also you may select what of them are selected by default. To change the attribute's price or SKU, go to !variations_and_attributes_page",
460          array("!variations_and_attributes_page" => l(t("variations and attributes page"), "admin/store/attributes"))) .".</p>";
461      }
462    } // ec_live_subproducts_help
463    
464    /**
465     * Implementation of hook_form_alter().
466     */
467    function ec_live_subproducts_form_alter($form_id, &$form) {
468      if ($form_id == "taxonomy_form_term" and $form["vid"]["#value"] == variable_get("ec_live_subproducts_variation_vid", 0)) {
469        if (isset($form["tid"])) {
470          $term = db_fetch_array(db_query("SELECT * FROM {ec_ls_variation} WHERE tid = %d", $form["tid"]["#value"]));
471        }
472          $form["listtype"] = array(
473            "#type" => "select",
474            "#title" => t("List type"),
475            "#default_value" => isset($form["tid"]) ? $term["listtype"] : 0,
476            "#options" => array(t("Radio List"), t("Drop-Down List")),
477            "#description" => t("How your attributes should be presented"),
478          );
479          $form["showprice"] = array(
480            "#type" => "select",
481            "#title" => t("Show surcharge"),
482            "#default_value" => isset($form["tid"]) ? $term["showprice"] : 0,
483            "#options" => array(t("Yes"), t("No")),
484            "#description" => t("Show surcharge after the attribute name, like 'Iron (+\$10.00)'"),
485          );
486        $form["submit"]["#weight"] = 10;
487        if (!empty($form["delete"])) {
488          $form["delete"]["#weight"] = 10;
489        }
490      }
491      elseif ($form_id == "taxonomy_form_vocabulary" and ($form["vid"]["#value"] == variable_get("ec_live_subproducts_variation_vid", NULL) or $form["vid"]["#value"] == variable_get("ec_live_subproducts_combination_cat", NULL) or $form["vid"]["#value"] == variable_get("ec_live_subproducts_combination_forbid", NULL))) {
492        $form["nodes"] = array(
493          "#type"   => "value",
494          "#value"  => array("ec_live_subproducts_attribute" => "ec_live_subproducts_attribute")
495        );
496        if ($form["vid"]["#value"] == variable_get("ec_live_subproducts_combination_cat", NULL) or $form["vid"]["#value"] == variable_get("ec_live_subproducts_combination_forbid", NULL)) {
497          $form["tags"]["#value"] = TRUE;
498          $form["tags"]["#disabled"] = TRUE;
499        }
500        else {
501          unset($form["tags"]);
502        }
503        unset($form["multiple"]);
504        unset($form["hierarchy"]);
505        unset($form["relations"]);
506        unset($form["required"]);
507        unset($form["delete"]);
508      }
509      elseif ($form_id == "ec_live_subproducts_attribute_node_form") {
510        $terms = db_query("SELECT tid FROM {term_data} WHERE vid = %d", variable_get("ec_live_subproducts_variation_vid", 0));
511        if (!db_num_rows($terms)) {
512          drupal_set_message("You cannot create an attribute without a variation. Create one variation first.", error);
513          $form["submit"]["#disabled"] = true;
514        }
515        if (!empty($_GET["variation"])) {
516          $form["taxonomy"][variable_get("ec_live_subproducts_variation_vid", 0)]["#value"] = $_GET["variation"];
517        }
518        $form["taxonomy"][variable_get("ec_live_subproducts_variation_vid", 0)]["#disabled"] = TRUE;
519      }
520    } // ec_live_subproducts_form_alter
521    
522    /**
523     * Define internal Drupal links
524     *
525     * This hook enables modules to add links to many parts of Drupal. Links
526     * may be added in nodes, in the global navigation bar, and in the main
527     * site navigation menu, for example.
528     */
529    function ec_live_subproducts_link($type, $node = NULL, $teaser = false) {
530      if (!empty($node->ptype) and !is_array(module_invoke($node->ptype, "productapi", NULL, "subproduct_types"))) {
531        return;
532      }
533      $links = array();
534      $links["add_to_cart"] = array();
535      return $links;
536    } // ec_live_subproducts_link
537    
538    /**
539     * Implementation of hook_menu().
540     *
541     * Add some new menu itens
542     */
543    function ec_live_subproducts_menu($may_cache) {
544      if (empty($may_cache)) {
545        $access_manage = user_access(t("manage subproducts"));
546        $access_own    = user_access(t("manage own subproducts"));
547        $items = array();
548        $items[] = array(
549          "path"                => "admin/store/attributes",
550          "callback"            => "drupal_get_form",
551          "callback arguments"  => "ec_live_subproducts_attribute_admin",
552          "title"               => t("Subproduct attributes"),
553          "description"         => t("List all variations and attributes"),
554          "access"              => $access_manage,
555          "type"                => MENU_NORMAL_ITEM);
556        $items[] = array(
557          "path"                => "admin/store/pproducts",
558          "callback"            => "ec_live_subproducts_pproduct_admin",
559          "title"               => t("Parent products"),
560          "description"         => t("Show all parent products"),
561          "access"              => $access_manage,
562          "type"                => MENU_NORMAL_ITEM);
563        if (arg(0) == "node" and is_numeric(arg(1))) {
564          $node = node_load(arg(1));
565            if (!empty($node->ptype) and is_array(module_invoke($node->ptype, "productapi", NULL, "subproduct_types"))) {
566            $items[] = array(
567              "path"                => "node/". arg(1) ."/attributes",
568              "callback"            => "drupal_get_form",
569              "callback arguments"  => "ec_live_subproducts_pproduct_attributes",
570              "title"               => t("Subproduct attributes"),
571              "description"         => t("Subproduct attributes"),
572              "access"              => $access_manage,
573              "type"                => MENU_LOCAL_TASK);
574          }
575        }
576        $items[] = array(
577          "path"                => "refreshpricestock",
578          "access"              => user_access("access content"),
579          "type"                => MENU_CALLBACK,
580          "callback"            => "ec_live_subproducts_refresh");
581      }
582      return $items;
583    } // ec_live_subproducts_menu
584    
585    /**
586     * Search if a VALUE is in a array like array[SOMETHING][SOMETHING] = VALUE
587     */
588    function ec_live_subproducts_multiarray_search($array, $field, $value) {
589      if (empty($array) or !is_array($array)) {
590        return FALSE;
591      }
592      while (isset($array[key($array)])) {
593        if ($array[key($array)][$field] == $value) {
594          return key($array);
595        }
596        next($array);
597      }
598      return FALSE;
599    } // ec_live_subproducts_multiarray_search
600    
601    /**
602     * Implementation of hook_node_info().
603     *
604     * Define the node type
605     */
606    function ec_live_subproducts_node_info() {
607      return array(
608        "ec_live_subproducts_attribute" => array(
609          "name"        => t("Subproduct Attribute"),
610          "module"      => "ec_live_subproducts_attribute",
611          "description" => t("Some special products can have changeable attributes."))
612    
613       );
614    } // ec_live_subproducts_node_info
615    
616    /**
617     * Implementation of hook_nodeapi().
618     *
619     * Modify the nodes aperance, loading and saving processes
620     */
621    function ec_live_subproducts_nodeapi(&$node, $op, $arg) {
622      // Only act on subproducts nodes.
623      if (!empty($node->ptype) and
624          !is_array(module_invoke($node->ptype, "productapi", NULL, "subproduct_types"))) {
625        return;
626      }
627      switch ($op) {
628        case "load":
629          if (empty($node->pparent)) {
630            $subproduct_options = array();
631            $options = db_query("SELECT * FROM {ec_ls_pproduct} WHERE nid = %d ", $node->nid);
632            if (db_num_rows($options)) {
633              $subproduct_options = db_fetch_array($options);
634              if (!empty($subproduct_options["default_selection"])) {
635                $temp = explode(";", $subproduct_options["default_selection"]);
636                foreach ($temp as $value) {
637                  $value = explode(",", $value);
638                  $default_selection[$value[0]] = $value[1];
639                }
640                $subproduct_options["default_selection"] = $default_selection;
641              }
642            }
643            return array("pproduct_attributes" => ec_live_subproducts_attribute_get($node->nid, "baseproduct"),
644                        "pproduct" => $subproduct_options);
645          }
646          else {
647            return array("subproduct_attributes" => ec_live_subproducts_attribute_get($node->nid, "product"));
648          }
649          break;
650        case "view":
651          if (empty($node->pparent) and !empty($node->pproduct_attributes) ) {
652            if (empty($arg)) {
653              $node->content["subproduct-attributes"] = array(
654                "#value" => drupal_get_form("ec_live_subproducts_pproduct_view", $node),
655                "#weight" => 1,
656              );
657            }
658            unset($node->content["price"]);
659            unset($node->content["cart"]);
660            unset($node->links["add_to_cart"]);
661          }
662          elseif (!empty($node->pparent) and !empty($node->subproduct_attributes)) {
663            foreach ($node->subproduct_attributes as $attribute) {
664              $attributes[$attribute["tid"]]["variation"] = $attribute["name"];
665              $attributes[$attribute["tid"]]["attribute"] = $attribute["title"];
666            }
667            $node->content["subproduct-attributes"] = array(
668                "#value" => theme("ec_live_subproducts_attribute_items", $attributes),
669                "#weight" => 1,
670              );
671          }
672          break;
673        case "update":
674          if (empty($node->pparent) and $nodes = db_query("SELECT nid FROM {ec_product} WHERE pparent = %d ", $node->nid)) {
675            while ($product = db_fetch_object($nodes)) {
676              node_delete($product->nid);
677            }
678          }
679          break;
680        case "delete":
681          // Delete any products that have this as a base product.
682          if (!empty($node->nid)) {
683            $nodes = db_query("SELECT nid FROM {ec_product} WHERE pparent = %d", $node->nid);
684            while ($product = db_fetch_object($nodes)) {
685              node_delete($product->nid);
686            }
687            db_query("DELETE FROM {ec_ls_bpattribute} WHERE pnid = %d", $node->nid);
688            db_query("DELETE FROM {ec_ls_pattribute} WHERE pnid = %d", $node->nid);
689            db_query("DELETE FROM {ec_ls_pproduct} WHERE nid = %d", $node->nid);
690          }
691          break;
692      }
693    } // ec_live_subproducts_nodeapi
694    
695    /**
696     * Implementation of hook_perm().
697     * Define the permissions this module uses
698     */
699    function ec_live_subproducts_perm() {
700      return array("manage subproducts", "manage own subproducts", "manage attributes and variations");
701    } // ec_live_subproducts_perm
702    
703    /**
704     * A PHP hack to deal with object cloning in PHP5 and PHP4
705     */
706    function ec_live_subproducts_php_clone($object) {
707      if (version_compare(phpversion(), "5.0") < 0) {
708        return $object;
709      }
710      else {
711        return @clone($object);
712      }
713    } // ec_live_subproducts_php_clone
714    
715    /**
716     * Show all subproducts
717     */
718    function ec_live_subproducts_pproduct_admin() {
719      if (!empty($_GET["update"])) {
720        $output = ec_live_subproducts_cron();
721      }
722      if (!$pproducts = ec_live_subproducts_pproduct_get()) {
723        return t("There are no parent products available.");
724      }
725      foreach ($pproducts as $pproduct) {
726        $actual_children = db_fetch_array(db_query("SELECT COUNT(nid) FROM {ec_product} WHERE pparent = %d", $pproduct->nid));
727        $rows[] = array(
728          l($pproduct->title, "node/". $pproduct->nid),
729          $actual_children["COUNT(nid)"]
730        );
731      }
732      $header = array( t("Parent Product"), t("Subproducts"));
733      $output = theme('table', $header, $rows);
734      return $output;
735    } // ec_live_subproducts_pproduct_admin
736    
737    /**
738     *
739     */
740    function ec_live_subproducts_pproduct_attributes() {
741      $form = array();
742      $attributes = ec_live_subproducts_attribute_get();
743      $node = node_load(arg(1));
744      if (empty($attributes)) {
745        return array();
746      }
747      $form["sku_title"] = array(
748        "#type" => "checkbox",
749        "#title" => t("Assume SKU as title"),
750        "#description" => t("If your product has many variations, your final product name will be quite long. This your set the products name the same as SKU."),
751        "#default_value" => !empty($node->pproduct) ? $node->pproduct["sku_title"] : 0);
752      foreach ($attributes as $attribute) {
753        if (empty($attribute["nid"])) {
754          continue;
755        }
756        $default = $node->pproduct["default_selection"][$attribute["tid"]];
757        if ($current_variation != $attribute["tid"]) { // default selection
758          $default = !empty($default) ? $default : $attribute["nid"];
759          $current_variation = $attribute["tid"];
760        }
761        $form_temp = array();
762        $form["include"][$attribute["nid"]] = array(
763          "#type" => "checkbox",
764          "#default_value" => ec_live_subproducts_multiarray_search($node->pproduct_attributes, "nid", $attribute["nid"]));
765        $form["variation"][$attribute["nid"]] = array(
766          "#type"           => "item",
767          "#value"          => $attribute["name"]);
768        $form["attribute"][$attribute["nid"]] = array(
769          "#type"           => "item",
770          "#value"          => $attribute["title"]);
771        $form["pricevariation"][$attribute["nid"]] = array(
772          "#type"           => "item",
773          "#value"          => payment_format($attribute["pricevariation"]));
774        $form["stock"][$attribute["nid"]] = array(
775          "#type"           => "item",
776          "#value"          => $attribute["stock"]);
777        $form["sku"][$attribute["nid"]] = array(
778          "#type"           => "item",
779          "#value"          => $attribute["sku"]);
780        $form["default"][$attribute["nid"]] = array(
781          "#type"           => "radio",
782          "#parents"        => array($attribute["tid"]),
783          '#return_value'   => $attribute["nid"],
784          "#default_value"  => $node->pproduct["default_selection"][$attribute["tid"]]);
785        $form["variation_hidden"][$attribute["nid"]] = array(
786          "#type"           => "hidden",
787          "#value"          => $attribute["name"]);
788      }
789      $form["nid"] = array("#type" => "hidden", "#value" => arg(1));
790      $form["#tree"] = TRUE;
791      $form["submit"] = array(
792        "#type"   => "submit",
793        "#value"  => t("Submit")
794      );
795      return $form;
796    } // ec_live_subproducts_pproduct_attributes
797    
798    /**
799     *
800     */
801    function ec_live_subproducts_pproduct_attributes_submit($form_id, &$form) {
802      $node = node_load($form["nid"]);
803      $sql = $sql_args = $default_selection = array();
804      // delete all parent product attributes and relink only the marked
805      db_query("DELETE FROM {ec_ls_bpattribute} WHERE pnid = %d", $node->nid);
806      foreach (element_children($form["include"]) as $attribute) {
807        if (!empty($form["include"][$attribute])) {
808          array_push($sql, "(%d, %d)");
809          array_push($sql_args, $node->nid);
810          array_push($sql_args, $attribute);
811        }
812      }
813      if (!empty($sql)) {
814        db_query("INSERT INTO {ec_ls_bpattribute} (pnid, nid) VALUES ". implode(",", $sql), $sql_args);
815      }
816      // assing special values to the parent product only
817      while (list($variation, $attribute) = each($form)) {
818        if (is_numeric($variation)) {
819          array_push($default_selection, $variation .",". $attribute);
820        }
821      }
822      $default_selection = implode(";", $default_selection);
823      if (empty($node->pproduct)) {
824        db_query("INSERT INTO {ec_ls_pproduct} (sku_title, default_selection, nid) VALUES (%d,'%s',%d)",
825          $form["sku_title"], $default_selection, $node->nid);
826      }
827      else {
828        db_query("UPDATE {ec_ls_pproduct} SET sku_title = %d, default_selection = '%s' WHERE nid = %d",
829          $form["sku_title"], $default_selection, $node->nid);
830      }
831      node_save($node); // save the parent product
832    } // ec_live_subproducts_pproduct_attributes_submit
833    
834    /**
835     *
836     */
837    function ec_live_subproducts_pproduct_attributes_validate($form_id, &$form) {
838      $current_variation = NULL;
839      foreach ($form as $variation => $attribute) {
840        if (is_numeric($variation)) {
841          $default_selection[$attribute] = true;
842        }
843      }
844      foreach (element_children($form["include"]) as $attribute) {
845        if ($current_variation != $form["variation_hidden"][$attribute]) {
846          if (!empty($forbidden_default_value) and !empty($variation_set)) {
847            form_set_error("submit", t("Only enabled attributes can be default."), "error");
848          }
849          $forbidden_default_value = FALSE;
850          $variation_set = FALSE;
851          $current_variation = $form["variation_hidden"][$attribute];
852        }
853        if (!empty($form["include"][$attribute])) {
854          $variation_set = TRUE;
855        }
856        elseif (empty($form["include"][$attribute]) and !empty($default_selection[$attribute])) {
857          $forbidden_default_value = TRUE;
858        }
859      }
860      if (!empty($forbidden_default_value) and !empty($variation_set)) {
861        form_set_error("submit", t("Only enabled attributes can be default."), "error");
862      }
863    } // ec_live_subproducts_pproduct_attributes_validate
864    
865    /**
866     * Get all attributes from a given parent product
867     *
868     * @param $id
869     *  number, the product id if only 1 product is needed
870     * @return
871     *  array, with all attributes of each product
872     */
873    function ec_live_subproducts_pproduct_get($id = NULL, $only_nid = NULL) {
874      if (empty($id)) {
875        $pproduct_types = module_invoke_all("productapi", NULL, "subproduct_types");
876        $pproducts = db_query("SELECT nid FROM {ec_product} WHERE pparent = 0 AND ptype IN ('%s')",
877          implode("','", $pproduct_types));
878      }
879      else {
880        $pproducts = db_query("SELECT nid FROM {ec_product} WHERE nid = %d", $id);
881      }
882      while ($pproduct = db_fetch_object($pproducts)) {
883        if (empty($only_nid)) {
884          $final_pproducts[$pproduct->nid] = node_load($pproduct->nid);
885        }
886        else {
887          $final_pproducts[$pproduct->nid] = $pproduct->nid;
888        }
889      }
890      return empty($final_pproducts) ? FALSE : $final_pproducts;
891    } // ec_live_subproducts_pproduct_get()
892    
893    /**
894     * Show all attributes from a given product
895     *
896     * @param &$node
897     *  object, the node/product
898     * @return
899     *  string, a HTML form with all attributes
900     */
901    function ec_live_subproducts_pproduct_view(&$node) {
902      $listtypes = array("radios", "select");
903      $price = $current_variation = NULL;
904      if (!empty($_GET["priceupdate"])) {
905        $variations = explode(";", $_GET["priceupdate"]);
906        while (list(, $variation) = each($variations)) {
907          if (empty($variation)) {
908            break;
909          }
910          $variation2 = explode(",", $variation);
911          $priceupdate[$variation2[0]] = $variation2[1];
912        }
913      }
914      while (list(, $attr) = each($node->pproduct_attributes)) {
915        if (empty($json_attr[$attr["tid"]]["checked"]) and // define the current selection
916            (!empty($priceupdate[$attr["tid"]]) and $priceupdate[$attr["tid"]] == $attr["nid"] or
917            (empty($priceupdate[$attr["tid"]]) and ($node->pproduct["default_selection"][$attr["tid"]] == $attr["nid"] or
918            empty($node->pproduct["default_selection"][$attr["tid"]]))))) {
919          $price += $attr["pricevariation"];
920          $json_attr[$attr["tid"]]["checked"] = $attr["nid"];
921        }
922        $options[$attr["tid"]][$attr["nid"]] = ec_live_subproducts_attribute_surcharge($attr, !$attr["showprice"]);
923        $json_attr[$attr["tid"]][$attr["nid"]]["price"] = (float) $attr["pricevariation"];
924        $json_attr[$attr["tid"]][$attr["nid"]]["category"] = $attr["category"];
925        $json_attr[$attr["tid"]][$attr["nid"]]["forbidden"] = $attr["forbidden"];
926        $json_attr[$attr["tid"]][$attr["nid"]]["stock"] = (int) $attr["stock"];
927        $json_attr[$attr["tid"]][$attr["nid"]]["description"] = check_markup($attr["attribute_description"], $attr["format"]);
928      }
929      reset($node->pproduct_attributes);
930      while (list(, $attr) = each($node->pproduct_attributes)) {
931        if ($current_variation != $attr["tid"] or empty($current_variation)) {
932          $current_variation = $attr["tid"];
933          $form["title"][$attr["tid"]] = array(
934            "#type" => "markup",
935            "#value" => $attr["name"]
936          );
937          $form["vardesc"][$attr["tid"]] = array(
938            "#type" => "markup",
939            "#value" => $attr["description"]
940          );
941          $form["variations"][$attr["tid"]] = array(
942            "#attributes" => array("class" => "attr"),
943            "#default_value" => $json_attr[$attr["tid"]]["checked"],
944            "#options" => $options[$attr["tid"]],
945            "#type" => $listtypes[$attr["listtype"]],
946          );
947          $form["description"][$attr["tid"]] = array(
948            "#type" => "markup" ,
949            "#value" => check_markup($attr["attribute_description"])
950          );
951        }
952      }
953      $payment_format["dpos"] = variable_get("payment_decimal_places", 2);  // decimal places
954      $payment_format["decimal"] = variable_get("payment_decimal", ".");    // decimal symbol
955      $payment_format["thousand"] = variable_get("payment_thousands", ","); // thousand symbol
956      $payment_format["symbol"] = variable_get("payment_symbol", "$");      // current symbol
957      $payment_format["spos"] = variable_get("payment_symbol_position", 1); // symbol position
958      drupal_add_js(array("FormatPrice" => $payment_format), "setting");
959      drupal_add_js(array("price" => (float) $node->price), "setting");
960      drupal_add_js(array("url_ajax" => url("refreshpricestock/")), "setting");
961      drupal_add_js(array("messages" => array("price" => t("Price"),
962        "sold_out" => t("Sold Out"),
963        "forbidden" => t("This combination is not possible."))), "setting");
964      drupal_add_js(array("attr" => $json_attr), "setting");
965      $form["newprice"] = array(
966        "#prefix" => "<div id='price'>",
967        "#type" => "item",
968        "#title" => t("Price"),
969        "#value" => payment_format($node->price + $price)
970      );
971      $form["priceupdate"] = array(
972        "#name" => "priceupdate",
973        "#type" => "button",
974        "#value" => t("update price")
975      );
976      $form["add_to_cart"] = array(
977        "#attributes" => array("class" => "addtocart"),
978        "#suffix" => "</div>",
979        "#type" => "submit",
980        "#value" => t("add to cart")
981      );
982      $form["#method"] = "post";
983      $form["#action"] = url("cart/add/". $node->nid, "destination=node/". $node->nid);
984      $form["#tree"] = TRUE;
985      return $form;
986    } // ec_live_subproducts_pproduct_view()
987    
988    /**
989     *
990     */
991    function ec_live_subproducts_subproduct_set(&$node, &$data, $show_erros = NULL) {
992      if (empty($data)) {
993        return FALSE;
994      }
995      $newnode = ec_live_subproducts_php_clone($node);
996      $sql_terms = $sql_args = $sql_select = $sql_where = $category = $forbidden = array();
997      $stock = -1;
998      $nid = $newnode->nid;
999      $price = $newnode->price;
1000      $title = empty($newnode->pproduct["sku_title"]) ? $newnode->title : $newnode->sku;
1001      foreach ($data as $attribute) {
1002        foreach ($newnode->pproduct_attributes as $pproduct_attribute) {
1003          if ($pproduct_attribute["nid"] != $attribute) {
1004            continue;
1005          }
1006          $attributes_subproduct[] = $pproduct_attribute;
1007          $sql_select[] = " INNER JOIN {ec_ls_pattribute} a". $attribute ." ON a". $attribute .".pnid = p.nid ";
1008          $sql_where[] = " AND a". $attribute .".nid = $attribute ";
1009          $toggle = TRUE;
1010          $price += $pproduct_attribute["pricevariation"];
1011          $forbidden = array_merge(explode(",", $pproduct_attribute["forbidden"]), $forbidden);
1012          $category  = array_merge(explode(",", $pproduct_attribute["category"]), $category);
1013          if (($stock > $pproduct_attribute["stock"] and $pproduct_attribute["stock"] != -1) or ($stock < $pproduct_attribute["stock"] and $stock == -1)) {
1014            $stock = $pproduct_attribute["stock"];
1015          }
1016          if (empty($newnode->pproduct["sku_title"])) {
1017            $title .= " ". $pproduct_attribute["title"];
1018          }
1019          else {
1020            $title .= $pproduct_attribute["sku"];
1021          }
1022        }
1023        $sql_terms[] = "(%d,%d)";
1024        $sql_args[] = "XX";
1025        $sql_args[] = $attribute;
1026      }
1027      if ($stock == 0) {
1028        if (empty($show_erros)) {
1029          drupal_set_message(t("Sold Out"), "error");
1030        }
1031        return FALSE;
1032      }
1033      // if this combination is forbidden, cancel process
1034      foreach ($forbidden as $forbid) {
1035        if (!empty($forbid) and in_array($forbid, $category)) {
1036          if (empty($show_erros)) {
1037            drupal_set_message(t("This combination is not possible."), "error");
1038          }
1039          return FALSE;
1040        }
1041      }
1042      $result = "SELECT p.nid FROM {ec_product} p ". implode("", $sql_select) ." WHERE p.pparent = %d ". implode("", $sql_where);
1043      $result = db_query($result, $nid);
1044      if (db_num_rows($result) == 0) {
1045        $newnode->title = $title;
1046        $newnode->stock = $stock;
1047        $newnode->price = $price;
1048        $newnode->pparent = $nid;
1049        $newnode->comment = 0;
1050        $newnode->voting = 0;
1051        unset($newnode->price_type);
1052        unset($newnode->path);
1053        unset($newnode->nid);
1054        node_save($newnode);
1055        $toggle = FALSE;
1056        foreach ($sql_args as $index => $value) {
1057          if ($value == "XX") {
1058            $sql_args[$index] = $newnode->nid;
1059          }
1060        }
1061        db_query("INSERT INTO {ec_ls_pattribute} (pnid, nid) VALUES ". implode(",", $sql_terms), $sql_args);
1062      }
1063      else {
1064        $newnode = db_fetch_object($result);
1065      }
1066      return $newnode->nid;
1067    } // ec_live_subproducts_subproduct_set
1068    
1069    /**
1070     * AJAX function that returns the updated price for a given
1071     * combination
1072     */
1073    function ec_live_subproducts_refresh() {
1074      // args[0] = price and args[1] = price or aid
1075      $args = explode( ";" , arg(1) );
1076      if ($args[0] == "permutation_price") {
1077        print payment_format($args[1]);
1078      }
1079      elseif ($args[0] == "attribute_price") {
1080        print ec_live_subproducts_attribute_surcharge($args[1]);
1081      }
1082      exit();
1083    } // ec_live_subproducts_refresh
1084    
1085    /**
1086     * Implementation of hook_taxonomy(): customise the taxomony form
1087     * (terms and vocabularies) results
1088     */
1089    function ec_live_subproducts_taxonomy($op, $type, $form = NULL) {
1090      if ($type == "term" and isset($form["showprice"])) {
1091        if ($op == "insert") {
1092          db_query("INSERT INTO {ec_ls_variation} (listtype,showprice,tid) VALUES (%d,%b,%d)", $form["listtype"], $form["showprice"], $form["tid"]);
1093        }
1094        if ($op == "update") {
1095          db_query("UPDATE {ec_ls_variation} SET listtype = %b, showprice = %b WHERE tid = %d", $form["listtype"], $form["showprice"], $form["tid"]);
1096        }
1097        if ($op == "delete") {
1098        }
1099      }
1100    } // ec_live_subproducts_submit
1101    
1102    /**
1103     * Theme function to display additional attribute data
1104     *
1105     * @param &$attributes
1106     *  array, all attributes and their attributes
1107     * @return
1108     *  string, HTML string with additional node information
1109     */
1110    function theme_ec_live_subproducts_attribute_items(&$attributes) {
1111      $header = array(array("data" => t("Attributes"), "colspan" => 2));
1112      foreach ($attributes as $attribute) {
1113        $rows[] = array(
1114          array("data" => check_plain($attribute["variation"]) .":"),
1115          array("data" => check_plain($attribute["attribute"])),
1116        );
1117      }
1118      $output .= theme("table", $header, $rows);
1119      return $output;
1120    } // theme_ec_live_subproducts_attribute_items
1121    
1122    function theme_ec_live_subproducts_attribute_view(&$node) {
1123      $output  = "<div class='ec_live_subproducts_attribute_pricevariation'>";
1124      $output .= t("Price Variation: %pricevariation", array("%pricevariation" => $node->pricevariation));
1125      $output .= "</div>";
1126    
1127      $output .= "<div class='ec_live_subproducts_attribute_sku'>";
1128      $output .= t("Sku: %sku", array("%sku" => $node->sku));
1129      $output .= "</div>";
1130    
1131      $output .= "<div class='ec_live_subproducts_attribute_stock'>";
1132      $output .= t("Current stock: %stock", array("%stock" => $node->stock));
1133      $output .= "</div>";
1134    
1135      return $output;
1136    } // theme_ec_live_subproducts_attribute_view
1137    
1138    function theme_ec_live_subproducts_attribute_admin(&$form) {
1139      $current_variation = "";
1140      $header = array(t("Variation") ."/". t("Attribute"), t("Price Variation"), t("Stock"), t("SKU"));
1141      foreach (element_children($form) as $attribute) {
1142        if (!empty($form[$attribute]["variation"]["#value"]) and $current_variation != $form[$attribute]["variation"]["#value"]) {
1143          $current_variation = $form[$attribute]["variation"]["#value"];
1144          $rows[] = array(
1145            array("data" => "<b>". drupal_render($form[$attribute]["variation"]) ."</b>", "colspan" => 4),
1146          );
1147        }
1148        if (!empty($form[$attribute]["attribute"]["#value"])) {
1149          $rows[] = array(
1150            drupal_render($form[$attribute]["attribute"]),
1151            drupal_render($form[$attribute]["pricevariation"]),
1152            drupal_render($form[$attribute]["stock"]),
1153            drupal_render($form[$attribute]["sku"])
1154          );
1155        }
1156      }
1157      if (!empty($rows)) {
1158        $pager = theme("pager", NULL, 50, 0);
1159        if (!empty($pager)) {
1160          $rows[] = array(array("data" => $pager, "colspan" => 6));
1161        }
1162        $output = theme("table", $header, $rows);
1163        $output .= drupal_render($form);
1164      }
1165      else {
1166        $output = "<p>". t("There is no variations or attributes.") ."</p>";
1167      }
1168      return $output;
1169    } // theme_ec_live_subproducts_attribute_admin
1170    
1171    function theme_ec_live_subproducts_pproduct_attributes(&$form) {
1172      $current_variation = NULL;
1173      foreach (element_children($form["include"]) as $attribute) {
1174        if (!empty($form["variation"][$attribute]["#value"]) and $current_variation != $form["variation"][$attribute]["#value"]) {
1175          $current_variation = $form["variation"][$attribute]["#value"];
1176          $rows[] = array(
1177            array("data" => "<em>". drupal_render($form["variation"][$attribute]) ."</em>", "colspan" => 6),
1178          );
1179        }
1180        unset($form["variation"][$attribute]);
1181        $rows[] = array(
1182          drupal_render($form["include"][$attribute]),
1183          drupal_render($form["default"][$attribute]),
1184          drupal_render($form["attribute"][$attribute]),
1185          drupal_render($form["pricevariation"][$attribute]),
1186          drupal_render($form["stock"][$attribute]),
1187          drupal_render($form["sku"][$attribute])
1188        );
1189      }
1190      if (!empty($rows)) {
1191        $pager = theme("pager", NULL, 50, 0);
1192        if (!empty($pager)) {
1193          $rows[] = array(array("data" => $pager, "colspan" => 6));
1194        }
1195        $header = array(theme("table_select_header_cell"), t("Default"), t("Attribute"), t("Price Variation"), t("Stock"), t("SKU"));
1196        $output = drupal_render($form["sku_title"]);
1197        $output .= drupal_render($form["submit"]);
1198        $output .= theme("table", $header, $rows);
1199        $output .= drupal_render($form);
1200      }
1201      else {
1202        $output = "<p>". t("There is no variations or attributes.") ."</p>";
1203      }
1204      return $output;
1205    } // theme_ec_live_subproducts_pproduct_attributes
1206    
1207    function theme_ec_live_subproducts_pproduct_view(&$form) {
1208      $output = "";
1209      drupal_add_js(drupal_get_path("module", "ec_live_subproducts") ."/ec_live_subproducts.js");
1210      drupal_add_css(drupal_get_path("module", "ec_live_subproducts") ."/ec_live_subproducts.css");
1211      foreach (element_children($form["variations"]) as $i) {
1212        $output .= "<div class='variation'><div class='name'>". drupal_render($form["title"][$i]) ."</div>" ;
1213        if (!empty($form["vardesc"][$i]["#value"])) {
1214          $output .= "<div class='vardesc'>". check_markup(drupal_render($form["vardesc"][$i]), $attribute2["format"]) ."</div>" ;
1215        }
1216        $output .= "<table><tr>";
1217        $output .= "<td class='description' id='description-". $i ."' >". drupal_render($form["description"][$i]) ."</td>";
1218        $output .= "<td>". drupal_render($form["variations"][$i]) ."</td></tr></table></div>";
1219        unset($form["image"][$i]);
1220      }
1221      $output .= drupal_render($form);
1222      $output = theme("fieldset", array("#children" => $output));
1223      return $output;
1224    } // theme_ec_live_subproducts_pproduct_view

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.3