//workflow for write to database
//first we will apply the rules that apply to all cases

if (count($WorkFlowReturnArray['Errors']) == 0 && count ($WorkFlowReturnArray['RuleErrors']) == 0)
{
				
				switch ($ApplyFor)
				{
				case COLLECTION_WORKFLOW_APPLY_IN_ADMIN_ADD:
				
								
								foreach ($taxonomy  as $record)
								{
										$column_name = $this->_getCollectionFieldName($CollectionID, $record["CollectionFieldId"]);
										$column_values[$column_name] = CollectionConverter::ConvertValue($SubmittedData[$column_name], $record["CollectionFieldType"]);											
								}
								
								//save the row in the collection
								$inserted_id = $this->insertCollectionRow_($CollectionID, $column_values , $UploadDirectory , $UploadPrefix);
								
								//save the user access levels for this row
								$UserAccessGroups = $this->GetUserGroupAccess($UserID);
								if ($SubmittedData['SelectedValuesUserGroups'] || count($UserAccessGroups) > 0)
								{
									$ugroups = array();
									foreach ($SubmittedData['SelectedValuesUserGroups'] as $saveKey => $saveItem)
									{
											$ugroups[$saveItem] = $saveItem;
									}
									
									//only super admins can update row access level
									if ($SuperAdminUser) 
									{
										$this->updateCollectionUserGroupAccessRow($CollectionID, $inserted_id, $ugroups, $UserID);
									}	
									else
									{
										$this->updateCollectionUserGroupAccessRow($CollectionID, $inserted_id, $UserAccessGroups, $UserID);
									}
								}
								
								//we need to save xrefs if they exist
								if ($collectionSettings)
								{
									//we get to see if this collection has xrefs :)
									if (count($collectionSettings[5]) > 0)
									{						
										$tmpCollectionsXRefSave  = $collectionSettings[5];
										
										foreach ($tmpCollectionsXRefSave as $key => $value)
										{
											$collectionXrefSave = $value['collectionXrefsSave'];
											$collectionXref 	= $value['collectionXrefs'];
											
											//first we delete the xrefs
											$this -> deleteCollectionXrefs ($collectionXrefSave  ,  $inserted_id);
											if ($SubmittedData['SelectedValues_'.$collectionXref])
											{
												foreach ($SubmittedData['SelectedValues_'.$collectionXref] as $saveKey => $saveItem)
												{
														$xref_column_values = array();
														
														$field1 = $this-> _getCollectionFieldName($collectionXrefSave, 1);
														$field2 = $this-> _getCollectionFieldName($collectionXrefSave, 2);
														
														$xref_column_values[$field1] = $inserted_id;
														$xref_column_values[$field2] = $saveItem;
														$res2 = $this->insertCollectionRow_($collectionXrefSave, $xref_column_values , $UploadDirectory , $UploadPrefix);
												}
											}
										}
									}
									
									if (count($collectionSettings[6]) > 0)
									{
												
												$tmpCollectionsXRefWith  = $collectionSettings[6];
												
												foreach ($tmpCollectionsXRefWith as $key => $collection_value)
												{
													$value = $collection_value['collectionComplexXref'];
													$taxonomyXref = $this->getCollectionTaxonomy($value);
													
													if (count($SubmittedData['ExtAttribute_'.$value.'_ID']) > 0)
													{
														
														foreach ($SubmittedData['ExtAttribute_'.$value.'_ID'] as $k => $v)
														{
																$XrefValues = array();
																$del = 1;
																foreach ($taxonomyXref as $kk => $vv )
																{
																	if ($vv['CollectionFieldRequired'] == 1 && $SubmittedData['ExtAttribute_'.$value.'_'.$vv['CollectionFieldId']][$k] != '')
																	{
																		$del = 0;
																	}
																	$column_name = $this->_getCollectionFieldName($value, $vv['CollectionFieldId']);
																	$XrefValues[$column_name] = $SubmittedData['ExtAttribute_'.$value.'_'.$vv['CollectionFieldId']][$k];
																	
																}//end foreach
																
																$column_name = $this->_getCollectionFieldName($value, 1);
																$XrefValues[$column_name] = $inserted_id;
																
																if ($del == 1 && $v !=0)
																{
																	//we will delete the entry
																	$this->deleteCollectionRow_($value, $v);
																}
																else
																{
																	//we will add/edit the entry
																	if ($v == 0)
																	{
																		//we will add the value
																		if ($del == 0)
																		{
																		  $this->insertCollectionRow_($value, $XrefValues , $UploadDirectory , $UploadPrefix);	
																		}
																	}
																	else
																	{
																		//we will edit the value
																		$this->updateCollectionRow_($value,  $v , $XrefValues , $UploadDirectory , $UploadPrefix);
																	}
																}//end if delete =0
																
														}//end foreach
														
													}
													
													
												}
																			
									}//end complex xref
									
									
										
								}//edn if save xrefs
								
								$WorkFlowReturnArray['InsertedID'] = $inserted_id;
				
				  break;  //end admin add specific code
				case COLLECTION_WORKFLOW_APPLY_IN_ADMIN_EDIT:
				  
				  				//we need the edit id param to be set up
								if (intval($EditID) > 0)
								{
										
										if (count($this->revisionCollections[$CollectionID]) > 0)
										{
												//add new revision with previous data
												$RevisionID = $this-> RevisionsAddRevisionForRow($CollectionID , $EditID , $UserID , COLLECTION_WORKFLOW_APPLY_IN_ADMIN_EDIT );
										}
										
										foreach ($taxonomy  as $record)
										{
											$column_name = $this->_getCollectionFieldName($CollectionID, $record["CollectionFieldId"]);
											if (isset($SubmittedData[$column_name]) || $_FILES[$column_name]['error'] == UPLOAD_ERR_OK )
											{
												$column_values[$column_name] = CollectionConverter::ConvertValue($SubmittedData[$column_name], $record["CollectionFieldType"]);
											}
										}
										
										
										
										$res = $this->updateCollectionRow_($CollectionID, $EditID, $column_values , $UploadDirectory , $UploadPrefix);
										
										//user level access is only updated if the row is edited by admins
										if ($SuperAdminUser)
										{
												if ($SubmittedData['SelectedValuesUserGroups'])
												{
													$ugroups = array();
													foreach ($SubmittedData['SelectedValuesUserGroups'] as $saveKey => $saveItem)
													{
															$ugroups[$saveItem] = $saveItem;
													}
												}
												$this->updateCollectionUserGroupAccessRow($CollectionID, $EditID, $ugroups, $UserID);	
										}
										
										
										//we need to save xrefs if they exist
										if ($collectionSettings)
										{
											//we get to see if this collection has xrefs :)
											if (count($collectionSettings[5]) > 0)
											{
												$edit_id = $EditID;
												
												$tmpCollectionsXRefSave  = $collectionSettings[5];
												
												foreach ($tmpCollectionsXRefSave as $key => $value)
												{
													$collectionXrefSave = $value['collectionXrefsSave'];
													$collectionXref 	= $value['collectionXrefs'];
													
													//first we delete the xrefs
													$this -> deleteCollectionXrefs ($collectionXrefSave , $edit_id);
													if ($SubmittedData['SelectedValues_'.$collectionXref])
													{
														foreach ($SubmittedData['SelectedValues_'.$collectionXref] as $saveKey => $saveItem)
														{
																$xref_column_values = array();
																
																$field1 = $this-> _getCollectionFieldName($collectionXrefSave, 1);
																$field2 = $this-> _getCollectionFieldName($collectionXrefSave, 2);
																
																$xref_column_values[$field1] = $edit_id;
																$xref_column_values[$field2] = $saveItem;
																
																$res = $this->insertCollectionRow_($collectionXrefSave, $xref_column_values , $UploadDirectory , $UploadPrefix);
														}
													}
												}
											}	
											
											if (count($collectionSettings[6]) > 0)
											{
											
														$tmpCollectionsXRefWith  = $collectionSettings[6];
														
														$edit_id = $EditID;														
														
														foreach ($tmpCollectionsXRefWith as $key => $collection_value)
														{
															$value = $collection_value['collectionComplexXref'];
															
															$taxonomyXref = $this->getCollectionTaxonomy($value);
															
															if (count($SubmittedData['ExtAttribute_'.$value.'_ID']) > 0)
															{
																
																if ($args['ComplexXREFDelete'] == 1)
																{
																	$this -> deleteCollectionXrefs ($value , $EditID);
																}
																
																foreach ($SubmittedData['ExtAttribute_'.$value.'_ID'] as $k => $v)
																{
																		$XrefValues = array();
																		$del = 1;
																		foreach ($taxonomyXref as $kk => $vv )
																		{
																			if ($vv['CollectionFieldRequired'] == 1 && $SubmittedData['ExtAttribute_'.$value.'_'.$vv['CollectionFieldId']][$k] != '')
																			{
																				$del = 0;
																			}
																			$column_name = $this->_getCollectionFieldName($value, $vv['CollectionFieldId']);
																			$XrefValues[$column_name] = $SubmittedData['ExtAttribute_'.$value.'_'.$vv['CollectionFieldId']][$k];
																			
																		}//end foreach
																		
																		$column_name = $this->_getCollectionFieldName($value, 1);
																		$XrefValues[$column_name] = $edit_id;
																		
																		
																																				
																		if ($del == 1 && $v !=0)
																		{
																			//we will delete the entry
																			$this->deleteCollectionRow_($value, $v);
																		}
																		else
																		{
																			//we will add/edit the entry
																			if ($v == 0)
																			{
																				//we will add the value
																				if ($del == 0)
																				{
																				  $this->insertCollectionRow_($value, $XrefValues , $UploadDirectory , $UploadPrefix);	
																				}
																			}
																			else
																			{
																				//we will edit the value
																				$this->updateCollectionRow_($value,  $v , $XrefValues , $UploadDirectory , $UploadPrefix);
																			}
																		}//end if delete =0
																		
																}//end foreach
																
															}
															
															
														}
																					
											}//end complex xref
											
											
										}//edn if save xrefs
								
								$WorkFlowReturnArray['EditedID'] = $EditID;
								
								}//end if we have edit id
				  
				  
				  break; //end admin edit specific code
				case COLLECTION_WORKFLOW_APPLY_IN_ADMIN_DELETE:
				  				
								
								if (count($this->revisionCollections[$CollectionID]) > 0)
								{
										//add new revision with previous data
										$RevisionID = $this-> RevisionsDeleteRevisionForRow($CollectionID , $DeleteID );
								}
								
								//we delete the row access levels
								$this->deleteCollectionUserGroupAccessRow($CollectionID, $DeleteID);
								//we delete the xrefs
								if (count($collectionSettings[5]) > 0)
								{
									
									$tmpCollectionsXRefSave  = $collectionSettings[5];
									
									foreach ($tmpCollectionsXRefSave as $key => $value)
									{
										$collectionXrefSave = $value['collectionXrefsSave'];
										$this -> deleteCollectionXrefs ($collectionXrefSave , $DeleteID);
									}
								}	
								if (count($collectionSettings[6]) > 0)
								{
									$tmpCollectionsXRefWith  = $collectionSettings[6];
									
									foreach ($tmpCollectionsXRefWith as $key => $value)
									{
										$this -> deleteCollectionXrefs ($value['collectionComplexXref'] , $DeleteID);
									}
								}
								//we delete the actual row
								$this->deleteCollectionRow_($CollectionID, $DeleteID);
				  			
				  
				  break;  //end admin delete specific code
				case COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_ADD:
								
							foreach ($taxonomy  as $record)
							{
								$column_name = $this->_getCollectionFieldName($CollectionID, $record["CollectionFieldId"]);
																
								if (($record['CollectionHidden'] == 1 && $SubmittedData[$column_name] =='' && $record['CollectionFormula'] == 1) || ( $_FILES[$column_name]['error'] != UPLOAD_ERR_OK) )
								{
									//do not add the field into the edit 
								}
								else
								{
									$column_values[$column_name] = CollectionConverter::ConvertValue($SubmittedData[$column_name], $record["CollectionFieldType"]);
								}	
					
							}
							
							//we need to treat the uploads in a different way if remote storage is active for front end
							include_once $_SERVER['DOCUMENT_ROOT'].'/classes/class.storage.php';
 							$storage = new StorageAPI($this);
							
							define("REMOTE_STORAGE", $storage->isRemoteStorageActive ());
							print REMOTE_STORAGE . "<hr/>"; 
							if (REMOTE_STORAGE == true)
							{
										
										//loop through every upload and get it done
										foreach ($taxonomy  as $record)
										{
												$column_name = $this->_getCollectionFieldName($CollectionID, $record["CollectionFieldId"]);
												if ($record['CollectionInputType'] == 7 && $_FILES[$column_name]['error'] == UPLOAD_ERR_OK )
												{
															$FileTypeArray = $storage->getFileTypeFromFileUpload($_FILES[$column_name]);
															$FileType = $FileTypeArray['FileType'];
															
															$ActualFileName = $_FILES[$column_name]["name"];
			
															$storageArray = array();
															$storageArray['ActualFileName'] = $ActualFileName;
															$storageArray['ActualFilePath'] = $_FILES[$column_name]["tmp_name"];
															$storageArray['FileTypeDetails']	= $FileTypeArray;
															$storageArray['UploadedFile']		= $_FILES[$column_name];
															
															//we get the default storage setting for uploads on front end
															$StorageMedia 	= intval($collectionSettings[2]['RemoteStorageType']);
															$StorageFolder 	= intval($collectionSettings[2]['RemoteStorageFolder']);
															
															$storage->setStorage($StorageMedia , $StorageFolder);
															
															$returnDetails = $storage-> SendFileToStorage ($storageArray);
															
																														
															if (count($returnDetails['errors'])> 0)
															{
																	//we do not save anything
															}
															else
															{
																	$PathToTheUploadedFile 	= $returnDetails['path'];
																	$UploadedFileName 		= $returnDetails['filename'];
																	
																	
																	$args = array();
																	$args['FileName'] = $ActualFileName;
																	$args['FileType'] = $FileType;
																	$args['PathToTheUploadedFile'] = $PathToTheUploadedFile;	
																	$args['UploadedFileName'] = $UploadedFileName;
																	
																	$FileUploadID = $storage->SaveUploadedFile($args);
																	
																	if (intval($FileUploadID) > 0)
																	{
																				//add the path to the collection save array
																				$column_values[$column_name] = $PathToTheUploadedFile;
																	}
																	else
																	{
																			//do nothing
																	}			
																	
															}
															
												}
											
										}//end foreach
										
																	
							}//end if remote storage active
							
														
							//end remote storage code
							
							$inserted_id = $this->insertCollectionRow_($CollectionID, $column_values , $UploadDirectory , $UploadPrefix);
								
							$WorkFlowReturnArray['InsertedID'] = $inserted_id;
				
				  break;  //end front end add specific code
				case COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_EDIT:
				  
				  										
							foreach ($taxonomy  as $record)
							{
								$column_name = $this->_getCollectionFieldName($CollectionID, $record["CollectionFieldId"]);
																
								if (($record['CollectionHidden'] == 1 && $SubmittedData[$column_name] =='' && $record['CollectionFormula'] == 1) || ( $_FILES[$column_name]['error'] != UPLOAD_ERR_OK) )
								{
									//do not add the field into the edit 
								}
								else
								{
									$column_values[$column_name] = CollectionConverter::ConvertValue($SubmittedData[$column_name], $record["CollectionFieldType"]);
								}	
					
							}
							
							//we need to treat the uploads in a different way if remote storage is active for front end
							include_once $_SERVER['DOCUMENT_ROOT'].'/classes/class.storage.php';
 							$storage = new StorageAPI($this);
							
							define("REMOTE_STORAGE", $storage->isRemoteStorageActive ());
							
							if (REMOTE_STORAGE == true)
							{
										
										
										//loop through every upload and get it done
										foreach ($taxonomy  as $record)
										{
												$column_name = $this->_getCollectionFieldName($CollectionID, $record["CollectionFieldId"]);
												if ($record['CollectionInputType'] == 7 && $_FILES[$column_name]['error'] == UPLOAD_ERR_OK )
												{
															$FileType = $storage->getFileTypeFromFileUpload($_FILES[$column_name]);
															$ActualFileName = $_FILES[$column_name]["name"];
			
															$storageArray = array();
															$storageArray['ActualFileName'] = $ActualFileName;
															$storageArray['ActualFilePath'] = $_FILES[$column_name]["tmp_name"];
															
															//we get the default storage setting for uploads on front end
															$StorageMedia 	= intval($collectionSettings[2]['RemoteStorageType']);
															$StorageFolder 	= intval($collectionSettings[2]['RemoteStorageFolder']);
											
															$storage->setStorage($StorageMedia , $StorageFolder);
															
															$returnDetails = $storage-> SendFileToStorage ($storageArray);
															
															
															
															if (count($returnDetails['errors'])> 0)
															{
																	//we do not save anything
															}
															else
															{
																	$PathToTheUploadedFile 	= $returnDetails['path'];
																	$UploadedFileName 		= $returnDetails['filename'];
																	
																	
																	$args = array();
																	$args['FileName'] = $ActualFileName;
																	$args['FileType'] = $FileType;
																	$args['PathToTheUploadedFile'] = $PathToTheUploadedFile;	
																	$args['UploadedFileName'] = $UploadedFileName;
																	
																	$FileUploadID = $storage->SaveUploadedFile($args);
																	
																	if (intval($FileUploadID) > 0)
																	{
																				//add the path to the collection save array
																				$column_values[$column_name] = $PathToTheUploadedFile;
																	}
																	else
																	{
																			//do nothing
																	}			
																	
															}
															
												}
											
										}//end foreach
										
																	
							}//end if remote storage active
							
							//end remote storage code
							
							$this->updateCollectionRow_($CollectionID, $EditID , $column_values , $UploadDirectory , $UploadPrefix);
							$WorkFlowReturnArray['EditedID'] = $EditID;
				  
				  break; //end front end edit specific code
				case COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_DELETE:
				  
				  			
				  
				  break;   //end front end delete specific code
				default:
				  
				  // we will not run any code here
				  
				} 
				
		
}