Open Journal Systems  3.0.0
 All Classes 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->setSequence($row['seq']);
178  $genre->setEnabled($row['enabled']);
179 
180  $this->getDataObjectSettings('genre_settings', 'genre_id', $row['genre_id'], $genre);
181 
182  HookRegistry::call('GenreDAO::_fromRow', array(&$genre, &$row));
183 
184  return $genre;
185  }
186 
191  function insertObject($genre) {
192  $this->update(
193  'INSERT INTO genres
194  (entry_key, seq, sortable, context_id, category, dependent)
195  VALUES
196  (?, ?, ?, ?, ?, ?)',
197  array(
198  $genre->getKey(),
199  (float) $genre->getSequence(),
200  $genre->getSortable() ? 1 : 0,
201  (int) $genre->getContextId(),
202  (int) $genre->getCategory(),
203  $genre->getDependent() ? 1 : 0
204  )
205  );
206 
207  $genre->setId($this->getInsertId());
208  $this->updateLocaleFields($genre);
209  return $genre->getId();
210  }
211 
216  function updateObject($genre) {
217  $this->update(
218  'UPDATE genres
219  SET entry_key = ?,
220  seq = ?,
221  sortable = ?,
222  dependent = ?,
223  enabled = ?
224  WHERE genre_id = ?',
225  array(
226  $genre->getKey(),
227  (float) $genre->getSequence(),
228  $genre->getSortable() ? 1 : 0,
229  $genre->getDependent() ? 1 : 0,
230  $genre->getEnabled() ? 1 : 0,
231  (int) $genre->getId(),
232  )
233  );
234  $this->updateLocaleFields($genre);
235  }
236 
241  function deleteObject($genre) {
242  return $this->deleteById($genre->getId());
243  }
244 
249  function deleteById($genreId) {
250  return $this->update(
251  'UPDATE genres SET enabled = ? WHERE genre_id = ?',
252  array(0, (int) $genreId)
253  );
254  }
255 
261  function deleteByContextId($contextId) {
262  $genres = $this->getByContextId($contextId);
263  while ($genre = $genres->next()) {
264  $this->update('DELETE FROM genre_settings WHERE genre_id = ?', (int) $genre->getId());
265  }
266  return $this->update(
267  'DELETE FROM genres WHERE context_id = ?', (int) $contextId
268  );
269  }
270 
275  function getInsertId() {
276  return $this->_getInsertId('genres', 'genre_id');
277  }
278 
284  function installDefaults($contextId, $locales) {
285  // Load all the necessary locales.
286  foreach ($locales as $locale) AppLocale::requireComponents(LOCALE_COMPONENT_APP_DEFAULT, LOCALE_COMPONENT_PKP_DEFAULT, $locale);
287 
288  $xmlDao = new XMLDAO();
289  $data = $xmlDao->parseStruct('registry/genres.xml', array('genre'));
290  if (!isset($data['genre'])) return false;
291  $seq = 0;
292 
293  foreach ($data['genre'] as $entry) {
294  $attrs = $entry['attributes'];
295  // attempt to retrieve an installed Genre with this key.
296  // Do this to preserve the genreId.
297  $genre = $this->getByKey($attrs['key'], $contextId);
298  if (!$genre) $genre = $this->newDataObject();
299  $genre->setContextId($contextId);
300  $genre->setKey($attrs['key']);
301  $genre->setSortable($attrs['sortable']);
302  $genre->setCategory($attrs['category']);
303  $genre->setDependent($attrs['dependent']);
304  $genre->setSequence($seq++);
305  foreach ($locales as $locale) {
306  $genre->setName(__($attrs['localeKey'], array(), $locale), $locale);
307  }
308  $genre->setDesignation($attrs['designation']);
309 
310  if ($genre->getId() > 0) { // existing genre.
311  $this->updateObject($genre);
312  } else {
313  $this->insertObject($genre);
314  }
315  }
316  }
317 
322  function deleteSettingsByLocale($locale) {
323  return $this->update('DELETE FROM genre_settings WHERE locale = ?', $locale);
324  }
325 }
326 
327 ?>
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()
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:457
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)
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:22
deleteById($genreId)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
getAdditionalFieldNames()
installDefaults($contextId, $locales)
updateLocaleFields($genre)