1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address [email protected].
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: Yii::import('system.test.CDbFixtureManager');
38:
39: /**
40: * @package application.components
41: */
42: class X2FixtureManager extends CDbFixtureManager {
43:
44: /**
45: * @var bool $loadFixture
46: */
47: public $loadFixtures;
48: public $loadFixturesForClassOnly;
49:
50: private $_referenceFixtures;
51: public function setReferenceFixtures ($referenceFixtures) {
52: $this->_referenceFixtures = $referenceFixtures;
53: }
54:
55: public function __construct () {
56: $this->loadFixtures = X2_LOAD_FIXTURES && !X2_LOAD_FIXTURES_FOR_CLASS_ONLY;
57: $this->loadFixturesForClassOnly = X2_LOAD_FIXTURES_FOR_CLASS_ONLY;
58: }
59:
60: public function prepare () {
61: if ($this->loadFixtures) parent::prepare ();
62: }
63:
64: /**
65: * Override of {@link CDbFixtureManager}'s resetTable
66: *
67: * Permits array-style definition of init scripts much like fixture files
68: *
69: * This method is Copyright (c) 2008-2014 by Yii Software LLC
70: * http://www.yiiframework.com/license/
71: */
72: public function resetTable($tableName) {
73: /* x2modstart */
74: if (!$this->loadFixtures && !$this->loadFixturesForClassOnly) return;
75: /* x2modend */
76: $initFile = $this->basePath . DIRECTORY_SEPARATOR . $tableName . $this->initScriptSuffix;
77: if (is_file($initFile)) {
78: $tbl_data = require($initFile);
79: if (is_array($tbl_data)) {
80: Yii::app()->db->createCommand()->truncateTable($tableName);
81: foreach ($tbl_data as $rec)
82: Yii::app()->db->createCommand()->insert($tableName, $rec);
83: }
84: } else {
85: $this->truncateTable($tableName);
86: }
87: }
88:
89: /**
90: * Override of {@link CDbFixtureManager}'s loadFixture
91: *
92: * Modified to enable fixture file suffixing. A fixture file suffix can be specified by
93: * setting a value in the fixtures array to an array with two properties:
94: * array (<tableName|modelClass>, <file suffix>)
95: *
96: * This method is Copyright (c) 2008-2014 by Yii Software LLC
97: * http://www.yiiframework.com/license/
98: */
99: public function loadFixture($tableName,/* x2modstart */ $suffix=''/* x2modend */)
100: {
101: $fileName=$this->basePath.DIRECTORY_SEPARATOR.$tableName/* x2modstart */.$suffix.
102: /* x2modend */'.php';
103: if(!is_file($fileName))
104: return false;
105:
106: $rows=array();
107: $schema=$this->getDbConnection()->getSchema();
108: $builder=$schema->getCommandBuilder();
109: $table=$schema->getTable($tableName);
110:
111: foreach(require($fileName) as $alias=>$row)
112: {
113: /* x2modstart */
114: if ($this->loadFixtures || $this->loadFixturesForClassOnly) {
115: /* x2modend */
116: $builder->createInsertCommand($table,$row)->execute();
117: $primaryKey=$table->primaryKey;
118: if($table->sequenceName!==null)
119: {
120: if(is_string($primaryKey) && !isset($row[$primaryKey]))
121: $row[$primaryKey]=$builder->getLastInsertID($table);
122: elseif(is_array($primaryKey))
123: {
124: foreach($primaryKey as $pk)
125: {
126: if(!isset($row[$pk]))
127: {
128: $row[$pk]=$builder->getLastInsertID($table);
129: break;
130: }
131: }
132: }
133: }
134: /* x2modstart */
135: }
136: /* x2modend */
137: $rows[$alias]=$row;
138: }
139: return $rows;
140: }
141:
142: /**
143: * Override of {@link CDbFixtureManager}'s load
144: *
145: * Modified to enable fixture file suffixing. A fixture file suffix can be specified by
146: * setting a value in the fixtures array to an array with two properties:
147: * array (<tableName|modelClass>, <file suffix>)
148: *
149: * This method is Copyright (c) 2008-2014 by Yii Software LLC
150: * http://www.yiiframework.com/license/
151: */
152: public function load($fixtures)
153: {
154: $schema=$this->getDbConnection()->getSchema();
155: $schema->checkIntegrity(false);
156:
157: $this->_rows=array();
158: $this->_records=array();
159: foreach($fixtures as $fixtureName=>$tableName)
160: {
161: /* x2modstart */
162: $suffix = null;
163: if (is_array ($tableName))
164: {
165: $suffix = $tableName[1];
166: $tableName = $tableName[0];
167: }
168: /* x2modend */
169:
170: if($tableName[0]===':')
171: {
172: $tableName=substr($tableName,1);
173: unset($modelClass);
174: }
175: else
176: {
177: $modelClass=Yii::import($tableName,true);
178: $tableName=CActiveRecord::model($modelClass)->tableName();
179: }
180: if(($prefix=$this->getDbConnection()->tablePrefix)!==null)
181: $tableName=preg_replace('/{{(.*?)}}/',$prefix.'\1',$tableName);
182: $this->resetTable($tableName);
183: $rows=$this->loadFixture($tableName/* x2modstart */,$suffix/* x2modend */);
184: if(is_array($rows) && is_string($fixtureName))
185: {
186: $this->_rows[$fixtureName]=$rows;
187: if(isset($modelClass))
188: {
189: foreach(array_keys($rows) as $alias)
190: $this->_records[$fixtureName][$alias]=$modelClass;
191: }
192: }
193: }
194:
195: $schema->checkIntegrity(true);
196: }
197:
198: /**
199: * Returns the specified ActiveRecord instance in the fixture data.
200: * @param string $name the fixture name
201: * @param string $alias the alias for the fixture data row
202: * @return CActiveRecord the ActiveRecord instance. False is returned if there is no such fixture row.
203: * This method is Copyright (c) 2008-2014 by Yii Software LLC
204: * http://www.yiiframework.com/license/
205: */
206: public function getRecord($name,$alias)
207: {
208: if(isset($this->_records[$name][$alias]))
209: {
210: if(is_string($this->_records[$name][$alias]))
211: {
212: $row=$this->_rows[$name][$alias];
213:
214: /* x2modstart */
215: if ($this->loadFixtures) {
216: /* x2modend */
217: $model=CActiveRecord::model($this->_records[$name][$alias]);
218: $key=$model->getTableSchema()->primaryKey;
219: if(is_string($key))
220: $pk=$row[$key];
221: else
222: {
223: foreach($key as $k)
224: $pk[$k]=$row[$k];
225: }
226: $this->_records[$name][$alias]=$model->findByPk($pk);
227: /* x2modstart */
228: } else {
229: $model = CActiveRecord::model ($this->_records[$name][$alias]);
230: if (isset ($row['id'])) {
231: $this->_records[$name][$alias]=$model->findByPk($row['id']);
232: } else {
233: $this->_records[$name][$alias]=$model->findByAttributes($row);
234: }
235: }
236: /* x2modend */
237: }
238: return $this->_records[$name][$alias];
239: }
240: else
241: return false;
242: }
243:
244: }
245:
246: ?>
247: