PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/Feedback/API.php

https://github.com/CodeYellowBV/piwik
PHP | 115 lines | 77 code | 18 blank | 20 comment | 10 complexity | 3daeef739f170eb24c229bbedba9f5ee MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Plugins\Feedback;
  10. use Piwik\Common;
  11. use Piwik\Config;
  12. use Piwik\IP;
  13. use Piwik\Mail;
  14. use Piwik\Piwik;
  15. use Piwik\Translate;
  16. use Piwik\Url;
  17. use Piwik\Version;
  18. /**
  19. * API for plugin Feedback
  20. *
  21. * @method static \Piwik\Plugins\Feedback\API getInstance()
  22. */
  23. class API extends \Piwik\Plugin\API
  24. {
  25. /**
  26. * Sends feedback for a specific feature to the Piwik team or alternatively to the email address configured in the
  27. * config: "feedback_email_address".
  28. *
  29. * @param string $featureName The name of a feature you want to give feedback to.
  30. * @param bool|int $like Whether you like the feature or not
  31. * @param string|bool $message A message containing the actual feedback
  32. */
  33. public function sendFeedbackForFeature($featureName, $like, $message = false)
  34. {
  35. Piwik::checkUserIsNotAnonymous();
  36. Piwik::checkUserHasSomeViewAccess();
  37. $featureName = $this->getEnglishTranslationForFeatureName($featureName);
  38. $likeText = 'Yes';
  39. if (empty($like)) {
  40. $likeText = 'No';
  41. }
  42. $body = sprintf("Feature: %s\nLike: %s\n", $featureName, $likeText, $message);
  43. $feedbackMessage = "";
  44. if (!empty($message) && $message != 'undefined') {
  45. $feedbackMessage = sprintf("Feedback:\n%s\n", trim($message));
  46. }
  47. $body .= $feedbackMessage ? $feedbackMessage : " \n";
  48. $subject = sprintf("%s for %s %s",
  49. empty($like) ? "-1" : "+1",
  50. $featureName,
  51. empty($feedbackMessage) ? "" : "(w/ feedback)"
  52. );
  53. $this->sendMail($subject, $body);
  54. }
  55. private function sendMail($subject, $body)
  56. {
  57. $feedbackEmailAddress = Config::getInstance()->General['feedback_email_address'];
  58. $subject = '[ Feedback Feature - Piwik ] ' . $subject;
  59. $body = Common::unsanitizeInputValue($body) . "\n"
  60. . 'Piwik ' . Version::VERSION . "\n"
  61. . 'IP: ' . IP::getIpFromHeader() . "\n"
  62. . 'URL: ' . Url::getReferrer() . "\n";
  63. $mail = new Mail();
  64. $mail->setFrom(Piwik::getCurrentUserEmail());
  65. $mail->addTo($feedbackEmailAddress, 'Piwik Team');
  66. $mail->setSubject($subject);
  67. $mail->setBodyText($body);
  68. @$mail->send();
  69. }
  70. private function findTranslationKeyForFeatureName($featureName)
  71. {
  72. if (empty($GLOBALS['Piwik_translations'])) {
  73. return;
  74. }
  75. foreach ($GLOBALS['Piwik_translations'] as $key => $translations) {
  76. $possibleKey = array_search($featureName, $translations);
  77. if (!empty($possibleKey)) {
  78. return $key . '_' . $possibleKey;
  79. }
  80. }
  81. }
  82. private function getEnglishTranslationForFeatureName($featureName)
  83. {
  84. $loadedLanguage = Translate::getLanguageLoaded();
  85. if ($loadedLanguage == 'en') {
  86. return $featureName;
  87. }
  88. $translationKeyForFeature = $this->findTranslationKeyForFeatureName($featureName);
  89. if (!empty($translationKeyForFeature)) {
  90. Translate::reloadLanguage('en');
  91. $featureName = Piwik::translate($translationKeyForFeature);
  92. Translate::reloadLanguage($loadedLanguage);
  93. return $featureName;
  94. }
  95. return $featureName;
  96. }
  97. }