Add attribute type conversion, error checking.

This commit is contained in:
objecttothis
2019-02-15 17:03:31 +04:00
parent 9e2f9f1322
commit e66c7bad24
2 changed files with 78 additions and 7 deletions

View File

@@ -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));
}
}
}

View File

@@ -19,7 +19,7 @@
<div class="form-group form-group-sm">
<?php echo form_label($this->lang->line('attributes_definition_type'), 'definition_type', array('class'=>'control-label col-xs-3')); ?>
<div class='col-xs-8'>
<?php echo form_dropdown('definition_type', DEFINITION_TYPES, array_search($definition_info->definition_type, DEFINITION_TYPES), 'id="definition_type" class="form-control" ' . ($definition_id != -1 ? 'disabled="disabled"' : ''));?>
<?php echo form_dropdown('definition_type', DEFINITION_TYPES, array_search($definition_info->definition_type, DEFINITION_TYPES), 'id="definition_type" class="form-control"');?>
</div>
</div>
@@ -69,10 +69,31 @@ $(document).ready(function()
var definition_id = <?php echo $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);
};