<?php namespace Phpcmf\Field;
class Pay extends \Phpcmf\Library\A_Field {
public function __construct(...$params) {
parent::__construct(...$params);
$this->fieldtype = ['DECIMAL' => '10,2']; $this->defaulttype = 'DECIMAL'; }
public function option($option) {
!$option['payfile'] && $option['payfile'] = 'buy.html';
$opt = '
<div class="form-group">
<label class="col-md-2 control-label">'.dr_lang('模板文件').'</label>
<div class="col-md-9">
<label><input type="text" class="form-control" size="20" name="data[setting][option][payfile]" value="'.$option['payfile'].'"></label>
<span class="help-block">'.dr_lang('模板位于./config/pay/模板文件名').'</span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">'.dr_lang('余额付款').'</label>
<div class="col-md-9">
<input type="checkbox" name="data[setting][option][is_finecms]" '.($option['is_finecms'] ? 'checked' : '').' value="1" data-on-text="'.dr_lang('开启').'" data-off-text="'.dr_lang('关闭').'" data-on-color="success" data-off-color="danger" class="make-switch" data-size="small">
</div>
</div>
';
$style = '
<div class="form-group">
<label class="col-md-2 control-label">'.dr_lang('控件宽度').'</label>
<div class="col-md-9">
<label><input type="text" class="form-control" size="10" name="data[setting][option][width]" value="'.$option['width'].'"></label>
<span class="help-block">'.dr_lang('[整数]表示固定宽带;[整数%]表示百分比').'</span>
</div>
</div>
';
return [$opt, $style];
}
public function insert_value($field) {
\Phpcmf\Service::L('Field')->data[$field['ismain']][$field['fieldname']] = (float)\Phpcmf\Service::L('Field')->post[$field['fieldname']];
}
public function input($field, $value = '', $html = []) {
if (!defined('FC_PAY') && (IS_MEMBER || IS_ADMIN)) {
$text = ($field['setting']['validate']['required'] ? '<span class="required" aria-required="true"> * </span>' : '').$field['name'];
$width = \Phpcmf\Service::_is_mobile() ? '100%' : ((int)$field['setting']['option']['width'] ? $field['setting']['option']['width'] : 250);
$attr = 'style="width:'.$width.(is_numeric($width) ? 'px' : '').';" '.$field['setting']['validate']['formattr'];
$tips = $field['setting']['validate']['tips'] ? '<span class="help-block" id="dr_'.$field['fieldname'].'_tips">'.$field['setting']['validate']['tips'].'</span>' : '';
$required = $field['setting']['validate']['required'] ? ' required="required"' : '';
$value = strlen($value) ? $value : $this->get_default_value($field['setting']['option']['value']);
$value = strlen($value) ? $value : $this->get_default_value($field['setting']['option']['value']);
$ipt = '<input class="form-control '.$field['setting']['option']['css'].'" type="text" name="data['.$field['fieldname'].']" id="dr_'.$field['fieldname'].'" value="'.$value.'" '.$required.' '.$attr.' />';
return $this->input_format($field['fieldname'], $text, $ipt.$tips);
} else {
$html['pay_value'] = $value ? $value : '';
$html['pay_type'] = [];
$html['pay_default'] = '';
if (\Phpcmf\Service::C()->member
&& $field['setting']['option']['is_finecms']
&& is_file(ROOTPATH.'api/pay/finecms/config.php')) {
$html['pay_default'] = 'finecms';
$html['pay_type']['finecms'] = require ROOTPATH.'api/pay/finecms/config.php';
}
if (\Phpcmf\Service::C()->member_cache['payapi']) {
foreach (\Phpcmf\Service::C()->member_cache['payapi'] as $name => $t) {
if (!is_file(ROOTPATH.'api/pay/'.$name.'/config.php')) {
continue; }
!$html['pay_default'] && $html['pay_default'] = $name;
$html['pay_type'][$name] = require ROOTPATH.'api/pay/'.$name.'/config.php';
}
}
$htmlfile = $field['setting']['option']['payfile'] && is_file(WEBPATH.'config/pay/'.$field['setting']['option']['payfile']) ? WEBPATH.'config/pay/'.$field['setting']['option']['payfile'] : ROOTPATH.'config/pay/buy.html';
if (!is_file($htmlfile)) {
return '支付表单模板文件不存在:'.$htmlfile;
}
$member = \Phpcmf\Service::C()->member;
$pay_url = \Phpcmf\Service::L('router')->member_url('pay/index');
ob_start();
$file = \Phpcmf\Service::V()->code2php(file_get_contents($htmlfile));
require $file;
$code = ob_get_clean();
return $code;
}
}
public function show($field, $value = null) {
$url = 'dr_iframe_show(\'cart\', \'/index.php?s=cart&c=show&m=index&fid='.$field['id'].'&id='.$this->id.'\')';
return $this->input_format($field['fieldname'], $field['name'], '<div class="form-control-static"><a href="javascript:'.$url.'">¥'.$value.'元</a></div>');
}
}