Skip to content

Commit

Permalink
Refactor getBulkLinks
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
  • Loading branch information
kamil-tekiela committed Mar 22, 2023
1 parent 49e79fc commit fa5cb8a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
29 changes: 15 additions & 14 deletions libraries/classes/Display/Results.php
Expand Up @@ -3553,7 +3553,7 @@ public function getTable(
$this->properties['display_params'] = null;

// 4. ----- Prepares the link for multi-fields edit and delete
$bulkLinks = $this->getBulkLinks($dtResult, $statementInfo, $displayParts->deleteLink);
$isClauseUnique = $this->isClauseUnique($dtResult, $statementInfo, $displayParts->deleteLink);

// 5. ----- Prepare "Query results operations"
$operations = [];
Expand All @@ -3568,7 +3568,9 @@ public function getTable(
'navigation' => $navigation,
'headers' => $headers,
'body' => $body,
'bulk_links' => $bulkLinks,
'has_bulk_links' => $displayParts->deleteLink === DisplayParts::DELETE_ROW,
'has_export_button' => $this->hasExportButton($statementInfo, $displayParts->deleteLink),
'clause_is_unique' => $isClauseUnique,
'operations' => $operations,
'db' => $this->properties['db'],
'table' => $this->properties['table'],
Expand Down Expand Up @@ -3873,19 +3875,16 @@ private function setParamForLinkForeignKeyRelatedTables(array $map): array
*
* @see getTable()
*
* @param ResultInterface $dtResult the link id associated to the query which
* results have to be displayed
* @param ResultInterface $dtResult the link id associated to the query which results have to be displayed
* @psalm-param DisplayParts::NO_DELETE|DisplayParts::DELETE_ROW|DisplayParts::KILL_PROCESS $deleteLink
*
* @psalm-return array{has_export_button:bool, clause_is_unique:mixed}|array<empty, empty>
*/
private function getBulkLinks(
private function isClauseUnique(
ResultInterface $dtResult,
StatementInfo $statementInfo,
int $deleteLink,
): array {
): bool {
if ($deleteLink !== DisplayParts::DELETE_ROW) {
return [];
return false;
}

// fetch last row of the result set
Expand All @@ -3894,7 +3893,7 @@ private function getBulkLinks(

$expressions = [];

if (isset($statementInfo->statement) && $statementInfo->statement instanceof SelectStatement) {
if ($statementInfo->statement instanceof SelectStatement) {
$expressions = $statementInfo->statement->expr;
}

Expand All @@ -3914,10 +3913,12 @@ private function getBulkLinks(
// reset to first row for the loop in getTableBody()
$dtResult->seek(0);

return [
'has_export_button' => $statementInfo->queryType === 'SELECT',
'clause_is_unique' => $clauseIsUnique,
];
return $clauseIsUnique;
}

private function hasExportButton(StatementInfo $statementInfo, int $deleteLink): bool
{
return $deleteLink === DisplayParts::DELETE_ROW && $statementInfo->queryType === 'SELECT';
}

/**
Expand Down
4 changes: 4 additions & 0 deletions psalm-baseline.xml
Expand Up @@ -6576,6 +6576,7 @@
</MixedAssignment>
<MixedInferredReturnType>
<code>Message</code>
<code>bool</code>
</MixedInferredReturnType>
<MixedMethodCall>
<code>new $className()</code>
Expand All @@ -6602,6 +6603,9 @@
<code><![CDATA[$statementInfo->statement->from[0]->table]]></code>
<code><![CDATA[$statementInfo->statement->limit->offset]]></code>
</MixedPropertyFetch>
<MixedReturnStatement>
<code>$clauseIsUnique</code>
</MixedReturnStatement>
<MixedReturnTypeCoercion>
<code>$map</code>
<code><![CDATA[[
Expand Down
6 changes: 3 additions & 3 deletions templates/display/results/table.twig
Expand Up @@ -298,7 +298,7 @@
</table>
</div>

{% if bulk_links is not empty %}
{% if has_bulk_links %}
<div class="d-print-none">
<img class="selectallarrow" src="{{ image('arrow_' ~ text_dir ~ '.png') }}" width="38" height="22" alt="{% trans 'With selected:' %}">
<input type="checkbox" id="resultsForm_{{ unique_id }}_checkall" class="checkall_box" title="{% trans 'Check all' %}">
Expand All @@ -317,14 +317,14 @@
{{ get_icon('b_drop', 'Delete'|trans) }}
</button>

{% if bulk_links.has_export_button %}
{% if has_export_button %}
<button class="btn btn-link mult_submit" type="submit" name="submit_mult" value="export" title="{% trans 'Export' %}">
{{ get_icon('b_tblexport', 'Export'|trans) }}
</button>
{% endif %}
</div>

<input type="hidden" name="clause_is_unique" value="{{ bulk_links.clause_is_unique }}">
<input type="hidden" name="clause_is_unique" value="{{ clause_is_unique }}">
<input type="hidden" name="sql_query" value="{{ sql_query }}">
</form>
{% endif %}
Expand Down

0 comments on commit fa5cb8a

Please sign in to comment.