Skip to content

Commit

Permalink
Use subquery to fix pronamic/orbis.pronamic.nl#49.
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Mar 12, 2024
1 parent e4d3fa9 commit 0d5f7d0
Showing 1 changed file with 50 additions and 35 deletions.
85 changes: 50 additions & 35 deletions classes/QueryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,67 +125,82 @@ public function posts_clauses( $pieces, $query ) {
return $pieces;
}

// Fields
$fields = ',
project.id AS project_id,
project.number_seconds AS project_number_seconds,
project.finished AS project_is_finished,
project.invoiced AS project_is_invoiced
';

// Join
$join = "
LEFT JOIN
$wpdb->orbis_projects AS project
ON $wpdb->posts.ID = project.post_id
";

if ( property_exists( $wpdb, 'orbis_companies' ) ) {
$fields .= ',
principal.id AS principal_id,
principal.name AS principal_name,
principal.post_id AS principal_post_id
';

$join .= "
LEFT JOIN
$wpdb->orbis_companies AS principal
ON project.principal_id = principal.id
";
/**
* Construct a subquery to join the project data.
*
* @link https://github.com/pronamic/orbis.pronamic.nl/issues/49
*/
$subquery_select_expr = [
'project.post_id',
'project.id AS project_id',
'project.number_seconds AS project_number_seconds',
'project.finished AS project_is_finished',
'project.invoiced AS project_is_invoiced',
'project.invoice_number AS project_invoice_number'
];

$subquery_table_references = [
$wpdb->orbis_projects . ' AS project',
];

if ( \property_exists( $wpdb, 'orbis_companies' ) ) {
$subquery_select_expr[] = 'principal.id AS principal_id';
$subquery_select_expr[] = 'principal.name AS principal_name';
$subquery_select_expr[] = 'principal.post_id AS principal_post_id';

$subquery_table_references[] = " LEFT JOIN $wpdb->orbis_companies AS principal ON project.principal_id = principal.id";
}

// Where
$where = '';
if ( \property_exists( $wpdb, 'orbis_timesheets' ) ) {
$subquery_select_expr[] = 'SUM( logged_time.number_seconds ) AS project_logged_time';

$subquery_table_references[] = " LEFT JOIN $wpdb->orbis_timesheets AS logged_time ON logged_time.project_id = project.id";
}

$subquery = \implode(
' ',
[
'SELECT ' . implode( ', ', $subquery_select_expr ),
'FROM ' . implode( ' ', $subquery_table_references ),
'GROUP BY project.id',
]
);

/**
* Adjust the WordPress query pieces.
*/
$fields = ', project_data.*';
$join = " LEFT JOIN ( $subquery ) AS project_data ON $wpdb->posts.id = project_data.post_id";
$where = '';

$principal = $query->get( 'orbis_project_principal' );

if ( ! empty( $principal ) ) {
$where .= $wpdb->prepare( ' AND principal.name LIKE %s', '%' . $wpdb->esc_like( $principal ) . '%' );
$where .= $wpdb->prepare( ' AND project_data.principal_name LIKE %s', '%' . $wpdb->esc_like( $principal ) . '%' );
}

$client_id = $query->get( 'orbis_project_client_id' );

if ( ! empty( $client_id ) ) {
$where .= $wpdb->prepare( ' AND principal.post_id LIKE %d ', $client_id );
$where .= $wpdb->prepare( ' AND project_data.principal_post_id LIKE %d ', $client_id );
}

$invoice_number = $query->get( 'orbis_project_invoice_number' );

if ( ! empty( $invoice_number ) ) {
$where .= $wpdb->prepare( ' AND project.invoice_number LIKE %s', '%' . $wpdb->esc_like( $invoice_number ) . '%' );
$where .= $wpdb->prepare( ' AND project_data.project_invoice_number LIKE %s', '%' . $wpdb->esc_like( $invoice_number ) . '%' );
}

$is_finished = $query->get( 'orbis_project_is_finished', null );

if ( null !== $is_finished ) {
$is_finished = filter_var( $is_finished, FILTER_VALIDATE_BOOLEAN );

$where .= $wpdb->prepare( ' AND project.finished = %d', $is_finished );
$where .= $wpdb->prepare( ' AND project_data.project_is_finished = %d', $is_finished );
}

// Pieces
$pieces['join'] .= $join;
$pieces['fields'] .= $fields;
$pieces['join'] .= $join;
$pieces['where'] .= $where;

return $pieces;
Expand Down

0 comments on commit 0d5f7d0

Please sign in to comment.