diff --git a/application/models/Attribute.php b/application/models/Attribute.php index c2edfe9b7..5fdc9ecfc 100644 --- a/application/models/Attribute.php +++ b/application/models/Attribute.php @@ -244,6 +244,42 @@ class Attribute extends CI_Model return $this->search($search)->num_rows(); } + public function convert_definition_type($definition_id,$from_type,$to_type) + { + if($from_type === TEXT) + { + //From TEXT to DATETIME + if($to_type === DATETIME) + { + $this->db->trans_start(); + + $query = 'UPDATE ospos_attribute_values '; + $query .= 'INNER JOIN ospos_attribute_links '; + $query .= 'ON ospos_attribute_values.attribute_id = ospos_attribute_links.attribute_id '; + $query .= 'SET attribute_datetime = attribute_value, '; + $query .= 'attribute_value = NULL '; + $query .= 'WHERE definition_id = '.$definition_id; + $this->db->query($query); + + $this->db->trans_complete(); + } + } + 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(); + } + + return $success; + } + /* Inserts or updates a definition */ @@ -251,23 +287,37 @@ class Attribute extends CI_Model { //Run these queries as a transaction, we want to make sure we do all or nothing $this->db->trans_start(); - + + //Definition doesn't exist if($definition_id === -1 || !$this->exists($definition_id)) { $success = $this->db->insert('attribute_definitions', $definition_data); $definition_data['definition_id'] = $this->db->insert_id(); } + //Definition already exists else { + $this->db->select('definition_type'); + $this->db->from('attribute_definitions'); + $this->db->where('definition_id',$definition_id); + + $from_definition_type = $this->db->get()->row()->definition_type; + $to_definition_type = $definition_data['definition_type']; + + if($from_definition_type !== $to_definition_type) + { + $this->convert_definition_type($definition_id,$from_definition_type,$to_definition_type); + } + $this->db->where('definition_id', $definition_id); $success = $this->db->update('attribute_definitions', $definition_data); $definition_data['definition_id'] = $definition_id; } - + $this->db->trans_complete(); - - $success &= $this->db->trans_status(); - + + $success &= $this->db->trans_status(); + return $success; } @@ -463,4 +513,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 8cda67c92..d55ec9fc5 100644 --- a/application/views/attributes/form.php +++ b/application/views/attributes/form.php @@ -19,7 +19,7 @@
lang->line('attributes_definition_type'), 'definition_type', array('class'=>'control-label col-xs-3')); ?>
- definition_type, DEFINITION_TYPES), 'id="definition_type" class="form-control" ' . ($definition_id != -1 ? 'disabled="disabled"' : ''));?> + definition_type, DEFINITION_TYPES), 'id="definition_type" class="form-control"');?>
@@ -69,10 +69,31 @@ $(document).ready(function() var definition_id = ; var is_new = definition_id == -1; + var disable_definition_types = function() + { + var definition_type = $("#definition_type option:selected").text(); + + if(definition_type == "DATETIME" || definition_type == "GROUP") + { + $('#definition_type').prop("disabled",true); + } + else if(definition_type == "DROPDOWN") + { + $("#definition_type option:contains('GROUP')").hide(); + $("#definition_type option:contains('DATETIME')").hide(); + } + else + { + $("#definition_type option:contains('GROUP')").hide(); + } + } + disable_definition_types(); + var show_hide_fields = function(event) { var is_dropdown = $('#definition_type').val() !== '1'; var is_no_group = $('#definition_type').val() !== '0'; + $('#definition_value, #definition_list_group').parents('.form-group').toggleClass('hidden', is_dropdown); $('#definition_flags').parents('.form-group').toggleClass('hidden', !is_no_group); };