From b34318a0289ce1b64179c00c04dfc7c075172e5f Mon Sep 17 00:00:00 2001 From: xia0ta0 Date: Thu, 26 Feb 2015 16:19:57 +0800 Subject: [PATCH] + add dev module. --- module/dev/control.php | 24 ++++++++ module/dev/lang/en.php | 0 module/dev/lang/zh-cn.php | 14 +++++ module/dev/model.php | 101 ++++++++++++++++++++++++++++++++ module/dev/view/api.html.php | 1 + module/dev/view/db.html.php | 43 ++++++++++++++ module/dev/view/header.html.php | 11 ++++ 7 files changed, 194 insertions(+) create mode 100644 module/dev/control.php create mode 100644 module/dev/lang/en.php create mode 100644 module/dev/lang/zh-cn.php create mode 100644 module/dev/model.php create mode 100644 module/dev/view/api.html.php create mode 100644 module/dev/view/db.html.php create mode 100644 module/dev/view/header.html.php diff --git a/module/dev/control.php b/module/dev/control.php new file mode 100644 index 0000000000..c37dafe1df --- /dev/null +++ b/module/dev/control.php @@ -0,0 +1,24 @@ +view->tables = $this->dev->getTables(); + $this->view->tab = 'api'; + $this->view->position[] = html::a(inlink('api'), $this->lang->dev->common); + $this->view->position[] = $this->lang->dev->api; + $this->display(); + } + + public function db($table = '') + { + $this->view->tables = $this->dev->getTables(); + $this->view->tab = 'db'; + $this->view->selectedTable = $table; + $this->view->tab = 'db'; + $this->view->fields = $table ? $this->dev->descTable($table) : array(); + $this->view->position[] = html::a(inlink('api'), $this->lang->dev->common); + $this->view->position[] = $this->lang->dev->db; + $this->display(); + } +} diff --git a/module/dev/lang/en.php b/module/dev/lang/en.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/module/dev/lang/zh-cn.php b/module/dev/lang/zh-cn.php new file mode 100644 index 0000000000..81087e25af --- /dev/null +++ b/module/dev/lang/zh-cn.php @@ -0,0 +1,14 @@ +dev->common = '二次开发'; +$lang->dev->api = 'API'; +$lang->dev->db = '数据库'; +$lang->dev->editor = '编辑器'; +$lang->dev->dbList = '数据库'; + +$lang->dev->fields = array(); +$lang->dev->fields['id'] = '序号'; +$lang->dev->fields['name'] = '字段'; +$lang->dev->fields['desc'] = '描述'; +$lang->dev->fields['type'] = '类型'; +$lang->dev->fields['length'] = '长度'; +$lang->dev->fields['null'] = '是否可空'; diff --git a/module/dev/model.php b/module/dev/model.php new file mode 100644 index 0000000000..1bbeb1cb90 --- /dev/null +++ b/module/dev/model.php @@ -0,0 +1,101 @@ +dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); + $sql = "SHOW TABLES"; + $tables = array(); + $datatables = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); + foreach($datatables as $table) + { + $tables[current($table)] = current($table); + } + return $tables; + } + + public function descTable($table) + { + $module = substr($table, strpos($table, '_') + 1); + try + { + if($module == 'case') $module = 'testcase'; + $this->app->loadLang($module); + } + catch(PDOException $e) + { + $this->lang->$module = new stdclass(); + } + + try + { + $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); + $sql = "DESC $table"; + $rawFields = $this->dbh->query($sql)->fetchAll(); + $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); + } + catch (PDOException $e) + { + $this->sqlError($e); + } + foreach($rawFields as $rawField) + { + $firstPOS = strpos($rawField->type, '('); + $type = substr($rawField->type, 0, $firstPOS > 0 ? $firstPOS : strlen($rawField->type)); + $type = str_replace(array('big', 'small', 'medium', 'tiny'), '', $type); + $field = array(); + $field['name'] = isset($this->lang->$module->{$rawField->field}) ? $this->lang->$module->{$rawField->field} : ''; + $field['null'] = $rawField->null; + + if($type == 'enum' or $type == 'set') + { + $rangeBegin = $firstPOS + 2; // Remove the first quote. + $rangeEnd = strrpos($rawField->type, ')') - 1; // Remove the last quote. + $range = substr($rawField->type, $rangeBegin, $rangeEnd - $rangeBegin); + $field['type'] = $rawField->type; + $field['options']['enum'] = str_replace("','", ',', $range); + } + elseif($type == 'varchar') + { + $begin = $firstPOS + 1; + $end = strpos($rawField->type, ')', $begin); + $length = substr($rawField->type, $begin, $end - $begin); + $field['type'] = 'varchar'; + $field['options']['max'] = $length; + $field['options']['min'] = 0; + } + elseif($type == 'char') + { + $begin = $firstPOS + 1; + $end = strpos($rawField->type, ')', $begin); + $length = substr($rawField->type, $begin, $end - $begin); + $field['type'] = 'char'; + $field['options']['max'] = $length; + $field['options']['min'] = 0; + } + elseif($type == 'int') + { + $begin = $firstPOS + 1; + $end = strpos($rawField->type, ')', $begin); + $length = substr($rawField->type, $begin, $end - $begin); + $field['type'] = 'int'; + $field['options']['max'] = $length; + $field['options']['min'] = 0; + } + elseif($type == 'float' or $type == 'double') + { + $field['type'] = 'float'; + } + elseif($type == 'date') + { + $field['type'] = 'date'; + } + else + { + $field['type'] = $type; + } + $fields[$rawField->field] = $field; + } + return $fields; + } +} diff --git a/module/dev/view/api.html.php b/module/dev/view/api.html.php new file mode 100644 index 0000000000..b8b4163544 --- /dev/null +++ b/module/dev/view/api.html.php @@ -0,0 +1 @@ + diff --git a/module/dev/view/db.html.php b/module/dev/view/db.html.php new file mode 100644 index 0000000000..e3f9f040c2 --- /dev/null +++ b/module/dev/view/db.html.php @@ -0,0 +1,43 @@ + +
+
+
dev->dbList?>
+ + + + +
+
+
+ + + + + + + + + + + + + + + + + + $field):?> + + + + + + + + + + +
dev->fields['id']?>dev->fields['name']?>dev->fields['desc']?>dev->fields['type']?>dev->fields['length']?>dev->fields['null']?>
+ +
+ diff --git a/module/dev/view/header.html.php b/module/dev/view/header.html.php new file mode 100644 index 0000000000..caa86c9018 --- /dev/null +++ b/module/dev/view/header.html.php @@ -0,0 +1,11 @@ + +
+ +
+