/inc/app/siteshop/lib/CustomProduct.php

https://github.com/durand54/sitellite · PHP · 201 lines · 164 code · 35 blank · 2 comment · 32 complexity · 2feb3f25b5c115df9d489ffda7fe49dc MD5 · raw file

  1. <?php
  2. loader_import ('saf.Database.Generic');
  3. class CustomProduct extends Generic {
  4. function getThumbnail ($id = false) {
  5. if (! $id) {
  6. $id = $this->val ('id');
  7. }
  8. if (@file_exists ('inc/app/siteshop/data/' . $id . '-1.jpg')) {
  9. return site_prefix () . '/inc/app/siteshop/data/' . $id . '-1.jpg';
  10. }
  11. return site_prefix () . '/' . appconf ('default_thumbnail');
  12. }
  13. function getImages ($id = false) {
  14. if (! $id) {
  15. $id = $this->val ('id');
  16. }
  17. $images = array ();
  18. $i = 1;
  19. while (@file_exists ('inc/app/siteshop/data/' . $id . '-' . $i . '.jpg')) {
  20. $images[] = site_prefix () . '/inc/app/siteshop/data/' . $id . '-' . $i . '.jpg';
  21. $i++;
  22. }
  23. if (count ($images) == 0) {
  24. $images[] = site_prefix () . '/' . appconf ('default_thumbnail');
  25. }
  26. return $images;
  27. }
  28. function selectAll () {
  29. return db_pairs (
  30. 'select id, concat(name, \' ($\', price, \')\') from siteshop_product order by name asc'
  31. );
  32. }
  33. function featured ($n) {
  34. if (session_admin ()) {
  35. $sql = session_allowed_sql ();
  36. } else {
  37. $sql = session_approved_sql ();
  38. }
  39. return db_fetch_array (
  40. 'select * from siteshop_product
  41. where availability != 8 and (quantity = -1 or quantity > 0)
  42. and ' . $sql . ' order by weight desc
  43. limit ' . $n
  44. );
  45. }
  46. function getPrice ($id = false) {
  47. if (! $id) {
  48. $id = $this->val ('id');
  49. }
  50. $p = new Product ($id);
  51. $price = $p->val ('price');
  52. // check for sale
  53. $s = new Sale ();
  54. if ($s->loadCurrent ()) {
  55. $sale_price = db_shift (
  56. 'select sale_price from siteshop_sale_product where sale_id = ? and product_id = ?',
  57. $s->val ('id'),
  58. $id
  59. );
  60. if ($sale_price) {
  61. $price = $sale_price;
  62. }
  63. }
  64. return $price;
  65. }
  66. function updateQuantity ($id = false, $qty = 1) {
  67. if (! $id) {
  68. $id = $this->val ('id');
  69. }
  70. $p = new Product ($id);
  71. if ($p->val ('quantity') == -1) {
  72. return true;
  73. } elseif ($p->val ('quantity') == 0) {
  74. return false;
  75. }
  76. $p->set ('quantity', $p->val ('quantity') - $qty);
  77. $p->save ();
  78. return true;
  79. }
  80. function taxable ($id = false) {
  81. if ($id) {
  82. $p = new Product ($id);
  83. if ($p->val ('taxable') == 'yes') {
  84. return true;
  85. }
  86. return false;
  87. }
  88. if ($this->val ('taxable') == 'yes') {
  89. return true;
  90. }
  91. return false;
  92. }
  93. function getAllOptions () {
  94. $options = db_fetch_array ('
  95. select distinct o.id, o.name, po.available, o.type from
  96. (select * from siteshop_option) as o
  97. left outer join
  98. (select * from siteshop_product_option where product_id = ?) as po
  99. on
  100. o.id = po.option_id order by o.type asc, o.weight, o.name asc',
  101. $this->val('id')
  102. );
  103. return $options;
  104. }
  105. function setOptionC ($option_id, $available = 'no') {
  106. if (empty ($option_id)) {
  107. return false;
  108. }
  109. if (! is_numeric ($option_id)) {
  110. return false;
  111. }
  112. db_execute ('delete from siteshop_product_option where product_id = ? and option_id = ?', $this->val ('id'), $option_id);
  113. if (empty ($available)) {
  114. return true;
  115. }
  116. db_execute ('insert into siteshop_product_option (product_id, option_id, available) values (?, ?, ?)', $this->val ('id'), $option_id, $available);
  117. //db_execute ('update siteshop_product_option set available = ? where product_id = ? and option_id = ?', $available, $this->val('id'), $option_id);
  118. return true;
  119. }
  120. function getVisibleOptions () {
  121. $options = db_fetch_array (
  122. 'select * from siteshop_product_option as po, siteshop_option as o where po.product_id = ? and po.option_id = o.id order by o.type asc, o.weight, o.name asc',
  123. $this->val ('id')
  124. );
  125. foreach ($options as $k => $o) {
  126. if (file_exists ('inc/app/siteshop/pix/options/' . $o->id . '.jpg')) {
  127. $options[$k]->has_thumbnail = 'yes';
  128. $options[$k]->thumbnail = 'inc/app/siteshop/pix/options/' . $o->id . '.jpg';
  129. } else {
  130. $options[$k]->has_thumbnail = 'no';
  131. }
  132. }
  133. return $options;
  134. }
  135. function getAddToCartForm () {
  136. $beginning = template_simple ('addtocartform1.spt', array ('id' => $this->val ('id')));
  137. $middle = '';
  138. $end = '';
  139. $options = $this->getVisibleOptions ();
  140. if (empty($options)) {
  141. return template_simple ('addtocartform_nooptions.spt', array ('id' => $this->val ('id')));
  142. }
  143. $previous_type = $options[0]->type;
  144. $data = array ();
  145. $first_enabled_found = false;
  146. foreach ($options as $o) {
  147. if ($o->type != $previous_type) {
  148. $middle .= template_simple ('addtocartform2.spt', array ('options' => $data, 'type' => $previous_type));
  149. $previous_type = $o->type;
  150. $first_enabled_found = false;
  151. $data = array ();
  152. $data[$o->id] = $o;
  153. } else {
  154. $data[$o->id] = $o;
  155. }
  156. if ($o->available == 'yes' && ! $first_enabled_found) {
  157. $data[$o->id]->selected = true;
  158. $first_enabled_found = true;
  159. }
  160. }
  161. $middle .= template_simple ('addtocartform2.spt', array ('options' => $data, 'type' => $previous_type));
  162. $end .= template_simple ('addtocartform3.spt');
  163. return $beginning . $middle . $end;
  164. }
  165. }
  166. ?>