define("ASYNCLOAD_FILE_DIR",$_SERVER[DOCUMENT_ROOT].'/../cache/asyncload/');
if(!file_exists(ASYNCLOAD_FILE_DIR))
mkdir_wrapper(ASYNCLOAD_FILE_DIR,0777,true);
//these functions are intended to be used to asyncrounously load content, while telling some background getter that it needs to generate updated content
// this can be used to 'background' the process of generating long running code and also to make it so some code does not have to be run multiple times when multiple users share it.
//HOW TO USE THIS FILE
//to create a new asyncronouslyloaded function
//function asyncload_get_mydata($id) , where id is the unique code you will use to cache things by, this function should return what you want cached.
//function asyncload_postpend_mydata($id) , you can optionally create a function called to automatically add some html to the end of your code before processing
//then to load your code, just run echo asyncload_get('mydata',100) , where the text returned asyncload_get_mydata(100) is what you would want to display.
//and the system will cache it and update the code about every minute or 2 if the function is called.
// the first main use of this file is 'apdashboard' for clientcompany /freo users where we dont want to run the dashboard at the same time that the user is view the apge
// so we just run it in the background, we have a span / asterisk that shows how old the dashboard is
//to makt this allwork, the /schedule/asyncload.php file must run every 5 minutes.
// **** NOTE ***** If you are going to add any asyncload_get_newshortname functions for new areas be aware that if you only add the functions here on dev and call the function on dev the
//cron job is currently running on live and will generate errors on live and the cronjob won't complete because of the missing functions on live. To Prevent this a one time change needs to happen.
//A seperate /cache/asyncload/ directory needs to be created for dev, test, and live. The ASYNCLOAD_FILE_DIR defined at the top of this file will need to be changed from /../cache/asyncload/
//to /cache/asyncload/ and a seperate cron job will need to be set up for dev and test. The changes in this file for ASYNCLOAD_FILE_DIR will need to be migrated to live before the old /cache/asyncload/
//directory can be removed.
function asyncload_postpend_apdashboard($loginid)
{
$since = asyncload_since(apdashboard,$loginid);
if(!is_developer() || $_SESSION['login']['clientcompanydev']!=1)
return ' * ';
return ' * ';
}
function asyncload_since($shortname,$id)
{
$st = stat(asyncload_filename_get($shortname,$id));
//d($st);
return time_since($st[mtime]);
}
function asyncload_get_apdashboard($loginid)
{
//we need to reset the session and permissions for this run
if(!is_background()&& !is_developer() && $loginid != loginid())
{
developer_error("a user loginid:".loginid()." somehow ran asyncload_get_apdashboard($loginid) with a mismatched loginid() ");
ddie("invalid!! you should not be running this");
}
if($loginid != loginid())
{
d("Resetting session from ".loginid()." to $loginid");
$sql = "select * from tbllogin
left join tblclientstaff using (clientstaffid)
join tblclientcompany using(clientcompanyid)
where loginid = ".db_number($loginid);
$qrylogin = db_first($sql);
d($sql,$qrylogin);
foreach($qrylogin as $col=>$val) //this method of zipping the array, makes sure not to clober some of the database cachcing stuff that we could benefit from
//from session to session.
$_SESSION[login][$col] = $val;
$_SESSION[login][cache] = array(); // kill the cache too.
setpermissions(loginid(), 'clientcompany');
d(session,$_SESSION);
$sql = "
select case when coalesce(configextensionentryvalue,'') = '' then configextensiondefault else configextensionentryvalue end as value
,configextensionkey as key
from tblconfigextension
left join (select * from tblconfigextensionentry where clientcompanyid = ".clientcompanyid().") a using (configextensionid)
where configextensionshortname = 'availablepropertieslabel'
";
$qry=db_cache($sql);
$qry=$qry[0];
if (!empty($qry))
$qry = array_shift($qry);
$AVAILABLEPROPERTIESLABEL = $qry;
}
if(!function_exists('add_apdashboard_link'))
{
function add_apdashboard_link(&$inarr,$count,$getdata,$label)
{
$arr['count'] = $count;
$arr["getdata"] = $getdata;
$arr["label"] = $label;
$inarr[] = $arr;
flush();
}
}
if(strlen($AVAILABLEPROPERTIESLABEL)<1)
$AVAILABLEPROPERTIESLABEL="My Company Residential";
timetrack_start("Dashboard","acq");
$ap_dashboard = array();
$thiscount=cache_getvalue('dashboard_active_count');
if($thiscount===false){
global $stopsql;
$stopsql=0;
timetrack_start("Dashboard","acq_active");
$thiscount = cache_setvalue('dashboard_active_count', get_hotsheetproperties('active', 1,$dashboard_datelimit,0,'',0));
timetrack_stop("Dashboard","acq_active");
}
add_apdashboard_link($ap_dashboard,$thiscount,'tab=active', $AVAILABLEPROPERTIESLABEL);
$thiscount=cache_getvalue('dashboard_acquisition_messages');
if($thiscount===false){ // Message counter needs property not count in order to work see message_count function in functions property. VL
$thiscount = cache_setvalue('dashboard_acquisition_messages',get_hotsheetproperties('messages_unread',2,$dashboard_datelimit,0,'',0));
}
add_apdashboard_link($ap_dashboard,$thiscount,'tab=messages_unread&unread=1', "My Messages");
if(!has_access("hideavailablepropswithoutoffer"))
{
//changed this cache value to use the same login as used when loading the records to the page - this ensure consistent counts JT
//d($thiscount,thiscount);
$thiscount=cache_getvalue('dashboard_props_count');
if($thiscount===false)
$thiscount = cache_setvalue('dashboard_props_count', get_hotsheetproperties('props', 1,$dashboard_datelimit,0,'',0,''));
add_apdashboard_link($ap_dashboard,$thiscount,'tab=props', "Off Market");
$thiscount=cache_getvalue('dashboard_props_unviewed_count');
if($thiscount===false){
timetrack_start("Dashboard","acq_uc");
$thiscount = cache_setvalue('dashboard_props_unviewed_count', get_hotsheetproperties('props', 1,$dashboard_datelimit,0,'',0,'',$unviewedtrue=1));
timetrack_stop("Dashboard","acq_uc");
}
add_apdashboard_link($ap_dashboard,$thiscount,'tab=props&unviewed=1', "Unviewed Off Market");
$thiscount=cache_getvalue('dashboard_acquisition_propertycount');
d("Escalated-count",$thiscount);
if($thiscount===false){
d('Escalated Count is FALSE ',"
".$thiscount."
");
timetrack_start("Dashboard","acq_ae");
$thiscount=cache_setvalue('dashboard_acquisition_propertycount', get_hotsheetproperties('acquisition',1,0,0,'',0));
d('Escalated',"". get_hotsheetproperties('acquisition',1,0,0,'',0)."
");
timetrack_stop("Dashboard","acq_ae");
}
add_apdashboard_link($ap_dashboard,$thiscount,'tab=acquisition', "Agent Escalated");
$thiscount=cache_getvalue('dashboard_acquisition_unviewed');
d("Unviewed-count",$thiscount);
if($thiscount===false)
$thiscount= cache_setvalue('dashboard_acquisition_unviewed', get_hotsheetproperties('acquisition',1,$dashboard_datelimit,0,'',0,'',1));
d('Unviewed',"".print_r( get_hotsheetproperties('acquisition',1,$dashboard_datelimit,0,'',0,'',1))."
");
add_apdashboard_link($ap_dashboard,$thiscount,'enhanced=1&tab=acquisition&unviewed=1', "Unviewed Agent Escalated");
$thiscount=cache_getvalue('dashboard_hotsheet_hotsheetcount');
//d($acquipropertycount,aquicnt);
if($thiscount===false)
$thiscount=cache_setvalue('dashboard_hotsheet_hotsheetcount', get_hotsheetproperties('mls', 1,0,0,'',0));
add_apdashboard_link($ap_dashboard,$thiscount,'tab=mls', "MLS Feed");
$thiscount=cache_getvalue('dashboard_hotsheet_unviewedhotsheetcount');
if($thiscount===false)
$thiscount = cache_setvalue('dashboard_hotsheet_unviewedhotsheetcount', get_hotsheetproperties('mls',1,$dashboard_datelimit,0,'',0,'',1));
add_apdashboard_link($ap_dashboard,$thiscount,'enhanced=1&tab=mls&unviewed=1', "Unviewed MLS Feed");
}
if(!has_access("removeavailablepropertydatatools")) // only five access to these tabas if they have the ability to view property data tools
{
$thiscount=cache_getvalue('dashboard_acquisition_propertiescountercount');
if($thiscount===false)
$thiscount = cache_setvalue('dashboard_acquisition_propertiescountercount',get_hotsheetproperties('active', 1,$dashboard_datelimit,0,'',array('counteroffer'=>1)));
add_apdashboard_link($ap_dashboard,$thiscount,'tab=active&counteroffer=1', "Counter Offers");
$thiscount=cache_getvalue('dashboard_acquisition_offeracceptedcount');
if($thiscount===false)
$thiscount = cache_setvalue('dashboard_acquisition_offeracceptedcount', get_hotsheetproperties('escrow', 1,$dashboard_datelimit,0,'',0));
add_apdashboard_link($ap_dashboard,$thiscount,'tab=escrow', "Offer Accepted");
$thiscount=cache_getvalue('dashboard_acquisition_propertiesclosedcount');
if($thiscount===false)
$thiscount = cache_setvalue('dashboard_acquisition_propertiesclosedcount', get_hotsheetproperties('closed', 1,$dashboard_datelimit,0,'',0));
add_apdashboard_link($ap_dashboard,$thiscount,'tab=closed', "Properties Closed");
$thiscount=cache_getvalue('dashboard_acquisition_declined');
if($thiscount===false)
$thiscount = cache_setvalue('dashboard_acquisition_declined', get_hotsheetproperties('declined', 1,$dashboard_datelimit,0,'',0));
add_apdashboard_link($ap_dashboard,$thiscount,'tab=declined', "Offer Declined");
}
if(has_customization("posttonotebiddingboard") || (has_access("viewnotebiddingboard") || has_access("updatenotebiddingboard")))
$checkdashboardtotal++;
if($checkdashboardtotal>4)
$needtwocolumns=TRUE;
//$ddstyle=1;
$ap_total=0;
foreach($ap_dashboard as $item)
$ap_total+=$item['count'];
$acqusitionout = "-
$ap_total
Acquisition Notifications
";
//d($acquisitionalerts,acuilert);
foreach($ap_dashboard as $acquisitionalert)
{
$color='';
$href="href='/availableproperty.php?".$acquisitionalert[getdata]."'";
if(!$acquisitionalert['count'])
{
$color='color:grey;';
$href='';
}
$acqusitionout.= "- ".$acquisitionalert[count]."".$acquisitionalert[label]."
";
}
$acqusitionout.="
";
return $acqusitionout;
} ///end asyncload_get_apdashboard
//----------------------CORE FUNCTIONS
function asyncload_postpend($shortname, $id)
{
$fn = "asyncload_postpend_$shortname";
if(function_exists($fn))
return $fn($id);
return '';//'shortname: '.$shortname;
}
function asyncload_get($shortname,$id,$appendscripttoreloadifold=0)
{
//$appendscripttoreloadifold //if true, the code should append some HTML which will make it so the users browser checks the server for updates until it finds an update
//configuraiton is correct
$fl = asyncload_filename_get($shortname,$id);
touch($fl.".load"); //set the last requested time just before we get the file
if(!file_exists($fl))
{
//this is wherew we would put some sort of asyncload_default($shortname,$id) code to load the default entry inforamtoin if it is too old (something like, "refresshign data.....")
//this is the first time this has run, so we must do this in the foreground
$content = asyncload_run($shortname,$id);
}else
$content = file_get_contents($fl);
if($content)
$content.=asyncload_postpend($shortname,$id);
if($appendscripttoreloadifold) //not yet implemented
{
d_dev("It has been more than a minute since this file ran, so we should append a ";
}
if(is_devsite())
{
d_dev($fl );
d_dev($contentl);
$curfile = @stat($fl);
$lastloaded = @stat($fl.".load");
//check to see if the last date the the file was loaded(requested) was more recently than it was created
//this means we need to get the latest content
d_dev($curfile[mtime],'$curfile[mtime]');
d_dev($lastloaded[mtime],'$lastloaded[mtime]');
if($curfile[mtime]<$lastloaded[mtime])
d_dev("We shoudl be rnning this again");
}
return $content;
}
function asyncload_run($shortname,$id)
{
if(!strlen($id) || ! is_numeric($id))
{
developer_error("id: must be set to use this function");
ddie("die");
}
if(preg_replace("/[a-zA-Z]/",'',$shortname))
{
developer_error("asyncload_get shortname may only have characters in it '$shortname' ,$id)");
ddie("die");
}
if(!function_exists("asyncload_get_$shortname"))
{
developer_error("$_SERVER[HTTP_HOST] - asyncload_get_$shortname () must be defined to call asyncload_get($shortname,$id)");
}
$fn = "asyncload_get_$shortname";
$fl = asyncload_filename_get($shortname,$id);
$curfile = @stat($fl);
$lastloaded = @stat($fl.".load");
//check to see if the last date the the file was loaded(requested) was more recently than it was created
//this means we need to get the latest content
if(is_devsite())
{
d_dev($curfile[mtime],'$curfile[mtime]');
d_dev($lastloaded[mtime],'$lastloaded[mtime]');
d_dev(text_highlight("Time Difference","red"),$lastloaded[mtime]-$curfile[mtime]);
}
//ddie($fl);
if(!$curfile[mtime] || $curfile[mtime]<$lastloaded[mtime])
{
timetrack_start($shortname,$id);
if(is_devsite())
d_dev("asyncload_run($shortname,$id) - running currfile-mtime:$curfile[mtime], lastloaded-mtime:$lastloaded[mtime]");
flush();
dlog('asyncload_run',"$shortname . $id - start");
$content = $fn($id);
if($content!==false)
file_put_contents($fl,$content);
if(is_devsite())
d_dev(filecontent,array(content=>$content));
$tm = timetrack_stop($shortname,$id);
if(is_devsite())
d_dev("asyncload_run($shortname,$id) - completing currfile-mtime:$curfile[mtime], lastloaded-mtime:$lastloaded[mtime]");
flush();
dlog('asyncload_run',"$shortname . $id - end $tm");
}
else
d_dev("asyncload_run($shortname,$id) - skipped, most recent file was new enough");
if(is_devsite())
d_dev($content,'content');
return $content;
}
function asyncload_filename_get($shortname,$id)
{
return ASYNCLOAD_FILE_DIR.$shortname."_".$id;
}