delete( self::table(), array( 'contact_id' => (int) $contact_id ) ); } public static function on_merge( $keep_id, $merge_id ) { global $wpdb; $wpdb->update( self::table(), array( 'contact_id' => (int) $keep_id ), array( 'contact_id' => (int) $merge_id ) ); } /** Due-date presets offered when planning a next step. */ public static function presets() { return array( 'today' => __( 'Today', 'nodebook' ), 'tomorrow' => __( 'Tomorrow', 'nodebook' ), '3days' => __( 'In 3 days', 'nodebook' ), 'week' => __( 'Next week', 'nodebook' ), '2weeks' => __( 'In 2 weeks', 'nodebook' ), 'month' => __( 'In a month', 'nodebook' ), 'date' => __( 'On a date…', 'nodebook' ), ); } /** A preset or a Y-m-d date → GMT datetime (9:00 site time), or null. */ public static function due_from( $preset, $date = '' ) { $tz = wp_timezone(); if ( 'date' === $preset ) { return preg_match( '/^\d{4}-\d{2}-\d{2}$/', (string) $date ) ? gmdate( 'Y-m-d H:i:s', ( new DateTimeImmutable( $date . ' 09:00', $tz ) )->getTimestamp() ) : null; } if ( 'today' === $preset ) { // Today: due now (it shows under "Today" straight away). return current_time( 'mysql', true ); } $ts = Nodebook_Followup::preset( $preset ); return $ts ? gmdate( 'Y-m-d H:i:s', $ts ) : null; } /** * Create a task. * * @param int $contact_id Contact. * @param string $title What to do. * @param array $args due_at (GMT datetime or null), assignee_id, created_by, deal_id. * @return int Task ID, or 0. */ public static function add( $contact_id, $title, array $args = array() ) { global $wpdb; $title = mb_substr( trim( sanitize_text_field( (string) $title ) ), 0, 190 ); if ( '' === $title || ( $contact_id && ! Nodebook_Contacts::get( $contact_id ) ) ) { return 0; } $by = isset( $args['created_by'] ) ? (int) $args['created_by'] : get_current_user_id(); $who = isset( $args['assignee_id'] ) ? (int) $args['assignee_id'] : $by; $now = current_time( 'mysql', true ); $row = array( 'contact_id' => (int) $contact_id, 'deal_id' => (int) ( $args['deal_id'] ?? 0 ), 'title' => $title, 'due_at' => ! empty( $args['due_at'] ) ? gmdate( 'Y-m-d H:i:s', strtotime( (string) $args['due_at'] . ' UTC' ) ) : null, 'assignee_id' => $who, 'created_by' => $by, 'created_at' => $now, 'updated_at' => $now, ); if ( false === $wpdb->insert( self::table(), $row ) ) { return 0; } $id = (int) $wpdb->insert_id; if ( $contact_id ) { Nodebook_Activities::add( $contact_id, 'task', /* translators: %s: task title */ sprintf( __( 'Next step: %s', 'nodebook' ), $title ) . ( $row['due_at'] ? ' · ' . self::due_label( $row['due_at'] ) : '' ), array( 'object_id' => $id, 'user_id' => $by, 'data' => array_filter( array( 'task' => $id, 'due' => $row['due_at'], 'assignee' => $who, ) ), ) ); } self::flush(); /** * Fires after a task is created. */ do_action( 'nodebook_task_added', $id, $row ); return $id; } public static function get( $id ) { global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', self::table(), $id ), ARRAY_A ); return $row ? $row : null; } /** Mark a task done, with an optional outcome ("Quote sent"). Logged on the timeline. */ public static function complete( $id, $outcome = '', $user_id = 0 ) { global $wpdb; $t = self::get( $id ); if ( ! $t || $t['done_at'] ) { return false; } $user = $user_id ? (int) $user_id : get_current_user_id(); $outcome = mb_substr( trim( sanitize_text_field( (string) $outcome ) ), 0, 190 ); $now = current_time( 'mysql', true ); $wpdb->update( self::table(), array( 'done_at' => $now, 'done_by' => $user, 'outcome' => $outcome, 'updated_at' => $now, ), array( 'id' => (int) $id ) ); if ( $t['contact_id'] ) { Nodebook_Activities::add( (int) $t['contact_id'], 'task', /* translators: %s: task title */ sprintf( __( 'Done: %s', 'nodebook' ), $t['title'] ) . ( '' !== $outcome ? ' · ' . $outcome : '' ), array( 'object_id' => (int) $id, 'user_id' => $user, 'data' => array_filter( array( 'task' => (int) $id, 'done' => true, 'outcome' => $outcome, ) ), ) ); // Working on the contact counts as following up. Nodebook_Contacts::update( (int) $t['contact_id'], array( 'followed_up_at' => $now ) ); } self::flush(); do_action( 'nodebook_task_completed', (int) $id, $outcome ); return true; } public static function reopen( $id ) { global $wpdb; $ok = (bool) $wpdb->update( self::table(), array( 'done_at' => null, 'done_by' => 0, 'updated_at' => current_time( 'mysql', true ), ), array( 'id' => (int) $id ) ); self::flush(); return $ok; } public static function update( $id, array $data ) { global $wpdb; $row = array(); if ( isset( $data['title'] ) ) { $row['title'] = mb_substr( trim( sanitize_text_field( (string) $data['title'] ) ), 0, 190 ); if ( '' === $row['title'] ) { unset( $row['title'] ); } } if ( array_key_exists( 'due_at', $data ) ) { $row['due_at'] = $data['due_at'] ? gmdate( 'Y-m-d H:i:s', strtotime( (string) $data['due_at'] . ' UTC' ) ) : null; } if ( isset( $data['assignee_id'] ) ) { $row['assignee_id'] = (int) $data['assignee_id']; } if ( ! $row ) { return false; } $row['updated_at'] = current_time( 'mysql', true ); $ok = false !== $wpdb->update( self::table(), $row, array( 'id' => (int) $id ) ); self::flush(); return $ok; } public static function delete( $id ) { global $wpdb; $ok = (bool) $wpdb->delete( self::table(), array( 'id' => (int) $id ) ); self::flush(); return $ok; } /** Tasks of a contact: open ones first (soonest due first), then the most recently done. */ public static function for_contact( $contact_id, $done_limit = 10 ) { global $wpdb; $open = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE contact_id = %d AND done_at IS NULL ORDER BY due_at IS NULL, due_at ASC, id ASC', self::table(), $contact_id ), ARRAY_A ); $done = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE contact_id = %d AND done_at IS NOT NULL ORDER BY done_at DESC, id DESC LIMIT %d', self::table(), $contact_id, $done_limit ), ARRAY_A ); return array( 'open' => $open, 'done' => $done, ); } /** Tasks of a deal: open first, then done. */ public static function for_deal( $deal_id ) { global $wpdb; $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE deal_id = %d ORDER BY done_at IS NOT NULL, done_at DESC, due_at IS NULL, due_at ASC, id ASC LIMIT 50', self::table(), $deal_id ), ARRAY_A ); $out = array( 'open' => array(), 'done' => array(), ); foreach ( $rows as $r ) { $out[ empty( $r['done_at'] ) ? 'open' : 'done' ][] = $r; } return $out; } /** * Open tasks due before a moment (overdue and today), or due in a window. * * @param int $assignee 0 = everyone. * @param string $from GMT datetime or '' for no lower bound. * @param string $to GMT datetime. * @param int $limit Max rows. */ public static function due( $assignee, $from, $to, $limit = 50 ) { global $wpdb; // Restricted users: their own tasks, or tasks on contacts they can see. list( $restricted, $me, $unassigned ) = Nodebook_Team::scope_args(); return (array) $wpdb->get_results( $wpdb->prepare( "SELECT t.*, c.first_name, c.last_name, c.email, c.phone, c.country, c.company_id, c.owner_id, c.watched, c.pinned FROM %i t LEFT JOIN %i c ON c.id = t.contact_id WHERE t.done_at IS NULL AND t.due_at IS NOT NULL AND t.due_at < %s AND ( %s = '' OR t.due_at >= %s ) AND ( %d = 0 OR t.assignee_id = %d ) AND ( %d = 0 OR t.assignee_id = %d OR c.owner_id = %d OR ( %d = 1 AND c.owner_id = 0 ) ) ORDER BY t.due_at ASC, t.id ASC LIMIT %d", self::table(), Nodebook_Contacts::table(), $to, (string) $from, (string) $from, (int) $assignee, (int) $assignee, $restricted, $me, $me, $unassigned, $limit ), ARRAY_A ); } /** End of today in the site's time zone, as GMT. */ public static function end_of_today() { return gmdate( 'Y-m-d H:i:s', ( new DateTimeImmutable( 'tomorrow', wp_timezone() ) )->getTimestamp() ); } /** Open tasks due today or earlier for a user (0 = everyone): cached for the menu badge. */ public static function count_due( $assignee = 0 ) { global $wpdb; $key = 'nodebook_tasks_due_' . (int) $assignee; $count = get_transient( $key ); if ( false === $count ) { $count = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE done_at IS NULL AND due_at IS NOT NULL AND due_at < %s AND ( %d = 0 OR assignee_id = %d )', self::table(), self::end_of_today(), (int) $assignee, (int) $assignee ) ); set_transient( $key, $count, 2 * MINUTE_IN_SECONDS ); } return (int) $count; } /** The next open task of each contact: contact_id => task. */ public static function next_for( array $contact_ids ) { global $wpdb; $ids = array_values( array_filter( array_map( 'absint', $contact_ids ) ) ); if ( ! $ids ) { return array(); } $out = array(); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- one %d placeholder per ID. foreach ( (array) $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE done_at IS NULL AND contact_id IN (' . Nodebook_Sql::list_of( count( $ids ) ) . ') ORDER BY due_at IS NULL, due_at ASC, id ASC', array_merge( array( self::table() ), $ids ) ), ARRAY_A ) as $t ) { if ( ! isset( $out[ (int) $t['contact_id'] ] ) ) { $out[ (int) $t['contact_id'] ] = $t; } } return $out; } /** "Today", "Tomorrow", "Overdue since 3 Nov", "Fri 14 Nov". */ public static function due_label( $gmt ) { if ( ! $gmt ) { return __( 'No date', 'nodebook' ); } $tz = wp_timezone(); $due = ( new DateTimeImmutable( $gmt, new DateTimeZone( 'UTC' ) ) )->setTimezone( $tz ); $today = new DateTimeImmutable( 'today', $tz ); $days = (int) floor( ( $due->setTime( 0, 0 )->getTimestamp() - $today->getTimestamp() ) / DAY_IN_SECONDS ); if ( $days < 0 ) { /* translators: %s: date */ return sprintf( __( 'Overdue since %s', 'nodebook' ), wp_date( 'j M', $due->getTimestamp() ) ); } if ( 0 === $days ) { return __( 'Today', 'nodebook' ); } if ( 1 === $days ) { return __( 'Tomorrow', 'nodebook' ); } return wp_date( $days < 7 ? 'l' : 'D j M', $due->getTimestamp() ); } public static function is_overdue( array $t ) { return empty( $t['done_at'] ) && ! empty( $t['due_at'] ) && strtotime( $t['due_at'] . ' UTC' ) < ( new DateTimeImmutable( 'today', wp_timezone() ) )->getTimestamp(); } /** Forget the cached counts (menu badges). */ public static function flush() { foreach ( array_merge( array( 0, get_current_user_id() ), array_keys( Nodebook_Team::users() ) ) as $uid ) { delete_transient( 'nodebook_tasks_due_' . (int) $uid ); } Nodebook_Followup::clear_badge(); } }