<?php
	
	// Custom Purpose

	function get_congregations() {
		
		return array_map( function( $post ) {
			
			return new App\Models\Congregation( $post );
			
		}, get_posts(array(
			'post_type' => 'congregation',
			'showposts' => -1,
			'order' => 'ASC',
			'orderby' => 'menu_order'
		)));
		
	}
	
	function get_congregation( $congregation_id ) {
		
		return get_congregation_by( 'id', $congregation_id );
		
	}
	
	function get_congregation_by( $attribute, $value ) {
		
		$congregations = array_filter(get_congregations(), function($congregation) use($attribute, $value) {
			
			return $congregation->$attribute == $value;
			
		});
		
		return reset($congregations);
		
	}
	
	function get_congregation_in_order( $order ) {
		
		$congregation = null;
			
		foreach($order->get_items() as $item) {
			
			if( ! empty( $item['Congregation'] ) ) {
				
				if( $congregation = get_congregation_by( 'title', $item['Congregation'] ) ) {
				
					break;
					
				}
				
			}
			
		}
		
		return $congregation;
		
	}