-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdata.php
213 lines (182 loc) · 6.66 KB
/
gdata.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
require_once 'settings.php';
set_include_path( get_include_path() . ':' . ROOT . "/lib/ZendGdata-1.7.6/library" );
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
Zend_Loader::loadClass('Zend_Gdata_App_AuthException');
Zend_Loader::loadClass('Zend_Http_Client');
class DB
{
public function __construct()
{
try {
$client = Zend_Gdata_ClientLogin::getHttpClient(EMAIL, PASS,
Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME);
} catch (Zend_Gdata_App_AuthException $ae) {
exit("Error: ". $ae->getMessage() ."\nCredentials provided were email: [xxx] and password [xxx].\n");
}
$this->gdClient = new Zend_Gdata_Spreadsheets($client);
$this->currKey = '';
$this->currWkshtId = '';
$this->listFeed = '';
$this->rowCount = 0;
$this->columnCount = 0;
$this->listFeedData = null;
$this->rowIndex = 0;
$this->changeSpreadsheet(SHEET);
$this->changeWorksheet(WKSHT);
}
public function changeSpreadsheet($name)
{
$feed = $this->gdClient->getSpreadsheetFeed();
$index = $this->getIndexOf($name, $feed);
$currKey = split('/', $feed->entries[$index]->id->text);
$this->currKey = $currKey[5];
}
public function changeWorksheet($name)
{
$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query->setSpreadsheetKey($this->currKey);
$feed = $this->gdClient->getWorksheetFeed($query);
$index = $this->getIndexOf($name, $feed);
$currWkshtId = split('/', $feed->entries[$index]->id->text);
$this->currWkshtId = $currWkshtId[8];
}
private function getData()
{
$query = $this->createListQuery();
$query->setReverse('true');
$listFeed = $this->gdClient->getListFeed($query);
$this->listFeedData = $listFeed;
return;
/*
$rowData = $listFeed->entries[0]->getCustom();
foreach($rowData as $customEntry) {
$data[$customEntry->getColumnName()] = "a";
}
print_r( $data );
*/
foreach($listFeed->entries as $entries) {
$rowData = $entries->getCustom();
foreach($rowData as $customEntry) {
echo $customEntry->getColumnName() . " = " . $customEntry->getText() . "<br/>";
}
}
}
public function getNextRow()
{
if( $this->listFeedData == null ) {
$this->getData();
$this->rowIndex = 0;
}
if(!isset($this->listFeedData->entries[$this->rowIndex]))
return null;
$rowData = $this->listFeedData->entries[$this->rowIndex]->getCustom();
$data = array();
foreach($rowData as $customEntry) {
$data = array_merge( $data,
array($customEntry->getColumnName() => $customEntry->getText()) );
}
$this->rowIndex++;
return $data;
}
public function insertRow($data)
{
$this->gdClient->insertRow($data, $this->currKey, $this->currWkshtId);
}
public function updateRows($field, $value, $newdata)
{
$query = $this->createListQuery();
$query->spreadsheetQuery = "$field=$value";
try{ $listFeed = $this->gdClient->getListFeed($query); }
catch (Exception $e) { die( $e->getMessage() ); return null; }
if( $listFeed == null || !isset($listFeed->entries[0]) ) {
return null;
}
foreach( $listFeed->entries as $entry ) {
try{ $new_entry = $this->gdClient->updateRow( $entry, $newdata ); }
catch (Exception $e){ die("got exception!!:" . $e->getMessage()); return -2; }
//print_r( $new_entry );
if( $new_entry instanceof Zend_Gdata_Spreadsheets_ListEntry ) {
try{ $response = $new_entry->save(); }
catch (Exception $e) { die("got exception!: " . $e->getMessage()); return -1; }
}
}
return 0;
}
public function getUniqueRow($key, $text)
{
$query = $this->createListQuery();
$query->spreadsheetQuery = "$key=$text";
try{ $listFeed = $this->gdClient->getListFeed($query); }
catch (Exception $e){ die( "caught exception: " . $e->getMessage() ); return null; }
if( $listFeed == null || !isset($listFeed->entries[0]) ) {
return null;
}
$rowData = $listFeed->entries[0]->getCustom();
$row = array();
foreach($rowData as $customEntry) {
$row = array_merge( $row,
array($customEntry->getColumnName() => $customEntry->getText()) );
}
return $row;
}
public function getUniqueRows($key, $text)
{
$query = $this->createListQuery();
$query->spreadsheetQuery = "$key=$text";
try {
$listFeed = $this->gdClient->getListFeed($query);
} catch (Exception $e) {
die( "caught exception: " . $e->getMessage() );
return null;
}
if( $listFeed == null || !isset($listFeed->entries[0]) ) {
return null;
}
$rows = array();
foreach($listFeed->entries as $rawRowData) {
$rowData = $rawRowData->getCustom();
$row = array();
foreach($rowData as $customEntry) {
$row = array_merge( $row,
array($customEntry->getColumnName() => $customEntry->getText()) );
}
$rows = array_merge( $rows, array($row) );
}
return $rows;
}
private function createListQuery()
{
$query = new Zend_Gdata_Spreadsheets_ListQuery();
$query->setSpreadsheetKey($this->currKey);
$query->setWorksheetId($this->currWkshtId);
return $query;
}
private function getIndexOf($item, $feed)
{
$i = 0;
foreach($feed->entries as $entry) {
if( $entry->title->text == $item )
return $i;
$i++;
}
}
private function printFeed($feed)
{
$i = 0;
foreach($feed->entries as $entry) {
if ($entry instanceof Zend_Gdata_Spreadsheets_CellEntry)
print $entry->title->text . ' ' . $entry->content->text . "<br/>";
else if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry)
print $i . ' ' . $entry->title->text . ' | ' . $entry->content->text . "<br/>";
else {
print $i . ' ' . $entry->title->text . "foo<br/>";
}
$i++;
}
}
}
?>