1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-11 09:04:38 +02:00

Custom Fields class added.

This commit is contained in:
Cameron
2017-01-24 19:53:40 -08:00
parent 4fe73d43d9
commit 4aa0329ddc
7 changed files with 513 additions and 151 deletions

View File

@@ -261,12 +261,12 @@ class page_chapters_ui extends e_admin_ui
{ {
// return $this->beforeCreate($new_data); // return $this->beforeCreate($new_data);
$new_data['chapter_fields'] = $this->processCustomFields($new_data['chapter_fields']); $new_data = e107::getCustomFields()->processConfigPost('chapter_fields', $new_data);
return $new_data; return $new_data;
} }
/*
private function processCustomFields($newdata) private function processCustomFields($newdata)
{ {
if(empty($newdata)) if(empty($newdata))
@@ -291,7 +291,7 @@ class page_chapters_ui extends e_admin_ui
} }
return $new; return $new;
} }*/
} }
@@ -345,68 +345,7 @@ class page_chapters_form_ui extends e_admin_form_ui
if($mode == 'write') if($mode == 'write')
{ {
return e107::getCustomFields()->loadConfig($curVal)->renderConfigForm('chapter_fields');
$value= array();
if(!empty($curVal))
{
$curVal = e107::unserialize($curVal);
$i = 0;
foreach($curVal as $k=>$v)
{
$v['key'] = $k;
$value[$i] = $v;
$i++;
}
}
$text = "<table class='table table-striped table-bordered'>
<colgroup>
<col />
<col />
<col />
<col style='width:40%' />
</colgroup>
<tbody>
<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 <= $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-]*'));
$fieldTitle = $this->text('chapter_fields['.$i.'][title]',$value[$i]['title'], 80);
$fieldType = $this->select('chapter_fields['.$i.'][type]',$this->getFieldTypes(),$value[$i]['type'], $writeParms);
$fieldParms = $this->text('chapter_fields['.$i.'][writeParms]',$value[$i]['writeParms'], 255, $parmsWriteParms);
$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 .= "</tbody></table>";
// $text .= print_a($value,true);
return $text;
} }
if($mode == 'filter') if($mode == 'filter')
@@ -420,31 +359,7 @@ 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 "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)
@@ -882,34 +797,9 @@ class page_admin_ui extends e_admin_ui
{ {
$tabId = 'additional'; $tabId = 'additional';
$this->tabs[$tabId] = "Additional Fields";
if(!empty($this->chapterFields[$chap])) if(!empty($this->chapterFields[$chap]))
{ {
foreach($this->chapterFields[$chap] as $key=>$fld) e107::getCustomFields()->loadConfig($this->chapterFields[$chap])->setTab($tabId, "Additional")->setAdminUIConfig('page_fields',$this);
{
$fld['tab'] = $tabId;
$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;
}
} }
else else
{ {
@@ -917,25 +807,6 @@ class page_admin_ui extends e_admin_ui
} }
} }
private function loadCustomFieldsData()
{
$row = e107::getDb()->retrieve('page', 'page_chapter, page_fields', 'page_id='.$this->getId());
$chap = intval($row['page_chapter']);
$this->getModel()->set('page_fields', null);
$curVal = e107::unserialize($row['page_fields']);
if(!empty($this->chapterFields[$chap]))
{
foreach($this->chapterFields[$chap] as $key=>$fld)
{
$this->getModel()->set('page_fields__'.$key, $curVal[$key]);
}
}
}
function CreateObserver() function CreateObserver()
{ {
@@ -954,8 +825,9 @@ class page_admin_ui extends e_admin_ui
$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(); e107::getCustomFields()->loadData($row['page_fields'])->setAdminUIData('page_fields',$this);
} }
@@ -963,7 +835,7 @@ class page_admin_ui extends e_admin_ui
* Filter/Process Posted page_field data; * Filter/Process Posted page_field data;
* @param $new_data * @param $new_data
* @return null * @return null
*/ *//*
private function processCustomFieldData($new_data) private function processCustomFieldData($new_data)
{ {
if(empty($new_data)) if(empty($new_data))
@@ -992,7 +864,7 @@ class page_admin_ui extends e_admin_ui
} }
*/
@@ -1091,7 +963,7 @@ class page_admin_ui extends e_admin_ui
function beforeUpdate($newdata,$olddata, $id) function beforeUpdate($newdata,$olddata, $id)
{ {
$newdata = $this->processCustomFieldData($newdata); $newdata = e107::getCustomFields()->processDataPost('page_fields',$newdata);
if(isset($newdata['menu_name'])) if(isset($newdata['menu_name']))
{ {

View File

@@ -1038,7 +1038,13 @@ class media_admin_ui extends e_admin_ui
$mes->addDebug("For:".$cat); $mes->addDebug("For:".$cat);
$mes->addDebug("Bbcode: ".$this->getQuery('bbcode')); $mes->addDebug("Bbcode: ".$this->getQuery('bbcode'));
$video = $this->getQuery('video');
if($video == 2)
{
echo $this->mediaSelectUpload('video');
return;
}
$this->processUploadUrl(true, $cat); $this->processUploadUrl(true, $cat);
@@ -1140,6 +1146,19 @@ class media_admin_ui extends e_admin_ui
function mediaSelectUpload($type='image') function mediaSelectUpload($type='image')
{ {
$frm = e107::getForm(); $frm = e107::getForm();
if($type === 'video')
{
$tabs = array(
'youtube' => array('caption'=>'Youtube', 'text' => $this->videoTab())
);
return $frm->tabs($tabs, array('class'=>'media-manager'));
}
$videoActive = 'inactive'; $videoActive = 'inactive';
$options = array(); $options = array();

View File

@@ -2608,6 +2608,10 @@ class e_admin_controller_ui extends e_admin_controller
return $this->tabs; return $this->tabs;
} }
public function addTab($key,$val)
{
$this->tabs[$key] = (string) $val;
}
/** /**
* Get Tab data * Get Tab data

View File

@@ -184,6 +184,7 @@ class e107
'e_array' => '{e_HANDLER}core_functions.php', // Old ArrayStorage. 'e_array' => '{e_HANDLER}core_functions.php', // Old ArrayStorage.
'e_bbcode' => '{e_HANDLER}bbcode_handler.php', 'e_bbcode' => '{e_HANDLER}bbcode_handler.php',
'e_bb_base' => '{e_HANDLER}bbcode_handler.php', 'e_bb_base' => '{e_HANDLER}bbcode_handler.php',
'e_customfields' => '{e_HANDLER}e_customfields_class.php',
'e_file' => '{e_HANDLER}file_class.php', 'e_file' => '{e_HANDLER}file_class.php',
'e_form' => '{e_HANDLER}form_handler.php', 'e_form' => '{e_HANDLER}form_handler.php',
'e_jshelper' => '{e_HANDLER}js_helper.php', 'e_jshelper' => '{e_HANDLER}js_helper.php',
@@ -1765,6 +1766,15 @@ class e107
return self::getSingleton('comment', true); return self::getSingleton('comment', true);
} }
/**
* Retrieve comments handler singleton object
* @return e_customfields
*/
public static function getCustomFields()
{
return self::getSingleton('e_customfields', true);
}
/** /**
* Retrieve Media handler singleton object * Retrieve Media handler singleton object
* @return e_media * @return e_media

View File

@@ -0,0 +1,447 @@
<?php
/**
* e107 website system
*
* Copyright (C) 2008-2017 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
*/
class e_customfields
{
private $_fieldTypes = array(
'number', 'email', 'url', 'password', 'text', 'tags', 'textarea',
'bbarea', 'image', 'file', 'icon', 'datestamp', 'checkboxes', 'dropdown', 'radio',
'userclass', 'user', 'boolean', 'checkbox', 'hidden', 'lanlist', 'language', 'country', 'video'
);
private $_config = array();
private $_data = array();
private $_field_limit = 20;
private $_tab = array('additional'=>'Additional FieldsXX');
function __construct()
{
asort($this->_fieldTypes);
}
public function getFieldTypes()
{
return $this->_fieldTypes;
}
/**
* Load the configuration for all custom fields
* @param string $data - json custom-field configuration data.
* @return $this
*/
public function loadConfig($data)
{
if(is_array($data))
{
$this->_config = $data;
return $this;
}
$this->_config = e107::unserialize($data);
return $this;
}
/**
* Load a set of custom-field data for the current configuration.
* @param string $data json custom-field form data.
* @return $this
*/
public function loadData($data)
{
$this->_data = e107::unserialize($data);
return $this;
}
public function getConfig()
{
return $this->_config;
}
public function getData()
{
return $this->_data;
}
public function setTab($key, $label)
{
$this->_tab = array((string) $key => (string) $label);
return $this;
}
public function getFieldValue($key, $parm=array())
{
$tp = e107::getParser();
$value = $this->_data[$key];
$raw = (!empty($parm['mode']) && $parm['mode'] === 'raw') ? true : false;
$type = (!empty($parm['type'])) ? $parm['type'] : null;
$fieldType = $this->_config[$key]['type'];
switch($fieldType)
{
case "dropdown":
case "checkboxes":
case "radio":
return ($raw) ? $value : e107::getForm()->renderValue($key,$value,$this->_config[$key]);
break;
case "video":
return ($raw) ? 'https://www.youtube.com/watch?v='.str_replace(".youtube", '', $value) : $tp->toVideo($value);
break;
case "image":
return ($raw) ? $tp->thumbUrl($value) : $tp->toImage($value);
break;
case "icon":
return ($raw) ? str_replace(".glyph", '', $value) : $tp->toIcon($value);
break;
case "country":
return ($raw) ? $value : e107::getForm()->getCountry($value);
break;
case "tags":
return ($raw) ? $value : $tp->toLabel($value,$type);
break;
case "lanlist":
case "language":
return ($raw) ? $value : e107::getLanguage()->convert($value);
break;
case "datestamp":
return ($raw) ? $value : $tp->toDate($value);
break;
case "file":
return ($raw) ? $tp->toFile($value, array('raw'=>1)) : $tp->toFile($value);
break;
case "url":
case "email":
return ($raw) ? $value : $tp->toHtml($value);
break;
case "user":
return ($raw) ? $value : e107::getSystemUser($value,true)->getName();
break;
case "userclass":
return ($raw) ? $value : e107::getUserClass()->getName($value);
break;
case "textarea":
case "bbarea":
return $tp->toHtml($value, true);
break;
default:
return $tp->toHtml($value);
}
}
public function renderTest()
{
$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>';
foreach($this->_data as $ok=>$v)
{
$text .= "<tr><td>".$ok."</td><td>".$this->getFieldTitle($ok)."</td><td>".$this->getFieldValue($ok)."</td><td>".$this->getFieldValue($ok, array('mode'=>'raw'))."</td></tr>";
}
$text .= "</table>";
return $text;
}
public function getFieldTitle($key)
{
if(!empty($this->_config[$key]['title']))
{
return $this->_config[$key]['title'];
}
return null;
}
public function renderConfigForm($name)
{
$frm = e107::getForm();
$curVal = $this->_config;
$value = array();
if(!empty($curVal))
{
$i = 0;
foreach($curVal as $k=>$v)
{
$v['key'] = $k;
$value[$i] = $v;
$i++;
}
}
$text = "<table class='table table-striped table-bordered'>
<colgroup>
<col />
<col />
<col />
<col style='width:40%' />
</colgroup>
<tbody>
<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 <= $this->_field_limit; $i++)
{
$writeParms = array(
// 'class' => 'form-control',
'useValues' => 1,
'default' => 'blank',
'data-src' => e_REQUEST_URI,
);
$parmsWriteParms= array(
'size' => 'block-level',
'placeholder' => $this->getCustomFieldPlaceholder($value[$i]['type'])
);
$fieldName = $frm->text($name.'['.$i.'][key]', $value[$i]['key'],30, array('pattern'=>'^[a-z0-9-]*'));
$fieldTitle = $frm->text($name.'['.$i.'][title]',$value[$i]['title'], 80);
$fieldType = $frm->select($name.'['.$i.'][type]',$this->getFieldTypes(),$value[$i]['type'], $writeParms);
$fieldParms = $frm->text($name.'['.$i.'][writeParms]',$value[$i]['writeParms'], 255, $parmsWriteParms);
$fieldHelp = $frm->text($name.'['.$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 .= "</tbody></table>";
return $text;
}
/**
* @param $type
* @return null|string
*/
private function getCustomFieldPlaceholder($type)
{
switch($type)
{
case "radio":
case "dropdown":
case "checkboxes":
return 'eg. { "optArray": { "blue": "Blue", "green": "Green", "red": "Red" }, "default": "blank" }';
break;
case "datestamp":
return 'eg. (Optional) { "format": "yyyy-mm-dd" }';
break;
default:
}
return null;
}
/**
*
* @param $fieldName
* @param e_admin_ui $ui
* @return $this
*/
public function setAdminUIConfig($fieldName, e_admin_ui &$ui)
{
$fields = array();
$tabKey = key($this->_tab);
$ui->addTab($tabKey, $this->_tab[$tabKey]);
foreach($this->_config as $key=>$fld)
{
$fld['tab'] = $tabKey;
$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;
}
$fields[$fieldName.'__'.$key] = $fld;
//$model->fields[$fieldName.'__'.$key] = $fld;
}
$ui->setFieldAttr($fields);
return $this;
}
/**
* @param $fieldname
* @param e_admin_ui $ui
* @return $this
*/
public function setAdminUIData($fieldname, e_admin_ui &$ui)
{
// $model->getModel()->set($fieldname, null);
// e107::getCustomFields()->setData($row['page_fields']);
$ui->getModel()->set($fieldname, null);
foreach($this->_data as $key=>$value)
{
$ui->getModel()->set($fieldname.'__'.$key, $value);
}
return $this;
}
/**
* Process Posted form data and compiled Configuration Form data if found.
* @param string $fieldname
* @param array $postData all posted data.
* @return array
*/
public function processConfigPost($fieldname, $postData)
{
if(empty($postData[$fieldname]))
{
return $postData;
}
$newdata = $postData[$fieldname];
$new = array();
foreach($newdata as $fields)
{
if(empty($fields['key']) || empty($fields['type']))
{
continue;
}
$key = $fields['key'];
unset($fields['key']);
$new[$key] = $fields;
}
return $new;
}
/**
* Process all posted data and compile into a single field array.
* @param $fieldname
* @param $new_data
* @return null
* @internal param array $newdata - all posted data.
*/
public function processDataPost($fieldname, $new_data)
{
if(empty($new_data))
{
return null;
}
unset($new_data[$fieldname]); // Reset.
$len = strlen($fieldname);
foreach($new_data as $k=>$v)
{
if(substr($k,0,$len) === $fieldname)
{
list($tmp,$newkey) = explode("__",$k);
$new_data[$fieldname][$newkey] = $v;
unset($new_data[$k]);
}
}
return $new_data;
}
}

View File

@@ -4012,6 +4012,11 @@ class e_parser
*/ */
public function isJSON($text) public function isJSON($text)
{ {
if(!is_string($text))
{
return false;
}
if(substr($text,0,1) === '{' || substr($text,0,1) === '[') // json if(substr($text,0,1) === '{' || substr($text,0,1) === '[') // json
{ {
$dat = json_decode($text, true); $dat = json_decode($text, true);

View File

@@ -67,12 +67,7 @@ class e_form
protected $_tabindex_enabled = true; protected $_tabindex_enabled = true;
protected $_cached_attributes = array(); protected $_cached_attributes = array();
private $fields = array(
'number', 'email', 'url', 'password', 'text', 'tags', 'textarea',
'bbarea', 'image', 'file', 'icon', 'datestamp', 'checkboxes', 'dropdown', 'radio',
'userclass', 'user', 'boolean', 'checkbox', 'hidden', 'lanlist', 'language', 'country'
);
/** /**
* @var user_class * @var user_class
@@ -538,10 +533,11 @@ class e_form
$text .= '</ul>'; $text .= '</ul>';
$initTab = varset($options['active'],false); $initTab = varset($options['active'],false);
$tabClass = varset($options['class'],null);
$text .= ' $text .= '
<!-- Tab panes --> <!-- Tab panes -->
<div class="tab-content">'; <div class="tab-content '.$tabClass.'">';
$c=0; $c=0;
foreach($array as $key=>$tab) foreach($array as $key=>$tab)
@@ -903,7 +899,7 @@ class e_form
if(!empty($extras['video'])) if(!empty($extras['video']))
{ {
$url .= "&amp;video=1"; $url .= ($extras['video'] == 2) ? "&amp;video=2" : "&amp;video=1";
} }
if(!empty($extras['path']) && $extras['path'] == 'plugin') if(!empty($extras['path']) && $extras['path'] == 'plugin')
@@ -3019,11 +3015,7 @@ class e_form
} }
protected function getFieldTypes()
{
asort($this->fields);
return $this->fields;
}
@@ -4997,6 +4989,11 @@ class e_form
case 'password': // encrypts to md5 when saved. case 'password': // encrypts to md5 when saved.
$maxlength = vartrue($parms['maxlength'], 255); $maxlength = vartrue($parms['maxlength'], 255);
unset($parms['maxlength']); unset($parms['maxlength']);
if(!isset($parms['required']))
{
$parms['required'] = false;
}
$ret = vartrue($parms['pre']).$this->password($key, $value, $maxlength, $parms).vartrue($parms['post']); // vartrue($parms['__options']) is limited. See 'required'=>true $ret = vartrue($parms['pre']).$this->password($key, $value, $maxlength, $parms).vartrue($parms['post']); // vartrue($parms['__options']) is limited. See 'required'=>true
break; break;
@@ -5068,9 +5065,17 @@ class e_form
$ret = vartrue($parms['pre']).$this->bbarea($key, $value, vartrue($parms['template']), vartrue($parms['media']), vartrue($parms['size'], 'medium'),$options ).vartrue($parms['post']); $ret = vartrue($parms['pre']).$this->bbarea($key, $value, vartrue($parms['template']), vartrue($parms['media']), vartrue($parms['size'], 'medium'),$options ).vartrue($parms['post']);
break; break;
case 'video':
case 'image': //TODO - thumb, image list shortcode, js tooltip... case 'image': //TODO - thumb, image list shortcode, js tooltip...
$label = varset($parms['label'], 'LAN_EDIT'); $label = varset($parms['label'], 'LAN_EDIT');
unset($parms['label']); unset($parms['label']);
if($attributes['type'] === 'video')
{
$parms['video'] = 2; // ie. video only.
$parms['w'] = 280;
}
$ret = $this->imagepicker($key, $value, defset($label, $label), $parms); $ret = $this->imagepicker($key, $value, defset($label, $label), $parms);
break; break;