예제 #1
0
    public static function get_readings($args = array())
    {
        global $wpdb;
        extract($args);
        $wheres = array();
        // $ref = new BfoxRef;
        if (isset($ref) && $ref->is_valid()) {
            $wheres[] = $ref->sql_where2();
        }
        if (!empty($reading_ids)) {
            $or = array();
            foreach ($reading_ids as $_revision_id => $plan_reading_ids) {
                if (!empty($_revision_id) && !empty($plan_reading_ids)) {
                    $or[] = $wpdb->prepare('(revision_id = %d AND reading_id IN (' . implode(',', (array) $wpdb->escape($plan_reading_ids)) . '))', $_revision_id);
                }
            }
            if (!empty($or)) {
                $wheres[] = '(' . implode(' OR ', $or) . ')';
            }
        }
        if (!empty($revision_id)) {
            $wheres[] = '(revision_id IN (' . implode(',', (array) $wpdb->escape($revision_id)) . '))';
        }
        $readings = array();
        if (!empty($wheres)) {
            // Get the reading info from the DB
            $_readings = $wpdb->get_results('
				SELECT *, GROUP_CONCAT(start) as start, GROUP_CONCAT(end) as end
				FROM ' . self::$readings_table_name . '
				WHERE ' . implode(' AND ', $wheres) . '
				GROUP BY revision_id, reading_id
				ORDER BY revision_id ASC, reading_id ASC');
            // Add all the readings to the reading plan
            foreach ($_readings as $_reading) {
                $reading = new BfoxRef();
                $reading->add_concat($_reading->start, $_reading->end);
                $readings[$_reading->revision_id][$_reading->reading_id] = $reading;
            }
        }
        return $readings;
    }