diff --git a/plugins/CustomAlerts/API.php b/plugins/CustomAlerts/API.php index 953b83854f3..9f3781d4319 100755 --- a/plugins/CustomAlerts/API.php +++ b/plugins/CustomAlerts/API.php @@ -22,32 +22,29 @@ */ class API extends \Piwik\Plugin\API { - - private function getModel() - { - return new Model(); - } - - /** - * Returns a single Alert - * - * @param int $idAlert - */ + /** + * Returns a single Alert + * + * @param int $idAlert + * + * @return array + */ public function getAlert($idAlert) { return $this->getModel()->getAlert($idAlert); } - /** - * Returns the Alerts that are defined on the idSites given. - * If no value is given, all Alerts for the current user will - * be returned. - * - * @param array $idSites - */ + /** + * Returns the Alerts that are defined on the idSites given. + * If no value is given, all Alerts for the current user will + * be returned. + * + * @param array $idSites + * @return array + */ public function getAlerts($idSites = array()) { - return $this->getModel()->getAlert($idSites); + return $this->getModel()->getAlerts($idSites); } public function getTriggeredAlerts($period, $date, $login = false) @@ -80,22 +77,23 @@ public function addAlert($name, $idSites, $period, $email, $metric, $metricCondi return $this->getModel()->addAlert($name, $idSites, $period, $email, $metric, $metricCondition, $metricValue, $report, $reportCondition, $reportValue); } - /** - * Edits an Alert for given website(s). - * - * @param string $idalert ID of the Alert to edit. - * @param string $name Name of Alert - * @param mixed $idSites Single int or array of ints of idSites. - * @param string $period Period the alert is defined on. - * @param bool $email - * @param string $metric (nb_uniq_visits, sum_visit_length, ..) - * @param string $metricCondition - * @param float $metricValue - * @param string $report - * @param string $reportCondition - * @param string $reportValue - * @return boolean - */ + /** + * Edits an Alert for given website(s). + * + * @param $idAlert + * @param string $name Name of Alert + * @param mixed $idSites Single int or array of ints of idSites. + * @param string $period Period the alert is defined on. + * @param bool $email + * @param string $metric (nb_uniq_visits, sum_visit_length, ..) + * @param string $metricCondition + * @param float $metricValue + * @param string $report + * @param string $reportCondition + * @param string $reportValue + * + * @return boolean + */ public function editAlert($idAlert, $name, $idSites, $period, $email, $metric, $metricCondition, $metricValue, $report, $reportCondition = '', $reportValue = '') { return $this->getModel()->editAlert($idAlert, $name, $idSites, $period, $email, $metric, $metricCondition, $metricValue, $report, $reportCondition, $reportValue); @@ -110,5 +108,11 @@ public function deleteAlert($idAlert) { $this->getModel()->deleteAlert($idAlert); } + + private function getModel() + { + return new Model(); + } + } ?> \ No newline at end of file diff --git a/plugins/CustomAlerts/Controller.php b/plugins/CustomAlerts/Controller.php index e80407384e0..3298d42fcd2 100755 --- a/plugins/CustomAlerts/Controller.php +++ b/plugins/CustomAlerts/Controller.php @@ -26,40 +26,17 @@ class Controller extends \Piwik\Plugin\Controller { - private $alertGroupConditions = array( - 'CustomAlerts_MatchesExactly' => 'matches_exactly', - 'CustomAlerts_DoesNotMatchExactly' => 'does_not_match_exactly', - 'CustomAlerts_MatchesRegularExpression' => 'matches_regex', - 'CustomAlerts_DoesNotMatchRegularExpression' => 'does_not_match_regex', - 'CustomAlerts_Contains' => 'contains', - 'CustomAlerts_DoesNotContain' => 'does_not_contain', - 'CustomAlerts_StartsWith' => 'starts_with', - 'CustomAlerts_DoesNotStartWith' => 'does_not_start_with', - 'CustomAlerts_EndsWith' => 'ends_with', - 'CustomAlerts_DoesNotEndWith' => 'does_not_end_with', - ); - private $alertMetricConditions = array( - 'CustomAlerts_IsLessThan' => 'less_than', - 'CustomAlerts_IsGreaterThan' => 'greater_than', - 'CustomAlerts_DecreasesMoreThan' => 'decrease_more_than', - 'CustomAlerts_IncreasesMoreThan' => 'increase_more_than', - 'CustomAlerts_PercentageDecreasesMoreThan' => 'percentage_decrease_more_than', - 'CustomAlerts_PercentageIncreasesMoreThan' => 'percentage_increase_more_than', - ); - /** * Shows all Alerts of the current selected idSite. */ public function index() { - $view = new View('@CustomAlerts/index'); - $this->setGeneralVariablesView($view); - - $idSite = Common::getRequestVar('idSite'); + $idSite = Common::getRequestVar('idSite', null, 'int'); - $alertList = API::getInstance()->getAlerts(array($idSite)); + $view = new View('@CustomAlerts/index'); + $this->setGeneralVariablesView($view); - $view->alertList = $alertList; + $view->alerts = API::getInstance()->getAlerts(array($idSite)); return $view->render(); } @@ -69,42 +46,34 @@ public function addNewAlert() $view = new View('@CustomAlerts/addNewAlert'); $this->setGeneralVariablesView($view); - $sitesList = SitesManagerApi::getInstance()->getSitesWithAtLeastViewAccess(); - $view->sitesList = $sitesList; - - $availableReports = MetadataApi::getInstance()->getReportMetadata(); + $view->sitesList = SitesManagerApi::getInstance()->getSitesWithAtLeastViewAccess(); // ToDo need to collect metrics,processedMetrics,goalMetrics, goalProcessedMetric $view->alertGroups = array(); - $view->alerts = $availableReports; - $view->alertGroupConditions = $this->alertGroupConditions; - $view->alertMetricConditions = $this->alertMetricConditions; + $view->alerts = MetadataApi::getInstance()->getReportMetadata(); + $view->alertGroupConditions = Processor::getGroupConditions(); + $view->alertMetricConditions = Processor::getMetricConditions(); return $view->render(); } public function editAlert() { - $idAlert = Common::getRequestVar('idalert'); + $idAlert = Common::getRequestVar('idAlert', null, 'int'); $view = new View('@CustomAlerts/editAlert'); $this->setGeneralVariablesView($view); - $alert = API::getInstance()->getAlert($idAlert); - $view->alert = $alert; - - $sitesList = SitesManagerApi::getInstance()->getSitesWithAtLeastViewAccess(); - $view->sitesList = $sitesList; + $view->alert = API::getInstance()->getAlert($idAlert); + $view->sitesList = SitesManagerApi::getInstance()->getSitesWithAtLeastViewAccess(); $model = new Model(); $view->sitesDefined = $model->fetchSiteIdsTheAlertWasDefinedOn($idAlert); - $availableReports = MetadataApi::getInstance()->getReportMetadata(); - - $view->alerts = $availableReports; - $view->alertGroupConditions = $this->alertGroupConditions; - $view->alertMetricConditions = $this->alertMetricConditions; + $view->alerts = MetadataApi::getInstance()->getReportMetadata(); + $view->alertGroupConditions = Processor::getGroupConditions(); + $view->alertMetricConditions = Processor::getMetricConditions(); return $view->render(); } diff --git a/plugins/CustomAlerts/Model.php b/plugins/CustomAlerts/Model.php index 67ab05d05cf..64c1b78190f 100755 --- a/plugins/CustomAlerts/Model.php +++ b/plugins/CustomAlerts/Model.php @@ -83,35 +83,28 @@ public static function uninstall() /** - * Returns a single Alert - * - * @param int $idAlert - */ + * Returns a single Alert + * + * @param int $idAlert + * + * @return array + */ public function getAlert($idAlert) { - $alert = Db::fetchAll("SELECT * FROM " - . Common::prefixTable('alert') - . " WHERE idalert = ?", - intval($idAlert) - ); + $query = sprintf('SELECT * FROM %s WHERE idalert = ?', Common::prefixTable('alert'), intval($idAlert)); + $alert = Db::fetchAll($query); - if (!$alert) { + if (empty($alert)) { throw new Exception(Piwik::translate('CustomAlerts_AlertDoesNotExist', $idAlert)); } - $alert = $alert[0]; + $alert = array_shift($alert); if (!Piwik::isUserIsSuperUserOrTheUser($alert['login'])) { throw new Exception(Piwik::translate('CustomAlerts_AccessException', $idAlert)); } -// $idSites = Piwik_FetchAll("SELECT * FROM " -// . Common::prefixTable('alert_site') -// . " WHERE idalert = ?", -// intval($idAlert) -// ); -// -// $alert['idsites'] = $idSites; + $alert['idSites'] = $this->fetchSiteIdsTheAlertWasDefinedOn($idAlert); return $alert; } @@ -132,6 +125,8 @@ public function getAlerts($idSites) $idSites = SitesManagerApi::getInstance()->getSitesIdWithAtLeastViewAccess(); } + $idSites = array_map('intval', $idSites); + $alerts = Db::fetchAll(("SELECT * FROM " . Common::prefixTable('alert') . " WHERE idalert IN ( @@ -149,7 +144,7 @@ public function getTriggeredAlerts($period, $date, $login) $this->checkPeriod($period); $piwikDate = Date::factory($date); - $date = Period::factory($period, $piwikDate); + $date = Period::factory($period, $piwikDate); $db = Db::get(); @@ -173,15 +168,18 @@ public function getTriggeredAlerts($period, $date, $login) WHERE period = ? AND ts_triggered BETWEEN ? AND ?"; + $values = array( + $period, + $date->getDateStart()->getDateStartUTC(), + $date->getDateEnd()->getDateEndUTC() + ); + if ($login !== false) { - $sql .= " AND login = \"" . $login . "\""; + $sql .= " AND login = ?"; + $values[] = $login; } - return $db->fetchAll($sql, array( - $period, - $date->getDateStart()->getDateStartUTC(), - $date->getDateEnd()->getDateEndUTC()) - ); + return $db->fetchAll($sql, $values); } @@ -204,21 +202,24 @@ public function getAllAlerts($period) return Db::fetchAll($sql); } - /** - * Creates an Alert for given website(s). - * - * @param string $name - * @param mixed $idSites - * @param string $period - * @param bool $email - * @param string $metric (nb_uniq_visits, sum_visit_length, ..) - * @param string $metricCondition - * @param float $metricValue - * @param string $report - * @param string $reportCondition - * @param string $reportValue - * @return int ID of new Alert - */ + /** + * Creates an Alert for given website(s). + * + * @param string $name + * @param mixed $idSites + * @param string $period + * @param bool $email + * @param string $metric (nb_uniq_visits, sum_visit_length, ..) + * @param string $metricCondition + * @param float $metricValue + * @param string $report + * @param string $reportCondition + * @param string $reportValue + * + * @throws \Exception + * + * @return int ID of new Alert + */ public function addAlert($name, $idSites, $period, $email, $metric, $metricCondition, $metricValue, $report, $reportCondition, $reportValue) { if (!is_array($idSites)) { @@ -230,30 +231,31 @@ public function addAlert($name, $idSites, $period, $email, $metric, $metricCondi $name = $this->checkName($name); $this->checkPeriod($period); - // save in db - $db = Db::get(); - $idAlert = Db::fetchOne("SELECT max(idalert) + 1 FROM " . Common::prefixTable('alert')); - if ($idAlert == false) { + $idAlert = $this->getNextAlertId(); + if (empty($idAlert)) { $idAlert = 1; } $newAlert = array( - 'idalert' => $idAlert, - 'name' => $name, - 'period' => $period, - 'login' => Piwik::getCurrentUserLogin(), - 'enable_mail' => (int) $email, - 'metric' => $metric, + 'idalert' => $idAlert, + 'name' => $name, + 'period' => $period, + 'login' => Piwik::getCurrentUserLogin(), + 'enable_mail' => (int) $email, + 'metric' => $metric, 'metric_condition' => $metricCondition, - 'metric_matched' => (float) $metricValue, - 'report' => $report, - 'deleted' => 0, + 'metric_matched' => (float) $metricValue, + 'report' => $report, + 'deleted' => 0, ); if (!empty($reportCondition) && !empty($reportCondition)) { $newAlert['report_condition'] = $reportCondition; - $newAlert['report_matched'] = $reportValue; - } + $newAlert['report_matched'] = $reportValue; + } else { + $alert['report_condition'] = null; + $alert['report_matched'] = null; + } // Do we have a valid alert for all given idSites? foreach ($idSites as $idSite) { @@ -262,32 +264,37 @@ public function addAlert($name, $idSites, $period, $email, $metric, $metricCondi } } + // save in db + $db = Db::get(); $db->insert(Common::prefixTable('alert'), $newAlert); foreach ($idSites as $idSite) { $db->insert(Common::prefixTable('alert_site'), array( - 'idalert' => $idAlert, - 'idsite' => $idSite + 'idalert' => intval($idAlert), + 'idsite' => intval($idSite) )); } return $idAlert; } - /** - * Edits an Alert for given website(s). - * - * @param string $idalert ID of the Alert to edit. - * @param string $name Name of Alert - * @param mixed $idSites Single int or array of ints of idSites. - * @param string $period Period the alert is defined on. - * @param bool $email - * @param string $metric (nb_uniq_visits, sum_visit_length, ..) - * @param string $metricCondition - * @param float $metricValue - * @param string $report - * @param string $reportCondition - * @param string $reportValue - * @return boolean - */ + /** + * Edits an Alert for given website(s). + * + * @param $idAlert + * @param string $name Name of Alert + * @param mixed $idSites Single int or array of ints of idSites. + * @param string $period Period the alert is defined on. + * @param bool $email + * @param string $metric (nb_uniq_visits, sum_visit_length, ..) + * @param string $metricCondition + * @param float $metricValue + * @param string $report + * @param string $reportCondition + * @param string $reportValue + * + * @throws \Exception + * + * @return boolean + */ public function editAlert($idAlert, $name, $idSites, $period, $email, $metric, $metricCondition, $metricValue, $report, $reportCondition, $reportValue) { if (!is_array($idSites)) { @@ -296,67 +303,65 @@ public function editAlert($idAlert, $name, $idSites, $period, $email, $metric, $ Piwik::checkUserHasViewAccess($idSites); - // Is the name in a valid format? $name = $this->checkName($name); - - // Is the period valid? $this->checkPeriod($period); - // Save in DB - $db = Db::get(); - $alert = array( - 'name' => $name, - 'period' => $period, - 'login' => 'admin', //Piwik::getCurrentUserLogin(), - 'enable_mail' => (boolean) $email, - 'metric' => $metric, + 'name' => $name, + 'period' => $period, + 'login' => 'admin', //Piwik::getCurrentUserLogin(), + 'enable_mail' => (boolean) $email, + 'metric' => $metric, 'metric_condition' => $metricCondition, - 'metric_matched' => (float) $metricValue, - 'report' => $report, - 'deleted' => 0, + 'metric_matched' => (float) $metricValue, + 'report' => $report, + 'deleted' => 0, ); - // if (!empty($reportCondition) && !empty($reportCondition)) { $alert['report_condition'] = $reportCondition; - $alert['report_matched'] = $reportValue; + $alert['report_matched'] = $reportValue; } else { $alert['report_condition'] = null; - $alert['report_matched'] = null; + $alert['report_matched'] = null; } // Do we have a valid alert for all given idSites? foreach ($idSites as $idSite) { - if (!$this->isValidAlert($alert, $idSites)) { + if (!$this->isValidAlert($alert, $idSite)) { throw new Exception(Piwik::translate('CustomAlerts_ReportOrMetricIsInvalid')); } } - $db->update(Common::prefixTable('alert'), $alert, "idalert = " . $idAlert); + // Save in DB + $db = Db::get(); + $db->update(Common::prefixTable('alert'), $alert, "idalert = " . intval($idAlert)); $db->query("DELETE FROM " . Common::prefixTable("alert_site") . " WHERE idalert = ?", $idAlert); foreach ($idSites as $idSite) { $db->insert(Common::prefixTable('alert_site'), array( - 'idalert' => $idAlert, - 'idsite' => $idSite + 'idalert' => intval($idAlert), + 'idsite' => intval($idSite) )); } + return $idAlert; } - /** - * Delete alert by id. - * - * @param int $idAlert - */ + /** + * Delete alert by id. + * + * @param int $idAlert + * + * @throws \Exception In case alert does not exist or not enough permission + */ public function deleteAlert($idAlert) { $alert = $this->getAlert($idAlert); - if (!$alert) { + if (empty($alert)) { throw new Exception(Piwik::translate('CustomAlerts_AlertDoesNotExist', $idAlert)); } @@ -366,18 +371,26 @@ public function deleteAlert($idAlert) $db->update( Common::prefixTable('alert'), array("deleted" => 1), - "idalert = " . $idAlert + "idalert = " . intval($idAlert) ); } public function triggerAlert($idAlert, $idSite) { + $alert = $this->getAlert($idAlert); + + if (empty($alert)) { + throw new Exception(Piwik::translate('CustomAlerts_AlertDoesNotExist', $idAlert)); + } + + Piwik::checkUserIsSuperUserOrTheUser($alert['login']); + $db = Db::get(); $db->insert( Common::prefixTable('alert_log'), array( - 'idalert' => $idAlert, - 'idsite' => $idSite, + 'idalert' => intval($idAlert), + 'idsite' => intval($idSite), 'ts_triggered' => Date::now()->getDatetime() ) ); @@ -401,7 +414,7 @@ public function fetchSiteIdsTheAlertWasDefinedOn($idAlert) * the given idSites and if the a dimension is * given (requires report_condition, report_matched) * - * @param array alert + * @param array $alert * @param int $idSite * @return boolean */ @@ -437,11 +450,9 @@ private function isValidAlert($alert, $idSite) if (isset($report[0]['dimension']) && (!isset($alert['report_condition']) || !isset($alert['report_matched']))) { return false; - } else { - return true; } - return false; + return true; } private function checkName($name) @@ -461,5 +472,11 @@ private function isValidPeriod($period) return in_array($period, array('day', 'week', 'month', 'year')); } + private function getNextAlertId() + { + $idAlert = Db::fetchOne("SELECT max(idalert) + 1 FROM " . Common::prefixTable('alert')); + return $idAlert; + } + } ?> \ No newline at end of file diff --git a/plugins/CustomAlerts/Processor.php b/plugins/CustomAlerts/Processor.php index 6b41cc195a3..b3c226391db 100755 --- a/plugins/CustomAlerts/Processor.php +++ b/plugins/CustomAlerts/Processor.php @@ -27,6 +27,33 @@ */ class Processor extends \Piwik\Plugin { + public static function getGroupConditions() + { + return array( + 'CustomAlerts_MatchesExactly' => 'matches_exactly', + 'CustomAlerts_DoesNotMatchExactly' => 'does_not_match_exactly', + 'CustomAlerts_MatchesRegularExpression' => 'matches_regex', + 'CustomAlerts_DoesNotMatchRegularExpression' => 'does_not_match_regex', + 'CustomAlerts_Contains' => 'contains', + 'CustomAlerts_DoesNotContain' => 'does_not_contain', + 'CustomAlerts_StartsWith' => 'starts_with', + 'CustomAlerts_DoesNotStartWith' => 'does_not_start_with', + 'CustomAlerts_EndsWith' => 'ends_with', + 'CustomAlerts_DoesNotEndWith' => 'does_not_end_with', + ); + } + + public static function getMetricConditions() + { + return array( + 'CustomAlerts_IsLessThan' => 'less_than', + 'CustomAlerts_IsGreaterThan' => 'greater_than', + 'CustomAlerts_DecreasesMoreThan' => 'decrease_more_than', + 'CustomAlerts_IncreasesMoreThan' => 'increase_more_than', + 'CustomAlerts_PercentageDecreasesMoreThan' => 'percentage_decrease_more_than', + 'CustomAlerts_PercentageIncreasesMoreThan' => 'percentage_increase_more_than', + ); + } public function processDailyAlerts() { diff --git a/plugins/CustomAlerts/templates/list.twig b/plugins/CustomAlerts/templates/list.twig index 1d93f67bccd..0daa540604b 100755 --- a/plugins/CustomAlerts/templates/list.twig +++ b/plugins/CustomAlerts/templates/list.twig @@ -11,7 +11,7 @@ - {% if not alertList %} + {% if not alerts %}
@@ -20,7 +20,7 @@ {% else %} - {% for alert in alertList %} + {% for alert in alerts %} {{ alert.idalert }} {{ alert.name }} @@ -28,7 +28,7 @@ {{ alert.report }} {% if alert.enable_mail %}Yes{% else %}No{% endif %} {% set idalert=alert.idalert %} - Edit + Edit {% endfor %} {% endif %}