-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZohoReportsDatabase.php
72 lines (63 loc) · 2.56 KB
/
ZohoReportsDatabase.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
<?php
class ZohoReportsDatabase {
const IMPORTTYPE_APPEND = "APPEND";
const IMPORTTYPE_TRUNCATEADD = "TRUNCATEADD";
const IMPORTTYPE_UPDATEADD = "UPDATEADD";
const IMPORTERROR_ABORT = "ABORT";
const IMPORTERROR_SKIPROW = "SKIPROW";
const IMPORTERROR_SETCOLUMNEMPTY = "SETCOLUMNEMPTY";
public $name;
public $owner;
protected $zohoReports;
public function __construct($name, $owner, ZohoReports $zohoReports) {
$this->name = $name;
$this->owner = $owner;
$this->zohoReports = $zohoReports;
}
public function call($tableName, $action, $format, $params=array(), $options=array()) {
return $this->zohoReports->call($this->owner, $this->name, $tableName, $action, $format, $params, $options);
}
/**
* @see http://zohoreportsapi.wiki.zoho.com/Applying-Filters.html
*/
protected function compileCriteria($criteria, $isAnd=true) {
if (is_array($criteria)) {
$parts = array();
foreach ($criteria as $key => $val) {
if (is_array($val)) {
$parts[] = '(' . $this->compileCriteria($val, !$isAnd) . ')';
} else {
$parts[] = '"' . addslashes($key) . '"=\'' . addslashes($val) . '\'';
}
}
return implode(($isAnd ? ' and ' : ' or '), $parts);
} else {
return $criteria;
}
}
/**
* @see http://zohoreportsapi.wiki.zoho.com/Export.html
*/
public function export($tableName, $format=ZohoReports::FORMAT_JSON, $criteria=null, $params=array()) {
$options = array();
if ($criteria !== null) {
$options['PostData'] = array('ZOHO_CRITERIA' => $this->compileCriteria($criteria));
}
return $this->call($tableName, 'EXPORT', $format, $params, $options);
}
/**
* @see https://zohoreportsapi.wiki.zoho.com/Importing-CSV-File.html
*/
public function importCsv($tableName, $path, $importType=self::IMPORTTYPE_APPEND, $onError=self::IMPORTERROR_ABORT, $autoIdentify=true, $params=array()) {
if (!is_readable($path)) {
throw new Exception('Unabled to read file: ' . $path);
}
$options = array('PostData' => $params+array(
'ZOHO_AUTO_IDENTIFY' => ($autoIdentify ? 'true' : 'false'),
'ZOHO_ON_IMPORT_ERROR' => $onError,
'ZOHO_IMPORT_TYPE' => $importType,
'ZOHO_FILE' => '@' . $path
));
return $this->call($tableName, 'IMPORT', ZohoReports::FORMAT_JSON, array(), $options);
}
}