Open Journal Systems  3.3.0
EditorialReportNotificationManager.inc.php
1 <?php
15 import('lib.pkp.classes.notification.NotificationManagerDelegate');
16 
19  private $_context;
21  private $_request;
23  private $_params;
25  private $_attachmentFilename;
27  private $_editorialTrends;
29  private $_editorialTrendsTotal;
31  private $_userRolesOverview;
32 
36  public function __construct(int $notificationType) {
37  parent::__construct($notificationType);
38  $this->_request = Application::get()->getRequest();
39  }
40 
47  public function initialize(Context $context, DateTimeInterface $dateStart, DateTimeInterface $dateEnd) : void
48  {
49  $this->_context = $context;
50  $locale = $this->_context->getPrimaryLocale();
51  $dateStart = $dateStart;
52  $dateEnd = $dateEnd;
53 
54  \AppLocale::requireComponents([LOCALE_COMPONENT_PKP_MANAGER, LOCALE_COMPONENT_APP_MANAGER, LOCALE_COMPONENT_PKP_USER], $locale);
55 
56  $dispatcher = Application::get()->getDispatcher();
57 
58  $this->_editorialTrends = Services::get('editorialStats')->getOverview([
59  'contextIds' => [$this->_context->getId()],
60  'dateStart' => $dateStart->format('Y-m-d'),
61  'dateEnd' => $dateEnd->format('Y-m-d'),
62  ]);
63  $this->_editorialTrendsTotal = Services::get('editorialStats')->getOverview([
64  'contextIds' => [$this->_context->getId()]
65  ]);
66 
67  foreach ($this->_editorialTrends as $stat) {
68  switch ($stat['key']) {
69  case 'submissionsReceived':
70  $newSubmissions = $stat['value'];
71  break;
72  case 'submissionsDeclined':
73  $declinedSubmissions = $stat['value'];
74  break;
75  case 'submissionsAccepted':
76  $acceptedSubmissions = $stat['value'];
77  break;
78  }
79  }
80 
81  $this->_params = [
82  'newSubmissions' => $newSubmissions,
83  'declinedSubmissions' => $declinedSubmissions,
84  'acceptedSubmissions' => $acceptedSubmissions,
85  'totalSubmissions' => Services::get('editorialStats')->countSubmissionsReceived(['contextIds' => [$this->_context->getId()]]),
86  'month' => $this->_getLocalizedMonthName($dateStart, $locale),
87  'year' => $dateStart->format('Y'),
88  'editorialStatsLink' => $dispatcher->url($this->_request, ROUTE_PAGE, $this->_context->getPath(), 'stats', 'editorial'),
89  'publicationStatsLink' => $dispatcher->url($this->_request, ROUTE_PAGE, $this->_context->getPath(), 'stats', 'publications')
90  ];
91 
92  $this->_userRolesOverview = Services::get('user')->getRolesOverview(['contextId' => $this->_context->getId()]);
93 
94  // Create the CSV file attachment
95  // Active submissions by stage
96  $file = new SplFileObject(tempnam(sys_get_temp_dir(), 'tmp'), 'wb');
97  // Adds BOM (byte order mark) to enforce the UTF-8 format
98  try {
99  $file->fwrite("\xEF\xBB\xBF");
100  $file->fputcsv([
101  __('stats.submissionsActive', [], $locale),
102  __('stats.total', [], $locale)
103  ]);
104  foreach (Application::get()->getApplicationStages() as $stageId) {
105  $file->fputcsv([
106  __(Application::get()->getWorkflowStageName($stageId), [], $locale),
107  Services::get('editorialStats')->countActiveByStages($stageId)
108  ]);
109  }
110 
111  $file->fputcsv([]);
112 
113  // Editorial trends
114  $file->fputcsv([
115  __('stats.trends', [], $locale),
116  $this->_getLocalizedMonthName($dateStart, $locale) . __('common.commaListSeparator', [], $locale) . $dateStart->format('Y'),
117  __('stats.total', [], $locale)
118  ]);
119  foreach ($this->_editorialTrends as $i => $stat) {
120  $file->fputcsv([
121  __($stat['name'], [], $locale),
122  $stat['value'],
123  $this->_editorialTrendsTotal[$i]['value']
124  ]);
125  }
126 
127  $file->fputcsv([]);
128 
129  // Count of users by role
130  $file->fputcsv([
131  __('manager.users', [], $locale),
132  __('stats.total', [], $locale)
133  ]);
134  foreach ($this->_userRolesOverview as $role) {
135  $file->fputcsv([
136  __($role['name'], [], $locale),
137  $role['value']
138  ]);
139  }
140 
141  $this->_attachmentFilename = $file->getRealPath();
142  }
143  finally {
144  $file = null;
145  }
146  }
147 
154  public function _getLocalizedMonthName(\DateTimeInterface $date, ?string $locale = null): string
155  {
156  static $cache = [];
157  $locale ?? $locale = \AppLocale::getLocale();
158  $formatter = $cache[$locale] ?? $cache[$locale] = \IntlDateFormatter::create($locale, null, null, null, null, 'MMMM');
159  return $formatter->format($date);
160  }
161 
165  public function getNotificationMessage($request, $notification) : string
166  {
167  return __('notification.type.editorialReport', [], $this->_context->getPrimaryLocale());
168  }
169 
173  public function getNotificationContents($request, $notification) : EmailTemplate
174  {
175  return Services::get('emailTemplate')->getByKey($notification->getContextId(), 'STATISTICS_REPORT_NOTIFICATION');
176  }
177 
181  public function getNotificationUrl($request, $notification) {
183  $context = $application->getContextDAO()->getById($notification->getContextId());
184  return $application->getDispatcher()->url($this->_request, ROUTE_PAGE, $context->getPath(), 'stats', 'editorial');
185  }
186 
190  public function getIconClass($notification) : string
191  {
192  return 'notifyIconInfo';
193  }
194 
198  public function getStyleClass($notification) : string
199  {
200  return NOTIFICATION_STYLE_CLASS_INFORMATION;
201  }
202 
208  public function notify(User $user) : PKPNotification
209  {
210  return parent::createNotification(
211  $this->_request,
212  $user->getId(),
213  NOTIFICATION_TYPE_EDITORIAL_REPORT,
214  $this->_context->getId(),
215  null,
216  null,
217  NOTIFICATION_LEVEL_TASK,
218  ['contents' => __('notification.type.editorialReport.contents', [], $this->_context->getPrimaryLocale())],
219  false,
220  function ($mail) use ($user) {
221  return $this->_setupMessage($mail, $user);
222  }
223  );
224  }
225 
229  protected function getMailTemplate($emailKey = null) : MailTemplate
230  {
231  import('lib.pkp.classes.mail.MailTemplate');
232  $mail = new MailTemplate('STATISTICS_REPORT_NOTIFICATION', $this->_context->getPrimaryLocale(), $this->_context, false);
233  return $mail;
234  }
235 
242  private function _setupMessage(Mail $mail, User $user) : Mail
243  {
244  $mail->assignParams($this->_getMessageParams($user));
245  if ($this->_getMessageAttachment()) {
246  $mail->addAttachment($this->_getMessageAttachment(), 'editorial-report.csv');
247  }
248  return $mail;
249  }
250 
256  private function _getMessageParams(User $user) : array
257  {
258  return $this->_params + ['name' => $user->getLocalizedGivenName($this->_context->getPrimaryLocale())];
259  }
260 
265  private function _getMessageAttachment() : string
266  {
267  return $this->_attachmentFilename;
268  }
269 }
EditorialReportNotificationManager\getNotificationMessage
getNotificationMessage($request, $notification)
Definition: EditorialReportNotificationManager.inc.php:186
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
EditorialReportNotificationManager\initialize
initialize(Context $context, DateTimeInterface $dateStart, DateTimeInterface $dateEnd)
Definition: EditorialReportNotificationManager.inc.php:68
$application
$application
Definition: index.php:65
EditorialReportNotificationManager\getStyleClass
getStyleClass($notification)
Definition: EditorialReportNotificationManager.inc.php:219
Context
Basic class describing a context.
Definition: Context.inc.php:23
NotificationManagerDelegate
Abstract class to support notification manager delegates that provide default implementation to the i...
Definition: NotificationManagerDelegate.inc.php:21
Context\getPath
getPath()
Definition: Context.inc.php:103
EditorialReportNotificationManager\getIconClass
getIconClass($notification)
Definition: EditorialReportNotificationManager.inc.php:211
Mail\addAttachment
addAttachment($filePath, $fileName='', $contentType='', $contentDisposition='attachment')
Definition: Mail.inc.php:261
EditorialReportNotificationManager\getNotificationContents
getNotificationContents($request, $notification)
Definition: EditorialReportNotificationManager.inc.php:194
MailTemplate
Subclass of Mail for mailing a template email.
Definition: MailTemplate.inc.php:21
EmailTemplate
Describes basic email template properties.
Definition: EmailTemplate.inc.php:17
EditorialReportNotificationManager\_getLocalizedMonthName
_getLocalizedMonthName(\DateTimeInterface $date, ?string $locale=null)
Definition: EditorialReportNotificationManager.inc.php:175
Mail
Class defining basic operations for handling and sending emails.
Definition: Mail.inc.php:23
EditorialReportNotificationManager
Editorial report notification manager.
Definition: EditorialReportNotificationManager.inc.php:17
EditorialReportNotificationManager\__construct
__construct(int $notificationType)
Definition: EditorialReportNotificationManager.inc.php:57
Context\getPrimaryLocale
getPrimaryLocale()
Definition: Context.inc.php:135
EditorialReportNotificationManager\getMailTemplate
getMailTemplate($emailKey=null)
Definition: EditorialReportNotificationManager.inc.php:250
EditorialReportNotificationManager\getNotificationUrl
getNotificationUrl($request, $notification)
Definition: EditorialReportNotificationManager.inc.php:202
PKPNotification
Definition: PKPNotification.inc.php:87
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
AppLocale\getLocale
static getLocale()
Definition: env1/MockAppLocale.inc.php:40
User
Basic class describing users existing in the system.
Definition: User.inc.php:23
EditorialReportNotificationManager\notify
notify(User $user)
Definition: EditorialReportNotificationManager.inc.php:229
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49