Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
SubscriptionTypeDAO.inc.php
1 <?php
2 
17 import('classes.subscription.SubscriptionType');
18 
19 class SubscriptionTypeDAO extends DAO {
25  function &getSubscriptionType($typeId) {
26  $result = $this->retrieve(
27  'SELECT * FROM subscription_types WHERE type_id = ?', $typeId
28  );
29 
30  $returner = null;
31  if ($result->RecordCount() != 0) {
32  $returner =& $this->_returnSubscriptionTypeFromRow($result->GetRowAssoc(false));
33  }
34 
35  $result->Close();
36  return $returner;
37  }
38 
44  function getSubscriptionTypeJournalId($typeId) {
45  $result = $this->retrieve(
46  'SELECT journal_id FROM subscription_types WHERE type_id = ?', $typeId
47  );
48 
49  $returner = isset($result->fields[0]) ? $result->fields[0] : false;
50 
51  $result->Close();
52  return $returner;
53  }
54 
60  function getSubscriptionTypeName($typeId) {
61  $result = $this->retrieve(
62  'SELECT COALESCE(l.setting_value, p.setting_value) FROM subscription_type_settings l LEFT JOIN subscription_type_settings p ON (p.type_id = ? AND p.setting_name = ? AND p.locale = ?) WHERE l.type_id = ? AND l.setting_name = ? AND l.locale = ?',
63  array(
64  $typeId, 'name', AppLocale::getLocale(),
65  $typeId, 'name', AppLocale::getPrimaryLocale()
66  )
67  );
68 
69  $returner = isset($result->fields[0]) ? $result->fields[0] : false;
70 
71  $result->Close();
72  return $returner;
73  }
74 
81  $result = $this->retrieve(
82  'SELECT institutional FROM subscription_types WHERE type_id = ?', $typeId
83  );
84 
85  $returner = isset($result->fields[0]) ? $result->fields[0] : false;
86 
87  $result->Close();
88  return $returner;
89  }
90 
96  function getSubscriptionTypeMembership($typeId) {
97  $result = $this->retrieve(
98  'SELECT membership FROM subscription_types WHERE type_id = ?', $typeId
99  );
100 
101  $returner = isset($result->fields[0]) ? $result->fields[0] : false;
102 
103  $result->Close();
104  return $returner;
105  }
106 
112  function getSubscriptionTypeNonExpiring($typeId) {
113  $result = $this->retrieve(
114  'SELECT non_expiring FROM subscription_types WHERE type_id = ?', $typeId
115  );
116 
117  $returner = isset($result->fields[0]) ? $result->fields[0] : false;
118 
119  $result->Close();
120  return $returner;
121  }
122 
129  $result = $this->retrieve(
130  'SELECT disable_public_display FROM subscription_types WHERE type_id = ?', $typeId
131  );
132 
133  $returner = isset($result->fields[0]) ? $result->fields[0] : false;
134 
135  $result->Close();
136  return $returner;
137  }
138 
145  function subscriptionTypeExistsByTypeId($typeId, $journalId) {
146  $result = $this->retrieve(
147  'SELECT COUNT(*)
148  FROM subscription_types
149  WHERE type_id = ?
150  AND journal_id = ?',
151  array(
152  $typeId,
153  $journalId
154  )
155  );
156  $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
157 
158  $result->Close();
159  return $returner;
160  }
161 
168  $subscriptionType = new SubscriptionType();
169  $subscriptionType->setTypeId($row['type_id']);
170  $subscriptionType->setJournalId($row['journal_id']);
171  $subscriptionType->setCost($row['cost']);
172  $subscriptionType->setCurrencyCodeAlpha($row['currency_code_alpha']);
173  $subscriptionType->setNonExpiring($row['non_expiring']);
174  $subscriptionType->setDuration($row['duration']);
175  $subscriptionType->setFormat($row['format']);
176  $subscriptionType->setInstitutional($row['institutional']);
177  $subscriptionType->setMembership($row['membership']);
178  $subscriptionType->setDisablePublicDisplay($row['disable_public_display']);
179  $subscriptionType->setSequence($row['seq']);
180 
181  $this->getDataObjectSettings('subscription_type_settings', 'type_id', $row['type_id'], $subscriptionType);
182 
183  HookRegistry::call('SubscriptionTypeDAO::_returnSubscriptionTypeFromRow', array(&$subscriptionType, &$row));
184 
185  return $subscriptionType;
186  }
187 
192  function getLocaleFieldNames() {
193  return array('name', 'description');
194  }
195 
200  function updateLocaleFields(&$subscriptionType) {
201  $this->updateDataObjectSettings('subscription_type_settings', $subscriptionType, array(
202  'type_id' => $subscriptionType->getTypeId()
203  ));
204  }
205 
211  function insertSubscriptionType(&$subscriptionType) {
212  $this->update(
213  'INSERT INTO subscription_types
214  (journal_id, cost, currency_code_alpha, non_expiring, duration, format, institutional, membership, disable_public_display, seq)
215  VALUES
216  (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
217  array(
218  $subscriptionType->getJournalId(),
219  $subscriptionType->getCost(),
220  $subscriptionType->getCurrencyCodeAlpha(),
221  $subscriptionType->getNonExpiring(),
222  $subscriptionType->getDuration(),
223  $subscriptionType->getFormat(),
224  $subscriptionType->getInstitutional(),
225  $subscriptionType->getMembership(),
226  $subscriptionType->getDisablePublicDisplay(),
227  $subscriptionType->getSequence()
228  )
229  );
230 
231  $subscriptionType->setTypeId($this->getInsertId());
232  $this->updateLocaleFields($subscriptionType);
233  return $subscriptionType->getTypeId();
234  }
235 
241  function updateSubscriptionType(&$subscriptionType) {
242  $returner = $this->update(
243  'UPDATE subscription_types
244  SET
245  journal_id = ?,
246  cost = ?,
247  currency_code_alpha = ?,
248  non_expiring = ?,
249  duration = ?,
250  format = ?,
251  institutional = ?,
252  membership = ?,
253  disable_public_display = ?,
254  seq = ?
255  WHERE type_id = ?',
256  array(
257  $subscriptionType->getJournalId(),
258  $subscriptionType->getCost(),
259  $subscriptionType->getCurrencyCodeAlpha(),
260  $subscriptionType->getNonExpiring(),
261  $subscriptionType->getDuration(),
262  $subscriptionType->getFormat(),
263  $subscriptionType->getInstitutional(),
264  $subscriptionType->getMembership(),
265  $subscriptionType->getDisablePublicDisplay(),
266  $subscriptionType->getSequence(),
267  $subscriptionType->getTypeId()
268  )
269  );
270  $this->updateLocaleFields($subscriptionType);
271  return $returner;
272  }
273 
279  function deleteSubscriptionType(&$subscriptionType) {
280  return $this->deleteSubscriptionTypeById($subscriptionType->getTypeId());
281  }
282 
289  function deleteSubscriptionTypeById($typeId) {
290  // Delete all subscriptions corresponding to this subscription type
291  $institutional = $this->getSubscriptionTypeInstitutional($typeId);
292 
293  if ($institutional) {
294  $subscriptionDao = DAORegistry::getDAO('InstitutionalSubscriptionDAO');
295  } else {
296  $subscriptionDao = DAORegistry::getDAO('IndividualSubscriptionDAO');
297  }
298  $returner = $subscriptionDao->deleteSubscriptionsByTypeId($typeId);
299 
300  // Delete subscription type
301  if ($returner) {
302  $returner = $this->update('DELETE FROM subscription_types WHERE type_id = ?', $typeId);
303  }
304 
305  // Delete all localization settings for this subscription type
306  if ($returner) {
307  $this->update('DELETE FROM subscription_type_settings WHERE type_id = ?', $typeId);
308  }
309 
310  return $returner;
311  }
312 
319  function deleteSubscriptionTypesByJournal($journalId) {
320  $result = $this->retrieve(
321  'SELECT type_id
322  FROM subscription_types
323  WHERE journal_id = ?',
324  $journalId
325  );
326 
327  $returner = false;
328 
329  if ($result->RecordCount() != 0) {
330  $returner = true;
331  while (!$result->EOF && $returner) {
332  $typeId = $result->fields[0];
333  $returner = $this->deleteSubscriptionTypeById($typeId);
334  $result->MoveNext();
335  }
336  }
337 
338  $result->Close();
339  return $returner;
340  }
341 
347  function &getSubscriptionTypesByJournalId($journalId, $rangeInfo = null) {
348  $result = $this->retrieveRange(
349  'SELECT * FROM subscription_types WHERE journal_id = ? ORDER BY seq',
350  $journalId, $rangeInfo
351  );
352 
353  $returner = new DAOResultFactory($result, $this, '_returnSubscriptionTypeFromRow');
354  return $returner;
355  }
356 
364  function &getSubscriptionTypesByInstitutional($journalId, $institutional = false, $disablePublicDisplay = null, $rangeInfo = null) {
365  if ($institutional) $institutional = 1; else $institutional = 0;
366 
367  if ($disablePublicDisplay === null) {
368  $disablePublicDisplaySql = '';
369  } elseif ($disablePublicDisplay) {
370  $disablePublicDisplaySql = 'AND disable_public_display = 1';
371  } else {
372  $disablePublicDisplaySql = 'AND disable_public_display = 0';
373  }
374 
375  $result = $this->retrieveRange(
376  'SELECT *
377  FROM
378  subscription_types
379  WHERE journal_id = ?
380  AND institutional = ? '
381  . $disablePublicDisplaySql .
382  ' ORDER BY seq',
383  array(
384  $journalId,
385  $institutional
386  ),
387  $rangeInfo
388  );
389 
390  $returner = new DAOResultFactory($result, $this, '_returnSubscriptionTypeFromRow');
391  return $returner;
392  }
393 
400  function subscriptionTypesExistByInstitutional($journalId, $institutional = false) {
401  if ($institutional) $institutional = 1; else $institutional = 0;
402 
403  $result = $this->retrieve(
404  'SELECT COUNT(*)
405  FROM
406  subscription_types st
407  WHERE st.journal_id = ?
408  AND st.institutional = ?',
409  array(
410  $journalId,
411  $institutional
412  )
413  );
414 
415  $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
416 
417  $result->Close();
418  return $returner;
419  }
420 
425  function getInsertId() {
426  return $this->_getInsertId('subscription_types', 'type_id');
427  }
428 
432  function resequenceSubscriptionTypes($journalId) {
433  $result = $this->retrieve(
434  'SELECT type_id FROM subscription_types WHERE journal_id = ? ORDER BY seq',
435  $journalId
436  );
437 
438  for ($i=1; !$result->EOF; $i++) {
439  list($subscriptionTypeId) = $result->fields;
440  $this->update(
441  'UPDATE subscription_types SET seq = ? WHERE type_id = ?',
442  array(
443  $i,
444  $subscriptionTypeId
445  )
446  );
447 
448  $result->MoveNext();
449  }
450 
451  $result->Close();
452  }
453 }
454 
455 ?>
static & getDAO($name, $dbconn=null)
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
static getPrimaryLocale()
deleteSubscriptionTypesByJournal($journalId)
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:474
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:174
static getLocale()
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
static call($hookName, $args=null)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
subscriptionTypesExistByInstitutional($journalId, $institutional=false)
& getSubscriptionTypesByJournalId($journalId, $rangeInfo=null)
insertSubscriptionType(&$subscriptionType)
getDataObjectSettings($tableName, $idFieldName, $idFieldValue, &$dataObject)
Definition: DAO.inc.php:567
deleteSubscriptionType(&$subscriptionType)
subscriptionTypeExistsByTypeId($typeId, $journalId)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
Operations for retrieving and modifying SubscriptionType objects.
updateLocaleFields(&$subscriptionType)
getSubscriptionTypeDisablePublicDisplay($typeId)
& getSubscriptionTypesByInstitutional($journalId, $institutional=false, $disablePublicDisplay=null, $rangeInfo=null)
updateSubscriptionType(&$subscriptionType)