Skip to content

Commit

Permalink
Modify folder structure to match namespaces
Browse files Browse the repository at this point in the history
* Move JSONText namespace to 'PhpTek\JSONText\ORM\FieldType' to align with "silverstripe/framework" DBString namespace (SilverStripe\ORM\FieldType)
  • Loading branch information
Jackson committed Mar 27, 2018
1 parent 98220cf commit 5d08cb7
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 165 deletions.
4 changes: 2 additions & 2 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Name: 'jsontextconfig'

Injector:
JSONText:
class: \phptek\JSONText\Fields\JSONText
class: \phptek\JSONText\ORM\FieldType\JSONText
backend: postgres
JSONTextExtension:
class: \phptek\JSONText\Extensions\JSONTextExtension
class: \phptek\JSONText\Extensions\JSONTextExtension
10 changes: 5 additions & 5 deletions code/backends/JSONBackend.php → code/Backend/JSONBackend.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* DB backend for use with the {@link JSONText} DB field. Allows us to use DB-specific JSON query syntax within
* the module.
*
*
* @package silverstripe-jsontext
* @subpackage models
* @author Russell Michell <[email protected]>
Expand All @@ -14,13 +14,13 @@
namespace PhpTek\JSONText\Backend;

use PhpTek\JSONText\Exceptions\JSONTextInvalidArgsException;
use PhpTek\JSONText\Fields\JSONText;
use PhpTek\JSONText\ORM\FieldType\JSONText;
use SilverStripe\Core\Config\Configurable;

abstract class JSONBackend
{
use Configurable;

/**
* @var string
*/
Expand All @@ -42,7 +42,7 @@ public function __construct($operand, $jsonText)
$this->operand = $operand;
$this->jsonText = $jsonText;
}

/**
* Match on keys by INT. If >1 matches are found, an indexed array of all matches is returned.
*
Expand Down Expand Up @@ -89,5 +89,5 @@ public function matchOnExpr()

return $fetch;
}

}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
/**
* This {@link DataExtension} allows you to declare arbitrary input fields in
* your getCMSFields() methods without them ever going near an equivalent DBField.
*
*
* The SilverStripe default gives you one DBField for every input field declared
* in getCMSFields(). This extension however allows you to use a single blob
* of JSON, stored in a single {@link JSONTextField} and manage each key=>value pair
* from individual form input fields, without needing to declare equivalent or further
* database fields. All you need to do is add a `$json_field_map` config static to
* your model (See below).
*
* Notes:
*
* Notes:
* - Untested on non CHAR DB field-types (v0.8)
* - Only works for single dimensional JSON data
*
*
* <code>
* private static $db = [
* 'TestJSON' => JSONText::class,
* ];
*
*
* private static $json_field_map = [
* 'TestJSON' => ['TestField1', 'TestField2']
* ];
*
*
* public function getCMSFields()
* {
* $fields = parent::getCMSFields();
Expand All @@ -32,7 +32,7 @@
* TextField::create('TestField2', 'Test 2'),
* TextField::create('TestJSON', 'Some JSON') // Uses a TextField for demo, normally this would be hidden from CMS users
* ]);
*
*
* return $fields;
* }
* </code>
Expand All @@ -52,23 +52,23 @@
throw new JSONTextException($msg);
}

use PhpTek\JSONText\Field\JSONText;
use PhpTek\JSONText\ORM\FieldType\JSONText;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Control\Controller;
use SilverStripe\CMS\Controllers\CMSPageEditController;

class JSONTextExtension extends DataExtension
{
{
/**
* Pre-process incoming CMS POST data, and modify any available {@link JSONText}
* data for presentation in "headless" input fields.
*
*
* @return null
*/
public function onBeforeWrite()
{
parent::onBeforeWrite();

$owner = $this->getOwner();
$controller = Controller::curr();
$postVars = $controller->getRequest()->postVars();
Expand All @@ -78,24 +78,24 @@ public function onBeforeWrite()
!empty($fieldMap) &&
in_array(get_class($controller), [CMSPageEditController::class])
);

if (!$doUpdate) {
return null;
}

// Could also use DataObject::getSchema()->fieldSpecs()
foreach ($owner->config()->get('db') as $field => $type) {
if ($type === JSONText::class) {
$this->updateJSON($postVars, $owner);
}
}
}

/**
* Called from {@link $this->onBeforeWrote()}. Inserts or updates each available
* JSONText DB field with the appropriate input-field data, as per the model's
* JSONText DB field with the appropriate input-field data, as per the model's
* "json_field_map" config static.
*
*
* @param array $postVars
* @param DataObject $owner
* @return void
Expand All @@ -104,54 +104,54 @@ public function onBeforeWrite()
public function updateJSON(array $postVars, $owner)
{
$jsonFieldMap = $owner->config()->get('json_field_map');

foreach ($jsonFieldMap as $jsonField => $mappedFields) {
$jsonFieldData = [];

foreach ($mappedFields as $fieldName) {
if (!isset($postVars[$fieldName])) {
$msg = sprintf('%s doesn\'t exist in POST data.', $fieldName);
throw new JSONTextException($msg);
}

$jsonFieldData[$fieldName] = $postVars[$fieldName];
}

$fieldValue = singleton(JSONText::class)->toJson($jsonFieldData);
$owner->setField($jsonField, $fieldValue);
}
}

/**
* The CMS input fields declared in the json_field_map static, are not DB-backed,
* by virtue of this extension, they are backed by specific values represented
* in the relevant JSON data. Therefore we need to pre-populate each such field's
* value.
*
*
* @param FieldList $fields
* @return void
*/
public function updateCMSFields(\SilverStripe\Forms\FieldList $fields)
{
$owner = $this->getOwner();
$jsonFieldMap = $owner->config()->get('json_field_map');

if (empty($jsonFieldMap)) {
return;
}

foreach ($jsonFieldMap as $jsonField => $mappedFields) {
if (!$owner->getField($jsonField)) {
continue;
}

$jsonFieldObj = $owner->dbObject($jsonField);

foreach ($mappedFields as $fieldName) {
if (!$fieldValue = $jsonFieldObj->query('->>', $fieldName)) {
continue;
}

if ($fieldValue = array_values($jsonFieldObj->toArray($fieldValue))) {
$fieldValue = $fieldValue[0];
$owner->setField($fieldName, $fieldValue);
Expand Down
Loading

0 comments on commit 5d08cb7

Please sign in to comment.