diff --git a/README.md b/README.md index 373371a0a663c5f80d605014ebe35bb8fed7b6a7..993b6b48e0a9d76c2f9281dd1acddfef8464a02b 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,6 @@ CalderaFormsQueries()->deleteByEntryIds([1,1,2,3,5,8,42]); CalderaFormsQueries()->deleteByUserId(42); ``` - ### Paginated Queries The selectByFieldValue feature method defaults to limiting queries to 25. You can set the page and limit with the 4th & 5th arguments. ```php @@ -72,12 +71,9 @@ $entries = CalderaFormsQueries()->selectByFieldValue( 'email', 'delete@please.eu $entries = CalderaFormsQueries()->selectByFieldValue( 'email', 'delete@please.eu', true, 2 ); //Get 5th page, with 50 results per page $entries = CalderaFormsQueries()->selectByFieldValue( 'email', 'delete@please.eu', true, 5, 50 ); - - - - ``` + ## Development ### Install Requires git and Composer diff --git a/src/Features/FeatureContainer.php b/src/Features/FeatureContainer.php index a6f47d49e5bed2bf6dbfaa6d4a5d1d3ca47cf952..a979f2adc479dfbaf526ec153c213eac1445c597 100644 --- a/src/Features/FeatureContainer.php +++ b/src/Features/FeatureContainer.php @@ -20,118 +20,119 @@ 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)); + } +<<<<<<< HEAD /** * Find all entries that have or do not have field with a slug and value * @@ -162,157 +163,209 @@ class FeatureContainer extends Container ->entrySelect() ->addPagination($page,$limit) ->queryByEntryIds($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); - return $this->collectResults($this->select($queryForValues)); - } + } +>>>>>>> 527d79ed169861a0db38e730e1ed8f374b533d04 - /** - * 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) - ); + /** + * 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); - return $this; - } + $queryForValues = $this + ->getQueries() + ->entrySelect() + ->queryByEntryIds($results); - /** - * 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) - ); - } - } + return $this->collectResults($this->select($queryForValues)); + } - /** - * @return string - */ - protected function entryValueTableName() - { - return "{$this->wpdb->prefix}cf_form_entry_values"; - } + /** + * 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 entryTableName() - { - return "{$this->wpdb->prefix}cf_form_entries"; - } + return $this; + } - /** - * 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); + /** + * 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) + ); + } + } - $entryValuesPrepared = $this->collectEntryValues($entriesValues); - $results[] = [ - 'entry' => $entry, - 'values' => $entryValuesPrepared - ]; - } - return $results; - } + /** + * @return string + */ + protected function entryValueTableName() + { + return "{$this->wpdb->prefix}cf_form_entry_values"; + } - /** - * 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; - } + /** + * @return string + */ + protected function entryTableName() + { + return "{$this->wpdb->prefix}cf_form_entries"; + } - /** - * Do a select query - * - * @param SelectQueryBuilder $query - * @return \stdClass[] - */ - private function select(SelectQueryBuilder $query) - { - return $this - ->getQueries() - ->select($query); - } + /** + * 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 delete query - * - * @param DeleteQueryBuilder $query - * @return \stdClass[] - */ - private function delete(DeleteQueryBuilder $query) - { - return $this-> - getQueries() - ->delete($query); - } + $entryValuesPrepared = []; + if ($addValues) { + $entriesValues = $this->select($query); + $entryValuesPrepared = $this->collectEntryValues($entriesValues); + } + $results[] = [ + 'entry' => $entry, + 'values' => $entryValuesPrepared + ]; + } + return $results; + } - /** - * @param $results - * @return array - */ - private function reduceResultsToEntryId($results, $colum = 'entry_id') - { - foreach ($results as &$result) { - $result = $result->$colum; - } - 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; + } + + /** + * 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; + } }