| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Controllers\Ajax; |
| 4 |
|
| 5 |
|
| 6 |
use Averta\Core\Utility\Arr; |
| 7 |
use Averta\WordPress\Utility\JSON; |
| 8 |
use Depicter\Rules\Conditions\ListConditions; |
| 9 |
use Depicter\Utility\Sanitize; |
| 10 |
use WPEmerge\Requests\RequestInterface; |
| 11 |
|
| 12 |
class RulesAjaxController { |
| 13 |
|
| 14 |
/** |
| 15 |
* Store rules data |
| 16 |
* |
| 17 |
* @param RequestInterface $request |
| 18 |
* @param $view |
| 19 |
* |
| 20 |
* @return \Psr\Http\Message\ResponseInterface |
| 21 |
*/ |
| 22 |
public function store( RequestInterface $request, $view ) { |
| 23 |
$id = Sanitize::int( $request->body( 'ID', '' ) ); |
| 24 |
$content = Sanitize::textfield( $request->body( 'content', '' ) ); |
| 25 |
|
| 26 |
if ( empty( $content ) || empty( $id ) || ! JSON::isJson( $content ) ) { |
| 27 |
return \Depicter::json([ |
| 28 |
'errors' => [ __( 'Both id and content are required.', 'depicter' ) ] |
| 29 |
])->withStatus(400); |
| 30 |
} |
| 31 |
|
| 32 |
\Depicter::metaRepository()->update( $id, 'rules', $content ); |
| 33 |
|
| 34 |
do_action( 'depicter/rules/after/store', $id, $content, $request ); |
| 35 |
|
| 36 |
return \Depicter::json( [ 'success' => true ] )->withStatus( 200 ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Show stored rules for provided document ID |
| 41 |
* |
| 42 |
* @param RequestInterface $request |
| 43 |
* @param $view |
| 44 |
* |
| 45 |
* @return \Psr\Http\Message\ResponseInterface |
| 46 |
*/ |
| 47 |
public function show( RequestInterface $request, $view ) { |
| 48 |
$id = Sanitize::int( $request->query( 'ID', '' ) ); |
| 49 |
|
| 50 |
if ( empty( $id ) ) { |
| 51 |
return \Depicter::json([ |
| 52 |
'errors' => [ __( 'Document id is required.', 'depicter' ) ] |
| 53 |
])->withStatus(400); |
| 54 |
} |
| 55 |
|
| 56 |
$result = [ |
| 57 |
'displayRules' => \Depicter::document()->displayRules( $id )->get(), |
| 58 |
'conditions' => \Depicter::conditions()->toArray( true ) |
| 59 |
]; |
| 60 |
|
| 61 |
return \Depicter::json( $result )->withStatus( 200 ); |
| 62 |
} |
| 63 |
|
| 64 |
public function all( RequestInterface $request, $view ) { |
| 65 |
// TODO - check following samples |
| 66 |
|
| 67 |
// $all = \Depicter::conditions()->all(); // get all defined conditions instances |
| 68 |
// $all = \Depicter::conditions()->toArray(); // get all defined conditions in array |
| 69 |
// $all = \Depicter::conditions()->toArray( true ); // get all defined conditions in groups |
| 70 |
// $all = \Depicter::conditions()->find('WordPress_Post')->getQueryResults(); // get query results for a condition |
| 71 |
// $all = \Depicter::conditions()->find('WordPress_Page')->getQueryResults(); // get query results for a condition |
| 72 |
|
| 73 |
// $all = \Depicter::document()->displayRules(18)->raw(); // get raw rules for a document by ID |
| 74 |
// $all = \Depicter::document()->displayRules(18)->toArray(); // get rules in array for a document by ID |
| 75 |
// $all = \Depicter::document()->displayRules(18)->get(); // get rules in object format for a document by ID |
| 76 |
|
| 77 |
// $all = \Depicter::document()->displayRules(18)->displayConditions; // get displayConditions for a document by ID in array format |
| 78 |
// $all = \Depicter::document()->displayRules(18)->displayConditionsSlim; // get displayConditions for a document by ID without redundant group and condition IDs |
| 79 |
|
| 80 |
// $all = \Depicter::document()->displayRules(18)->displayConditions()->all(); // get all instances of displayConditions for a document |
| 81 |
// $all = \Depicter::document()->displayRules(18)->displayConditions()->groups(); // get all instances of displayConditionGroups for a document |
| 82 |
// $all = \Depicter::document()->displayRules(18)->displayConditions()->areMet(); // check if displayConditions for a document are met or not |
| 83 |
|
| 84 |
$all = \Depicter::document()->getConditionalDocumentIDs(); |
| 85 |
|
| 86 |
return \Depicter::json( $all )->withStatus( 200 ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* List condition values for given query |
| 91 |
* |
| 92 |
* @param RequestInterface $request |
| 93 |
* @param $view |
| 94 |
* |
| 95 |
* @return \Psr\Http\Message\ResponseInterface |
| 96 |
*/ |
| 97 |
public function conditionValues( RequestInterface $request, $view ) { |
| 98 |
$query = Sanitize::textfield( $request->query( 'query', '' ) ); |
| 99 |
|
| 100 |
if ( empty( $query ) ) { |
| 101 |
return \Depicter::json([ |
| 102 |
'errors' => [ __( 'query is required.', 'depicter' ) ] |
| 103 |
])->withStatus(400); |
| 104 |
} |
| 105 |
|
| 106 |
try{ |
| 107 |
// return \Depicter::json( \Depicter::conditionsManager()->getConditionOptions( $query ) )->withStatus(200); |
| 108 |
return \Depicter::json( \Depicter::conditions()->find( $query )->getQueryResults() )->withStatus(200); |
| 109 |
} catch( \Exception $exception ) { |
| 110 |
return \Depicter::json([ |
| 111 |
'errors' => [ $exception->getMessage() ] |
| 112 |
])->withStatus(400); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|