PageRenderTime 28ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/ExamplePlugin/ExamplePlugin.php

https://github.com/ntulip/piwik
PHP | 170 lines | 110 code | 22 blank | 38 comment | 4 complexity | 3b47f1a96a1350e41d900164563fe983 MD5 | raw file
  1. <?php
  2. /**
  3. * Piwik - Open source web analytics
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
  7. * @version $Id: ExamplePlugin.php 1495 2009-10-12 13:54:46Z vipsoft $
  8. *
  9. * @category Piwik_Plugins
  10. * @package Piwik_ExamplePlugin
  11. */
  12. /**
  13. *
  14. * @package Piwik_ExamplePlugin
  15. */
  16. class Piwik_ExamplePlugin extends Piwik_Plugin
  17. {
  18. public function getInformation()
  19. {
  20. return array(
  21. 'name' => 'Example Plugin',
  22. 'description' => 'Example Plugin: This plugin shows how to create a very simple plugin, that exports two widgets in the Dashboard.',
  23. 'author' => 'Piwik',
  24. 'homepage' => 'http://piwik.org/',
  25. 'version' => '0.1',
  26. 'translationAvailable' => true,
  27. );
  28. }
  29. public function getListHooksRegistered()
  30. {
  31. return array(
  32. // 'Controller.renderView' => 'addUniqueVisitorsColumnToGivenReport',
  33. );
  34. }
  35. function addUniqueVisitorsColumnToGivenReport($notification)
  36. {
  37. $view = $notification->getNotificationInfo();
  38. $view = $view['view'];
  39. if($view->getCurrentControllerName() == 'Referers'
  40. && $view->getCurrentControllerAction() == 'getWebsites')
  41. {
  42. $view->addColumnToDisplay('nb_uniq_visitors');
  43. }
  44. }
  45. function postLoad()
  46. {
  47. // we register the widgets so they appear in the "Add a new widget" window in the dashboard
  48. // Note that the first two parameters can be either a normal string, or an index to a translation string
  49. Piwik_AddWidget('ExamplePlugin_exampleWidgets', 'ExamplePlugin_exampleWidget', 'ExamplePlugin', 'exampleWidget');
  50. Piwik_AddWidget('ExamplePlugin_exampleWidgets', 'ExamplePlugin_blogPiwikRss', 'ExamplePlugin', 'blogPiwik');
  51. Piwik_AddWidget('ExamplePlugin_exampleWidgets', 'ExamplePlugin_photostreamMatt', 'ExamplePlugin', 'photostreamMatt');
  52. }
  53. }
  54. /**
  55. *
  56. * @package Piwik_ExamplePlugin
  57. */
  58. class Piwik_ExamplePlugin_Controller extends Piwik_Controller
  59. {
  60. /**
  61. * Go to /piwik/?module=ExamplePlugin&action=helloWorld to execute this method
  62. *
  63. */
  64. function helloWorld()
  65. {
  66. echo "Hello world! <br />";
  67. echo "Happy coding with Piwik :)";
  68. }
  69. /**
  70. * See the result on piwik/?module=ExamplePlugin&action=exampleWidget
  71. * or in the dashboard > Add a new widget
  72. *
  73. */
  74. function exampleWidget()
  75. {
  76. echo "Hello world! <br> You can output whatever you want in widgets, and put them on dashboard or everywhere on the web (in your blog, website, etc.).
  77. <br>Widgets can include graphs, tables, flash, text, images, etc.
  78. <br>It's very easy to create a new plugin and widgets in Piwik. Have a look at this example file (/plugins/ExamplePlugin/ExamplePlugin.php).
  79. <div id='happycoding'><i>Happy coding!</i></div>
  80. <div id='jsenabled'>You can easily use Jquery in widgets</div>
  81. <script type=\"text/javascript\">$('#happycoding').hide().fadeIn(5000);$('#jsenabled').hide().css({'color':'red'}).fadeIn(10000);</script>";
  82. }
  83. /**
  84. * Embed Piwik.org blog using widgetbox.com widget code
  85. */
  86. function blogPiwik()
  87. {
  88. echo '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash" width="400px" height="343px" id="InsertWidget_0bf84c7c-70b5-41c1-adbc-6f4f823c598c" align="middle"><param name="movie" value="http://widgetserver.com/syndication/flash/wrapper/InsertWidget.swf"/><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="menu" value="false" /><param name="flashvars" value="r=2&appId=0bf84c7c-70b5-41c1-adbc-6f4f823c598c" /> <embed src="http://widgetserver.com/syndication/flash/wrapper/InsertWidget.swf" name="InsertWidget_0bf84c7c-70b5-41c1-adbc-6f4f823c598c" width="400px" height="343px" quality="high" menu="false" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" align="middle" flashvars="r=2&appId=0bf84c7c-70b5-41c1-adbc-6f4f823c598c" /></object>';
  89. }
  90. function photostreamMatt()
  91. {
  92. echo '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash" width="400px" height="343px" id="InsertWidget_35ba729b-1935-4165-a818-f17e7c9b4748" align="middle"><param name="movie" value="http://widgetserver.com/syndication/flash/wrapper/InsertWidget.swf"/><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="menu" value="false" /><param name="flashvars" value="r=2&appId=35ba729b-1935-4165-a818-f17e7c9b4748" /> <embed src="http://widgetserver.com/syndication/flash/wrapper/InsertWidget.swf" name="InsertWidget_35ba729b-1935-4165-a818-f17e7c9b4748" width="400px" height="343px" quality="high" menu="false" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" align="middle" flashvars="r=2&appId=35ba729b-1935-4165-a818-f17e7c9b4748" /></object>';
  93. }
  94. /**
  95. * This method displays a text containing an help about "How to build plugins for Piwik".
  96. * This help is then used on http://dev.piwik.org/trac/wiki/Plugins/GlobalFunctions
  97. *
  98. */
  99. function index()
  100. {
  101. $out = '';
  102. $out .= '<i>This page aims to list the different functions you can use when programming plugins for Piwik.</i><br>';
  103. $out .= '<b>Be careful, the following APIs may change in the near future as Piwik is still in development.</b><br>';
  104. $out .= '<h2>General</h2>';
  105. $out .= '<h3>Accessible from your plugin controller</h3>';
  106. $out .= '<code>$this->date</code> = current selected <b>Piwik_Date</b> object (<a href="http://dev.piwik.org/trac/browser/trunk/core/Date.php">class</a>)<br/>';
  107. $out .= '<code>$period = Piwik_Common::getRequestVar("period");</code> - Get the current selected period<br/>';
  108. $out .= '<code>$idSite = Piwik_Common::getRequestVar("idSite");</code> - Get the selected idSite<br/>';
  109. $out .= '<code>$site = new Piwik_Site($idSite);</code> - Build the Piwik_Site object (<a href="http://dev.piwik.org/trac/browser/trunk/core/Site.php">class</a>)<br/>';
  110. $out .= '<code>$this->str_date</code> = current selected date in YYYY-MM-DD format<br/>';
  111. $out .= '<h3>Misc</h3>';
  112. $out .= '<code>Piwik_AddMenu( $mainMenuName, $subMenuName, $url );</code> - Adds an entry to the menu in the Piwik interface (See the example in the <a href="http://dev.piwik.org/trac/browser/trunk/plugins/UserCountry/UserCountry.php#L146">UserCountry Plugin file</a>)<br/>';
  113. $out .= '<code>Piwik_AddWidget( $widgetCategory, $widgetName, $controllerName, $controllerAction, $customParameters = array());</code> - Adds a widget that users can add in the dashboard, or export using the Widgets link at the top of the screen. See the example in the <a href="http://dev.piwik.org/trac/browser/trunk/plugins/UserCountry/UserCountry.php#L143">UserCountry Plugin file</a> or any other plugin)<br/>';
  114. $out .= '<code>Piwik::prefixTable("site")</code> = <b>' . Piwik::prefixTable("site") . '</b><br/>';
  115. $out .= '<h2>User access</h2>';
  116. $out .= '<code>Piwik::getCurrentUserLogin()</code> = <b>' . Piwik::getCurrentUserLogin() . '</b><br/>';
  117. $out .= '<code>Piwik::isUserHasSomeAdminAccess()</code> = <b>' . self::boolToString(Piwik::isUserHasSomeAdminAccess()) . '</b><br/>';
  118. $out .= '<code>Piwik::isUserHasAdminAccess( array $idSites = array(1,2) )</code> = <b>' . self::boolToString(Piwik::isUserHasAdminAccess(array(1,2) )) . '</b><br/>';
  119. $out .= '<code>Piwik::isUserHasViewAccess( array $idSites = array(1) ) </code> = <b>' . self::boolToString(Piwik::isUserHasViewAccess(array(1))) . '</b><br/>';
  120. $out .= '<code>Piwik::isUserIsSuperUser()</code> = <b>' . self::boolToString(Piwik::isUserIsSuperUser()) . '</b><br/>';
  121. $out .= '<h2>Execute SQL queries</h2>';
  122. $txtQuery = "SELECT token_auth FROM ".Piwik::prefixTable('user')." WHERE login = ?";
  123. $result = Piwik_FetchOne($txtQuery, array('anonymous'));
  124. $out .= '<code>Piwik_FetchOne("'.$txtQuery.'", array("anonymous"))</code> = <b>' . var_export($result,true) . '</b><br/>';
  125. $out .= '<br>';
  126. $query = Piwik_Query($txtQuery, array('anonymous'));
  127. $fetched = $query->fetch();
  128. $token_auth = $fetched['token_auth'];
  129. $out .= '<code>$query = Piwik_Query("'.$txtQuery.'", array("anonymous"))</code><br>';
  130. $out .= '<code>$fetched = $query->fetch();</code><br>';
  131. $out .= 'At this point, we have: <code>$fetched[\'token_auth\'] == <b>'.var_export($token_auth,true) . '</b></code><br/>';
  132. $out .= '<h2>Example Sites information API</h2>';
  133. $out .= '<code>Piwik_SitesManager_API::getSitesWithViewAccess()</code> = <b><pre>' .var_export(Piwik_SitesManager_API::getSitesWithViewAccess(),true) . '</pre></b><br/>';
  134. $out .= '<code>Piwik_SitesManager_API::getSitesWithAdminAccess()</code> = <b><pre>' .var_export(Piwik_SitesManager_API::getSitesWithAdminAccess(),true) . '</pre></b><br/>';
  135. $out .= '<h2>Example API Users information</h2>';
  136. $out .= 'View the list of API methods you can call on <a href="http://dev.piwik.org/trac/wiki/API/Reference#Methods">API reference</a><br/>';
  137. $out .= 'For example you can try <code>Piwik_UsersManager_API::getUsersSitesFromAccess("view");</code> or <code>Piwik_UsersManager_API::deleteUser("userToDelete");</code><br/>';
  138. $out .= '<h2>Smarty plugins</h2>';
  139. $out .= 'There are some builtin plugins for Smarty especially developped for Piwik. <br>
  140. You can find them on the <a href="http://dev.piwik.org/trac/browser/trunk/core/SmartyPlugins">SVN at /trunk/core/SmartyPlugins</a>. <br>
  141. More documentation to come about smarty plugins.<br/>';
  142. echo $out;
  143. }
  144. static private function boolToString($bool)
  145. {
  146. return $bool ? "true" : "false";
  147. }
  148. }