Skip to content

Commit

Permalink
fix #3896
Browse files Browse the repository at this point in the history
  • Loading branch information
julienmoumne committed Apr 23, 2013
1 parent 84dfea2 commit e2a02e8
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 34 deletions.
4 changes: 2 additions & 2 deletions core/ReportRenderer.php
Expand Up @@ -100,12 +100,12 @@ abstract public function getRenderedReport();
/**
* Generate the first page.
*
* @param string $websiteName
* @param string $reportTitle
* @param string $prettyDate formatted date
* @param string $description
* @param array $reportMetadata metadata for all reports
*/
abstract public function renderFrontPage($websiteName, $prettyDate, $description, $reportMetadata);
abstract public function renderFrontPage($reportTitle, $prettyDate, $description, $reportMetadata);

/**
* Render the provided report.
Expand Down
5 changes: 3 additions & 2 deletions core/ReportRenderer/Html.php
Expand Up @@ -82,12 +82,13 @@ private function epilogue()
$this->rendering .= $smarty->fetch(self::prefixTemplatePath("html_report_footer.tpl"));
}

public function renderFrontPage($websiteName, $prettyDate, $description, $reportMetadata)
public function renderFrontPage($reportTitle, $prettyDate, $description, $reportMetadata)
{
$smarty = new Piwik_Smarty();
$this->assignCommonParameters($smarty);

$smarty->assign("websiteName", $websiteName);
// todo rename 'websiteName' to 'reportTitle' once branch twig is merged
$smarty->assign("websiteName", $reportTitle);
$smarty->assign("prettyDate", $prettyDate);
$smarty->assign("description", $description);
$smarty->assign("reportMetadata", $reportMetadata);
Expand Down
8 changes: 4 additions & 4 deletions core/ReportRenderer/Pdf.php
Expand Up @@ -142,14 +142,14 @@ public function getRenderedReport()
return $this->TCPDF->Output(null, 'S');
}

public function renderFrontPage($websiteName, $prettyDate, $description, $reportMetadata)
public function renderFrontPage($reportTitle, $prettyDate, $description, $reportMetadata)
{
$websiteTitle = $this->formatText($websiteName);
$reportTitle = $this->formatText($reportTitle);
$dateRange = $this->formatText(Piwik_Translate('General_DateRange') . " " . $prettyDate);

//Setup Footer font and data
$this->TCPDF->SetFooterFont(array($this->reportFont, $this->reportFontStyle, $this->reportSimpleFontSize));
$this->TCPDF->SetFooterContent($websiteTitle . " | " . $dateRange . " | ");
$this->TCPDF->SetFooterContent($reportTitle . " | " . $dateRange . " | ");

$this->TCPDF->setPrintHeader(false);
// $this->SetMargins($left = , $top, $right=-1, $keepmargins=true)
Expand All @@ -163,7 +163,7 @@ public function renderFrontPage($websiteName, $prettyDate, $description, $report

$this->TCPDF->SetFont($this->reportFont, '', $this->reportHeaderFontSize + 5);
$this->TCPDF->SetTextColor($this->headerTextColor[0], $this->headerTextColor[1], $this->headerTextColor[2]);
$this->TCPDF->Cell(40, 210, $websiteTitle);
$this->TCPDF->Cell(40, 210, $reportTitle);
$this->TCPDF->Ln(8 * 4);

$this->TCPDF->SetFont($this->reportFont, '', $this->reportHeaderFontSize);
Expand Down
10 changes: 5 additions & 5 deletions plugins/MobileMessaging/MobileMessaging.php
Expand Up @@ -242,22 +242,22 @@ function sendReport($notification)
$notificationInfo = $notification->getNotificationInfo();
$report = $notificationInfo[Piwik_PDFReports_API::REPORT_KEY];
$contents = $notificationInfo[Piwik_PDFReports_API::REPORT_CONTENT_KEY];
$reportSubject = $notificationInfo[Piwik_PDFReports_API::REPORT_SUBJECT_KEY];

$parameters = $report['parameters'];
$phoneNumbers = $parameters[self::PHONE_NUMBERS_PARAMETER];

if (in_array('MultiSites_getAll', $report['reports'])) {
$from = Piwik_Translate('General_Reports');
} else {
$from = $notificationInfo[Piwik_PDFReports_API::WEBSITE_NAME_KEY];
// 'All Websites' is one character above the limit, use 'Reports' instead
if ($reportSubject == Piwik_Translate('General_MultiSitesSummary')) {
$reportSubject = Piwik_Translate('General_Reports');
}

$mobileMessagingAPI = Piwik_MobileMessaging_API::getInstance();
foreach ($phoneNumbers as $phoneNumber) {
$mobileMessagingAPI->sendSMS(
$contents,
$phoneNumber,
$from
$reportSubject
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/MobileMessaging/ReportRenderer/Exception.php
Expand Up @@ -59,7 +59,7 @@ public function getRenderedReport()
return $this->rendering;
}

public function renderFrontPage($websiteName, $prettyDate, $description, $reportMetadata)
public function renderFrontPage($reportTitle, $prettyDate, $description, $reportMetadata)
{
// nothing to do
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/MobileMessaging/ReportRenderer/Sms.php
Expand Up @@ -47,7 +47,7 @@ public function getRenderedReport()
return $this->rendering;
}

public function renderFrontPage($websiteName, $prettyDate, $description, $reportMetadata)
public function renderFrontPage($reportTitle, $prettyDate, $description, $reportMetadata)
{
// nothing to do
}
Expand Down
42 changes: 27 additions & 15 deletions plugins/PDFReports/API.php
Expand Up @@ -46,7 +46,8 @@ class Piwik_PDFReports_API
const REPORT_CONTENT_KEY = 'contents';
const FILENAME_KEY = 'filename';
const PRETTY_DATE_KEY = 'prettyDate';
const WEBSITE_NAME_KEY = 'websiteName';
const REPORT_SUBJECT_KEY = 'reportSubject';
const REPORT_TITLE_KEY = 'reportTitle';
const ADDITIONAL_FILES_KEY = 'additionalFiles';

const REPORT_TRUNCATE = 23;
Expand Down Expand Up @@ -405,16 +406,10 @@ public function generateReport($idReport, $date, $language = false, $outputType
// render report
$description = str_replace(array("\r", "\n"), ' ', $report['description']);

// if the only report is "All websites", we don't display the site name
$websiteName = Piwik_Translate('General_Website') . " " . Piwik_Site::getNameFor($idSite);
if (count($report['reports']) == 1
&& $report['reports'][0] == 'MultiSites_getAll'
) {
$websiteName = Piwik_Translate('General_MultiSitesSummary');
}
$reportTitle = "$websiteName - $prettyDate - $description";
list($reportSubject, $reportTitle) = self::getReportSubjectAndReportTitle(Piwik_Site::getNameFor($idSite), $report['reports']);
$filename = "$reportTitle - $prettyDate - $description";

$reportRenderer->renderFrontPage($websiteName, $prettyDate, $description, $reportMetadata);
$reportRenderer->renderFrontPage($reportTitle, $prettyDate, $description, $reportMetadata);
array_walk($processedReports, array($reportRenderer, 'renderReport'));

switch ($outputType) {
Expand Down Expand Up @@ -447,14 +442,15 @@ public function generateReport($idReport, $date, $language = false, $outputType
return array(
$outputFilename,
$prettyDate,
$websiteName,
$reportSubject,
$reportTitle,
$additionalFiles,
);
break;

case self::OUTPUT_INLINE:

$reportRenderer->sendToBrowserInline($reportTitle);
$reportRenderer->sendToBrowserInline($filename);
break;

case self::OUTPUT_RETURN:
Expand All @@ -464,7 +460,7 @@ public function generateReport($idReport, $date, $language = false, $outputType

default:
case self::OUTPUT_DOWNLOAD:
$reportRenderer->sendToBrowserDownload($reportTitle);
$reportRenderer->sendToBrowserDownload($filename);
break;
}
}
Expand All @@ -491,7 +487,7 @@ public function sendReport($idReport, $period = false, $date = false)
$language = Piwik_LanguagesManager_API::getInstance()->getLanguageForUser($report['login']);

// generate report
list($outputFilename, $prettyDate, $websiteName, $additionalFiles) =
list($outputFilename, $prettyDate, $reportSubject, $reportTitle, $additionalFiles) =
$this->generateReport(
$idReport,
$date,
Expand Down Expand Up @@ -519,7 +515,8 @@ public function sendReport($idReport, $period = false, $date = false)
self::REPORT_CONTENT_KEY => $contents,
self::FILENAME_KEY => $filename,
self::PRETTY_DATE_KEY => $prettyDate,
self::WEBSITE_NAME_KEY => $websiteName,
self::REPORT_SUBJECT_KEY => $reportSubject,
self::REPORT_TITLE_KEY => $reportTitle,
self::ADDITIONAL_FILES_KEY => $additionalFiles,
)
);
Expand All @@ -536,6 +533,21 @@ public function sendReport($idReport, $period = false, $date = false)
}
}

private static function getReportSubjectAndReportTitle($websiteName, $reports)
{
// if the only report is "All websites", we don't display the site name
$reportTitle = Piwik_Translate('General_Website') . " " . $websiteName;
$reportSubject = $websiteName;
if (count($reports) == 1
&& $reports[0] == 'MultiSites_getAll'
) {
$reportSubject = Piwik_Translate('General_MultiSitesSummary');
$reportTitle = $reportSubject;
}

return array($reportSubject, $reportTitle);
}

private static function validateReportParameters($reportType, $parameters)
{
// get list of valid parameters
Expand Down
8 changes: 4 additions & 4 deletions plugins/PDFReports/PDFReports.php
Expand Up @@ -304,15 +304,15 @@ function sendReport($notification)
if (self::manageEvent($notification)) {
$notificationInfo = $notification->getNotificationInfo();
$report = $notificationInfo[Piwik_PDFReports_API::REPORT_KEY];
$websiteName = $notificationInfo[Piwik_PDFReports_API::WEBSITE_NAME_KEY];
$reportTitle = $notificationInfo[Piwik_PDFReports_API::REPORT_TITLE_KEY];
$prettyDate = $notificationInfo[Piwik_PDFReports_API::PRETTY_DATE_KEY];
$contents = $notificationInfo[Piwik_PDFReports_API::REPORT_CONTENT_KEY];
$filename = $notificationInfo[Piwik_PDFReports_API::FILENAME_KEY];
$additionalFiles = $notificationInfo[Piwik_PDFReports_API::ADDITIONAL_FILES_KEY];

$periods = self::getPeriodToFrequency();
$message = Piwik_Translate('PDFReports_EmailHello');
$subject = Piwik_Translate('General_Report') . ' ' . $websiteName . " - " . $prettyDate;
$subject = Piwik_Translate('General_Report') . ' ' . $reportTitle . " - " . $prettyDate;

$mail = new Piwik_Mail();
$mail->setSubject($subject);
Expand All @@ -328,13 +328,13 @@ function sendReport($notification)

// Needed when using images as attachment with cid
$mail->setType(Zend_Mime::MULTIPART_RELATED);
$message .= "<br/>" . Piwik_Translate('PDFReports_PleaseFindBelow', array($periods[$report['period']], $websiteName));
$message .= "<br/>" . Piwik_Translate('PDFReports_PleaseFindBelow', array($periods[$report['period']], $reportTitle));
$mail->setBodyHtml($message . "<br/><br/>" . $contents);
break;

default:
case 'pdf':
$message .= "\n" . Piwik_Translate('PDFReports_PleaseFindAttachedFile', array($periods[$report['period']], $websiteName));
$message .= "\n" . Piwik_Translate('PDFReports_PleaseFindAttachedFile', array($periods[$report['period']], $reportTitle));
$mail->setBodyText($message);
$mail->createAttachment(
$contents,
Expand Down
49 changes: 49 additions & 0 deletions tests/PHPUnit/Plugins/MobileMessagingTest.php
Expand Up @@ -211,4 +211,53 @@ public function testPhoneNumberIsSanitized()
$mobileMessagingAPI->addPhoneNumber(' 6 76 93 26 47');
$this->assertEquals('676932647', key($mobileMessagingAPI->getPhoneNumbers()));
}

/**
* Dataprovider for testSendReport
*/
public function getSendReportTestCases()
{
return array(
array('reportContent', '0101010101', 'Piwik.org', 'reportContent', '0101010101', 'Piwik.org'),
array('reportContent', '0101010101', 'General_Reports', 'reportContent', '0101010101', 'General_MultiSitesSummary'),
);
}

/**
* @group Plugins
* @group MobileMessaging
* @dataProvider getSendReportTestCases
*/
public function testSendReport($expectedReportContent, $expectedPhoneNumber, $expectedFrom, $reportContent, $phoneNumber, $reportSubject)
{
$eventNotification = new Piwik_Event_Notification(
$this,
null,
array(
Piwik_PDFReports_API::REPORT_CONTENT_KEY => $reportContent,
Piwik_PDFReports_API::REPORT_SUBJECT_KEY => $reportSubject,
Piwik_PDFReports_API::REPORT_TYPE_INFO_KEY => Piwik_MobileMessaging::MOBILE_TYPE,
Piwik_PDFReports_API::REPORT_KEY => array(
'parameters' => array(Piwik_MobileMessaging::PHONE_NUMBERS_PARAMETER => array($phoneNumber)),
),
)
);

$stubbedMobileMessagingAPI = $this->getMock('Piwik_MobileMessaging_API');
$stubbedMobileMessagingAPI->expects($this->once())->method('sendSMS')->with(
$this->equalTo($expectedReportContent, 0),
$this->equalTo($expectedPhoneNumber, 1),
$this->equalTo($expectedFrom, 2)
);

$stubbedMobileMessagingAPIClass = new ReflectionProperty('Piwik_MobileMessaging_API', 'instance');
$stubbedMobileMessagingAPIClass->setAccessible(true);
$stubbedMobileMessagingAPIClass->setValue($stubbedMobileMessagingAPI);

$mobileMessaging = new Piwik_MobileMessaging();
$mobileMessaging->sendReport($eventNotification);

// restore Piwik_MobileMessaging_API
$stubbedMobileMessagingAPIClass->setValue(null);
}
}
29 changes: 29 additions & 0 deletions tests/PHPUnit/Plugins/PDFReportsTest.php
Expand Up @@ -388,6 +388,35 @@ public function testGetScheduledTasks()
$stubbedPDFReportsAPIClass->setValue(null);
}

/**
* Dataprovider for testGetReportSubjectAndReportTitle
*/
public function getGetReportSubjectAndReportTitleTestCases()
{
return array(
array('Piwik.org', 'General_Website Piwik.org', 'Piwik.org', array('UserSettings_getBrowserType')),
array('Piwik.org', 'General_Website Piwik.org', 'Piwik.org', array('MultiSites_getAll', 'UserSettings_getBrowserType')),
array('General_MultiSitesSummary', 'General_MultiSitesSummary', 'Piwik.org', array('MultiSites_getAll')),
);
}

/**
* @group Plugins
* @group PDFReports
* @dataProvider getGetReportSubjectAndReportTitleTestCases
*/
public function testGetReportSubjectAndReportTitle($expectedReportSubject, $expectedReportTitle, $websiteName, $reports)
{
$getReportSubjectAndReportTitle = new ReflectionMethod(
'Piwik_PDFReports_API', 'getReportSubjectAndReportTitle'
);
$getReportSubjectAndReportTitle->setAccessible(true);

list($reportSubject, $reportTitle) = $getReportSubjectAndReportTitle->invoke(new Piwik_PDFReports_API(), $websiteName, $reports);
$this->assertEquals($expectedReportSubject, $reportSubject);
$this->assertEquals($expectedReportTitle, $reportTitle);
}

private function assertReportsEqual($report, $data)
{
foreach ($data as $key => $value) {
Expand Down

0 comments on commit e2a02e8

Please sign in to comment.