Open Journal Systems  2.4.4
 All Classes Namespaces Functions Variables Groups Pages
SubscriptionDAO.inc.php
1 <?php
2 
17 import('classes.subscription.Subscription');
18 import('classes.subscription.SubscriptionType');
19 
20 define('SUBSCRIPTION_USER', 0x01);
21 define('SUBSCRIPTION_MEMBERSHIP', 0x02);
22 define('SUBSCRIPTION_REFERENCE_NUMBER', 0x03);
23 define('SUBSCRIPTION_NOTES', 0x04);
24 
25 define('SUBSCRIPTION_REMINDER_FIELD_BEFORE_EXPIRY', 1);
26 define('SUBSCRIPTION_REMINDER_FIELD_AFTER_EXPIRY', 2);
27 
28 class SubscriptionDAO extends DAO {
29 
35  function &getSubscription($subscriptionId) {
36  // must be implemented by sub-classes
37  assert(false);
38  }
39 
45  function getSubscriptionJournalId($subscriptionId) {
46  $result =& $this->retrieve(
47  'SELECT journal_id FROM subscriptions WHERE subscription_id = ?', $subscriptionId
48  );
49 
50  $returner = isset($result->fields[0]) ? $result->fields[0] : false;
51 
52  $result->Close();
53  unset($result);
54 
55  return $returner;
56  }
57 
63  function flagReminded($subscriptionId, $reminderType) {
64  $this->update(
65  sprintf(
66  'UPDATE subscriptions SET %s=%s WHERE subscription_id=?',
67  $reminderType==SUBSCRIPTION_REMINDER_FIELD_BEFORE_EXPIRY?'date_reminded_before':'date_reminded_after',
69  ),
70  array((int) $subscriptionId)
71  );
72  }
73 
78  function &getStatusOptions() {
79  $statusOptions = array(
80  SUBSCRIPTION_STATUS_ACTIVE => 'subscriptions.status.active',
81  SUBSCRIPTION_STATUS_NEEDS_INFORMATION => 'subscriptions.status.needsInformation',
82  SUBSCRIPTION_STATUS_NEEDS_APPROVAL => 'subscriptions.status.needsApproval',
83  SUBSCRIPTION_STATUS_AWAITING_MANUAL_PAYMENT => 'subscriptions.status.awaitingManualPayment',
84  SUBSCRIPTION_STATUS_AWAITING_ONLINE_PAYMENT => 'subscriptions.status.awaitingOnlinePayment',
85  SUBSCRIPTION_STATUS_OTHER => 'subscriptions.status.other'
86  );
87 
88  return $statusOptions;
89  }
90 
96  function getStatusCount($status) {
97  // must be implemented by sub-classes
98  assert(false);
99  }
100 
106  function subscriptionExists($subscriptionId) {
107  // must be implemented by sub-classes
108  assert(false);
109  }
110 
117  function subscriptionExistsByUser($subscriptionId, $userId) {
118  // must be implemented by sub-classes
119  assert(false);
120  }
121 
128  function subscriptionExistsByUserForJournal($userId, $journalId) {
129  // must be implemented by sub-classes
130  assert(false);
131  }
132 
138  function insertSubscription(&$subscription) {
139  // must be implemented by sub-classes
140  assert(false);
141  }
142 
148  return $this->getInsertId('subscriptions', 'subscription_id');
149  }
150 
156  function updateSubscription(&$subscription) {
157  // must be implemented by sub-classes
158  assert(false);
159  }
160 
166  function deleteSubscriptionById($subscriptionId) {
167  // must be implemented by sub-classes
168  assert(false);
169  }
170 
176  function deleteSubscriptionsByJournal($journalId) {
177  // must be implemented by sub-classes
178  assert(false);
179  }
180 
186  function deleteSubscriptionsByUserId($userId) {
187  // must be implemented by sub-classes
188  assert(false);
189  }
190 
196  function deleteSubscriptionsByTypeId($subscriptionTypeId) {
197  // must be implemented by sub-classes
198  assert(false);
199  }
200 
205  function &getSubscriptions($rangeInfo = null) {
206  // must be implemented by sub-classes
207  assert(false);
208  }
209 
222  function &getSubscriptionsByJournalId($journalId, $status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, $rangeInfo = null) {
223  // must be implemented by sub-classes
224  assert(false);
225  }
226 
234  function &getSubscriptionsToRemind($dateEnd, $journalId, $reminderType, $rangeInfo = null) {
235  // must be implemented by sub-classes
236  assert(false);
237  }
238 
245  function renewSubscription(&$subscription) {
246  // must be implemented by sub-classes
247  assert(false);
248  }
249 
254  function _generateUserNameSearchSQL($search, $searchMatch, $prefix, &$params) {
255  $first_last = $this->concat($prefix.'first_name', '\' \'', $prefix.'last_name');
256  $first_middle_last = $this->concat($prefix.'first_name', '\' \'', $prefix.'middle_name', '\' \'', $prefix.'last_name');
257  $last_comma_first = $this->concat($prefix.'last_name', '\', \'', $prefix.'first_name');
258  $last_comma_first_middle = $this->concat($prefix.'last_name', '\', \'', $prefix.'first_name', '\' \'', $prefix.'middle_name');
259  if ($searchMatch === 'is') {
260  $searchSql = " AND (LOWER({$prefix}last_name) = LOWER(?) OR LOWER($first_last) = LOWER(?) OR LOWER($first_middle_last) = LOWER(?) OR LOWER($last_comma_first) = LOWER(?) OR LOWER($last_comma_first_middle) = LOWER(?))";
261  } elseif ($searchMatch === 'contains') {
262  $searchSql = " AND (LOWER({$prefix}last_name) LIKE LOWER(?) OR LOWER($first_last) LIKE LOWER(?) OR LOWER($first_middle_last) LIKE LOWER(?) OR LOWER($last_comma_first) LIKE LOWER(?) OR LOWER($last_comma_first_middle) LIKE LOWER(?))";
263  $search = '%' . $search . '%';
264  } else { // $searchMatch === 'startsWith'
265  $searchSql = " AND (LOWER({$prefix}last_name) LIKE LOWER(?) OR LOWER($first_last) LIKE LOWER(?) OR LOWER($first_middle_last) LIKE LOWER(?) OR LOWER($last_comma_first) LIKE LOWER(?) OR LOWER($last_comma_first_middle) LIKE LOWER(?))";
266  $search = $search . '%';
267  }
268  $params[] = $params[] = $params[] = $params[] = $params[] = $search;
269  return $searchSql;
270  }
271 
276  function _generateSearchSQL($status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, &$params) {
277 
278  $searchSql = '';
279 
280  if (!empty($search)) switch ($searchField) {
281  case SUBSCRIPTION_USER:
282  $searchSql = $this->_generateUserNameSearchSQL($search, $searchMatch, 'u.', $params);
283  break;
284  case SUBSCRIPTION_MEMBERSHIP:
285  if ($searchMatch === 'is') {
286  $searchSql = ' AND LOWER(s.membership) = LOWER(?)';
287  } elseif ($searchMatch === 'contains') {
288  $searchSql = ' AND LOWER(s.membership) LIKE LOWER(?)';
289  $search = '%' . $search . '%';
290  } else { // $searchMatch === 'startsWith'
291  $searchSql = ' AND LOWER(s.membership) LIKE LOWER(?)';
292  $search = $search . '%';
293  }
294  $params[] = $search;
295  break;
296  case SUBSCRIPTION_REFERENCE_NUMBER:
297  if ($searchMatch === 'is') {
298  $searchSql = ' AND LOWER(s.reference_number) = LOWER(?)';
299  } elseif ($searchMatch === 'contains') {
300  $searchSql = ' AND LOWER(s.reference_number) LIKE LOWER(?)';
301  $search = '%' . $search . '%';
302  } else { // $searchMatch === 'startsWith'
303  $searchSql = ' AND LOWER(s.reference_number) LIKE LOWER(?)';
304  $search = $search . '%';
305  }
306  $params[] = $search;
307  break;
308  case SUBSCRIPTION_NOTES:
309  if ($searchMatch === 'is') {
310  $searchSql = ' AND LOWER(s.notes) = LOWER(?)';
311  } elseif ($searchMatch === 'contains') {
312  $searchSql = ' AND LOWER(s.notes) LIKE LOWER(?)';
313  $search = '%' . $search . '%';
314  } else { // $searchMatch === 'startsWith'
315  $searchSql = ' AND LOWER(s.notes) LIKE LOWER(?)';
316  $search = $search . '%';
317  }
318  $params[] = $search;
319  break;
320  }
321 
322  if (!empty($dateFrom) || !empty($dateTo)) switch($dateField) {
323  case SUBSCRIPTION_DATE_START:
324  if (!empty($dateFrom)) {
325  $searchSql .= ' AND s.date_start >= ' . $this->datetimeToDB($dateFrom);
326  }
327  if (!empty($dateTo)) {
328  $searchSql .= ' AND s.date_start <= ' . $this->datetimeToDB($dateTo);
329  }
330  break;
331  case SUBSCRIPTION_DATE_END:
332  if (!empty($dateFrom)) {
333  $searchSql .= ' AND s.date_end >= ' . $this->datetimeToDB($dateFrom);
334  }
335  if (!empty($dateTo)) {
336  $searchSql .= ' AND s.date_end <= ' . $this->datetimeToDB($dateTo);
337  }
338  break;
339  }
340 
341  if (!empty($status)) {
342  $searchSql .= ' AND s.status = ' . (int) $status;
343  }
344 
345  return $searchSql;
346  }
347 
352  function createObject() {
353  // must be implemented by sub-classes
354  assert(false);
355  }
356 
362  function &_returnSubscriptionFromRow(&$row) {
363  $subscription = $this->createObject();
364  $subscription->setId($row['subscription_id']);
365  $subscription->setJournalId($row['journal_id']);
366  $subscription->setUserId($row['user_id']);
367  $subscription->setTypeId($row['type_id']);
368  $subscription->setDateStart($this->dateFromDB($row['date_start']));
369  $subscription->setDateEnd($this->dateFromDB($row['date_end']));
370  $subscription->setDateRemindedBefore($this->datetimeFromDB($row['date_reminded_before']));
371  $subscription->setDateRemindedAfter($this->datetimeFromDB($row['date_reminded_after']));
372  $subscription->setStatus($row['status']);
373  $subscription->setMembership($row['membership']);
374  $subscription->setReferenceNumber($row['reference_number']);
375  $subscription->setNotes($row['notes']);
376 
377  HookRegistry::call('SubscriptionDAO::_returnSubscriptionFromRow', array(&$subscription, &$row));
378 
379  return $subscription;
380  }
381 
387  function _insertSubscription(&$subscription) {
388  $returner = $this->update(
389  sprintf('INSERT INTO subscriptions
390  (journal_id, user_id, type_id, date_start, date_end, date_reminded_before, date_reminded_after, status, membership, reference_number, notes)
391  VALUES
392  (?, ?, ?, %s, %s, %s, %s, ?, ?, ?, ?)',
393  $this->dateToDB($subscription->getDateStart()),
394  $this->datetimeToDB($subscription->getDateEnd()),
395  $this->datetimeToDB($subscription->getDateRemindedBefore()),
396  $this->datetimeToDB($subscription->getDateRemindedAfter())
397  ), array(
398  $subscription->getJournalId(),
399  $subscription->getUserId(),
400  $subscription->getTypeId(),
401  $subscription->getStatus(),
402  $subscription->getMembership(),
403  $subscription->getReferenceNumber(),
404  $subscription->getNotes()
405  )
406  );
407 
408  $subscriptionId = $this->getInsertSubscriptionId();
409  $subscription->setId($subscriptionId);
410 
411  return $subscriptionId;
412  }
413 
419  function _updateSubscription(&$subscription) {
420  $returner = $this->update(
421  sprintf('UPDATE subscriptions
422  SET
423  journal_id = ?,
424  user_id = ?,
425  type_id = ?,
426  date_start = %s,
427  date_end = %s,
428  date_reminded_before = %s,
429  date_reminded_after = %s,
430  status = ?,
431  membership = ?,
432  reference_number = ?,
433  notes = ?
434  WHERE subscription_id = ?',
435  $this->dateToDB($subscription->getDateStart()),
436  $this->datetimeToDB($subscription->getDateEnd()),
437  $this->datetimeToDB($subscription->getDateRemindedBefore()),
438  $this->datetimeToDB($subscription->getDateRemindedAfter())
439  ), array(
440  $subscription->getJournalId(),
441  $subscription->getUserId(),
442  $subscription->getTypeId(),
443  $subscription->getStatus(),
444  $subscription->getMembership(),
445  $subscription->getReferenceNumber(),
446  $subscription->getNotes(),
447  $subscription->getId()
448  )
449  );
450 
451  return $returner;
452  }
453 
460  function _renewSubscription(&$subscription) {
461  if ($subscription->isNonExpiring()) return;
462 
463  $subscriptionTypeDao =& DAORegistry::getDAO('SubscriptionTypeDAO');
464  $subscriptionType =& $subscriptionTypeDao->getSubscriptionType($subscription->getTypeId());
465 
466  $duration = $subscriptionType->getDuration();
467  $dateEnd = strtotime($subscription->getDateEnd());
468 
469  // if the subscription is expired, extend it to today + duration of subscription
470  $time = time();
471  if ($dateEnd < $time ) $dateEnd = $time;
472 
473  $subscription->setDateEnd(mktime(23, 59, 59, date("m", $dateEnd)+$duration, date("d", $dateEnd), date("Y", $dateEnd)));
474 
475  // Reset reminder dates
476  $subscription->setDateRemindedBefore(null);
477  $subscription->setDateRemindedAfter(null);
478 
479  $this->updateSubscription($subscription);
480  }
481 }
482 
483 ?>
_renewSubscription(&$subscription)
& getSubscription($subscriptionId)
_updateSubscription(&$subscription)
& getSubscriptions($rangeInfo=null)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
getCurrentDate($ts=null)
Definition: Core.inc.php:94
dateToDB($d)
Definition: DAO.inc.php:306
getSubscriptionJournalId($subscriptionId)
Abstract class for retrieving and modifying subscriptions.
& getSubscriptionsToRemind($dateEnd, $journalId, $reminderType, $rangeInfo=null)
deleteSubscriptionsByJournal($journalId)
subscriptionExists($subscriptionId)
concat()
Definition: DAO.inc.php:52
datetimeFromDB($dt)
Definition: DAO.inc.php:316
_insertSubscription(&$subscription)
& _returnSubscriptionFromRow(&$row)
deleteSubscriptionsByUserId($userId)
subscriptionExistsByUser($subscriptionId, $userId)
flagReminded($subscriptionId, $reminderType)
& getDAO($name, $dbconn=null)
renewSubscription(&$subscription)
call($hookName, $args=null)
updateSubscription(&$subscription)
_generateSearchSQL($status=null, $searchField=null, $searchMatch=null, $search=null, $dateField=null, $dateFrom=null, $dateTo=null, &$params)
datetimeToDB($dt)
Definition: DAO.inc.php:296
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
insertSubscription(&$subscription)
& getSubscriptionsByJournalId($journalId, $status=null, $searchField=null, $searchMatch=null, $search=null, $dateField=null, $dateFrom=null, $dateTo=null, $rangeInfo=null)
deleteSubscriptionsByTypeId($subscriptionTypeId)
subscriptionExistsByUserForJournal($userId, $journalId)
deleteSubscriptionById($subscriptionId)
dateFromDB($d)
Definition: DAO.inc.php:326
_generateUserNameSearchSQL($search, $searchMatch, $prefix, &$params)