[44] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | /**
|
---|
| 4 | * Zend Framework
|
---|
| 5 | *
|
---|
| 6 | * LICENSE
|
---|
| 7 | *
|
---|
| 8 | * This source file is subject to the new BSD license that is bundled
|
---|
| 9 | * with this package in the file LICENSE.txt.
|
---|
| 10 | * It is also available through the world-wide-web at this URL:
|
---|
| 11 | * http://framework.zend.com/license/new-bsd
|
---|
| 12 | * If you did not receive a copy of the license and are unable to
|
---|
| 13 | * obtain it through the world-wide-web, please send an email
|
---|
| 14 | * to license@zend.com so we can send you a copy immediately.
|
---|
| 15 | *
|
---|
| 16 | * @category Zend
|
---|
| 17 | * @package Zend_Gdata
|
---|
| 18 | * @subpackage Health
|
---|
| 19 | * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
| 20 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * @see Zend_Gdata
|
---|
| 25 | */
|
---|
| 26 | require_once 'Zend/Gdata.php';
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * @see Zend_Gdata_Health_ProfileFeed
|
---|
| 30 | */
|
---|
| 31 | require_once 'Zend/Gdata/Health/ProfileFeed.php';
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @see Zend_Gdata_Health_ProfileListFeed
|
---|
| 35 | */
|
---|
| 36 | require_once 'Zend/Gdata/Health/ProfileListFeed.php';
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * @see Zend_Gdata_Health_ProfileListEntry
|
---|
| 40 | */
|
---|
| 41 | require_once 'Zend/Gdata/Health/ProfileListEntry.php';
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * @see Zend_Gdata_Health_ProfileEntry
|
---|
| 45 | */
|
---|
| 46 | require_once 'Zend/Gdata/Health/ProfileEntry.php';
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * Service class for interacting with the Google Health Data API
|
---|
| 50 | *
|
---|
| 51 | * @link http://code.google.com/apis/health
|
---|
| 52 | *
|
---|
| 53 | * @category Zend
|
---|
| 54 | * @package Zend_Gdata
|
---|
| 55 | * @subpackage Health
|
---|
| 56 | * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
| 57 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
| 58 | */
|
---|
| 59 | class Zend_Gdata_Health extends Zend_Gdata
|
---|
| 60 | {
|
---|
| 61 | /**
|
---|
| 62 | * URIs of the AuthSub/OAuth feeds.
|
---|
| 63 | */
|
---|
| 64 | const AUTHSUB_PROFILE_FEED_URI =
|
---|
| 65 | 'https://www.google.com/health/feeds/profile/default';
|
---|
| 66 | const AUTHSUB_REGISTER_FEED_URI =
|
---|
| 67 | 'https://www.google.com/health/feeds/register/default';
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * URIs of the ClientLogin feeds.
|
---|
| 71 | */
|
---|
| 72 | const CLIENTLOGIN_PROFILELIST_FEED_URI =
|
---|
| 73 | 'https://www.google.com/health/feeds/profile/list';
|
---|
| 74 | const CLIENTLOGIN_PROFILE_FEED_URI =
|
---|
| 75 | 'https://www.google.com/health/feeds/profile/ui';
|
---|
| 76 | const CLIENTLOGIN_REGISTER_FEED_URI =
|
---|
| 77 | 'https://www.google.com/health/feeds/register/ui';
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * Authentication service names for Google Health and the H9 Sandbox.
|
---|
| 81 | */
|
---|
| 82 | const HEALTH_SERVICE_NAME = 'health';
|
---|
| 83 | const H9_SANDBOX_SERVICE_NAME = 'weaver';
|
---|
| 84 |
|
---|
| 85 | /**
|
---|
| 86 | * Profile ID used for all API interactions. This can only be set when
|
---|
| 87 | * using ClientLogin for authentication.
|
---|
| 88 | *
|
---|
| 89 | * @var string
|
---|
| 90 | */
|
---|
| 91 | private $_profileID = null;
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * True if API calls should be made to the H9 developer sandbox at /h9
|
---|
| 95 | * rather than /health
|
---|
| 96 | *
|
---|
| 97 | * @var bool
|
---|
| 98 | */
|
---|
| 99 | private $_useH9Sandbox = false;
|
---|
| 100 |
|
---|
| 101 | public static $namespaces =
|
---|
| 102 | array('ccr' => 'urn:astm-org:CCR',
|
---|
| 103 | 'batch' => 'http://schemas.google.com/gdata/batch',
|
---|
| 104 | 'h9m' => 'http://schemas.google.com/health/metadata',
|
---|
| 105 | 'gAcl' => 'http://schemas.google.com/acl/2007',
|
---|
| 106 | 'gd' => 'http://schemas.google.com/g/2005');
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * Create Zend_Gdata_Health object
|
---|
| 110 | *
|
---|
| 111 | * @param Zend_Http_Client $client (optional) The HTTP client to use when
|
---|
| 112 | * when communicating with the Google Health servers.
|
---|
| 113 | * @param string $applicationId The identity of the application in the form
|
---|
| 114 | * of Company-AppName-Version
|
---|
| 115 | * @param bool $useH9Sandbox True if the H9 Developer's Sandbox should be
|
---|
| 116 | * used instead of production Google Health.
|
---|
| 117 | */
|
---|
| 118 | public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0', $useH9Sandbox = false)
|
---|
| 119 | {
|
---|
| 120 | $this->registerPackage('Zend_Gdata_Health');
|
---|
| 121 | $this->registerPackage('Zend_Gdata_Health_Extension_Ccr');
|
---|
| 122 | parent::__construct($client, $applicationId);
|
---|
| 123 | $this->_useH9Sandbox = $useH9Sandbox;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 127 | * Gets the id of the user's profile
|
---|
| 128 | *
|
---|
| 129 | * @return string The profile id
|
---|
| 130 | */
|
---|
| 131 | public function getProfileID()
|
---|
| 132 | {
|
---|
| 133 | return $this->_profileID;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * Sets which of the user's profiles will be used
|
---|
| 138 | *
|
---|
| 139 | * @param string $id The profile ID
|
---|
| 140 | * @return Zend_Gdata_Health Provides a fluent interface
|
---|
| 141 | */
|
---|
| 142 | public function setProfileID($id) {
|
---|
| 143 | $this->_profileID = $id;
|
---|
| 144 | return $this;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
| 148 | * Retrieves the list of profiles associated with the user's ClientLogin
|
---|
| 149 | * credentials.
|
---|
| 150 | *
|
---|
| 151 | * @param string $query The query of the feed as a URL or Query object
|
---|
| 152 | * @return Zend_Gdata_Feed
|
---|
| 153 | */
|
---|
| 154 | public function getHealthProfileListFeed($query = null)
|
---|
| 155 | {
|
---|
| 156 | if ($this->_httpClient->getClientLoginToken() === null) {
|
---|
| 157 | require_once 'Zend/Gdata/App/AuthException.php';
|
---|
| 158 | throw new Zend_Gdata_App_AuthException(
|
---|
| 159 | 'Profiles list feed is only available when using ClientLogin');
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | if($query === null) {
|
---|
| 163 | $uri = self::CLIENTLOGIN_PROFILELIST_FEED_URI;
|
---|
| 164 | } else if ($query instanceof Zend_Gdata_Query) {
|
---|
| 165 | $uri = $query->getQueryUrl();
|
---|
| 166 | } else {
|
---|
| 167 | $uri = $query;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | // use correct feed for /h9 or /health
|
---|
| 171 | if ($this->_useH9Sandbox) {
|
---|
| 172 | $uri = preg_replace('/\/health\//', '/h9/', $uri);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | return parent::getFeed($uri, 'Zend_Gdata_Health_ProfileListFeed');
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /**
|
---|
| 179 | * Retrieve a user's profile as a feed object. If ClientLogin is used, the
|
---|
| 180 | * profile associated with $this->_profileID is returned, otherwise
|
---|
| 181 | * the profile associated with the AuthSub token is read.
|
---|
| 182 | *
|
---|
| 183 | * @param mixed $query The query for the feed, as a URL or Query
|
---|
| 184 | * @return Zend_Gdata_Health_ProfileFeed
|
---|
| 185 | */
|
---|
| 186 | public function getHealthProfileFeed($query = null)
|
---|
| 187 | {
|
---|
| 188 | if ($this->_httpClient->getClientLoginToken() !== null &&
|
---|
| 189 | $this->getProfileID() == null) {
|
---|
| 190 | require_once 'Zend/Gdata/App/AuthException.php';
|
---|
| 191 | throw new Zend_Gdata_App_AuthException(
|
---|
| 192 | 'Profile ID must not be null. Did you call setProfileID()?');
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | if ($query instanceof Zend_Gdata_Query) {
|
---|
| 196 | $uri = $query->getQueryUrl();
|
---|
| 197 | } else if ($this->_httpClient->getClientLoginToken() !== null &&
|
---|
| 198 | $query == null) {
|
---|
| 199 | $uri = self::CLIENTLOGIN_PROFILE_FEED_URI . '/' . $this->getProfileID();
|
---|
| 200 | } else if ($query === null) {
|
---|
| 201 | $uri = self::AUTHSUB_PROFILE_FEED_URI;
|
---|
| 202 | } else {
|
---|
| 203 | $uri = $query;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | // use correct feed for /h9 or /health
|
---|
| 207 | if ($this->_useH9Sandbox) {
|
---|
| 208 | $uri = preg_replace('/\/health\//', '/h9/', $uri);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | return parent::getFeed($uri, 'Zend_Gdata_Health_ProfileFeed');
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /**
|
---|
| 215 | * Retrieve a profile entry object
|
---|
| 216 | *
|
---|
| 217 | * @param mixed $query The query for the feed, as a URL or Query
|
---|
| 218 | * @return Zend_Gdata_Health_ProfileEntry
|
---|
| 219 | */
|
---|
| 220 | public function getHealthProfileEntry($query = null)
|
---|
| 221 | {
|
---|
| 222 | if ($query === null) {
|
---|
| 223 | require_once 'Zend/Gdata/App/InvalidArgumentException.php';
|
---|
| 224 | throw new Zend_Gdata_App_InvalidArgumentException(
|
---|
| 225 | 'Query must not be null');
|
---|
| 226 | } else if ($query instanceof Zend_Gdata_Query) {
|
---|
| 227 | $uri = $query->getQueryUrl();
|
---|
| 228 | } else {
|
---|
| 229 | $uri = $query;
|
---|
| 230 | }
|
---|
| 231 | return parent::getEntry($uri, 'Zend_Gdata_Health_ProfileEntry');
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
| 235 | * Posts a new notice using the register feed. This function constructs
|
---|
| 236 | * the atom profile entry.
|
---|
| 237 | *
|
---|
| 238 | * @param string $subject The subject line of the notice
|
---|
| 239 | * @param string $body The message body of the notice
|
---|
| 240 | * @param string $bodyType The (optional) type of message body
|
---|
| 241 | * (text, xhtml, html, etc.)
|
---|
| 242 | * @param string $ccrXML The (optional) CCR to add to the user's profile
|
---|
| 243 | * @return Zend_Gdata_Health_ProfileEntry
|
---|
| 244 | */
|
---|
| 245 | public function sendHealthNotice($subject, $body, $bodyType = null, $ccrXML = null)
|
---|
| 246 | {
|
---|
| 247 | if ($this->_httpClient->getClientLoginToken()) {
|
---|
| 248 | $profileID = $this->getProfileID();
|
---|
| 249 | if ($profileID !== null) {
|
---|
| 250 | $uri = self::CLIENTLOGIN_REGISTER_FEED_URI . '/' . $profileID;
|
---|
| 251 | } else {
|
---|
| 252 | require_once 'Zend/Gdata/App/AuthException.php';
|
---|
| 253 | throw new Zend_Gdata_App_AuthException(
|
---|
| 254 | 'Profile ID must not be null. Did you call setProfileID()?');
|
---|
| 255 | }
|
---|
| 256 | } else {
|
---|
| 257 | $uri = self::AUTHSUB_REGISTER_FEED_URI;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | $entry = new Zend_Gdata_Health_ProfileEntry();
|
---|
| 261 | $entry->title = $this->newTitle($subject);
|
---|
| 262 | $entry->content = $this->newContent($body);
|
---|
| 263 | $entry->content->type = $bodyType ? $bodyType : 'text';
|
---|
| 264 | $entry->setCcr($ccrXML);
|
---|
| 265 |
|
---|
| 266 | // use correct feed for /h9 or /health
|
---|
| 267 | if ($this->_useH9Sandbox) {
|
---|
| 268 | $uri = preg_replace('/\/health\//', '/h9/', $uri);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | return $this->insertEntry($entry, $uri, 'Zend_Gdata_Health_ProfileEntry');
|
---|
| 272 | }
|
---|
| 273 | }
|
---|