1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-17 20:58:30 +01:00
php-e107/e107_handlers/e_customfields_class.php

598 lines
11 KiB
PHP
Raw Normal View History

2017-01-24 19:53:40 -08:00
<?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', 'progressbar'
2017-01-24 19:53:40 -08:00
);
private $_config = array();
private $_data = array();
private $_field_limit = 30;
2017-01-24 19:53:40 -08:00
private $_tab = array();
private $_tab_default = 'additional';
2017-01-24 19:53:40 -08:00
2020-12-25 10:31:42 -08:00
public function __construct()
2017-01-24 19:53:40 -08:00
{
asort($this->_fieldTypes);
2020-12-25 10:31:42 -08:00
$this->_tab = array($this->_tab_default => 'Additional');
2017-01-24 19:53:40 -08:00
}
/**
* @return string[]
*/
2017-01-24 19:53:40 -08:00
public function getFieldTypes()
{
return $this->_fieldTypes;
}
/**
* @return string
*/
public function getTabId()
{
return $this->_tab_default;
}
/**
* @return mixed|string
*/
public function getTabLabel()
{
return $this->_tab[$this->_tab_default];
}
2017-01-24 19:53:40 -08:00
/**
* Load the configuration for all custom fields
* @param string $data - json custom-field configuration data.
* @return $this
*/
public function loadConfig($data)
{
if(empty($data))
{
return $this;
}
2017-01-24 19:53:40 -08:00
if(is_array($data))
{
if(isset($data['__tabs__']))
{
$this->_tab = $data['__tabs__'];
unset($data['__tabs__']);
}
2017-01-24 19:53:40 -08:00
$this->_config = $data;
2017-01-24 19:53:40 -08:00
return $this;
}
$tp = e107::getParser();
if($arr = $tp->isJSON($data))
{
if(!empty($arr['__tabs__']))
{
$this->_tab = $arr['__tabs__'];
unset($arr['__tabs__']);
}
$this->_config = $arr;
}
2017-01-24 19:53:40 -08:00
// e107::getDebug()->log($this->_config);
2017-01-25 10:30:28 -08:00
2017-01-24 19:53:40 -08:00
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);
// e107::getDebug()->log($this->_data);
2017-01-24 19:53:40 -08:00
return $this;
}
/**
* @return array
*/
2017-01-24 19:53:40 -08:00
public function getConfig()
{
return $this->_config;
}
/**
* @return array
*/
2017-01-24 19:53:40 -08:00
public function getData()
{
return $this->_data;
}
/**
* @param $key
* @param $label
* @return $this
*/
2017-01-24 19:53:40 -08:00
public function setTab($key, $label)
{
$this->_tab = array((string) $key => (string) $label);
return $this;
}
/**
* @param $key
* @param $parm
* @return array|bool|mixed|string|null
*/
2017-01-24 19:53:40 -08:00
public function getFieldValue($key, $parm=array())
{
$tp = e107::getParser();
$value = $this->_data[$key];
2021-01-24 17:00:02 -08:00
$raw = (!empty($parm['mode']) && $parm['mode'] === 'raw');
2017-01-24 19:53:40 -08:00
$type = (!empty($parm['type'])) ? $parm['type'] : null;
$fieldType = $this->_config[$key]['type'];
if($value === null)
{
return null;
}
2021-06-13 12:59:01 -07:00
$ret = null;
2017-01-24 19:53:40 -08:00
switch($fieldType)
{
2020-12-25 10:31:42 -08:00
case 'dropdown':
case 'checkboxes':
case 'radio':
$ret = ($raw) ? $value : e107::getForm()->renderValue($key,$value,$this->_config[$key]);
2017-01-24 19:53:40 -08:00
break;
2020-12-25 10:31:42 -08:00
case 'video':
2021-06-13 12:59:01 -07:00
if(!empty($value))
2020-12-25 10:31:42 -08:00
{
$ret = ($raw) ? 'https://www.youtube.com/watch?v='.str_replace('.youtube', '', $value) : $tp->toVideo($value);
}
2017-01-24 19:53:40 -08:00
break;
2020-12-25 10:31:42 -08:00
case 'image':
2021-06-13 12:59:01 -07:00
if(!empty($value))
{
$ret = ($raw) ? $tp->thumbUrl($value,$parm) : $tp->toImage($value,$parm);
}
2017-01-24 19:53:40 -08:00
break;
2020-12-25 10:31:42 -08:00
case 'icon':
$ret = ($raw) ? str_replace('.glyph', '', $value) : $tp->toIcon($value);
2017-01-24 19:53:40 -08:00
break;
2020-12-25 10:31:42 -08:00
case 'country':
$ret = ($raw) ? $value : e107::getForm()->getCountry($value);
2017-01-24 19:53:40 -08:00
break;
2020-12-25 10:31:42 -08:00
case 'tags':
$ret = ($raw) ? $value : $tp->toLabel($value,$type);
2017-01-24 19:53:40 -08:00
break;
2020-12-25 10:31:42 -08:00
case 'lanlist':
case 'language':
$ret = ($raw) ? $value : e107::getLanguage()->convert($value);
2017-01-24 19:53:40 -08:00
break;
2020-12-25 10:31:42 -08:00
case 'datestamp':
$ret = ($raw) ? $value : $tp->toDate($value);
2017-01-24 19:53:40 -08:00
break;
2020-12-25 10:31:42 -08:00
case 'file':
2021-06-13 12:59:01 -07:00
if(!empty($value))
2020-12-25 10:31:42 -08:00
{
$ret = ($raw) ? $tp->toFile($value, array('raw'=>1)) : $tp->toFile($value);
}
2017-01-24 19:53:40 -08:00
break;
2020-12-25 10:31:42 -08:00
case 'url':
case 'email':
$ret = ($raw) ? $value : $tp->toHTML($value);
2017-01-24 19:53:40 -08:00
break;
2020-12-25 10:31:42 -08:00
case 'user':
$ret = ($raw) ? $value : e107::getSystemUser($value,true)->getName();
2017-01-24 19:53:40 -08:00
break;
2020-12-25 10:31:42 -08:00
case 'userclass':
$ret = ($raw) ? $value : e107::getUserClass()->getName($value);
2017-01-24 19:53:40 -08:00
break;
2020-12-25 10:31:42 -08:00
case 'progressbar':
if($raw)
{
2020-12-25 10:31:42 -08:00
$ret = (strpos($value, '/') === false) ? $value.'%' : $value;
}
else
{
$ret = e107::getForm()->progressBar($key,$value,$this->_config[$key]);
}
break;
2020-12-25 10:31:42 -08:00
case 'textarea':
case 'bbarea':
$ret = ($raw) ? $value : $tp->toHTML($value, true, 'BODY');
2017-01-24 19:53:40 -08:00
break;
2020-12-25 10:31:42 -08:00
case 'boolean':
if($raw)
{
return $value;
}
2020-12-25 10:31:42 -08:00
else
{
$ret = empty($value) ? $tp->toGlyph('fa-times') : $tp->toGlyph('fa-check');
}
break;
2020-12-25 10:31:42 -08:00
case 'checkbox':
if($raw)
{
2020-12-25 10:31:42 -08:00
$ret = $value;
}
2020-12-25 10:31:42 -08:00
elseif(is_numeric($value))
{
2020-12-25 10:31:42 -08:00
$ret = empty($value) ? $tp->toGlyph('fa-times') : $tp->toGlyph('fa-check');
}
else
{
$ret = $value;
}
break;
2017-01-24 19:53:40 -08:00
default:
$ret = ($raw) ? $value : $tp->toHTML($value);
2017-01-24 19:53:40 -08:00
}
2020-12-25 10:31:42 -08:00
return $ret;
2017-01-24 19:53:40 -08:00
}
/**
* @return string
*/
2017-01-24 19:53:40 -08:00
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)
{
2020-12-25 10:31:42 -08:00
$text .= '<tr><td>' .$ok. '</td><td>' .$this->getFieldTitle($ok). '</td><td>' .$this->getFieldValue($ok). '</td><td>' .$this->getFieldValue($ok, array('mode' =>'raw')). '</td></tr>';
2017-01-24 19:53:40 -08:00
}
2020-12-25 10:31:42 -08:00
$text .= '</table>';
2017-01-24 19:53:40 -08:00
return $text;
}
/**
* @param $key
* @return mixed|null
*/
2017-01-24 19:53:40 -08:00
public function getFieldTitle($key)
{
if(!empty($this->_config[$key]['title']))
{
return $this->_config[$key]['title'];
}
return null;
}
/**
* @param $name
* @return string
*/
2017-01-24 19:53:40 -08:00
public function renderConfigForm($name)
{
2017-01-24 19:53:40 -08:00
$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 = "
<div class='form-group'>
2020-12-25 10:31:42 -08:00
".$frm->text('__e_customfields_tabs__', $this->_tab[$this->_tab_default], 30, array('placeholder' => 'Tab label', 'size' =>'medium', 'required' =>1))."
</div>
<table class='table table-striped table-bordered'>
2017-01-24 19:53:40 -08:00
<colgroup>
<col />
<col />
<col />
<col style='width:40%' />
</colgroup>
<tbody>
2020-12-25 10:31:42 -08:00
<tr><th>".LAN_NAME. '</th><th>' .LAN_TITLE. '</th><th>' .LAN_TYPE. '</th><th>Params</th><th>' .LAN_TOOLTIP. '</th></tr>
';
2017-01-24 19:53:40 -08:00
for($i = 0; $i <= $this->_field_limit; $i++)
2017-01-24 19:53:40 -08:00
{
$writeParms = array(
// 'class' => 'form-control',
2017-01-24 19:53:40 -08:00
'useValues' => 1,
'default' => 'blank',
'data-src' => e_REQUEST_URI,
2017-01-24 19:53:40 -08:00
);
$parmsWriteParms = array(
'size' => 'block-level',
'placeholder' => isset($value[$i]['type']) ? $this->getCustomFieldPlaceholder($value[$i]['type']) : '',
2017-01-24 19:53:40 -08:00
);
$fieldName = $frm->text($name . '[' . $i . '][key]', varset($value[$i]['key']), 30, array('pattern' => '^[a-z0-9-]*'));
$fieldTitle = $frm->text($name . '[' . $i . '][title]', varset($value[$i]['title']), 80);
$fieldType = $frm->select($name . '[' . $i . '][type]', $this->getFieldTypes(), varset($value[$i]['type']), $writeParms);
$fieldParms = $frm->text($name . '[' . $i . '][writeParms]', varset($value[$i]['writeParms']), 255, $parmsWriteParms);
$fieldHelp = $frm->text($name . '[' . $i . '][help]', varset($value[$i]['help']), 255, array('size' => 'block-level'));
2020-12-25 10:31:42 -08:00
$text .= '<tr><td>' . $fieldName . '</td><td>' . $fieldTitle . '</td><td>' . $fieldType . '</td><td>' . $fieldParms . '</td><td>' . $fieldHelp . '</td></tr>';
2017-01-24 19:53:40 -08:00
}
2020-12-25 10:31:42 -08:00
$text .= '</tbody></table>';
2017-01-24 19:53:40 -08:00
return $text;
}
/**
* @param $type
* @return null|string
*/
private function getCustomFieldPlaceholder($type)
{
switch($type)
{
2020-12-25 10:31:42 -08:00
case 'radio':
case 'dropdown':
case 'checkboxes':
2017-01-24 19:53:40 -08:00
return 'eg. { "optArray": { "blue": "Blue", "green": "Green", "red": "Red" }, "default": "blank" }';
break;
2020-12-25 10:31:42 -08:00
case 'datestamp':
2017-01-24 19:53:40 -08:00
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]);
2017-01-25 10:30:28 -08:00
foreach($this->_config as $key=>$fld)
2017-01-24 19:53:40 -08:00
{
$fld['tab'] = $tabKey;
$fld['data'] = false;
if($fld['type'] === 'icon')
{
2020-12-25 10:31:42 -08:00
$fld['writeParms'] .= '&glyphs=1';
2017-01-24 19:53:40 -08:00
}
2017-01-29 18:43:52 -08:00
if($fld['type'] === 'checkboxes')
2017-01-24 19:53:40 -08:00
{
if($tmp = e107::getParser()->isJSON($fld['writeParms']))
{
$fld['writeParms'] = $tmp;
}
$fld['writeParms']['useKeyValues'] = 1;
}
$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)
{
$ui->getModel()->set($fieldname, null);
$tp = e107::getParser();
2017-01-24 19:53:40 -08:00
foreach($this->_data as $key=>$value)
{
$ui->getModel()->set($fieldname.'__'.$key, $tp->toDB($value));
// e107::getDebug()->log($fieldname.'__'.$key.": ".$value);
2017-01-24 19:53:40 -08:00
}
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;
}
$new = array();
if(!empty($postData['__e_customfields_tabs__']))
{
$new['__tabs__'] = array($this->_tab_default => $postData['__e_customfields_tabs__']);
unset($postData['__e_customfields_tabs__']);
}
2017-01-25 10:30:28 -08:00
foreach($postData[$fieldname] as $fields)
2017-01-24 19:53:40 -08:00
{
2017-01-24 19:53:40 -08:00
if(empty($fields['key']) || empty($fields['type']))
{
continue;
}
$key = $fields['key'];
unset($fields['key']);
$new[$key] = $fields;
}
$postData[$fieldname] = empty($new) ? null : $new;
2017-01-25 10:30:28 -08:00
return $postData;
2017-01-24 19:53:40 -08:00
}
/**
* 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);
2017-01-24 19:53:40 -08:00
foreach($new_data as $k=>$v)
{
2021-01-24 17:00:02 -08:00
if(strpos($k, $fieldname) === 0)
2017-01-24 19:53:40 -08:00
{
2020-12-25 10:31:42 -08:00
list($tmp,$newkey) = explode('__',$k);
2017-01-24 19:53:40 -08:00
$new_data[$fieldname][$newkey] = $v;
unset($new_data[$k]);
}
}
2020-12-18 19:55:12 -08:00
// if(empty($new_data[$fieldname]))
// {
// $new_data[$fieldname] = array();
2020-12-18 19:55:12 -08:00
// }
2017-01-24 19:53:40 -08:00
return $new_data;
}
}