Open Journal Systems  3.3.0
OpenAccessNotification.inc.php
1 <?php
2 
16 import('lib.pkp.classes.scheduledTask.ScheduledTask');
17 
19 
23  public function getName() {
24  return __('admin.scheduledTask.openAccessNotification');
25  }
26 
33  public function sendNotification ($users, $journal, $issue) {
34  if ($users->getCount() != 0) {
35 
36  import('lib.pkp.classes.mail.MailTemplate');
37  $email = new MailTemplate('OPEN_ACCESS_NOTIFY', $journal->getPrimaryLocale(), $journal, false);
38 
39  $email->setSubject($email->getSubject($journal->getPrimaryLocale()));
40  $email->setReplyTo(null);
41  $email->setFrom($journal->getData('contactEmail'), $journal->getData('contactName'));
42  $email->addRecipient($journal->getData('contactEmail'), $journal->getData('contactName'));
43 
44  $request = Application::get()->getRequest();
45  $paramArray = array(
46  'journalName' => $journal->getLocalizedName(),
47  'journalUrl' => $request->url($journal->getPath()),
48  'editorialContactSignature' => $journal->getData('contactName') . "\n" . $journal->getLocalizedName(),
49  );
50  $email->assignParams($paramArray);
51 
52  $submissions = Services::get('submission')->getInSections($issue->getId());
53  $mimeBoundary = '==boundary_' . md5(microtime());
54 
55  $templateMgr = TemplateManager::getManager();
56  $templateMgr->assign(array(
57  'body' => $email->getBody($journal->getPrimaryLocale()),
58  'templateSignature' => $journal->getData('emailSignature'),
59  'mimeBoundary' => $mimeBoundary,
60  'issue' => $issue,
61  'publishedSubmissions' => $submissions,
62  ));
63 
64  $email->addHeader('MIME-Version', '1.0');
65  $email->setContentType('multipart/alternative; boundary="'.$mimeBoundary.'"');
66  $email->setBody($templateMgr->fetch('subscription/openAccessNotifyEmail.tpl'));
67 
68  while ($user = $users->next()) {
69  $email->addBcc($user->getEmail(), $user->getFullName());
70  }
71  $email->send();
72  }
73  }
74 
80  public function sendNotifications ($journal, $curDate) {
81  // Only send notifications if subscriptions and open access notifications are enabled
82  if ($journal->getData('publishingMode') == PUBLISHING_MODE_SUBSCRIPTION && $journal->getData('enableOpenAccessNotification')) {
83 
84  $curYear = $curDate['year'];
85  $curMonth = $curDate['month'];
86  $curDay = $curDate['day'];
87 
88  // Check if the current date corresponds to the open access date of any issues
89  $issueDao = DAORegistry::getDAO('IssueDAO'); /* @var $issueDao IssueDAO */
90  $issues = $issueDao->getPublishedIssues($journal->getId());
91 
92  while ($issue = $issues->next()) {
93  $accessStatus = $issue->getAccessStatus();
94  $openAccessDate = $issue->getOpenAccessDate();
95 
96  if ($accessStatus == ISSUE_ACCESS_SUBSCRIPTION && !empty($openAccessDate) && strtotime($openAccessDate) == mktime(0,0,0,$curMonth, $curDay, $curYear)) {
97  // Notify all users who have open access notification set for this journal
98  $userSettingsDao = DAORegistry::getDAO('UserSettingsDAO'); /* @var $userSettingsDao UserSettingsDAO */
99  $users = $userSettingsDao->getUsersBySetting('openAccessNotification', true, 'bool', $journal->getId());
100  $this->sendNotification($users, $journal, $issue);
101  }
102  }
103  }
104  }
105 
109  protected function executeActions() {
110  $journalDao = DAORegistry::getDAO('JournalDAO'); /* @var $journalDao JournalDAO */
111  $journals = $journalDao->getAll(true);
112 
113  $todayDate = array(
114  'year' => date('Y'),
115  'month' => date('n'),
116  'day' => date('j')
117  );
118 
119  while ($journal = $journals->next()) {
120  // Send notifications based on current date
121  $this->sendNotifications($journal, $todayDate);
122  }
123 
124  // If it is the first day of a month but previous month had only
125  // 30 days then simulate 31st day for open access dates that end on
126  // that day.
127  $shortMonths = array(2,4,6,9,11);
128 
129  if (($todayDate['day'] == 1) && in_array(($todayDate['month'] - 1), $shortMonths)) {
130 
131  $curDate['day'] = 31;
132  $curDate['month'] = $todayDate['month'] - 1;
133 
134  if ($curDate['month'] == 0) {
135  $curDate['month'] = 12;
136  $curDate['year'] = $todayDate['year'] - 1;
137  } else {
138  $curDate['year'] = $todayDate['year'];
139  }
140 
141  $journals = $journalDao->getAll(true);
142  while ($journal = $journals->next()) {
143  // Send reminders for simulated 31st day of short month
144  $this->sendNotifications($journal, $curDate);
145  }
146  }
147 
148  // If it is the first day of March, simulate 29th and 30th days for February
149  // or just the 30th day in a leap year.
150  if (($todayDate['day'] == 1) && ($todayDate['month'] == 3)) {
151 
152  $curDate['day'] = 30;
153  $curDate['month'] = 2;
154  $curDate['year'] = $todayDate['year'];
155 
156  $journals = $journalDao->getAll(true);
157  while ($journal = $journals->next()) {
158  // Send reminders for simulated 30th day of February
159  $this->sendNotifications($journal, $curDate);
160  }
161 
162  // Check if it's a leap year
163  if (date("L", mktime(0,0,0,0,0,$curDate['year'])) != '1') {
164 
165  $curDate['day'] = 29;
166 
167  $journals = $journalDao->getAll(true);
168  while ($journal = $journals->next()) {
169  // Send reminders for simulated 29th day of February
170  $this->sendNotifications($journal, $curDate);
171  }
172  }
173  }
174  return true;
175  }
176 }
177 
178 
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
OpenAccessNotification
Class to perform automated email notifications when an issue becomes open access.
Definition: OpenAccessNotification.inc.php:18
OpenAccessNotification\executeActions
executeActions()
Definition: OpenAccessNotification.inc.php:109
OpenAccessNotification\sendNotifications
sendNotifications($journal, $curDate)
Definition: OpenAccessNotification.inc.php:80
MailTemplate
Subclass of Mail for mailing a template email.
Definition: MailTemplate.inc.php:21
ScheduledTask
Base class for executing scheduled tasks. All scheduled task classes must extend this class and imple...
Definition: ScheduledTask.inc.php:20
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
OpenAccessNotification\sendNotification
sendNotification($users, $journal, $issue)
Definition: OpenAccessNotification.inc.php:33
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
OpenAccessNotification\getName
getName()
Definition: OpenAccessNotification.inc.php:23
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49