Created
January 29, 2014 16:35
-
-
Save legierski/8691782 to your computer and use it in GitHub Desktop.
Convert new data format to the old one ( GatherContent API, >=v0.3) https://help.gathercontent.com/developer-api/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
function foreach_safe($arr) { | |
if(is_array($arr)) { | |
if(count($arr) > 0) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function get_default_config() { | |
$config = array( | |
0 => (object) array( | |
'label' => 'Content', | |
'hidden' => false, | |
'elements' => array() | |
), | |
1 => (object) array( | |
'label' => 'Meta', | |
'hidden' => true, | |
'elements' => array() | |
) | |
); | |
return base64_encode(json_encode($config)); | |
} | |
function convert_from_new_config_structure($config) { | |
if($config == '') { | |
$config = get_default_config(); | |
} | |
$config = json_decode(base64_decode($config)); | |
$result = array( | |
'page_config' => array(), | |
'page_values' => array(), | |
'meta_config' => array(), | |
'meta_values' => array() | |
); | |
if(foreach_safe($config)) { | |
foreach($config as $tab_id => $tab_pane) { | |
if(foreach_safe($tab_pane->elements)) { | |
foreach($tab_pane->elements as $element) { | |
switch($element->type) { | |
case 'text': | |
$el = array( | |
'type' => 'text', | |
'name' => $element->name, | |
'label' => $element->label, | |
'instructions' => $element->microcopy, | |
'required' => $element->required?'Y':'N', | |
'limit' => $element->limit, | |
'limit_type' => $element->limit_type, | |
'plain_text' => $element->plain_text?'Y':'N' | |
); | |
if($tab_id === 0) { | |
$result['page_config'][] = $el; | |
$result['page_values'][$el['name']] = $element->value; | |
} | |
else { | |
$result['meta_config'][] = $el; | |
$result['meta_values'][$el['name']] = $$element->value; | |
} | |
break; | |
case 'section': | |
$el = array( | |
'type' => 'section', | |
'name' => $element->name, | |
'label' => $element->title, | |
'instructions' => $element->subtitle | |
); | |
if($tab_id === 0) { | |
$result['page_config'][] = $el; | |
} | |
else { | |
$result['meta_config'][] = $el; | |
} | |
break; | |
case 'choice_radio': | |
$value = ''; | |
$el = array( | |
'type' => 'mc', | |
'name' => $element->name, | |
'label' => $element->label, | |
'instructions' => $element->microcopy, | |
'required' => $element->required?'Y':'N', | |
'options' => array() | |
); | |
if(foreach_safe($element->options)) { | |
foreach($element->options as $option) { | |
$el['options'][] = array( | |
'label' => $option->label, | |
'val' => $option->label, | |
'checked' => $option->selected?'Y':'N' | |
); | |
if($option->selected) { | |
$value = $option->label; | |
} | |
} | |
} | |
if($element->other_option) { | |
$last_option_id = count($el['options']) - 1; | |
$el['options'][$last_option_id]['allow_other'] = 'Y'; | |
$other_option_value = $element->options[$last_option_id]->value; | |
if($tab_id === 0) { | |
$result['page_values'][$el['name'].'_other'] = $other_option_value; | |
} | |
else { | |
$result['meta_values'][$el['name'].'_other'] = $other_option_value; | |
} | |
} | |
if($tab_id === 0) { | |
$result['page_config'][] = $el; | |
$result['page_values'][$el['name']] = $value; | |
} | |
else { | |
$result['meta_config'][] = $el; | |
$result['meta_values'][$el['name']] = $value; | |
} | |
break; | |
case 'choice_checkbox': | |
$value = array(); | |
$el = array( | |
'type' => 'checkbox', | |
'name' => $element->name, | |
'label' => $element->label, | |
'instructions' => $element->microcopy, | |
'required' => $element->required?'Y':'N', | |
'options' => array() | |
); | |
if(foreach_safe($element->options)) { | |
foreach($element->options as $option) { | |
$el['options'][] = array( | |
'label' => $option->label, | |
'val' => $option->label, | |
'checked' => $option->selected?'Y':'N' | |
); | |
if($option->selected) { | |
$value[] = $option->label; | |
} | |
} | |
} | |
if($tab_id === 0) { | |
$result['page_config'][] = $el; | |
$result['page_values'][$el['name']] = $value; | |
} | |
else { | |
$result['meta_config'][] = $el; | |
$result['meta_values'][$el['name']] = $value; | |
} | |
break; | |
case 'files': | |
$el = array( | |
'type' => 'attach', | |
'name' => $element->name, | |
'label' => $element->label, | |
'instructions' => $element->microcopy, | |
'required' => $element->required?'Y':'N' | |
); | |
if($tab_id === 0) { | |
$result['page_config'][] = $el; | |
} | |
else { | |
$result['meta_config'][] = $el; | |
} | |
break; | |
default: | |
// nothing here! | |
} | |
} | |
} | |
} | |
} | |
$result['page_config'] = serialize($result['page_config']); | |
$result['page_values'] = serialize($result['page_values']); | |
$result['meta_config'] = serialize($result['meta_config']); | |
$result['meta_values'] = serialize($result['meta_values']); | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment