Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
AuditEvent.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
37  class AuditEvent extends RedBeanModel
38  {
39  public static $isTableOptimized = false;
40 
41  public static function getSinceTimestamp($timestamp)
42  {
43  assert('is_int($timestamp)');
44  return getSinceDateTime(self::convertTimestampToDbFormatDateTime($timestamp));
45  }
46 
47  public static function getSinceDate($date)
48  {
49  assert('DateTimeUtil::isValidDbFormattedDate($date)');
50  return self::makeModels($beans = ZurmoRedBean::find('auditevent', "datetime >= '$date 00-00-00'"));
51  }
52 
53  public static function getSinceDateTime($dateTime)
54  {
55  assert('DateTimeUtil::isValidDbFormattedDateTime($dateTime)');
56  return self::makeModels($beans = ZurmoRedBean::find('auditevent', "datetime >= '$dateTime'"));
57  }
58 
59  public static function getTailEvents($count)
60  {
61  assert('is_int($count)');
62  $sql = "select id
63  from
64  (select id
65  from auditevent
66  order by id desc
67  limit $count) as temp
68  order by id";
69  $ids = ZurmoRedBean::getCol($sql);
70  $beans = ZurmoRedBean::batch ('auditevent', $ids);
71  return self::makeModels($beans, __CLASS__);
72  }
73 
74  public static function logAuditEvent($moduleName, $eventName, $data = null, RedBeanModel $model = null, User $user = null)
75  {
76  assert('is_string($moduleName) && $moduleName != ""');
77  assert('is_string($eventName) && $eventName != ""');
78  if ($user === null)
79  {
80  $user = Yii::app()->user->userModel;
81  if (!$user instanceof User)
82  {
84  }
85  }
86  if ($eventName == ZurmoModule::AUDIT_EVENT_ITEM_VIEWED)
87  {
90  $model,
91  AuditEventsRecentlyViewedUtil::RECENTLY_VIEWED_COUNT + 1);
92  }
93  if ($eventName == ZurmoModule::AUDIT_EVENT_ITEM_DELETED)
94  {
95  $modelClassName = get_class($model);
97  deleteModelFromRecentlyViewed($modelClassName::getModuleClassName(),
98  $model);
99  }
100  if ($eventName != ZurmoModule::AUDIT_EVENT_ITEM_VIEWED)
101  {
102  if (!AuditEvent::$isTableOptimized && !AUDITING_OPTIMIZED)
103  {
104  $auditEvent = new AuditEvent();
105  $auditEvent->dateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
106  $auditEvent->moduleName = $moduleName;
107  $auditEvent->eventName = $eventName;
108  $auditEvent->user = $user;
109  $auditEvent->modelClassName = $model !== null ? get_class($model) : null;
110  $auditEvent->modelId = $model !== null ? $model->id : null;
111  $auditEvent->serializedData = serialize($data);
112  $saved = $auditEvent->save();
113  AuditEvent::$isTableOptimized = true;
114  }
115  else
116  {
117  $sql = "insert into auditevent (datetime,
118  modulename,
119  eventname,
120  _user_id,
121  modelclassname,
122  modelid,
123  serializeddata)
124  values ('" . DateTimeUtil::convertTimestampToDbFormatDateTime(time()) . "',
125  '$moduleName',
126  '$eventName',
127  {$user->id}, " .
128  ($model !== null ? "'" . get_class($model) . "', " : 'null, ') .
129  ($model !== null ? "{$model->id}, " : 'null, ') .
130  ":data)";
131  ZurmoRedBean::exec($sql, array('data' => serialize($data))) !== null;
132  $saved = true;
133  }
134  return $saved;
135  }
136  }
137 
138  public function __toString()
139  {
140  $modelClassName = $this->modelClassName;
141  $s = DateTimeUtil::convertDbFormattedDateTimeToLocaleFormattedDisplay($this->dateTime) . ', ';
142  $s .= $this->user . ', ';
143  $s .= $this->eventName;
144  if ($this->modelClassName !== null)
145  {
146  assert('is_string($this->modelClassName) && $this->modelClassName != ""');
147  assert('is_numeric($this->modelId)');
148  $s .= ', ' . $modelClassName::getModelLabelByTypeAndLanguage('Singular') . '(' . $this->modelId . ')';
149  }
150  return $s;
151  }
152 
153  protected static function translatedAttributeLabels($language)
154  {
155  return array_merge(parent::translatedAttributeLabels($language),
156  array(
157  'auditEvent' => Zurmo::t('ZurmoModule', 'Audit Event', array(), null, $language)
158  )
159  );
160  }
161 
162  public static function getDefaultMetadata()
163  {
164  $metadata = parent::getDefaultMetadata();
165  $metadata[__CLASS__] = array(
166  'members' => array(
167  'dateTime',
168  'eventName',
169  'moduleName',
170  'modelClassName',
171  'modelId',
172  'serializedData',
173  ),
174  'relations' => array(
175  'user' => array(static::HAS_ONE, 'User'),
176  ),
177  'rules' => array(
178  array('dateTime', 'required'),
179  array('dateTime', 'type', 'type' => 'datetime'),
180  array('eventName', 'required'),
181  array('eventName', 'type', 'type' => 'string'),
182  array('eventName', 'length', 'min' => 1, 'max' => 64),
183  array('moduleName', 'required'),
184  array('moduleName', 'type', 'type' => 'string'),
185  array('moduleName', 'length', 'min' => 1, 'max' => 64),
186  array('modelClassName', 'type', 'type' => 'string'),
187  array('modelClassName', 'length', 'min' => 1, 'max' => 64),
188  array('modelId', 'type', 'type' => 'integer'),
189  array('serializedData', 'required'),
190  array('serializedData', 'type', 'type' => 'string'),
191  ),
192  'indexes' => array(
193  'modelclass_modelid_index' => array(
194  'members' => array('modelClassName', 'modelId'),
195  'unique' => false),
196  ),
197  );
198  return $metadata;
199  }
200 
201  public static function deleteAllByModel(RedBeanModel $model)
202  {
203  if ($model instanceof Item)
204  {
205  $searchAttributeData = array();
206  $searchAttributeData['clauses'] = array(
207  1 => array(
208  'attributeName' => 'modelClassName',
209  'operatorType' => 'equals',
210  'value' => get_class($model),
211  ),
212  2 => array(
213  'attributeName' => 'modelId',
214  'operatorType' => 'equals',
215  'value' => $model->id,
216  ),
217  );
218  $searchAttributeData['structure'] = '1 and 2';
219  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('AuditEvent');
220  $where = RedBeanModelDataProvider::makeWhere('AuditEvent', $searchAttributeData, $joinTablesAdapter);
221  $auditEvents = self::getSubset($joinTablesAdapter, null, null, $where, null);
222  foreach ($auditEvents as $event)
223  {
224  $event->delete();
225  }
226  }
227  }
228  }
229 ?>
Definition: User.php:37
static makeWhere($modelClassName, array $metadata, &$joinTablesAdapter)
static resolveNewRecentlyViewedModel($moduleName, RedBeanModel $model, $count)
Definition: Item.php:37
static deleteModelFromRecentlyViewed($moduleName, RedBeanModel $model)
Generated on Thu Jul 2 2020 07:10:38
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.