Source for file adsense.php

Documentation is available at adsense.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 Adsense data class for the Joomla Platform.
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Google
  17.  * @since       12.3
  18.  */
  19. {
  20.     /**
  21.      * Constructor.
  22.      *
  23.      * @param   JRegistry    $options  Google options object
  24.      * @param   JGoogleAuth  $auth     Google data http client object
  25.      *
  26.      * @since   12.3
  27.      */
  28.     public function __construct(JRegistry $options nullJGoogleAuth $auth null)
  29.     {
  30.         parent::__construct($options$auth);
  31.  
  32.         if (isset($this->auth&& !$this->auth->getOption('scope'))
  33.         {
  34.             $this->auth->setOption('scope''https://www.googleapis.com/auth/adsense');
  35.         }
  36.     }
  37.  
  38.     /**
  39.      * Method to get an Adsense account's settings from Google
  40.      *
  41.      * @param   string   $accountID    ID of account to get
  42.      * @param   boolean  $subaccounts  Include list of subaccounts
  43.      *
  44.      * @return  mixed  Data from Google
  45.      *
  46.      * @since   12.3
  47.      */
  48.     public function getAccount($accountID$subaccounts true)
  49.     {
  50.         if ($this->isAuthenticated())
  51.         {
  52.             $url 'https://www.googleapis.com/adsense/v1.1/accounts/' urlencode($accountID($subaccounts '?tree=true' '');
  53.             $jdata $this->query($url);
  54.  
  55.             if ($data json_decode($jdata->bodytrue))
  56.             {
  57.                 return $data;
  58.             }
  59.             else
  60.             {
  61.                 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
  62.             }
  63.         }
  64.         else
  65.         {
  66.             return false;
  67.         }
  68.     }
  69.  
  70.     /**
  71.      * Method to retrieve a list of AdSense accounts from Google
  72.      *
  73.      * @param   array  $options   Search settings
  74.      * @param   int    $maxpages  Maximum number of pages of accounts to return
  75.      *
  76.      * @return  mixed  Data from Google
  77.      *
  78.      * @since   12.3
  79.      * @throws UnexpectedValueException
  80.      */
  81.     public function listAccounts($options array()$maxpages 1)
  82.     {
  83.         if ($this->isAuthenticated())
  84.         {
  85.             $next array_key_exists('nextPageToken'$options$options['nextPage'null;
  86.             unset($options['nextPageToken']);
  87.             $url 'https://www.googleapis.com/adsense/v1.1/accounts?' http_build_query($options);
  88.  
  89.             return $this->listGetData($url$maxpages$next);
  90.         }
  91.         else
  92.         {
  93.             return false;
  94.         }
  95.     }
  96.  
  97.     /**
  98.      * Method to retrieve a list of AdSense clients from Google
  99.      *
  100.      * @param   string  $accountID  ID of account to list the clients from
  101.      * @param   array   $options    Search settings
  102.      * @param   int     $maxpages   Maximum number of pages of accounts to return
  103.      *
  104.      * @return  mixed  Data from Google
  105.      *
  106.      * @since   12.3
  107.      * @throws UnexpectedValueException
  108.      */
  109.     public function listClients($accountID$options array()$maxpages 1)
  110.     {
  111.         if ($this->isAuthenticated())
  112.         {
  113.             $next array_key_exists('nextPageToken'$options$options['nextPage'null;
  114.             unset($options['nextPageToken']);
  115.             $url 'https://www.googleapis.com/adsense/v1.1/accounts/' urlencode($accountID'/adclients?' http_build_query($options);
  116.  
  117.             return $this->listGetData($url$maxpages$next);
  118.         }
  119.         else
  120.         {
  121.             return false;
  122.         }
  123.     }
  124.  
  125.     /**
  126.      * Method to get an AdSense AdUnit
  127.      *
  128.      * @param   string  $accountID   ID of account to get
  129.      * @param   string  $adclientID  ID of client to get
  130.      * @param   string  $adunitID    ID of adunit to get
  131.      *
  132.      * @return  mixed  Data from Google
  133.      *
  134.      * @since   12.3
  135.      */
  136.     public function getUnit($accountID$adclientID$adunitID)
  137.     {
  138.         if ($this->isAuthenticated())
  139.         {
  140.             $url 'https://www.googleapis.com/adsense/v1.1/accounts/' urlencode($accountID);
  141.             $url .= '/adclients/' urlencode($adclientID'/adunits/' urlencode($adunitID);
  142.             $jdata $this->query($url);
  143.  
  144.             if ($data json_decode($jdata->bodytrue))
  145.             {
  146.                 return $data;
  147.             }
  148.             else
  149.             {
  150.                 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
  151.             }
  152.         }
  153.         else
  154.         {
  155.             return false;
  156.         }
  157.     }
  158.  
  159.     /**
  160.      * Method to retrieve a list of AdSense Custom Channels for a specific Adunit
  161.      *
  162.      * @param   string  $accountID   ID of account
  163.      * @param   string  $adclientID  ID of client
  164.      * @param   string  $adunitID    ID of adunit to list channels from
  165.      * @param   array   $options     Search settings
  166.      * @param   int     $maxpages    Maximum number of pages of accounts to return
  167.      *
  168.      * @return  mixed  Data from Google
  169.      *
  170.      * @since   12.3
  171.      * @throws UnexpectedValueException
  172.      */
  173.     public function listUnitChannels($accountID$adclientID$adunitID$options array()$maxpages 1)
  174.     {
  175.         if ($this->isAuthenticated())
  176.         {
  177.             $next array_key_exists('nextPageToken'$options$options['nextPage'null;
  178.             unset($options['nextPageToken']);
  179.             $url 'https://www.googleapis.com/adsense/v1.1/accounts/' urlencode($accountID);
  180.             $url .= '/adclients/' urlencode($adclientID'/adunits/' urlencode($adunitID'/customchannels?' http_build_query($options);
  181.  
  182.             return $this->listGetData($url$maxpages$next);
  183.         }
  184.         else
  185.         {
  186.             return false;
  187.         }
  188.     }
  189.  
  190.     /**
  191.      * Method to get an Adsense Channel
  192.      *
  193.      * @param   string  $accountID   ID of account to get
  194.      * @param   string  $adclientID  ID of client to get
  195.      * @param   string  $channelID   ID of channel to get
  196.      *
  197.      * @return  mixed  Data from Google
  198.      *
  199.      * @since   12.3
  200.      */
  201.     public function getChannel($accountID$adclientID$channelID)
  202.     {
  203.         if ($this->isAuthenticated())
  204.         {
  205.             $url 'https://www.googleapis.com/adsense/v1.1/accounts/' urlencode($accountID'/adclients/';
  206.             $url .= urlencode($adclientID'/customchannels/' urlencode($channelID);
  207.             $jdata $this->query($url);
  208.  
  209.             if ($data json_decode($jdata->bodytrue))
  210.             {
  211.                 return $data;
  212.             }
  213.             else
  214.             {
  215.                 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
  216.             }
  217.         }
  218.         else
  219.         {
  220.             return false;
  221.         }
  222.     }
  223.  
  224.     /**
  225.      * Method to retrieve a list of AdSense Custom Channels
  226.      *
  227.      * @param   string  $accountID   ID of account
  228.      * @param   string  $adclientID  ID of client to list channels from
  229.      * @param   array   $options     Search settings
  230.      * @param   int     $maxpages    Maximum number of pages of accounts to return
  231.      *
  232.      * @return  mixed  Data from Google
  233.      *
  234.      * @since   12.3
  235.      * @throws UnexpectedValueException
  236.      */
  237.     public function listChannels($accountID$adclientID$options array()$maxpages 1)
  238.     {
  239.         if ($this->isAuthenticated())
  240.         {
  241.             $next array_key_exists('nextPageToken'$options$options['nextPage'null;
  242.             unset($options['nextPageToken']);
  243.             $url 'https://www.googleapis.com/adsense/v1.1/accounts/' urlencode($accountID'/adclients/' urlencode($adclientID);
  244.             $url .= '/customchannels?' http_build_query($options);
  245.  
  246.             return $this->listGetData($url$maxpages$next);
  247.         }
  248.         else
  249.         {
  250.             return false;
  251.         }
  252.     }
  253.  
  254.     /**
  255.      * Method to retrieve a list of AdSense Adunits for a specific Custom Channel
  256.      *
  257.      * @param   string  $accountID   ID of account
  258.      * @param   string  $adclientID  ID of client
  259.      * @param   string  $channelID   ID of channel to list units from
  260.      * @param   array   $options     Search settings
  261.      * @param   int     $maxpages    Maximum number of pages of accounts to return
  262.      *
  263.      * @return  mixed  Data from Google
  264.      *
  265.      * @since   12.3
  266.      * @throws UnexpectedValueException
  267.      */
  268.     public function listChannelUnits($accountID$adclientID$channelID$options array()$maxpages 1)
  269.     {
  270.         if ($this->isAuthenticated())
  271.         {
  272.             $next array_key_exists('nextPageToken'$options$options['nextPage'null;
  273.             unset($options['nextPageToken']);
  274.             $url 'https://www.googleapis.com/adsense/v1.1/accounts/' urlencode($accountID'/adclients/' urlencode($adclientID);
  275.             $url .= '/customchannels/' urlencode($channelID'/adunits?' http_build_query($options);
  276.  
  277.             return $this->listGetData($url$maxpages$next);
  278.         }
  279.         else
  280.         {
  281.             return false;
  282.         }
  283.     }
  284.  
  285.     /**
  286.      * Method to generate a report from Google AdSense
  287.      *
  288.      * @param   string  $accountID   ID of account
  289.      * @param   string  $adclientID  ID of client
  290.      * @param   array   $options     Search settings
  291.      * @param   int     $maxpages    Maximum number of pages of accounts to return
  292.      *
  293.      * @return  mixed  Data from Google
  294.      *
  295.      * @since   12.3
  296.      * @throws UnexpectedValueException
  297.      */
  298.     public function listUrlChannels($accountID$adclientID$options array()$maxpages 1)
  299.     {
  300.         if ($this->isAuthenticated())
  301.         {
  302.             $next array_key_exists('nextPageToken'$options$options['nextPage'null;
  303.             unset($options['nextPageToken']);
  304.             $url 'https://www.googleapis.com/adsense/v1.1/accounts/' urlencode($accountID);
  305.             $url .= '/adclients/' urlencode($adclientID'/urlchannels?' http_build_query($options);
  306.  
  307.             return $this->listGetData($url$maxpages$next);
  308.         }
  309.         else
  310.         {
  311.             return false;
  312.         }
  313.     }
  314.  
  315.     /**
  316.      * Method to retrieve a list of AdSense Channel URLs
  317.      *
  318.      * @param   string  $accountID  ID of account
  319.      * @param   mixed   $start      Start day
  320.      * @param   mixed   $end        End day
  321.      * @param   array   $options    Search settings
  322.      * @param   int     $maxpages   Maximum number of pages of accounts to return
  323.      *
  324.      * @return  mixed  Data from Google
  325.      *
  326.      * @since   12.3
  327.      * @throws UnexpectedValueException
  328.      */
  329.     public function generateReport($accountID$start$end false$options array()$maxpages 1)
  330.     {
  331.         if ($this->isAuthenticated())
  332.         {
  333.             if (is_int($start))
  334.             {
  335.                 $startobj new DateTime;
  336.                 $startobj->setTimestamp($start);
  337.             }
  338.             elseif (is_string($start))
  339.             {
  340.                 $startobj new DateTime($start);
  341.             }
  342.             elseif (is_a($start'DateTime'))
  343.             {
  344.                 $startobj $start;
  345.             }
  346.             else
  347.             {
  348.                 throw new InvalidArgumentException('Invalid start time.');
  349.             }
  350.  
  351.             if (!$end)
  352.             {
  353.                 $endobj new DateTime;
  354.             }
  355.             elseif (is_int($end))
  356.             {
  357.                 $endobj new DateTime;
  358.                 $endobj->setTimestamp($end);
  359.             }
  360.             elseif (is_string($end))
  361.             {
  362.                 $endobj new DateTime($end);
  363.             }
  364.             elseif (is_a($end'DateTime'))
  365.             {
  366.                 $endobj $end;
  367.             }
  368.             else
  369.             {
  370.                 throw new InvalidArgumentException('Invalid end time.');
  371.             }
  372.  
  373.             $options['startDate'$startobj->format('Y-m-d');
  374.             $options['endDate'$endobj->format('Y-m-d');
  375.  
  376.             unset($options['startIndex']);
  377.  
  378.             $url 'https://www.googleapis.com/adsense/v1.1/accounts/' urlencode($accountID'/reports?' http_build_query($options);
  379.  
  380.             if (strpos($url'&'))
  381.             {
  382.                 $url .= '&';
  383.             }
  384.  
  385.             $i 0;
  386.             $data['rows'array();
  387.  
  388.             do
  389.             {
  390.                 $jdata $this->query($url 'startIndex=' count($data['rows']));
  391.                 $newdata json_decode($jdata->bodytrue);
  392.  
  393.                 if ($newdata && array_key_exists('rows'$newdata))
  394.                 {
  395.                     $newdata['rows'array_merge($data['rows']$newdata['rows']);
  396.                     $data $newdata;
  397.                 }
  398.                 else
  399.                 {
  400.                     throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
  401.                 }
  402.  
  403.                 $i++;
  404.             }
  405.             while (count($data['rows']$data['totalMatchedRows'&& $i $maxpages);
  406.  
  407.             return $data;
  408.         }
  409.         else
  410.         {
  411.             return false;
  412.         }
  413.     }
  414. }

Documentation generated on Tue, 19 Nov 2013 14:53:37 +0100 by phpDocumentor 1.4.3