1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-01 20:30:39 +02:00

Form Handler: renderElement() and renderValue() now support json values. (used by Pages/Menus custom fields)

This commit is contained in:
Cameron
2017-01-22 13:31:03 -08:00
parent 3e9624be81
commit 2c2b9d4091
4 changed files with 148 additions and 22 deletions

View File

@@ -335,8 +335,8 @@ class page_chapters_form_ui extends e_admin_form_ui
function chapter_fields($curVal,$mode,$parm) function chapter_fields($curVal,$mode,$parm)
{ {
$fieldAmount = (deftrue('e_DEBUG')) ? 20 :10;
$frm = e107::getForm();
if($mode == 'read') if($mode == 'read')
{ {
@@ -372,13 +372,32 @@ class page_chapters_form_ui extends e_admin_form_ui
<tr><th>".LAN_NAME."</th><th>".LAN_TITLE."</th><th>".LAN_TYPE."</th><th>Params</th><th>".LAN_TOOLTIP."</th></tr> <tr><th>".LAN_NAME."</th><th>".LAN_TITLE."</th><th>".LAN_TYPE."</th><th>Params</th><th>".LAN_TOOLTIP."</th></tr>
"; ";
for ($i = 0; $i <= 20; $i++)
for ($i = 0; $i <= $fieldAmount; $i++)
{ {
$writeParms = array(
'class' => 'e-ajax',
'useValues' => 1,
'default' => 'blank',
'data-src' => e_REQUEST_URI,
);
$parmsWriteParms= array(
'size' => 'block-level',
'placeholder' => $this->getCustomFieldPlaceholder($value[$i]['type'])
);
$fieldName = $this->text('chapter_fields['.$i.'][key]',$value[$i]['key'],30, array('pattern'=>'^[a-z0-9-]*')); $fieldName = $this->text('chapter_fields['.$i.'][key]',$value[$i]['key'],30, array('pattern'=>'^[a-z0-9-]*'));
$fieldTitle = $this->text('chapter_fields['.$i.'][title]',$value[$i]['title'], 80); $fieldTitle = $this->text('chapter_fields['.$i.'][title]',$value[$i]['title'], 80);
$fieldType = $this->select('chapter_fields['.$i.'][type]',$this->getFieldTypes(),$value[$i]['type'], 'useValues=1&default=blank'); $fieldType = $this->select('chapter_fields['.$i.'][type]',$this->getFieldTypes(),$value[$i]['type'], $writeParms);
$fieldParms = $this->text('chapter_fields['.$i.'][writeParms]',$value[$i]['writeParms'], 80, array('size'=>'block-level')); $fieldParms = $this->text('chapter_fields['.$i.'][writeParms]',$value[$i]['writeParms'], 255, $parmsWriteParms);
$fieldHelp = $this->text('chapter_fields['.$i.'][help]',$value[$i]['help'], 80, array('size'=>'block-level')); $fieldHelp = $this->text('chapter_fields['.$i.'][help]',$value[$i]['help'], 255, array('size'=>'block-level'));
$text .= "<tr><td>".$fieldName."</td><td>".$fieldTitle."</td><td>".$fieldType."</td><td>".$fieldParms."</td><td>".$fieldHelp."</td></tr>"; $text .= "<tr><td>".$fieldName."</td><td>".$fieldTitle."</td><td>".$fieldType."</td><td>".$fieldParms."</td><td>".$fieldHelp."</td></tr>";
} }
@@ -401,6 +420,35 @@ class page_chapters_form_ui extends e_admin_form_ui
} }
//@XXX Move to Form-handler?
public function getCustomFieldPlaceholder($type)
{
switch($type)
{
case "radio":
case "dropdown":
case "checkboxes":
return 'eg. { "optArray": { "blue": "Blue", "green": "Green", "red": "Red" }, "default": "blank" }';
break;
case "tags":
return 'eg. tag1,tag2,tag3,tag4';
break;
case "datestamp":
return 'eg. (Optional) { "format": "yyyy-mm-dd" }';
break;
default:
}
return null;
}
// Override the default Options field. // Override the default Options field.
function options($parms, $value, $id, $attributes) function options($parms, $value, $id, $attributes)
@@ -785,8 +833,18 @@ class page_admin_ui extends e_admin_ui
$this->fields['page_chapter']['writeParms']['ajax'] = array('src'=>e_SELF."?mode=page&action=chapter-change",'target'=>'tabadditional'); $this->fields['page_chapter']['writeParms']['ajax'] = array('src'=>e_SELF."?mode=page&action=chapter-change",'target'=>'tabadditional');
} }
if(e_AJAX_REQUEST)
{
// @todo insert placeholder examples in params input when 'type' dropdown value is changed
}
if(e_AJAX_REQUEST && isset($_POST['page_chapter']) ) //&& $this->getAction() === 'chapter-change' if(e_AJAX_REQUEST && isset($_POST['page_chapter']) ) //&& $this->getAction() === 'chapter-change'
{ {
$this->initCustomFields($_POST['page_chapter']); $this->initCustomFields($_POST['page_chapter']);
$elid = 'core-page-create'; $elid = 'core-page-create';
@@ -821,7 +879,9 @@ class page_admin_ui extends e_admin_ui
} }
} }
/*
* @todo Move to admin-ui ?
*/
private function initCustomFields($chap=null) private function initCustomFields($chap=null)
{ {
$tabId = 'additional'; $tabId = 'additional';
@@ -835,7 +895,24 @@ class page_admin_ui extends e_admin_ui
$fld['tab'] = $tabId; $fld['tab'] = $tabId;
$fld['data'] = false; $fld['data'] = false;
if($fld['type'] === 'icon')
{
$fld['writeParms'] .= "&glyphs=1";
}
if($fld['type'] == 'checkboxes')
{
if($tmp = e107::getParser()->isJSON($fld['writeParms']))
{
$fld['writeParms'] = $tmp;
}
$fld['writeParms']['useKeyValues'] = 1;
}
$this->fields['page_fields__'.$key] = $fld; $this->fields['page_fields__'.$key] = $fld;
} }
} }
else else
@@ -879,19 +956,11 @@ class page_admin_ui extends e_admin_ui
parent::EditObserver(); parent::EditObserver();
if(!deftrue('e_DEBUG'))
{
// return;
}
$row = e107::getDb()->retrieve('page', 'page_chapter, page_fields', 'page_id='.$this->getId()); $row = e107::getDb()->retrieve('page', 'page_chapter, page_fields', 'page_id='.$this->getId());
$chap = intval($row['page_chapter']); $chap = intval($row['page_chapter']);
$this->initCustomFields($chap); $this->initCustomFields($chap);
$this->loadCustomFieldsData(); $this->loadCustomFieldsData();
} }
/** /**
@@ -928,6 +997,11 @@ class page_admin_ui extends e_admin_ui
} }
/** /**
* Overrid * Overrid
*/ */

View File

@@ -652,6 +652,9 @@ class cpage_shortcodes extends e_shortcode
$fieldType = $fields[$key]['type']; $fieldType = $fields[$key]['type'];
// print_a($fields);
// @todo Move this part to form_handler or e_parse somewhere. // @todo Move this part to form_handler or e_parse somewhere.
if(isset($fieldData[$key])) if(isset($fieldData[$key]))
@@ -660,6 +663,12 @@ class cpage_shortcodes extends e_shortcode
switch($fieldType) switch($fieldType)
{ {
case "dropdown":
case "checkboxes":
case "radio":
return ($raw) ? $value : e107::getForm()->renderValue($key,$value,$fields[$key]);
break;
case "image": case "image":
return ($raw) ? $tp->thumbUrl($value) : $tp->toImage($value); return ($raw) ? $tp->thumbUrl($value) : $tp->toImage($value);
break; break;
@@ -728,6 +737,7 @@ class cpage_shortcodes extends e_shortcode
{ {
$fieldData = e107::unserialize($this->var['page_fields']); $fieldData = e107::unserialize($this->var['page_fields']);
$text = '<table class="table table-bordered table-striped"> $text = '<table class="table table-bordered table-striped">
<tr><th>Name</th><th>Title<br /><small>&#123;CPAGEFIELDTITLE: name=x&#125;</small></th><th>Normal<br /><small>&#123;CPAGEFIELD: name=x&#125;</small></th><th>Raw<br /><small>&#123;CPAGEFIELD: name=x&mode=raw&#125;</small></th></tr>'; <tr><th>Name</th><th>Title<br /><small>&#123;CPAGEFIELDTITLE: name=x&#125;</small></th><th>Normal<br /><small>&#123;CPAGEFIELD: name=x&#125;</small></th><th>Raw<br /><small>&#123;CPAGEFIELD: name=x&mode=raw&#125;</small></th></tr>';

View File

@@ -3995,6 +3995,30 @@ class e_parser
} }
/**
* Check if string is json and parse or return false.
* @param $text
* @return bool|mixed return false if not json, and json values if true.
*/
public function isJSON($text)
{
if(substr($text,0,1) === '{' || substr($text,0,1) === '[') // json
{
$dat = json_decode($text, true);
if(json_last_error() != JSON_ERROR_NONE)
{
// e107::getDebug()->log("Json data found");
return false;
}
return $dat;
}
return false;
}
/** /**

View File

@@ -3893,6 +3893,12 @@ class e_form
$parms = $attributes['readParms']; $parms = $attributes['readParms'];
} }
// @see custom fields in cpage which accept json params.
if(!empty($attributes['writeParms']) && $tmpOpt = e107::getParser()->isJSON($attributes['writeParms']))
{
$attributes['writeParms'] = $tmpOpt;
unset($tmpOpt);
}
if(!empty($attributes['inline'])) $parms['editable'] = true; // attribute alias if(!empty($attributes['inline'])) $parms['editable'] = true; // attribute alias
@@ -3998,9 +4004,15 @@ class e_form
break; break;
case 'checkboxes': case 'checkboxes':
$value = $this->checkbox(vartrue($attributes['toggle'], 'multiselect').'['.$id.']', $id);
//$attributes['type'] = 'text'; //$attributes['type'] = 'text';
if(empty($attributes['writeParms'])) // avoid comflicts with a field called 'checkboxes'
{
$value = $this->checkbox(vartrue($attributes['toggle'], 'multiselect').'['.$id.']', $id);
return $value; return $value;
}
break; break;
} }
@@ -4102,7 +4114,7 @@ class e_form
$opts['multiple'] = true; $opts['multiple'] = true;
} }
if(vartrue($opts['multiple'])) if(!empty($opts['multiple']))
{ {
$ret = array(); $ret = array();
$value = is_array($value) ? $value : explode(',', $value); $value = is_array($value) ? $value : explode(',', $value);
@@ -4111,6 +4123,8 @@ class e_form
if(isset($wparms[$v])) $ret[] = $wparms[$v]; if(isset($wparms[$v])) $ret[] = $wparms[$v];
} }
$value = implode(', ', $ret); $value = implode(', ', $ret);
} }
else else
{ {
@@ -4857,10 +4871,14 @@ class e_form
{ {
$tp = e107::getParser(); $tp = e107::getParser();
$parms = vartrue($attributes['writeParms'], array()); $parms = vartrue($attributes['writeParms'], array());
if($tmpOpt = $tp->isJSON($parms))
{
$parms = $tmpOpt;
unset($tmpOpt);
}
if(is_string($parms)) parse_str($parms, $parms); if(is_string($parms)) parse_str($parms, $parms);
$ajaxParms = array(); $ajaxParms = array();