Commit 15ccde89 authored by Kyle Johnson's avatar Kyle Johnson 📚

Add Field-related objects.

parent 1d9c6ea0
Pipeline #762 passed with stage
in 52 seconds
{
"name": "saturday-drive/ninja-forms",
"license": "GPL-v3",
"require": {},
"require-dev": {
"phpunit/phpunit": "^7.4"
}
}
<?php
class NF_Database_FieldsCollection implements JsonSerializable
{
protected $fields = array();
/**
* @param array $field_data [ field_id => [ field_settig, ... ], ... ]
*/
public function __construct( $fields_data = array() )
{
$this->fields = $fields_data;
}
/**
* @param int $field_id
* @return array $field_settings
*/
public function get_field_settings( $field_id )
{
if( ! isset( $this->fields[ $field_id ] ) ){
return array();
}
return $field_settings = $this->fields[ $field_id ];
}
/**
* @return array [ field_id => [ field_settig, ... ], ... ]
*/
public function jsonSerialize() {
return $this->fields;
}
/**
* @return array
*/
public function __debugInfo()
{
return array(
'fields' => $this->fields
);
}
}
\ No newline at end of file
<?php
/**
* Builds an SQL statement to fetch fields (with settings) for a given form ID.
*/
class NF_Database_FieldsQueryBuilder
{
protected $table_name = '';
protected $meta_table_name = '';
protected $parent_id = 0;
/**
* @param string $table_name
* @param string $meta_table_name
* @param string $parent_id Form ID
*/
public function __construct( $table_name = '', $meta_table_name = '', $parent_id = 0 ) {
$this->table_name = $table_name;
$this->meta_table_name = $meta_table_name;
$this->parent_id = $parent_id;
}
/**
* @return string
*/
public function get_field_ids_sql()
{
return "SELECT DISTINCT $this->table_name.id FROM $this->table_name WHERE {$this->table_name}.parent_id = $this->parent_id";
}
/**
* @return string
*/
public function get_fields_sql()
{
return "
SELECT *
FROM $this->table_name
WHERE id IN ( {$this->get_field_ids_sql()} )
";
}
public function get_field_meta_sql()
{
return "
SELECT Meta.parent_id, Meta.key, Meta.value
FROM $this->table_name as Object
JOIN $this->meta_table_name as Meta
ON Object.id = Meta.parent_id
WHERE Object.id IN ( {$this->get_field_ids_sql()} )
";
}
}
\ No newline at end of file
<?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';
$query_builder = new NF_Database_FieldsQueryBuilder( $table_name, $meta_table_name, $form_id );
$fields_sql = $query_builder->get_fields_sql();
$field_meta_sql = $query_builder->get_field_meta_sql();
$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 );
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment