Open Journal Systems  3.0.0
 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->_fromRow($result->GetRowAssoc(false));
41  }
42 
43  $result->Close();
44  return $returner;
45  }
46 
53  function getSubscriptionByUserForJournal($userId, $journalId) {
54  $result = $this->retrieveRange(
55  'SELECT s.*
56  FROM
57  subscriptions s,
58  subscription_types st
59  WHERE s.type_id = st.type_id
60  AND st.institutional = 0
61  AND s.user_id = ?
62  AND s.journal_id = ?',
63  array(
64  $userId,
65  $journalId
66  )
67  );
68 
69  $returner = null;
70  if ($result->RecordCount() != 0) {
71  $returner = $this->_fromRow($result->GetRowAssoc(false));
72  }
73 
74  $result->Close();
75  return $returner;
76  }
77 
83  function getSubscriptionsByUser($userId, $rangeInfo = null) {
84  $result = $this->retrieveRange(
85  'SELECT s.*
86  FROM
87  subscriptions s,
88  subscription_types st
89  WHERE s.type_id = st.type_id
90  AND st.institutional = 0
91  AND s.user_id = ?',
92  $userId,
93  $rangeInfo
94  );
95 
96  $returner = new DAOResultFactory($result, $this, '_fromRow');
97 
98  return $returner;
99  }
100 
107  function getSubscriptionIdByUser($userId, $journalId) {
108  $result = $this->retrieve(
109  'SELECT s.subscription_id
110  FROM
111  subscriptions s,
112  subscription_types st
113  WHERE s.type_id = st.type_id
114  AND st.institutional = 0
115  AND s.user_id = ?
116  AND s.journal_id = ?',
117  array(
118  $userId,
119  $journalId
120  )
121  );
122 
123  $returner = isset($result->fields[0]) ? $result->fields[0] : 0;
124 
125  $result->Close();
126  return $returner;
127  }
128 
134  function getStatusCount($journalId, $status = null) {
135  $params = array((int) $journalId);
136  if ($status !== null) $params[] = (int) $status;
137 
138  $result = $this->retrieve(
139  'SELECT COUNT(*)
140  FROM subscriptions s,
141  subscription_types st
142  WHERE s.type_id = st.type_id AND
143  st.institutional = 0 AND
144  s.journal_id = ?
145  ' . ($status !== null?' AND s.status = ?':''),
146  $params
147  );
148 
149  $returner = isset($result->fields[0]) ? $result->fields[0] : 0;
150 
151  $result->Close();
152  return $returner;
153  }
154 
160  function getSubscribedUserCount($journalId) {
161  return $this->getStatusCount($journalId);
162  }
163 
169  function subscriptionExists($subscriptionId) {
170  $result = $this->retrieve(
171  'SELECT COUNT(*)
172  FROM
173  subscriptions s,
174  subscription_types st
175  WHERE s.type_id = st.type_id
176  AND st.institutional = 0
177  AND s.subscription_id = ?',
178  $subscriptionId
179  );
180 
181  $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
182 
183  $result->Close();
184  return $returner;
185  }
186 
193  function subscriptionExistsByUser($subscriptionId, $userId) {
194  $result = $this->retrieve(
195  'SELECT COUNT(*)
196  FROM
197  subscriptions s,
198  subscription_types st
199  WHERE s.type_id = st.type_id
200  AND st.institutional = 0
201  AND s.subscription_id = ?
202  AND s.user_id = ?',
203  array(
204  $subscriptionId,
205  $userId
206  )
207  );
208 
209  $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
210 
211  $result->Close();
212  return $returner;
213  }
214 
221  function subscriptionExistsByUserForJournal($userId, $journalId) {
222  $result = $this->retrieve(
223  'SELECT COUNT(*)
224  FROM
225  subscriptions s,
226  subscription_types st
227  WHERE s.type_id = st.type_id
228  AND st.institutional = 0
229  AND s.user_id = ?
230  AND s.journal_id = ?',
231  array(
232  $userId,
233  $journalId
234  )
235  );
236 
237  $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
238 
239  $result->Close();
240  return $returner;
241  }
242 
247  function newDataObject() {
248  return new IndividualSubscription();
249  }
250 
256  function _fromRow($row) {
257  $individualSubscription = parent::_fromRow($row);
258  HookRegistry::call('IndividualSubscriptionDAO::_fromRow', array(&$individualSubscription, &$row));
259 
260  return $individualSubscription;
261  }
262 
268  function insertSubscription($individualSubscription) {
269  return $this->_insertSubscription($individualSubscription);
270  }
271 
277  function updateSubscription($individualSubscription) {
278  return $this->_updateSubscription($individualSubscription);
279  }
280 
285  function deleteSubscriptionById($subscriptionId) {
286  $this->update(
287  'DELETE
288  FROM
289  subscriptions
290  WHERE subscription_id = ?',
291  (int) $subscriptionId
292  );
293  }
294 
300  function deleteSubscriptionsByJournal($journalId) {
301  $result = $this->retrieve(
302  'SELECT s.subscription_id
303  FROM
304  subscriptions s
305  WHERE s.journal_id = ?',
306  $journalId
307  );
308 
309  $returner = true;
310  if ($result->RecordCount() != 0) {
311  while (!$result->EOF) {
312  $subscriptionId = $result->fields[0];
313  $returner = $this->deleteSubscriptionById($subscriptionId);
314  if (!$returner) {
315  break;
316  }
317  $result->MoveNext();
318  }
319  }
320 
321  $result->Close();
322  return $returner;
323  }
324 
330  function deleteSubscriptionsByUserId($userId) {
331  $result = $this->retrieve(
332  'SELECT s.subscription_id
333  FROM
334  subscriptions s
335  WHERE s.user_id = ?',
336  $userId
337  );
338 
339  $returner = true;
340  if ($result->RecordCount() != 0) {
341  while (!$result->EOF) {
342  $subscriptionId = $result->fields[0];
343  $returner = $this->deleteSubscriptionById($subscriptionId);
344  if (!$returner) {
345  break;
346  }
347  $result->MoveNext();
348  }
349  }
350 
351  $result->Close();
352  return $returner;
353  }
354 
361  function deleteSubscriptionsByUserIdForJournal($userId, $journalId) {
362  $result = $this->retrieve(
363  'SELECT s.subscription_id
364  FROM
365  subscriptions s
366  WHERE s.user_id = ?
367  AND s.journal_id = ?',
368  array (
369  $userId,
370  $journalId
371  )
372  );
373 
374  $returner = true;
375  if ($result->RecordCount() != 0) {
376  while (!$result->EOF) {
377  $subscriptionId = $result->fields[0];
378  $returner = $this->deleteSubscriptionById($subscriptionId);
379  if (!$returner) {
380  break;
381  }
382  $result->MoveNext();
383  }
384  }
385 
386  $result->Close();
387  return $returner;
388  }
389 
395  function deleteSubscriptionsByTypeId($subscriptionTypeId) {
396  $result = $this->retrieve(
397  'SELECT s.subscription_id
398  FROM
399  subscriptions s
400  WHERE s.type_id = ?',
401  $subscriptionTypeId
402  );
403 
404  $returner = true;
405  if ($result->RecordCount() != 0) {
406  while (!$result->EOF) {
407  $subscriptionId = $result->fields[0];
408  $returner = $this->deleteSubscriptionById($subscriptionId);
409  if (!$returner) {
410  break;
411  }
412  $result->MoveNext();
413  }
414  }
415 
416  $result->Close();
417  return $returner;
418  }
419 
424  function getSubscriptions($rangeInfo = null) {
425  $result = $this->retrieveRange(
426  'SELECT s.*
427  FROM
428  subscriptions s,
429  subscription_types st,
430  users u
431  WHERE s.type_id = st.type_id
432  AND st.institutional = 0
433  AND s.user_id = u.user_id
434  ORDER BY
435  u.last_name ASC,
436  s.subscription_id',
437  false,
438  $rangeInfo
439  );
440 
441  return new DAOResultFactory($result, $this, '_fromRow');
442  }
443 
448  function getSubscribedUsers($journalId, $rangeInfo = null) {
449  $result = $this->retrieveRange(
450  'SELECT u.*
451  FROM subscriptions s,
452  subscription_types st,
453  users u
454  WHERE s.type_id = st.type_id AND
455  st.institutional = 0 AND
456  s.user_id = u.user_id AND
457  s.journal_id = ?
458  ORDER BY u.last_name ASC, s.subscription_id',
459  array((int) $journalId),
460  $rangeInfo
461  );
462 
463  $userDao = DAORegistry::getDAO('UserDAO');
464  return new DAOResultFactory($result, $userDao, '_returnUserFromRow');
465  }
466 
479  function getSubscriptionsByJournalId($journalId, $status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, $rangeInfo = null) {
480 
481  $params = array($journalId);
482  $searchSql = parent::_generateSearchSQL($status, $searchField, $searchMatch, $search, $dateField, $dateFrom, $dateTo, $params);
483 
484  $sql = 'SELECT s.*
485  FROM
486  subscriptions s,
487  subscription_types st,
488  users u
489  WHERE s.type_id = st.type_id
490  AND st.institutional = 0
491  AND s.user_id = u.user_id
492  AND s.journal_id = ?';
493 
494  $result = $this->retrieveRange(
495  $sql . ' ' . $searchSql . ' ORDER BY u.last_name ASC, s.subscription_id',
496  count($params)===1?array_shift($params):$params,
497  $rangeInfo
498  );
499 
500  return new DAOResultFactory($result, $this, '_fromRow');
501  }
502 
511  function isValidIndividualSubscription($userId, $journalId, $check = SUBSCRIPTION_DATE_BOTH, $checkDate = null) {
512  if (empty($userId) || empty($journalId)) {
513  return false;
514  }
515  $returner = false;
516 
517  $today = $this->dateToDB(Core::getCurrentDate());
518 
519  if ($checkDate == null) {
520  $checkDate = $today;
521  } else {
522  $checkDate = $this->dateToDB($checkDate);
523  }
524 
525  switch($check) {
526  case SUBSCRIPTION_DATE_START:
527  $dateSql = sprintf('%s >= s.date_start AND %s >= s.date_start', $checkDate, $today);
528  break;
529  case SUBSCRIPTION_DATE_END:
530  $dateSql = sprintf('%s <= s.date_end AND %s >= s.date_start', $checkDate, $today);
531  break;
532  default:
533  $dateSql = sprintf('%s >= s.date_start AND %s <= s.date_end', $checkDate, $checkDate);
534  }
535 
536  $nonExpiringSql = "AND ((st.non_expiring = 1) OR (st.non_expiring = 0 AND ($dateSql)))";
537 
538  $result = $this->retrieve('
539  SELECT s.subscription_id
540  FROM
541  subscriptions s,
542  subscription_types st
543  WHERE s.user_id = ?
544  AND s.journal_id = ?
545  AND s.status = ' . SUBSCRIPTION_STATUS_ACTIVE . '
546  AND s.type_id = st.type_id
547  AND st.institutional = 0 '
548  . $nonExpiringSql .
549  ' AND (st.format = ' . SUBSCRIPTION_TYPE_FORMAT_ONLINE . '
550  OR st.format = ' . SUBSCRIPTION_TYPE_FORMAT_PRINT_ONLINE . ')',
551  array(
552  $userId,
553  $journalId
554  )
555  );
556 
557  if ($result->RecordCount() != 0) {
558  $returner = $result->fields[0];
559  }
560 
561  $result->Close();
562  return $returner;
563  }
564 
571  function getSubscriptionsByDateEnd($dateEnd, $journalId, $rangeInfo = null) {
572  $dateEnd = explode('-', $dateEnd);
573 
574  $result = $this->retrieveRange(
575  'SELECT s.*
576  FROM
577  subscriptions s,
578  subscription_types st,
579  users u
580  WHERE s.type_id = st.type_id
581  AND s.status = ' . SUBSCRIPTION_STATUS_ACTIVE . ' ' .
582  'AND st.institutional = 0
583  AND u.user_id = s.user_id AND
584  EXTRACT(YEAR FROM s.date_end) = ? AND
585  EXTRACT(MONTH FROM s.date_end) = ? AND
586  EXTRACT(DAY FROM s.date_end) = ? AND
587  s.journal_id = ?
588  ORDER BY u.last_name ASC, s.subscription_id',
589  array(
590  $dateEnd[0],
591  $dateEnd[1],
592  $dateEnd[2],
593  $journalId
594  ), $rangeInfo
595  );
596 
597  return new DAOResultFactory($result, $this, '_fromRow');
598  }
599 
606  function renewSubscription($individualSubscription) {
607  return $this->_renewSubscription($individualSubscription);
608  }
609 }
610 
611 ?>
static & getDAO($name, $dbconn=null)
subscriptionExistsByUser($subscriptionId, $userId)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
insertSubscription($individualSubscription)
Operations for retrieving and modifying IndividualSubscription objects.
deleteSubscriptionsByTypeId($subscriptionTypeId)
subscriptionExistsByUserForJournal($userId, $journalId)
dateToDB($d)
Definition: DAO.inc.php:303
Abstract class for retrieving and modifying subscriptions.
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:174
getSubscriptionsByDateEnd($dateEnd, $journalId, $rangeInfo=null)
getSubscriptionsByJournalId($journalId, $status=null, $searchField=null, $searchMatch=null, $search=null, $dateField=null, $dateFrom=null, $dateTo=null, $rangeInfo=null)
renewSubscription($individualSubscription)
getSubscriptionsByUser($userId, $rangeInfo=null)
isValidIndividualSubscription($userId, $journalId, $check=SUBSCRIPTION_DATE_BOTH, $checkDate=null)
static call($hookName, $args=null)
_renewSubscription($subscription)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
getStatusCount($journalId, $status=null)
deleteSubscriptionsByUserIdForJournal($userId, $journalId)
_insertSubscription($subscription)
_updateSubscription($subscription)
getSubscribedUsers($journalId, $rangeInfo=null)
getSubscriptionByUserForJournal($userId, $journalId)
Basic class describing an individual (non-institutional) subscription.
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
updateSubscription($individualSubscription)
static getCurrentDate($ts=null)
Definition: Core.inc.php:95