Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the best way to save Unyson options json ? #387

Closed
danyj opened this issue Mar 20, 2015 · 9 comments
Closed

What is the best way to save Unyson options json ? #387

danyj opened this issue Mar 20, 2015 · 9 comments

Comments

@danyj
Copy link
Contributor

danyj commented Mar 20, 2015

I tried several parsing options and form values to json jq plugins but some of them do not get unyson options right because some of the values either have same option name like addable options and some of the values are json values.
http://prntscr.com/6j2jkz

that one should have been parsed in to a sub array
http://prntscr.com/6j2kvc

Something like redux has is what I am looking for and it would be nice to have it by default actually or some kind a fw form helper method that would do this.

http://prntscr.com/6j2h6v

@ghost ghost self-assigned this Mar 20, 2015
@ghost
Copy link

ghost commented Mar 20, 2015

Database values differs from form input values. There is no way/possibility to manipulate/use/change options form values from js, form is generate and saved by option types on server side. Option type is allowed to do anything it wants with form inputs (it's a private zone for it), the requirement is to return a good/valid/simple value from _get_value_from_input() method that will be saved in the database and then used by developers in templates through fw_get_db_..._option() functions.

Something like redux has is what I am looking for and it would be nice to have it by default actually or some kind a fw form helper method that would do this.
http://prntscr.com/6j2h6v

In that textarea can be only db values (not form input values extracted with js), and the import can work only through server side db values update (not updating from inputs from js the submitting the form), for e.g. the values are update in the database then the form is refreshed and will show the new values.


From js you can use these 2 ajax actions to render form inputs html from options array and their values array, and with second action to extract the values in db format from form inputs (the code examples are from fw.OptionsModal, you can open one and inspect ajax requests).

@danyj
Copy link
Contributor Author

danyj commented Mar 20, 2015

:) that is what I finally did and you figured out what I am doing.

In short , I am adjusting in options, say , change the font color
exporting the adjusted form options to a file
load that adjusted format trough that last ajax action you linked to on "something" click/change.

@danyj
Copy link
Contributor Author

danyj commented Mar 20, 2015

OK one pesky bug is messing with me , I managed to get all options exactly as needed just the typography option style select is messing with me and gives me 2 selected options ,

this is what I have

    $values = json_decode($values['thzstyle'],true); // this one is 100% identical like DB options


foreach ( fw_extract_only_options($options) as $option_id => $option ) {

    if ( ! isset( $values[ $option_id ] ) ) {
        continue;
    }
    /**
     * We detect if option is using booleans by sending it a boolean input value
     * If it returns a boolean, then it works with booleans
     */
    if ( ! is_bool(
        fw()->backend->option_type( $option['type'] )->get_value_from_input( $option, true )
    )
    ) {
        continue;
    }
    if ( is_bool( $values[ $option_id ] ) ) {
        // value is already boolean, does not need to fix
        continue;
    }

    $values[ $option_id ] = ( $values[ $option_id ] === 'true' );
}

$html = fw()->backend->render_options( $options, $values, $data );
echo $html;

But I get this

http://prntscr.com/6j9jmq

this is the option array printed

[topmenu_font] => Array
    (
        [gadget] => inherit
        [custom] => Array
            (
                [topmenu_font_val] => Array
                    (
                        [size] => 12
                        [family] => Arial
                        [style] => 300
                        [color] => #000000
                    )

            )

    )

what am I missing ? value not a string or? if I do this in typography option

$data['value']['style'] === $key

instead of

$data['value']['style'] == $key

but that messes up the js selection on load. must be variable type. but how if the arrays are identical.

@danyj
Copy link
Contributor Author

danyj commented Mar 20, 2015

darn , it is , my array style is integer instead of string
http://prntscr.com/6j9pl0

@ghost
Copy link

ghost commented Mar 20, 2015

Sorry, I don't understand, doing

$data['value']['style'] === $key

fixes the select problem or not? (I think it should)

@danyj
Copy link
Contributor Author

danyj commented Mar 20, 2015

well yes for php , but than messes it up for js
this one here

<option value="<?php echo esc_attr($key) ?>" <?php if ($data['value']['style'] == $key): ?>selected="selected"<?php endif; ?>><?php echo fw_htmlspecialchars($style) ?></option>

and somehow my value gets converted to integer via json_decode I guess

@ghost ghost assigned valeriuzdrobau and unassigned ghost Mar 20, 2015
@danyj
Copy link
Contributor Author

danyj commented Mar 20, 2015

You dont have to assign anyone , I dont think this is your (Unyson) issue , it is my json array that should be corrected , look http://prntscr.com/6j9xol

@ghost
Copy link

ghost commented Mar 20, 2015

I think that == should be ===, and it's a bug.

@danyj
Copy link
Contributor Author

danyj commented Mar 20, 2015

yes, that fixes it , but note that you would have to fix default selected in js, since it defaults to 300 since none of them has selected via js

http://prntscr.com/6ja2i7

html += '<option value="300">Thin</option><option value="300italic">Thin/Italic</option><option value="400" selected="selected">Normal</option><option value="italic">Italic</option><option value="700">Bold</option><option value="700italic">Bold/Italic</option>';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants