queuedCount(); } catch ( \Throwable ) { return 0; } } private static function url( array $args = array() ): string { return Admin::url( self::PAGE, $args ); } /* ---------------------------------------------------------------- render */ public static function render(): void { Admin::open( 'Rewards' ); $edit = Admin::get_int( 'edit' ); if ( 'new' === Admin::get_str( 'view' ) || $edit ) { self::renderForm( $edit ); } else { self::renderQueue(); self::renderMenu(); } Admin::close(); } private static function renderQueue(): void { $cur = Admin::currency(); $repo = self::repo(); $queued = $repo->queued( 200 ); echo '

Waiting to be done (' . count( $queued ) . ')

'; if ( ! $queued ) { echo '

Nothing in the queue. When a donation triggers a reward, it shows up here.

'; } else { echo ''; foreach ( $queued as $e ) { echo ''; echo ''; echo ''; echo ''; echo ''; } echo '
DonorAmountRewardTimeMessage
' . esc_html( Format::donorName( $e ) ) . '' . esc_html( Format::money( (int) ( $e['base_minor'] ?? 0 ), Format::currency( $e['currency'] ?? $cur ) ) ) . '' . esc_html( (string) $e['title'] ) . ' × ' . (int) $e['times'] . '' . esc_html( self::when( (string) $e['created_at'] ) ) . '' . esc_html( (string) ( $e['message'] ?? '' ) ) . ''; Admin::form( 'sg_event_done', array( 'event_id' => (int) $e['id'] ), 'sg-inline' ); echo '
'; Admin::form( 'sg_event_done_all' ); echo '

'; } $done = $repo->recentDone( 15 ); if ( $done ) { echo '

Recently done

'; } echo '
'; } private static function when( string $utc ): string { return '' === $utc ? '' : get_date_from_gmt( $utc, 'Y-m-d H:i' ); } /** @return array goal id => title */ private static function goalTitles(): array { $out = array(); foreach ( Store::goals()->all() as $g ) { $out[ (int) $g['id'] ] = (string) $g['title']; } return $out; } private static function renderMenu(): void { $cur = Admin::currency(); $actions = self::repo()->all(); $goals = self::goalTitles(); echo '

Your reward menu

'; echo '

Donors pick a reward on the donation form (or just donate the amount). Rewards must be at least ' . esc_html( Format::money( self::minMinor(), $cur ) ) . ', the minimum donation.

'; echo '

Add a reward

'; if ( ! $actions ) { echo '

No rewards yet.

'; return; } echo ''; foreach ( $actions as $a ) { $id = (int) $a['id']; echo ''; echo ''; echo ''; echo ''; } echo '
RewardAmountTriggersApplies toOrderActions
' . esc_html( $a['title'] ) . ''; if ( '' !== $a['description'] ) { echo '
' . esc_html( $a['description'] ) . ''; } echo '
' . esc_html( Format::money( $a['amount_minor'], $cur ) ) . '' . esc_html( Matcher::EVERY === $a['mode'] ? 'Every time it adds up' : 'Exactly this amount' ) . '' . esc_html( 0 === $a['goal_id'] ? 'All goals' : ( $goals[ $a['goal_id'] ] ?? 'Goal #' . $a['goal_id'] ) ) . '' . (int) $a['sort'] . ''; echo 'Edit '; Admin::form( 'sg_action_toggle', array( 'action_id' => $id, 'enabled' => $a['enabled'] ? 0 : 1 ), 'sg-inline' ); echo ' '; Admin::form( 'sg_action_delete', array( 'action_id' => $id ), 'sg-inline sg-confirm' ); echo ''; echo '
'; } private static function renderForm( int $id ): void { $a = $id ? self::repo()->find( $id ) : null; if ( $id && ! $a ) { echo '

That reward no longer exists. Back to rewards

'; return; } $cur = Admin::currency(); $goals = self::goalTitles(); $val = static fn( string $k, $d = '' ) => $a[ $k ] ?? $d; $mode = (string) $val( 'mode', Matcher::EXACT ); $data = array( 'currency' => $cur, 'exp' => Money::exponent( $cur ), 'editId' => $id, 'actions' => array_values( array_map( static fn( array $r ): array => array( 'id' => $r['id'], 'goal_id' => $r['goal_id'], 'title' => $r['title'], 'amount_minor' => $r['amount_minor'], 'mode' => $r['mode'] ), array_filter( self::repo()->all(), static fn( array $r ): bool => (bool) $r['enabled'] && $r['id'] !== $id ) ) ), ); echo '

' . ( $a ? 'Edit reward' : 'Add a reward' ) . '

'; Admin::form( 'sg_action_save', array( 'action_id' => $id ), 'sg-action-form' ); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '

What would a donation do?

'; echo '

'; echo '
'; echo '

Cancel

'; } /* -------------------------------------------------------------- handlers */ public static function handleSave(): void { Admin::guard( 'sg_action_save' ); $post = Admin::post_raw(); $id = isset( $post['action_id'] ) ? absint( $post['action_id'] ) : 0; $back = self::url( $id ? array( 'edit' => $id ) : array( 'view' => 'new' ) ); $repo = self::repo(); if ( $id && ! $repo->find( $id ) ) { Admin::redirect( self::url(), 'error', 'That reward no longer exists.' ); } $res = ActionInput::validate( $post, self::minMinor(), Admin::currency(), array_keys( self::goalTitles() ), $repo->nextSort() ); if ( null === $res['data'] ) { Admin::redirect( $back, 'error', (string) $res['error'] ); } if ( $id ) { $repo->update( $id, $res['data'] ); } else { $repo->create( $res['data'] ); } Admin::redirect( self::url(), 'success', 'Reward saved.' ); } public static function handleToggle(): void { Admin::guard( 'sg_action_toggle' ); $post = Admin::post_raw(); self::repo()->setEnabled( isset( $post['action_id'] ) ? absint( $post['action_id'] ) : 0, ! empty( $post['enabled'] ) ); Admin::redirect( self::url(), 'success', 'Reward updated.' ); } public static function handleDelete(): void { Admin::guard( 'sg_action_delete' ); $post = Admin::post_raw(); self::repo()->delete( isset( $post['action_id'] ) ? absint( $post['action_id'] ) : 0 ); Admin::redirect( self::url(), 'success', 'Reward deleted.' ); } public static function handleDone(): void { Admin::guard( 'sg_event_done' ); $post = Admin::post_raw(); self::repo()->markDone( isset( $post['event_id'] ) ? absint( $post['event_id'] ) : 0 ); Admin::redirect( self::url(), 'success', 'Marked as done.' ); } public static function handleDoneAll(): void { Admin::guard( 'sg_event_done_all' ); $n = self::repo()->markAllDone(); Admin::redirect( self::url(), 'success', $n . ( 1 === $n ? ' reward' : ' rewards' ) . ' marked as done.' ); } }