Open Monograph Press  3.3.0
GenreDAO.inc.php
1 <?php
2 
17 import('lib.pkp.classes.submission.Genre');
18 import('lib.pkp.classes.db.DAO');
19 
20 class GenreDAO extends DAO {
21 
27  function getById($genreId, $contextId = null) {
28  $params = array((int) $genreId);
29  if ($contextId) $params[] = (int) $contextId;
30 
31  $result = $this->retrieve(
32  'SELECT * FROM genres WHERE genre_id = ?' .
33  ($contextId ? ' AND context_id = ?' : '') .
34  ' ORDER BY seq',
35  $params
36  );
37  $returner = null;
38  if ($result->RecordCount() != 0) {
39  $returner = $this->_fromRow($result->GetRowAssoc(false));
40  }
41  $result->Close();
42  return $returner;
43  }
44 
52  function getEnabledByContextId($contextId, $rangeInfo = null) {
53  $params = array(1, (int) $contextId);
54 
55  $result = $this->retrieveRange(
56  'SELECT * FROM genres
57  WHERE enabled = ? AND context_id = ?
58  ORDER BY seq',
59  $params, $rangeInfo
60  );
61 
62  return new DAOResultFactory($result, $this, '_fromRow', array('id'));
63  }
64 
72  function getByDependenceAndContextId($dependentFilesOnly, $contextId, $rangeInfo = null) {
73  $result = $this->retrieveRange(
74  'SELECT * FROM genres
75  WHERE enabled = ? AND context_id = ? AND dependent = ?
76  ORDER BY seq',
77  array(1, (int) $contextId, (int) $dependentFilesOnly),
78  $rangeInfo
79  );
80 
81  return new DAOResultFactory($result, $this, '_fromRow', array('id'));
82  }
83 
91  public function getBySupplementaryAndContextId($supplementaryFilesOnly, $contextId, $rangeInfo = null) {
92  $result = $this->retrieveRange(
93  'SELECT * FROM genres
94  WHERE enabled = ? AND context_id = ? AND supplementary = ?
95  ORDER BY seq',
96  array(1, (int) $contextId, (int) $supplementaryFilesOnly),
97  $rangeInfo
98  );
99 
100  return new DAOResultFactory($result, $this, '_fromRow', array('id'));
101  }
102 
109  public function getPrimaryByContextId($contextId, $rangeInfo = null) {
110  $result = $this->retrieveRange(
111  'SELECT * FROM genres
112  WHERE enabled = ? AND context_id = ? AND dependent = ? AND supplementary = ?
113  ORDER BY seq',
114  array(1, (int) $contextId, 0, 0),
115  $rangeInfo
116  );
117 
118  return new DAOResultFactory($result, $this, '_fromRow', array('id'));
119  }
120 
127  function getByContextId($contextId, $rangeInfo = null) {
128  $result = $this->retrieveRange(
129  'SELECT * FROM genres WHERE context_id = ? ORDER BY seq',
130  array((int) $contextId),
131  $rangeInfo
132  );
133 
134  return new DAOResultFactory($result, $this, '_fromRow', array('id'));
135  }
136 
143  function getByKey($key, $contextId = null) {
144  $params = array($key);
145  if ($contextId) $params[] = (int) $contextId;
146 
147  $sql = 'SELECT * FROM genres WHERE entry_key = ? ' .
148  ($contextId ? ' AND context_id = ?' : '');
149 
150  $result = $this->retrieve($sql, $params);
151 
152  $returner = null;
153  if ($result->RecordCount() != 0) {
154  $returner = $this->_fromRow($result->GetRowAssoc(false));
155  }
156  $result->Close();
157  return $returner;
158  }
159 
164  function getLocaleFieldNames() {
165  return array('name');
166  }
167 
172  function updateLocaleFields($genre) {
174  'genre_settings', $genre,
175  array('genre_id' => $genre->getId())
176  );
177  }
178 
183  function newDataObject() {
184  return new Genre();
185  }
186 
192  function _fromRow($row) {
193  $genre = $this->newDataObject();
194  $genre->setId($row['genre_id']);
195  $genre->setKey($row['entry_key']);
196  $genre->setContextId($row['context_id']);
197  $genre->setCategory($row['category']);
198  $genre->setDependent($row['dependent']);
199  $genre->setSupplementary($row['supplementary']);
200  $genre->setSequence($row['seq']);
201  $genre->setEnabled($row['enabled']);
202 
203  $this->getDataObjectSettings('genre_settings', 'genre_id', $row['genre_id'], $genre);
204 
205  HookRegistry::call('GenreDAO::_fromRow', array(&$genre, &$row));
206 
207  return $genre;
208  }
209 
215  function insertObject($genre) {
216  $this->update(
217  'INSERT INTO genres
218  (entry_key, seq, context_id, category, dependent, supplementary)
219  VALUES
220  (?, ?, ?, ?, ?, ?)',
221  array(
222  $genre->getKey(),
223  (float) $genre->getSequence(),
224  (int) $genre->getContextId(),
225  (int) $genre->getCategory(),
226  $genre->getDependent() ? 1 : 0,
227  $genre->getSupplementary() ? 1 : 0,
228  )
229  );
230 
231  $genre->setId($this->getInsertId());
232  $this->updateLocaleFields($genre);
233  return $genre->getId();
234  }
235 
240  function updateObject($genre) {
241  $this->update(
242  'UPDATE genres
243  SET entry_key = ?,
244  seq = ?,
245  dependent = ?,
246  supplementary = ?,
247  enabled = ?,
248  category = ?
249  WHERE genre_id = ?',
250  array(
251  $genre->getKey(),
252  (float) $genre->getSequence(),
253  $genre->getDependent() ? 1 : 0,
254  $genre->getSupplementary() ? 1 : 0,
255  $genre->getEnabled() ? 1 : 0,
256  $genre->getCategory(),
257  (int) $genre->getId(),
258  )
259  );
260  $this->updateLocaleFields($genre);
261  }
262 
267  function deleteObject($genre) {
268  return $this->deleteById($genre->getId());
269  }
270 
275  function deleteById($genreId) {
276  return $this->update(
277  'UPDATE genres SET enabled = ? WHERE genre_id = ?',
278  array(0, (int) $genreId)
279  );
280  }
281 
287  function deleteByContextId($contextId) {
288  $genres = $this->getByContextId($contextId);
289  while ($genre = $genres->next()) {
290  $this->update('DELETE FROM genre_settings WHERE genre_id = ?', (int) $genre->getId());
291  }
292  $this->update(
293  'DELETE FROM genres WHERE context_id = ?', (int) $contextId
294  );
295  }
296 
301  function getInsertId() {
302  return $this->_getInsertId('genres', 'genre_id');
303  }
304 
310  function installDefaults($contextId, $locales) {
311  // Load all the necessary locales.
312  foreach ($locales as $locale) AppLocale::requireComponents(LOCALE_COMPONENT_APP_DEFAULT, LOCALE_COMPONENT_PKP_DEFAULT, $locale);
313 
314  $xmlDao = new XMLDAO();
315  $data = $xmlDao->parseStruct('registry/genres.xml', array('genre'));
316  if (!isset($data['genre'])) return false;
317  $seq = 0;
318 
319  foreach ($data['genre'] as $entry) {
320  $attrs = $entry['attributes'];
321  // attempt to retrieve an installed Genre with this key.
322  // Do this to preserve the genreId.
323  $genre = $this->getByKey($attrs['key'], $contextId);
324  if (!$genre) $genre = $this->newDataObject();
325  $genre->setContextId($contextId);
326  $genre->setKey($attrs['key']);
327  $genre->setCategory($attrs['category']);
328  $genre->setDependent($attrs['dependent']);
329  $genre->setSupplementary($attrs['supplementary']);
330  $genre->setSequence($seq++);
331  foreach ($locales as $locale) {
332  $genre->setName(__($attrs['localeKey'], array(), $locale), $locale);
333  }
334 
335  if ($genre->getId() > 0) { // existing genre.
336  $genre->setEnabled(1);
337  $this->updateObject($genre);
338  } else {
339  $this->insertObject($genre);
340  }
341  }
342  }
343 
348  function getDefaultKeys() {
349  $defaultKeys = array();
350  $xmlDao = new XMLDAO();
351  $data = $xmlDao->parseStruct('registry/genres.xml', array('genre'));
352  if (isset($data['genre'])) foreach ($data['genre'] as $entry) {
353  $attrs = $entry['attributes'];
354  $defaultKeys[] = $attrs['key'];
355  }
356  return $defaultKeys;
357  }
358 
366  function keyExists($key, $contextId, $genreId = null) {
367  $params = array($key, (int) $contextId);
368  if ($genreId) $params[] = (int) $genreId;
369  $result = $this->retrieveRange(
370  'SELECT COUNT(*) FROM genres WHERE entry_key = ? AND context_id = ?' . (isset($genreId) ? ' AND genre_id <> ?' : ''),
371  $params
372  );
373  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
374  $result->Close();
375  return $returner;
376  }
377 
382  function deleteSettingsByLocale($locale) {
383  $this->update('DELETE FROM genre_settings WHERE locale = ?', $locale);
384  }
385 }
386 
387 
GenreDAO\getLocaleFieldNames
getLocaleFieldNames()
Definition: GenreDAO.inc.php:164
XMLDAO
Operations for retrieving and modifying objects from an XML data source.
Definition: XMLDAO.inc.php:19
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
DAOResultFactory
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
Definition: DAOResultFactory.inc.php:21
GenreDAO\getByContextId
getByContextId($contextId, $rangeInfo=null)
Definition: GenreDAO.inc.php:127
GenreDAO\getEnabledByContextId
getEnabledByContextId($contextId, $rangeInfo=null)
Definition: GenreDAO.inc.php:52
GenreDAO\updateLocaleFields
updateLocaleFields($genre)
Definition: GenreDAO.inc.php:172
DAO\retrieveRange
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
GenreDAO\getBySupplementaryAndContextId
getBySupplementaryAndContextId($supplementaryFilesOnly, $contextId, $rangeInfo=null)
Definition: GenreDAO.inc.php:91
GenreDAO\updateObject
updateObject($genre)
Definition: GenreDAO.inc.php:240
Genre
Basic class describing a genre.
Definition: Genre.inc.php:21
GenreDAO\_fromRow
_fromRow($row)
Definition: GenreDAO.inc.php:192
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
GenreDAO\deleteByContextId
deleteByContextId($contextId)
Definition: GenreDAO.inc.php:287
GenreDAO\getById
getById($genreId, $contextId=null)
Definition: GenreDAO.inc.php:27
GenreDAO\getDefaultKeys
getDefaultKeys()
Definition: GenreDAO.inc.php:348
GenreDAO\newDataObject
newDataObject()
Definition: GenreDAO.inc.php:183
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
GenreDAO\installDefaults
installDefaults($contextId, $locales)
Definition: GenreDAO.inc.php:310
DAO\_getInsertId
_getInsertId($table='', $id='')
Definition: DAO.inc.php:255
GenreDAO\deleteObject
deleteObject($genre)
Definition: GenreDAO.inc.php:267
DAO\getDataObjectSettings
getDataObjectSettings($tableName, $idFieldName, $idFieldValue, $dataObject)
Definition: DAO.inc.php:582
GenreDAO\getInsertId
getInsertId()
Definition: GenreDAO.inc.php:301
GenreDAO\insertObject
insertObject($genre)
Definition: GenreDAO.inc.php:215
GenreDAO\deleteSettingsByLocale
deleteSettingsByLocale($locale)
Definition: GenreDAO.inc.php:382
DAO\updateDataObjectSettings
updateDataObjectSettings($tableName, $dataObject, $idArray)
Definition: DAO.inc.php:488
GenreDAO\deleteById
deleteById($genreId)
Definition: GenreDAO.inc.php:275
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
GenreDAO\getByKey
getByKey($key, $contextId=null)
Definition: GenreDAO.inc.php:143
GenreDAO\keyExists
keyExists($key, $contextId, $genreId=null)
Definition: GenreDAO.inc.php:366
GenreDAO\getByDependenceAndContextId
getByDependenceAndContextId($dependentFilesOnly, $contextId, $rangeInfo=null)
Definition: GenreDAO.inc.php:72
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31
GenreDAO\getPrimaryByContextId
getPrimaryByContextId($contextId, $rangeInfo=null)
Definition: GenreDAO.inc.php:109
GenreDAO
Operations for retrieving and modifying Genre objects.
Definition: GenreDAO.inc.php:20