1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace GLFramework\DaMa;
10:
11:
12: use GLFramework\DaMa\Manipulators\ManipulatorCore;
13: use GLFramework\Model;
14:
15: class Manipulator
16: {
17:
18: private $currentSheet;
19: 20: 21:
22: private $association = array();
23: private $modelName;
24: private $filename;
25: 26: 27:
28: private $core;
29:
30: 31: 32:
33: public function getFilename()
34: {
35: return $this->filename;
36: }
37:
38: 39: 40: 41:
42: public function setFilename($filename)
43: {
44: $this->filename = $filename;
45: return $this;
46: }
47:
48: 49: 50:
51: public function getCore()
52: {
53: return $this->core;
54: }
55:
56: 57: 58: 59:
60: public function setCore($core)
61: {
62: $this->core = $core;
63: return $this;
64: }
65: public function model($modelName)
66: {
67: $this->modelName = $modelName;
68: return $this;
69: }
70:
71: public function field($nameInFile, $nameInModel, $fn = null)
72: {
73: if($association = $this->getAssociation($nameInModel))
74: {
75: $association->addNameInFile($nameInFile);
76: }
77: else
78: {
79: $association = new Association();
80: $association->addNameInFile($nameInFile);
81: $association->setNameInModel($nameInModel);
82: $association->setParser($fn);
83: $this->association[] = $association;
84: }
85: return $association;
86: }
87:
88: private function getAssociation($nameInModel)
89: {
90: foreach($this->association as $association)
91: {
92: if($association->getNameInModel() == $nameInModel)
93: {
94: return $association;
95: }
96: }
97: return null;
98: }
99:
100: public function constant($nameInModel, $value)
101: {
102: if($association = $this->getAssociation($nameInModel))
103: {
104: $association->setConstant($value);
105: }
106: else
107: {
108: $association = new Association();
109: $association->setNameInModel($nameInModel);
110: $association->setConstant($value);
111: $this->association[] = $association;
112: }
113: }
114:
115: public function sheet($index)
116: {
117: $this->currentSheet = $index;
118: }
119:
120: private function init($config = array())
121: {
122: $this->getCore()->open($this->getFilename(), $config);
123: if($this->currentSheet !== null)
124: $this->getCore()->setSheet($this->currentSheet);
125: }
126:
127: public function exec($config = array())
128: {
129: $count = 0;
130: $this->init($config);
131: if($header = $this->getCore()->next())
132: {
133: while($next = $this->getCore()->next())
134: {
135: if(implode("", $next) != "")
136: {
137: $model = $this->build($header, $next);
138: if($model->valid() && $model->save())
139: {
140: $count++;
141: }
142: }
143: }
144: return $count;
145: }
146:
147: return false;
148: }
149:
150: 151: 152: 153: 154:
155: public function build($header, $row)
156: {
157: $associative = array();
158: foreach($header as $key => $value)
159: {
160: $associative[$value] = $row[$key];
161: }
162: $model = new $this->modelName();
163: foreach($this->association as $association)
164: {
165: if($association->index)
166: {
167: if($association->fill($model, $associative))
168: {
169: $value = $model->{$association->nameInModel};
170: if($value && !empty($value))
171: {
172: $model = $model->get(array($association->nameInModel => $value))->getModel();
173: break;
174: }
175: }
176: }
177: }
178: foreach($this->association as $association)
179: {
180: $association->fill($model, $associative);
181: }
182: return $model;
183: }
184:
185: public function debug($number, $config = array())
186: {
187: $tmp = array();
188: $list = array();
189: $this->init($config);
190: if($header = $this->getCore()->next())
191: {
192: while($data = $this->getCore()->next())
193: {
194: if($data == null) break;
195: $tmp = $data;
196: $model = $this->build($header, $data);
197: $number--;
198: if($number >= 0)
199: $list[] = $model;
200: }
201: }
202: print_debug($header, $tmp, $list);
203: }
204: }