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->_returnSubscriptionFromRow($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->_returnSubscriptionFromRow($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, '_returnSubscriptionFromRow');
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 createObject() {
248  return new IndividualSubscription();
249  }
250 
256  function &_returnSubscriptionFromRow($row) {
257  $individualSubscription = parent::_returnSubscriptionFromRow($row);
258  HookRegistry::call('IndividualSubscriptionDAO::_returnSubscriptionFromRow', 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 
286  function deleteSubscriptionById($subscriptionId) {
287  if ($this->subscriptionExists($subscriptionId)) {
288  return $this->update(
289  'DELETE
290  FROM
291  subscriptions
292  WHERE subscription_id = ?',
293  $subscriptionId
294  );
295  } else {
296  return false;
297  }
298  }
299 
305  function deleteSubscriptionsByJournal($journalId) {
306  $result = $this->retrieve(
307  'SELECT s.subscription_id
308  FROM
309  subscriptions s
310  WHERE s.journal_id = ?',
311  $journalId
312  );
313 
314  $returner = true;
315  if ($result->RecordCount() != 0) {
316  while (!$result->EOF) {
317  $subscriptionId = $result->fields[0];
318  $returner = $this->deleteSubscriptionById($subscriptionId);
319  if (!$returner) {
320  break;
321  }
322  $result->MoveNext();
323  }
324  }
325 
326  $result->Close();
327  return $returner;
328  }
329 
335  function deleteSubscriptionsByUserId($userId) {
336  $result = $this->retrieve(
337  'SELECT s.subscription_id
338  FROM
339  subscriptions s
340  WHERE s.user_id = ?',
341  $userId
342  );
343 
344  $returner = true;
345  if ($result->RecordCount() != 0) {
346  while (!$result->EOF) {
347  $subscriptionId = $result->fields[0];
348  $returner = $this->deleteSubscriptionById($subscriptionId);
349  if (!$returner) {
350  break;
351  }
352  $result->MoveNext();
353  }
354  }
355 
356  $result->Close();
357  return $returner;
358  }
359 
366  function deleteSubscriptionsByUserIdForJournal($userId, $journalId) {
367  $result = $this->retrieve(
368  'SELECT s.subscription_id
369  FROM
370  subscriptions s
371  WHERE s.user_id = ?
372  AND s.journal_id = ?',
373  array (
374  $userId,
375  $journalId
376  )
377  );
378 
379  $returner = true;
380  if ($result->RecordCount() != 0) {
381  while (!$result->EOF) {
382  $subscriptionId = $result->fields[0];
383  $returner = $this->deleteSubscriptionById($subscriptionId);
384  if (!$returner) {
385  break;
386  }
387  $result->MoveNext();
388  }
389  }
390 
391  $result->Close();
392  return $returner;
393  }
394 
400  function deleteSubscriptionsByTypeId($subscriptionTypeId) {
401  $result = $this->retrieve(
402  'SELECT s.subscription_id
403  FROM
404  subscriptions s
405  WHERE s.type_id = ?',
406  $subscriptionTypeId
407  );
408 
409  $returner = true;
410  if ($result->RecordCount() != 0) {
411  while (!$result->EOF) {
412  $subscriptionId = $result->fields[0];
413  $returner = $this->deleteSubscriptionById($subscriptionId);
414  if (!$returner) {
415  break;
416  }
417  $result->MoveNext();
418  }
419  }
420 
421  $result->Close();
422  return $returner;
423  }
424 
429  function &getSubscriptions($rangeInfo = null) {
430  $result = $this->retrieveRange(
431  'SELECT s.*
432  FROM
433  subscriptions s,
434  subscription_types st,
435  users u
436  WHERE s.type_id = st.type_id
437  AND st.institutional = 0
438  AND s.user_id = u.user_id
439  ORDER BY
440  u.last_name ASC,
441  s.subscription_id',
442  false,
443  $rangeInfo
444  );
445 
446  $returner = new DAOResultFactory($result, $this, '_returnSubscriptionFromRow');
447 
448  return $returner;
449  }
450 
455  function &getSubscribedUsers($journalId, $rangeInfo = null) {
456  $result = $this->retrieveRange(
457  'SELECT u.*
458  FROM subscriptions s,
459  subscription_types st,
460  users u
461  WHERE s.type_id = st.type_id AND
462  st.institutional = 0 AND
463  s.user_id = u.user_id AND
464  s.journal_id = ?
465  ORDER BY u.last_name ASC, s.subscription_id',
466  array((int) $journalId),
467  $rangeInfo
468  );
469 
470  $userDao = DAORegistry::getDAO('UserDAO');
471  $returner = new DAOResultFactory($result, $userDao, '_returnUserFromRow');
472 
473  return $returner;
474  }
475 
488  function &getSubscriptionsByJournalId($journalId, $status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, $rangeInfo = null) {
489 
490  $params = array($journalId);
491  $searchSql = parent::_generateSearchSQL($status, $searchField, $searchMatch, $search, $dateField, $dateFrom, $dateTo, $params);
492 
493  $sql = 'SELECT s.*
494  FROM
495  subscriptions s,
496  subscription_types st,
497  users u
498  WHERE s.type_id = st.type_id
499  AND st.institutional = 0
500  AND s.user_id = u.user_id
501  AND s.journal_id = ?';
502 
503  $result = $this->retrieveRange(
504  $sql . ' ' . $searchSql . ' ORDER BY u.last_name ASC, s.subscription_id',
505  count($params)===1?array_shift($params):$params,
506  $rangeInfo
507  );
508 
509  $returner = new DAOResultFactory($result, $this, '_returnSubscriptionFromRow');
510 
511  return $returner;
512  }
513 
522  function isValidIndividualSubscription($userId, $journalId, $check = SUBSCRIPTION_DATE_BOTH, $checkDate = null) {
523  if (empty($userId) || empty($journalId)) {
524  return false;
525  }
526  $returner = false;
527 
528  $today = $this->dateToDB(Core::getCurrentDate());
529 
530  if ($checkDate == null) {
531  $checkDate = $today;
532  } else {
533  $checkDate = $this->dateToDB($checkDate);
534  }
535 
536  switch($check) {
537  case SUBSCRIPTION_DATE_START:
538  $dateSql = sprintf('%s >= s.date_start AND %s >= s.date_start', $checkDate, $today);
539  break;
540  case SUBSCRIPTION_DATE_END:
541  $dateSql = sprintf('%s <= s.date_end AND %s >= s.date_start', $checkDate, $today);
542  break;
543  default:
544  $dateSql = sprintf('%s >= s.date_start AND %s <= s.date_end', $checkDate, $checkDate);
545  }
546 
547  $nonExpiringSql = "AND ((st.non_expiring = 1) OR (st.non_expiring = 0 AND ($dateSql)))";
548 
549  $result = $this->retrieve('
550  SELECT s.subscription_id
551  FROM
552  subscriptions s,
553  subscription_types st
554  WHERE s.user_id = ?
555  AND s.journal_id = ?
556  AND s.status = ' . SUBSCRIPTION_STATUS_ACTIVE . '
557  AND s.type_id = st.type_id
558  AND st.institutional = 0 '
559  . $nonExpiringSql .
560  ' AND (st.format = ' . SUBSCRIPTION_TYPE_FORMAT_ONLINE . '
561  OR st.format = ' . SUBSCRIPTION_TYPE_FORMAT_PRINT_ONLINE . ')',
562  array(
563  $userId,
564  $journalId
565  )
566  );
567 
568  if ($result->RecordCount() != 0) {
569  $returner = $result->fields[0];
570  }
571 
572  $result->Close();
573  return $returner;
574  }
575 
582  function &getSubscriptionsByDateEnd($dateEnd, $journalId, $rangeInfo = null) {
583  $dateEnd = explode('-', $dateEnd);
584 
585  $result = $this->retrieveRange(
586  'SELECT s.*
587  FROM
588  subscriptions s,
589  subscription_types st,
590  users u
591  WHERE s.type_id = st.type_id
592  AND s.status = ' . SUBSCRIPTION_STATUS_ACTIVE . ' ' .
593  'AND st.institutional = 0
594  AND u.user_id = s.user_id AND
595  EXTRACT(YEAR FROM s.date_end) = ? AND
596  EXTRACT(MONTH FROM s.date_end) = ? AND
597  EXTRACT(DAY FROM s.date_end) = ? AND
598  s.journal_id = ?
599  ORDER BY u.last_name ASC, s.subscription_id',
600  array(
601  $dateEnd[0],
602  $dateEnd[1],
603  $dateEnd[2],
604  $journalId
605  ), $rangeInfo
606  );
607 
608  $returner = new DAOResultFactory($result, $this, '_returnSubscriptionFromRow');
609 
610  return $returner;
611  }
612 
619  function renewSubscription(&$individualSubscription) {
620  return $this->_renewSubscription($individualSubscription);
621  }
622 }
623 
624 ?>
_renewSubscription(&$subscription)
static & getDAO($name, $dbconn=null)
_updateSubscription(&$subscription)
subscriptionExistsByUser($subscriptionId, $userId)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
Operations for retrieving and modifying IndividualSubscription objects.
updateSubscription(&$individualSubscription)
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
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)
static call($hookName, $args=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)
& 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:208
static getCurrentDate($ts=null)
Definition: Core.inc.php:95