Open Journal Systems  2.4.3
 All Classes Namespaces Functions Variables Groups Pages
IndividualSubscriptionDAO.inc.php
1 <?php
2 
17 import('classes.subscription.SubscriptionDAO');
18 import('classes.subscription.IndividualSubscription');
19 
26  function &getSubscription($subscriptionId) {
27  $result =& $this->retrieve(
28  'SELECT s.*
29  FROM
30  subscriptions s,
31  subscription_types st
32  WHERE s.type_id = st.type_id
33  AND st.institutional = 0
34  AND s.subscription_id = ?',
35  $subscriptionId
36  );
37 
38  $returner = null;
39  if ($result->RecordCount() != 0) {
40  $returner =& $this->_returnSubscriptionFromRow($result->GetRowAssoc(false));
41  }
42 
43  $result->Close();
44  unset($result);
45 
46  return $returner;
47  }
48 
55  function &getSubscriptionByUserForJournal($userId, $journalId) {
56  $result =& $this->retrieveRange(
57  'SELECT s.*
58  FROM
59  subscriptions s,
60  subscription_types st
61  WHERE s.type_id = st.type_id
62  AND st.institutional = 0
63  AND s.user_id = ?
64  AND s.journal_id = ?',
65  array(
66  $userId,
67  $journalId
68  )
69  );
70 
71  $returner = null;
72  if ($result->RecordCount() != 0) {
73  $returner =& $this->_returnSubscriptionFromRow($result->GetRowAssoc(false));
74  }
75 
76  $result->Close();
77  unset($result);
78 
79  return $returner;
80  }
81 
87  function &getSubscriptionsByUser($userId, $rangeInfo = null) {
88  $result =& $this->retrieveRange(
89  'SELECT s.*
90  FROM
91  subscriptions s,
92  subscription_types st
93  WHERE s.type_id = st.type_id
94  AND st.institutional = 0
95  AND s.user_id = ?',
96  $userId,
97  $rangeInfo
98  );
99 
100  $returner = new DAOResultFactory($result, $this, '_returnSubscriptionFromRow');
101 
102  return $returner;
103  }
104 
111  function getSubscriptionIdByUser($userId, $journalId) {
112  $result =& $this->retrieve(
113  'SELECT s.subscription_id
114  FROM
115  subscriptions s,
116  subscription_types st
117  WHERE s.type_id = st.type_id
118  AND st.institutional = 0
119  AND s.user_id = ?
120  AND s.journal_id = ?',
121  array(
122  $userId,
123  $journalId
124  )
125  );
126 
127  $returner = isset($result->fields[0]) ? $result->fields[0] : 0;
128 
129  $result->Close();
130  unset($result);
131 
132  return $returner;
133  }
134 
140  function getStatusCount($journalId, $status = null) {
141  $params = array((int) $journalId);
142  if ($status !== null) $params[] = (int) $status;
143 
144  $result =& $this->retrieve(
145  'SELECT COUNT(*)
146  FROM subscriptions s,
147  subscription_types st
148  WHERE s.type_id = st.type_id AND
149  st.institutional = 0 AND
150  s.journal_id = ?
151  ' . ($status !== null?' AND s.status = ?':''),
152  $params
153  );
154 
155  $returner = isset($result->fields[0]) ? $result->fields[0] : 0;
156 
157  $result->Close();
158  unset($result);
159 
160  return $returner;
161  }
162 
168  function getSubscribedUserCount($journalId) {
169  return $this->getStatusCount($journalId);
170  }
171 
177  function subscriptionExists($subscriptionId) {
178  $result =& $this->retrieve(
179  'SELECT COUNT(*)
180  FROM
181  subscriptions s,
182  subscription_types st
183  WHERE s.type_id = st.type_id
184  AND st.institutional = 0
185  AND s.subscription_id = ?',
186  $subscriptionId
187  );
188 
189  $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
190 
191  $result->Close();
192  unset($result);
193 
194  return $returner;
195  }
196 
203  function subscriptionExistsByUser($subscriptionId, $userId) {
204  $result =& $this->retrieve(
205  'SELECT COUNT(*)
206  FROM
207  subscriptions s,
208  subscription_types st
209  WHERE s.type_id = st.type_id
210  AND st.institutional = 0
211  AND s.subscription_id = ?
212  AND s.user_id = ?',
213  array(
214  $subscriptionId,
215  $userId
216  )
217  );
218 
219  $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
220 
221  $result->Close();
222  unset($result);
223 
224  return $returner;
225  }
226 
233  function subscriptionExistsByUserForJournal($userId, $journalId) {
234  $result =& $this->retrieve(
235  'SELECT COUNT(*)
236  FROM
237  subscriptions s,
238  subscription_types st
239  WHERE s.type_id = st.type_id
240  AND st.institutional = 0
241  AND s.user_id = ?
242  AND s.journal_id = ?',
243  array(
244  $userId,
245  $journalId
246  )
247  );
248 
249  $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
250 
251  $result->Close();
252  unset($result);
253 
254  return $returner;
255  }
256 
261  function createObject() {
262  return new IndividualSubscription();
263  }
264 
270  function &_returnSubscriptionFromRow(&$row) {
271  $individualSubscription = parent::_returnSubscriptionFromRow($row);
272  HookRegistry::call('IndividualSubscriptionDAO::_returnSubscriptionFromRow', array(&$individualSubscription, &$row));
273 
274  return $individualSubscription;
275  }
276 
282  function insertSubscription(&$individualSubscription) {
283  return $this->_insertSubscription($individualSubscription);
284  }
285 
291  function updateSubscription(&$individualSubscription) {
292  return $this->_updateSubscription($individualSubscription);
293  }
294 
300  function deleteSubscriptionById($subscriptionId) {
301  if ($this->subscriptionExists($subscriptionId)) {
302  return $this->update(
303  'DELETE
304  FROM
305  subscriptions
306  WHERE subscription_id = ?',
307  $subscriptionId
308  );
309  } else {
310  return false;
311  }
312  }
313 
319  function deleteSubscriptionsByJournal($journalId) {
320  $result =& $this->retrieve(
321  'SELECT s.subscription_id
322  FROM
323  subscriptions s
324  WHERE s.journal_id = ?',
325  $journalId
326  );
327 
328  $returner = true;
329  if ($result->RecordCount() != 0) {
330  while (!$result->EOF) {
331  $subscriptionId = $result->fields[0];
332  $returner = $this->deleteSubscriptionById($subscriptionId);
333  if (!$returner) {
334  break;
335  }
336  $result->moveNext();
337  }
338  }
339 
340  $result->Close();
341  unset($result);
342 
343  return $returner;
344  }
345 
351  function deleteSubscriptionsByUserId($userId) {
352  $result =& $this->retrieve(
353  'SELECT s.subscription_id
354  FROM
355  subscriptions s
356  WHERE s.user_id = ?',
357  $userId
358  );
359 
360  $returner = true;
361  if ($result->RecordCount() != 0) {
362  while (!$result->EOF) {
363  $subscriptionId = $result->fields[0];
364  $returner = $this->deleteSubscriptionById($subscriptionId);
365  if (!$returner) {
366  break;
367  }
368  $result->moveNext();
369  }
370  }
371 
372  $result->Close();
373  unset($result);
374 
375  return $returner;
376  }
377 
384  function deleteSubscriptionsByUserIdForJournal($userId, $journalId) {
385  $result =& $this->retrieve(
386  'SELECT s.subscription_id
387  FROM
388  subscriptions s
389  WHERE s.user_id = ?
390  AND s.journal_id = ?',
391  array (
392  $userId,
393  $journalId
394  )
395  );
396 
397  $returner = true;
398  if ($result->RecordCount() != 0) {
399  while (!$result->EOF) {
400  $subscriptionId = $result->fields[0];
401  $returner = $this->deleteSubscriptionById($subscriptionId);
402  if (!$returner) {
403  break;
404  }
405  $result->moveNext();
406  }
407  }
408 
409  $result->Close();
410  unset($result);
411 
412  return $returner;
413  }
414 
420  function deleteSubscriptionsByTypeId($subscriptionTypeId) {
421  $result =& $this->retrieve(
422  'SELECT s.subscription_id
423  FROM
424  subscriptions s
425  WHERE s.type_id = ?',
426  $subscriptionTypeId
427  );
428 
429  $returner = true;
430  if ($result->RecordCount() != 0) {
431  while (!$result->EOF) {
432  $subscriptionId = $result->fields[0];
433  $returner = $this->deleteSubscriptionById($subscriptionId);
434  if (!$returner) {
435  break;
436  }
437  $result->moveNext();
438  }
439  }
440 
441  $result->Close();
442  unset($result);
443 
444  return $returner;
445  }
446 
451  function &getSubscriptions($rangeInfo = null) {
452  $result =& $this->retrieveRange(
453  'SELECT s.*
454  FROM
455  subscriptions s,
456  subscription_types st,
457  users u
458  WHERE s.type_id = st.type_id
459  AND st.institutional = 0
460  AND s.user_id = u.user_id
461  ORDER BY
462  u.last_name ASC,
463  s.subscription_id',
464  false,
465  $rangeInfo
466  );
467 
468  $returner = new DAOResultFactory($result, $this, '_returnSubscriptionFromRow');
469 
470  return $returner;
471  }
472 
477  function &getSubscribedUsers($journalId, $rangeInfo = null) {
478  $result =& $this->retrieveRange(
479  'SELECT u.*
480  FROM subscriptions s,
481  subscription_types st,
482  users u
483  WHERE s.type_id = st.type_id AND
484  st.institutional = 0 AND
485  s.user_id = u.user_id AND
486  s.journal_id = ?
487  ORDER BY u.last_name ASC, s.subscription_id',
488  array((int) $journalId),
489  $rangeInfo
490  );
491 
492  $userDao =& DAORegistry::getDAO('UserDAO');
493  $returner = new DAOResultFactory($result, $userDao, '_returnUserFromRow');
494 
495  return $returner;
496  }
497 
510  function &getSubscriptionsByJournalId($journalId, $status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, $rangeInfo = null) {
511 
512  $params = array($journalId);
513  $searchSql = parent::_generateSearchSQL($status, $searchField, $searchMatch, $search, $dateField, $dateFrom, $dateTo, $params);
514 
515  $sql = 'SELECT s.*
516  FROM
517  subscriptions s,
518  subscription_types st,
519  users u
520  WHERE s.type_id = st.type_id
521  AND st.institutional = 0
522  AND s.user_id = u.user_id
523  AND s.journal_id = ?';
524 
525  $result =& $this->retrieveRange(
526  $sql . ' ' . $searchSql . ' ORDER BY u.last_name ASC, s.subscription_id',
527  count($params)===1?array_shift($params):$params,
528  $rangeInfo
529  );
530 
531  $returner = new DAOResultFactory($result, $this, '_returnSubscriptionFromRow');
532 
533  return $returner;
534  }
535 
544  function isValidIndividualSubscription($userId, $journalId, $check = SUBSCRIPTION_DATE_BOTH, $checkDate = null) {
545  if (empty($userId) || empty($journalId)) {
546  return false;
547  }
548  $returner = false;
549 
550  $today = $this->dateToDB(Core::getCurrentDate());
551 
552  if ($checkDate == null) {
553  $checkDate = $today;
554  } else {
555  $checkDate = $this->dateToDB($checkDate);
556  }
557 
558  switch($check) {
559  case SUBSCRIPTION_DATE_START:
560  $dateSql = sprintf('%s >= s.date_start AND %s >= s.date_start', $checkDate, $today);
561  break;
562  case SUBSCRIPTION_DATE_END:
563  $dateSql = sprintf('%s <= s.date_end AND %s >= s.date_start', $checkDate, $today);
564  break;
565  default:
566  $dateSql = sprintf('%s >= s.date_start AND %s <= s.date_end', $checkDate, $checkDate);
567  }
568 
569  $nonExpiringSql = "AND ((st.non_expiring = 1) OR (st.non_expiring = 0 AND ($dateSql)))";
570 
571  $result =& $this->retrieve('
572  SELECT s.subscription_id
573  FROM
574  subscriptions s,
575  subscription_types st
576  WHERE s.user_id = ?
577  AND s.journal_id = ?
578  AND s.status = ' . SUBSCRIPTION_STATUS_ACTIVE . '
579  AND s.type_id = st.type_id
580  AND st.institutional = 0 '
581  . $nonExpiringSql .
582  ' AND (st.format = ' . SUBSCRIPTION_TYPE_FORMAT_ONLINE . '
583  OR st.format = ' . SUBSCRIPTION_TYPE_FORMAT_PRINT_ONLINE . ')',
584  array(
585  $userId,
586  $journalId
587  )
588  );
589 
590  if ($result->RecordCount() != 0) {
591  $returner = $result->fields[0];
592  }
593 
594  $result->Close();
595  unset($result);
596 
597  return $returner;
598  }
599 
606  function &getSubscriptionsByDateEnd($dateEnd, $journalId, $rangeInfo = null) {
607  $dateEnd = explode('-', $dateEnd);
608 
609  $result =& $this->retrieveRange(
610  'SELECT s.*
611  FROM
612  subscriptions s,
613  subscription_types st,
614  users u
615  WHERE s.type_id = st.type_id
616  AND s.status = ' . SUBSCRIPTION_STATUS_ACTIVE . ' ' .
617  'AND st.institutional = 0
618  AND u.user_id = s.user_id AND
619  EXTRACT(YEAR FROM s.date_end) = ? AND
620  EXTRACT(MONTH FROM s.date_end) = ? AND
621  EXTRACT(DAY FROM s.date_end) = ? AND
622  s.journal_id = ?
623  ORDER BY u.last_name ASC, s.subscription_id',
624  array(
625  $dateEnd[0],
626  $dateEnd[1],
627  $dateEnd[2],
628  $journalId
629  ), $rangeInfo
630  );
631 
632  $returner = new DAOResultFactory($result, $this, '_returnSubscriptionFromRow');
633 
634  return $returner;
635  }
636 
643  function renewSubscription(&$individualSubscription) {
644  return $this->_renewSubscription($individualSubscription);
645  }
646 }
647 
648 ?>
_renewSubscription(&$subscription)
_updateSubscription(&$subscription)
subscriptionExistsByUser($subscriptionId, $userId)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
Operations for retrieving and modifying IndividualSubscription objects.
updateSubscription(&$individualSubscription)
deleteSubscriptionsByTypeId($subscriptionTypeId)
subscriptionExistsByUserForJournal($userId, $journalId)
getCurrentDate($ts=null)
Definition: Core.inc.php:94
dateToDB($d)
Definition: DAO.inc.php:306
Abstract class for retrieving and modifying subscriptions.
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
isValidIndividualSubscription($userId, $journalId, $check=SUBSCRIPTION_DATE_BOTH, $checkDate=null)
renewSubscription(&$individualSubscription)
& getSubscribedUsers($journalId, $rangeInfo=null)
& getSubscriptionsByJournalId($journalId, $status=null, $searchField=null, $searchMatch=null, $search=null, $dateField=null, $dateFrom=null, $dateTo=null, $rangeInfo=null)
_insertSubscription(&$subscription)
& getSubscriptionByUserForJournal($userId, $journalId)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
getStatusCount($journalId, $status=null)
deleteSubscriptionsByUserIdForJournal($userId, $journalId)
insertSubscription(&$individualSubscription)
& getDAO($name, $dbconn=null)
call($hookName, $args=null)
& getSubscriptionsByUser($userId, $rangeInfo=null)
Basic class describing an individual (non-institutional) subscription.
& getSubscriptionsByDateEnd($dateEnd, $journalId, $rangeInfo=null)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211