//workflow for sending email

//we will only send email if there are no errors

if (count($WorkFlowReturnArray['Errors']) == 0 && count ($WorkFlowReturnArray['RuleErrors']) == 0)
{

		switch ($ApplyFor)
		{
		case COLLECTION_WORKFLOW_APPLY_IN_ADMIN_ADD:
		
		
		
		  break;  //end admin add specific code
		case COLLECTION_WORKFLOW_APPLY_IN_ADMIN_EDIT:
		  
		  
		  
		  break; //end admin edit specific code
		case COLLECTION_WORKFLOW_APPLY_IN_ADMIN_DELETE:
		  
		  
		  
		  break;  //end admin delete specific code
		case COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_ADD:
		
					//we send email only if data was succesfuly saved
							if ($WorkFlowReturnArray['InsertedID'] > 0)
							{
							
										
										$EmailFrom 			= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 7)];
										$EmailFromName 		= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 6)];
										$EmailHost 			= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 8)];
										$EmailSubject		= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 9)];
										$EmailTemplate		= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 10)];
										$SendEmailTo		= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 5)];
										$EmailHTML			= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 11)];
										$EmailToName		= $WorkFlow['WorkflowSetting'][$this->_getCollectionFieldName($WFlowCollection, 4)];
										
										require_once("class.phpmailer.php");	
				
										$mail = new PHPMailer();
										$mail->From     = $EmailFrom;
										$mail->FromName = $EmailFromName;
										$mail->Host     = $EmailHost;
										$mail->Subject  = $EmailSubject;
										
										$page_id = $EmailTemplate;
										
										$collection_table = $this->_getCollectionTableName($CollectionID);
										$query  = "SELECT * FROM $collection_table WHERE ". COLLECTION_TABLE_FIELD_PREFIX . $CollectionID ."_ID='".$WorkFlowReturnArray['InsertedID']."'";
										$res =  $this->db->query($query);
										$row =  $res -> fetchRow(MDB2_FETCHMODE_ASSOC);
										
										$i = 0;
										foreach ($taxonomy as $key => $v )
										{
											$prefix = COLLECTION_TABLE_FIELD_PREFIX . $CollectionID. "_" . $v['CollectionFieldId'];
											if ($v['CollectionInputType'] != 7)
											{
													if ($v['CollectionInputType'] == 2)
													{
														
														$emailArray[] = array ( "taxonomy" => $v , "value" => nl2br($column_values[$prefix]) , "lookups" => $lookups[$prefix]);
													}
													else
													{
														$emailArray[] = array ( "taxonomy" => $v , "value" => $column_values[$prefix] , "lookups" => $lookups[$prefix]);
													}
											}
											else
											{
													if (stripos($row[$prefix], 'http') !== 0) 
													{
													   $emailArray[] = array ( "taxonomy" => $v , "value" => "http://" . $_SERVER['HTTP_HOST'] . $row[$prefix] , "lookups" => $lookups[$prefix] );
													}
													else
													{
														$emailArray[] = array ( "taxonomy" => $v , "value" => $row[$prefix] , "lookups" => $lookups[$prefix] );
													}


													
											}
										
										}
										
										$tmp = $this->getCollection($CollectionID);
										$smarty -> assign ("Collection" , $tmp[0]);
										$smarty -> assign ("EmailData" , $emailArray);
										
										$arr = array ('id' => $page_id, 'display' => 1);
										$Content = print_page_data($arr, $smarty);
										
										if ($EmailHTML == 'on')
										{
											$mail->IsHTML (true);
											$mail->AltBody = $Content;
										}
										else
										{
											$mail->IsHTML (false);
										}	
										
										$mail->Body    = $Content;
										
										$tmpEmailAddressesNames = explode(',',$EmailToName);
										$tmpEmailAddresses = explode(',',$SendEmailTo);
										
										foreach ($tmpEmailAddresses as $k => $v)
										{
												$mail->AddAddress($v , $tmpEmailAddressesNames[$k]);		
										
										}
										
												
										$mail->Send();
										$mail->ClearAddresses();
							
							
							}
		
		  break;  //end front end add specific code
		case COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_EDIT:
		  
		  
		  
		  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
		  
		} 



} //end workflow for sending email