setting('field', $data['update'], true, 'text'); foreach ($data['update'] as $k => $v) { $data['field'][$v['key']] = $v['value']; } if ($this->id) { $data['info'] = $this->db->find($this->id); if ($data['info']) { foreach ($data['info'] as $k => $v) { if (isset($data['field'][$k])) { if (is_array($data['field'][$k])) { $v = explode(',', $v); } if (isset($this->config['field'][$k]) && isset($this->config['field'][$k]['value'])) { $v = $this->config['field'][$k]['value']; } $data['field'][$k] = $v; } } } } $data['desc'] = $this->config['desc'] ?? ''; $this->layout($data); $this->tab($data, 'step'); if (!$data['step']) { $this->tab($data); } return $data; } private function tab(&$data, $type = 'tab') { $field = Dever::input('field'); $data[$type] = array(); if (empty($data['layout']) && !$field && isset($this->config[$type])) { foreach ($this->config[$type] as $k => $v) { if (is_string($v)) { $field = array(); $data[$type][] = array ( 'name' => $k, 'update' => $this->getUpdate($v, $data['update'], $field), 'field' => $field, ); } else { $field = array(); $result = array(); $result['name'] = $k; foreach ($v as $v1) { $result['layout'][] = $this->getUpdate($v1, $data['update'], $field); } $result['field'] = $field; $data[$type][] = $result; } } $data['update'] = array(); } } private function layout(&$data) { $field = Dever::input('field'); $data['layout'] = array(); if (!$field && isset($this->config['layout'])) { foreach ($this->config['layout'] as $k => $v) { $field = array(); $data['layout'][] = $this->getUpdate($v, $data['update'], $field); } $data['update'] = array(); } } private function getUpdate($set, $update, &$field) { $result = array(); if (is_string($set)) { $set = explode(',', $set); foreach ($set as $k => $v) { foreach ($update as $value) { if ($value['key'] == $v) { $result[] = $value; $field[] = $v; } } } } else { foreach ($set as $k => $v) { foreach ($update as $value) { if ($value['key'] == $k) { $result[] = array('span' => $v, 'update' => array($value)); $field[] = $k; } } } } return $result; } public function do_commit(){} public function do() { $update = array(); $this->setting('update', $update, true, 'text'); if ($update) { $data = array(); $input = Dever::input(); $id = false; if (isset($input['id']) && $input['id'] > 0) { $id = $input['id']; } foreach ($update as $k => $v) { if (isset($input[$v['key']])) { if (isset($v['rule'])) { $this->checkRules($v, $input[$v['key']]); } $this->doData($data, $v['key'], $input[$v['key']]); } elseif ($id) { $data[$v['key']] = ''; } } if (!$data) { Dever::error('无效数据'); } $this->check($id, $data); $this->start($id, $data); if ($id) { $info = $this->db->find($id); if (!$info) { Dever::error('无效数据'); } $state = $this->db->update($info['id'], $data); if ($state) { $id = $info['id']; } } else { $id = $this->db->insert($data); } if (!$id) { Dever::error('操作失败'); } $this->end($id, $data); return '操作成功'; } } private function doData(&$data, $key, $value) { if (is_array($value)) { if (isset($value[0])) { $value = ltrim(implode(',', $value), ','); } else { $value = Dever::json_ecode($value); } } if ($value && isset($this->config['field'][$key]) && $handle = Dever::isset($this->config['field'][$key], 'handle')) { $value = Dever::call($handle, $value); if (is_array($value) && isset($value[$key])) { foreach ($value as $k => $v) { $data[$k] = $v; } return; } } $data[$key] = $value; } private function check($id, $data) { if (isset($this->config['check']) && $this->config['check']) { $check = explode(',', $this->config['check']); $where = array(); $name = array(); foreach ($check as $k => $v) { if (isset($data[$v])) { if (isset($this->config['field'][$v]) && isset($this->config['field'][$v]['name'])) { $n = $this->config['field'][$v]['name']; } elseif (isset($this->db->config['struct'][$v])) { $n = $this->db->config['struct'][$v]['name']; } else { $n = $v; } $where[$v] = $data[$v]; $name[] = $n; } } if ($where) { if ($id) { $where['id'] = array('!=', $id); } $info = $this->db->find($where); if ($info) { $name = implode('、', $name); Dever::error($name . '已存在'); } } } } private function start($id, &$data) { if (isset($this->config['start']) && $this->config['start']) { echo $this->config['start'];die; } } private function end($id, $data) { if (isset($this->config['end']) && $this->config['end']) { echo $this->config['end'];die; } } }