Skip to content

Commit

Permalink
Custom fields in CP tabs are now bound to the tabs template
Browse files Browse the repository at this point in the history
New field options: type can be str or textarea, set maxlength or rows and define how the field should render in frontend: markdown, html (as is) or plain — everything else will be html encoded.
  • Loading branch information
slackero committed Dec 19, 2015
1 parent aad9226 commit 1bac96a
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 109 deletions.
12 changes: 10 additions & 2 deletions include/config/conf.template_default.inc.php
Expand Up @@ -440,7 +440,15 @@

'tabs_custom_fields' => array(
// Enable and customise to enable additional tab input fields:
// 'fieldname1' => 'legend', // [TAB_FIELDNAME1]{TAB_FIELDNAME1}[/TAB_FIELDNAME1]
// 'fieldname2_html'=>'legend not html encoded' // [TAB_FIELDNAME2]{TAB_FIELDNAME2}[/TAB_FIELDNAME2]
/*
'fieldgroup1' => array(
'legend' => 'Field group name',
'template' => 'default', // bind the fieldgroup to a specific template, or default
'fields' => array(
'fieldname1' => array('legend' => 'field 1', 'type' => 'str', 'render' => 'html', 'maxlength' => 100), // [TAB_FIELDNAME1]{TAB_FIELDNAME1}[/TAB_FIELDNAME1]
'fieldname2' => array('legend' => 'field 2', 'type' => 'textarea', 'render' => '', 'rows' => 3), // [TAB_FIELDNAME2]{TAB_FIELDNAME2}[/TAB_FIELDNAME2]
)
)
*/
)
);
2 changes: 0 additions & 2 deletions include/inc_front/content/cnt0.article.inc.php
Expand Up @@ -72,5 +72,3 @@


$CNT_TMP .= LF.trim($crow["acontent_template"]).LF;

?>
60 changes: 35 additions & 25 deletions include/inc_front/content/cnt32.article.inc.php
Expand Up @@ -23,6 +23,9 @@
$tabs['tabs'] = @unserialize($crow["acontent_form"]);
unset($tabs['tabs']['tabwysiwygoff']);

$tabs['tab_fieldgroup'] = empty($tabs['tabs']['tab_fieldgroup']) ? '' : $tabs['tabs']['tab_fieldgroup'];
unset($tabs['tabs']['tab_fieldgroup']);

// read template
if(empty($crow["acontent_template"]) && is_file(PHPWCMS_TEMPLATE.'inc_default/tabs.tmpl')) {

Expand All @@ -40,11 +43,18 @@

if($tabs['template']) {

$tabs['entries'] = array();
$tabs['entries'] = array();

$tabs['tmpl_entry'] = get_tmpl_section('TABS_ENTRY', $tabs['template']);
$tabs['template'] = get_tmpl_section('TABS', $tabs['template']);

$tabs['tmpl_entry'] = get_tmpl_section('TABS_ENTRY', $tabs['template']);
$tabs['template'] = get_tmpl_section('TABS', $tabs['template']);
$tabs['custom_tab_fields'] = empty($template_default['settings']['tabs_custom_fields']) ? array() : array_keys($template_default['settings']['tabs_custom_fields']);
if($tabs['tab_fieldgroup'] === '' || empty($template_default['settings']['tabs_custom_fields'][ $tabs['tab_fieldgroup'] ]['fields'])) {
$tabs['custom_tab_fields'] = array();
} else {
$tabs['custom_tab_fields'] = array_keys($template_default['settings']['tabs_custom_fields'][ $tabs['tab_fieldgroup'] ]['fields']);
$tabs['field_render'] = array('html', 'markdown', 'plain');
$tabs['fieldgroup'] =& $template_default['settings']['tabs_custom_fields'][ $tabs['tab_fieldgroup'] ];
}

foreach($tabs['tabs'] as $key => $entry) {

Expand All @@ -61,27 +71,29 @@
$tabs['entries'][$key] = str_replace('{TARGET}', $entry['tablink']['target'], $tabs['entries'][$key]);
}

if(!empty($entry['custom_fields']) && count($entry['custom_fields'])) {

if(count($tabs['custom_tab_fields'])) {
$tabs['custom_field_items'] = array_unique( array_merge($tabs['custom_tab_fields'], array_keys($entry['custom_fields'])) );
} else {
$tabs['custom_field_items'] = array_keys($entry['custom_fields']);
}

} else {

$tabs['custom_field_items'] = $tabs['custom_tab_fields'];

}

if($tabs['custom_field_items']) {
foreach($tabs['custom_field_items'] as $custom_field_key) {
if($tabs['custom_tab_fields']) {
foreach($tabs['custom_tab_fields'] as $custom_field_key) {
$custom_field_value = isset($entry['custom_fields'][$custom_field_key]) ? $entry['custom_fields'][$custom_field_key] : '';
$custom_field_key = 'TAB_'.strtoupper($custom_field_key);
if(substr($custom_field_key, -5) === '_HTML') {
$custom_field_key = substr($custom_field_key, 0, -5);
$tabs['entries'][$key] = render_cnt_template($tabs['entries'][$key], $custom_field_key, $custom_field_value);

if($custom_field_value === '') {
render_cnt_template($tabs['entries'][$key], $custom_field_key, '');
continue;
}

if(isset($tabs['fieldgroup'][$custom_field_key]['render']) && in_array($tabs['fieldgroup'][$custom_field_key]['render'], $tabs['field_render'])) {
if($tabs['fieldgroup'][$custom_field_key]['render'] === 'markdown') {
if(!isset($phpwcms['parsedown_class'])) {
require_once(PHPWCMS_ROOT.'/include/inc_ext/parsedown/Parsedown.php');
require_once(PHPWCMS_ROOT.'/include/inc_ext/parsedown-extra/ParsedownExtra.php');
$phpwcms['parsedown_class'] = new ParsedownExtra();
}
$tabs['entries'][$key] = render_cnt_template($tabs['entries'][$key], $custom_field_key, $phpwcms['parsedown_class']->text($custom_field_value));
} elseif($tabs['fieldgroup'][$custom_field_key]['render'] === 'plain') {
$tabs['entries'][$key] = render_cnt_template($tabs['entries'][$key], $custom_field_key, plaintext_htmlencode($custom_field_value));
} else {
render_cnt_template($tabs['entries'][$key], $custom_field_key, $custom_field_value);
}
} else {
$tabs['entries'][$key] = render_cnt_template($tabs['entries'][$key], $custom_field_key, html($custom_field_value));
}
Expand All @@ -105,5 +117,3 @@
}

unset($tabs);

?>
1 change: 1 addition & 0 deletions include/inc_lang/backend/de/lang.inc.php
Expand Up @@ -1398,3 +1398,4 @@
$BL['be_fsearch_nor'] = 'KEINES';
$BL['be_tab_toggle'] = 'Reiter aus- bzw. einklappen';
$BL['be_custom_textfield'] = 'Freitext';
$BL['be_tab_template_toggle_warning'] = 'Wenn Sie die Vorlage umstellen, kann passieren, dass sich die Freitextfelder ändern und Werte verloren gehen.\n\nMöchten Sie wirklich fortfahren?';
2 changes: 2 additions & 0 deletions include/inc_lang/backend/en/lang.inc.php
Expand Up @@ -1407,3 +1407,5 @@
$BL['be_fsearch_nor'] = 'NONE';
$BL['be_tab_toggle'] = 'Toggle tab to expanded or closed';
$BL['be_custom_textfield'] = 'custom text';
$BL['be_tab_template_toggle_warning'] = 'Changing the template can have the effect that custom fields get changed too and existing values get lost.\n\nAre you really sure to continue?';

20 changes: 20 additions & 0 deletions include/inc_lib/backend.functions.inc.php
Expand Up @@ -1307,3 +1307,23 @@ function get_struct_alias($start_id=0, $parent_alias=false) {

return implode($GLOBALS['phpwcms']['alias_allow_slash'] ? '/' : '-', array_reverse($data));
}


/**
* Correct the text in case phpwcms charset is different from UTF-8
*
* @access public
* @param string $text
* @param bool $js (default: false)
* @return void
*/
function correct_charset($text='', $js=false) {

if(PHPWCMS_CHARSET !== 'utf-8' && phpwcms_seems_utf8($text)) {
$text = utf8_decode($text);
}
if($js) {
$text = str_replace("'", "\'", $text);
}
return $text;
}
14 changes: 13 additions & 1 deletion include/inc_lib/content/cnt32.readform.inc.php
Expand Up @@ -25,6 +25,17 @@
$content['html'] = array();
$content['tabwysiwygoff'] = empty($_POST['tabwysiwygoff']) ? 0 : 1;

$tab_fieldgroup_fields = null;
$tab_fieldgroup_field_render = array('html', 'markdown');
if(empty($_POST['tab_fieldgroup'])) {
$content['tab_fieldgroup'] = '';
} else {
$content['tab_fieldgroup'] = clean_slweg($_POST['tab_fieldgroup']);
if(isset($template_default['settings']['tabs_custom_fields'][ $content['tab_fieldgroup'] ]['fields'])) {
$tab_fieldgroup_fields =& $template_default['settings']['tabs_custom_fields'][ $content['tab_fieldgroup'] ]['fields'];
}
}

// get all tabs
if(isset($_POST['tabtitle']) && is_array($_POST['tabtitle']) && count($_POST['tabtitle'])) {

Expand All @@ -44,7 +55,7 @@

if(!empty($_POST['customfield'][$key]) && count($_POST['customfield'][$key])) {
foreach($_POST['customfield'][$key] as $custom_field => $custom_field_value) {
if(substr(strtolower($custom_field), -5) === '_html') {
if($tab_fieldgroup_fields !== null && isset($tab_fieldgroup_fields[$custom_field]['render']) && in_array($tab_fieldgroup_fields[$custom_field]['render'], $tab_fieldgroup_field_render)) {
$content["tabs"][$x]['custom_fields'][$custom_field] = slweg($custom_field_value);
} else {
$content["tabs"][$x]['custom_fields'][$custom_field] = clean_slweg($custom_field_value);
Expand Down Expand Up @@ -84,3 +95,4 @@
}

$content['tabs']['tabwysiwygoff'] = $content['tabwysiwygoff'];
$content['tabs']['tab_fieldgroup'] = $content['tab_fieldgroup'];
4 changes: 0 additions & 4 deletions include/inc_lib/content/cnt32.sql.inc.php
Expand Up @@ -17,14 +17,10 @@
}
// ----------------------------------------------------------------



// Content Type Tabs

//$SQL .= "acontent_html="._dbEscape($content["image_html"]).", ";
$SQL .= "acontent_template="._dbEscape($content["tabs_template"]).", ";
$SQL .= "acontent_form="._dbEscape(serialize($content["tabs"])).", ";
$SQL .= "acontent_text="._dbEscape($content['search']).", ";
$SQL .= "acontent_html="._dbEscape($content['html'])." ";

?>
5 changes: 0 additions & 5 deletions include/inc_lib/content/cnt32.takeval.inc.php
Expand Up @@ -17,11 +17,6 @@
}
// ----------------------------------------------------------------



// Content Type Tabs

$content["tabs"] = @unserialize($row["acontent_form"]);
$content["tabs_template"] = $row["acontent_template"];

?>

0 comments on commit 1bac96a

Please sign in to comment.