1: <?php namespace Laravella\Crud;
2:
3: use Laravella\Crud\Log;
4: use \Seeder;
5: use \DB;
6:
7: class SeedTables extends CrudSeeder {
8:
9: private function addWidgetTypes()
10: {
11: $wTypes = array();
12:
13: DB::table('_db_widget_types')->delete();
14:
15: $wTypes['input:text'] = $this->addWidgetType('input:text');
16: $wTypes['input:hidden'] = $this->addWidgetType('input:hidden');
17: $wTypes['input:text'] = $this->addWidgetType('input:text');
18: $wTypes['input:checkbox'] = $this->addWidgetType('input:checkbox');
19: $wTypes['input:radio'] = $this->addWidgetType('input:radio');
20: $wTypes['textarea'] = $this->addWidgetType('textarea');
21: $wTypes['select'] = $this->addWidgetType('select');
22: $wTypes['multiselect'] = $this->addWidgetType('multiselect');
23: $wTypes['ckeditor'] = $this->addWidgetType('ckeditor');
24: $wTypes['span'] = $this->addWidgetType('span');
25: $wTypes['password'] = $this->addWidgetType('password');
26: $wTypes['password:hashed'] = $this->addWidgetType('password:hashed');
27: $wTypes['password:md5'] = $this->addWidgetType('password:md5');
28: $wTypes['thumbnail'] = $this->addWidgetType('thumbnail');
29: }
30:
31: private function addDisplayTypes()
32: {
33: $types = array();
34: DB::table('_db_display_types')->delete();
35:
36: 37: 38:
39: $types['nodisplay'] = $this->addDisplayType('nodisplay');
40: $types['edit'] = $this->addDisplayType('edit');
41: $types['display'] = $this->addDisplayType('display');
42: $types['hidden'] = $this->addDisplayType('hidden');
43: $types['link'] = $this->addDisplayType('link');
44: $types['widget'] = $this->addDisplayType('widget');
45: $types['thumbnail'] = $this->addDisplayType('thumbnail');
46:
47: return $types;
48: }
49:
50: public function run()
51: {
52:
53: DB::table('_db_tables')->delete();
54: DB::table('_db_fields')->delete();
55:
56: $displayTypes = $this->addDisplayTypes();
57:
58: $widgetTypes = $this->addWidgetTypes();
59:
60:
61: $tables = DB::select('show tables');
62:
63: foreach ($tables as $table)
64: {
65: try
66: {
67:
68: foreach ($table as $tableName)
69: {
70:
71: $id = DB::table('_db_tables')->insertGetId(array('name' => $tableName));
72: Log::write("success", "Added $tableName to _db_table with id $id");
73: try
74: {
75:
76: $cols = DB::select("show columns from $tableName");
77:
78: $displayOrder = 0;
79: foreach ($cols as $col)
80: {
81: try
82: {
83:
84: $colRec = array();
85: $colRec['table_id'] = $id;
86: $colRec['name'] = $col->Field;
87: $colRec['fullname'] = $tableName . "." . $col->Field;
88: $colRec['label'] = $this->makeLabel($col->Field);
89: $colRec['searchable'] = 1;
90: $colRec['display_order'] = $displayOrder++;
91: $colRec['type'] = $this->getFieldType($col->Type);
92: $colRec['length'] = $this->getFieldLength($col->Type);
93: $colRec['width'] = $this->getFieldWidth($colRec['type'], $colRec['length']);
94: $colRec['widget_type_id'] = $this->getFieldWidget($colRec['type'], $colRec['length']);
95: $colRec['null'] = $col->Null;
96: $colRec['key'] = $col->Key;
97: $colRec['default'] = $col->Default;
98: $colRec['extra'] = $col->Extra;
99:
100: $colRec['display_type_id'] = $this->getDisplayType($colRec, $displayTypes);
101:
102: $fid = DB::table('_db_fields')->insertGetId($colRec);
103: Log::write("success", " - {$colRec['name']} inserted with id $fid");
104: }
105: catch (Exception $e)
106: {
107: Log::write("important", $e->getMessage());
108: $message = " x column {$colRec['name']} could not be inserted.";
109: Log::write("important", $message);
110: throw new Exception($message, 1, $e);
111: }
112: }
113: }
114: catch (Exception $e)
115: {
116: Log::write("important", $e->getMessage());
117: $message = "Could not select columns for table $tableName";
118: Log::write("important", $message);
119: throw new Exception($message, 1, $e);
120: }
121: }
122: }
123: catch (Exception $e)
124: {
125: Log::write("important", $e->getMessage());
126: $message = "Error inserting table name '$tableName' into _db_tables";
127: Log::write("important", $message);
128: throw new Exception($message, 1, $e);
129: }
130: }
131: }
132:
133: }
134:
135: ?>