Completed
Push — master ( 32cc9c...0d7657 )
by Russell
02:29
created

JSONTextExtension::updateCMSFields()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 5
nop 1
1
<?php
2
3
/**
4
 * Add this DataExtension to your models if you wish to have specific DB fields
5
 * receive JSON data POSTed from particular input fields in the CMS. 
6
 * 
7
 * You'll need to declare declare a `$jsontext_field_map` config static on the decorated
8
 * model(s) as follows:
9
 * 
10
 * <code>
11
 * private static $jsontext_field_map = [
12
 *  'MyDBField1' => [
13
 *      'MyInputField1',
14
 *      'MyInputField2'
15
 *  ],
16
 *  'MyDBField2' => [
17
 *       'MyInputField3',
18
         'MyInputField4'
19
 *  ]
20
 * ];
21
 * <code>
22
 * 
23
 * @package silverstripe-jsontext
24
 * @subpackage extensions
25
 * @author Russell Michell <[email protected]>
26
 */
27
         
28
class JSONTextExtension extends \DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    /**
31
     * Manipulate POSTed data from pre-specified CMS fields and write their data
32
     * as JSON.
33
     * 
34
     * @return void
35
     */
36
    public function onBeforeWrite()
37
    {
38
        // Fields are declared in a config static on decorated models
39
        $fieldMap = $this->getOwner()->config()->get('jsontext_field_map');
40
        $postVars = Controller::curr()->getRequest()->postVars();
41
        $data = [];
42
        
43
        foreach ($fieldMap as $dbFieldName => $inputFields) {
44
            foreach ($inputFields as $inputField) {
45
                $data[$dbFieldName][] = [$inputField => $postVars[$inputField]];
46
            }
47
            
48
            $jsonTextField = $this->getOwner()->dbObject($dbFieldName);
49
            $this->getOwner()->setField($dbFieldName, $jsonTextField->toJSON($data[$dbFieldName]));
50
        }
51
        
52
        parent::onBeforeWrite();
53
    }
54
    
55
    /**
56
     * Ensure any CMS input fields used with a {@link JSONText} field, show the
57
     * appropriate value taken from the stored JSON data once data is saved.
58
     * 
59
     * Note: UNSTABLE API, logic assumes a specific JSON structure.
60
     * 
61
     * @param FieldList $fields
62
     * @return void
63
     */
64
    public function updateCMSFields(\FieldList $fields)
65
    {
66
        $fieldMap = $this->getOwner()->config()->get('jsontext_field_map');
67
        
68
        foreach ($fieldMap as $dbFieldName => $inputFields) {
69
            $dbObject = $this->getOwner()->dbObject($dbFieldName);
70
            $jsonData = $dbObject->getStoreAsArray();
71
            
72
            foreach ($inputFields as $inputField) {
73
                foreach ($jsonData as $i => $array) {
74
                    if (isset($jsonData[$i][$inputField])) {
75
                        $this->getOwner()->setField($inputField, $array[$inputField]);
76
                    }
77
                }
78
            }
79
        }
80
    }
81
    
82
}
83