1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8/** 9 * Manage the cache of changes to send to users 10 * in a period report. 11 * 12 * @package Tiki 13 * @subpackage Reports 14 */ 15class Reports_Cache 16{ 17 /** 18 * @var TikiDb 19 */ 20 protected $db; 21 22 protected $table; 23 24 /** 25 * @var DateTime 26 */ 27 protected $dt; 28 29 /** 30 * @param TikiDb $db 31 * @return null 32 */ 33 public function __construct(TikiDb $db, DateTime $dt) 34 { 35 $this->db = $db; 36 $this->table = $db->table('tiki_user_reports_cache'); 37 $this->dt = $dt; 38 } 39 40 /** 41 * Return cache entries for a given user. 42 * @param string $user 43 * @return array 44 */ 45 public function get($user) 46 { 47 $entries = $this->table->fetchAll(['user', 'event', 'data', 'time'], ['user' => $user], -1, -1, 'time ASC'); 48 49 $ret = []; 50 51 foreach ($entries as $entry) { 52 $entry['data'] = unserialize($entry['data']); 53 $ret[] = $entry; 54 } 55 56 return $ret; 57 } 58 59 /** 60 * Delete all cache entries for a given user. 61 * 62 * @param string $user 63 * @return null 64 */ 65 public function delete($user) 66 { 67 $this->table->deleteMultiple(['user' => $user]); 68 } 69 70 /** 71 * Add Tiki object change information to reports cache 72 * and remove it from the $watches array so that it is not 73 * send to the user in a single email. 74 * 75 * @param array $watches a list of users watching the changed object and some information about the object itself 76 * @param array $data information about the changed object 77 * @param array $users a list of users that are using periodic reports 78 * @return null 79 */ 80 public function add(&$watches, $data, $users) 81 { 82 $data["base_url"] = TikiLib::tikiURL(); //Store $base_url in the database. Use it to construct links in the email. 83 84 foreach ($watches as $key => $watch) { 85 // if user in the watch has enabled periodic reports 86 if (in_array($watch['user'], $users)) { 87 // add data to report cache 88 $this->table->insert( 89 [ 90 'user' => $watch['user'], 91 'event' => $data['event'], 92 'data' => serialize($data), 93 'time' => $this->dt->format('Y-m-d H:i:s') 94 ] 95 ); 96 97 // remove data from $watches array so that the user doesn't receive a email 98 // notification for the event 99 unset($watches[$key]); 100 } 101 } 102 } 103} 104