HEX
Server: LiteSpeed
System: Linux cp13-tx.privatesystems.net 4.18.0-553.40.1.lve.el8.x86_64 #1 SMP Wed Feb 12 18:54:57 UTC 2025 x86_64
User: storetra12 (2696)
PHP: 8.2.32
Disabled: syslog
Upload Files
File: /home/storetra12/www/hubstaff-operations/export-entries.php
<?php 


require_once('../wp-load.php');

require_once('global-vars.php');



function write_to_csv_write_log($message) {

    // Get WordPress root directory

    $log_file = ABSPATH . 'user_wise_csv_write_log.txt';



    // Format message with timestamp

    $log_message = "[" . date("Y-m-d H:i:s") . "] " . $message . PHP_EOL;



    // Write to log file (append mode)

    file_put_contents($log_file, $log_message, FILE_APPEND | LOCK_EX);

}



function convert_time_to_sheet_duration($time_str) {

    list($hours, $minutes, $seconds) = explode(':', $time_str);

    $duration = ($hours / 24) + ($minutes / 1440) + ($seconds / 86400);

    return $duration;

}



function convert_time_to_sheet_duration_duration_row($time_str) {

    list($hours, $minutes, $seconds) = explode(':', $time_str);



    // Ensure two digits + milliseconds

    $formatted_time = sprintf("%02d:%02d:%02d.000", $hours, $minutes, $seconds);



    // Return as plain text (so Google Sheets won't auto-format it)

    return $formatted_time;

}







include 'googleapiclient/vendor/autoload.php';

putenv('GOOGLE_APPLICATION_CREDENTIALS=/home/storetra12/public_html/hubstaff-operations/googleapiclient/new.json');



$client = new Google_Client();

$client->useApplicationDefaultCredentials();

$client->setScopes([Google_Service_Sheets::SPREADSHEETS]);

$service = new Google_Service_Sheets($client);

$spreadsheetId = '1c9OJrH3gfYdU0eZgWyIH71Ym3qrjn4MCMUM79tH4RCc';

$spreadsheet = $service->spreadsheets->get($spreadsheetId);

$sheets = $spreadsheet->getSheets();



// Your Google Sheet ID and range

$range = 'Sheet1!A2'; // Starting cell



global $wpdb;

$hubstaff_user_wise = $wpdb->prefix . 'hubstaff_user_wise_record';

$projects_array = $wpdb->get_results("SELECT * FROM $hubstaff_user_wise where csv_entry = 'n'");



$csv_array_write = [];



foreach ($projects_array as $key => $row) {



  $csv_status = $row->csv_entry;

  // $csv_status = 'n';

      

      $entry_id = $row->id;

      $formated_duration = convert_time_to_sheet_duration($row->hours);

      $new_row_duration = convert_time_to_sheet_duration_duration_row($row->hours);

      



      $csv_array_write[] = [

          $row->project_name,

          $row->member_name,

          $formated_duration,

          $row->record_date,

          $new_row_duration,
          
          $row->activity,

      ];





          $result = $wpdb->update(

                  $hubstaff_user_wise,

                  [

                      'csv_entry'  => 'y',                      

                  ],

                  [ 'id' => $entry_id ], // WHERE clause`

                  [ '%s' ],            // Value formats

                  [ '%d' ]                         // WHERE clause format

              );



          if($result){

                  write_to_csv_write_log("CSV WRITE SUCCESFULLY FOR Project : $row->project_name  MEMBER: $row->member_name  ");   

          }

 

}





// print_r($csv_array_write);

// die();



$body = new Google_Service_Sheets_ValueRange([

  'values' => $csv_array_write

]);



$params = [

  'valueInputOption' => 'RAW' // or 'USER_ENTERED'

];



// Write to sheet

$result = $service->spreadsheets_values->append(

  $spreadsheetId,

  $range,

  $body,

  $params

);





$updates = $result->getUpdates();

if ($updates) {

    $updatedCells = $updates->getUpdatedCells();

    echo "Updated cells: " . $updatedCells;

}



// echo $result->getUpdatedCells() . " cells updated.\n";



 ?>