Open Journal Systems  3.0.0
 All Data Structures Namespaces Functions Variables Groups Pages
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 {
24  function GenreDAO() {
25  parent::DAO();
26  }
27 
33  function getById($genreId, $contextId = null) {
34  $params = array((int) $genreId);
35  if ($contextId) $params[] = (int) $contextId;
36 
37  $result = $this->retrieve(
38  'SELECT * FROM genres WHERE genre_id = ?' .
39  ($contextId ? ' AND context_id = ?' : '') .
40  ' ORDER BY seq',
41  $params
42  );
43  $returner = null;
44  if ($result->RecordCount() != 0) {
45  $returner = $this->_fromRow($result->GetRowAssoc(false));
46  }
47  $result->Close();
48  return $returner;
49  }
50 
58  function getEnabledByContextId($contextId, $rangeInfo = null) {
59  $params = array(1, (int) $contextId);
60 
61  $result = $this->retrieveRange(
62  'SELECT * FROM genres
63  WHERE enabled = ? AND context_id = ?
64  ORDER BY seq',
65  $params, $rangeInfo
66  );
67 
68  return new DAOResultFactory($result, $this, '_fromRow', array('id'));
69  }
70 
78  function getByDependenceAndContextId($dependentFilesOnly, $contextId, $rangeInfo = null) {
79  $result = $this->retrieveRange(
80  'SELECT * FROM genres
81  WHERE enabled = ? AND context_id = ? AND dependent = ?
82  ORDER BY seq',
83  array(1, (int) $contextId, (int) $dependentFilesOnly),
84  $rangeInfo
85  );
86 
87  return new DAOResultFactory($result, $this, '_fromRow', array('id'));
88  }
89 
96  function getByContextId($contextId, $rangeInfo = null) {
97  $result = $this->retrieveRange(
98  'SELECT * FROM genres WHERE context_id = ? ORDER BY seq',
99  array((int) $contextId),
100  $rangeInfo
101  );
102 
103  return new DAOResultFactory($result, $this, '_fromRow', array('id'));
104  }
105 
112  function getByKey($key, $contextId = null) {
113  $params = array($key);
114  if ($contextId) $params[] = (int) $contextId;
115 
116  $sql = 'SELECT * FROM genres WHERE entry_key = ? ' .
117  ($contextId ? ' AND context_id = ?' : '');
118 
119  $result = $this->retrieve($sql, $params);
120 
121  $returner = null;
122  if ($result->RecordCount() != 0) {
123  $returner = $this->_fromRow($result->GetRowAssoc(false));
124  }
125  $result->Close();
126  return $returner;
127  }
128 
133  function getLocaleFieldNames() {
134  return array('name');
135  }
136 
142  return array('designation');
143  }
144 
149  function updateLocaleFields($genre) {
151  'genre_settings', $genre,
152  array('genre_id' => $genre->getId())
153  );
154  }
155 
160  function newDataObject() {
161  return new Genre();
162  }
163 
169  function _fromRow($row) {
170  $genre = $this->newDataObject();
171  $genre->setId($row['genre_id']);
172  $genre->getKey($row['entry_key']);
173  $genre->setContextId($row['context_id']);
174  $genre->setSortable($row['sortable']);
175  $genre->setCategory($row['category']);
176  $genre->setDependent($row['dependent']);
177  $genre->setSupplementary($row['supplementary']);
178  $genre->setSequence($row['seq']);
179  $genre->setEnabled($row['enabled']);
180 
181  $this->getDataObjectSettings('genre_settings', 'genre_id', $row['genre_id'], $genre);
182 
183  HookRegistry::call('GenreDAO::_fromRow', array(&$genre, &$row));
184 
185  return $genre;
186  }
187 
193  function insertObject($genre) {
194  $this->update(
195  'INSERT INTO genres
196  (entry_key, seq, sortable, context_id, category, dependent, supplementary)
197  VALUES
198  (?, ?, ?, ?, ?, ?, ?)',
199  array(
200  $genre->getKey(),
201  (float) $genre->getSequence(),
202  $genre->getSortable() ? 1 : 0,
203  (int) $genre->getContextId(),
204  (int) $genre->getCategory(),
205  $genre->getDependent() ? 1 : 0,
206  $genre->getSupplementary() ? 1 : 0,
207  )
208  );
209 
210  $genre->setId($this->getInsertId());
211  $this->updateLocaleFields($genre);
212  return $genre->getId();
213  }
214 
219  function updateObject($genre) {
220  $this->update(
221  'UPDATE genres
222  SET entry_key = ?,
223  seq = ?,
224  sortable = ?,
225  dependent = ?,
226  supplementary = ?,
227  enabled = ?,
228  category = ?
229  WHERE genre_id = ?',
230  array(
231  $genre->getKey(),
232  (float) $genre->getSequence(),
233  $genre->getSortable() ? 1 : 0,
234  $genre->getDependent() ? 1 : 0,
235  $genre->getSupplementary() ? 1 : 0,
236  $genre->getEnabled() ? 1 : 0,
237  $genre->getCategory(),
238  (int) $genre->getId(),
239  )
240  );
241  $this->updateLocaleFields($genre);
242  }
243 
248  function deleteObject($genre) {
249  return $this->deleteById($genre->getId());
250  }
251 
256  function deleteById($genreId) {
257  return $this->update(
258  'UPDATE genres SET enabled = ? WHERE genre_id = ?',
259  array(0, (int) $genreId)
260  );
261  }
262 
268  function deleteByContextId($contextId) {
269  $genres = $this->getByContextId($contextId);
270  while ($genre = $genres->next()) {
271  $this->update('DELETE FROM genre_settings WHERE genre_id = ?', (int) $genre->getId());
272  }
273  $this->update(
274  'DELETE FROM genres WHERE context_id = ?', (int) $contextId
275  );
276  }
277 
282  function getInsertId() {
283  return $this->_getInsertId('genres', 'genre_id');
284  }
285 
291  function installDefaults($contextId, $locales) {
292  // Load all the necessary locales.
293  foreach ($locales as $locale) AppLocale::requireComponents(LOCALE_COMPONENT_APP_DEFAULT, LOCALE_COMPONENT_PKP_DEFAULT, $locale);
294 
295  $xmlDao = new XMLDAO();
296  $data = $xmlDao->parseStruct('registry/genres.xml', array('genre'));
297  if (!isset($data['genre'])) return false;
298  $seq = 0;
299 
300  foreach ($data['genre'] as $entry) {
301  $attrs = $entry['attributes'];
302  // attempt to retrieve an installed Genre with this key.
303  // Do this to preserve the genreId.
304  $genre = $this->getByKey($attrs['key'], $contextId);
305  if (!$genre) $genre = $this->newDataObject();
306  $genre->setContextId($contextId);
307  $genre->setKey($attrs['key']);
308  $genre->setSortable($attrs['sortable']);
309  $genre->setCategory($attrs['category']);
310  $genre->setDependent($attrs['dependent']);
311  $genre->setSupplementary($attrs['supplementary']);
312  $genre->setSequence($seq++);
313  foreach ($locales as $locale) {
314  $genre->setName(__($attrs['localeKey'], array(), $locale), $locale);
315  }
316  $genre->setDesignation($attrs['designation']);
317 
318  if ($genre->getId() > 0) { // existing genre.
319  $this->updateObject($genre);
320  } else {
321  $this->insertObject($genre);
322  }
323  }
324  }
325 
330  function deleteSettingsByLocale($locale) {
331  $this->update('DELETE FROM genre_settings WHERE locale = ?', $locale);
332  }
333 }
334 
335 ?>
updateObject($genre)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
deleteByContextId($contextId)
deleteSettingsByLocale($locale)
static requireComponents()
Operations for retrieving and modifying Genre objects.
getLocaleFieldNames()
getByKey($key, $contextId=null)
Operations for retrieving and modifying objects from an XML data source.
Definition: XMLDAO.inc.php:19
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:174
getByDependenceAndContextId($dependentFilesOnly, $contextId, $rangeInfo=null)
insertObject($genre)
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
_fromRow($row)
static call($hookName, $args=null)
updateDataObjectSettings($tableName, $dataObject, $idArray)
Definition: DAO.inc.php:474
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
getById($genreId, $contextId=null)
getByContextId($contextId, $rangeInfo=null)
deleteObject($genre)
getEnabledByContextId($contextId, $rangeInfo=null)
Basic class describing a genre.
Definition: Genre.inc.php:23
getDataObjectSettings($tableName, $idFieldName, $idFieldValue, $dataObject)
Definition: DAO.inc.php:568
deleteById($genreId)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
getAdditionalFieldNames()
installDefaults($contextId, $locales)
updateLocaleFields($genre)