Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update "Custom field is used" message #7157

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/bundles/CoreBundle/Translations/en_US/flashes.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ mautic.core.notice.batch_deleted="%count% items deleted"
mautic.core.notice.created="<a href='%url%' data-toggle='ajax' data-menu-link='%menu_link%'><strong>%name%</strong></a> has been created!"
mautic.core.notice.deleted="<strong>%name%</strong> has been deleted!"
mautic.core.notice.updated="<a href='%url%' data-toggle='ajax' data-menu-link='%menu_link%'><strong>%name%</strong></a> has been updated!"
mautic.core.notice.used.field="Field %name% (#%id%) is used and cannot be deleted."
mautic.core.notice.used.fields="Some fields are used and cannot be deleted."
mautic.core.notice.used.field="Field "%name%" (#%id%) cannot be deleted because it's used in the following Segment(s): %segments%."
mautic.core.notice.used.fields="Field(s) "%fields%" cannot be deleted because they are used in the following Segment(s): %segments%."
mautic.core.language.helper.error.fetching.package="An error occurred while downloading the language package."
mautic.core.language.helper.error.follow.redirects="Whoops, either safe_mode or open_basedir is turned on. Download the language from <a href='%url%'>here</a>. Unzip and upload it to the /translations directory."
mautic.core.language.helper.invalid.language="Requested language '%language%' does not exist among the available Mautic languages. The language was reset to the default one."
Expand Down
32 changes: 28 additions & 4 deletions app/bundles/LeadBundle/Controller/FieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,19 @@ public function deleteAction($objectId)
return $this->accessDenied();
}

if ($model->isUsedField($field)) {
$segments = [];
foreach ($model->getFieldSegments($field) as $segment) {
$segments[] = sprintf('"%s" (%d)', $segment->getName(), $segment->getId());
}

if (count($segments)) {
$flashMessage = [
'type' => 'error',
'msg' => 'mautic.core.notice.used.field',
'msgVars' => [
'%name%' => $field->getLabel(),
'%id%' => $objectId,
'%name%' => $field->getLabel(),
'%id%' => $objectId,
'%segments%' => implode(', ', $segments),
],
];
} else {
Expand Down Expand Up @@ -497,16 +503,34 @@ public function batchDeleteAction()
// Delete everything we are able to
if (!empty($deleteIds)) {
$filteredDeleteIds = $model->filterUsedFieldIds($deleteIds);
$usedFieldIds = array_diff($deleteIds, $filteredDeleteIds);
$segments = [];
$usedFieldsNames = [];

if ($usedFieldIds) {
// Iterating through all used fileds to get segments they are used in
foreach ($usedFieldIds as $usedFieldId) {
$fieldEntity = $model->getEntity($usedFieldId);
foreach ($model->getFieldSegments($fieldEntity) as $segment) {
$segments[$segment->getId()] = sprintf('"%s" (%d)', $segment->getName(), $segment->getId());
$usedFieldsNames[] = sprintf('"%s"', $fieldEntity->getName());
}
}
}

if ($filteredDeleteIds !== $deleteIds) {
$flashes[] = [
'type' => 'error',
'msg' => 'mautic.core.notice.used.fields',
'msgVars' => [
'%segments%' => implode(', ', $segments),
'%fields%' => implode(', ', array_unique($usedFieldsNames)),
],
];
}

if (count($filteredDeleteIds)) {
$entities = $model->deleteEntities($deleteIds);
$entities = $model->deleteEntities($filteredDeleteIds);

$flashes[] = [
'type' => 'notice',
Expand Down
12 changes: 12 additions & 0 deletions app/bundles/LeadBundle/Model/FieldModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,18 @@ public function isUsedField(LeadField $field)
return $this->leadListModel->isFieldUsed($field);
}

/**
* Returns list of all segments that use $field.
*
* @param LeadField $field
*
* @return \Doctrine\ORM\Tools\Pagination\Paginator
*/
public function getFieldSegments(LeadField $field)
{
return $this->leadListModel->getFieldSegments($field);
}

/**
* Filter used field ids.
*
Expand Down
9 changes: 8 additions & 1 deletion app/bundles/LeadBundle/Model/ListModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,13 @@ public function getSegmentContactsLineChartData($unit, \DateTime $dateFrom, \Dat
* @return bool
*/
public function isFieldUsed(LeadField $field)
{
$segments = $this->getFieldSegments($field);

return 0 < $segments->count();
}

public function getFieldSegments(LeadField $field)
{
$alias = $field->getAlias();
$aliasLength = mb_strlen($alias);
Expand All @@ -1733,6 +1740,6 @@ public function isFieldUsed(LeadField $field)
],
];

return $this->getEntities(['filter' => $filter])->count() !== 0;
return $this->getEntities(['filter' => $filter]);
}
}