1: <?php
2: /**
3: * This file contains the abstract base item class of the generic db.
4: *
5: * @package Core
6: * @subpackage GenericDB
7: * @version SVN Revision $Rev:$
8: *
9: * @author Murat Purc <murat@purc.de>
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: // Try to load GenericDB database driver
19: // TODO: check if this is needed any longer because we have autoloading feature
20: global $cfg;
21: $driver_filename = cRegistry::getBackendPath() . $cfg['path']['classes'] . 'drivers/' . $cfg['sql']['gdb_driver'] . '/class.gdb.' . $cfg['sql']['gdb_driver'] . '.php';
22: if (cFileHandler::exists($driver_filename)) {
23: include_once($driver_filename);
24: }
25:
26: /**
27: * Class cItemBaseAbstract.
28: * Base class with common features for database based items and item
29: * collections.
30: *
31: * NOTE:
32: * Because of required downwards compatibilitiy all protected/private member
33: * variables or methods don't have an leading underscore.
34: *
35: * @package Core
36: * @subpackage GenericDB
37: */
38: abstract class cItemBaseAbstract extends cGenericDb {
39:
40: /**
41: * Database instance, contains the database object
42: *
43: * @var cDb
44: */
45: protected $db;
46:
47: /**
48: * Second DB instance, is required for some additional queries without
49: * losing an current existing query result.
50: *
51: * @var cDb
52: */
53: protected $secondDb;
54:
55: /**
56: * Property collection instance
57: *
58: * @var cApiPropertyCollection
59: */
60: protected $properties;
61:
62: /**
63: * Item cache instance
64: *
65: * @var cItemCache
66: */
67: protected $_oCache;
68:
69: /**
70: * GenericDB settings, see $cfg['sql']
71: *
72: * @var array
73: */
74: protected $_settings;
75:
76: /**
77: * Storage of the source table to use for the information
78: *
79: * @var string
80: */
81: protected $table;
82:
83: /**
84: * Storage of the primary key
85: *
86: * @var string
87: * @todo remove access from public
88: */
89: public $primaryKey;
90:
91: /**
92: * Checks for the virginity of created objects.
93: * If true, the object
94: * is virgin and no operations on it except load-Functions are allowed.
95: *
96: * @todo remove access from public
97: * @var bool
98: */
99: public $virgin = true;
100:
101: /**
102: * Storage of the last occured error
103: *
104: * @var string
105: */
106: protected $lasterror = '';
107:
108: /**
109: * Classname of current instance
110: *
111: * @var string
112: */
113: protected $_className;
114:
115: /**
116: * Sets some common properties
117: *
118: * @param string $sTable Name of table
119: * @param string $sPrimaryKey Primary key of table
120: * @param string $sClassName Name of parent class
121: * @throws cInvalidArgumentException If table name or primary key is not set
122: */
123: protected function __construct($sTable, $sPrimaryKey, $sClassName) {
124: global $cfg;
125:
126: $this->db = cRegistry::getDb();
127:
128: if ($sTable == '') {
129: $sMsg = "$sClassName: No table specified. Inherited classes *need* to set a table";
130: throw new cInvalidArgumentException($sMsg);
131: } elseif ($sPrimaryKey == '') {
132: $sMsg = "No primary key specified. Inherited classes *need* to set a primary key";
133: throw new cInvalidArgumentException($sMsg);
134: }
135:
136: $this->_settings = $cfg['sql'];
137:
138: // instantiate caching
139: $aCacheOpt = (isset($this->_settings['cache'])) ? $this->_settings['cache'] : array();
140: $this->_oCache = cItemCache::getInstance($sTable, $aCacheOpt);
141:
142: $this->table = $sTable;
143: $this->primaryKey = $sPrimaryKey;
144: $this->_className = $sClassName;
145: }
146:
147: /**
148: * Resets class variables back to default
149: * This is handy in case a new item is tried to be loaded into this class instance.
150: */
151: protected function _resetItem() {
152: $this->virgin = true;
153: $this->properties = null;
154: $this->lasterror = '';
155: }
156:
157: /**
158: * Escape string for using in SQL-Statement.
159: *
160: * @param string $sString The string to escape
161: * @return string Escaped string
162: */
163: public function escape($sString) {
164: return $this->db->escape($sString);
165: }
166:
167: /**
168: * Returns the second database instance, usable to run additional statements
169: * without losing current query results.
170: *
171: * @return cDb
172: */
173: protected function _getSecondDBInstance() {
174: if (!isset($this->secondDb) || !($this->secondDb instanceof cDb)) {
175: $this->secondDb = cRegistry::getDb();
176: }
177: return $this->secondDb;
178: }
179:
180: /**
181: * Returns properties instance, instantiates it if not done before.
182: * NOTE: This funtion changes always the client variable of property
183: * collection instance.
184: *
185: * @param int $idclient Id of client to use in property collection. If not
186: * passed
187: * it uses global variable
188: * @return cApiPropertyCollection
189: */
190: protected function _getPropertiesCollectionInstance($idclient = 0) {
191: global $client;
192:
193: if ((int) $idclient <= 0) {
194: $idclient = $client;
195: }
196:
197: // Runtime on-demand allocation of the properties object
198: if (!isset($this->properties) || !($this->properties instanceof cApiPropertyCollection)) {
199: $this->properties = new cApiPropertyCollection();
200: }
201:
202: if ((int) $idclient > 0) {
203: $this->properties->changeClient($idclient);
204: }
205:
206: return $this->properties;
207: }
208: }
209:
210: ?>