@@ -128,14 +128,11 @@ public function migrate()
128128 // Save ucmSubForm records
129129 if (!empty ($ ucmSubFormDataSet ))
130130 {
131- $ itemFormModel = BaseDatabaseModel::getInstance ('ItemForm ' , 'TjucmModel ' , array ('ignore_request ' => true ));
132-
133131 // Set ucm type id to check permission in Item form model save() for logged-in user
134132 $ ucmTypeId = $ tjUcmModelType ->getTypeId ($ ucmSubFormClient );
135- $ itemFormModel ->setState ('ucmType.id ' , $ ucmTypeId );
136133
137134 // Call method to save ucmsubform data into new UCM data
138- $ subFormContentIds = $ itemFormModel ->saveUcmSubFormRecords ($ validData , $ ucmSubFormDataSet );
135+ $ subFormContentIds = $ this ->saveUcmSubFormRecords ($ validData , $ ucmSubFormDataSet, $ ucmTypeId );
139136
140137 // To update existing ucm subform field value from JSON to subform ucm type name
141138 if ($ subFormContentIds )
@@ -163,4 +160,104 @@ public function migrate()
163160
164161 return $ result ;
165162 }
163+
164+ /**
165+ * Function to save ucmSubForm records
166+ *
167+ * @param ARRAY &$validData Parent record data
168+ * @param ARRAY $ucmSubFormDataSet ucmSubForm records data
169+ * @param ARRAY $ucmTypeId UCM Type Id
170+ *
171+ * @return ARRAY
172+ */
173+ public function saveUcmSubFormRecords (&$ validData , $ ucmSubFormDataSet , $ ucmTypeId )
174+ {
175+ $ db = JFactory::getDbo ();
176+ $ subFormContentIds = array ();
177+ $ isNew = empty ($ validData ['id ' ]) ? 1 : 0 ;
178+
179+ // Delete removed subform details
180+ if (!$ isNew )
181+ {
182+ $ query = $ db ->getQuery (true );
183+ $ query ->select ('id ' );
184+ $ query ->from ($ db ->quoteName ('#__tj_ucm_data ' ));
185+ $ query ->where ($ db ->quoteName ('parent_id ' ) . '= ' . $ validData ['id ' ]);
186+ $ db ->setQuery ($ query );
187+ $ oldSubFormContentIds = $ db ->loadColumn ();
188+ }
189+
190+ JLoader::import ('components.com_tjfields.tables.fieldsvalue ' , JPATH_ADMINISTRATOR );
191+ JLoader::import ('components.com_tjfields.tables.field ' , JPATH_ADMINISTRATOR );
192+ JModelLegacy::addIncludePath (JPATH_ADMINISTRATOR . '/components/com_tjucm/models ' );
193+ $ tjUcmModelType = JModelLegacy::getInstance ('Type ' , 'TjucmModel ' );
194+ $ itemFormModel = BaseDatabaseModel::getInstance ('ItemForm ' , 'TjucmModel ' , array ('ignore_request ' => true ));
195+ $ itemFormModel ->setState ('ucmType.id ' , $ ucmTypeId );
196+
197+ if (!empty ($ ucmSubFormDataSet ))
198+ {
199+ foreach ($ ucmSubFormDataSet as $ client => $ ucmSubFormTypeData )
200+ {
201+ $ validData ['client ' ] = $ client ;
202+ $ validData ['type_id ' ] = $ tjUcmModelType ->getTypeId ($ client );
203+ $ clientDetail = explode ('. ' , $ client );
204+
205+ // This is an extra field which is used to render the reference of the ucmsubform field on the form (used in case of edit)
206+ $ ucmSubformContentIdFieldName = $ clientDetail [0 ] . '_ ' . $ clientDetail [1 ] . '_ ' . 'contentid ' ;
207+ $ count = 0 ;
208+
209+ foreach ($ ucmSubFormTypeData as $ ucmSubFormData )
210+ {
211+ $ validData ['id ' ] = isset ($ ucmSubFormData [$ ucmSubformContentIdFieldName ]) ? (int ) $ ucmSubFormData [$ ucmSubformContentIdFieldName ] : 0 ;
212+
213+ // Unset extra data
214+ $ sfFieldName = $ ucmSubFormData ['ucmSubformFieldName ' ];
215+ unset($ ucmSubFormData ['ucmSubformFieldName ' ]);
216+ $ ucmSubformContentFieldElementId = 'jform[ ' . $ sfFieldName . '][ ' . $ sfFieldName . $ count . '][ ' . $ ucmSubformContentIdFieldName . '] ' ;
217+ $ count ++;
218+
219+ if ($ insertedId = $ itemFormModel ->save ($ validData , $ ucmSubFormData ))
220+ {
221+ $ validData ['id ' ] = $ insertedId ;
222+ $ subFormContentIds [] = array ('elementName ' => $ ucmSubformContentFieldElementId , 'content_id ' => $ insertedId );
223+ $ ucmSubFormData [$ ucmSubformContentIdFieldName ] = $ insertedId ;
224+
225+ // Get field id of contentid field
226+ $ fieldTable = JTable::getInstance ('Field ' , 'TjfieldsTable ' , array ('dbo ' , $ db ));
227+ $ fieldTable ->load (array ('name ' => $ ucmSubformContentIdFieldName ));
228+
229+ // Add-Update the value of content id field in the fields value table - start
230+ $ fieldsValueTable = JTable::getInstance ('Fieldsvalue ' , 'TjfieldsTable ' , array ('dbo ' , $ db ));
231+ $ fieldsValueTable ->load (array ('field_id ' => $ fieldTable ->id , 'content_id ' => $ insertedId , 'client ' => $ validData ['client ' ]));
232+
233+ if (empty ($ fieldsValueTable ->id ))
234+ {
235+ $ fieldsValueTable ->field_id = $ fieldTable ->id ;
236+ $ fieldsValueTable ->value = $ fieldsValueTable ->content_id = $ insertedId ;
237+ $ fieldsValueTable ->client = $ validData ['client ' ];
238+ }
239+
240+ $ fieldsValueTable ->user_id = JFactory::getUser ()->id ;
241+ $ fieldsValueTable ->store ();
242+
243+ // Add-Update the value of content id field in the fields value table - end
244+ }
245+ }
246+ }
247+ }
248+
249+ // Delete removed ucmSubForm record from the form
250+ if (!empty ($ oldSubFormContentIds ))
251+ {
252+ foreach ($ oldSubFormContentIds as $ oldSubFormContentId )
253+ {
254+ if (array_search ($ oldSubFormContentId , array_column ($ subFormContentIds , 'content_id ' )) === false )
255+ {
256+ $ itemFormModel ->delete ($ oldSubFormContentId );
257+ }
258+ }
259+ }
260+
261+ return $ subFormContentIds ;
262+ }
166263}
0 commit comments