Skip to content

Commit

Permalink
Merge pull request #184 from prepare_for_migration
Browse files Browse the repository at this point in the history
Prepare for migration
  • Loading branch information
DidierViret committed May 9, 2023
2 parents d5babe9 + 6a442ca commit d878368
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 21 deletions.
70 changes: 54 additions & 16 deletions orif/stock/Controllers/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ public function index($page = 1) {

$output['item_conditions'] = $this->item_condition_model->dropdown('name');

$output['item_groups'] = $this->item_group_model->dropdown('name');

$output['stocking_places'] = $this->stocking_place_model->dropdown('name');
$output['sort_order'] = array(lang('MY_application.sort_order_name'),
lang('MY_application.sort_order_stocking_place_id'),
lang('MY_application.sort_order_date'),
Expand Down Expand Up @@ -141,6 +138,16 @@ public function index($page = 1) {
}
}

$filters = $_GET;

$filters['e'] = $this->getEFilter($filters);

$where = isset($filters['e']) && $filters['e'] !== 0 ? "fk_entity_id = {$filters['e']}" : "fk_entity_id IS NULL";

$output['item_groups'] = $this->dropdown($this->item_group_model->select(["item_group_id", "name"])->where($where)->findAll(), 'item_group_id');

$output['stocking_places'] = $this->dropdown($this->stocking_place_model->select(["stocking_place_id", "name"])->where($where)->findAll(), 'stocking_place_id');

$output['entities_has_items'] = $this->has_items(false);

// Send the data to the View
Expand All @@ -162,19 +169,7 @@ private function load_list($page = 1)
$filters['c'] = array($this->config->functional_item_condition);
}

if (!isset($filters['e'])) {
if (isset($_SESSION['user_id'])) {
$entityId = $this->user_entity_model->where('fk_user_id', $_SESSION['user_id'])->first()['fk_entity_id'] ?? 0;
} else {
$entityId = 0;
}

if ($entityId !== 0) {
$filters['e'] = $entityId;
} else {
$filters['e'] = $this->entity_model->first()['entity_id'] ?? 0;
}
}
$filters['e'] = $this->getEFilter($filters);

// Sanitize $page parameter
if (empty($page) || !is_numeric($page) || $page<1) {
Expand Down Expand Up @@ -1132,6 +1127,8 @@ private function set_validation_rules() {

$validation->setRule("name", lang('MY_application.field_item_name'), 'required');
$validation->setRule("inventory_prefix", lang('MY_application.field_inventory_number'), 'required');
$validation->setRule("item_group_id", lang('MY_application.field_group'), 'required');
$validation->setRule("stocking_place_id", lang('MY_application.field_stocking_place'), 'required');

return $validation;
}
Expand Down Expand Up @@ -1170,4 +1167,45 @@ public function return_loan() {

}

/**
* Transform the array for dropdown display
*
* @param array $array
* @param string $id = column name of the id
* @return array
*/
private function dropdown($array, $id) {
$result = array();

foreach ($array as $row) {
$result[$row[$id]] = $row['name'];
}

return $result;
}

/**
* Get e filter or set the default one
*
* @param array $filters = $_GET variable
* @return int
*/
private function getEFilter($filters): int {
if (isset($filters['e'])) {
return $filters['e'];
}

if (isset($_SESSION['user_id'])) {
$entityId = $this->user_entity_model->where('fk_user_id', $_SESSION['user_id'])->first()['fk_entity_id'] ?? 0;
} else {
$entityId = 0;
}

if ($entityId !== 0) {
return $entityId;
}

return $this->entity_model->first()['entity_id'] ?? 0;
}

}
16 changes: 12 additions & 4 deletions orif/stock/Views/item/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,20 @@ class="btn btn-success mb-3"><?php echo htmlspecialchars(lang('MY_application.bt
filters = location.search;
load_items(page, filters);

// Reload the page entirely if entity has been changed
$('#e ul.multiselect-container input[type=radio]').change(() => {
// Load page 1 with new filters
load_items(1, getFilters()).finally(() => {
location.reload();
});
});

// Reload items list on filter update
$("input[type=checkbox], input[type=radio]").change(function(){
$("input[type=checkbox], input[type=radio]").change(function() {
// Load page 1 with new filters
load_items(1, getFilters());
});
$("#text_search").on("change blur", function(){
$("#text_search").on("change blur", function() {
// Load page 1 with new filters
load_items(1, getFilters());
});
Expand All @@ -181,7 +189,7 @@ class="btn btn-success mb-3"><?php echo htmlspecialchars(lang('MY_application.bt
// ******************************************
let populating = false;

function load_items(page, filters){
async function load_items(page, filters) {
if (populating) return;
populating = true;

Expand All @@ -201,7 +209,7 @@ function load_items(page, filters){
// URL for ajax call to PHP controller Stock\Controllers\
url = "<?= base_url("/Item/load_list_json")?>"+ "/" + page+filters;

$.ajax({
await $.ajax({
url: url,
type: "get",
success: function (response) {
Expand Down
2 changes: 1 addition & 1 deletion public/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Options All -Indexes
# request to the front controller, index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]
RewriteRule ^([\s\S]*)$ index.php?/$1 [L,NC,QSA]

# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
Expand Down

0 comments on commit d878368

Please sign in to comment.