checkFunc(); $remote = $show = array(); $data['update'] = $data['field'] = $data['option'] = array(); $this->setting('field', $data['update'], true, 'text'); foreach ($data['update'] as $k => $v) { if ($v['type'] == 'tree' || $v['type'] == 'upload') { $v['value'] = array(); } if (isset($v['remote'])) { # 需要设置remote $remote[$v['key']] = array($k, $v['remote']); } if (isset($v['show']) && is_string($v['show'])) { $show[$v['key']] = array($k, $v['show']); } $data['field'][$v['key']] = $v['value']; if (isset($v['option'])) { $data['option'][$v['key']] = $v['option']; unset($data['update'][$k]['option']); } } $this->column($data); $data['info_id'] = false; if (!$this->info && $data['column'] && $data['column']['where'] == 'id') { $this->info = $this->db->find($data['column']['active']); if ($this->info) { $data['info_id'] = $this->info['id']; if (!$func) { $func = $this->getFunc('edit', '编辑', 1); if (!$func) { Dever::error('无操作权限'); } } } } elseif (!$func) { $func = $this->getFunc('update', '更新', 1); if (!$func) { Dever::error('无操作权限'); } } if ($this->info) { $info = $this->info; $this->setInfo($info, $data, $remote, $show); } elseif ($value) { foreach ($value as $k => $v) { $this->setInfo($v, $data, $remote, $show); $option[$k] = $data['option']; } } else { if ($remote) { foreach ($remote as $k => $v) { $data['update'][$v[0]]['remote'] = Dever::url($v[1]); } } if ($show) { foreach ($show as $k => $v) { $data['update'][$v[0]]['show'] = true; } } } $data['desc'] = $this->config['desc'] ?? ''; $data['drag'] = $this->config['drag'] ?? false; $this->layout($data); $this->tab($data, 'step'); if (!$data['step']) { $this->tab($data); } $this->control($data); return $data; } private function setInfo($info, &$data, $remote, $show) { foreach ($info as $k => $v) { if (isset($data['field'][$k])) { if (is_array($data['field'][$k])) { if ($v) { $v = explode(',', $v); } else { $v = array(); } } if (isset($this->config['field'][$k]) && isset($this->config['field'][$k]['update'])) { $v = $this->config['field'][$k]['update']; } $data['field'][$k] = $v; if (isset($remote[$k])) { $data['update'][$remote[$k][0]]['remote'] = Dever::url($remote[$k][1]); $result = Dever::call($remote[$k][1], array($v), 'api'); if ($result) { $this->setUpdate($data['update'], $data['option'], $result); } } if (isset($show[$k])) { $data['update'][$show[$k][0]]['show'] = $this->getShow($show[$k][1], $info); } } } } private function setUpdate(&$update, &$option, $result) { foreach ($update as $k => $v) { if (isset($result[$v['key']])) { if (isset($result[$v['key']]['option'])) { $option[$v['key']] = $result[$v['key']]['option']; unset($result[$v['key']]['option']); } $update[$k] = array_merge($update[$k], $result[$v['key']]); } } } private function control(&$data) { $data['control'] = array(); if (isset($this->config['control']) && $data['control'] = $this->config['control']) { foreach ($data['control'] as $k => $v) { foreach ($data['update'] as $k1 => $v1) { if ($v1['key'] == $k) { $show = true; foreach ($v as $k2 => $v2) { if (is_array($v2)) { $temp = false; foreach ($v2 as $k3 => $v3) { if (is_array($data['field'][$k2]) && in_array($v3, $data['field'][$k2])) { $temp = true; } elseif ($data['field'][$k2] == $v3) { $temp = true; } } $show = $temp; } else { if (is_array($data['field'][$k2]) && !in_array($v2, $data['field'][$k2])) { $show = false; } elseif ($data['field'][$k2] != $v2) { $show = false; } } } $data['update'][$k1]['show'] = $show; } } } } } 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() { $this->checkFunc(); $update = array(); $this->setting('field', $update, true, 'text'); if ($update) { $data = $other = 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['rules'])) { $this->checkRules($v, $input[$v['key']]); } if (strpos($v['key'], '/')) { $other[$v['key']] = array($v['where'], $v['content']['field'], $v['content']['drag'], $input[$v['key']]); } else { $this->doData($data, $v['key'], $input[$v['key']]); } } elseif ($id) { $data[$v['key']] = ''; } if (isset($data[$v['key']]) && !$data[$v['key']] && isset($v['empty']) && !$v['empty']) { unset($data[$v['key']]); } } if (!$data) { Dever::error('无效数据'); } $this->exists($id, $data); $this->start($id, $data); if ($id) { $info = $this->db->find($id); if ($info) { $state = $this->db->update($info['id'], $data); if ($state) { $id = $info['id']; } } else { $data['id'] = $id; $id = $this->db->insert($data); } } else { $id = $this->db->insert($data); } if (!$id) { Dever::error('操作失败'); } $this->end($id, $data); $this->other($id, $data, $other); return '操作成功'; } } private function doData(&$data, $key, $value) { if (is_array($value)) { if (isset($value[0])) { $value = ltrim(implode(',', $value), ','); } else { $value = Dever::json_encode($value); } } if ($value && isset($this->config['field'][$key]) && $handle = Dever::isset($this->config['field'][$key], 'handle')) { $value = Dever::call($handle, array($value)); if (is_array($value) && isset($value[$key])) { foreach ($value as $k => $v) { $data[$k] = $v; } return; } } $data[$key] = $value; } private function exists($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']) { $data['id'] = $id; $data = Dever::call($this->config['start'], array($data)); } } private function end($id, $data) { if (isset($this->config['end']) && $this->config['end']) { $data['id'] = $id; Dever::call($this->config['end'], array($data)); } } private function other($rid, $data, $other) { if ($other) { foreach ($other as $k => $v) { $common = $v[0]; $update = $v[1]; $drag = $v[2]; $input = $v[3]; $value = array(); foreach ($input as $k1 => $v1) { if (isset($v1['id'])) { $value['id'] = $v1['id']; } foreach ($common as $k2 => $v2) { if ($v2 == 'id') { $value[$k2] = $rid; } elseif (isset($data[$v2])) { $value[$k2] = $data[$v2]; } } foreach ($update as $k2 => $v2) { if (isset($v1[$k2])) { $value[$k2] = $v1[$k2]; } } if ($drag) { $value[$drag] = $k1+1; } if (isset($value['id']) && $value['id'] > 0) { $id = $value['id']; unset($value['id']); Dever::db($k)->update($id, $value); } else { Dever::db($k)->insert($value); } } } } } }