From fa10b8f811bf4ee82d56e20adef08672fce7eb65 Mon Sep 17 00:00:00 2001
From: Uma <uma.s@vtiger.com>
Date: Thu, 26 Sep 2019 15:52:43 +0530
Subject: [PATCH] Multi picklist Validation and save for editable picklist
 values is supported

---
 modules/Calendar/actions/Save.php       | 10 ++++++++-
 modules/SalesOrder/actions/SaveAjax.php | 20 ++++++++++++++++--
 modules/Vtiger/actions/Save.php         | 10 ++++++++-
 modules/Vtiger/actions/SaveAjax.php     | 27 +++++++++++++++++++++++--
 4 files changed, 61 insertions(+), 6 deletions(-)

diff --git a/modules/Calendar/actions/Save.php b/modules/Calendar/actions/Save.php
index da5307f8b..32cb30162 100644
--- a/modules/Calendar/actions/Save.php
+++ b/modules/Calendar/actions/Save.php
@@ -150,7 +150,15 @@ class Calendar_Save_Action extends Vtiger_Save_Action {
             if(($fieldDataType == 'picklist' || $fieldDataType == 'multipicklist' || $fieldDataType == 'multiowner') && $fieldValue !== null){
                 $fieldInfo = $fieldModel->getFieldInfo();
                 $editablePicklistValues = $fieldInfo['editablepicklistvalues'];
-                if(!empty($editablePicklistValues) && !in_array($fieldValue, $editablePicklistValues)){
+                if($fieldDataType == 'multipicklist'&& !empty($editablePicklistValues)){
+                    $selectedMultiPick = array();
+                    foreach ($fieldValue as $key => $selectedValue) {
+                        if(in_array($selectedValue, $editablePicklistValues)){
+                            $selectedMultiPick[] = $selectedValue;
+                        }
+                    }
+                    $fieldValue = $selectedMultiPick;
+                }else if( $fieldDataType != 'multipicklist' && !empty($editablePicklistValues) && !in_array($fieldValue, $editablePicklistValues)){
                     $fieldValue = null;
                 }
             }
diff --git a/modules/SalesOrder/actions/SaveAjax.php b/modules/SalesOrder/actions/SaveAjax.php
index 333268357..bcb53e74a 100755
--- a/modules/SalesOrder/actions/SaveAjax.php
+++ b/modules/SalesOrder/actions/SaveAjax.php
@@ -62,7 +62,15 @@ class SalesOrder_SaveAjax_Action extends Inventory_SaveAjax_Action {
                 if(($fieldDataType == 'picklist' || $fieldDataType == 'multipicklist' || $fieldDataType == 'multiowner') && $fieldValue !== null){
                     $fieldInfo = $fieldModel->getFieldInfo();
                     $editablePicklistValues = $fieldInfo['editablepicklistvalues'];
-                    if(!empty($editablePicklistValues) && !in_array($fieldValue, $editablePicklistValues)){
+                    if($fieldDataType == 'multipicklist'&& !empty($editablePicklistValues)){
+                        $selectedMultiPick = array();
+                        foreach ($fieldValue as $key => $selectedValue) {
+                            if(in_array($selectedValue, $editablePicklistValues)){
+                                $selectedMultiPick[] = $selectedValue;
+                            }
+                        }
+                        $fieldValue = $selectedMultiPick;
+                    }else if( $fieldDataType != 'multipicklist' && !empty($editablePicklistValues) && !in_array($fieldValue, $editablePicklistValues)){
                         $fieldValue = null;
                     }
                 }
@@ -94,7 +102,15 @@ class SalesOrder_SaveAjax_Action extends Inventory_SaveAjax_Action {
                 if(($fieldDataType == 'picklist' || $fieldDataType == 'multipicklist' || $fieldDataType == 'multiowner') && $fieldValue !== null){
                     $fieldInfo = $fieldModel->getFieldInfo();
                     $editablePicklistValues = $fieldInfo['editablepicklistvalues'];
-                    if(!empty($editablePicklistValues) && !in_array($fieldValue, $editablePicklistValues)){
+                    if($fieldDataType == 'multipicklist'&& !empty($editablePicklistValues)){
+                        $selectedMultiPick = array();
+                        foreach ($fieldValue as $key => $selectedValue) {
+                            if(in_array($selectedValue, $editablePicklistValues)){
+                                $selectedMultiPick[] = $selectedValue;
+                            }
+                        }
+                        $fieldValue = $selectedMultiPick;
+                    }else if( $fieldDataType != 'multipicklist' && !empty($editablePicklistValues) && !in_array($fieldValue, $editablePicklistValues)){
                         $fieldValue = null;
                     }
                 }
diff --git a/modules/Vtiger/actions/Save.php b/modules/Vtiger/actions/Save.php
index d106ec740..fdbf584dc 100644
--- a/modules/Vtiger/actions/Save.php
+++ b/modules/Vtiger/actions/Save.php
@@ -164,7 +164,15 @@ class Vtiger_Save_Action extends Vtiger_Action_Controller {
             if(($fieldDataType == 'picklist' || $fieldDataType == 'multipicklist' || $fieldDataType == 'multiowner') && $fieldValue !== null){
                 $fieldInfo = $fieldModel->getFieldInfo();
                 $editablePicklistValues = $fieldInfo['editablepicklistvalues'];
-                if(!empty($editablePicklistValues) && !in_array($fieldValue, $editablePicklistValues)){
+                if($fieldDataType == 'multipicklist'&& !empty($editablePicklistValues)){
+                    $selectedMultiPick = array();
+                    foreach ($fieldValue as $key => $selectedValue) {
+                        if(in_array($selectedValue, $editablePicklistValues)){
+                            $selectedMultiPick[] = $selectedValue;
+                        }
+                    }
+                    $fieldValue = $selectedMultiPick;
+                }else if( $fieldDataType != 'multipicklist' && !empty($editablePicklistValues) && !in_array($fieldValue, $editablePicklistValues)){
                     $fieldValue = null;
                 }
             }
diff --git a/modules/Vtiger/actions/SaveAjax.php b/modules/Vtiger/actions/SaveAjax.php
index 829cf8b14..2a560adaa 100644
--- a/modules/Vtiger/actions/SaveAjax.php
+++ b/modules/Vtiger/actions/SaveAjax.php
@@ -110,7 +110,15 @@ class Vtiger_SaveAjax_Action extends Vtiger_Save_Action {
                 if(($fieldDataType == 'picklist' || $fieldDataType == 'multipicklist' || $fieldDataType == 'multiowner') && $fieldValue !== null){
                     $fieldInfo = $fieldModel->getFieldInfo();
                     $editablePicklistValues = $fieldInfo['editablepicklistvalues'];
-                    if(!empty($editablePicklistValues) && !in_array($fieldValue, $editablePicklistValues)){
+                    if($fieldDataType == 'multipicklist'&& !empty($editablePicklistValues)){
+                        $selectedMultiPick = array();
+                        foreach ($fieldValue as $key => $selectedValue) {
+                            if(in_array($selectedValue, $editablePicklistValues)){
+                                $selectedMultiPick[] = $selectedValue;
+                            }
+                        }
+                        $fieldValue = $selectedMultiPick;
+                    }else if( $fieldDataType != 'multipicklist' && !empty($editablePicklistValues) && !in_array($fieldValue, $editablePicklistValues)){
                         $fieldValue = null;
                     }
                 }
@@ -140,9 +148,24 @@ class Vtiger_SaveAjax_Action extends Vtiger_Save_Action {
 					$fieldValue = $fieldModel->getDefaultFieldValue();
 				}
 				$fieldDataType = $fieldModel->getFieldDataType();
-				if ($fieldDataType == 'time') {
+				if ($fieldDataType == 'time' && $fieldValue !== null) {
 					$fieldValue = Vtiger_Time_UIType::getTimeValueWithSeconds($fieldValue);
 				}
+                if(($fieldDataType == 'picklist' || $fieldDataType == 'multipicklist' || $fieldDataType == 'multiowner') && $fieldValue !== null){
+                    $fieldInfo = $fieldModel->getFieldInfo();
+                    $editablePicklistValues = $fieldInfo['editablepicklistvalues'];
+                    if($fieldDataType == 'multipicklist'){
+                        $selectedMultiPick = array();
+                        foreach ($fieldValue as $key => $selectedValue) {
+                            if(in_array($selectedValue, $editablePicklistValues)){
+                                $selectedMultiPick[] = $selectedValue;
+                            }
+                        }
+                        $fieldValue = $selectedMultiPick;
+                    }else if( $fieldDataType != 'multipicklist' && !empty($editablePicklistValues) && !in_array($fieldValue, $editablePicklistValues)){
+                        $fieldValue = null;
+                    }
+                }
 				if ($fieldValue !== null) {
 					if (!is_array($fieldValue)) {
 						$fieldValue = trim($fieldValue);
-- 
GitLab