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 
144  function deleteSubscriptionById($subscriptionId) {
145  // must be implemented by sub-classes
146  assert(false);
147  }
148 
154  function deleteSubscriptionsByJournal($journalId) {
155  // must be implemented by sub-classes
156  assert(false);
157  }
158 
164  function deleteSubscriptionsByUserId($userId) {
165  // must be implemented by sub-classes
166  assert(false);
167  }
168 
174  function deleteSubscriptionsByTypeId($subscriptionTypeId) {
175  // must be implemented by sub-classes
176  assert(false);
177  }
178 
183  function getSubscriptions($rangeInfo = null) {
184  // must be implemented by sub-classes
185  assert(false);
186  }
187 
200  function getSubscriptionsByJournalId($journalId, $status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, $rangeInfo = null) {
201  // must be implemented by sub-classes
202  assert(false);
203  }
204 
211  function getSubscriptionsByDateEnd($dateEnd, $journalId, $rangeInfo = null) {
212  // must be implemented by sub-classes
213  assert(false);
214  }
215 
222  function renewSubscription($subscription) {
223  // must be implemented by sub-classes
224  assert(false);
225  }
226 
231  function _generateUserNameSearchSQL($search, $searchMatch, $prefix, &$params) {
232  $first_last = $this->concat($prefix.'first_name', '\' \'', $prefix.'last_name');
233  $first_middle_last = $this->concat($prefix.'first_name', '\' \'', $prefix.'middle_name', '\' \'', $prefix.'last_name');
234  $last_comma_first = $this->concat($prefix.'last_name', '\', \'', $prefix.'first_name');
235  $last_comma_first_middle = $this->concat($prefix.'last_name', '\', \'', $prefix.'first_name', '\' \'', $prefix.'middle_name');
236  if ($searchMatch === 'is') {
237  $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(?))";
238  } elseif ($searchMatch === 'contains') {
239  $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(?))";
240  $search = '%' . $search . '%';
241  } else { // $searchMatch === 'startsWith'
242  $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(?))";
243  $search = $search . '%';
244  }
245  $params[] = $params[] = $params[] = $params[] = $params[] = $search;
246  return $searchSql;
247  }
248 
253  function _generateSearchSQL($status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, &$params) {
254 
255  $searchSql = '';
256 
257  if (!empty($search)) switch ($searchField) {
258  case SUBSCRIPTION_USER:
259  $searchSql = $this->_generateUserNameSearchSQL($search, $searchMatch, 'u.', $params);
260  break;
261  case SUBSCRIPTION_MEMBERSHIP:
262  if ($searchMatch === 'is') {
263  $searchSql = ' AND LOWER(s.membership) = LOWER(?)';
264  } elseif ($searchMatch === 'contains') {
265  $searchSql = ' AND LOWER(s.membership) LIKE LOWER(?)';
266  $search = '%' . $search . '%';
267  } else { // $searchMatch === 'startsWith'
268  $searchSql = ' AND LOWER(s.membership) LIKE LOWER(?)';
269  $search = $search . '%';
270  }
271  $params[] = $search;
272  break;
273  case SUBSCRIPTION_REFERENCE_NUMBER:
274  if ($searchMatch === 'is') {
275  $searchSql = ' AND LOWER(s.reference_number) = LOWER(?)';
276  } elseif ($searchMatch === 'contains') {
277  $searchSql = ' AND LOWER(s.reference_number) LIKE LOWER(?)';
278  $search = '%' . $search . '%';
279  } else { // $searchMatch === 'startsWith'
280  $searchSql = ' AND LOWER(s.reference_number) LIKE LOWER(?)';
281  $search = $search . '%';
282  }
283  $params[] = $search;
284  break;
285  case SUBSCRIPTION_NOTES:
286  if ($searchMatch === 'is') {
287  $searchSql = ' AND LOWER(s.notes) = LOWER(?)';
288  } elseif ($searchMatch === 'contains') {
289  $searchSql = ' AND LOWER(s.notes) LIKE LOWER(?)';
290  $search = '%' . $search . '%';
291  } else { // $searchMatch === 'startsWith'
292  $searchSql = ' AND LOWER(s.notes) LIKE LOWER(?)';
293  $search = $search . '%';
294  }
295  $params[] = $search;
296  break;
297  }
298 
299  if (!empty($dateFrom) || !empty($dateTo)) switch($dateField) {
300  case SUBSCRIPTION_DATE_START:
301  if (!empty($dateFrom)) {
302  $searchSql .= ' AND s.date_start >= ' . $this->datetimeToDB($dateFrom);
303  }
304  if (!empty($dateTo)) {
305  $searchSql .= ' AND s.date_start <= ' . $this->datetimeToDB($dateTo);
306  }
307  break;
308  case SUBSCRIPTION_DATE_END:
309  if (!empty($dateFrom)) {
310  $searchSql .= ' AND s.date_end >= ' . $this->datetimeToDB($dateFrom);
311  }
312  if (!empty($dateTo)) {
313  $searchSql .= ' AND s.date_end <= ' . $this->datetimeToDB($dateTo);
314  }
315  break;
316  }
317 
318  if (!empty($status)) {
319  $searchSql .= ' AND s.status = ' . (int) $status;
320  }
321 
322  return $searchSql;
323  }
324 
329  function newDataObject() {
330  // must be implemented by sub-classes
331  assert(false);
332  }
333 
339  function _fromRow($row) {
340  $subscription = $this->newDataObject();
341  $subscription->setId($row['subscription_id']);
342  $subscription->setJournalId($row['journal_id']);
343  $subscription->setUserId($row['user_id']);
344  $subscription->setTypeId($row['type_id']);
345  $subscription->setDateStart($this->dateFromDB($row['date_start']));
346  $subscription->setDateEnd($this->dateFromDB($row['date_end']));
347  $subscription->setStatus($row['status']);
348  $subscription->setMembership($row['membership']);
349  $subscription->setReferenceNumber($row['reference_number']);
350  $subscription->setNotes($row['notes']);
351 
352  HookRegistry::call('SubscriptionDAO::_fromRow', array(&$subscription, &$row));
353 
354  return $subscription;
355  }
356 
362  function _insertSubscription($subscription) {
363  $this->update(
364  sprintf('INSERT INTO subscriptions
365  (journal_id, user_id, type_id, date_start, date_end, status, membership, reference_number, notes)
366  VALUES
367  (?, ?, ?, %s, %s, ?, ?, ?, ?)',
368  $this->dateToDB($subscription->getDateStart()), $this->datetimeToDB($subscription->getDateEnd())),
369  array(
370  $subscription->getJournalId(),
371  $subscription->getUserId(),
372  $subscription->getTypeId(),
373  $subscription->getStatus(),
374  $subscription->getMembership(),
375  $subscription->getReferenceNumber(),
376  $subscription->getNotes()
377  )
378  );
379 
380  $subscriptionId = $this->getInsertId();
381  $subscription->setId($subscriptionId);
382 
383  return $subscriptionId;
384  }
385 
391  function _updateSubscription($subscription) {
392  $returner = $this->update(
393  sprintf('UPDATE subscriptions
394  SET
395  journal_id = ?,
396  user_id = ?,
397  type_id = ?,
398  date_start = %s,
399  date_end = %s,
400  status = ?,
401  membership = ?,
402  reference_number = ?,
403  notes = ?
404  WHERE subscription_id = ?',
405  $this->dateToDB($subscription->getDateStart()), $this->datetimeToDB($subscription->getDateEnd())),
406  array(
407  $subscription->getJournalId(),
408  $subscription->getUserId(),
409  $subscription->getTypeId(),
410  $subscription->getStatus(),
411  $subscription->getMembership(),
412  $subscription->getReferenceNumber(),
413  $subscription->getNotes(),
414  $subscription->getId()
415  )
416  );
417 
418  return $returner;
419  }
420 
427  function _renewSubscription($subscription) {
428  if ($subscription->isNonExpiring()) return;
429 
430  $subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO');
431  $subscriptionType = $subscriptionTypeDao->getSubscriptionType($subscription->getTypeId());
432 
433  $duration = $subscriptionType->getDuration();
434  $dateEnd = strtotime($subscription->getDateEnd());
435 
436  // if the subscription is expired, extend it to today + duration of subscription
437  $time = time();
438  if ($dateEnd < $time ) $dateEnd = $time;
439 
440  $subscription->setDateEnd(mktime(23, 59, 59, date("m", $dateEnd)+$duration, date("d", $dateEnd), date("Y", $dateEnd)));
441  $this->updateSubscription($subscription);
442  }
443 }
444 
445 ?>
static & getDAO($name, $dbconn=null)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
updateSubscription($subscription)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
getSubscriptionsByJournalId($journalId, $status=null, $searchField=null, $searchMatch=null, $search=null, $dateField=null, $dateFrom=null, $dateTo=null, $rangeInfo=null)
dateToDB($d)
Definition: DAO.inc.php:303
getSubscriptions($rangeInfo=null)
getSubscriptionJournalId($subscriptionId)
Abstract class for retrieving and modifying subscriptions.
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
insertSubscription($subscription)
getSubscription($subscriptionId)
deleteSubscriptionsByJournal($journalId)
renewSubscription($subscription)
subscriptionExists($subscriptionId)
concat()
Definition: DAO.inc.php:53
getSubscriptionsByDateEnd($dateEnd, $journalId, $rangeInfo=null)
static call($hookName, $args=null)
_renewSubscription($subscription)
deleteSubscriptionsByUserId($userId)
_insertSubscription($subscription)
_updateSubscription($subscription)
subscriptionExistsByUser($subscriptionId, $userId)
_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
deleteSubscriptionsByTypeId($subscriptionTypeId)
subscriptionExistsByUserForJournal($userId, $journalId)
deleteSubscriptionById($subscriptionId)
dateFromDB($d)
Definition: DAO.inc.php:323
_generateUserNameSearchSQL($search, $searchMatch, $prefix, &$params)