Skip to content
Snippets Groups Projects
Commit 527d79ed authored by Josh Pollock's avatar Josh Pollock
Browse files

select by form id

parent 9fa2016d
No related branches found
No related tags found
No related merge requests found
......@@ -49,6 +49,7 @@ CalderaFormsQueries()->deleteByUserId(42);
```
## Development
### Install
Requires git and Composer
......
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "736a984915e9e1f970336e5a8e5b38eb",
"content-hash": "66049b1b98fbc3be0172f1d471393cb3",
"packages": [
{
"name": "calderawp/caldera-containers",
......@@ -273,7 +273,7 @@
"version": "dev-feature/2404",
"source": {
"type": "git",
"url": "git@github.com:CalderaWP/Caldera-Forms.git",
"url": "https://github.com/CalderaWP/Caldera-Forms",
"reference": "c4c2d1db6042c82f2229c89ed7140c8b214c9c8e"
},
"type": "wordpress-plugin",
......@@ -819,23 +819,23 @@
},
{
"name": "phpspec/prophecy",
"version": "1.7.5",
"version": "1.7.6",
"source": {
"type": "git",
"url": "https://github.com/phpspec/prophecy.git",
"reference": "dfd6be44111a7c41c2e884a336cc4f461b3b2401"
"reference": "33a7e3c4fda54e912ff6338c48823bd5c0f0b712"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpspec/prophecy/zipball/dfd6be44111a7c41c2e884a336cc4f461b3b2401",
"reference": "dfd6be44111a7c41c2e884a336cc4f461b3b2401",
"url": "https://api.github.com/repos/phpspec/prophecy/zipball/33a7e3c4fda54e912ff6338c48823bd5c0f0b712",
"reference": "33a7e3c4fda54e912ff6338c48823bd5c0f0b712",
"shasum": ""
},
"require": {
"doctrine/instantiator": "^1.0.2",
"php": "^5.3|^7.0",
"phpdocumentor/reflection-docblock": "^2.0|^3.0.2|^4.0",
"sebastian/comparator": "^1.1|^2.0",
"sebastian/comparator": "^1.1|^2.0|^3.0",
"sebastian/recursion-context": "^1.0|^2.0|^3.0"
},
"require-dev": {
......@@ -878,20 +878,20 @@
"spy",
"stub"
],
"time": "2018-02-19T10:16:54+00:00"
"time": "2018-04-18T13:57:24+00:00"
},
{
"name": "phpunit/php-code-coverage",
"version": "6.0.1",
"version": "6.0.4",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/php-code-coverage.git",
"reference": "f8ca4b604baf23dab89d87773c28cc07405189ba"
"reference": "52187754b0eed0b8159f62a6fa30073327e8c2ca"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/f8ca4b604baf23dab89d87773c28cc07405189ba",
"reference": "f8ca4b604baf23dab89d87773c28cc07405189ba",
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/52187754b0eed0b8159f62a6fa30073327e8c2ca",
"reference": "52187754b0eed0b8159f62a6fa30073327e8c2ca",
"shasum": ""
},
"require": {
......@@ -902,7 +902,7 @@
"phpunit/php-text-template": "^1.2.1",
"phpunit/php-token-stream": "^3.0",
"sebastian/code-unit-reverse-lookup": "^1.0.1",
"sebastian/environment": "^3.0",
"sebastian/environment": "^3.1",
"sebastian/version": "^2.0.1",
"theseer/tokenizer": "^1.1"
},
......@@ -941,7 +941,7 @@
"testing",
"xunit"
],
"time": "2018-02-02T07:01:41+00:00"
"time": "2018-04-29T14:59:09+00:00"
},
{
"name": "phpunit/php-file-iterator",
......@@ -1131,16 +1131,16 @@
},
{
"name": "phpunit/phpunit",
"version": "7.0.3",
"version": "7.1.5",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "536f4d853c12d8189963435088e8ff7c0daeab2e"
"reference": "ca64dba53b88aba6af32aebc6b388068db95c435"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/536f4d853c12d8189963435088e8ff7c0daeab2e",
"reference": "536f4d853c12d8189963435088e8ff7c0daeab2e",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/ca64dba53b88aba6af32aebc6b388068db95c435",
"reference": "ca64dba53b88aba6af32aebc6b388068db95c435",
"shasum": ""
},
"require": {
......@@ -1158,8 +1158,8 @@
"phpunit/php-file-iterator": "^1.4.3",
"phpunit/php-text-template": "^1.2.1",
"phpunit/php-timer": "^2.0",
"phpunit/phpunit-mock-objects": "^6.0",
"sebastian/comparator": "^2.1",
"phpunit/phpunit-mock-objects": "^6.1.1",
"sebastian/comparator": "^3.0",
"sebastian/diff": "^3.0",
"sebastian/environment": "^3.1",
"sebastian/exporter": "^3.1",
......@@ -1181,7 +1181,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "7.0-dev"
"dev-master": "7.1-dev"
}
},
"autoload": {
......@@ -1207,20 +1207,20 @@
"testing",
"xunit"
],
"time": "2018-03-26T07:36:48+00:00"
"time": "2018-04-29T15:09:19+00:00"
},
{
"name": "phpunit/phpunit-mock-objects",
"version": "6.0.1",
"version": "6.1.1",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git",
"reference": "e3249dedc2d99259ccae6affbc2684eac37c2e53"
"reference": "70c740bde8fd9ea9ea295be1cd875dd7b267e157"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/e3249dedc2d99259ccae6affbc2684eac37c2e53",
"reference": "e3249dedc2d99259ccae6affbc2684eac37c2e53",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/70c740bde8fd9ea9ea295be1cd875dd7b267e157",
"reference": "70c740bde8fd9ea9ea295be1cd875dd7b267e157",
"shasum": ""
},
"require": {
......@@ -1238,7 +1238,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "6.0.x-dev"
"dev-master": "6.1-dev"
}
},
"autoload": {
......@@ -1263,7 +1263,7 @@
"mock",
"xunit"
],
"time": "2018-02-15T05:27:38+00:00"
"time": "2018-04-11T04:50:36+00:00"
},
{
"name": "sebastian/code-unit-reverse-lookup",
......@@ -1312,30 +1312,30 @@
},
{
"name": "sebastian/comparator",
"version": "2.1.3",
"version": "3.0.0",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/comparator.git",
"reference": "34369daee48eafb2651bea869b4b15d75ccc35f9"
"reference": "ed5fd2281113729f1ebcc64d101ad66028aeb3d5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/34369daee48eafb2651bea869b4b15d75ccc35f9",
"reference": "34369daee48eafb2651bea869b4b15d75ccc35f9",
"url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/ed5fd2281113729f1ebcc64d101ad66028aeb3d5",
"reference": "ed5fd2281113729f1ebcc64d101ad66028aeb3d5",
"shasum": ""
},
"require": {
"php": "^7.0",
"sebastian/diff": "^2.0 || ^3.0",
"php": "^7.1",
"sebastian/diff": "^3.0",
"sebastian/exporter": "^3.1"
},
"require-dev": {
"phpunit/phpunit": "^6.4"
"phpunit/phpunit": "^7.1"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.1.x-dev"
"dev-master": "3.0-dev"
}
},
"autoload": {
......@@ -1372,7 +1372,7 @@
"compare",
"equality"
],
"time": "2018-02-01T13:46:46+00:00"
"time": "2018-04-18T13:33:00+00:00"
},
{
"name": "sebastian/diff",
......
......@@ -20,297 +20,319 @@ use calderawp\CalderaFormsQuery\SelectQueries;
class FeatureContainer extends Container
{
/**
* @var ServiceContainer
*/
protected $serviceContainer;
/**
* @var \wpdb
*/
protected $wpdb;
/**
* @var ServiceContainer
*/
protected $serviceContainer;
/**
* @var \wpdb
*/
protected $wpdb;
/**
* FeatureContainer constructor.
* @param ServiceContainer $serviceContainer
* @param \wpdb $wpdb
*/
public function __construct(ServiceContainer $serviceContainer, \wpdb $wpdb)
{
$this->serviceContainer = $serviceContainer;
$this->wpdb = $wpdb;
$this->bindServices();
}
/**
* FeatureContainer constructor.
* @param ServiceContainer $serviceContainer
* @param \wpdb $wpdb
*/
public function __construct(ServiceContainer $serviceContainer, \wpdb $wpdb)
{
$this->serviceContainer = $serviceContainer;
$this->wpdb = $wpdb;
$this->bindServices();
}
/**
* Bind services to service container
*/
protected function bindServices()
{
//@TODO move these to service provider classes
$this->serviceContainer->singleton(MySqlBuilder::class, function () {
return new MySqlBuilder();
});
/**
* Bind services to service container
*/
protected function bindServices()
{
//@TODO move these to service provider classes
$this->serviceContainer->singleton(MySqlBuilder::class, function () {
return new MySqlBuilder();
});
$this->serviceContainer->bind(SelectQueries::class, function () {
//@TODO Factory
return new SelectQueries(
new EntrySelect(
$this->getBuilder(),
$this->entryTableName()
),
new EntryValueSelect(
$this->getBuilder(),
$this->entryValueTableName()
),
$this->wpdb
);
});
$this->serviceContainer->bind(SelectQueries::class, function () {
//@TODO Factory
return new SelectQueries(
new EntrySelect(
$this->getBuilder(),
$this->entryTableName()
),
new EntryValueSelect(
$this->getBuilder(),
$this->entryValueTableName()
),
$this->wpdb
);
});
$this->serviceContainer->bind(DeleteQueries::class, function () {
//@TODO Factory
return new DeleteQueries(
new EntryDelete(
$this->getBuilder(),
$this->entryTableName()
),
new EntryValuesDelete(
$this->getBuilder(),
$this->entryValueTableName()
),
$this->wpdb
);
});
$this->serviceContainer->bind(DeleteQueries::class, function () {
//@TODO Factory
return new DeleteQueries(
new EntryDelete(
$this->getBuilder(),
$this->entryTableName()
),
new EntryValuesDelete(
$this->getBuilder(),
$this->entryValueTableName()
),
$this->wpdb
);
});
$this->serviceContainer->singleton(Queries::class, function () {
return new Queries(
$this
->serviceContainer
->make(SelectQueries::class),
$this
->serviceContainer
->make(DeleteQueries::class)
);
});
}
$this->serviceContainer->singleton(Queries::class, function () {
return new Queries(
$this
->serviceContainer
->make(SelectQueries::class),
$this
->serviceContainer
->make(DeleteQueries::class)
);
});
}
/**
* Get MySQL builder
*
* @return MySqlBuilder
*/
public function getBuilder()
{
return $this
->serviceContainer
->make(MySqlBuilder::class);
}
/**
* Get MySQL builder
*
* @return MySqlBuilder
*/
public function getBuilder()
{
return $this
->serviceContainer
->make(MySqlBuilder::class);
}
/**
* Get query runner
*
* @return Queries
*/
public function getQueries()
{
return $this
->serviceContainer
->make(Queries::class);
}
/**
* Get query runner
*
* @return Queries
*/
public function getQueries()
{
return $this
->serviceContainer
->make(Queries::class);
}
/**
* Select all entries and entry values by user ID
*
* @param int $userId
* @return array
*/
public function selectByUserId($userId)
{
$query = $this
->getQueries()
->entrySelect()
->queryByUserId($userId);
return $this->collectResults($this->select($query));
}
/**
* Select all entries and entry values by user ID
*
* @param int $userId
* @return array
*/
public function selectByUserId($userId)
{
$query = $this
->getQueries()
->entrySelect()
->queryByUserId($userId);
return $this->collectResults($this->select($query));
}
/**
* Find all entries that have or do not have field with a slug and value
*
* @param string $fieldSlug Field slug
* @param string $fieldValue Field value
* @param bool $have Optional. Default: true. If true query is for fields with this value
*
* @return array
*/
public function selectByFieldValue($fieldSlug, $fieldValue, $have = true)
{
$type = $have ? 'equals' : 'notEquals';
$queryForEntryValues = $this
->getQueries()
->entryValuesSelect()
->queryByFieldValue($fieldSlug, $fieldValue, $type, 'AND', [
'entry_id'
]);
$results = $this->select($queryForEntryValues);
if (empty($results) || 0 >= count($results)) {
return [];
}
$results = $this->reduceResultsToEntryId($results);
/**
* Select entries by form ID
*
* @param string $formId ID of form to select
*
* @param bool $addValues Optional Add entry values? Default is true.
* @return array
*/
public function selectByFormId($formId, $addValues = true )
{
$query = $this
->getQueries()
->entrySelect()
->queryByFormsId($formId);
return $this->collectResults($this->select($query), $addValues);
$queryForValues = $this
->getQueries()
->entrySelect()
->queryByEntryIds($results);
}
return $this->collectResults($this->select($queryForValues));
}
/**
* Find all entries that have or do not have field with a slug and value
*
* @param string $fieldSlug Field slug
* @param string $fieldValue Field value
* @param bool $have Optional. Default: true. If true query is for fields with this value
*
* @return array
*/
public function selectByFieldValue($fieldSlug, $fieldValue, $have = true)
{
$type = $have ? 'equals' : 'notEquals';
$queryForEntryValues = $this
->getQueries()
->entryValuesSelect()
->queryByFieldValue($fieldSlug, $fieldValue, $type, 'AND', [
'entry_id'
]);
$results = $this->select($queryForEntryValues);
if (empty($results) || 0 >= count($results)) {
return [];
}
$results = $this->reduceResultsToEntryId($results);
/**
* Delete all entry data, including field values for a collection of entries
*
* @param array $entryIds Entry Ids to delete
* @return $this
*/
public function deleteByEntryIds(array $entryIds)
{
$this->delete(
$this
->getQueries()
->entryDelete()
->deleteByEntryIds($entryIds)
);
$this->delete(
$this->getQueries()
->entryValueDelete()
->deleteByEntryIds($entryIds)
);
$queryForValues = $this
->getQueries()
->entrySelect()
->queryByEntryIds($results);
return $this;
}
return $this->collectResults($this->select($queryForValues));
}
/**
* Delete all entries and entry values by user ID
*
* @param int $userId
*/
public function deleteByUserId($userId)
{
$entries = $this->select(
$this
->getQueries()
->entrySelect()
->queryByUserId($userId)
);
if (!empty($entries)) {
$ids = $this->reduceResultsToEntryId($entries, 'id');
$this->delete(
$this
->getQueries()
->entryDelete()
->deleteByEntryIds($ids)
);
$this->delete(
$this
->getQueries()
->entryValueDelete()
->deleteByEntryIds($ids)
);
}
}
/**
* Delete all entry data, including field values for a collection of entries
*
* @param array $entryIds Entry Ids to delete
* @return $this
*/
public function deleteByEntryIds(array $entryIds)
{
$this->delete(
$this
->getQueries()
->entryDelete()
->deleteByEntryIds($entryIds)
);
$this->delete(
$this->getQueries()
->entryValueDelete()
->deleteByEntryIds($entryIds)
);
/**
* @return string
*/
protected function entryValueTableName()
{
return "{$this->wpdb->prefix}cf_form_entry_values";
}
return $this;
}
/**
* @return string
*/
protected function entryTableName()
{
return "{$this->wpdb->prefix}cf_form_entries";
}
/**
* Delete all entries and entry values by user ID
*
* @param int $userId
*/
public function deleteByUserId($userId)
{
$entries = $this->select(
$this
->getQueries()
->entrySelect()
->queryByUserId($userId)
);
if (!empty($entries)) {
$ids = $this->reduceResultsToEntryId($entries, 'id');
$this->delete(
$this
->getQueries()
->entryDelete()
->deleteByEntryIds($ids)
);
$this->delete(
$this
->getQueries()
->entryValueDelete()
->deleteByEntryIds($ids)
);
}
}
/**
* 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) {
$entry = new \Caldera_Forms_Entry_Entry($entry);
$query = $this
->getQueries()
->entryValuesSelect()
->queryByEntryId($entry->id);
$entriesValues = $this->select($query);
/**
* @return string
*/
protected function entryValueTableName()
{
return "{$this->wpdb->prefix}cf_form_entry_values";
}
$entryValuesPrepared = $this->collectEntryValues($entriesValues);
$results[] = [
'entry' => $entry,
'values' => $entryValuesPrepared
];
}
return $results;
}
/**
* @return string
*/
protected function entryTableName()
{
return "{$this->wpdb->prefix}cf_form_entries";
}
/**
* 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;
}
/**
* Collect results using Caldera_Forms_Entry_Entry and Caldera_Forms_Entry_Field to represent values
*
* @param \stdClass[] $entriesValues
* @param bool $addValues Optional Add entry values? Default is true.
* @return array
*/
private function collectResults($entriesValues, $addValues = true)
{
$results = [];
foreach ($entriesValues as $entry) {
$entry = new \Caldera_Forms_Entry_Entry($entry);
$query = $this
->getQueries()
->entryValuesSelect()
->queryByEntryId($entry->id);
/**
* Do a select query
*
* @param SelectQueryBuilder $query
* @return \stdClass[]
*/
private function select(SelectQueryBuilder $query)
{
return $this
->getQueries()
->select($query);
}
$entryValuesPrepared = [];
if ($addValues) {
$entriesValues = $this->select($query);
$entryValuesPrepared = $this->collectEntryValues($entriesValues);
}
$results[] = [
'entry' => $entry,
'values' => $entryValuesPrepared
];
}
return $results;
}
/**
* Do a delete query
*
* @param DeleteQueryBuilder $query
* @return \stdClass[]
*/
private function delete(DeleteQueryBuilder $query)
{
return $this->
getQueries()
->delete($query);
}
/**
* 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;
}
/**
* @param $results
* @return array
*/
private function reduceResultsToEntryId($results, $colum = 'entry_id')
{
foreach ($results as &$result) {
$result = $result->$colum;
}
return $results;
}
/**
* Do a select query
*
* @param SelectQueryBuilder $query
* @return \stdClass[]
*/
private function select(SelectQueryBuilder $query)
{
return $this
->getQueries()
->select($query);
}
/**
* Do a delete query
*
* @param DeleteQueryBuilder $query
* @return \stdClass[]
*/
private function delete(DeleteQueryBuilder $query)
{
return $this->
getQueries()
->delete($query);
}
/**
* @param $results
* @return array
*/
private function reduceResultsToEntryId($results, $colum = 'entry_id')
{
foreach ($results as &$result) {
$result = $result->$colum;
}
return $results;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment