| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Plugins\CustomAlerts; |
|
|
| use Exception; |
| use Piwik\Common; |
| use Piwik\Piwik; |
| use Piwik\Site; |
|
|
| |
| |
| |
| |
| |
| |
| class API extends \Piwik\Plugin\API |
| { |
| private $validator; |
| |
| |
| |
| private $processor; |
|
|
| public function __construct(Processor $processor, Validator $validator) |
| { |
| $this->validator = $validator; |
| $this->processor = $processor; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function getValuesForAlertInPast($idAlert, $subPeriodN) |
| { |
| $alert = $this->getAlert($idAlert); |
|
|
| $values = []; |
| foreach ($alert['id_sites'] as $idSite) { |
| $values[] = array( |
| 'idSite' => (int)$idSite, |
| 'value' => $this->processor->getValueForAlertInPast($alert, $idSite, (int)$subPeriodN) |
| ); |
| } |
|
|
| return $values; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function getAlert($idAlert) |
| { |
| $alert = $this->getModel()->getAlert($idAlert); |
|
|
| if (empty($alert)) { |
| throw new Exception(Piwik::translate('CustomAlerts_AlertDoesNotExist', $idAlert)); |
| } |
|
|
| $this->validator->checkUserHasPermissionForAlert($alert); |
|
|
| return $alert; |
| } |
|
|
| private function getModel() |
| { |
| return new Model(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function getAlerts($idSites, $ifSuperUserReturnAllAlerts = false) |
| { |
| $idSites = Site::getIdSitesFromIdSitesString($idSites); |
|
|
| if (empty($idSites)) { |
| return []; |
| } |
|
|
| Piwik::checkUserHasViewAccess($idSites); |
|
|
| if (Piwik::hasUserSuperUserAccess() && $ifSuperUserReturnAllAlerts) { |
| $login = false; |
| } else { |
| $login = Piwik::getCurrentUserLogin(); |
| } |
|
|
| $alerts = $this->getModel()->getAlerts($idSites, $login); |
|
|
| return $alerts; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function addAlert($name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition = false, $reportValue = false, array $reportMediums = [], string $slackChannelID = '', string $msTeamsWebhookUrl = '', string $description = '') |
| { |
| $idSites = Site::getIdSitesFromIdSitesString($idSites); |
|
|
| $this->checkAlert($idSites, $name, $description, $period, $emailMe, $additionalEmails, $phoneNumbers, $slackChannelID, $msTeamsWebhookUrl, $metricCondition, $metric, $comparedTo, $reportCondition, $reportUniqueId, $reportMediums); |
|
|
| $name = Common::unsanitizeInputValue($name); |
| $description = Common::unsanitizeInputValue($description); |
| $login = Piwik::getCurrentUserLogin(); |
|
|
| if (empty($reportCondition) || empty($reportValue)) { |
| $reportCondition = null; |
| $reportValue = null; |
| } |
|
|
| $metricValue = Common::forceDotAsSeparatorForDecimalPoint((float)$metricValue); |
|
|
| return $this->getModel()->createAlert($name, $idSites, $login, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition, $reportValue, $reportMediums, $slackChannelID, $msTeamsWebhookUrl, $description); |
| } |
|
|
| private function filterAdditionalEmails($additionalEmails) |
| { |
| if (empty($additionalEmails)) { |
| return array(); |
| } |
|
|
| foreach ($additionalEmails as &$email) { |
| $email = trim($email); |
| if (empty($email)) { |
| $email = false; |
| } |
| } |
|
|
| return array_filter($additionalEmails); |
| } |
|
|
| private function filterPhoneNumbers($phoneNumbers) |
| { |
| $availablePhoneNumbers = (new \Piwik\Plugins\MobileMessaging\Model())->getActivatedPhoneNumbers(Piwik::getCurrentUserLogin()); |
|
|
| foreach ($phoneNumbers as $key => &$phoneNumber) { |
| $phoneNumber = trim($phoneNumber); |
|
|
| if (!in_array($phoneNumber, $availablePhoneNumbers)) { |
| unset($phoneNumbers[$key]); |
| } |
| } |
|
|
| return array_values($phoneNumbers); |
| } |
|
|
| private function checkAlert($idSites, $name, $description, $period, &$emailMe, &$additionalEmails, &$phoneNumbers, &$slackChannelID, &$msTeamsWebhookUrl, $metricCondition, $metricValue, $comparedTo, $reportCondition, $reportUniqueId, $reportMediums) |
| { |
| Piwik::checkUserHasViewAccess($idSites); |
| $additionalEmails = in_array('email', $reportMediums) ? $this->filterAdditionalEmails($additionalEmails) : []; |
| $phoneNumbers = in_array('mobile', $reportMediums) ? $this->filterPhoneNumbers($phoneNumbers) : []; |
| $emailMe = in_array('email', $reportMediums) && $emailMe; |
| $slackChannelID = in_array('slack', $reportMediums) ? $slackChannelID : ''; |
| $msTeamsWebhookUrl = in_array('teams', $reportMediums) ? $msTeamsWebhookUrl : ''; |
|
|
| $this->validator->checkName($name); |
| $this->validator->checkDescription($description); |
| $this->validator->checkPeriod($period); |
| $this->validator->checkComparedTo($period, $comparedTo); |
| $this->validator->checkMetricCondition($metricCondition); |
| $this->validator->checkReportCondition($reportCondition); |
| $this->validator->checkReportMediums($reportMediums); |
|
|
| foreach ($idSites as $idSite) { |
| $this->validator->checkApiMethodAndMetric($idSite, $reportUniqueId, $metricValue); |
| } |
|
|
| $this->validator->checkAdditionalEmails($additionalEmails); |
|
|
| foreach ($reportMediums as $reportMedium) { |
| Piwik::postEvent('CustomAlerts.validateReportParameters', [get_defined_vars(), $reportMedium]); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function editAlert($idAlert, $name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition = false, $reportValue = false, array $reportMediums = [], string $slackChannelID = '', string $msTeamsWebhookUrl = '', string $description = '') |
| { |
| |
| $this->getAlert($idAlert); |
|
|
| $idSites = Site::getIdSitesFromIdSitesString($idSites); |
|
|
| $this->checkAlert($idSites, $name, $description, $period, $emailMe, $additionalEmails, $phoneNumbers, $slackChannelID, $msTeamsWebhookUrl, $metricCondition, $metric, $comparedTo, $reportCondition, $reportUniqueId, $reportMediums); |
|
|
| $name = Common::unsanitizeInputValue($name); |
| $description = Common::unsanitizeInputValue($description); |
|
|
| if (empty($reportCondition) || empty($reportValue)) { |
| $reportCondition = null; |
| $reportValue = null; |
| } |
|
|
| $metricValue = Common::forceDotAsSeparatorForDecimalPoint((float)$metricValue); |
|
|
| return $this->getModel()->updateAlert($idAlert, $name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition, $reportValue, $reportMediums, $slackChannelID, $msTeamsWebhookUrl, $description); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function deleteAlert($idAlert) |
| { |
| |
| $this->getAlert($idAlert); |
|
|
| $this->getModel()->deleteAlert($idAlert); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function getTriggeredAlerts($idSites) |
| { |
| if (empty($idSites)) { |
| return array(); |
| } |
|
|
| $idSites = Site::getIdSitesFromIdSitesString($idSites); |
| Piwik::checkUserHasViewAccess($idSites); |
|
|
| $login = Piwik::getCurrentUserLogin(); |
|
|
| return $this->getModel()->getTriggeredAlerts($idSites, $login); |
| } |
| } |
|
|