Open Journal Systems  3.0.0
 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 class SubscriptionDAO extends DAO {
26 
32  function &getSubscription($subscriptionId) {
33  // must be implemented by sub-classes
34  assert(false);
35  }
36 
42  function getSubscriptionJournalId($subscriptionId) {
43  $result = $this->retrieve(
44  'SELECT journal_id FROM subscriptions WHERE subscription_id = ?', $subscriptionId
45  );
46 
47  $returner = isset($result->fields[0]) ? $result->fields[0] : false;
48 
49  $result->Close();
50  return $returner;
51  }
52 
57  function &getStatusOptions() {
58  $statusOptions = array(
59  SUBSCRIPTION_STATUS_ACTIVE => 'subscriptions.status.active',
60  SUBSCRIPTION_STATUS_NEEDS_INFORMATION => 'subscriptions.status.needsInformation',
61  SUBSCRIPTION_STATUS_NEEDS_APPROVAL => 'subscriptions.status.needsApproval',
62  SUBSCRIPTION_STATUS_AWAITING_MANUAL_PAYMENT => 'subscriptions.status.awaitingManualPayment',
63  SUBSCRIPTION_STATUS_AWAITING_ONLINE_PAYMENT => 'subscriptions.status.awaitingOnlinePayment',
64  SUBSCRIPTION_STATUS_OTHER => 'subscriptions.status.other'
65  );
66 
67  return $statusOptions;
68  }
69 
75  function getStatusCount($status) {
76  // must be implemented by sub-classes
77  assert(false);
78  }
79 
85  function subscriptionExists($subscriptionId) {
86  // must be implemented by sub-classes
87  assert(false);
88  }
89 
96  function subscriptionExistsByUser($subscriptionId, $userId) {
97  // must be implemented by sub-classes
98  assert(false);
99  }
100 
107  function subscriptionExistsByUserForJournal($userId, $journalId) {
108  // must be implemented by sub-classes
109  assert(false);
110  }
111 
117  function insertSubscription(&$subscription) {
118  // must be implemented by sub-classes
119  assert(false);
120  }
121 
126  function getInsertId() {
127  return $this->_getInsertId('subscriptions', 'subscription_id');
128  }
129 
135  function updateSubscription(&$subscription) {
136  // must be implemented by sub-classes
137  assert(false);
138  }
139 
145  function deleteSubscriptionById($subscriptionId) {
146  // must be implemented by sub-classes
147  assert(false);
148  }
149 
155  function deleteSubscriptionsByJournal($journalId) {
156  // must be implemented by sub-classes
157  assert(false);
158  }
159 
165  function deleteSubscriptionsByUserId($userId) {
166  // must be implemented by sub-classes
167  assert(false);
168  }
169 
175  function deleteSubscriptionsByTypeId($subscriptionTypeId) {
176  // must be implemented by sub-classes
177  assert(false);
178  }
179 
184  function &getSubscriptions($rangeInfo = null) {
185  // must be implemented by sub-classes
186  assert(false);
187  }
188 
201  function &getSubscriptionsByJournalId($journalId, $status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, $rangeInfo = null) {
202  // must be implemented by sub-classes
203  assert(false);
204  }
205 
212  function &getSubscriptionsByDateEnd($dateEnd, $journalId, $rangeInfo = null) {
213  // must be implemented by sub-classes
214  assert(false);
215  }
216 
223  function renewSubscription(&$subscription) {
224  // must be implemented by sub-classes
225  assert(false);
226  }
227 
232  function _generateUserNameSearchSQL($search, $searchMatch, $prefix, &$params) {
233  $first_last = $this->concat($prefix.'first_name', '\' \'', $prefix.'last_name');
234  $first_middle_last = $this->concat($prefix.'first_name', '\' \'', $prefix.'middle_name', '\' \'', $prefix.'last_name');
235  $last_comma_first = $this->concat($prefix.'last_name', '\', \'', $prefix.'first_name');
236  $last_comma_first_middle = $this->concat($prefix.'last_name', '\', \'', $prefix.'first_name', '\' \'', $prefix.'middle_name');
237  if ($searchMatch === 'is') {
238  $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(?))";
239  } elseif ($searchMatch === 'contains') {
240  $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(?))";
241  $search = '%' . $search . '%';
242  } else { // $searchMatch === 'startsWith'
243  $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(?))";
244  $search = $search . '%';
245  }
246  $params[] = $params[] = $params[] = $params[] = $params[] = $search;
247  return $searchSql;
248  }
249 
254  function _generateSearchSQL($status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, &$params) {
255 
256  $searchSql = '';
257 
258  if (!empty($search)) switch ($searchField) {
259  case SUBSCRIPTION_USER:
260  $searchSql = $this->_generateUserNameSearchSQL($search, $searchMatch, 'u.', $params);
261  break;
262  case SUBSCRIPTION_MEMBERSHIP:
263  if ($searchMatch === 'is') {
264  $searchSql = ' AND LOWER(s.membership) = LOWER(?)';
265  } elseif ($searchMatch === 'contains') {
266  $searchSql = ' AND LOWER(s.membership) LIKE LOWER(?)';
267  $search = '%' . $search . '%';
268  } else { // $searchMatch === 'startsWith'
269  $searchSql = ' AND LOWER(s.membership) LIKE LOWER(?)';
270  $search = $search . '%';
271  }
272  $params[] = $search;
273  break;
274  case SUBSCRIPTION_REFERENCE_NUMBER:
275  if ($searchMatch === 'is') {
276  $searchSql = ' AND LOWER(s.reference_number) = LOWER(?)';
277  } elseif ($searchMatch === 'contains') {
278  $searchSql = ' AND LOWER(s.reference_number) LIKE LOWER(?)';
279  $search = '%' . $search . '%';
280  } else { // $searchMatch === 'startsWith'
281  $searchSql = ' AND LOWER(s.reference_number) LIKE LOWER(?)';
282  $search = $search . '%';
283  }
284  $params[] = $search;
285  break;
286  case SUBSCRIPTION_NOTES:
287  if ($searchMatch === 'is') {
288  $searchSql = ' AND LOWER(s.notes) = LOWER(?)';
289  } elseif ($searchMatch === 'contains') {
290  $searchSql = ' AND LOWER(s.notes) LIKE LOWER(?)';
291  $search = '%' . $search . '%';
292  } else { // $searchMatch === 'startsWith'
293  $searchSql = ' AND LOWER(s.notes) LIKE LOWER(?)';
294  $search = $search . '%';
295  }
296  $params[] = $search;
297  break;
298  }
299 
300  if (!empty($dateFrom) || !empty($dateTo)) switch($dateField) {
301  case SUBSCRIPTION_DATE_START:
302  if (!empty($dateFrom)) {
303  $searchSql .= ' AND s.date_start >= ' . $this->datetimeToDB($dateFrom);
304  }
305  if (!empty($dateTo)) {
306  $searchSql .= ' AND s.date_start <= ' . $this->datetimeToDB($dateTo);
307  }
308  break;
309  case SUBSCRIPTION_DATE_END:
310  if (!empty($dateFrom)) {
311  $searchSql .= ' AND s.date_end >= ' . $this->datetimeToDB($dateFrom);
312  }
313  if (!empty($dateTo)) {
314  $searchSql .= ' AND s.date_end <= ' . $this->datetimeToDB($dateTo);
315  }
316  break;
317  }
318 
319  if (!empty($status)) {
320  $searchSql .= ' AND s.status = ' . (int) $status;
321  }
322 
323  return $searchSql;
324  }
325 
330  function createObject() {
331  // must be implemented by sub-classes
332  assert(false);
333  }
334 
340  function &_returnSubscriptionFromRow($row) {
341  $subscription = $this->createObject();
342  $subscription->setId($row['subscription_id']);
343  $subscription->setJournalId($row['journal_id']);
344  $subscription->setUserId($row['user_id']);
345  $subscription->setTypeId($row['type_id']);
346  $subscription->setDateStart($this->dateFromDB($row['date_start']));
347  $subscription->setDateEnd($this->dateFromDB($row['date_end']));
348  $subscription->setStatus($row['status']);
349  $subscription->setMembership($row['membership']);
350  $subscription->setReferenceNumber($row['reference_number']);
351  $subscription->setNotes($row['notes']);
352 
353  HookRegistry::call('SubscriptionDAO::_returnSubscriptionFromRow', array(&$subscription, &$row));
354 
355  return $subscription;
356  }
357 
363  function _insertSubscription(&$subscription) {
364  $returner = $this->update(
365  sprintf('INSERT INTO subscriptions
366  (journal_id, user_id, type_id, date_start, date_end, status, membership, reference_number, notes)
367  VALUES
368  (?, ?, ?, %s, %s, ?, ?, ?, ?)',
369  $this->dateToDB($subscription->getDateStart()), $this->datetimeToDB($subscription->getDateEnd())),
370  array(
371  $subscription->getJournalId(),
372  $subscription->getUserId(),
373  $subscription->getTypeId(),
374  $subscription->getStatus(),
375  $subscription->getMembership(),
376  $subscription->getReferenceNumber(),
377  $subscription->getNotes()
378  )
379  );
380 
381  $subscriptionId = $this->getInsertId();
382  $subscription->setId($subscriptionId);
383 
384  return $subscriptionId;
385  }
386 
392  function _updateSubscription(&$subscription) {
393  $returner = $this->update(
394  sprintf('UPDATE subscriptions
395  SET
396  journal_id = ?,
397  user_id = ?,
398  type_id = ?,
399  date_start = %s,
400  date_end = %s,
401  status = ?,
402  membership = ?,
403  reference_number = ?,
404  notes = ?
405  WHERE subscription_id = ?',
406  $this->dateToDB($subscription->getDateStart()), $this->datetimeToDB($subscription->getDateEnd())),
407  array(
408  $subscription->getJournalId(),
409  $subscription->getUserId(),
410  $subscription->getTypeId(),
411  $subscription->getStatus(),
412  $subscription->getMembership(),
413  $subscription->getReferenceNumber(),
414  $subscription->getNotes(),
415  $subscription->getId()
416  )
417  );
418 
419  return $returner;
420  }
421 
428  function _renewSubscription(&$subscription) {
429  if ($subscription->isNonExpiring()) return;
430 
431  $subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO');
432  $subscriptionType =& $subscriptionTypeDao->getSubscriptionType($subscription->getTypeId());
433 
434  $duration = $subscriptionType->getDuration();
435  $dateEnd = strtotime($subscription->getDateEnd());
436 
437  // if the subscription is expired, extend it to today + duration of subscription
438  $time = time();
439  if ($dateEnd < $time ) $dateEnd = $time;
440 
441  $subscription->setDateEnd(mktime(23, 59, 59, date("m", $dateEnd)+$duration, date("d", $dateEnd), date("Y", $dateEnd)));
442  $this->updateSubscription($subscription);
443  }
444 }
445 
446 ?>
_renewSubscription(&$subscription)
& getSubscription($subscriptionId)
static & getDAO($name, $dbconn=null)
_updateSubscription(&$subscription)
& getSubscriptions($rangeInfo=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
& getSubscriptionsByDateEnd($dateEnd, $journalId, $rangeInfo=null)
dateToDB($d)
Definition: DAO.inc.php:303
getSubscriptionJournalId($subscriptionId)
Abstract class for retrieving and modifying subscriptions.
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
deleteSubscriptionsByJournal($journalId)
subscriptionExists($subscriptionId)
concat()
Definition: DAO.inc.php:53
static call($hookName, $args=null)
_insertSubscription(&$subscription)
& _returnSubscriptionFromRow($row)
deleteSubscriptionsByUserId($userId)
subscriptionExistsByUser($subscriptionId, $userId)
renewSubscription(&$subscription)
updateSubscription(&$subscription)
_generateSearchSQL($status=null, $searchField=null, $searchMatch=null, $search=null, $dateField=null, $dateFrom=null, $dateTo=null, &$params)
datetimeToDB($dt)
Definition: DAO.inc.php:293
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
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:323
_generateUserNameSearchSQL($search, $searchMatch, $prefix, &$params)