/** * View entries * * @param int $uid * @return string */ protected function _view($uid = 0) { // Build the final HTML $view = $this->view('default', 'stats'); // Get pub stats for each publication $pubLog = new \Components\Publications\Tables\Log($this->_database); $view->pubstats = $this->_stats ? $this->_stats : $pubLog->getAuthorStats($uid, 0, false); // Get date of first log $view->firstlog = $pubLog->getFirstLogDate(); $view->totals = $pubLog->getTotals($uid); // Output HTML $view->option = $this->_option; $view->database = $this->_database; $view->uid = $uid; $view->pubconfig = Component::params('com_publications'); if ($this->getError()) { $view->setError($this->getError()); } return $view->loadTemplate(); }
/** * Send emails to authors with the monthly stats * * @param object $job \Components\Cron\Models\Job * @return boolean */ public function sendAuthorStats(\Components\Cron\Models\Job $job) { $database = App::get('db'); $pconfig = Component::params('com_publications'); // Get some params $limit = $pconfig->get('limitStats', 5); $image = $pconfig->get('email_image', ''); Lang::load('com_publications', PATH_CORE) || Lang::load('com_publications', PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'site'); // Is logging enabled? if (is_file(PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'tables' . DS . 'logs.php')) { require_once PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'tables' . DS . 'logs.php'; } else { $this->setError('Publication logs not present on this hub, cannot email stats to authors'); return false; } // Helpers require_once PATH_CORE . DS . 'components' . DS . 'com_members' . DS . 'helpers' . DS . 'imghandler.php'; require_once PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'helpers' . DS . 'html.php'; // Get all registered authors who subscribed to email $query = "SELECT A.user_id, P.picture "; $query .= " FROM #__publication_authors as A "; $query .= " JOIN #__xprofiles as P ON A.user_id = P.uidNumber "; $query .= " WHERE P.mailPreferenceOption != 0 "; // either 1 (set to YES) or -1 (never set) $query .= " AND A.user_id > 0 AND A.status=1 "; // If we need to restrict to selected authors $params = $job->get('params'); if (is_object($params) && $params->get('userids')) { $apu = explode(',', $params->get('userids')); $apu = array_map('trim', $apu); $query .= " AND A.user_id IN ("; $tquery = ''; foreach ($apu as $a) { $tquery .= "'" . $a . "',"; } $tquery = substr($tquery, 0, strlen($tquery) - 1); $query .= $tquery . ") "; } $query .= " GROUP BY A.user_id "; $database->setQuery($query); if (!($authors = $database->loadObjectList())) { return true; } // Set email config $from = array('name' => Config::get('fromname') . ' ' . Lang::txt('Publications'), 'email' => Config::get('mailfrom'), 'multipart' => md5(date('U'))); $subject = Lang::txt('Monthly Publication Usage Report'); $i = 0; foreach ($authors as $author) { // Get the user's account $user = User::getInstance($author->user_id); if (!$user->get('id')) { // Skip if not registered continue; } // Get pub stats for each author $pubLog = new \Components\Publications\Tables\Log($database); $pubstats = $pubLog->getAuthorStats($author->user_id); if (!$pubstats || !count($pubstats)) { // Nothing to send continue; } // Plain text $eview = new \Hubzero\Plugin\View(array('folder' => 'cron', 'element' => 'publications', 'name' => 'emails', 'layout' => 'stats_plain')); $eview->option = 'com_publications'; $eview->controller = 'publications'; $eview->user = $user; $eview->pubstats = $pubstats; $eview->limit = $limit; $eview->image = $image; $eview->config = $pconfig; $eview->totals = $pubLog->getTotals($author->user_id, 'author'); $plain = $eview->loadTemplate(); $plain = str_replace("\n", "\r\n", $plain); // HTML $eview->setLayout('stats_html'); $html = $eview->loadTemplate(); $html = str_replace("\n", "\r\n", $html); // Build message $message = new \Hubzero\Mail\Message(); $message->setSubject($subject)->addFrom($from['email'], $from['name'])->addTo($user->get('email'), $user->get('name'))->addHeader('X-Component', 'com_publications')->addHeader('X-Component-Object', 'publications'); $message->addPart($plain, 'text/plain'); $message->addPart($html, 'text/html'); // Send mail if (!$message->send()) { $this->setError(Lang::txt('PLG_CRON_PUBLICATIONS_ERROR_FAILED_TO_MAIL', $user->get('email'))); } $mailed[] = $user->get('email'); } return true; }