overrides = $db->prefix . 'schedulereed_schedule_overrides'; } /** * An owner's weekly hours, or null when it has none of its own. * * @param string $owner_type Owner type. * @param int $owner_id Owner. */ public function weekly( string $owner_type, int $owner_id ): ?WeeklyHours { return $this->weekly_many( $owner_type, array( $owner_id ) )[ $owner_id ] ?? null; } /** * Weekly hours for many owners of one type. * * @param string $owner_type Owner type. * @param int[] $owner_ids Owners. * @return array Only owners that have hours. */ public function weekly_many( string $owner_type, array $owner_ids ): array { $owner_ids = array_values( array_unique( array_map( 'intval', $owner_ids ) ) ); if ( array() === $owner_ids ) { return array(); } $args = array_merge( array( $owner_type ), $owner_ids ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.DirectDatabaseQuery $rows = (array) $this->db->get_results( $this->db->prepare( "SELECT owner_id, hours FROM {$this->table} WHERE owner_type = %s AND owner_id IN (" . self::in( $owner_ids ) . ')', $args ), ARRAY_A ); $out = array(); foreach ( $rows as $row ) { try { $out[ (int) $row['owner_id'] ] = WeeklyHours::from_array( self::json( $row['hours'] ) ); } catch ( InvalidArgumentException ) { continue; } } return $out; } /** * Set or clear an owner's weekly hours. * * @param string $owner_type Owner type. * @param int $owner_id Owner. * @param WeeklyHours|null $hours Hours, or null to use the next level's. */ public function set_weekly( string $owner_type, int $owner_id, ?WeeklyHours $hours ): void { if ( null === $hours ) { $this->db->delete( $this->table, array( 'owner_type' => $owner_type, 'owner_id' => $owner_id, ) ); return; } $this->db->replace( $this->table, array( 'owner_type' => $owner_type, 'owner_id' => $owner_id, 'hours' => wp_json_encode( $hours->to_array() ), 'updated_at' => self::now(), ) ); } /** * Overrides of owners overlapping a date range. * * @param string $owner_type Owner type. * @param int[] $owner_ids Owners. * @param string $from First date, Y-m-d. * @param string $to Last date. * @return array Owner ID => overrides. */ public function overrides( string $owner_type, array $owner_ids, string $from, string $to ): array { $owner_ids = array_values( array_unique( array_map( 'intval', $owner_ids ) ) ); if ( array() === $owner_ids ) { return array(); } $args = array_merge( array( $owner_type ), $owner_ids, array( $to, $from ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.DirectDatabaseQuery $rows = (array) $this->db->get_results( $this->db->prepare( "SELECT * FROM {$this->overrides} WHERE owner_type = %s AND owner_id IN (" . self::in( $owner_ids ) . ') AND date_from <= %s AND date_to >= %s ORDER BY date_from', $args ), ARRAY_A ); $out = array(); foreach ( $rows as $row ) { $out[ (int) $row['owner_id'] ][] = self::override( $row ); } return $out; } /** * Every override of one owner, newest first, with IDs. * * @param string $owner_type Owner type. * @param int $owner_id Owner. * @param string $since Leave out ones that ended before this date. * @return array> */ public function list_overrides( string $owner_type, int $owner_id, string $since = '' ): array { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $rows = (array) $this->db->get_results( $this->db->prepare( "SELECT * FROM {$this->overrides} WHERE owner_type = %s AND owner_id = %d AND date_to >= %s ORDER BY date_from", $owner_type, $owner_id, '' === $since ? '1000-01-01' : $since ), ARRAY_A ); return array_map( static fn( array $row ): array => array( 'id' => (int) $row['id'], 'source' => (string) $row['source'], ) + self::override( $row )->to_array(), $rows ); } /** * Add an override. * * @param string $owner_type Owner type. * @param int $owner_id Owner. * @param DateOverride $override Override. * @param string $source owner or holidays. * @return int ID. */ public function add_override( string $owner_type, int $owner_id, DateOverride $override, string $source = 'owner' ): int { $this->db->insert( $this->overrides, array( 'owner_type' => $owner_type, 'owner_id' => $owner_id, 'date_from' => $override->from, 'date_to' => $override->to, 'hours' => wp_json_encode( $override->to_array()['hours'] ), 'note' => $override->note, 'source' => $source, ) ); return (int) $this->db->insert_id; } /** * Delete an override of an owner. * * @param string $owner_type Owner type. * @param int $owner_id Owner. * @param int $id Override ID. */ public function delete_override( string $owner_type, int $owner_id, int $id ): bool { return (bool) $this->db->delete( $this->overrides, array( 'id' => $id, 'owner_type' => $owner_type, 'owner_id' => $owner_id, ) ); } /** * Remove generated holidays, e.g. before loading another country's. */ public function clear_holidays(): void { $this->db->delete( $this->overrides, array( 'source' => self::HOLIDAY ) ); } /** * Build an override from a row. * * @param array $row Row. */ private static function override( array $row ): DateOverride { try { $hours = WeeklyHours::ranges( (array) self::json( $row['hours'] ), 'hours' ); } catch ( InvalidArgumentException ) { $hours = array(); } return new DateOverride( (string) $row['date_from'], (string) $row['date_to'], $hours, (string) $row['note'] ); } }