diff --git a/app/Config/Constants.php b/app/Config/Constants.php index 48b873e15..3502cae6b 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -97,6 +97,8 @@ define('EVENT_PRIORITY_HIGH', 10); * Global Constants. */ const NEW_ENTRY = -1; +const ACTIVE = 0; +const DELETED = 1; /** * Attribute Related Constants. diff --git a/app/Controllers/Attributes.php b/app/Controllers/Attributes.php index 090d57970..2b4b4c234 100644 --- a/app/Controllers/Attributes.php +++ b/app/Controllers/Attributes.php @@ -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) { diff --git a/app/Helpers/tabular_helper.php b/app/Helpers/tabular_helper.php index f84c427da..66768989c 100644 --- a/app/Helpers/tabular_helper.php +++ b/app/Helpers/tabular_helper.php @@ -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", '', [ 'class' => 'modal-dlg', diff --git a/app/Models/Attribute.php b/app/Models/Attribute.php index 891c6b3e1..4e38533bd 100644 --- a/app/Models/Attribute.php +++ b/app/Models/Attribute.php @@ -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]); } /** diff --git a/app/Views/attributes/form.php b/app/Views/attributes/form.php index 8778dbfff..6766952a5 100644 --- a/app/Views/attributes/form.php +++ b/app/Views/attributes/form.php @@ -13,7 +13,7 @@
- 'attribute_form', 'class' => 'form-horizontal']) //TODO: String Interpolation?> + 'attribute_form', 'class' => 'form-horizontal'])?>