Newer
Older
<?php
namespace calderawp\CalderaFormsQuery;
use calderawp\CalderaFormsQuery\Select\Entry;
use calderawp\CalderaFormsQuery\Select\EntryValues;
* Used to query entry data, using SQL created by generators
class SelectQueries implements CreatesSelectQueries
{
/**
* SQL generator for entry table
*
* @var Entry
*/
protected $entryGenerator;
/**
* SQL generator for entry values table
*
* @var EntryValues
*/
protected $entryValueGenerator;
/**
* @var \wpdb
*/
protected $wpdb;
public function __construct(Entry $entryGenerator, EntryValues $entryValueGenerator, \wpdb $wpdb)
{
$this->entryGenerator = $entryGenerator;
$this->entryValueGenerator = $entryValueGenerator;
$this->wpdb = $wpdb;
}
/** @inheritdoc */
$results = $this->wpdb->get_results($sql);
if (empty($results)) {
return [];
}
return $results;
}
/** @inheritdoc */
public function getEntryValueGenerator()
{
return $this->entryValueGenerator;
}
/** @inheritdoc */
public function getEntryGenerator()
{
return $this->entryGenerator;
}
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Get all data for a user by Id
*
* @param $userId
* @return array
*/
public function selectByUserId($userId)
{
$this->resetEntryGenerator();
$this
->getEntryGenerator()
->queryByUserId($userId);
$entries = $this->getResults($this->getEntryGenerator()->getPreparedSql());
return $this->collectResults($entries);
}
/**
* Reset entry generator
*/
private function resetEntryGenerator()
{
$this->entryGenerator->resetQuery();
}
/**
* Reset entry value generator
*/
private function resetEntryValueGenerator()
{
$this->entryValueGenerator->resetQuery();
}
/**
* Collect results using Caldera_Forms_Entry_Entry and Caldera_Forms_Entry_Field to represent values
*
* @param \stdClass[] $entriesValues
* @return array
*/
private function collectResults($entriesValues)
{
$results = [];
foreach ($entriesValues as $entry) {
$this->resetEntryValueGenerator();
$entry = new \Caldera_Forms_Entry_Entry($entry);
$this
->getEntryValueGenerator()
->queryByEntryId($entry->id);
$entriesValues =$this->getResults(
$this->getEntryValueGenerator()
->getPreparedSql()
);
$entryValuesPrepared = $this->collectEntryValues($entriesValues);
$results[] = [
'entry' => $entry,
'values' => $entryValuesPrepared
];
}
return $results;
}
/**
* Collect entry values as Caldera_Forms_Entry_Field objects
*
* @param \stdClass[] $entriesValues
* @return array
*/
private function collectEntryValues($entriesValues): array
{
$entryValuesPrepared = [];
if (!empty($entriesValues)) {
foreach ($entriesValues as $entryValue) {
$entryValuesPrepared[] = new \Caldera_Forms_Entry_Field($entryValue);
}
}
return $entryValuesPrepared;
}