Source for file photo.php

Documentation is available at photo.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Google
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Google Picasa data class for the Joomla Platform.
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Google
  17.  * @since       12.3
  18.  */
  19. {
  20.     /**
  21.      * @var    SimpleXMLElement  The photo's XML
  22.      * @since  12.3
  23.      */
  24.     protected $xml;
  25.  
  26.     /**
  27.      * Constructor.
  28.      *
  29.      * @param   SimpleXMLElement  $xml      XML from Google
  30.      * @param   JRegistry         $options  Google options object
  31.      * @param   JGoogleAuth       $auth     Google data http client object
  32.      *
  33.      * @since   12.3
  34.      */
  35.     public function __construct(SimpleXMLElement $xmlJRegistry $options nullJGoogleAuth $auth null)
  36.     {
  37.         $this->xml = $xml;
  38.  
  39.         parent::__construct($options$auth);
  40.  
  41.         if (isset($this->auth&& !$this->auth->getOption('scope'))
  42.         {
  43.             $this->auth->setOption('scope''https://picasaweb.google.com/data/');
  44.         }
  45.     }
  46.  
  47.     /**
  48.      * Method to delete a Picasa photo
  49.      *
  50.      * @param   mixed  $match  Check for most up to date photo
  51.      *
  52.      * @return  boolean  Success or failure.
  53.      *
  54.      * @since   12.3
  55.      * @throws UnexpectedValueException
  56.      */
  57.     public function delete($match '*')
  58.     {
  59.         if ($this->isAuthenticated())
  60.         {
  61.             $url $this->getLink();
  62.  
  63.             if ($match === true)
  64.             {
  65.                 $match $this->xml->xpath('./@gd:etag');
  66.                 $match $match[0];
  67.             }
  68.  
  69.             try
  70.             {
  71.                 $jdata $this->query($urlnullarray('GData-Version' => 2'If-Match' => $match)'delete');
  72.             }
  73.             catch (Exception $e)
  74.             {
  75.                 if (strpos($e->getMessage()'Error code 412 received requesting data: Mismatch: etags'=== 0)
  76.                 {
  77.                     throw new RuntimeException("Etag match failed: `$match`.");
  78.                 }
  79.                 throw $e;
  80.             }
  81.  
  82.             if ($jdata->body != '')
  83.             {
  84.                 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
  85.             }
  86.             $this->xml = null;
  87.  
  88.             return true;
  89.         }
  90.         else
  91.         {
  92.             return false;
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Method to get the photo link
  98.      *
  99.      * @param   string  $type  Type of link to return
  100.      *
  101.      * @return  string  Link or false on failure
  102.      *
  103.      * @since   12.3
  104.      */
  105.     public function getLink($type 'edit')
  106.     {
  107.         $links $this->xml->link;
  108.  
  109.         foreach ($links as $link)
  110.         {
  111.             if ($link->attributes()->rel == $type)
  112.             {
  113.                 return (string) $link->attributes()->href;
  114.             }
  115.         }
  116.         return false;
  117.     }
  118.  
  119.     /**
  120.      * Method to get the photo's URL
  121.      *
  122.      * @return  string  Link
  123.      *
  124.      * @since   12.3
  125.      */
  126.     public function getURL()
  127.     {
  128.         return (string) $this->xml->children()->content->attributes()->src;
  129.     }
  130.  
  131.     /**
  132.      * Method to get the photo's thumbnails
  133.      *
  134.      * @return  array  An array of thumbnails
  135.      *
  136.      * @since   12.3
  137.      */
  138.     public function getThumbnails()
  139.     {
  140.         $thumbs array();
  141.  
  142.         foreach ($this->xml->children('media'true)->group->thumbnail as $item)
  143.         {
  144.             $url = (string) $item->attributes()->url;
  145.             $width = (int) $item->attributes()->width;
  146.             $height = (int) $item->attributes()->height;
  147.             $thumbs[$widtharray('url' => $url'w' => $width'h' => $height);
  148.         }
  149.         return $thumbs;
  150.     }
  151.  
  152.     /**
  153.      * Method to get the title of the photo
  154.      *
  155.      * @return  string  Photo title
  156.      *
  157.      * @since   12.3
  158.      */
  159.     public function getTitle()
  160.     {
  161.         return (string) $this->xml->children()->title;
  162.     }
  163.  
  164.     /**
  165.      * Method to get the summary of the photo
  166.      *
  167.      * @return  string  Photo description
  168.      *
  169.      * @since   12.3
  170.      */
  171.     public function getSummary()
  172.     {
  173.         return (string) $this->xml->children()->summary;
  174.     }
  175.  
  176.     /**
  177.      * Method to get the access level of the photo
  178.      *
  179.      * @return  string  Photo access level
  180.      *
  181.      * @since   12.3
  182.      */
  183.     public function getAccess()
  184.     {
  185.         return (string) $this->xml->children('gphoto'true)->access;
  186.     }
  187.  
  188.     /**
  189.      * Method to get the time of the photo
  190.      *
  191.      * @return  double  Photo time
  192.      *
  193.      * @since   12.3
  194.      */
  195.     public function getTime()
  196.     {
  197.         return (double) $this->xml->children('gphoto'true)->timestamp 1000;
  198.     }
  199.  
  200.     /**
  201.      * Method to get the size of the photo
  202.      *
  203.      * @return  int  Photo size
  204.      *
  205.      * @since   12.3
  206.      */
  207.     public function getSize()
  208.     {
  209.         return (int) $this->xml->children('gphoto'true)->size;
  210.     }
  211.  
  212.     /**
  213.      * Method to get the height of the photo
  214.      *
  215.      * @return  int  Photo height
  216.      *
  217.      * @since   12.3
  218.      */
  219.     public function getHeight()
  220.     {
  221.         return (int) $this->xml->children('gphoto'true)->height;
  222.     }
  223.  
  224.     /**
  225.      * Method to get the width of the photo
  226.      *
  227.      * @return  int  Photo width
  228.      *
  229.      * @since   12.3
  230.      */
  231.     public function getWidth()
  232.     {
  233.         return (int) $this->xml->children('gphoto'true)->width;
  234.     }
  235.  
  236.     /**
  237.      * Method to set the title of the photo
  238.      *
  239.      * @param   string  $title  New photo title
  240.      *
  241.      * @return  JGoogleDataPicasaPhoto  The object for method chaining
  242.      *
  243.      * @since   12.3
  244.      */
  245.     public function setTitle($title)
  246.     {
  247.         $this->xml->children()->title $title;
  248.  
  249.         return $this;
  250.     }
  251.  
  252.     /**
  253.      * Method to set the summary of the photo
  254.      *
  255.      * @param   string  $summary  New photo description
  256.      *
  257.      * @return  JGoogleDataPicasaPhoto  The object for method chaining
  258.      *
  259.      * @since   12.3
  260.      */
  261.     public function setSummary($summary)
  262.     {
  263.         $this->xml->children()->summary $summary;
  264.  
  265.         return $this;
  266.     }
  267.  
  268.     /**
  269.      * Method to set the access level of the photo
  270.      *
  271.      * @param   string  $access  New photo access level
  272.      *
  273.      * @return  JGoogleDataPicasaPhoto  The object for method chaining
  274.      *
  275.      * @since   12.3
  276.      */
  277.     public function setAccess($access)
  278.     {
  279.         $this->xml->children('gphoto'true)->access $access;
  280.  
  281.         return $this;
  282.     }
  283.  
  284.     /**
  285.      * Method to set the time of the photo
  286.      *
  287.      * @param   int  $time  New photo time
  288.      *
  289.      * @return  JGoogleDataPicasaPhoto  The object for method chaining
  290.      *
  291.      * @since   12.3
  292.      */
  293.     public function setTime($time)
  294.     {
  295.         $this->xml->children('gphoto'true)->timestamp $time 1000;
  296.  
  297.         return $this;
  298.     }
  299.  
  300.     /**
  301.      * Method to modify a Picasa Photo
  302.      *
  303.      * @param   string  $match  Optional eTag matching parameter
  304.      *
  305.      * @return  mixed  Data from Google.
  306.      *
  307.      * @since   12.3
  308.      */
  309.     public function save($match '*')
  310.     {
  311.         if ($this->isAuthenticated())
  312.         {
  313.             $url $this->getLink();
  314.  
  315.             if ($match === true)
  316.             {
  317.                 $match $this->xml->xpath('./@gd:etag');
  318.                 $match $match[0];
  319.             }
  320.  
  321.             try
  322.             {
  323.                 $headers array('GData-Version' => 2'Content-type' => 'application/atom+xml''If-Match' => $match);
  324.                 $jdata $this->query($url$this->xml->asXML()$headers'put');
  325.             }
  326.             catch (Exception $e)
  327.             {
  328.                 if (strpos($e->getMessage()'Error code 412 received requesting data: Mismatch: etags'=== 0)
  329.                 {
  330.                     throw new RuntimeException("Etag match failed: `$match`.");
  331.                 }
  332.                 throw $e;
  333.             }
  334.  
  335.             $this->xml = $this->safeXML($jdata->body);
  336.  
  337.             return $this;
  338.         }
  339.         else
  340.         {
  341.             return false;
  342.         }
  343.     }
  344.  
  345.     /**
  346.      * Refresh photo data
  347.      *
  348.      * @return  mixed  Data from Google
  349.      *
  350.      * @since   12.3
  351.      */
  352.     public function refresh()
  353.     {
  354.         if ($this->isAuthenticated())
  355.         {
  356.             $url $this->getLink();
  357.             $jdata $this->query($urlnullarray('GData-Version' => 2));
  358.             $this->xml = $this->safeXML($jdata->body);
  359.  
  360.             return $this;
  361.         }
  362.         else
  363.         {
  364.             return false;
  365.         }
  366.     }
  367. }

Documentation generated on Tue, 19 Nov 2013 15:10:31 +0100 by phpDocumentor 1.4.3