Skip to content

Commit

Permalink
Fixing audit log for companies (mautic#13346)
Browse files Browse the repository at this point in the history
* Recording field changes for Company entity

* Adding test for company property changes

* Update app/bundles/LeadBundle/Entity/Company.php

---------

Co-authored-by: Saurabh Gupta <[email protected]>
  • Loading branch information
escopecz and dadarya0 authored Feb 9, 2024
1 parent 6a3ae90 commit 01007a0
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 30 deletions.
2 changes: 2 additions & 0 deletions app/bundles/CoreBundle/Entity/CommonEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public function __toString(): string
/**
* @param string $prop
* @param mixed $val
*
* @return void
*/
protected function isChanged($prop, $val)
{
Expand Down
89 changes: 64 additions & 25 deletions app/bundles/LeadBundle/Entity/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,33 @@ public static function getDefaultIdentifierFields(): array
];
}

protected function isChanged($prop, $val)
{
$prefix = 'company';

if (str_starts_with($prop, $prefix)) {
$getter = 'get'.ucfirst(substr($prop, strlen($prefix)));
$current = $this->$getter();
if ($current !== $val) {
$this->addChange($prop, [$current, $val]);
}
} elseif ('owner' === $prop) {
$current = $this->getOwner();
if ($current && !$val) {
$this->changes['owner'] = [$current->getName().' ('.$current->getId().')', $val];
} elseif (!$current && $val) {
$this->changes['owner'] = [$current, $val->getName().' ('.$val->getId().')'];
} elseif ($current && $current->getId() != $val->getId()) {
$this->changes['owner'] = [
$current->getName().'('.$current->getId().')',
$val->getName().'('.$val->getId().')',
];
}
} else {
parent::isChanged($prop, $val);
}
}

/**
* @return int
*/
Expand Down Expand Up @@ -227,7 +254,7 @@ public function getPermissionUser()
}

/**
* @param User $score
* @param int $score
*
* @return Company
*/
Expand All @@ -250,240 +277,252 @@ public function getScore()
}

/**
* @return mixed
* @return string|null
*/
public function getName()
{
return $this->name;
}

/**
* @param mixed $name
* @param string|null $name
*
* @return Company
*/
public function setName($name)
{
$this->isChanged('companyname', $name);
$this->name = $name;

return $this;
}

/**
* @return mixed
* @return string|null
*/
public function getEmail()
{
return $this->email;
}

/**
* @param mixed $email
* @param string|null $email
*
* @return Company
*/
public function setEmail($email)
{
$this->isChanged('companyemail', $email);
$this->email = $email;

return $this;
}

/**
* @return mixed
* @return string|null
*/
public function getAddress1()
{
return $this->address1;
}

/**
* @param mixed $address1
* @param string|null $address1
*
* @return Company
*/
public function setAddress1($address1)
{
$this->isChanged('companyaddress1', $address1);
$this->address1 = $address1;

return $this;
}

/**
* @return mixed
* @return string|null
*/
public function getAddress2()
{
return $this->address2;
}

/**
* @param mixed $address2
* @param string|null $address2
*
* @return Company
*/
public function setAddress2($address2)
{
$this->isChanged('companyaddress2', $address2);
$this->address2 = $address2;

return $this;
}

/**
* @return mixed
* @return string|null
*/
public function getPhone()
{
return $this->phone;
}

/**
* @param mixed $phone
* @param string|null $phone
*
* @return Company
*/
public function setPhone($phone)
{
$this->isChanged('companyphone', $phone);
$this->phone = $phone;

return $this;
}

/**
* @return mixed
* @return string|null
*/
public function getCity()
{
return $this->city;
}

/**
* @param mixed $city
* @param string|null $city
*
* @return Company
*/
public function setCity($city)
{
$this->isChanged('companycity', $city);
$this->city = $city;

return $this;
}

/**
* @return mixed
* @return string|null
*/
public function getState()
{
return $this->state;
}

/**
* @param mixed $state
* @param string|null $state
*
* @return Company
*/
public function setState($state)
{
$this->isChanged('companystate', $state);
$this->state = $state;

return $this;
}

/**
* @return mixed
* @return string|null
*/
public function getZipcode()
{
return $this->zipcode;
}

/**
* @param mixed $zipcode
* @param string|null $zipcode
*
* @return Company
*/
public function setZipcode($zipcode)
{
$this->isChanged('companyzipcode', $zipcode);
$this->zipcode = $zipcode;

return $this;
}

/**
* @return mixed
* @return string|null
*/
public function getCountry()
{
return $this->country;
}

/**
* @param mixed $country
* @param string|null $country
*
* @return Company
*/
public function setCountry($country)
{
$this->isChanged('companycountry', $country);
$this->country = $country;

return $this;
}

/**
* @return mixed
* @return string|null
*/
public function getWebsite()
{
return $this->website;
}

/**
* @param mixed $website
* @param string|null $website
*
* @return Company
*/
public function setWebsite($website)
{
$this->isChanged('companywebsite', $website);
$this->website = $website;

return $this;
}

/**
* @return mixed
* @return string|null
*/
public function getIndustry()
{
return $this->industry;
}

/**
* @param mixed $industry
* @param string|null $industry
*
* @return Company
*/
public function setIndustry($industry)
{
$this->isChanged('companyindustry', $industry);
$this->industry = $industry;

return $this;
}

/**
* @return mixed
* @return string|null
*/
public function getDescription()
{
return $this->description;
}

/**
* @param mixed $description
* @param string|null $description
*
* @return Company
*/
public function setDescription($description)
{
$this->isChanged('companydescription', $description);
$this->description = $description;

return $this;
Expand Down
Loading

0 comments on commit 01007a0

Please sign in to comment.