|
- <?php
- /**
- * Plugin Name: Acare Advanced Office
- * Plugin URI: http://biukop.com.au/acaresydney/timesheets
- * Description: Advanced Office system, timesheet, Payroll for AcareSydney
- * Version: 2.1
- * Author: Biukop Intelligence
- * Author URI: http://biukop.com.au/
- */
-
- namespace Biukop;
- require_once(dirname(__FILE__) . '/autoload.php');
- require_once (ABSPATH . 'wp-includes/pluggable.php');
-
-
- class AcareOffice{
-
- private $nonce; //for ajax verification
- private $pages = array('time-sheets', 'user-list');
- private $acaresydney_userid = 0;
-
- private $xero ;
-
- public function __construct() {
- add_action('init', array($this, 'class_loader'));
- add_action('wp', array($this, 'check_auth'));
- add_action('wp_enqueue_scripts', array($this, 'register_js_css'), 99);
-
- add_filter('show_admin_bar', '__return_false');
-
- //ts-xx for sync single user
- add_shortcode( 'ts-sync-users', array($this, 'sync_users'));
- //bts-xx for webpage
- add_shortcode( 'bts_staff_item', array($this, 'bts_staff_item'));
- add_shortcode( 'bts_client_item', array($this, 'bts_client_item'));
- add_shortcode( 'bts_job_item', array($this, 'bts_job_item'));
- add_shortcode( 'bts_tos_options', array($this, 'bts_tos_options'));
-
- add_action('wp_ajax_list_staff', array($this,'list_staff' ));
- add_action('wp_ajax_list_client', array($this,'list_client' ));
-
- 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' ));
-
-
- }
-
- /**
- * Autoload the custom theme classes
- */
- public function class_loader()
- {
- // Create a new instance of the autoloader
- $loader = new \Psr4AutoloaderClass();
-
- // Register this instance
- $loader->register();
-
- // Add our namespace and the folder it maps to
- $loader->addNamespace('\XeroPHP', dirname(__FILE__) . '/xero-php-master/src/XeroPHP');
- $loader->addNamespace('\Biukop', dirname(__FILE__) . '/' );
-
- $this->xero = new Xero();
- $this->xero->init_wp();
- }
-
-
- //
- //
- ///check auth
- public function check_auth(){
- global $pagename;
- //echo $pagename;
- }
-
- ///
- // enqueue / register css /js
- //
- public function register_js_css() {
- $this->nonce = wp_create_nonce('acaresydney');
- $this->acaresydney_userid = get_query_var( 'acaresydney_userid' ) ;
- $this->register_bts_js();
- $this->register_timesheet_js_css();
- }
- private function register_bts_js()
- {
- wp_enqueue_style( 'bts', plugins_url('css/ts.css', __FILE__));
- wp_enqueue_script('bts', plugins_url('js/ts.js', __FILE__), array('jquery', 'jquery-ui-core'));
- wp_localize_script( 'bts', 'bts1', array(
- 'ajax_url' => admin_url( 'admin-ajax.php' ),
- 'nonce' => $this->nonce, // It is common practice to comma after
- 'display_name' => wp_get_current_user()->display_name,
- 'anonymous' => !is_user_logged_in(),
- 'me'=> get_current_user_id(),
- 'userid'=> $this->acaresydney_userid,
- 'load_user_img'=> plugins_url('img/loading_user.gif', __FILE__),
- ) );
- }
-
- private function register_timesheet_js_css(){
- global $pagename;
- if ($pagename != 'time-sheets'){
- return;
- }
- wp_enqueue_style( 'bts_ts', plugins_url('css/bts_timesheet.css', __FILE__));
- wp_enqueue_script( 'bts_ts', plugins_url('js/bts_timesheet.js', __FILE__), array( 'jquery' , 'bts' ));
- wp_enqueue_script('mustache', plugins_url('js/mustache.min.js', __FILE__), array('jquery'));
- }
-
- public function sync_users()
- {
- //dummy sync
- return;
- }
-
- // Usage: `wp sync_users --mininterval=123
- public function sync_user_cli($args = array(), $assoc_args = array()){
- $arguments = wp_parse_args( $assoc_args, array(
- 'mininterval' => 600,
- ) );
- $this->xero->sync_users($arguments['mininterval']);
- return;
- }
-
-
- public function bts_staff_item($attr){
- return $this->template('staff_item', 'staff.html');
- }
-
- public function bts_client_item($attr){
- return $this->template('client_item', 'client.html');
- }
-
- public function bts_job_item($attr){
- $html =$this->template('job_item', 'job.html');
- //$html = str_replace('[bts-tos-options]', $this->bts_tos_options([]), $html);
- $html = do_shortcode($html);
- return $html;
- }
-
- public function bts_tos_options($attr){
- $result = "<select> \n";
- $options = get_option('bts_payitem_earnings_rate');
- foreach($options as $o){
- $result.=sprintf("<option value='%s'> $%03.2f-%s</option>",
- $o['EarningsRateID'], $o['RatePerUnit'], $o['Name']);
- }
- $result .="</select>";
- return $result;
- }
-
-
- //generate template based on html file
- private function template($id, $file)
- {
- $text = '<script id="' . $id .'" type="text/x-biukop-template">';
- $text .= file_get_contents(plugin_dir_path(__FILE__) . "/html/$file");
- $text .= '</script>';
- return $text;
- }
-
-
- function list_staff(){
- check_ajax_referer('acaresydney');
- return $this->list_people_by_role('staff');
- }
- function list_client(){
- check_ajax_referer('acaresydney');
- return $this->list_people_by_role('client');
- }
-
- function list_people_by_role($role){
- check_ajax_referer('acaresydney');
- // Handle the ajax request
- $response = array(
- 'status' =>'error',
- 'users' => [],
- );
- //search all users that are staff
- $staffq = new \WP_User_Query(array('role'=>$role));
- $staff = $staffq->get_results();
- if (! empty($staff)){
- $response['status'] = 'success';
- foreach( $staff as $s){
- $response['users'][] = array(
- 'login' => $s->user_login,
- 'firstname'=> $s->first_name,
- 'lastname'=> $s->last_name,
- 'mobile'=> get_user_meta($s->ID, 'mobile', true),
- 'email'=> $s->user_email,
- 'wages'=> 0,
- 'hour' => 0 ,
- 'OT' => 0 ,
- 'petrol'=> 0 ,
- 'rating'=> 0,
- 'unconfirmedjob'=> 0,
- );
- }
- }
- wp_send_json($response);
- wp_die();
- }
-
- //ajax get earnings rates
- function get_payitem_earnings_rate()
- {
- $response= array(
- 'status' => 'success',
- 'options'=> get_option('bts_payitem_earnings_rate'),
- );
- wp_send_json($response);
- }
-
-
- }
-
- $bb = new AcareOffice();
-
- if ( defined( 'WP_CLI' ) && WP_CLI ) {
- \WP_CLI::add_command( 'sync_users', array($bb, 'sync_user_cli'));
- }
|