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