mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-07-12 16:07:26 -04:00
For #3730. This fixes the inability to add a TEXT field to an item.
This commit is contained in:
@@ -73,7 +73,7 @@ class Attributes extends Secure_Controller
|
||||
public function postDelete_attribute_value(): void
|
||||
{
|
||||
$success = $this->attribute->delete_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)
|
||||
);
|
||||
|
||||
@@ -153,9 +153,9 @@ class Attributes extends Secure_Controller
|
||||
* @param int $definition_id
|
||||
* @return void
|
||||
*/
|
||||
public function suggest_attribute(int $definition_id): void
|
||||
public function getSuggestAttribute(int $definition_id): void
|
||||
{
|
||||
$suggestions = $this->attribute->get_suggestions($definition_id, $this->request->getVar('term', FILTER_SANITIZE_STRING));
|
||||
$suggestions = $this->attribute->get_suggestions($definition_id, html_entity_decode($this->request->getVar('term')));
|
||||
|
||||
echo json_encode($suggestions);
|
||||
}
|
||||
|
||||
@@ -491,7 +491,41 @@ class Items extends Secure_Controller
|
||||
public function getAttributes(int $item_id = NEW_ENTRY): void
|
||||
{
|
||||
$data['item_id'] = $item_id;
|
||||
$definition_ids = json_decode($this->request->getPost('definition_ids', FILTER_SANITIZE_STRING), TRUE);
|
||||
$definition_ids = json_decode($this->request->getGet('definition_ids'), TRUE);
|
||||
$data['definition_values'] = $this->attribute->get_attributes_by_item($item_id) + $this->attribute->get_values_by_definitions($definition_ids);
|
||||
$data['definition_names'] = $this->attribute->get_definition_names();
|
||||
|
||||
foreach($data['definition_values'] as $definition_id => $definition_value)
|
||||
{
|
||||
$attribute_value = $this->attribute->get_attribute_value($item_id, $definition_id);
|
||||
$attribute_id = (empty($attribute_value) || empty($attribute_value->attribute_id)) ? NULL : $attribute_value->attribute_id;
|
||||
$values = &$data['definition_values'][$definition_id];
|
||||
$values['attribute_id'] = $attribute_id;
|
||||
$values['attribute_value'] = $attribute_value;
|
||||
$values['selected_value'] = '';
|
||||
|
||||
if ($definition_value['definition_type'] === DROPDOWN)
|
||||
{
|
||||
$values['values'] = $this->attribute->get_definition_values($definition_id);
|
||||
$link_value = $this->attribute->get_link_value($item_id, $definition_id);
|
||||
$values['selected_value'] = (empty($link_value)) ? '' : $link_value->attribute_id;
|
||||
}
|
||||
|
||||
if (!empty($definition_ids[$definition_id]))
|
||||
{
|
||||
$values['selected_value'] = $definition_ids[$definition_id];
|
||||
}
|
||||
|
||||
unset($data['definition_names'][$definition_id]);
|
||||
}
|
||||
|
||||
echo view('attributes/item', $data);
|
||||
}
|
||||
|
||||
public function postAttributes(int $item_id = NEW_ENTRY): void
|
||||
{
|
||||
$data['item_id'] = $item_id;
|
||||
$definition_ids = json_decode($this->request->getPost('definition_ids'), TRUE);
|
||||
$data['definition_values'] = $this->attribute->get_attributes_by_item($item_id) + $this->attribute->get_values_by_definitions($definition_ids);
|
||||
$data['definition_names'] = $this->attribute->get_definition_names();
|
||||
|
||||
@@ -695,21 +729,21 @@ class Items extends Secure_Controller
|
||||
}
|
||||
|
||||
// Save item attributes
|
||||
$attribute_links = $this->request->getPost('attribute_links') !== NULL ? $this->request->getPost('attribute_links', FILTER_SANITIZE_NUMBER_INT) : [];
|
||||
$attribute_links = $this->request->getPost('attribute_links') !== NULL ? $this->request->getPost('attribute_links') : [];
|
||||
$attribute_ids = $this->request->getPost('attribute_ids', FILTER_SANITIZE_NUMBER_INT);
|
||||
|
||||
$this->attribute->delete_link($item_id);
|
||||
|
||||
foreach($attribute_links as $definition_id => $attribute_id)
|
||||
foreach($attribute_links as $definition_id => $attribute_value)
|
||||
{
|
||||
$definition_type = $this->attribute->get_info($definition_id)->definition_type;
|
||||
|
||||
if($definition_type !== DROPDOWN)
|
||||
{
|
||||
$attribute_id = $this->attribute->save_value($attribute_id, $definition_id, $item_id, $attribute_ids[$definition_id], $definition_type);
|
||||
$attribute_id = $this->attribute->save_value($attribute_value, $definition_id, $item_id, $attribute_ids[$definition_id], $definition_type);
|
||||
}
|
||||
|
||||
$this->attribute->save_link($item_id, $definition_id, $attribute_id);
|
||||
$this->attribute->save_link($item_id, $definition_id, intval($attribute_ids[$definition_id]));
|
||||
}
|
||||
|
||||
if($success && $upload_success)
|
||||
|
||||
@@ -622,7 +622,7 @@ class Attribute extends Model
|
||||
return $builder->get();
|
||||
}
|
||||
|
||||
public function get_attribute_value(int $item_id, int $definition_id): object
|
||||
public function get_attribute_value(int $item_id, int $definition_id): ?object
|
||||
{
|
||||
$builder = $this->db->table('attribute_values');
|
||||
$builder->join('attribute_links', 'attribute_links.attribute_id = attribute_values.attribute_id');
|
||||
@@ -630,8 +630,43 @@ class Attribute extends Model
|
||||
$builder->where('sale_id', null);
|
||||
$builder->where('receiving_id', null);
|
||||
$builder->where('definition_id', $definition_id);
|
||||
$query = $builder->get();
|
||||
|
||||
return $builder->get()->getRowObject();
|
||||
if($query->getNumRows() == 1)
|
||||
{
|
||||
return $query->getRow();
|
||||
}
|
||||
|
||||
return $this->getEmptyObject('attribute_values');
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes an empty object based on database definitions
|
||||
* @param string $table_name
|
||||
* @return object
|
||||
*/
|
||||
private function getEmptyObject(string $table_name): object
|
||||
{
|
||||
// Return an empty base parent object, as $item_id is NOT an item
|
||||
$empty_obj = new stdClass();
|
||||
|
||||
// Iterate through field definitions to determine how the fields should be initialized
|
||||
|
||||
foreach($this->db->getFieldData($table_name) as $field) {
|
||||
|
||||
$field_name = $field->name;
|
||||
|
||||
if(in_array($field->type, array('int', 'tinyint', 'decimal')))
|
||||
{
|
||||
$empty_obj->$field_name = ($field->primary_key == 1) ? NEW_ENTRY : 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
$empty_obj->$field_name = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return $empty_obj;
|
||||
}
|
||||
|
||||
public function get_attribute_values(int $item_id): array //TODO: Is this function used anywhere in the code?
|
||||
|
||||
@@ -100,7 +100,7 @@ foreach($definition_values as $definition_id => $definition_value)
|
||||
$("input[name='attribute_ids[" + definition_id + "]']").val('');
|
||||
}).autocomplete({
|
||||
source: function(request, response) {
|
||||
$.get('<?php echo 'attributes/suggest_attribute/' ?>' + this.element.data('definition-id') + '?term=' + request.term, function(data) {
|
||||
$.get('<?php echo 'attributes/suggestAttribute/' ?>' + this.element.data('definition-id') + '?term=' + request.term, function(data) {
|
||||
return response(data);
|
||||
}, 'json');
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user