All Data Structures Functions Variables Pages
Item.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 Item extends CustomFieldsModel
38  {
39  private $insideOnModified;
40 
41  protected $isSetting = false;
42 
43  // On changing a member value the original value
44  // is saved (ie: on change it again the original
45  // value is not overwritten) so that on save the
46  // changes can be written to the audit log.
47  public $originalAttributeValues = array();
48 
53  protected $isAudited = true;
54 
55  private $_workflowsToProcessAfterSave = array();
56 
57  private $_processWorkflowOnSave = true;
58 
59  public function onCreated()
60  {
61  $this->unrestrictedSet('createdDateTime', DateTimeUtil::convertTimestampToDbFormatDateTime(time()));
62  $this->unrestrictedSet('modifiedDateTime', DateTimeUtil::convertTimestampToDbFormatDateTime(time()));
63  }
64 
65  public function onModified()
66  {
67  if (!$this->insideOnModified)
68  {
69  $this->insideOnModified = true;
70  if (!($this->unrestrictedGet('id') < 0 &&
71  $this->getScenario() == 'importModel' &&
72  array_key_exists('modifiedDateTime', $this->originalAttributeValues)) &&
73  $this->getScenario() != 'doNotSetModifiedDateTimeOrUser')
74  {
75  $this->unrestrictedSet('modifiedDateTime', DateTimeUtil::convertTimestampToDbFormatDateTime(time()));
76  }
77  if (Yii::app()->user->userModel != null && Yii::app()->user->userModel->id > 0)
78  {
79  if (!($this->unrestrictedGet('id') < 0 &&
80  $this->getScenario() == 'importModel' &&
81  array_key_exists('modifiedByUser', $this->originalAttributeValues)) &&
82  $this->getScenario() != 'doNotSetModifiedDateTimeOrUser')
83  {
84  $this->unrestrictedSet('modifiedByUser', Yii::app()->user->userModel);
85  }
86  }
87  $this->insideOnModified = false;
88  }
89  }
90 
91  public function __set($attributeName, $value)
92  {
93  $this->isSetting = true;
94  try
95  {
96  if (!$this->isSaving)
97  {
98  AuditUtil::saveOriginalAttributeValue($this, $attributeName, $value);
99  }
100  parent::__set($attributeName, $value);
101  $this->isSetting = false;
102  }
103  catch (Exception $e)
104  {
105  $this->isSetting = false;
106  throw $e;
107  }
108  }
109 
110  public function delete()
111  {
112  if ($this->isAudited)
113  {
114  AuditEvent::logAuditEvent('ZurmoModule', ZurmoModule::AUDIT_EVENT_ITEM_DELETED, strval($this), $this);
115  }
116  return parent::delete();
117  }
118 
119  // Makes Item appear on the stack so that auditing can ensure
120  // that things owned by Item are only saved by Item and not directly.
121  public function save($runValidation = true, array $attributeNames = null)
122  {
123  return parent::save($runValidation, $attributeNames);
124  }
125 
126  public function addWorkflowToProcessAfterSave(Workflow $workflow)
127  {
128  $this->_workflowsToProcessAfterSave[] = $workflow;
129  }
130 
131  public function getWorkflowsToProcessAfterSave()
132  {
133  return $this->_workflowsToProcessAfterSave;
134  }
135 
136  public function setDoNotProcessWorkflowOnSave()
137  {
138  $this->_processWorkflowOnSave = false;
139  }
140 
141  public function setProcessWorkflowOnSave()
142  {
143  $this->_processWorkflowOnSave = true;
144  }
145 
146  public function shouldProcessWorkflowOnSave()
147  {
148  return $this->_processWorkflowOnSave;
149  }
150 
156  protected static function getByNameOrEquivalent($attributeName, $value)
157  {
158  assert('is_string($attributeName)');
159  assert('is_string($value) && $value != ""');
160  return static::getSubset(null, null, null, $attributeName . " = '" . DatabaseCompatibilityUtil::escape($value) . "'");
161  }
162 
169  protected function beforeSave()
170  {
171  $this->isNewModel = $this->id < 0;
172  if ($this->unrestrictedGet('id') < 0)
173  {
174  if ($this->getScenario() != 'importModel' ||
175  ($this->getScenario() == 'importModel' && $this->createdByUser->id < 0))
176  {
177  if (Yii::app()->user->userModel != null && Yii::app()->user->userModel->id > 0)
178  {
179  $this->unrestrictedSet('createdByUser', Yii::app()->user->userModel);
180  }
181  }
182  }
183  if (parent::beforeSave())
184  {
185  if ($this->isModified())
186  {
187  $this->onModified();
188  }
189  return true;
190  }
191  else
192  {
193  return false;
194  }
195  }
196 
197  protected function afterSave()
198  {
199  parent::afterSave();
200  if ($this->isAudited)
201  {
202  $this->logAuditEventsListForModified($this->isNewModel);
204  }
205  $this->originalAttributeValues = array();
206  $this->_workflowsToProcessAfterSave = array();
207  $this->isNewModel = false; //reset.
208  }
209 
210  protected function logAuditEventsListForModified($newModel)
211  {
212  if (!$newModel)
213  {
215  }
216  }
217 
218  public function forgetOriginalAttributeValues()
219  {
220  $this->unrestrictedSet('originalAttributeValues', array());
221  }
222 
223  public static function getDefaultMetadata()
224  {
225  $metadata = parent::getDefaultMetadata();
226  $metadata[__CLASS__] = array(
227  'members' => array(
228  'createdDateTime',
229  'modifiedDateTime',
230  ),
231  'relations' => array(
232  'createdByUser' => array(static::HAS_ONE, 'User', static::NOT_OWNED,
233  static::LINK_TYPE_SPECIFIC, 'createdByUser'),
234  'modifiedByUser' => array(static::HAS_ONE, 'User', static::NOT_OWNED,
235  static::LINK_TYPE_SPECIFIC, 'modifiedByUser'),
236  ),
237  'rules' => array(
238  array('createdDateTime', 'required'),
239  array('createdDateTime', 'readOnly'),
240  array('createdDateTime', 'type', 'type' => 'datetime'),
241  array('createdByUser', 'readOnly'),
242  array('modifiedDateTime', 'required'),
243  array('modifiedDateTime', 'readOnly'),
244  array('modifiedDateTime', 'type', 'type' => 'datetime'),
245  array('modifiedByUser', 'readOnly'),
246  ),
247  'elements' => array(
248  'createdDateTime' => 'DateTime',
249  'modifiedDateTime' => 'DateTime',
250  ),
251  );
252  return $metadata;
253  }
254 
255  public static function isTypeDeletable()
256  {
257  return false;
258  }
259 
264  public function testBeforeDelete()
265  {
266  $this->beforeDelete();
267  }
268 
272  public function isAttributeAudited($attributeName)
273  {
274  assert("\$this->isAttribute('$attributeName')");
275  assert('$attributeName != "id"');
276  $attributeModelClassName = static::getAttributeModelClassName($attributeName);
277  $metadata = static::getMetadata();
278  if (isset($metadata[$attributeModelClassName]['noAudit']) &&
279  in_array($attributeName, $metadata[$attributeModelClassName]['noAudit']))
280  {
281  return false;
282  }
283  return true;
284  }
285 
292  public function isAllowedToSetReadOnlyAttribute($attributeName)
293  {
294  if ($this->getScenario() == 'importModel' || $this->getScenario() == 'searchModel')
295  {
296  if ($this->unrestrictedGet('id') > 0)
297  {
298  return false;
299  }
300  if ( in_array($attributeName, array('createdByUser', 'modifiedByUser', 'createdDateTime', 'modifiedDateTime')))
301  {
302  return true;
303  }
304  else
305  {
306  throw new NotSupportedException();
307  }
308  }
309  return false;
310  }
311 
315  public static function getGamificationRulesType()
316  {
317  return null;
318  }
319 
320  protected static function translatedAttributeLabels($language)
321  {
322  return array_merge(parent::translatedAttributeLabels($language), array(
323  'createdByUser' => Zurmo::t('ZurmoModule', 'Created By User', array(), null, $language),
324  'createdDateTime' => Zurmo::t('ZurmoModule', 'Created Date Time', array(), null, $language),
325  'modifiedByUser' => Zurmo::t('ZurmoModule', 'Modified By User', array(), null, $language),
326  'modifiedDateTime' => Zurmo::t('ZurmoModule', 'Modified Date Time', array(), null, $language),
327  ));
328  }
329 
330  public static function getNonConfigurableAttributes()
331  {
332  $metadata = static::getMetadata();
333  if (isset($metadata[get_called_class()]['nonConfigurableAttributes']))
334  {
335  return $metadata[get_called_class()]['nonConfigurableAttributes'];
336  }
337  return array();
338  }
339 
345  public static function getByName($name)
346  {
347  return self::getByNameOrEquivalent('name', $name);
348  }
349  }
350 ?>
static getByName($name)
Definition: Item.php:345
static getByNameOrEquivalent($attributeName, $value)
Definition: Item.php:156
static getGamificationRulesType()
Definition: Item.php:315
beforeSave()
Definition: Item.php:169
isAttributeAudited($attributeName)
Definition: Item.php:272
static saveOriginalAttributeValue($auditableModel, $attributeName, $value)
Definition: AuditUtil.php:66
static logAuditEventsListForChangedAttributeValues(Item $item, array $attributeNames=array(), RedBeanModel $ownedModel=null)
Definition: AuditUtil.php:102
Definition: Item.php:37
unrestrictedSet($attributeName, $value)
static clearRelatedModelsOriginalAttributeValues(Item $item)
Definition: AuditUtil.php:250
isAllowedToSetReadOnlyAttribute($attributeName)
Definition: Item.php:292
testBeforeDelete()
Definition: Item.php:264
Generated on Thu Apr 9 2020 07:10:45