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

if (count($WorkFlowReturnArray['Errors']) == 0 && count ($WorkFlowReturnArray['RuleErrors']) == 0)
{
				$WorkFlowReturnArray['ApprovalID'] = 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);
								
								//we need to add code so we can retreive the info and approve it
								$approval_column_values = array();
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_COLLECTION_ID)] = $CollectionID;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_REFERENCE_ID)] = $inserted_id;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_CHANGED_DATA_ID)] = 0;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_ACTION_ID)] = COLLECTION_WORKFLOW_APPLY_IN_ADMIN_ADD;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_USER_ID)] = $UserID;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_TIMESTAMP)] = date(time());
								
								$mainApproveId = $this->insertCollectionRow_(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, $approval_column_values , '' , '');
																
								//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;
								$WorkFlowReturnArray['ApprovalID'] = $mainApproveId;
				
				  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)
								{
										
										//we need to check if there was an edit made before to this row by the same user
										$qTest = "SELECT * FROM " . $this-> _getCollectionTableName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID) . " WHERE 
											 	".$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_CHANGED_DATA_ID)." = '".$EditID."'
												AND ".$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_USER_ID)." = '".$UserID."'
										     ";
											
										$testRes = $this->db->query($qTest);	 
										
										if ($testRes -> numRows() == 0)
										{
										
															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);
															
															//we need to add code so we can retreive the info and approve it
															$approval_column_values = array();
															$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_COLLECTION_ID)] = $CollectionID;
															$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_REFERENCE_ID)] = $inserted_id;
															$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_CHANGED_DATA_ID)] = $EditID;
															$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_ACTION_ID)] = COLLECTION_WORKFLOW_APPLY_IN_ADMIN_EDIT;
															$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_USER_ID)] = $UserID;
															$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_TIMESTAMP)] = date(time());
															
															$mainApproveId = $this->insertCollectionRow_(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, $approval_column_values , '' , '');
																							
															//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 do not delete the xrefs because we create a new one and we do not want to erase the old ones
																		//$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 not delete any of the entries
																								//$this->deleteCollectionRow_($value, $v);
																							}
																							else
																							{
																								
																								if ($v == 0)
																								{
																									
																									if ($del == 0)
																									{
																									  $this->insertCollectionRow_($value, $XrefValues , $UploadDirectory , $UploadPrefix);	
																									}
																								}
																								else
																								{
																									
																									$this->insertCollectionRow_($value, $XrefValues , $UploadDirectory , $UploadPrefix);	
																								}
																							}//end if delete =0
																							
																					}//end foreach
																					
																				}
																				
																				
																			}
																										
																}//end complex xref
																
																
																	
															}//edn if save xrefs
															
															$WorkFlowReturnArray['ApprovalID'] = $mainApproveId;
															
											}
											else
											{
													//in this case we need to edit the row that was already pending
													$pendingRow = $testRes -> fetchRow(MDB2_FETCHMODE_ASSOC);
													$editPendingRowId = $pendingRow[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_REFERENCE_ID)];
													
													//we simply edit the row
													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, $editPendingRowId, $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, $editPendingRowId, $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 = $editPendingRowId;
															
															$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 = $editPendingRowId;														
																	
																	foreach ($tmpCollectionsXRefWith as $key => $collection_value)
																	{
																		$value = $collection_value['collectionComplexXref'];
																		
																		$taxonomyXref = $this->getCollectionTaxonomy($value);
																		
																		if (count($SubmittedData['ExtAttribute_'.$value.'_ID']) > 0)
																		{
																			
																			$this -> deleteCollectionXrefs ($value , $editPendingRowId);
																			
																			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;
																					
																					
																					$this->insertCollectionRow_($value, $XrefValues , $UploadDirectory , $UploadPrefix);	
																						

																					
																			}//end foreach
																			
																		}
																		
																		
																	}
																								
														}//end complex xref
														
														
													}//edn if save xrefs
													
													
													$WorkFlowReturnArray['ApprovalID'] = $pendingRow[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, 'ID')];;
													$WorkFlowReturnArray['ApprovalNoEmail'] = 1;
											} //end if desire to edit twice the same row by the same user
								
								
								$WorkFlowReturnArray['EditedID'] = $inserted_id;
								
								
								}//end if we have edit id
				  
				  
				  break; //end admin edit specific code
				case COLLECTION_WORKFLOW_APPLY_IN_ADMIN_DELETE:
				  				
								$approval_column_values = array();
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_COLLECTION_ID)] = $CollectionID;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_REFERENCE_ID)] = $DeleteID;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_CHANGED_DATA_ID)] = $DeleteID;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_ACTION_ID)] = COLLECTION_WORKFLOW_APPLY_IN_ADMIN_DELETE;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_USER_ID)] = $UserID;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_TIMESTAMP)] = date(time());
							
								$mainApproveId = $this->insertCollectionRow_(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, $approval_column_values , '' , '');
								
								$WorkFlowReturnArray['DeletedID'] = $DeleteID;
								$WorkFlowReturnArray['ApprovalID'] = $mainApproveId;				
								  			
				  
				  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"]);
								}	
					
							}
							
							$inserted_id = $this->insertCollectionRow_($CollectionID, $column_values , $UploadDirectory , $UploadPrefix);
							
								$approval_column_values = array();
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_COLLECTION_ID)] = $CollectionID;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_REFERENCE_ID)] = $inserted_id;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_CHANGED_DATA_ID)] = 0;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_ACTION_ID)] = COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_ADD;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_USER_ID)] = $UserID;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_TIMESTAMP)] = date(time());
							
								$mainApproveId = $this->insertCollectionRow_(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, $approval_column_values , '' , '');
								
							$WorkFlowReturnArray['InsertedID'] = $inserted_id;
							$WorkFlowReturnArray['ApprovalID'] = $mainApproveId;
				
				  break;  //end front end add specific code
				case COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_EDIT:
				  
				  						//we need to check if there was an edit made before to this row by the same user
										$qTest = "SELECT * FROM " . $this-> _getCollectionTableName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID) . " WHERE 
											 	".$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_CHANGED_DATA_ID)." = '".$EditID."'
												AND ".$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_USER_ID)." = '".$UserID."'
										     ";
										$testRes = $this->db->query($qTest);	 
										
										if ($testRes -> numRows() == 0)
										{	
							
													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"]);
														}	
											
													}
													
													$inserted_id = $this->insertCollectionRow_($CollectionID, $column_values , $UploadDirectory , $UploadPrefix);
													
														$approval_column_values = array();
														$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_COLLECTION_ID)] = $CollectionID;
														$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_REFERENCE_ID)] = $inserted_id;
														$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_CHANGED_DATA_ID)] = $EditID;
														$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_ACTION_ID)] = COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_EDIT;
														$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_USER_ID)] = $UserID;
														$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_TIMESTAMP)] = date(time());
													
														$mainApproveId = $this->insertCollectionRow_(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, $approval_column_values , '' , '');
													
													
													$WorkFlowReturnArray['ApprovalID'] = $mainApproveId;
													
										}
										else
										{
												$pendingRow = $testRes -> fetchRow(MDB2_FETCHMODE_ASSOC);
												$editPendingRowId = $pendingRow[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_REFERENCE_ID)];
												
												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"]);
													}	
										
												}
							
												$this->updateCollectionRow_($CollectionID, $editPendingRowId , $column_values , $UploadDirectory , $UploadPrefix);
													
										
												$WorkFlowReturnArray['ApprovalID'] = $pendingRow[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, 'ID')];;
												$WorkFlowReturnArray['ApprovalNoEmail'] = 1;
										}			
													
										$WorkFlowReturnArray['EditedID'] = $EditID;	
													
				  
				  break; //end front end edit specific code
				case COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_DELETE:
				  			
							$approval_column_values = array();
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_COLLECTION_ID)] = $CollectionID;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_REFERENCE_ID)] = $DeleteID;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_CHANGED_DATA_ID)] = $DeleteID;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_ACTION_ID)] = COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_DELETE;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_USER_ID)] = $UserID;
								$approval_column_values[$this-> _getCollectionFieldName(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, COLLECTION_WORKFLOW_APPROVE_TABLE_TIMESTAMP)] = date(time());
							
								$mainApproveId = $this->insertCollectionRow_(COLLECTION_WORKFLOW_APPROVE_TABLE_ID, $approval_column_values , '' , '');
							
							$WorkFlowReturnArray['DeletedID'] = $DeleteID;
				  			$WorkFlowReturnArray['ApprovalID'] = $mainApproveId;
				  
				  break;   //end front end delete specific code
				default:
				  
				  // we will not run any code here
				  
				} 
				
		if ($WorkFlowReturnArray['ApprovalID'] > 0 && $WorkFlowReturnArray['ApprovalNoEmail'] != 1)
		{
				//we are going to send the approval email
				$args = array();
				$args['changeid'] 	= $WorkFlowReturnArray['ApprovalID'];
				$args['settingid'] 	= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 'ID')];
				
				$EmailFrom 			= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 5)];
				$EmailFromName 		= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 6)];
				$EmailSubject		= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 7)];
				$EmailTemplate		= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 9)];
				$SendEmailTo		= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 4)];
				$EmailToName		= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 10)];
				
				require_once("class.phpmailer.php");	
								
				$mail = new PHPMailer();
				$mail->From     = $EmailFrom;
				$mail->FromName = $EmailFromName;
				$mail->Subject  = $EmailSubject;
				
				$page_id = $EmailTemplate;
														
				$smarty -> assign ("args" , $args);
				
				$arr = array ('id' => $page_id, 'display' => 1);
				$Content = print_page_data($arr, $smarty);
				
				$mail->IsHTML (true);
				$mail->AltBody = $Content;
				$mail->Body    = $Content;
				
				$mail->AddAddress($EmailToName." <".$SendEmailTo.">");			
				$mail->Send();
				$mail->ClearAddresses();
		}
}