我上传字段有问题。 Error page image
当我将上传字段添加到表单脚本时,Actualy会出错。
服务器为此行提供了错误:$filenamefields[$group]['sub']
foreach ($fields as $group => $fieldlist) {
if (isset($fieldlist['active']) && $fieldlist['active']) {
$min_req = true;
if (isset($fieldlist['sub'])) {
$this->customrule_addrec(
$fieldlist['sub'],
$required, $title,
$type,
$gprefix.'[' . $group . '][sub]',
$filenamefields[$group]['sub'],
$filetypefields[$group]['sub'],
$filetmpnamefields[$group]['sub'],
$fileerrorfields[$group]['sub'],
$filesizefields[$group]['sub']);
和这一行:$this->files['size']['fields']);
$min_req = $this->customrule_addrec($fields, $required, $title, $type, '',
$this->files['name']['fields'],
$this->files['type']['fields'],
$this->files['tmp_name']['fields'],
$this->files['error']['fields'],
$this->files['size']['fields']);
if (!$min_req)
$this->addError($attribute, 'Select a group');
文件代码:
<?php
class Orders extends CActiveRecord
{
public $fields;
public $files;
public $file;
public $remove;
public function tableName()
{
return 'orders';
}
public function rules()
{
return array(
array(
'name, start_date, ordertype, user',
'required'
),
array(
'status, ordertype, user',
'numerical',
'integerOnly' => true
),
array(
'name',
'length',
'max' => 200
),
array(
'finish_date, desc',
'safe'
),
array(
'id, name, start_date, finish_date, status, ordertype, user, desc',
'safe',
'on' => 'search'
),
array(
'fields,files',
'safe'
),
array(
'fields',
'customrule_add',
'on' => 'add'
),
array(
'fields',
'customrule_edit',
'on' => 'edit'
),
array(
'file, remove',
'safe',
'on' => 'answer'
)
);
}
public function customrule_add($attribute, $params)
{
$fieldtypes = Orderfield::model()->findAll('ordergroup in (select id from ' . Ordergroup::model()->tableName() . ' where orderform = \'' . $this->ordertype . '\')');
$required = CHtml::listData($fieldtypes, 'id', 'required');
$title = CHtml::listData($fieldtypes, 'id', 'name');
$type = CHtml::listData($fieldtypes, 'id', 'type');
$fields = $this->$attribute;
$min_req = $this->customrule_addrec($fields, $required, $title, $type, '',
$this->files['name']['fields'],
$this->files['type']['fields'],
$this->files['tmp_name']['fields'],
$this->files['error']['fields'],
$this->files['size']['fields']);
if (!$min_req)
$this->addError($attribute, 'Select a group');
}
private function customrule_addrec($fields, $required, $title, $type, $gprefix, $filenamefields, $filetypefields, $filetmpnamefields, $fileerrorfields, $filesizefields)
{
$min_req = false;
foreach ($fields as $group => $fieldlist) {
if (isset($fieldlist['active']) && $fieldlist['active']) {
$min_req = true;
if (isset($fieldlist['sub'])) {
$this->customrule_addrec(
$fieldlist['sub'],
$required, $title,
$type,
$gprefix.'[' . $group . '][sub]',
$filenamefields[$group]['sub'],
$filetypefields[$group]['sub'],
$filetmpnamefields[$group]['sub'],
$fileerrorfields[$group]['sub'],
$filesizefields[$group]['sub']);
foreach ($fieldlist['sub'] as $sgroup => $sfieldlist) {
if (isset($sfieldlist['active']) && $sfieldlist['active']) {
foreach ($sfieldlist as $key => $value) {
if (($key != 'active') && ($key != 'sub') && $required[$key]) {
if ($type[$key] != 2) {
if (!$value)
$this->addError('fields[' . $group . '][sub][' . $sgroup . '][' . $key . ']', 'Value ' . $title[$key] . ' Can not be empty');
} else if (!isset($this->files['name']['fields'][$group]['sub'][$sgroup][$key]) || !$this->files['name']['fields'][$group]['sub'][$sgroup][$key]) {
$this->addError('fields[' . $group . '][sub][' . $sgroup . '][' . $key . ']', 'File ' . $title[$key] . ' Must send');
}
}
}
}
}
}
foreach ($fieldlist as $key => $value) {
if (($key != 'active') && ($key != 'sub') && $required[$key]) {
if ($type[$key] != 2) {
if (!$value)
$this->addError('fields' . $gprefix . '[' . $group . '][' . $key . ']', 'Value ' . $title[$key] . ' Can not be empty');
} else if (!isset($filenamefields[$group][$key]) || !$filenamefields[$group][$key]) {
$this->addError('fields' . $gprefix . '[' . $group . '][' . $key . ']', 'File ' . $title[$key] . ' Must send');
}
}
}
}
}
return $min_req;
}
public function customrule_edit($attribute, $params)
{
$fieldtypes = Orderfield::model()->findAll('ordergroup in (select id from ' . Ordergroup::model()->tableName() . ' where orderform = \'' . $this->ordertype . '\')');
$required = CHtml::listData($fieldtypes, 'id', 'required');
$title = CHtml::listData($fieldtypes, 'id', 'name');
$type = CHtml::listData($fieldtypes, 'id', 'type');
$groups = CHtml::listData(Ordergroup::model()->findAll(array(
'select' => 'id,name,orderform',
'condition' => 'orderform = \'' . $this->ordertype . '\''
)), 'id', 'name');
$fields = $this->$attribute;
$min_req = false;
foreach ($fields as $group => $fieldlist) {
if (isset($fieldlist['active']) && $fieldlist['active']) {
$min_req = true;
if (isset($fieldlist['sub'])) {
foreach ($fieldlist['sub'] as $sgroup => $sfieldlist) {
if (isset($sfieldlist['active']) && $sfieldlist['active']) {
foreach ($sfieldlist as $key => $value) {
if (($key != 'active') && ($key != 'sub') && $required[$key]) {
if ($type[$key] != 2) {
if (!$value)
$this->addError('fields[' . $group . '][sub][' . $sgroup . '][' . $key . ']', 'value ' . $title[$key] . ' Can not be empty');
}
}
}
}
}
}
foreach ($fieldlist as $key => $value) {
if (($key != 'active') && $required[$key]) {
if ($type[$key] != 2) {
if (!$value)
$this->addError('fields[' . $group . '][' . $key . ']', 'value ' . $title[$key] . 'Can not be empty');
}
}
}
}
}
if (!$min_req)
$this->addError($attribute, 'Choose a group');
}
public function add()
{
$name = $this->name;
$exists = Orders::model()->exists('`name` = \'' . $name . '\'');
if ($exists) {
$count = Orders::model()->count('`name` regexp \'^' . $name . ' - [0-9]+$\'');
$this->name = $name . ' - ' . ($count + 2);
}
$status = $this->save();
$status = $this->addsub(
$this->fields,
$status,
$this->files['name']['fields'],
$this->files['type']['fields'],
$this->files['tmp_name']['fields'],
$this->files['error']['fields'],
$this->files['size']['fields']
);
if (!$status) {
Ordervalues::model()->deleteAllByAttributes(array(
'order' => $this->id
));
GroupOfOrder::model()->deleteAllByAttributes(array(
'order' => $this->id
));
$this->delete();
return false;
}
return true;
}
private function addsub($thisfields, $status, $filenamefields, $filetypefields, $filetmpnamefields, $fileerrorfields, $filesizefields)
{
foreach ($thisfields as $group => $fieldlist) {
if (isset($fieldlist['active']) && $fieldlist['active'] && $status) {
$gofo = new GroupOfOrder('insert');
$gofo->group = $group;
$gofo->order = $this->id;
$gofo->save();
if (isset($fieldlist['sub'])) {
$status = $this->addsub(
$fieldlist['sub'],
$status,
$filenamefields[$group]['sub'],
$filetypefields[$group]['sub'],
$filetmpnamefields[$group]['sub'],
$fileerrorfields[$group]['sub'],
$filesizefields[$group]['sub']
);
if(!$status)
return false;
}
foreach ($fieldlist as $key => $value) {
if ($key != 'active' && $key != 'sub' && $status) {
$ftype = Orderfield::model()->findByPk($key);
if (!$ftype) {
$this->addError('fields', 'Field type error');
Ordervalues::model()->deleteAllByAttributes(array(
'order' => $this->id
));
GroupOfOrder::model()->deleteAllByAttributes(array(
'order' => $this->id
));
$this->delete();
return false;
}
if (!empty($value)) {
$field = new Ordervalues('insert');
$field->field = $key;
$field->order = $this->id;
$field->value = $value;
$status = ($status && $field->save());
} elseif (isset($filenamefields[$group][$key]) && !empty($filenamefields[$group][$key])) {
$file = new CUploadedFile($filenamefields[$group][$key], $filetmpnamefields[$group][$key], $filetypefields[$group][$key], $filesizefields[$group][$key], $fileerrorfields[$group][$key]);
if ($ftype->file_type) {
$all = explode("|", $ftype->file_type); // check every allowed extensions with uploaded file
$allowed = false;
foreach ($all as $ex) {
if ($ex == $file->extensionName) {
$allowed = true;
break;
}
}
if (!$allowed) {
$this->addError('files', 'This format' . $file->extensionName . 'is not true');
Ordervalues::model()->deleteAllByAttributes(array(
'order' => $this->id
));
GroupOfOrder::model()->deleteAllByAttributes(array(
'order' => $this->id
));
$this->delete();
return false;
}
}
$ffield = new Ordervalues('insert');
$ffield->field = $key;
$ffield->order = $this->id;
$ffield->value = $file->name;
$ffield->file_size = $file->size;
$status = ($status && $ffield->save());
$status = ($status && $file->saveAs('files/' . $ffield->id));
}
}
}
}
}
return true;
}
public function edit()
{
$status = true;
foreach ($this->fields as $group => $fieldlist) {
if (isset($fieldlist['active']) && $fieldlist['active']) {
$gofo = GroupOfOrder::model()->findByAttributes(array(
'order' => $this->id,
'group' => $group
));
if (!$gofo) {
$gofo = new GroupOfOrder('insert');
$gofo->group = $group;
$gofo->order = $this->id;
$gofo->save();
}
if (isset($fieldlist['sub'])) {
foreach ($fieldlist['sub'] as $sgroup => $sfieldlist) {
if (isset($sfieldlist['active']) && $sfieldlist['active']) {
$sgofo = GroupOfOrder::model()->findByAttributes(array(
'order' => $this->id,
'group' => $sgroup
));
if (!$sgofo) {
$sgofo = new GroupOfOrder('insert');
$sgofo->group = $sgroup;
$sgofo->order = $this->id;
$sgofo->save();
}
foreach ($sfieldlist as $key => $value) {
if ($key != 'active' && $key != 'sub' && $status) {
$ftype = Orderfield::model()->findByPk($key);
if (!$ftype) {
$this->addError('fields', 'Field type error');
return false;
}
if (!empty($value)) {
$field = Ordervalues::model()->findByAttributes(array(
'order' => $this->id,
'field' => $key
));
if (!$field) {
$field = new Ordervalues('insert');
$field->field = $key;
$field->order = $this->id;
}
$field->value = $value;
$status = ($status && $field->save());
} elseif (isset($this->files['name']['fields'][$group]['sub'][$sgroup][$key]) && !empty($this->files['name']['fields'][$group]['sub'][$sgroup][$key])) {
$file = new CUploadedFile($this->files['name']['fields'][$group]['sub'][$sgroup][$key], $this->files['tmp_name']['fields'][$group]['sub'][$sgroup][$key], $this->files['type']['fields'][$group]['sub'][$sgroup][$key], $this->files['size']['fields'][$group]['sub'][$sgroup][$key], $this->files['error']['fields'][$group]['sub'][$sgroup][$key]);
if ($ftype->file_type) {
$all = explode("|", $ftype->file_type); // check every allowed extensions with uploaded file
$allowed = false;
foreach ($all as $ex) {
if ($ex == $file->extensionName) {
$allowed = true;
break;
}
}
if (!$allowed) {
$this->addError('files', 'This format ' . $file->extensionName . ' is not true');
return false;
}
}
$ffield = Ordervalues::model()->findByAttributes(array(
'order' => $this->id,
'field' => $key
));
if (!$ffield) {
$ffield = new Ordervalues('insert');
$ffield->field = $key;
$ffield->order = $this->id;
}
$ffield->value = $file->name;
$ffield->file_size = $file->size;
$status = ($status && $ffield->save());
$status = ($status && $file->saveAs('files/' . $ffield->id));
}
}
}
}
}
}
foreach ($fieldlist as $key => $value) {
if ($key != 'active' && $key != 'sub' && $status) {
$ftype = Orderfield::model()->findByPk($key);
if (!$ftype) {
$this->addError('fields', 'Field error');
return false;
}
if (!empty($value)) {
$field = Ordervalues::model()->findByAttributes(array(
'order' => $this->id,
'field' => $key
));
if (!$field) {
$field = new Ordervalues('insert');
$field->field = $key;
$field->order = $this->id;
}
$field->value = $value;
$status = ($status && $field->save());
} elseif (isset($this->files['name']['fields'][$group][$key]) && !empty($this->files['name']['fields'][$group][$key])) {
$file = new CUploadedFile($this->files['name']['fields'][$group][$key], $this->files['tmp_name']['fields'][$group][$key], $this->files['type']['fields'][$group][$key], $this->files['size']['fields'][$group][$key], $this->files['error']['fields'][$group][$key]);
if ($ftype->file_type) {
$all = explode("|", $ftype->file_type); // check every allowed extensions with uploaded file
$allowed = false;
foreach ($all as $ex) {
if ($ex == $file->extensionName) {
$allowed = true;
break;
}
}
if (!$allowed) {
$this->addError('files', 'This format ' . $file->extensionName . ' is not true');
return false;
}
}
$ffield = Ordervalues::model()->findByAttributes(array(
'order' => $this->id,
'field' => $key
));
if (!$ffield) {
$ffield = new Ordervalues('insert');
$ffield->field = $key;
$ffield->order = $this->id;
}
$ffield->value = $file->name;
$ffield->file_size = $file->size;
$status = ($status && $ffield->save());
$status = ($status && $file->saveAs('files/' . $ffield->id));
}
}
}
} else {
$gofo = GroupOfOrder::model()->findByAttributes(array(
'order' => $this->id,
'group' => $group
));
if ($gofo) {
$gofo->delete();
$ovs = Ordervalues::model()->findAll('order = ' . $this->id . ' AND field in (select id from ' . Orderfield::model()->tableName() . ' where ordergroup=' . $group . ')');
foreach ($ovs as $ov) {
if ($ov->field0->type == 2)
unlink(Yii::app()->getBasePath() . '/../files/' . $ov->id);
$ov->delete();
}
}
}
}
$status = ($status && $this->save());
return $status;
}
public function answer()
{
if ($this->remove) {
foreach ($this->remove as $id => $f) {
if ($f) {
$filemodel = Orderfiles::model()->findByAttributes(array(
'id' => $id,
'order_id' => $this->id
));
if ($filemodel) {
$filemodel->delete();
$path = Yii::app()->basePath . '/../files/answers/' . $id;
if (file_exists($path))
unlink($path);
}
}
}
}
$file = CUploadedFile::getInstance($this, 'file');
if ($file) {
$fm = new Orderfiles('insert');
$fm->name = $file->name;
$fm->size = $file->size;
$fm->order_id = $this->id;
if ($fm->save() && $this->save())
return $file->saveAs('files/answers/' . $fm->id);
return false;
}
return $this->save();
}
public function fill_in_fields()
{
$this->fields = array();
foreach ($this->ordervalues as $field) {
$f = $field->field0;
if (!isset($this->fields[$f->ordergroup]['active']))
$this->fields[$f->ordergroup]['active'] = true;
$this->fields[$f->ordergroup][$f->id] = $field->value;
}
}
public function getPrice()
{
return Yii::app()->db->createCommand('select sum(price) from ' . Ordergroup::model()->tableName() . ' where id in (select `group` from ' . GroupOfOrder::model()->tableName() . ' where `order` = ' . $this->id . ')')->queryScalar();
}
public function deleteTree()
{
$ovsff = Ordervalues::model()->findAll('`order` = ' . $this->id . ' AND field in (select id from ' . Orderfield::model()->tableName() . ' where type = 2)');
if ($ovsff) {
foreach ($ovsff as $value) {
$path = Yii::app()->getBasePath() . '/../files/' . $value->id;
if (file_exists($path))
unlink($path);
}
}
Ordervalues::model()->deleteAll('`order` = ' . $this->id);
GroupOfOrder::model()->deleteAll('`order` = ' . $this->id);
$status = true;
$ovsff = Orderfiles::model()->findAll('`order_id` = ' . $this->id); // files of answered orders
if ($ovsff) {
foreach ($ovsff as $value) {
$path = Yii::app()->getBasePath() . '/../files/answers/' . $value->id;
if (file_exists($path))
unlink($path);
$status = $status && $value->delete();
}
}
$trans = $this->trackCode;
$status = $this->delete();
$trans->delete();
return $status;
}
public function relations()
{
return array(
'orderfiles' => array(
self::HAS_MANY,
'Orderfiles',
'order_id'
),
'ordertype0' => array(
self::BELONGS_TO,
'Orderform',
'ordertype'
),
'trackCode' => array(
self::BELONGS_TO,
'Transaction',
'track_code'
),
'user0' => array(
self::BELONGS_TO,
'Users',
'user'
),
'ordervalues' => array(
self::HAS_MANY,
'Ordervalues',
'order'
),
'tickets' => array(
self::HAS_MANY,
'Ticket',
'order_id'
)
);
}
public function attributeLabels()
{
return array(
'id' => 'Code',
'name' => 'Name',
'start_date' => 'Date',
'finish_date' => 'End Date',
'status' => 'status',
'ordertype' => 'Type',
'user' => 'User',
'track_code' => 'Pay code',
'desc' => 'Description',
'file' => 'File'
);
}
public function getStatus()
{
$status = array(
0 => 'Waiting for pay',
1 => 'In progress',
2 => 'Completed',
3 => 'Draft'
);
return $status[$this->status];
}
public function getButton()
{
$status = array(
0 => 'Waiting for pay',
1 => 'In progress',
2 => 'Completed',
4 => 'Draft'
);
if ($this->status == 0) {
return CHtml::button('Pay', array(
'onclick' => 'window.location.href=\'' . Yii::app()->createUrl('financial/invoice', array(
'id' => $this->track_code
)) . '\''
));
}
return CHtml::button('Details', array(
'onclick' => 'window.location.href=\'' . Yii::app()->createUrl('service/order', array(
'id' => $this->id
)) . '\''
));
}
public function search()
{
$criteria = new CDbCriteria();
$criteria->compare('id', $this->id);
$criteria->compare('name', $this->name, true);
$criteria->compare('start_date', $this->start_date, true);
$criteria->compare('finish_date', $this->finish_date, true);
$criteria->compare('status', $this->status);
$criteria->compare('ordertype', $this->ordertype);
$criteria->compare('user', $this->user);
$criteria->compare('track_code', $this->track_code);
$criteria->compare('desc', $this->desc, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria
));
}
public static function model($className = __CLASS__)
{
return parent::model($className);
}
}