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