FieldsRepository.php 1.14 KB
Newer Older
Kyle Johnson's avatar
Kyle Johnson committed
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

class NF_Database_FieldsRepository
{
    protected $db;

    public function __construct( $db ) {
        $this->db = $db;
    }

    public function fetch( $form_id ) {
        $table_name = $this->db->prefix . 'nf3_fields';
        $meta_table_name = $this->db->prefix . 'nf3_field_meta';
14
        $query_builder = new NF_Database_MetaQueryBuilder( $table_name, $meta_table_name, $form_id );
Kyle Johnson's avatar
Kyle Johnson committed
15
        
16 17
        $fields_sql = $query_builder->get_sql();
        $field_meta_sql = $query_builder->get_meta_sql();
Kyle Johnson's avatar
Kyle Johnson committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

        $fields = array();

        $field_data = $this->db->get_results( $fields_sql, ARRAY_A );
        foreach( $field_data as $field_data ) {
            $field_id = $field_data[ 'id' ];
            unset( $field_data[ 'id' ] );
            $fields[ $field_id ] = $field_data;
        }

        $field_meta = $this->db->get_results( $field_meta_sql );
        foreach( $field_meta as $meta ) {
            if( ! isset( $fields[ $meta->parent_id ][ $meta->key ] ) ){
                $fields[ $meta->parent_id ][ $meta->key ] = $meta->value;
            }
        }

        return $collection = new NF_Database_FieldsCollection( $fields );
    }
}