For #3730. This corrects errors I encountered doing attribute configuration.

This commit is contained in:
Steve Ireland
2023-04-09 15:43:54 -04:00
parent fa38042298
commit 47e25013bf
5 changed files with 43 additions and 39 deletions

View File

@@ -97,6 +97,8 @@ define('EVENT_PRIORITY_HIGH', 10);
* Global Constants.
*/
const NEW_ENTRY = -1;
const ACTIVE = 0;
const DELETED = 1;
/**
* Attribute Related Constants.

View File

@@ -29,7 +29,7 @@ class Attributes extends Secure_Controller
}
/**
* Returns customer table data rows. This will be called with AJAX.
* Returns attribute table data rows. This will be called with AJAX.
*/
public function getSearch(): void
{
@@ -43,10 +43,10 @@ class Attributes extends Secure_Controller
$total_rows = $this->attribute->get_found_rows($search);
$data_rows = [];
foreach($attributes->getResult() as $attribute)
foreach($attributes->getResult() as $attribute_row)
{
$attribute->definition_flags = $this->get_attributes($attribute->definition_flags);
$data_rows[] = get_attribute_definition_data_row($attribute);
$attribute_row->definition_flags = $this->get_attributes($attribute_row->definition_flags);
$data_rows[] = get_attribute_definition_data_row($attribute_row);
}
echo json_encode(['total' => $total_rows, 'rows' => $data_rows]);
@@ -55,10 +55,10 @@ class Attributes extends Secure_Controller
/**
* @return void
*/
public function postSave_attribute_value(): void
public function postSaveAttributeValue(): void
{
$success = $this->attribute->save_value(
$this->request->getPost('attribute_value', FILTER_SANITIZE_STRING),
html_entity_decode($this->request->getPost('attribute_value')),
$this->request->getPost('definition_id', FILTER_SANITIZE_NUMBER_INT),
$this->request->getPost('item_id', FILTER_SANITIZE_NUMBER_INT),
$this->request->getPost('attribute_id', FILTER_SANITIZE_NUMBER_INT)
@@ -84,7 +84,7 @@ class Attributes extends Secure_Controller
* @param int $definition_id
* @return void
*/
public function postSave_definition(int $definition_id = NO_DEFINITION_ID): void
public function postSaveDefinition(int $definition_id = NO_DEFINITION_ID): void
{
$definition_flags = 0;
@@ -97,15 +97,15 @@ class Attributes extends Secure_Controller
//Save definition data
$definition_data = [
'definition_name' => $this->request->getPost('definition_name', FILTER_SANITIZE_STRING),
'definition_unit' => $this->request->getPost('definition_unit') != '' ? $this->request->getPost('definition_unit', FILTER_SANITIZE_STRING) : NULL,
'definition_name' => $this->request->getPost('definition_name'),
'definition_unit' => $this->request->getPost('definition_unit') != '' ? $this->request->getPost('definition_unit') : NULL,
'definition_flags' => $definition_flags,
'definition_fk' => $this->request->getPost('definition_group') != '' ? $this->request->getPost('definition_group', FILTER_SANITIZE_STRING) : NULL
'definition_fk' => $this->request->getPost('definition_group') != '' ? $this->request->getPost('definition_group') : NULL
];
if ($this->request->getPost('definition_type') != NULL)
{
$definition_data['definition_type'] = DEFINITION_TYPES[$this->request->getPost('definition_type', FILTER_SANITIZE_STRING)];
$definition_data['definition_type'] = DEFINITION_TYPES[$this->request->getPost('definition_type')];
}
$definition_name = $definition_data['definition_name'];
@@ -113,9 +113,9 @@ class Attributes extends Secure_Controller
if($this->attribute->save_definition($definition_data, $definition_id))
{
//New definition
if($definition_id == 0)
if($definition_id == NO_DEFINITION_ID)
{
$definition_values = json_decode($this->request->getPost('definition_values', FILTER_SANITIZE_STRING));
$definition_values = json_decode(html_entity_decode($this->request->getPost('definition_values')));
foreach($definition_values as $definition_value)
{

View File

@@ -679,13 +679,13 @@ function get_attribute_definition_manage_table_headers(): string
return transform_headers($headers);
}
function get_attribute_definition_data_row(object $attribute): array
function get_attribute_definition_data_row(object $attribute_row): array
{
$attribute = model('Attribute');
$controller = get_controller();
if(count($attribute->definition_flags) == 0) //TODO: === ?
if(count($attribute->get_definition_flags()) == 0)
{
$definition_flags = lang('Common.none_selected_text');
}
@@ -695,17 +695,17 @@ function get_attribute_definition_data_row(object $attribute): array
}
else
{
$definition_flags = implode(', ', $attribute->definition_flags);
$definition_flags = implode(', ', $attribute->get_definition_flags());
}
return [
'definition_id' => $attribute->definition_id,
'definition_name' => $attribute->definition_name,
'definition_type' => $attribute->definition_type,
'definition_group' => $attribute->definition_group,
'definition_id' => $attribute_row->definition_id,
'definition_name' => $attribute_row->definition_name,
'definition_type' => $attribute_row->definition_type,
'definition_group' => $attribute_row->definition_group,
'definition_flags' => $definition_flags,
'edit' => anchor(
"$controller/view/$attribute->definition_id",
"$controller/view/$attribute_row->definition_id",
'<span class="glyphicon glyphicon-edit"></span>',
[
'class' => 'modal-dlg',

View File

@@ -85,9 +85,10 @@ class Attribute extends Model
/*
* Determines if a given attribute_value exists in the attribute_values table and returns the attribute_id if it does
*/
public function value_exists($attribute_value, string $definition_type = TEXT): bool
public function value_exists($attribute_value, string $definition_type = TEXT)
{
$config = config('OSPOS')->settings;
switch($definition_type)
{
case DATE:
@@ -468,8 +469,6 @@ class Attribute extends Model
{
$this->db->transStart();
$builder = $this->db->table('attribute_definitions');
//Definition doesn't exist
if($definition_id === NO_DEFINITION_ID || !$this->exists($definition_id))
{
@@ -479,6 +478,7 @@ class Attribute extends Model
}
else
{
$builder = $this->db->table('attribute_definitions');
$success = $builder->insert($definition_data);
$definition_data['definition_id'] = $this->db->insertID();
}
@@ -487,11 +487,13 @@ class Attribute extends Model
//Definition already exists
else
{
//Get current definition type and name
$builder = $this->db->table('attribute_definitions');
$builder->select('definition_type');
$builder->where('definition_id', $definition_id);
$builder->where('deleted', ACTIVE);
$query = $builder->get();
$row = $query->getRow();
$row = $builder->get('attribute_definitions')->getRow();
$from_definition_type = $row->definition_type;
$to_definition_type = $definition_data['definition_type'];
@@ -599,7 +601,7 @@ class Attribute extends Model
$builder->join('attribute_values', 'attribute_values.attribute_id = attribute_links.attribute_id');
$builder->join('attribute_definitions', 'attribute_definitions.definition_id = attribute_links.definition_id');
$builder->where('definition_type <>', GROUP);
$builder->where('deleted', 0);
$builder->where('deleted', ACTIVE);
$builder->where('item_id', $item_id);
if(!empty($id))
@@ -664,7 +666,7 @@ class Attribute extends Model
$builder->join('attribute_links', 'attribute_links.definition_id = definition.definition_id');
$builder->join('attribute_values', 'attribute_values.attribute_id = attribute_links.attribute_id');
$builder->like('attribute_value', $term);
$builder->where('deleted', 0);
$builder->where('deleted', ACTIVE);
$builder->where('definition.definition_id', $definition_id);
$builder->orderBy('attribute_value','ASC');
@@ -679,11 +681,11 @@ class Attribute extends Model
public function save_value(string $attribute_value, int $definition_id, $item_id = FALSE, $attribute_id = FALSE, string $definition_type = DROPDOWN): int
{
$this->db->transStart();
$config = config('OSPOS')->settings;
$locale_date_format = $config['dateformat'];
$this->db->transStart();
//New Attribute
if(empty($attribute_id) || empty($item_id))
{
@@ -769,15 +771,15 @@ class Attribute extends Model
$builder = $this->db->table('attribute_definitions');
$builder->where('definition_id', $definition_id);
return $builder->update(['deleted' => 1]);
return $builder->update(['deleted' => DELETED]);
}
public function delete_definition_list(string $definition_ids): bool
public function delete_definition_list(array $definition_ids): bool
{
$builder = $this->db->table('attribute_definitions');
$builder->whereIn('definition_id', $definition_ids);
return $builder->update(['deleted' => 1]);
return $builder->update(['deleted' => DELETED]);
}
/**
@@ -841,7 +843,7 @@ class Attribute extends Model
$builder = $this->db->table('attribute_definitions');
$builder->where('definition_id', $definition_id);
return $builder->update(['deleted' => 0]);
return $builder->update(['deleted' => ACTIVE]);
}
/**

View File

@@ -13,7 +13,7 @@
<ul id="error_message_box" class="error_message_box"></ul>
<?php echo form_open('attributes/save_definition/' . esc($definition_id), ['id' => 'attribute_form', 'class' => 'form-horizontal']) //TODO: String Interpolation?>
<?php echo form_open("attributes/saveDefinition/$definition_id", ['id' => 'attribute_form', 'class' => 'form-horizontal'])?>
<fieldset id="attribute_basic_info">
<div class="form-group form-group-sm">
@@ -23,7 +23,7 @@
'name' => 'definition_name',
'id' => 'definition_name',
'class' => 'form-control input-sm',
'value'=>esc($definition_info->definition_name)
'value'=>$definition_info->definition_name
]
) ?>
</div>
@@ -75,7 +75,7 @@
<div class="input-group">
<?php echo form_input ([
'name' => 'definition_unit',
'value' => esc($definition_info->definition_unit),
'value' => $definition_info->definition_unit,
'class' => 'form-control input-sm',
'id' => 'definition_unit'
]) ?>
@@ -210,7 +210,7 @@ $(document).ready(function()
}
else
{
$.post('<?php echo "attributes/save_attribute_value/" ?>', {definition_id: definition_id, attribute_value: value});
$.post('<?php echo "attributes/saveAttributeValue/" ?>', {definition_id: definition_id, attribute_value: value});
}
}
@@ -228,7 +228,7 @@ $(document).ready(function()
}
});
var definition_values = <?php echo json_encode(array_values(esc($definition_values))) ?>;
var definition_values = <?php echo json_encode(array_values($definition_values)) ?>;
$.each(definition_values, function(index, element) {
add_attribute_value(element);
});