diff --git a/app/bundles/CoreBundle/Translations/en_US/flashes.ini b/app/bundles/CoreBundle/Translations/en_US/flashes.ini
index 20a60784bba..62e22db7895 100644
--- a/app/bundles/CoreBundle/Translations/en_US/flashes.ini
+++ b/app/bundles/CoreBundle/Translations/en_US/flashes.ini
@@ -11,8 +11,8 @@ mautic.core.notice.batch_deleted="%count% items deleted"
mautic.core.notice.created="%name% has been created!"
mautic.core.notice.deleted="%name% has been deleted!"
mautic.core.notice.updated="%name% 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 here. 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."
diff --git a/app/bundles/LeadBundle/Controller/FieldController.php b/app/bundles/LeadBundle/Controller/FieldController.php
index cdc16262665..c5cac1e4c00 100644
--- a/app/bundles/LeadBundle/Controller/FieldController.php
+++ b/app/bundles/LeadBundle/Controller/FieldController.php
@@ -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 {
@@ -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',
diff --git a/app/bundles/LeadBundle/Model/FieldModel.php b/app/bundles/LeadBundle/Model/FieldModel.php
index 6c583099b6b..abaaa787aba 100644
--- a/app/bundles/LeadBundle/Model/FieldModel.php
+++ b/app/bundles/LeadBundle/Model/FieldModel.php
@@ -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.
*
diff --git a/app/bundles/LeadBundle/Model/ListModel.php b/app/bundles/LeadBundle/Model/ListModel.php
index f951bac54a3..1a7ebeb4bda 100644
--- a/app/bundles/LeadBundle/Model/ListModel.php
+++ b/app/bundles/LeadBundle/Model/ListModel.php
@@ -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);
@@ -1733,6 +1740,6 @@ public function isFieldUsed(LeadField $field)
],
];
- return $this->getEntities(['filter' => $filter])->count() !== 0;
+ return $this->getEntities(['filter' => $filter]);
}
}