ProductCombination2ValuePair.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /* Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * Class ProductCombination2ValuePair
  19. * Used to represent the relation between a product combination, a product attribute and a product attribute value
  20. */
  21. class ProductCombination2ValuePair
  22. {
  23. /**
  24. * Database handler
  25. * @var DoliDB
  26. */
  27. private $db;
  28. /**
  29. * Combination 2 value pair id
  30. * @var int
  31. */
  32. public $id;
  33. /**
  34. * Product combination id
  35. * @var int
  36. */
  37. public $fk_prod_combination;
  38. /**
  39. * Product attribute id
  40. * @var int
  41. */
  42. public $fk_prod_attr;
  43. /**
  44. * Product attribute value id
  45. * @var int
  46. */
  47. public $fk_prod_attr_val;
  48. /**
  49. * Constructor
  50. *
  51. * @param DoliDB $db Database handler
  52. */
  53. public function __construct(DoliDB $db)
  54. {
  55. $this->db = $db;
  56. }
  57. /**
  58. * Translates this class to a human-readable string
  59. *
  60. * @return string
  61. */
  62. public function __toString()
  63. {
  64. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttributeValue.class.php';
  65. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttribute.class.php';
  66. $prodattr = new ProductAttribute($this->db);
  67. $prodattrval = new ProductAttributeValue($this->db);
  68. $prodattr->fetch($this->fk_prod_attr);
  69. $prodattrval->fetch($this->fk_prod_attr_val);
  70. return $prodattr->label.': '.$prodattrval->value;
  71. }
  72. /**
  73. * Creates a product combination 2 value pair
  74. * @return int <0 KO, >0 OK
  75. */
  76. public function create()
  77. {
  78. $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_attribute_combination2val
  79. (fk_prod_combination, fk_prod_attr, fk_prod_attr_val)
  80. VALUES(".(int) $this->fk_prod_combination.", ".(int) $this->fk_prod_attr.", ".(int) $this->fk_prod_attr_val.")";
  81. $query = $this->db->query($sql);
  82. if ($query) {
  83. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'product_attribute_combination2val');
  84. return 1;
  85. }
  86. return -1;
  87. }
  88. /**
  89. * Retrieves a product combination 2 value pair from its rowid
  90. *
  91. * @param int $fk_combination Fk combination to search
  92. * @return int|ProductCombination2ValuePair[] -1 if KO
  93. */
  94. public function fetchByFkCombination($fk_combination)
  95. {
  96. $sql = "SELECT
  97. c.rowid,
  98. c2v.fk_prod_attr_val,
  99. c2v.fk_prod_attr,
  100. c2v.fk_prod_combination
  101. FROM ".MAIN_DB_PREFIX."product_attribute c LEFT JOIN ".MAIN_DB_PREFIX."product_attribute_combination2val c2v ON c.rowid = c2v.fk_prod_attr
  102. WHERE c2v.fk_prod_combination = ".(int) $fk_combination;
  103. $sql .= $this->db->order('c.rang', 'asc');
  104. $query = $this->db->query($sql);
  105. if (!$query) {
  106. return -1;
  107. }
  108. $return = array();
  109. while ($result = $this->db->fetch_object($query)) {
  110. $tmp = new ProductCombination2ValuePair($this->db);
  111. $tmp->fk_prod_attr_val = $result->fk_prod_attr_val;
  112. $tmp->fk_prod_attr = $result->fk_prod_attr;
  113. $tmp->fk_prod_combination = $result->fk_prod_combination;
  114. $tmp->id = $result->rowid;
  115. $return[] = $tmp;
  116. }
  117. return $return;
  118. }
  119. /**
  120. * Deletes a product combination 2 value pair
  121. *
  122. * @param int $fk_combination Rowid of the combination
  123. * @return int >0 OK <0 KO
  124. */
  125. public function deleteByFkCombination($fk_combination)
  126. {
  127. $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_combination2val WHERE fk_prod_combination = ".(int) $fk_combination;
  128. if ($this->db->query($sql)) {
  129. return 1;
  130. }
  131. return -1;
  132. }
  133. }