//workflow for rules

//first we will apply the rules that apply to all cases

$error = NULL;
$errors = array();
$ruleErrors = array();

if (count($_FILES) > 0)
{
		//we start calculating the size that the uploaded files will occupy
		$totalSize = 0;
		foreach ($_FILES as $filek => $filev)
		{
			$totalSize = $totalSize + $_FILES[$filek]["size"];
		}	
		
		
		exec('du -c '.$_SERVER['DOCUMENT_ROOT'].' | grep total', $retval, $rval);
				
		$last_line 			= explode(" ",$retval[0]);
		$totalSizeOccupied 	= intval($last_line[0]) * 1000;	
				
		$companyInfoCollection = 12;
		$crow = $this -> getFirstRow($companyInfoCollection);			
		
		$quotadisk = intval($crow[$this->_getCollectionFieldName($companyInfoCollection,20)]);
		
		$aaaa = ( $totalSizeOccupied + $totalSize ) / 1000000;
				
		if ($quotadisk < $aaaa && $quotadisk > 0)
				$ruleErrors[] 		= "Disk Quota Exceeded. Please talk with the hosting administrator to increase it. ";

}


switch ($ApplyFor)
{
case COLLECTION_WORKFLOW_APPLY_IN_ADMIN_ADD:
			
			
			foreach ($taxonomy  as $record)
			{
					$column_name = $this->_getCollectionFieldName($CollectionID, $record["CollectionFieldId"]);
					$error = $this->verifyFieldValue($CollectionID, $SubmittedData[$column_name], $record);
							
					if($error != NULL)
					{
						$errors[$record["CollectionFieldId"]] = 1;
					}
					
					if (REMOTE_STORAGE == false)
					{							
							if ($_FILES[$column_name]['error'] != UPLOAD_ERR_OK && $res[$column_name] == '' && $record["CollectionInputType"] == 7 && $record["CollectionFieldRequired"] == 1)
							{
									$errors[$record["CollectionFieldId"]] = 1;
							}
							
					}		
					else
					{	
						if ($record["CollectionInputType"] == 7 && $record["CollectionFieldRequired"] == 1 && $SubmittedData[$column_name] == '')
						{
							$errors[$record["CollectionFieldId"]] = 1;
						}	
						
					}
							
			}


  break;  //end admin add specific code
case COLLECTION_WORKFLOW_APPLY_IN_ADMIN_EDIT:
  			
			$res 	= $this-> getCollectionSingleRow (array('collection_id' =>$CollectionID , 'row_id' => $EditID));
			
			foreach ($taxonomy  as $record)
			{
				$column_name = $this->_getCollectionFieldName($CollectionID, $record["CollectionFieldId"]);
				
				if ($_FILES[$column_name]['error'] == UPLOAD_ERR_NO_FILE && $res[$column_name] == '' && $record["CollectionInputType"] == 7 && $record["CollectionFieldRequired"] == 1)
				{
					$errors[$record["CollectionFieldId"]] = 1;
				}
				
				if (REMOTE_STORAGE == true && $record["CollectionInputType"] == 7 && $record["CollectionFieldRequired"] == 1 && $SubmittedData[$column_name] == '')
				{		
						$errors[$record["CollectionFieldId"]] = 1;
				}
				
				if (isset($SubmittedData[$column_name]) || $_FILES[$column_name]['error'] == UPLOAD_ERR_OK )
				{
				
					$error = $this->verifyFieldValue($CollectionID, $SubmittedData[$column_name], $record);		
					if($error != NULL)
					{
						$errors[$record["CollectionFieldId"]] = 1;
					}
					
				}
			}
			
  
  
  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:
			
			
			
			foreach ($taxonomy  as $record)
			{
				$column_name = $this->_getCollectionFieldName($CollectionID, $record["CollectionFieldId"]);
				$error = $this->verifyFieldValue($CollectionID, $SubmittedData[$column_name], $record);
							
				if ($record['CollectionFieldRequired'] == 1 && $SubmittedData[$column_name] == "")
				{
					if ($record["CollectionInputType"] == 7 )
					{
						$errorupload = $_FILES[$column_name]['error'];
						if ($errorupload != UPLOAD_ERR_OK && $res[$column_name] == '')
						{
							$errors[$record["CollectionFieldId"]] = 1;
						}
						
					}
					else
					{
						$errors[$record["CollectionFieldId"]] = 1;
					}	
				}
				
				
				
				
				
				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
				{
					
				}	
	
			}


  break;  //end front end add specific code
case COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_EDIT:
  				
			$res 	= $this-> getCollectionSingleRow (array('collection_id' =>$CollectionID , 'row_id' => $EditID));
			
			foreach ($taxonomy  as $record)
			{
				$column_name = $this->_getCollectionFieldName($CollectionID, $record["CollectionFieldId"]);
				$error = $this->verifyFieldValue($CollectionID, $SubmittedData[$column_name], $record);
							
				if ($record['CollectionFieldRequired'] == 1 && $SubmittedData[$column_name] == "")
				{
					if ($record["CollectionInputType"] == 7 )
					{
						$errorupload = $_FILES[$column_name]['error'];
						if ($errorupload != UPLOAD_ERR_OK && $res[$column_name] == '')
						{
							$errors[$record["CollectionFieldId"]] = 1;
						}
					}
					else
					{
						if ($record["CollectionInputType"] == 3 )
						{
						
						}
						else
						{
							$errors[$record["CollectionFieldId"]] = 1;
						}	
					}	
				}
				
				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
				{
					
				}	
	
			}	
  
  
  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
  
} 




			$collection_table 	= $this->_getCollectionTableName(COLLECTION_RULES);
			$addName 			= $this->_getCollectionFieldName(COLLECTION_RULES, COLLECTION_RULES_ADD);
			$deleteName 		= $this->_getCollectionFieldName(COLLECTION_RULES, COLLECTION_RULES_DELETE);
			$editName 			= $this->_getCollectionFieldName(COLLECTION_RULES, COLLECTION_RULES_EDIT);
			$collectionName		= $this->_getCollectionFieldName(COLLECTION_RULES, COLLECTION_RULES_COLLECTION);
			$pluginName			= $this->_getCollectionFieldName(COLLECTION_RULES, COLLECTION_RULES_FILE);
			$collectionAdmin	= $this->_getCollectionFieldName(COLLECTION_RULES, COLLECTION_RULES_ADMIN);
			$collectionFront	= $this->_getCollectionFieldName(COLLECTION_RULES, COLLECTION_RULES_FRONT_END);
			$collectionSort		= $this->_getCollectionFieldName(COLLECTION_RULES, COLLECTION_RULES_SORT_ORDER);

switch ($ApplyFor)
{
case COLLECTION_WORKFLOW_APPLY_IN_ADMIN_ADD:

   $q = "SELECT $pluginName as pluginName FROM $collection_table WHERE $collectionAdmin='on' and $addName='on' and $collectionName='".$CollectionID."' Order By ". $collectionSort;
   
   $UserAccessGroups = $this->GetUserGroupAccess($UserID);
   if ($SuperAdminUser || count($UserAccessGroups) > 0)
   {
			//do nothinh		   
   }
   else
   {
   		$ruleErrors[] = "You do not have right to add into this collection row.";
   }
   
  break;  //end admin add specific code
case COLLECTION_WORKFLOW_APPLY_IN_ADMIN_EDIT:
  
  $q = "SELECT $pluginName as pluginName FROM $collection_table WHERE $collectionAdmin='on' and $editName ='on' and $collectionName='".$CollectionID."' Order By ". $collectionSort;
  
  if(!$SuperAdminUser && !$this->CanUserAccess($UserID,  $CollectionID, intval($EditID)))
  {
		$ruleErrors[] = "You do not have right to edit this collection row.";
  }
  
  break; //end admin edit specific code
case COLLECTION_WORKFLOW_APPLY_IN_ADMIN_DELETE:
  
  $q = "SELECT $pluginName as pluginName FROM $collection_table WHERE $collectionAdmin='on' and $deleteName ='on' and $collectionName='".$CollectionID."' Order By ". $collectionSort;
  
  if(!$SuperAdminUser && !$this->CanUserAccess($UserID,  $CollectionID, intval($DeleteID)))
  {
		$ruleErrors[] = "You do not have right to delete this collection row.";
  }
  
  break;  //end admin delete specific code
case COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_ADD:

  $q = "SELECT $pluginName as pluginName FROM $collection_table WHERE $collectionFront='on' and $addName='on' and $collectionName='".$CollectionID."' Order By ". $collectionSort;

  break;  //end front end add specific code
case COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_EDIT:
  
  $q = "SELECT $pluginName as pluginName FROM $collection_table WHERE $collectionFront='on' and $editName ='on' and $collectionName='".$CollectionID."' Order By ". $collectionSort;
    
  break; //end front end edit specific code
case COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_DELETE:
  
  $q = "SELECT $pluginName as pluginName FROM $collection_table WHERE $collectionFront='on' and $deleteName ='on' and $collectionName='".$CollectionID."' Order By ". $collectionSort;
  
  if(!$SuperAdminUser && !$this->CanUserAccess($UserID,  $CollectionID, intval($DeleteID)))
  {
		$ruleErrors[] = "You do not have right to delete this collection row.";
  }
  
  break;   //end front end delete specific code
default:
  
  // we will not run any code here
  
} 

$Rules = $this-> db -> queryAll($q);

if ($Rules)
	foreach ( $Rules as $k => $v )
		eval($v['pluginName']);



/*
				We will also see if the uploaded files are of the allowed type but only if 
				if we are on the front end
*/

if (count($_FILES) > 0 && ( REMOTE_STORAGE == true && ( $ApplyFor == COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_ADD || $ApplyFor == COLLECTION_WORKFLOW_APPLY_IN_FRONTEND_EDIT ) ) || REMOTE_STORAGE == false )
{

	foreach ($taxonomy  as $record)
	{
				$column_name = $this->_getCollectionFieldName($CollectionID, $record["CollectionFieldId"]);
				if ( $record["CollectionInputType"] == 7)
				{
							$errorupload = $_FILES[$column_name]['error'];
							
							if ($errorupload == UPLOAD_ERR_OK )
							{	
												
												$UploadArgs = array();
												$UploadArgs['UploadedFile'] = $_FILES[$column_name];
												$FileTypeErrors = $storage->CheckIfFileIsAllowedToBeUploaded($UploadArgs);

												if (count($FileTypeErrors)> 0)
												{
														foreach ($FileTypeErrors as $ErrorKey => $ErrorValue)
														{
																$ruleErrors[] = $record["CollectionFieldName"] . " - "  . $ErrorValue;
														}
												} //end if there are errors
												
							}	//end if error upload is no error				
				}//end if field is of file type
	} //end foreach

}//end if we check file types



$WorkFlowReturnArray['Errors'] = $errors;
$WorkFlowReturnArray['RuleErrors'] = $ruleErrors;