diff -r 877f952ae2bd -r 6b6c2214f778 web/lib/Zend/Gdata/Analytics/DataQuery.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/lib/Zend/Gdata/Analytics/DataQuery.php Thu Mar 21 19:52:38 2013 +0100 @@ -0,0 +1,403 @@ +"; + const LESS = ">"; + const GREATER_EQUAL = ">="; + const LESS_EQUAL = "<="; + const CONTAINS = "=@"; + const CONTAINS_NOT ="!@"; + const REGULAR ="=~"; + const REGULAR_NOT ="!~"; + + /** + * @var string + */ + protected $_profileId; + /** + * @var array + */ + protected $_dimensions = array(); + /** + * @var array + */ + protected $_metrics = array(); + /** + * @var array + */ + protected $_sort = array(); + /** + * @var array + */ + protected $_filters = array(); + + /** + * @param string $id + * @return Zend_Gdata_Analytics_DataQuery + */ + public function setProfileId($id) + { + $this->_profileId = $id; + return $this; + } + + /** + * @return string + */ + public function getProfileId() + { + return $this->_profileId; + } + + /** + * @param string $dimension + * @return Zend_Gdata_Analytics_DataQuery + */ + public function addDimension($dimension) + { + $this->_dimensions[$dimension] = true; + return $this; + } + + /** + * @param string $metric + * @return Zend_Gdata_Analytics_DataQuery + */ + public function addMetric($metric) + { + $this->_metrics[$metric] = true; + return $this; + } + + /** + * @return array + */ + public function getDimensions() + { + return $this->_dimensions; + } + + /** + * @return array + */ + public function getMetrics() + { + return $this->_metrics; + } + + /** + * @param string $dimension + * @return Zend_Gdata_Analytics_DataQuery + */ + public function removeDimension($dimension) + { + unset($this->_dimensions[$dimension]); + return $this; + } + /** + * @param string $metric + * @return Zend_Gdata_Analytics_DataQuery + */ + public function removeMetric($metric) + { + unset($this->_metrics[$metric]); + return $this; + } + /** + * @param string $value + * @return Zend_Gdata_Analytics_DataQuery + */ + public function setStartDate($date) + { + $this->setParam("start-date", $date); + return $this; + } + /** + * @param string $value + * @return Zend_Gdata_Analytics_DataQuery + */ + public function setEndDate($date) + { + $this->setParam("end-date", $date); + return $this; + } + + /** + * @param string $filter + * @return Zend_Gdata_Analytics_DataQuery + */ + public function addFilter($filter) + { + $this->_filters[] = array($filter, true); + return $this; + } + + /** + * @param string $filter + * @return Zend_Gdata_Analytics_DataQuery + */ + public function addOrFilter($filter) + { + $this->_filters[] = array($filter, false); + return $this; + } + + /** + * @param string $sort + * @param boolean[optional] $descending + * @return Zend_Gdata_Analytics_DataQuery + */ + public function addSort($sort, $descending=false) + { + // add to sort storage + $this->_sort[] = ($descending?'-':'').$sort; + return $this; + } + + /** + * @return Zend_Gdata_Analytics_DataQuery + */ + public function clearSort() + { + $this->_sort = array(); + return $this; + } + + /** + * @param string $segment + * @return Zend_Gdata_Analytics_DataQuery + */ + public function setSegment($segment) + { + $this->setParam('segment', $segment); + return $this; + } + + /** + * @return string url + */ + public function getQueryUrl() + { + $uri = $this->_defaultFeedUri; + if (isset($this->_url)) { + $uri = $this->_url; + } + + $dimensions = $this->getDimensions(); + if (!empty($dimensions)) { + $this->setParam('dimensions', implode(",", array_keys($dimensions))); + } + + $metrics = $this->getMetrics(); + if (!empty($metrics)) { + $this->setParam('metrics', implode(",", array_keys($metrics))); + } + + // profile id (ga:tableId) + if ($this->getProfileId() != null) { + $this->setParam('ids', 'ga:'.ltrim($this->getProfileId(), "ga:")); + } + + // sorting + if ($this->_sort) { + $this->setParam('sort', implode(",", $this->_sort)); + } + + // filtering + $filters = ""; + foreach ($this->_filters as $filter) { + $filters.=($filter[1]===true?';':',').$filter[0]; + } + + if ($filters!="") { + $this->setParam('filters', ltrim($filters, ",;")); + } + + $uri .= $this->getQueryString(); + return $uri; + } +}