ProductCombination2ValuePair.class.php 4.0 KB

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