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;
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->virgin = true;
145: $this->_className = $sClassName;
146: }
147:
148: /**
149: * Escape string for using in SQL-Statement.
150: *
151: * @param string $sString The string to escape
152: * @return string Escaped string
153: */
154: public function escape($sString) {
155: return $this->db->escape($sString);
156: }
157:
158: /**
159: * Returns the second database instance, usable to run additional statements
160: * without losing current query results.
161: *
162: * @return cDb
163: */
164: protected function _getSecondDBInstance() {
165: if (!isset($this->secondDb) || !($this->secondDb instanceof cDb)) {
166: $this->secondDb = cRegistry::getDb();
167: }
168: return $this->secondDb;
169: }
170:
171: /**
172: * Returns properties instance, instantiates it if not done before.
173: * NOTE: This funtion changes always the client variable of property
174: * collection instance.
175: *
176: * @param int $idclient Id of client to use in property collection. If not
177: * passed
178: * it uses global variable
179: * @return cApiPropertyCollection
180: */
181: protected function _getPropertiesCollectionInstance($idclient = 0) {
182: global $client;
183:
184: if ((int) $idclient <= 0) {
185: $idclient = $client;
186: }
187:
188: // Runtime on-demand allocation of the properties object
189: if (!isset($this->properties) || !($this->properties instanceof cApiPropertyCollection)) {
190: $this->properties = new cApiPropertyCollection();
191: }
192:
193: if ((int) $idclient > 0) {
194: $this->properties->changeClient($idclient);
195: }
196:
197: return $this->properties;
198: }
199: }
200:
201: ?>