15 import(
'lib.pkp.classes.db.DAO');
16 import(
'classes.core.Services');
46 public function getById($objectId) {
48 'SELECT * FROM ' . $this->tableName .
' WHERE ' . $this->primaryKeyColumn .
' = ?',
53 if ($result->RecordCount() != 0) {
54 $returner = $this->
_fromRow($result->GetRowAssoc(
false));
68 $schema = $schemaService->get($this->schemaName);
69 $sanitizedProps = $schemaService->sanitize($this->schemaName, $object->_data);
71 $primaryDbProps = $this->_getPrimaryDbProps($object);
73 if (empty($primaryDbProps)) {
74 throw new Exception(
'Tried to insert ' . get_class($object) .
' without any properties for the ' . $this->tableName .
' table.');
77 $columnsList = join(
', ', array_keys($primaryDbProps));
78 $bindList = join(
', ', array_fill(0, count($primaryDbProps),
'?'));
79 $this->
update(
"INSERT INTO $this->tableName ($columnsList) VALUES ($bindList)", array_values($primaryDbProps));
84 if (count($sanitizedProps) !== count($primaryDbProps)) {
85 $columns = array($this->primaryKeyColumn,
'locale',
'setting_name',
'setting_value');
86 $columnsList = join(
', ', $columns);
87 $bindList = join(
', ', array_fill(0, count($columns),
'?'));
88 foreach ($schema->properties as $propName => $propSchema) {
89 if (!isset($sanitizedProps[$propName]) || array_key_exists($propName, $this->primaryTableColumns)) {
92 if (!empty($propSchema->multilingual)) {
93 foreach ($sanitizedProps[$propName] as $localeKey => $localeValue) {
94 $this->
update(
"INSERT INTO $this->settingsTableName ($columnsList) VALUES ($bindList)", array(
98 $this->convertToDB($localeValue, $schema->properties->{$propName}->type),
102 $this->
update(
"INSERT INTO $this->settingsTableName ($columnsList) VALUES ($bindList)", array(
106 $this->convertToDB($sanitizedProps[$propName], $schema->properties->{$propName}->type),
112 return $object->getId();
130 public function updateObject($object) {
132 $schema = $schemaService->get($this->schemaName);
133 $sanitizedProps = $schemaService->sanitize($this->schemaName, $object->_data);
135 $primaryDbProps = $this->_getPrimaryDbProps($object);
137 $set = join(
'=?,', array_keys($primaryDbProps)) .
'=?';
139 "UPDATE $this->tableName SET $set WHERE $this->primaryKeyColumn = ?",
140 array_merge(array_values($primaryDbProps), array($object->getId()))
143 $deleteSettings = [];
144 $keyColumns = [$this->primaryKeyColumn,
'locale',
'setting_name'];
145 foreach ($schema->properties as $propName => $propSchema) {
146 if (array_key_exists($propName, $this->primaryTableColumns)) {
148 } elseif (!isset($sanitizedProps[$propName])) {
149 $deleteSettings[] = $propName;
152 if (!empty($propSchema->multilingual)) {
153 foreach ($sanitizedProps[$propName] as $localeKey => $localeValue) {
155 if (is_null($localeValue)) {
156 $this->update(
"DELETE FROM $this->settingsTableName WHERE $this->primaryKeyColumn = ? AND setting_name = ? AND locale = ?", [
163 $this->primaryKeyColumn => $object->getId(),
164 'locale' => $localeKey,
165 'setting_name' => $propName,
166 'setting_value' => $this->convertToDB($localeValue, $schema->properties->{$propName}->type),
168 $this->replace($this->settingsTableName, $updateArray, $keyColumns);
173 $this->primaryKeyColumn => $object->getId(),
175 'setting_name' => $propName,
176 'setting_value' => $this->convertToDB($sanitizedProps[$propName], $schema->properties->{$propName}->type),
178 $this->replace($this->settingsTableName, $updateArray, $keyColumns);
182 if (count($deleteSettings)) {
183 $deleteSettingNames = join(
',', array_map(
function($settingName) {
184 return "'$settingName'";
185 }, $deleteSettings));
186 $this->update(
"DELETE FROM $this->settingsTableName WHERE $this->primaryKeyColumn = ? AND setting_name in ($deleteSettingNames)", [
199 public function deleteObject($object) {
200 $this->deleteById($object->getId());
208 public function deleteById($objectId) {
210 "DELETE FROM $this->tableName WHERE $this->primaryKeyColumn = ?",
214 "DELETE FROM $this->settingsTableName WHERE $this->primaryKeyColumn = ?",
225 public function _fromRow($primaryRow) {
227 $schema = $schemaService->get($this->schemaName);
229 $object = $this->newDataObject();
231 foreach ($this->primaryTableColumns as $propName => $column) {
232 if (isset($primaryRow[$column])) {
235 $this->convertFromDb($primaryRow[$column], $schema->properties->{$propName}->type)
240 $result = $this->retrieve(
241 "SELECT * FROM $this->settingsTableName WHERE $this->primaryKeyColumn = ?",
242 array($primaryRow[$this->primaryKeyColumn])
245 while (!$result->EOF) {
246 $settingRow = $result->getRowAssoc(
false);
247 if (!empty($schema->properties->{$settingRow[
'setting_name']})) {
249 $settingRow[
'setting_name'],
250 $this->convertFromDB(
251 $settingRow[
'setting_value'],
252 $schema->properties->{$settingRow[
'setting_name']}->type
254 empty($settingRow[
'locale']) ?
null : $settingRow[
'locale']
267 public function getInsertId() {
268 return $this->_getInsertId($this->tableName, $this->primaryKeyColumn);
277 private function _getPrimaryDbProps($object) {
279 $sanitizedProps =
Services::get(
'schema')->sanitize($this->schemaName, $object->_data);
281 $primaryDbProps = [];
282 foreach ($this->primaryTableColumns as $propName => $columnName) {
283 if ($propName !==
'id' && array_key_exists($propName, $sanitizedProps)) {
284 $primaryDbProps[$columnName] = $this->convertToDB($sanitizedProps[$propName], $schema->properties->{$propName}->type);
287 if ($primaryDbProps[$columnName] ===
''
288 && isset($schema->properties->{$propName}->validation)
290 in_array(
'date_format:Y-m-d H:i:s', $schema->properties->{$propName}->validation)
291 || in_array(
'date_format:Y-m-d', $schema->properties->{$propName}->validation)
294 $primaryDbProps[$columnName] =
null;
299 return $primaryDbProps;