/gocart/models/option_model.php

https://bitbucket.org/adrianricardo/newstatus · PHP · 335 lines · 236 code · 54 blank · 45 comment · 32 complexity · 620feedf8806260ac2667bac9ce38a8a MD5 · raw file

  1. <?php
  2. Class Option_model extends CI_Model
  3. {
  4. function __construct()
  5. {
  6. parent::__construct();
  7. $this->load->helper('formatting_helper');
  8. }
  9. /********************************************************************
  10. Options Management
  11. ********************************************************************/
  12. //get product options
  13. function get_all_options($product_id)
  14. {
  15. $this->db->where('product_id', $product_id);
  16. $this->db->order_by('id', 'DESC');
  17. $result = $this->db->get('options');
  18. $return = array();
  19. foreach($result->result() as $option)
  20. {
  21. $option->values = $this->get_option_values($option->id);
  22. $return[] = $option;
  23. }
  24. return $return;
  25. }
  26. function get_option($id, $as_array = false)
  27. {
  28. $result = $this->db->get_where('options', array('id'=>$id));
  29. $data = $result->row();
  30. if($as_array)
  31. {
  32. $data->values = $this->get_option_values($id, true);
  33. }
  34. else
  35. {
  36. $data->values = $this->get_option_values($id);
  37. }
  38. return $data;
  39. }
  40. function get_option_id($product_id)
  41. {
  42. $this->db->where('product_id', $product_id);
  43. $this->db->limit(1);
  44. $result = $this->db->get('options');
  45. return $result->row();
  46. }
  47. function save_option($option, $values)
  48. {
  49. if(isset($option['id']))
  50. {
  51. $this->db->where('id', $option['id']);
  52. $this->db->update('options', $option);
  53. $id = $option['id'];
  54. //eliminate existing options
  55. $this->delete_option_values($id);
  56. }
  57. else
  58. {
  59. $this->db->insert('options', $option);
  60. $id = $this->db->insert_id();
  61. }
  62. //add options to the database
  63. $sequence = 0;
  64. foreach($values as $value)
  65. {
  66. $value['option_id'] = $id;
  67. $value['sequence'] = $sequence;
  68. $value['weight'] = floatval($value['weight']);
  69. $value['price'] = floatval($value['price']);
  70. $sequence++;
  71. $this->db->insert('option_values', $value);
  72. }
  73. return $id;
  74. }
  75. // for product level options
  76. function clear_options($product_id)
  77. {
  78. // get the list of options for this product
  79. $list = $this->db->where('product_id', $product_id)->get('options')->result();
  80. foreach($list as $opt)
  81. {
  82. $this->delete_option($opt->id);
  83. }
  84. }
  85. // also deletes child records in option_values and product_option
  86. function delete_option($id)
  87. {
  88. $this->db->where('id', $id);
  89. $this->db->delete('options');
  90. $this->delete_option_values($id);
  91. }
  92. /********************************************************************
  93. Option values Management
  94. ********************************************************************/
  95. function get_option_values($option_id)
  96. {
  97. $this->db->where('option_id',$option_id);
  98. $this->db->order_by('sequence', 'ASC');
  99. return $this->db->get('option_values')->result();
  100. }
  101. function get_value($value_id)
  102. {
  103. $this->db->where('id', $value_id);
  104. return $this->db->get('option_values')->row();
  105. }
  106. function delete_option_values($id)
  107. {
  108. $this->db->where('option_id', $id);
  109. $this->db->delete('option_values');
  110. }
  111. function get_option_value($option_id,$option_value,$id = False)
  112. {
  113. if ($id != False){
  114. $this->db->where('id', $id);
  115. } else{
  116. $this->db->where('option_id', $option_id);
  117. $this->db->where('value', $option_value);
  118. }
  119. return $this->db->get('option_values')->row();
  120. }
  121. function update_option_qty($option_quantity)
  122. {
  123. if(isset($option_quantity['id']))
  124. {
  125. $this->db->where('id', $option_quantity['id']);
  126. $this->db->update('option_values', $option_quantity);
  127. }
  128. }
  129. function is_sizes($id){
  130. $this->db->where('id', $id);
  131. $result = $this->db->get('options');
  132. if ($result->num_rows() > 0) {
  133. if($result->row()->type == "sizes"){
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. /********************************************************************
  140. Product options Management
  141. ********************************************************************/
  142. function get_product_options($product_id)
  143. {
  144. $this->db->where('product_id',$product_id);
  145. $this->db->order_by('sequence', 'ASC');
  146. $result = $this->db->get('options');
  147. $return = array();
  148. foreach($result->result() as $option)
  149. {
  150. $option->values = $this->get_option_values($option->id);
  151. $return[] = $option;
  152. }
  153. return $return;
  154. }
  155. /***************************************************
  156. Options Live Use Functionality
  157. ****************************************************/
  158. function validate_product_options(&$product, $post_options)
  159. {
  160. if( ! isset($product['id'])) return false;
  161. // set up to catch option errors
  162. $error = false;
  163. $msg = 'The following options were not selected and are required:<br/>';
  164. // Get the list of options for the product
  165. // We will check the submitted options against this to make sure required options were selected
  166. $db_options = $this->get_product_options($product['id']);
  167. // Loop through the options from the database
  168. foreach($db_options as $option)
  169. {
  170. // Use the product option to see if we have matching data from the product form
  171. $option_value = @$post_options[$option->id];
  172. // are we missing any required values?
  173. if((int) $option->required && empty($option_value))
  174. {
  175. // Set our error flag and add to the user message
  176. // then continue processing the other options to built a full list of missing requirements
  177. $error = true;
  178. $msg .= "- ". $option->name .'<br/>';
  179. continue; // don't bother processing this particular option any further
  180. }
  181. // process checklist items specially
  182. // multi-valued
  183. if($option->type == 'checklist')
  184. {
  185. $opts = array();
  186. // tally our adjustments
  187. //check to make sure this is an array before looping
  188. if(is_array($option_value))
  189. {
  190. foreach($option_value as $check_value)
  191. {
  192. //$val = $this->get_value($check_value);
  193. foreach($option->values as $check_match)
  194. {
  195. if($check_match->id == $check_value)
  196. {
  197. $val = $check_match;
  198. }
  199. }
  200. $price = '';
  201. if($val->price > 0)
  202. {
  203. $price = ' ('.format_currency($val->price).')';
  204. }
  205. $product['price'] = $product['price'] + $val->price;
  206. $product['weight'] = $product['weight'] + $val->weight;
  207. array_push($opts, $val->value.$price);
  208. }
  209. }
  210. // If only one option was checked, add it as a single value
  211. if(count($opts)==1)
  212. {
  213. $product['options'][$option->name] = $opts[0];
  214. }
  215. // otherwise, add it as an array of values
  216. else if(!empty($opts))
  217. {
  218. $product['options'][$option->name] = $opts;
  219. }
  220. }
  221. // handle text fields
  222. else if($option->type == 'textfield' || $option->type == 'textarea')
  223. {
  224. //get the value and weight of the textfield/textarea and add it!
  225. if($option_value)
  226. {
  227. //get the potential price and weight of this field
  228. $val = $option->values[0];
  229. //add the weight and price to the product
  230. $product['price'] = $product['price'] + $val->price;
  231. $product['weight'] = $product['weight'] + $val->weight;
  232. //if there is additional cost, add it to the item description
  233. $price = '';
  234. if($val->price > 0)
  235. {
  236. $price = ' ('.format_currency($val->price).')';
  237. }
  238. $product['options'][$option->name] = $option_value;
  239. }
  240. }
  241. // handle radios and droplists
  242. else
  243. {
  244. //make sure that blank options aren't used
  245. if ($option_value)
  246. {
  247. // we only need the one selected
  248. //$val = $this->get_value($option_value);
  249. foreach($option->values as $check_match)
  250. {
  251. if($check_match->id == $option_value)
  252. {
  253. $val = $check_match;
  254. }
  255. }
  256. //adjust product price and weight
  257. $product['price'] = $product['price'] + $val->price;
  258. $product['weight'] = $product['weight'] + $val->weight;
  259. $price = '';
  260. if($val->price > 0)
  261. {
  262. $price = ' ('.format_currency($val->price).')';
  263. }
  264. //add the option to the options
  265. //$product['options'][$option->name] = $val->name.$price.$weight;
  266. $product['options'][$option->name] = $val->name;
  267. }
  268. }
  269. }
  270. if($error)
  271. {
  272. return( array( 'validated' => false,
  273. 'message' => $msg
  274. ));
  275. }
  276. else
  277. {
  278. return( array( 'validated' => true ));
  279. }
  280. }
  281. }