GithubHelp home page GithubHelp logo

kcristiano / caldera-forms-query Goto Github PK

View Code? Open in Web Editor NEW

This project forked from calderawp/caldera-forms-query

0.0 2.0 0.0 123 KB

Query builder tool for Caldera Forms

JavaScript 1.19% PHP 89.20% Shell 9.61%

caldera-forms-query's Introduction

Build Status

This library provides for developer-friendly ways to query for or delete Caldera Forms entry data.

Why?

Install

composer require calderawp/caldera-forms-query

Requires

  • WordPress - tested with 4.8, latest and trunk
  • PHP 5.6+ - tested with PHP 7.1 and 7.2
  • Caldera Forms 1.6.0+ - tested with Caldera Forms 1.6.1 beta 1

Status

  • Works
  • Does not yet select/delete by date range
  • Prepared SQL needs to be sanitized better.

Usage

Basic Queries

/**
 * Examples of simple queries
 *
 * Using the class: \calderawp\CalderaFormsQuery\Features\FeatureContainer
 * Via the static accessor function: calderawp\CalderaFormsQueries\CalderaFormsQueries()
 */

/** First make the function usable without a full namespace */
use function calderawp\CalderaFormsQueries\CalderaFormsQueries;

/** Do Some Queries */
//Select all data by user ID
$entries = CalderaFormsQueries()->selectByUserId(42);

//Select all entries that have a field whose slug is "email" and the value of that field's value is "[email protected]"
$entries = CalderaFormsQueries()->selectByFieldValue( 'email', '[email protected]' );

//Select all entries that do not have field whose slug is "size" and the value of that field's value is "big"
$entries = CalderaFormsQueries()->selectByFieldValue( 'size', 'big', false );

//Delete all data by Entry ID
CalderaFormsQueries()->deleteByEntryIds([1,1,2,3,5,8,42]);

//Delete all data by User ID
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.

/**
 * Examples of simple queries
 *
 * Using the class: \calderawp\CalderaFormsQuery\Features\FeatureContainer
 * Via the static accessor function: calderawp\CalderaFormsQueries\CalderaFormsQueries()
 */

/** First make the function usable without a full namespace */
use function calderawp\CalderaFormsQueries\CalderaFormsQueries;

/** Do Some Queries */
//Select all entries that have a field whose slug is "email" and the value of that field's value is "[email protected]"
//The first 25 entries
$entries = CalderaFormsQueries()->selectByFieldValue( 'email', '[email protected]' );
//The second 25 entries
$entries = CalderaFormsQueries()->selectByFieldValue( 'email', '[email protected]', true, 2 );
//Get 5th page, with 50 results per page
$entries = CalderaFormsQueries()->selectByFieldValue( 'email', '[email protected]', true, 5, 50 );

Constructing Other Queries

The feature container provides helper methods that allow for simple queries like those listed above. It also exposes the underlying query generators.

You can access any of the generators using the getQueries() method. For example:

 $featureContainer = \calderawp\CalderaFormsQueries\CalderaFormsQueries();
    $fieldValue = '[email protected]';
    $formId = 'CF5afb00e97d698';
    $count = Caldera_Forms_Entry_Bulk::count($formId );

    $entrySelector = $featureContainer
        ->getQueries()
        ->entrySelect();

is() Helper Method

This is a more complete example showing a selection of entry values where the field with the slug primary_email is [email protected] and the field with the slug of first_name is Mike. It is also using the is() method to add WHERE statements, as well as the addPagination() method to query for the second page of results with 50 results per page.

    $featureContainer = \calderawp\CalderaFormsQueries\CalderaFormsQueries();
    $entrySelector = $featureContainer
        ->getQueries()
        ->entrySelect()
        ->is( 'primary_email', '[email protected]' )
        ->is( 'first_name', 'Mike' )
        ->addPagination(2,50 );

in() Helper Method

This example shows selection of all entry values where the entry ID is in an array of entry IDs.

    $featureContainer = \calderawp\CalderaFormsQueries\CalderaFormsQueries();
    $entrySelector = $featureContainer
        ->getQueries()
        ->entrySelect()
        ->in( 'entry_id', [ 42, 3 ] );

Query Generators

All query generators extend the \calderawp\CalderaFormsQuery\QueryBuilder class and impairment \calderawp\CalderaFormsQuery\CreatesSqlQueries.

Query generators are responsible for creating SQL queries. They do not perform sequel queries.

Select Query Generators

Select query generators extend \calderawp\CalderaFormsQuery\Select\SelectQueryBuilder and impliment \calderawp\CalderaFormsQuery\Select\DoesSelectQuery and \calderawp\CalderaFormsQuery\Select\DoesSelectQueryByEntryId.

Useful Methods of SelectQueryBuilders

  • in()

Using Query Generators To Perform SQL Queries

SELECT

The getQueries() method of the FeatureContainer returns a calderawp\CalderaFormsQuery\Features\Queries instance. This provides us with a select method when passed a SelectQueryBuilder returns an array of stdClass object of results.

        $featureContainer = \calderawp\CalderaFormsQueries\CalderaFormsQueries();
        $entryValueSelect = $featureContainer
            ->getQueries()
            ->entryValuesSelect()
            ->is( 'size', 'large' );

       $featureContainer->getQueries()->select( $entryValueSelect );

You can also access the generated SQL as a string.

  $featureContainer = \calderawp\CalderaFormsQueries\CalderaFormsQueries();
        $sql = $featureContainer
            ->getQueries()
            ->entryValuesSelect()
            ->is( 'size', 'large' )
            ->getPreparedSql();

DELETE

The Queries class also has a delete method we can pass a DeleteQueryBuilder to perform a DELETE query against the database.

Development

Install

Requires git and Composer

  • git clone [email protected]:calderawp/caldera-forms-query.git
  • cd caldera-forms-query
  • composer install

Local Development Environment

A local development environment is included, and provided. It is used for integration tests. Requires Composer, Docker and Docker Compose.

  • Install Local Environment And WordPress "Unit" Test Suite
  • composer wp-install

You should know have WordPress at http://localhost:8888/

  • (re)Start Server: Once server is installed, you can start it again
  • composer wp-start

Testing

Install

Follow the steps above to create local development environment, then you can use the commands listed in the next section.

Use

Run these commands from the plugin's root directory.

  • Run All Tests and Code Sniffs and Fixes
    • composer tests
  • Run Unit Tests
    • composer unit-tests
  • Run WordPress Integration Tests
    • composer wp-tests
  • Fix All Code Formatting
    • composer formatting

WordPress and Caldera Forms Dependency

For now, this library is dependent on Caldera Forms and WordPress (for \WPDB.) This will change, possibly with breaking changes, when caldera-interop is integrated with this tool.

Stuff.

Copyright 2018 CalderaWP LLC. License: GPL v2 or later.

caldera-forms-query's People

Contributors

shelob9 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.