|
|
|
|
|
|
|
|
class AcareOffice{ |
|
|
class AcareOffice{ |
|
|
private $nonce; //for ajax verification |
|
|
private $nonce; //for ajax verification |
|
|
private $pages = array('time-sheets', 'user-list'); |
|
|
private $pages = array('time-sheets', 'user-list'); |
|
|
private $acaresydney_userid = 0; |
|
|
|
|
|
|
|
|
private $bts_user_id = 0; |
|
|
|
|
|
private $bts_week_id = 1; //week 1, we will try to calculate current week; |
|
|
private $xero ; |
|
|
private $xero ; |
|
|
private $db; |
|
|
private $db; |
|
|
private $table_name; |
|
|
private $table_name; |
|
|
|
|
|
|
|
|
add_action('wp_ajax_save_job', array($this,'save_job' )); |
|
|
add_action('wp_ajax_save_job', array($this,'save_job' )); |
|
|
add_action('wp_ajax_list_job', array($this,'list_job' )); |
|
|
add_action('wp_ajax_list_job', array($this,'list_job' )); |
|
|
add_action('wp_ajax_delete_job', array($this,'delete_job' )); |
|
|
add_action('wp_ajax_delete_job', array($this,'delete_job' )); |
|
|
|
|
|
add_action('wp_ajax_email_job', array($this,'email_job' )); |
|
|
|
|
|
|
|
|
add_action('wp_ajax_earnings_rate', array($this,'get_payitem_earnings_rate' )); |
|
|
add_action('wp_ajax_earnings_rate', array($this,'get_payitem_earnings_rate' )); |
|
|
add_action('wp_ajax_nopriv_earnings_rate', array($this,'get_payitem_earnings_rate' )); |
|
|
add_action('wp_ajax_nopriv_earnings_rate', array($this,'get_payitem_earnings_rate' )); |
|
|
|
|
|
|
|
|
//query var |
|
|
//query var |
|
|
public function add_query_vars($aVars) { |
|
|
public function add_query_vars($aVars) { |
|
|
$aVars[] = "bts_user_id"; // represents the name of the product category as shown in the URL |
|
|
$aVars[] = "bts_user_id"; // represents the name of the product category as shown in the URL |
|
|
|
|
|
$aVars[] = "bts_week_id"; // represents the name of the product category as shown in the URL |
|
|
|
|
|
$aVars[] = "bts_job_start"; // represents the name of the product category as shown in the URL |
|
|
|
|
|
$aVars[] = "bts_job_finish"; // represents the name of the product category as shown in the URL |
|
|
return $aVars; |
|
|
return $aVars; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function my_add_rewrite_rules($aRules) { |
|
|
public function my_add_rewrite_rules($aRules) { |
|
|
$aNewRules = array( |
|
|
$aNewRules = array( |
|
|
'user/([^/]+)/?$' => 'index.php?pagename=user&bts_user_id=$matches[1]', |
|
|
'user/([^/]+)/?$' => 'index.php?pagename=user&bts_user_id=$matches[1]', |
|
|
'task/([^/]+)/?$' => 'index.php?pagename=task&bts_user_id=$matches[1]' |
|
|
|
|
|
|
|
|
'task/week-([^/]+)/?$' => 'index.php?pagename=task&bts_week_id=$matches[1]', |
|
|
|
|
|
'task/start-([^/]+)/finish-([^/]+)/?$' => 'index.php?pagename=task&bts_job_start=$matches[1]&bts_job_finish=$matches[2]', |
|
|
|
|
|
'task/([^/]+)/?$' => 'index.php?pagename=task&bts_user_id=$matches[1]', |
|
|
|
|
|
'task/([^/]+)/week-([^/]+)/?$' => 'index.php?pagename=task&bts_user_id=$matches[1]&bts_week_id=$matches[2]', |
|
|
|
|
|
'task/([^/]+)/start-([^/]+)/finish-([^/]+)/?$' => 'index.php?pagename=task&bts_user_id=$matches[1]&bts_job_start=$matches[2]&bts_job_finish=$matches[3]', |
|
|
|
|
|
|
|
|
); |
|
|
); |
|
|
$aRules = $aNewRules + $aRules; |
|
|
$aRules = $aNewRules + $aRules; |
|
|
|
|
|
|
|
|
return $aRules; |
|
|
return $aRules; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// |
|
|
// |
|
|
// |
|
|
// |
|
|
///check auth |
|
|
///check auth |
|
|
public function check_auth(){ |
|
|
public function check_auth(){ |
|
|
global $pagename; |
|
|
global $pagename; |
|
|
if ( $pagename == 'task'){ |
|
|
|
|
|
$login = get_query_var( 'bts_user_id' ); |
|
|
|
|
|
if ($login != "")//perform autologin |
|
|
|
|
|
{ |
|
|
|
|
|
$staff = get_user_by('login', $login); |
|
|
|
|
|
if ($staff->ID !=0 && in_array('staff', $staff->roles)){//is valid staff; |
|
|
|
|
|
//wp_set_current_user($staff->ID); |
|
|
|
|
|
$current = wp_get_current_user(); |
|
|
|
|
|
if( $current->ID != $staff->ID) |
|
|
|
|
|
wp_redirect("/task"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch($pagename){ |
|
|
|
|
|
case 'task': |
|
|
|
|
|
$this->cauth_task(); |
|
|
|
|
|
break; |
|
|
|
|
|
case 'time-sheets': |
|
|
|
|
|
$this->cauth_time_sheet(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private function cauth_task(){ |
|
|
|
|
|
$login = get_query_var( 'bts_user_id' ); |
|
|
|
|
|
$this->bts_job_start = get_query_var( 'bts_job_start' ); |
|
|
|
|
|
$this->bts_job_finish = get_query_var( 'bts_job_finish' ); |
|
|
|
|
|
$this->bts_week_id = get_query_var('bts_week_id'); |
|
|
|
|
|
|
|
|
|
|
|
$redirect_url = $this->get_redirect_url_for_task(); |
|
|
|
|
|
// wp_send_json(array( |
|
|
|
|
|
// 'week'=> $week, |
|
|
|
|
|
// 'userid'=>$login, |
|
|
|
|
|
// 'job_start' => $this->bts_job_start, |
|
|
|
|
|
// 'job_finish' => $this->bts_job_finish, |
|
|
|
|
|
// 'redirect' => $redirect_url, |
|
|
|
|
|
// )); |
|
|
|
|
|
|
|
|
|
|
|
if ($login != "")//perform autologin, and redirect |
|
|
|
|
|
{ |
|
|
|
|
|
$staff = get_user_by('login', $login); |
|
|
|
|
|
if ($this->is_staff($staff)){//is valid staff; |
|
|
|
|
|
$current = wp_get_current_user(); |
|
|
|
|
|
if($current->ID != $staff->ID){ |
|
|
|
|
|
wp_logout(); |
|
|
|
|
|
wp_set_current_user($staff->ID, $staff->display_name); //this is a must |
|
|
|
|
|
wp_set_auth_cookie($staff->ID, true);//only with this, wordpress calls login + redirect and lost week-%d |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
wp_redirect($redirect_url); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//no auto login is required if reach here. |
|
|
|
|
|
$current = wp_get_current_user(); |
|
|
|
|
|
if ($this->is_admin($current)){ |
|
|
|
|
|
wp_redirect("/time-sheets/"); |
|
|
|
|
|
return; |
|
|
} |
|
|
} |
|
|
//echo $pagename; |
|
|
|
|
|
|
|
|
if (!$this->is_staff($current) && ! $this->is_admin($current)) |
|
|
|
|
|
{ |
|
|
|
|
|
wp_logout(); |
|
|
|
|
|
wp_redirect("/login/"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private function get_week_id() |
|
|
|
|
|
{ |
|
|
|
|
|
$week = get_query_var( 'bts_week_id' ); |
|
|
|
|
|
$week_id = intval($week); |
|
|
|
|
|
if ($week_id == 0 || $week_id >53 ||$week_id < 1) |
|
|
|
|
|
return $this->get_current_week_id(); |
|
|
|
|
|
else |
|
|
|
|
|
return $week; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private function get_current_week_id() |
|
|
|
|
|
{ |
|
|
|
|
|
$now = new \DateTime(); |
|
|
|
|
|
$week = $now->format("W"); |
|
|
|
|
|
return $week; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private function get_redirect_url_for_task() |
|
|
|
|
|
{ |
|
|
|
|
|
if ($this->bts_week_id != "") |
|
|
|
|
|
return "/task/week-" . $this->bts_week_id . "/"; |
|
|
|
|
|
if ($this->bts_job_start!="" && $this->bts_job_finish !="") |
|
|
|
|
|
return "/task/start-" . $this->bts_job_start . "/finish-" .$this->bts_job_finish . "/"; |
|
|
|
|
|
return '/task/'; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function cauth_time_sheet() |
|
|
|
|
|
{ |
|
|
|
|
|
$current = wp_get_current_user(); |
|
|
|
|
|
if ($current->ID == 0 ) { //visitor not logged in |
|
|
|
|
|
wp_redirect("/wp-login.php?"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
if ($this->is_staff($current)){ |
|
|
|
|
|
wp_redirect("/task"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
if ($this->is_admin($current)){ |
|
|
|
|
|
//proceed |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
if ($this->is_client($current)){ |
|
|
|
|
|
wp_redirect("/service"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
//everything else |
|
|
|
|
|
wp_redirect("/?invalid-access"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// |
|
|
/// |
|
|
|
|
|
|
|
|
// |
|
|
// |
|
|
public function register_js_css() { |
|
|
public function register_js_css() { |
|
|
$this->nonce = wp_create_nonce('acaresydney'); |
|
|
$this->nonce = wp_create_nonce('acaresydney'); |
|
|
$this->acaresydney_userid = get_query_var( 'bts_user_id' ) ; |
|
|
|
|
|
|
|
|
$this->bts_user_id = get_query_var( 'bts_user_id' ) ; |
|
|
$this->register_bts_js(); |
|
|
$this->register_bts_js(); |
|
|
$this->register_timesheet_js_css(); |
|
|
$this->register_timesheet_js_css(); |
|
|
$this->register_task_js_css(); |
|
|
$this->register_task_js_css(); |
|
|
|
|
|
|
|
|
'display_name' => wp_get_current_user()->display_name, |
|
|
'display_name' => wp_get_current_user()->display_name, |
|
|
'anonymous' => !is_user_logged_in(), |
|
|
'anonymous' => !is_user_logged_in(), |
|
|
'me'=> get_current_user_id(), |
|
|
'me'=> get_current_user_id(), |
|
|
'userid'=> $this->acaresydney_userid, |
|
|
|
|
|
|
|
|
'userid'=> $this->bts_user_id, |
|
|
'load_user_img'=> plugins_url('img/loading_user.gif', __FILE__), |
|
|
'load_user_img'=> plugins_url('img/loading_user.gif', __FILE__), |
|
|
'load_job_img'=> plugins_url('img/loading_job.gif', __FILE__), |
|
|
'load_job_img'=> plugins_url('img/loading_job.gif', __FILE__), |
|
|
'earnings_rate'=> get_option('bts_payitem_earnings_rate'), |
|
|
'earnings_rate'=> get_option('bts_payitem_earnings_rate'), |
|
|
|
|
|
|
|
|
if ($pagename != 'task'){ |
|
|
if ($pagename != 'task'){ |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->bts_job_start = get_query_var( 'bts_job_start' ); |
|
|
|
|
|
$this->bts_job_finish = get_query_var( 'bts_job_finish' ); |
|
|
|
|
|
$this->bts_week_id = get_query_var('bts_week_id'); |
|
|
|
|
|
|
|
|
wp_enqueue_style( 'bts_task', plugins_url('css/bts_task.css', __FILE__)); |
|
|
wp_enqueue_style( 'bts_task', plugins_url('css/bts_task.css', __FILE__)); |
|
|
wp_enqueue_script( 'bts_task', plugins_url('js/bts_task.js', __FILE__), array( 'jquery' , 'bts' )); |
|
|
wp_enqueue_script( 'bts_task', plugins_url('js/bts_task.js', __FILE__), array( 'jquery' , 'bts' )); |
|
|
wp_enqueue_script('mustache', plugins_url('js/mustache.min.js', __FILE__), array('jquery')); |
|
|
wp_enqueue_script('mustache', plugins_url('js/mustache.min.js', __FILE__), array('jquery')); |
|
|
|
|
|
|
|
|
wp_localize_script('bts_task','bts_task1',array( |
|
|
wp_localize_script('bts_task','bts_task1',array( |
|
|
'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
|
'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
|
'nonce' => wp_create_nonce('bts_task'), |
|
|
'nonce' => wp_create_nonce('bts_task'), |
|
|
|
|
|
'week_id' => $this->bts_week_id, |
|
|
|
|
|
'bts_job_start' => $this->bts_job_start, |
|
|
|
|
|
'bts_job_finish' => $this->bts_job_finish, |
|
|
) ); |
|
|
) ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$n = new UserJob($u->user_login); |
|
|
$n = new UserJob($u->user_login); |
|
|
$resp = $n->list_jobs("2019-07-22 00:00:00", "2019-07-28 23:59:59"); |
|
|
$resp = $n->list_jobs("2019-07-22 00:00:00", "2019-07-28 23:59:59"); |
|
|
if ($resp['status']=='success' && $resp['job_count'] >0 ){ |
|
|
if ($resp['status']=='success' && $resp['job_count'] >0 ){ |
|
|
// if( $u->user_login != "3e8c2e62-8e28-4b68-ae98-9ef1d76188c4" ) |
|
|
|
|
|
// continue; |
|
|
|
|
|
|
|
|
if( $u->user_login != "9aa3308e-cc19-4c21-a110-f2c6abec4337" ) |
|
|
|
|
|
continue; |
|
|
$msg = sprintf("Staff = %s, Login=%s, email=%s Job=%d\n", $u->display_name, $u->user_login, $u->user_email, $resp['job_count']); |
|
|
$msg = sprintf("Staff = %s, Login=%s, email=%s Job=%d\n", $u->display_name, $u->user_login, $u->user_email, $resp['job_count']); |
|
|
echo $msg; |
|
|
echo $msg; |
|
|
$this->send_email_with_job_link($u); |
|
|
|
|
|
|
|
|
$this->send_email_with_job_link($u, "2019-07-22", "2019-07-28"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private function send_email_with_job_link($staff) |
|
|
|
|
|
|
|
|
private function send_email_with_job_link($staff, $start, $finish) |
|
|
{ |
|
|
{ |
|
|
$message = file_get_contents(plugin_dir_path(__FILE__) . "/html/email_job.html"); |
|
|
$message = file_get_contents(plugin_dir_path(__FILE__) . "/html/email_job.html"); |
|
|
$message = str_ireplace("{{display_name}}", $staff->display_name, $message); |
|
|
$message = str_ireplace("{{display_name}}", $staff->display_name, $message); |
|
|
$message = str_ireplace("{{user_login}}", $staff->user_login, $message); |
|
|
$message = str_ireplace("{{user_login}}", $staff->user_login, $message); |
|
|
|
|
|
$message = str_ireplace("{{job_start}}", $start, $message); |
|
|
|
|
|
$message = str_ireplace("{{job_finish}}", $finish, $message); |
|
|
$headers = ['Bcc: patrick@biukop.com.au']; |
|
|
$headers = ['Bcc: patrick@biukop.com.au']; |
|
|
//wp_mail("sp@lawipac.com", "Your Job arrangement 22 July ~ 28 July", $message, $headers); |
|
|
//wp_mail("sp@lawipac.com", "Your Job arrangement 22 July ~ 28 July", $message, $headers); |
|
|
wp_mail($staff->user_email, "Your Job arrangement 22 July ~ 28 July", $message, $headers); |
|
|
|
|
|
|
|
|
wp_mail($staff->user_email, "Your Job arrangement $start ~ $finish", $message, $headers); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function bts_staff_item($attr){ |
|
|
public function bts_staff_item($attr){ |
|
|
|
|
|
|
|
|
wp_die(); |
|
|
wp_die(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//ajax email staff their job arrangement |
|
|
|
|
|
function email_job() |
|
|
|
|
|
{ |
|
|
|
|
|
check_ajax_referer('acaresydney'); |
|
|
|
|
|
$staff = $_POST['staff']; |
|
|
|
|
|
$start = $_POST['start']; |
|
|
|
|
|
$finish = $_POST['finish']; |
|
|
|
|
|
$response=array( |
|
|
|
|
|
'status' => 'success', |
|
|
|
|
|
'staff' => $staff, |
|
|
|
|
|
'start' => $start, |
|
|
|
|
|
'finish' => $finish, |
|
|
|
|
|
'error' => '', |
|
|
|
|
|
'sent' => false, |
|
|
|
|
|
'emailstatus'=>"Bypass (no job)", |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
$u = get_user_by('login', $staff); |
|
|
|
|
|
if ($this->is_staff($u)){ |
|
|
|
|
|
$n = new UserJob($staff); |
|
|
|
|
|
$resp = $n->list_jobs("$start 00:00:00", "$finish 23:59:59"); |
|
|
|
|
|
if ($resp['status']=='success' && $resp['job_count'] >0 ){ |
|
|
|
|
|
$msg = sprintf("Email to <strong>%s</strong> (with job=%d) \n", $u->user_email, $resp['job_count']); |
|
|
|
|
|
$this->send_email_with_job_link($u, $start, $finish); |
|
|
|
|
|
$response['sent'] = true; |
|
|
|
|
|
$response['emailstatus'] = $msg; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
wp_send_json($response); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//ajax browse job with different filters |
|
|
//ajax browse job with different filters |
|
|
function list_job(){ |
|
|
function list_job(){ |
|
|
check_ajax_referer('acaresydney'); |
|
|
check_ajax_referer('acaresydney'); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
public function list_job_by_staff() |
|
|
public function list_job_by_staff() |
|
|
{ |
|
|
{ |
|
|
check_ajax_referer('acaresydney'); |
|
|
|
|
|
|
|
|
//check_ajax_referer('acaresydney'); |
|
|
$start = $_POST['start']; |
|
|
$start = $_POST['start']; |
|
|
$finish = $_POST['finish']; |
|
|
$finish = $_POST['finish']; |
|
|
$login = $_POST['login']; |
|
|
|
|
|
|
|
|
|
|
|
$response= array( |
|
|
|
|
|
'status' => 'error', |
|
|
|
|
|
'current_user' => wp_get_current_user(), |
|
|
|
|
|
); |
|
|
|
|
|
wp_send_json($response); |
|
|
|
|
|
//$user = wp_get_current_user();// should be staff; |
|
|
|
|
|
//$user = get_user_by('login', 'a6536a3b-ef22-4a28-8d55-e2a26d4ae227'); |
|
|
|
|
|
//$login = get_query_var( 'bts_user_id' ); |
|
|
|
|
|
//if ($user->ID ==0){ |
|
|
|
|
|
$user = get_user_by('login', $login); |
|
|
|
|
|
//} |
|
|
|
|
|
|
|
|
//$start="2019-07-01 00:00:00"; |
|
|
|
|
|
//$finish="2019-07-14 23:59:59"; |
|
|
|
|
|
|
|
|
if (in_array('staff', $user->roles)){ |
|
|
|
|
|
//require_once (dirname(__FILE__) . "/UserJob.php"); |
|
|
|
|
|
|
|
|
$user = wp_get_current_user();// should be staff; |
|
|
|
|
|
if ( $this->is_staff($user) ){ |
|
|
$n = new UserJob($user->user_login); |
|
|
$n = new UserJob($user->user_login); |
|
|
$response = $n->list_jobs($start, $finish); |
|
|
$response = $n->list_jobs($start, $finish); |
|
|
$response['current_user'] = wp_get_current_user(); |
|
|
|
|
|
|
|
|
wp_send_json($response); |
|
|
|
|
|
}else{ |
|
|
|
|
|
$response = array( |
|
|
|
|
|
'status' => 'error', |
|
|
|
|
|
'errmsg' => 'invalid access', |
|
|
|
|
|
'user' => $user, |
|
|
|
|
|
); |
|
|
wp_send_json($response); |
|
|
wp_send_json($response); |
|
|
} |
|
|
} |
|
|
wp_die(); |
|
|
wp_die(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private function is_staff($user) |
|
|
|
|
|
{ |
|
|
|
|
|
return ($user->ID !=0 && in_array('staff', $user->roles)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private function is_admin($user) |
|
|
|
|
|
{ |
|
|
|
|
|
$allowed_roles = array('administrator', 'acare_owner'); |
|
|
|
|
|
if( array_intersect($allowed_roles, $user->roles ) ) { |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public function staff_ack_job() |
|
|
public function staff_ack_job() |
|
|
{ |
|
|
{ |
|
|
check_ajax_referer('acaresydney'); |
|
|
check_ajax_referer('acaresydney'); |
|
|
|
|
|
|
|
|
\WP_CLI::add_command( 'email_jobs', array($bb, 'email_jobs')); |
|
|
\WP_CLI::add_command( 'email_jobs', array($bb, 'email_jobs')); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//$bb->class_loader(); |
|
|
|
|
|
//$bb->list_job_by_staff(); |