PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/include/Dashlets/Dashlet.php

https://github.com/jacknicole/sugarcrm_dev
PHP | 378 lines | 183 code | 36 blank | 159 comment | 35 complexity | c6096d2756a85211bdd40d8013047e78 MD5 | raw file
  1. <?php
  2. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  3. /*********************************************************************************
  4. * SugarCRM Community Edition is a customer relationship management program developed by
  5. * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU Affero General Public License version 3 as published by the
  9. * Free Software Foundation with the addition of the following permission added
  10. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  11. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  12. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License along with
  20. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  21. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. * 02110-1301 USA.
  23. *
  24. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  25. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  26. *
  27. * The interactive user interfaces in modified source and object code versions
  28. * of this program must display Appropriate Legal Notices, as required under
  29. * Section 5 of the GNU Affero General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  32. * these Appropriate Legal Notices must retain the display of the "Powered by
  33. * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  34. * technical reasons, the Appropriate Legal Notices must display the words
  35. * "Powered by SugarCRM".
  36. ********************************************************************************/
  37. require_once('include/Sugar_Smarty.php');
  38. require_once('include/utils/layout_utils.php');
  39. class Dashlet
  40. {
  41. /**
  42. * Id of the Dashlet
  43. * @var guid
  44. */
  45. var $id;
  46. /**
  47. * Title of the Dashlet
  48. * @var string
  49. */
  50. var $title = 'Generic Dashlet';
  51. /**
  52. * true if the Dashlet has configuration options.
  53. * @var bool
  54. */
  55. var $isConfigurable = false;
  56. /**
  57. * true if the Dashlet is refreshable (ie charts that provide their own refresh)
  58. * @var bool
  59. */
  60. var $isRefreshable = true;
  61. /**
  62. * true if the Dashlet contains javascript
  63. * @var bool
  64. */
  65. var $hasScript = false;
  66. /**
  67. * Language strings, must be loaded at the Dashlet level w/ loadLanguage
  68. * @var array
  69. */
  70. var $dashletStrings;
  71. /**
  72. * Time period in minutes to refresh the dashlet (0 for never)
  73. * Do not refresh if $isRefreshable is set to false
  74. *
  75. * To support auto refresh all refreshable dashlets that override process() must call processAutoRefresh()
  76. * @var int
  77. */
  78. var $autoRefresh = "0";
  79. /**
  80. * Constructor
  81. *
  82. * @param $id
  83. */
  84. public function Dashlet($id)
  85. {
  86. $this->id = $id;
  87. }
  88. /**
  89. * Returns the HTML for the configure icon
  90. *
  91. * @return string HTML
  92. */
  93. public function setConfigureIcon()
  94. {
  95. if($this->isConfigurable) {
  96. $additionalTitle = '<td nowrap width="1%" style="padding-right: 0px;"><div class="dashletToolSet"><a href="javascript:void(0)" onclick="SUGAR.mySugar.configureDashlet(\''
  97. . $this->id . '\'); return false;">'
  98. . SugarThemeRegistry::current()->getImage('dashlet-header-edit','title="' . translate('LBL_DASHLET_EDIT', 'Home') . '" alt="' . translate('LBL_DASHLET_EDIT', 'Home') . '" border="0" align="absmiddle"').'</a>'
  99. . '';
  100. }
  101. else {
  102. $additionalTitle = '<td nowrap width="1%" style="padding-right: 0px;"><div class="dashletToolSet">';
  103. }
  104. return $additionalTitle;
  105. }
  106. /**
  107. * Returns the HTML for the refresh icon
  108. *
  109. * @return string HTML
  110. */
  111. public function setRefreshIcon()
  112. {
  113. $additionalTitle = '';
  114. if($this->isRefreshable) {
  115. $additionalTitle .= '<a href="javascript:void(0)" onclick="SUGAR.mySugar.retrieveDashlet(\''
  116. . $this->id . '\'); return false;">'
  117. . SugarThemeRegistry::current()->getImage('dashlet-header-refresh','border="0" align="absmiddle" title="' . translate('LBL_DASHLET_REFRESH', 'Home') . '" alt="' . translate('LBL_DASHLET_REFRESH', 'Home') . '"')
  118. . '</a>';
  119. }
  120. return $additionalTitle;
  121. }
  122. /**
  123. * Returns the HTML for the delete icon
  124. *
  125. * @return string HTML
  126. */
  127. public function setDeleteIcon()
  128. {
  129. global $sugar_config;
  130. if (!empty($sugar_config['lock_homepage']) && $sugar_config['lock_homepage'] == true) {
  131. return '</div></td></tr></table>';
  132. }
  133. $additionalTitle = '<a href="javascript:void(0)" onclick="SUGAR.mySugar.deleteDashlet(\''
  134. . $this->id . '\'); return false;">'
  135. . SugarThemeRegistry::current()->getImage('dashlet-header-close','border="0" align="absmiddle" title="' . translate('LBL_DASHLET_DELETE', 'Home') . '" alt="' . translate('LBL_DASHLET_DELETE', 'Home') . '"')
  136. . '</a></div></td></tr></table>';
  137. return $additionalTitle;
  138. }
  139. /**
  140. * @deprecated No longer needed, replaced with Dashlet::getHeader() and Dashlet::getFooter()
  141. *
  142. * @param string $text
  143. * @return string HTML
  144. */
  145. public function getTitle($text = '')
  146. {
  147. return '';
  148. }
  149. /**
  150. * Called when Dashlet is displayed
  151. *
  152. * @param string $text text after the title
  153. * @return string Header html
  154. */
  155. public function getHeader($text = '')
  156. {
  157. global $sugar_config;
  158. $title = '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td width="99%">' . $text . '</td>';
  159. $title .= $this->setConfigureIcon();
  160. $title .= $this->setRefreshIcon();
  161. $title .= $this->setDeleteIcon();
  162. $str = '<div ';
  163. if(empty($sugar_config['lock_homepage']) || $sugar_config['lock_homepage'] == false) $str .= 'onmouseover="this.style.cursor = \'move\';" ';
  164. $str .= 'id="dashlet_header_' . $this->id . '" class="hd"><div class="tl"></div><div class="hd-center">' . get_form_header($this->title, $title, false) . '</div><div class="tr"></div></div><div class="bd"><div class="ml"></div><div class="bd-center">';
  165. return $str;
  166. }
  167. /**
  168. * Called when Dashlet is displayed
  169. *
  170. * @return string footer HTML
  171. */
  172. public function getFooter()
  173. {
  174. $footer = '</div><div class="mr"></div></div><div class="ft"><div class="bl"></div><div class="ft-center"></div><div class="br"></div></div>';
  175. return $footer;
  176. }
  177. /**
  178. * Called when Dashlet is displayed, override this
  179. *
  180. * @param string $text text after the title
  181. * @return string title HTML
  182. */
  183. public function display($text = '')
  184. {
  185. return '';
  186. }
  187. /**
  188. * Called when Dashlets configuration options are called
  189. */
  190. public function displayOptions()
  191. {
  192. }
  193. /**
  194. * Override if you need to do pre-processing before display is called
  195. */
  196. public function process()
  197. {
  198. }
  199. /**
  200. * Processes and displays the auto refresh code for the dashlet
  201. *
  202. * @param int $dashletOffset
  203. * @return string HTML code
  204. */
  205. protected function processAutoRefresh($dashletOffset = 0)
  206. {
  207. global $sugar_config;
  208. if ( empty($dashletOffset) ) {
  209. $dashletOffset = 0;
  210. $module = $_REQUEST['module'];
  211. if(isset($_REQUEST[$module.'2_'.strtoupper($this->seedBean->object_name).'_offset'])) {
  212. $dashletOffset = $_REQUEST[$module.'2_'.strtoupper($this->seedBean->object_name).'_offset'];
  213. }
  214. }
  215. if ( !$this->isRefreshable ) {
  216. return '';
  217. }
  218. if ( !empty($sugar_config['dashlet_auto_refresh_min']) && $sugar_config['dashlet_auto_refresh_min'] == -1 ) {
  219. return '';
  220. }
  221. $autoRefreshSS = new Sugar_Smarty();
  222. $autoRefreshSS->assign('dashletOffset', $dashletOffset);
  223. $autoRefreshSS->assign('dashletId', $this->id);
  224. $autoRefreshSS->assign('strippedDashletId', str_replace("-","",$this->id)); //javascript doesn't like "-" in function names
  225. if ( empty($this->autoRefresh) ) {
  226. $this->autoRefresh = 0;
  227. }
  228. elseif ( !empty($sugar_config['dashlet_auto_refresh_min']) ) {
  229. $this->autoRefresh = min($sugar_config['dashlet_auto_refresh_min'],$this->autoRefresh);
  230. }
  231. $autoRefreshSS->assign('dashletRefreshInterval', $this->autoRefresh * 1000);
  232. $tpl = 'include/Dashlets/DashletGenericAutoRefresh.tpl';
  233. if ( $_REQUEST['action'] == "DynamicAction" ) {
  234. $tpl = 'include/Dashlets/DashletGenericAutoRefreshDynamic.tpl';
  235. }
  236. return $autoRefreshSS->fetch($tpl);
  237. }
  238. /**
  239. * Override this if your dashlet is configurable (this is called when the the configureDashlet form is shown)
  240. * Filters the array for only the parameters it needs to save
  241. *
  242. * @param array $req the array to pull options from
  243. * @return array options array
  244. */
  245. public function saveOptions($req)
  246. {
  247. }
  248. /**
  249. * Sets the language strings
  250. *
  251. * @param string $dashletClassname classname of the dashlet
  252. * @param string $dashletDirectory directory path of the dashlet
  253. */
  254. public function loadLanguage($dashletClassname, $dashletDirectory = 'modules/Home/Dashlets/')
  255. {
  256. global $current_language, $dashletStrings;
  257. if(!isset($dashletStrings[$dashletClassname])) {
  258. // load current language strings for current language, else default to english
  259. if(is_file($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php')
  260. || is_file('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php') ) {
  261. if(is_file($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php')) {
  262. require($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php');
  263. }
  264. if(is_file('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php')) {
  265. require('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php');
  266. }
  267. }
  268. else {
  269. if(is_file($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php')) {
  270. require($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php');
  271. }
  272. if(is_file('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php')) {
  273. require('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php');
  274. }
  275. }
  276. }
  277. $this->dashletStrings = $dashletStrings[$dashletClassname];
  278. }
  279. /**
  280. * Generic way to store an options array into UserPreferences
  281. *
  282. * @param array $optionsArray the array to save
  283. */
  284. public function storeOptions($optionsArray)
  285. {
  286. global $current_user;
  287. $dashletDefs = $current_user->getPreference('dashlets', 'Home'); // load user's dashlets config
  288. $dashletDefs[$this->id]['options'] = $optionsArray;
  289. $current_user->setPreference('dashlets', $dashletDefs, 0, 'Home');
  290. }
  291. /**
  292. * Generic way to retrieve options array from UserPreferences
  293. *
  294. * @return array options array stored in UserPreferences
  295. */
  296. public function loadOptions()
  297. {
  298. global $current_user;
  299. $dashletDefs = $current_user->getPreference('dashlets', 'Home'); // load user's dashlets config
  300. if(isset($dashletDefs[$this->id]['options']))
  301. return $dashletDefs[$this->id]['options'];
  302. else
  303. return array();
  304. }
  305. /**
  306. * Override this in the subclass. It is used to determine whether the dashlet can be displayed.
  307. *
  308. * @return bool indicating whether or not the current user has access to display this Dashlet.
  309. */
  310. public function hasAccess()
  311. {
  312. return true;
  313. }
  314. /**
  315. * Returns the available auto refresh settings you can set a dashlet to
  316. *
  317. * @return array options available
  318. */
  319. protected function getAutoRefreshOptions()
  320. {
  321. $options = $GLOBALS['app_list_strings']['dashlet_auto_refresh_options'];
  322. if ( isset($GLOBALS['sugar_config']['dashlet_auto_refresh_min']) ) {
  323. foreach ( $options as $time => $desc ) {
  324. if ( $time != -1 && $time < $GLOBALS['sugar_config']['dashlet_auto_refresh_min'] ) {
  325. unset($options[$time]);
  326. }
  327. }
  328. }
  329. return $options;
  330. }
  331. /**
  332. * Returns true if the dashlet is auto refreshable
  333. *
  334. * @return bool
  335. */
  336. protected function isAutoRefreshable()
  337. {
  338. return $this->isRefreshable &&
  339. ( isset($GLOBALS['sugar_config']['dashlet_auto_refresh_min']) ?
  340. $GLOBALS['sugar_config']['dashlet_auto_refresh_min'] != -1 : true );
  341. }
  342. }