Forked from justenj/Google auth and work with google speadsheets
Created
April 4, 2016 11:13
-
-
Save Burick/c194d93a685a48a64cc4047f01d92a04 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The first create app to link https://console.developers.google.com | |
* Later save json file of created app to own site. Path to the file transmit to GoogleHandler initialize method | |
* | |
* composer.json | |
* { | |
* "require": { | |
* "asimlqt/php-google-spreadsheet-client": "2.3.*", // https://github.com/asimlqt/php-google-spreadsheet-client | |
* "google/apiclient": "^2.0.0@RC" | |
* } | |
* } | |
* | |
*/ | |
class GoogleHandler | |
{ | |
public function initialize($credentials_file) | |
{ | |
putenv("GOOGLE_APPLICATION_CREDENTIALS=service-account-credentials.json"); | |
$client = new Google_Client(); | |
$credentials_file = $credentials_file; | |
if (file_exists($credentials_file)) { | |
$client->setAuthConfig($credentials_file); | |
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) { | |
$client->useApplicationDefaultCredentials(); | |
} else { | |
return false; | |
} | |
$appName = $this->modx->getOption('google_app_name'); | |
$client->setApplicationName($appName); | |
$client->setScopes([ | |
'https://spreadsheets.google.com/feeds' | |
]); | |
$tokenArray = $client->fetchAccessTokenWithAssertion(); | |
return $tokenArray["access_token"]; | |
} | |
} | |
/** | |
* Example work with Google API | |
*/ | |
require_once dirname(dirname(dirname(dirname(__FILE__)))).'/index.php'; // include $modx | |
require_once(MODX_CORE_PATH . 'vendor/autoload.php'); | |
include_once MODX_CORE_PATH . 'vendor/google/apiclient/examples/templates/base.php'; | |
use Google\Spreadsheet\DefaultServiceRequest; | |
use Google\Spreadsheet\ServiceRequestFactory; | |
$google_handler = new GoogleHandler($modx); | |
$accessToken = $google_handler->initialize(MODX_CORE_PATH . your_path_to_file); | |
if (!$accessToken) { | |
return false; | |
} | |
$row = array( | |
'команда' => !empty($_POST['team']) ? $_POST['team'] : '', | |
"учреждение" => !empty($_POST['institution']) ? $_POST['institution'] : '', | |
"город" => !empty($_POST['city']) ? $_POST['city'] : '', | |
"регион" => !empty($_POST['region']) ? $_POST['region'] : '', | |
"фиоруководителя" => !empty($_POST['leader']) ? $_POST['leader'] : '', | |
"телефон" => !empty($_POST['phone']) ? $_POST['phone'] : '', | |
"e-mail" => !empty($_POST['email']) ? $_POST['email'] : '' | |
); | |
$serviceRequest = new DefaultServiceRequest($accessToken); | |
ServiceRequestFactory::setInstance($serviceRequest); | |
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService(); | |
$spreadsheetFeed = $spreadsheetService->getSpreadsheets(); | |
if ($type === 'Заявка на регистрацию.') { | |
$spreadsheet = $spreadsheetFeed->getByTitle( first_spreadsheet ); | |
} else { | |
$spreadsheet = $spreadsheetFeed->getByTitle( second_spreadsheet ); | |
} | |
$worksheetFeed = $spreadsheet->getWorksheets(); | |
$subject = $google_handler->getSubject($_POST['subject']); | |
$worksheet = $worksheetFeed->getByTitle($subject['title']); | |
if (!$worksheet) { | |
$worksheet = $spreadsheet->addWorksheet($subject['title'], 50, count($row)); | |
$cellFeed = $worksheet->getCellFeed(); | |
$i = 1; | |
foreach ($row as $key => $value) { | |
$cellFeed->editCell(1, $i, $key); | |
$i++; | |
} | |
} | |
$listFeed = $worksheet->getListFeed(); | |
$listFeed->insert($row); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment