File:
0.00.1a/core/entity/EntityFactory.php (
View as Code)
1: 2: /* -------------------------------------------------------------
3: This file is part of FreeDESK
4:
5: FreeDESK is (C) Copyright 2012 David Cutting
6:
7: FreeDESK is free software: you can redistribute it and/or modify
8: it under the terms of the GNU General Public License as published by
9: the Free Software Foundation, either version 3 of the License, or
10: (at your option) any later version.
11:
12: FreeDESK is distributed in the hope that it will be useful,
13: but WITHOUT ANY WARRANTY; without even the implied warranty of
14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: GNU General Public License for more details.
16:
17: You should have received a copy of the GNU General Public License
18: along with FreeDESK. If not, see www.gnu.org/licenses
19:
20: For more information see www.purplepixie.org/freedesk/
21: -------------------------------------------------------------- */
22:
23: /**
24: * Entity Factory - create an entity of the correct class
25: **/
26: class EntityFactory
27: {
28: /**
29: * Register ourselves with FreeDESK
30: * @param mixed $desk FreeDESK instance
31: **/
32: static function Exec(&$desk)
33: {
34: $desk->PluginManager->Register(new Plugin(
35: "Entity Factory", "0.01", "Core", "" ));
36: }
37:
38: /**
39: * Create an instance of the required entity class
40: * @param mixed &$desk FreeDESK instance
41: * @param string $entity Entity
42: * @return mixed Entity object
43: **/
44: static function Create(&$desk, $entity)
45: {
46: $methodlist = $desk->PluginManager->GetType("Entity");
47: $table = $desk->DataDictionary->GetTable($entity);
48:
49: if ($table)
50: $class = $table->class;
51: else
52: $class = "";
53:
54: $default = "Entity"; // fail-safe
55: foreach($methodlist as $method)
56: {
57: if ($method->subtype == $class)
58: {
59: $ent = new $method->classname($desk);
60: $ent->SetEntity($entity);
61: return $ent;
62: }
63: if ($method->subtype == "")
64: $default=$method->classname;
65: }
66: $ent = new $default($desk);
67: $ent->SetEntity($entity);
68: return $ent;
69: }
70:
71: }
72: ?>
73: