diff --git a/application/controllers/Attributes.php b/application/controllers/Attributes.php index 322af867c..f190c5c3e 100644 --- a/application/controllers/Attributes.php +++ b/application/controllers/Attributes.php @@ -183,4 +183,4 @@ class Attributes extends Secure_Controller } } -} +} \ No newline at end of file diff --git a/application/controllers/Items.php b/application/controllers/Items.php index e55ed851a..f31f8bafb 100644 --- a/application/controllers/Items.php +++ b/application/controllers/Items.php @@ -1115,4 +1115,4 @@ class Items extends Secure_Controller } } } -?> +?> \ No newline at end of file diff --git a/application/models/Attribute.php b/application/models/Attribute.php index 651f4b045..7090f1381 100644 --- a/application/models/Attribute.php +++ b/application/models/Attribute.php @@ -306,9 +306,10 @@ class Attribute extends CI_Model { $success = FALSE; - //From TEXT to DATETIME + //From TEXT if($from_type === TEXT) { + //To DATETIME or DECIMAL if(in_array($to_type, [DATE, DECIMAL], TRUE)) { $field = ($to_type === DATE ? 'attribute_date' : 'attribute_decimal'); @@ -328,27 +329,64 @@ class Attribute extends CI_Model $this->db->trans_complete(); } } - else if(in_array($to_type, [DROPDOWN, CHECKBOX], TRUE)) + //To DROPDOWN or CHECKBOX + else if($to_type === DROPDOWN) { $success = TRUE; } + else if($to_type === CHECKBOX) + { + $checkbox_attribute_values = $this->checkbox_attribute_values($definition_id); + + $this->db->trans_start(); + + $query = 'UPDATE ospos_attribute_values a '; + $query .= 'INNER JOIN ospos_attribute_links b '; + $query .= 'ON a.attribute_id = b.attribute_id '; + $query .= "SET b.attribute_id = IF((a.attribute_value IN('FALSE','0','') OR (a.attribute_value IS NULL)), $checkbox_attribute_values[0], $checkbox_attribute_values[1]) "; + $query .= 'WHERE definition_id = ' . $this->db->escape($definition_id); + $success = $this->db->query($query); + + $this->db->trans_complete(); + } } - //From DROPDOWN to TEXT + //From DROPDOWN else if($from_type === DROPDOWN) { - //From DROPDOWN to TEXT - $this->db->trans_start(); - - $this->db->from('ospos_attribute_links'); - $this->db->where('definition_id',$definition_id); - $this->db->where('item_id', NULL); - $success = $this->db->delete(); - - $this->db->trans_complete(); + //To TEXT + if(in_array($to_type, [TEXT, CHECKBOX], TRUE)) + { + $this->db->trans_start(); + + $this->db->from('ospos_attribute_links'); + $this->db->where('definition_id',$definition_id); + $this->db->where('item_id', NULL); + $success = $this->db->delete(); + + $this->db->trans_complete(); + + //To CHECKBOX + if($to_type === CHECKBOX) + { + $checkbox_attribute_values = $this->checkbox_attribute_values($definition_id); + + $this->db->trans_start(); + + $query = 'UPDATE ospos_attribute_values a '; + $query .= 'INNER JOIN ospos_attribute_links b '; + $query .= 'ON a.attribute_id = b.attribute_id '; + $query .= "SET b.attribute_id = IF((a.attribute_value IN('FALSE','0','') OR (a.attribute_value IS NULL)), $checkbox_attribute_values[0], $checkbox_attribute_values[1]) "; + $query .= 'WHERE definition_id = ' . $this->db->escape($definition_id); + $success = $this->db->query($query); + + $this->db->trans_complete(); + } + + } } - //Any other allowed conversion does not get checked here + //From any other type else { $success = TRUE; @@ -357,6 +395,24 @@ class Attribute extends CI_Model return $success; } + private function checkbox_attribute_values($definition_id) + { + $zero_attribute_id = $this->value_exists('0'); + $one_attribute_id = $this->value_exists('1'); + + if($zero_attribute_id === FALSE) + { + $zero_attribute_id = $this->save_value('0', $definition_id, FALSE, FALSE, CHECKBOX); + } + + if($one_attribute_id === FALSE) + { + $one_attribute_id = $this->save_value('1', $definition_id, FALSE, FALSE, CHECKBOX); + } + + return array($zero_attribute_id, $one_attribute_id); + } + /* Inserts or updates a definition */ @@ -527,7 +583,6 @@ class Attribute extends CI_Model public function save_value($attribute_value, $definition_id, $item_id = FALSE, $attribute_id = FALSE, $definition_type = DROPDOWN) { - //TODO: Right now when you uncheck the checkbox type it should be sending 0 but that's being interpreted funny and erasing the attribute_link altogether for that item. $this->db->trans_start(); //New Attribute @@ -596,4 +651,4 @@ class Attribute extends CI_Model return $this->db->update('attribute_definitions', array('deleted' => 1)); } -} +} \ No newline at end of file diff --git a/application/views/attributes/form.php b/application/views/attributes/form.php index 349048ce8..be4606cce 100644 --- a/application/views/attributes/form.php +++ b/application/views/attributes/form.php @@ -221,4 +221,4 @@ $(document).ready(function() } }, form_support.error)); }); - + \ No newline at end of file