From e5dcdd59701d08b64dd6f3fde6fa578d0705be7a Mon Sep 17 00:00:00 2001 From: objecttothis Date: Mon, 6 Nov 2023 15:05:06 +0400 Subject: [PATCH] Attributes queries fixes - Minor formatting fixes - Adding back bitwise equals into query using RawSql() - Corrected GET method to POST - Removed if statement causing no attribute values - Removed param in get() from CI3 - Changed setAttribute to setTextAttribute - Replaced NULL constant with null keyword PSR-2,12 - Replaced TRUE/FALSE constants with true/false keywords PSR-2,12 - explicit cast to get rid of deprecation warning --- app/Controllers/Items.php | 14 +++----------- app/Controllers/Secure_Controller.php | 26 +++++++++++++++----------- app/Helpers/locale_helper.php | 12 ++++++------ app/Models/Attribute.php | 21 ++++++++++++++++----- 4 files changed, 40 insertions(+), 33 deletions(-) diff --git a/app/Controllers/Items.php b/app/Controllers/Items.php index 34adf91db..fc8469185 100644 --- a/app/Controllers/Items.php +++ b/app/Controllers/Items.php @@ -492,17 +492,9 @@ class Items extends Secure_Controller public function getAttributes(int $item_id = NEW_ENTRY): void { $data['item_id'] = $item_id; - if($this->request->getGet('definition_ids') != null) - { - $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(); - } - else - { - $data['definition_values'] = []; - $data['definition_names'] = []; - } + $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) { diff --git a/app/Controllers/Secure_Controller.php b/app/Controllers/Secure_Controller.php index 10ba7d78c..603b11289 100644 --- a/app/Controllers/Secure_Controller.php +++ b/app/Controllers/Secure_Controller.php @@ -21,7 +21,7 @@ class Secure_Controller extends BaseController { public array $global_view_data; - public function __construct(string $module_id = '', string $submodule_id = NULL, string $menu_group = NULL) + public function __construct(string $module_id = '', string $submodule_id = null, string $menu_group = null) { $this->employee = model('Employee'); $this->module = model('Module'); @@ -69,16 +69,20 @@ class Secure_Controller extends BaseController view('viewData', $global_view_data); } + /** + * AJAX function used to confirm whether values sent in the request are numeric + * @return void + */ public function getCheckNumeric() { - $result = TRUE; + $result = true; - foreach($this->request->getVar(NULL, FILTER_SANITIZE_FULL_SPECIAL_CHARS) as $str) + foreach($this->request->getVar(null, FILTER_SANITIZE_FULL_SPECIAL_CHARS) as $value) { - $result &= parse_decimals($str); + $result &= (int)parse_decimals($value); } - echo $result !== FALSE ? 'true' : 'false'; + echo $result !== false ? 'true' : 'false'; } public function getConfig($key) @@ -90,10 +94,10 @@ class Secure_Controller extends BaseController } // this is the basic set of methods most OSPOS Controllers will implement - public function getIndex() { return FALSE; } - public function getSearch() { return FALSE; } - public function suggest_search() { return FALSE; } - public function getView(int $data_item_id = -1) { return FALSE; } - public function postSave(int $data_item_id = -1) { return FALSE; } - public function postDelete() { return FALSE; } + public function getIndex() { return false; } + public function getSearch() { return false; } + public function suggest_search() { return false; } + public function getView(int $data_item_id = -1) { return false; } + public function postSave(int $data_item_id = -1) { return false; } + public function postDelete() { return false; } } diff --git a/app/Helpers/locale_helper.php b/app/Helpers/locale_helper.php index 42c1a9630..b78566a50 100644 --- a/app/Helpers/locale_helper.php +++ b/app/Helpers/locale_helper.php @@ -414,7 +414,7 @@ function parse_tax(string $number) * @param int|NULL $decimals * @return false|float|int|mixed|string */ -function parse_decimals(string $number, int $decimals = NULL) +function parse_decimals(string $number, int $decimals = null) { // ignore empty strings and return if(empty($number)) @@ -424,17 +424,17 @@ function parse_decimals(string $number, int $decimals = NULL) if ($number > MAX_PRECISION) { - return FALSE; + return false; } if ($number > 1.e14) { - return FALSE; + return false; } $config = config('OSPOS')->settings; - if($decimals === NULL) + if($decimals === false) { $decimals = $config['currency_decimals']; //TODO: $decimals is never used. } @@ -443,7 +443,7 @@ function parse_decimals(string $number, int $decimals = NULL) if(empty($config['thousands_separator'])) { - $fmt->setAttribute(NumberFormatter::GROUPING_SEPARATOR_SYMBOL, ''); + $fmt->setTextAttribute(NumberFormatter::GROUPING_SEPARATOR_SYMBOL, ''); } try @@ -452,7 +452,7 @@ function parse_decimals(string $number, int $decimals = NULL) } catch(Exception $e) { - return FALSE; + return false; } } diff --git a/app/Models/Attribute.php b/app/Models/Attribute.php index 92b66a92c..edd848a52 100644 --- a/app/Models/Attribute.php +++ b/app/Models/Attribute.php @@ -236,10 +236,11 @@ class Attribute extends Model public function get_definitions_by_flags(int $definition_flags): array { $builder = $this->db->table('attribute_definitions'); - $builder->where('definition_flags', $definition_flags); + $builder->where(new RawSql("definition_flags & $definition_flags")); //TODO: we need to heed CI warnings to escape properly $builder->where('deleted', 0); $builder->where('definition_type <>', GROUP); $builder->orderBy('definition_id'); + $results = $builder->get()->getResultArray(); return $this->to_array($results, 'definition_id', 'definition_name'); @@ -422,7 +423,7 @@ class Attribute extends Model $checkbox_attribute_values = $this->checkbox_attribute_values($definition_id); $this->db->transStart(); -//TODO: We should see if we can convert these queries to use query builder from CI4. +//TODO: We should see if we can convert these queries to use query builder from CI4. Likely we need to do this using RawSql() https://codeigniter.com/user_guide/database/query_builder.html?highlight=rawsql#query-builder-where-rawsql //TODO: We should see if we can refactor a function out of this block and the identical block above it. $query = 'UPDATE '. $this->db->prefixTable('attribute_links') .' links '; $query .= 'JOIN '. $this->db->prefixTable('attribute_values') .' vals '; @@ -533,6 +534,14 @@ class Attribute extends Model return $builder->get()->getResultArray(); } + /** + * Inserts or updates an attribute link + * + * @param int $item_id + * @param int $definition_id + * @param int $attribute_id + * @return bool + */ public function save_link(int $item_id, int $definition_id, int $attribute_id): bool { $this->db->transStart(); @@ -589,7 +598,7 @@ class Attribute extends Model $builder->where('definition_id', $definition_id); } - return $builder->get('attribute_links')->getRowObject(); + return $builder->get()->getRowObject(); } public function get_link_values(int $item_id, string $sale_receiving_fk, ?int $id, ?int $definition_flags): ResultInterface @@ -617,10 +626,12 @@ class Attribute extends Model if(!empty($id)) { - $builder->where('definition_flags & ', $definition_flags); + $builder->where('definition_flags &=', $definition_flags); } - return $builder->get(); + $result = $builder->get(); + + return $result; } public function get_attribute_value(int $item_id, int $definition_id): ?object