| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Plugins\Annotations; |
|
|
| use Exception; |
| use Piwik\Common; |
| use Piwik\Date; |
| use Piwik\Piwik; |
| use Piwik\Site; |
|
|
| |
| |
| |
| |
| |
| |
| |
| class API extends \Piwik\Plugin\API |
| { |
| |
| protected $autoSanitizeInputParams = false; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function add(int $idSite, string $date, string $note, bool $starred = false): array |
| { |
| $this->checkUserCanAddNotesFor($idSite); |
| $this->checkSiteExists($idSite); |
| $this->checkDateIsValid($date); |
| $date = Date::factory($date)->toString(); |
|
|
| $annotation = [ |
| 'idsite' => $idSite, |
| 'date' => $date, |
| 'note' => $this->filterNote($note), |
| 'starred' => (int) $starred, |
| 'user' => Piwik::getCurrentUserLogin(), |
| ]; |
|
|
| $model = new Model(); |
| $idNote = $model->createAnnotation($annotation); |
| $annotation['id'] = $idNote; |
| $this->decorateAnnotation($annotation); |
|
|
| return $annotation; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function save(int $idSite, int $idNote, ?string $date = null, ?string $note = null, ?bool $starred = null): array |
| { |
| $this->checkSiteExists($idSite); |
| $this->checkDateIsValid($date, $canBeNull = true); |
| if (null !== $date) { |
| $date = Date::factory($date)->toString(); |
| } |
|
|
| $originalAnnotation = $this->get($idSite, $idNote); |
|
|
| |
| $this->checkUserCanModifyOrDelete($originalAnnotation); |
|
|
| if (isset($starred)) { |
| $starred = intval($starred); |
| } |
|
|
| $updatedValues = array_diff_assoc( |
| array_filter( |
| [ |
| 'date' => $date, |
| 'note' => $this->filterNote($note), |
| 'starred' => $starred, |
| ], |
| function ($value) { |
| return isset($value); |
| } |
| ), |
| $originalAnnotation |
| ); |
|
|
| if (!empty($updatedValues)) { |
| $model = new Model(); |
| $originalAnnotation = $model->updateAnnotation($idNote, $idSite, $updatedValues); |
| } |
| $this->decorateAnnotation($originalAnnotation); |
|
|
| return $originalAnnotation; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function delete(int $idSite, int $idNote): void |
| { |
| $this->checkSiteExists($idSite); |
|
|
| $annotation = $this->get($idSite, $idNote); |
| |
| $this->checkUserCanModifyOrDelete($annotation); |
|
|
| $model = new Model(); |
| $model->deleteAnnotation($idNote, $idSite); |
| } |
|
|
| |
| |
| |
| |
| |
| public function deleteAll(int $idSite): void |
| { |
| Piwik::checkUserHasSuperUserAccess(); |
|
|
| $this->checkSiteExists($idSite); |
|
|
| $model = new Model(); |
| $model->deleteAllAnnotationsForSite($idSite); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function get(int $idSite, int $idNote): array |
| { |
| Piwik::checkUserHasViewAccess($idSite); |
| $this->checkSiteExists($idSite); |
|
|
| $model = new Model(); |
| $annotation = $model->getAnnotation($idNote, $idSite); |
| if (empty($annotation)) { |
| throw new Exception("There is no note with id '$idNote' for site with id '$idSite'."); |
| } |
| $this->decorateAnnotation($annotation); |
|
|
| return $annotation; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function getAll(string $idSite, ?string $date = null, string $period = 'day', ?int $lastN = null): array |
| { |
| Piwik::checkUserHasViewAccess($idSite); |
|
|
| $ids = array_map('intval', Site::getIdSitesFromIdSitesString($idSite, false, true)); |
| $model = new Model(); |
| $annotations = []; |
| foreach ($ids as $id) { |
| [$startDate, $endDate] = Annotations::getDateRangeForPeriod($date ?: false, $period, $lastN ?? false, $id); |
|
|
| $annotations[$id] = $model->getAllAnnotationsForSiteInRange( |
| $id, |
| $startDate ? $startDate->toString() : null, |
| $endDate ? $endDate->toString() : null |
| ); |
| for ($i = 0; $i < count($annotations[$id]); $i++) { |
| $this->decorateAnnotation($annotations[$id][$i]); |
| } |
| } |
|
|
| return $annotations; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function getAnnotationCountForDates( |
| string $idSite, |
| string $date, |
| string $period, |
| ?int $lastN = null, |
| bool $getAnnotationText = false |
| ): array { |
| Piwik::checkUserHasViewAccess($idSite); |
|
|
| $siteIds = array_map('intval', Site::getIdSitesFromIdSitesString($idSite, false, true)); |
| if (empty($siteIds)) { |
| return []; |
| } |
|
|
| |
| [$startDate, $endDate] = Annotations::getDateRangeForPeriod($date, $period, $lastN ?? false, $siteIds[0]); |
|
|
| if (!($startDate && $endDate)) { |
| return []; |
| } |
|
|
| if ($period == 'range') { |
| $period = 'day'; |
| } |
|
|
| |
| $dates = []; |
| for (; $startDate->getTimestamp() <= $endDate->getTimestamp(); $startDate = $startDate->addPeriod(1, $period)) { |
| $dates[] = $startDate; |
| } |
| |
| $dates[] = $startDate; |
|
|
| $model = new Model(); |
| $result = []; |
| foreach ($siteIds as $siteId) { |
| $result[$siteId] = []; |
| for ($i = 0; $i < count($dates) - 1; $i++) { |
| $date = $dates[$i]; |
| $nextDate = $dates[$i + 1]; |
| $strDate = $date->toString(); |
| $strNextDate = $nextDate->toString(); |
|
|
| [$totalCount, $starredCount] = $model->getCountAnnotationsForSiteInRange($siteId, $strDate, $strNextDate); |
|
|
| $result[$siteId][] = [ |
| $strDate, |
| [ |
| 'count' => $totalCount, |
| 'starred' => $starredCount, |
| ], |
| ]; |
|
|
| if ($getAnnotationText && $totalCount === 1) { |
| [$annotation] = $model->getAllAnnotationsForSiteInRange($siteId, $strDate, $strNextDate, 1); |
| |
| $result[$siteId][$i][1]['note'] = (string)$annotation['note']; |
| } |
| } |
| } |
|
|
| return $result; |
| } |
|
|
| |
| |
| |
| private function checkUserCanModifyOrDelete(array $annotation): void |
| { |
| if (!Annotations::canUserModifyOrDelete($annotation)) { |
| throw new Exception("The current user is not allowed to modify or delete notes for site #{$annotation['idsite']}"); |
| } |
| } |
|
|
| private static function checkUserCanAddNotesFor(int $idSite): void |
| { |
| if (!Piwik::isUserHasViewAccess($idSite) || Piwik::isUserIsAnonymous()) { |
| throw new Exception("The current user is not allowed to add notes for site #$idSite."); |
| } |
| } |
|
|
| private function checkSiteExists(int $idSite): void |
| { |
| new Site($idSite); |
| } |
|
|
| private function checkDateIsValid(?string $date, bool $canBeNull = false): void |
| { |
| if ( |
| $date === null |
| && $canBeNull |
| ) { |
| return; |
| } |
|
|
| Date::factory($date); |
| } |
|
|
| private function filterNote(?string $note): ?string |
| { |
| if (empty($note)) { |
| return $note; |
| } |
|
|
| |
| if (mb_strlen($note) > 255) { |
| $note = mb_substr($note, 0, 254) . '…'; |
| } |
|
|
| return $note; |
| } |
|
|
| |
| |
| |
| private function decorateAnnotation(array &$annotation): void |
| { |
| $annotation['date'] = substr($annotation['date'], 0, 10); |
| $annotation['note'] = Common::sanitizeInputValue($annotation['note']); |
| $annotation['canEditOrDelete'] = Annotations::canUserModifyOrDelete($annotation); |
| $annotation['idNote'] = $annotation['id']; |
| } |
| } |
|
|