If you’re writing a complex piece of logic, make it easier for yourself later. I’m going to provide two code examples below and I’d like you to choose which one you’re rather work with.

Hint: There’s generally no need to nest if statements more than 1 or 2 levels deep. Use additional variables to guide your code through how to evaluate itself.

Version 1:

function isCandidateProfileVisible($params=array()) {
	
	if($this->profileVisibleData['showLinkToApplicant']===NULL) {
			
		
		$isDTstaff = $_SESSION['client']['isDTstaff'];
		$id_company = $_SESSION['client']['id_company'];
		
		$id_user = $_SESSION['client']['id_contact'];
		if($id_user=='') {
			$id_user = $_SESSION['session_id_user'];
		}
		
		if($params['isDTstaff']===true) {
			$isDTstaff = true;
		}
		
		$showLinkToApplicant = false;
		$howAccessGranted = NULL;
		
		if($isDTstaff) {
			$showLinkToApplicant = true;
			$howAccessGranted = "DT Staff";
		} else {
			if(strtolower($this->getOwner())=="client") {
				$showLinkToApplicant = true;
				$howAccessGranted = "owner=client";
			} else {
				if($this->getActivityStage()>=2) {
					$showLinkToApplicant = true;
					$howAccessGranted = "getActivityStage";
				} else {
					$currentEmployee = strtolower($this->getCurrentEmployee());
					if($currentEmployee=='yes' || $currentEmployee=='alumni') {
						$showLinkToApplicant = true;
						$howAccessGranted = "current/alumni employee";
					} else {
						$accessObj = $params['accessObject'];
						if(is_object($accessObj) && $accessObj->id!='') {
							$viewApps = $accessObj->getViewApps();
						} else {
							$query = "SELECT viewApps FROM access 
										WHERE id_company='".$id_company."' 
											AND id_contact='".$_SESSION['client']['id_contact']."'";
							$viewApps = getMySQLValue($query,true);
						}
						if(strtolower($viewApps)=="all") {
							$showLinkToApplicant = true;
							$howAccessGranted = "viewApps=all";
						} else {
							$jobObj = $params['jobObj'];
							$jobviewapps = false;
							
							$id_job = $params['id_job'];
							if(!is_object($jobObj) && $id_job!='') {
								$jobObj = new job($id_job);
							}
							
							if($id_job=='' && is_object($jobObj)) {
								$id_job = $jobObj->id;
							}
							
					
							$jobAccessObj = $params['jobAccessObj'];
							if(is_object($jobAccessObj) && $jobAccessObj->id!='') {
								$jobviewapps = $jobAccessObj->getViewApps();
							} else if($id_job!='') {
								$id_user = $_SESSION['session_id_user'];
								$query = "SELECT viewapps FROM jobaccess 
											WHERE id_job='".$id_job."' 
											AND id_contact='".$id_user."'";
								$jobviewapps = getMySQLValue($query,true);
							}
							
							if($jobviewapps!==false && strtolower($jobviewapps)=="all") {
								$showLinkToApplicant = true;
								$howAccessGranted = "jobviewapps=all";
							} else {
								$id_most_recent_job = $this->getMostRecentIDjob();
								if($id_most_recent_job>0) {
									$query = "SELECT Status_ActiveOrPending FROM jobs 
												WHERE id='".$id_most_recent_job."'";
									$Status_ActiveOrPending = getMySQLValue($query,true);
									if($Status_ActiveOrPending!='Yes') {
										$showLinkToApplicant = true;
										$howAccessGranted = "most recent job (".$id_most_recent_job.") ";
										$howAccessGranted .= "is not active or pending";
									} else {
										$query = "SELECT viewapps FROM jobaccess 
													WHERE id_job='".$id_most_recent_job."' 
														AND id_contact='".$id_user."'";
										$jobviewapps = getMySQLValue($query,true);
										if(strtolower($jobviewapps)=="all") {
											$showLinkToApplicant = true;
											$howAccessGranted = "jobviewapps=all";
										} else {
											if(is_object($jobObj) && $jobObj->id>0) {
				
												if(	strtolower($jobObj->getFlagAppAccessClient())=="view" 
													|| strtolower($jobObj->getFlagAppAccessClient())=="edit") {
													
													$showLinkToApplicant = true;
													$howAccessGranted = "getFlagAppAccessClient=";
													$howAccessGranted .= $jobObj->getFlagAppAccessClient()." on ".$jobObj->id;
												} else {
													$query = "SELECT r.id
																FROM responses AS r 
																LEFT JOIN jobs AS j ON j.id=r.id_job 
																WHERE r.id_applicant='".$this->id."'
																	AND 
																		(
																			j.flag_masterlist='client'
																			OR
																			j.title='Future Opportunities'
																		)
																LIMIT 1";
													$id_self_service_response = getMySQLValue($query,true);
													if($id_self_service_response) {
														$showLinkToApplicant = true;
														$howAccessGranted = "self service job found via response record: ";
														$howAccessGranted .= $id_self_service_response;
													} else {
														if($this->getOwner()=='') {
															$showLinkToApplicant = true;
															$howAccessGranted = "owner=NULL";
														} else {
															$query = "SELECT f.id
																		FROM flags AS f 
																		LEFT JOIN jobs AS j ON j.id=f.id_job 
																		WHERE f.id_applicant='".$this->id."'
																			AND 
																				(
																					j.flag_masterlist='client'
																					OR
																					j.title='Future Opportunities'
																				)
																		LIMIT 1";
															$id_self_service_response = getMySQLValue($query,true);
															if($id_self_service_response) {
																$showLinkToApplicant = true;
																$howAccessGranted = "self service job found via flag record: ";
																$howAccessGranted .= $id_self_service_response;
															} else {
																if($this->getIDreferrer()=='15') {
																	$showLinkToApplicant = true;
																	$howAccessGranted = "Candidate referrer is 'Our Corporate Website'";
																} else {
																	$query = "SELECT r.id
																				FROM responses AS r 
																				WHERE r.id_applicant='".$this->id."'
																					AND id_referrer='15'
																				LIMIT 1";
																	$id_website_application = getMySQLValue($query,true);
																	if($id_website_application) {
																		$showLinkToApplicant = true;
																		$howAccessGranted = "'our corporate website' referrer via response record: ";
																		$howAccessGranted .= $id_website_application;
																	} else {
																		$query = "SELECT id FROM activity 
																					WHERE id_applicant='".$this->id."'
																						AND activity_type='access'";
																		$id_access_activity = getMySQLValue($query,true);
																		if($id_access_activity) {
																			$showLinkToApplicant = true;
																			$howAccessGranted = "access activity record found: ".$id_access_activity;
																		} else {
																			$howAccessGranted = "Not Granted";
																		}
																	}
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}

		$this->profileVisibleData['showLinkToApplicant'] = $showLinkToApplicant;
		$this->profileVisibleData['howAccessGranted'] = $howAccessGranted;
	
	}
	
	
	if($params['returnKind']=="" || $params['returnKind']=="boolean") {
		return $this->profileVisibleData['showLinkToApplicant'];
	} else if($params['returnKind']=="howAccessGranted" || $params['returnKind']=="debug") {
		return $this->profileVisibleData['howAccessGranted'];
	}
}

Version 2:

function isCandidateProfileVisible($params=array()) {
	
	if($this->profileVisibleData['showLinkToApplicant']===NULL) {
			
		
		$isDTstaff = $_SESSION['client']['isDTstaff'];
		$id_company = $_SESSION['client']['id_company'];
		
		$id_user = $_SESSION['client']['id_contact'];
		if($id_user=='') {
			$id_user = $_SESSION['session_id_user'];
		}
		
		if($params['isDTstaff']===true) {
			$isDTstaff = true;
		}
		
		$showLinkToApplicant = false;
		$howAccessGranted = NULL;
		
		$useInternalDTrules = false;
		if($isDTstaff && $this->getIDclient()=='9786') {
			$useInternalDTrules = true;
		}
		

		if(!$showLinkToApplicant && $isDTstaff) {
			$showLinkToApplicant = true;
			$howAccessGranted = "DT Staff";
		}

		if(!$showLinkToApplicant && strtolower($this->getOwner())=="client") {
			$showLinkToApplicant = true;
			$howAccessGranted = "owner=client";
		}
		
		if(!$showLinkToApplicant && $this->getActivityStage()>=2) {
			$showLinkToApplicant = true;
			$howAccessGranted = "getActivityStage";
		}
		
		if(!$showLinkToApplicant) {
			$currentEmployee = strtolower($this->getCurrentEmployee());
			if($currentEmployee=='yes' || $currentEmployee=='alumni') {
				$showLinkToApplicant = true;
				$howAccessGranted = "current/alumni employee";
			}
		}

		if(!$showLinkToApplicant) {
			$accessObj = $params['accessObject'];
			if(is_object($accessObj) && $accessObj->id!='') {
				$viewApps = $accessObj->getViewApps();
			} else {
				$query = "SELECT viewApps FROM access 
							WHERE id_company='".$id_company."' 
								AND id_contact='".$_SESSION['client']['id_contact']."'";
				$viewApps = getMySQLValue($query,true);
			}

		}

		if(!$showLinkToApplicant && strtolower($viewApps)=="all") {
			$showLinkToApplicant = true;
			$howAccessGranted = "viewApps=all";
		}
		
		// special rules for DT persons viewing DT candidates (internal jobs, etc.)
		if($useInternalDTrules) {
			// the above rules don't apply to this situation so we should override any previous data
			$showLinkToApplicant = false;
			$howAccessGranted = NULL;
		}
		

		if(!$showLinkToApplicant) {
			$jobObj = $params['jobObj'];
			$jobviewapps = false;
			
			$id_job = $params['id_job'];
			if(!is_object($jobObj) && $id_job!='') {
				$jobObj = new job($id_job);
			}
			
			if($id_job=='' && is_object($jobObj)) {
				$id_job = $jobObj->id;
			}
			
	
			$jobAccessObj = $params['jobAccessObj'];
			if(is_object($jobAccessObj) && $jobAccessObj->id!='') {
				$jobviewapps = $jobAccessObj->getViewApps();
			} else if($id_job!='') {
				$id_user = $_SESSION['session_id_user'];
				$query = "SELECT viewapps FROM jobaccess 
							WHERE id_job='".$id_job."' 
							AND id_contact='".$id_user."'";
				$jobviewapps = getMySQLValue($query,true);
			}
				
	
			if($jobviewapps!==false && !$showLinkToApplicant && strtolower($jobviewapps)=="all") {
				$showLinkToApplicant = true;
				$howAccessGranted = "jobviewapps=all";
			}
			
			
			
			if(!$showLinkToApplicant) {
				$id_most_recent_job = $this->getMostRecentIDjob();
				if($id_most_recent_job>0) {
					$query = "SELECT Status_ActiveOrPending FROM jobs 
								WHERE id='".$id_most_recent_job."'";
					$Status_ActiveOrPending = getMySQLValue($query,true);
					if($Status_ActiveOrPending!='Yes') {
						$showLinkToApplicant = true;
						$howAccessGranted = "most recent job (".$id_most_recent_job.") ";
						$howAccessGranted .= "is not active or pending";
					} else {
						$query = "SELECT viewapps FROM jobaccess 
									WHERE id_job='".$id_most_recent_job."' 
										AND id_contact='".$id_user."'";
						$jobviewapps = getMySQLValue($query,true);
						if(strtolower($jobviewapps)=="all") {
							$showLinkToApplicant = true;
							$howAccessGranted = "jobviewapps=all";
						}
					}
				}
			}
			
			
			if(!$showLinkToApplicant && is_object($jobObj) && $jobObj->id>0) {
				
				if(	strtolower($jobObj->getFlagAppAccessClient())=="view" 
					|| strtolower($jobObj->getFlagAppAccessClient())=="edit") {
					
					$showLinkToApplicant = true;
					$howAccessGranted = "getFlagAppAccessClient=";
					$howAccessGranted .= $jobObj->getFlagAppAccessClient()." on ".$jobObj->id;
				}
			}
		}
		
		
		if(!$showLinkToApplicant) {
			// check to see if the app has applied to any self-service jobs
			$query = "SELECT r.id
						FROM responses AS r 
						LEFT JOIN jobs AS j ON j.id=r.id_job 
						WHERE r.id_applicant='".$this->id."'
							AND 
								(
									j.flag_masterlist='client'
									OR
									j.title='Future Opportunities'
								)
						LIMIT 1";
			$id_self_service_response = getMySQLValue($query,true);
			if($id_self_service_response) {
				$showLinkToApplicant = true;
				$howAccessGranted = "self service job found via response record: ";
				$howAccessGranted .= $id_self_service_response;
				if($this->getIDclient()=='104093') {
					$subcommand="correct_hm_access";
					require config_include_path_constant.'client/reports/104093/dr.inc.php';
				}
			}
		}
		
		if(!$showLinkToApplicant && $this->getOwner()=='') {
			$showLinkToApplicant = true;
			$howAccessGranted = "owner=NULL";
		}
		
		if(!$showLinkToApplicant) {
			// check to see if the app has been attached to any self-service jobs
			$query = "SELECT f.id
						FROM flags AS f 
						LEFT JOIN jobs AS j ON j.id=f.id_job 
						WHERE f.id_applicant='".$this->id."'
							AND 
								(
									j.flag_masterlist='client'
									OR
									j.title='Future Opportunities'
								)
						LIMIT 1";
			$id_self_service_response = getMySQLValue($query,true);
			if($id_self_service_response) {
				$showLinkToApplicant = true;
				$howAccessGranted = "self service job found via flag record: ";
				$howAccessGranted .= $id_self_service_response;
				if($this->getIDclient()=='104093') {
					$subcommand="correct_hm_access";
					require config_include_path_constant.'client/reports/104093/dr.inc.php';
				}
			}
		}
		
		/*
			2013-06-03: Jay says that all candidates that come from the "Our Corporate Website" 
			should be visible by the client.
		*/
		if(!$showLinkToApplicant) {
			if($this->getIDreferrer()=='15') {
				$showLinkToApplicant = true;
				$howAccessGranted = "Candidate referrer is 'Our Corporate Website'";
			}
		}
		
		if(!$showLinkToApplicant) {
			// check to see if the app has applied to any self-service jobs
			$query = "SELECT r.id
						FROM responses AS r 
						WHERE r.id_applicant='".$this->id."'
							AND id_referrer='15'
						LIMIT 1";
			$id_website_application = getMySQLValue($query,true);
			if($id_website_application) {
				$showLinkToApplicant = true;
				$howAccessGranted = "'our corporate website' referrer via response record: ";
				$howAccessGranted .= $id_website_application;
			}
		}
		
		if(!$showLinkToApplicant) {
			// check to see if any activity "access" records have been created
			// this may need to be modified later to check for job-specific
			// restrictions and/or contact-specific restrictions
			$query = "SELECT id FROM activity 
						WHERE id_applicant='".$this->id."'
							AND activity_type='access'";
			$id_access_activity = getMySQLValue($query,true);
			if($id_access_activity) {
				$showLinkToApplicant = true;
				$howAccessGranted = "access activity record found: ".$id_access_activity;
			}
			
		}
		
			
		
		if($howAccessGranted=='') {
			$howAccessGranted = "Not Granted";
		}
		
		$this->profileVisibleData['showLinkToApplicant'] = $showLinkToApplicant;
		$this->profileVisibleData['howAccessGranted'] = $howAccessGranted;
	
	}
	
	
	if($params['returnKind']=="" || $params['returnKind']=="boolean") {
		return $this->profileVisibleData['showLinkToApplicant'];
	} else if($params['returnKind']=="howAccessGranted" || $params['returnKind']=="debug") {
		return $this->profileVisibleData['howAccessGranted'];
	}
}

August 12th, 2013

Posted In: Controllers, Syntax

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: