PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/include/Dashlets/Dashlet.php

https://gitlab.com/tjaafar/SuiteCRM
PHP | 403 lines | 197 code | 39 blank | 167 comment | 38 complexity | a2b7374b3aacd060afb4fc2ffd5d47cf 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-2013 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. /**
  40. * Basic Dashlet
  41. * @api
  42. */
  43. class Dashlet
  44. {
  45. /**
  46. * Id of the Dashlet
  47. * @var guid
  48. */
  49. var $id;
  50. /**
  51. * Title of the Dashlet
  52. * @var string
  53. */
  54. var $title = 'Generic Dashlet';
  55. /**
  56. * true if the Dashlet has configuration options.
  57. * @var bool
  58. */
  59. var $isConfigurable = false;
  60. /**
  61. * true if the Dashlet is refreshable (ie charts that provide their own refresh)
  62. * @var bool
  63. */
  64. var $isRefreshable = true;
  65. /**
  66. * true if the Dashlet configuration options panel has the clear button
  67. * @var bool
  68. */
  69. public $isConfigPanelClearShown = true;
  70. /**
  71. * true if the Dashlet contains javascript
  72. * @var bool
  73. */
  74. var $hasScript = false;
  75. /**
  76. * Language strings, must be loaded at the Dashlet level w/ loadLanguage
  77. * @var array
  78. */
  79. var $dashletStrings;
  80. /**
  81. * Time period in minutes to refresh the dashlet (0 for never)
  82. * Do not refresh if $isRefreshable is set to false
  83. *
  84. * To support auto refresh all refreshable dashlets that override process() must call processAutoRefresh()
  85. * @var int
  86. */
  87. var $autoRefresh = "0";
  88. /**
  89. * Constructor
  90. *
  91. * @param $id
  92. */
  93. public function Dashlet($id)
  94. {
  95. $this->id = $id;
  96. }
  97. /**
  98. * Returns the HTML for the configure icon
  99. *
  100. * @return string HTML
  101. */
  102. public function setConfigureIcon()
  103. {
  104. if($this->isConfigurable) {
  105. $additionalTitle = '<td nowrap width="1%" style="padding-right: 0px;"><div class="dashletToolSet"><a href="javascript:void(0)" onclick="SUGAR.mySugar.configureDashlet(\''
  106. . $this->id . '\'); return false;">'
  107. . SugarThemeRegistry::current()->getImage('dashlet-header-edit','title="' . translate('LBL_DASHLET_EDIT', 'Home') . '" border="0" align="absmiddle"', null,null,'.gif',translate('LBL_DASHLET_EDIT', 'Home')).'</a>'
  108. . '';
  109. }
  110. else {
  111. $additionalTitle = '<td nowrap width="1%" style="padding-right: 0px;"><div class="dashletToolSet">';
  112. }
  113. return $additionalTitle;
  114. }
  115. /**
  116. * Returns the HTML for the refresh icon
  117. *
  118. * @return string HTML
  119. */
  120. public function setRefreshIcon()
  121. {
  122. $additionalTitle = '';
  123. if($this->isRefreshable) {
  124. $additionalTitle .= '<a href="javascript:void(0)" onclick="SUGAR.mySugar.retrieveDashlet(\''
  125. . $this->id . '\'); return false;">'
  126. . SugarThemeRegistry::current()->getImage('dashlet-header-refresh','border="0" align="absmiddle" title="' . translate('LBL_DASHLET_REFRESH', 'Home') . '"',null,null,'.gif',translate('LBL_DASHLET_REFRESH', 'Home'))
  127. . '</a>';
  128. }
  129. return $additionalTitle;
  130. }
  131. /**
  132. * Returns the HTML for the delete icon
  133. *
  134. * @return string HTML
  135. */
  136. public function setDeleteIcon()
  137. {
  138. global $sugar_config;
  139. if (!empty($sugar_config['lock_homepage']) && $sugar_config['lock_homepage'] == true) {
  140. return '</div></td></tr></table>';
  141. }
  142. $additionalTitle = '<a href="javascript:void(0)" onclick="SUGAR.mySugar.deleteDashlet(\''
  143. . $this->id . '\'); return false;">'
  144. . SugarThemeRegistry::current()->getImage('dashlet-header-close','border="0" align="absmiddle" title="' . translate('LBL_DASHLET_DELETE', 'Home') . '"',null,null,'.gif',translate('LBL_DASHLET_DELETE', 'Home'))
  145. . '</a></div></td></tr></table>';
  146. return $additionalTitle;
  147. }
  148. /**
  149. * @deprecated No longer needed, replaced with Dashlet::getHeader() and Dashlet::getFooter()
  150. *
  151. * @param string $text
  152. * @return string HTML
  153. */
  154. public function getTitle($text = '')
  155. {
  156. return '';
  157. }
  158. /**
  159. * Called when Dashlet is displayed
  160. *
  161. * @param string $text text after the title
  162. * @return string Header html
  163. */
  164. public function getHeader($text = '')
  165. {
  166. global $sugar_config;
  167. $title = '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td width="99%">' . $text . '</td>';
  168. $title .= $this->setConfigureIcon();
  169. $title .= $this->setRefreshIcon();
  170. $title .= $this->setDeleteIcon();
  171. $str = '<div ';
  172. if(empty($sugar_config['lock_homepage']) || $sugar_config['lock_homepage'] == false) $str .= 'onmouseover="this.style.cursor = \'move\';" ';
  173. $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">';
  174. return $str;
  175. }
  176. /**
  177. * Called when Dashlet is displayed
  178. *
  179. * @return string footer HTML
  180. */
  181. public function getFooter()
  182. {
  183. $footer = '</div><div class="mr"></div></div><div class="ft"><div class="bl"></div><div class="ft-center"></div><div class="br"></div></div>';
  184. return $footer;
  185. }
  186. /**
  187. * Called when Dashlet is displayed, override this
  188. *
  189. * @param string $text text after the title
  190. * @return string title HTML
  191. */
  192. public function display($text = '')
  193. {
  194. return '';
  195. }
  196. /**
  197. * Called when Dashlets configuration options are called
  198. */
  199. public function displayOptions()
  200. {
  201. }
  202. /**
  203. * Override if you need to do pre-processing before display is called
  204. */
  205. public function process()
  206. {
  207. }
  208. /**
  209. * Processes and displays the auto refresh code for the dashlet
  210. *
  211. * @param int $dashletOffset
  212. * @return string HTML code
  213. */
  214. protected function processAutoRefresh($dashletOffset = 0)
  215. {
  216. global $sugar_config;
  217. if ( empty($dashletOffset) ) {
  218. $dashletOffset = 0;
  219. $module = $_REQUEST['module'];
  220. if(isset($_REQUEST[$module.'2_'.strtoupper($this->seedBean->object_name).'_offset'])) {
  221. $dashletOffset = $_REQUEST[$module.'2_'.strtoupper($this->seedBean->object_name).'_offset'];
  222. }
  223. }
  224. if ( !$this->isRefreshable ) {
  225. return '';
  226. }
  227. if ( !empty($sugar_config['dashlet_auto_refresh_min']) && $sugar_config['dashlet_auto_refresh_min'] == -1 ) {
  228. return '';
  229. }
  230. $autoRefreshSS = new Sugar_Smarty();
  231. $autoRefreshSS->assign('dashletOffset', $dashletOffset);
  232. $autoRefreshSS->assign('dashletId', $this->id);
  233. $autoRefreshSS->assign('strippedDashletId', str_replace("-","",$this->id)); //javascript doesn't like "-" in function names
  234. $autoRefreshSS->assign('dashletRefreshInterval', $this->getAutoRefresh());
  235. $tpl = 'include/Dashlets/DashletGenericAutoRefresh.tpl';
  236. if ( $_REQUEST['action'] == "DynamicAction" ) {
  237. $tpl = 'include/Dashlets/DashletGenericAutoRefreshDynamic.tpl';
  238. }
  239. return $autoRefreshSS->fetch($tpl);
  240. }
  241. protected function getAutoRefresh()
  242. {
  243. global $sugar_config;
  244. if (empty($this->autoRefresh) || $this->autoRefresh == -1)
  245. {
  246. $autoRefresh = 0;
  247. }
  248. elseif (!empty($sugar_config['dashlet_auto_refresh_min'])
  249. && $this->autoRefresh > 0
  250. && $sugar_config['dashlet_auto_refresh_min'] > $this->autoRefresh)
  251. {
  252. $autoRefresh = $sugar_config['dashlet_auto_refresh_min'];
  253. }
  254. else
  255. {
  256. $autoRefresh = $this->autoRefresh;
  257. }
  258. return $autoRefresh * 1000;
  259. }
  260. /**
  261. * Override this if your dashlet is configurable (this is called when the the configureDashlet form is shown)
  262. * Filters the array for only the parameters it needs to save
  263. *
  264. * @param array $req the array to pull options from
  265. * @return array options array
  266. */
  267. public function saveOptions($req)
  268. {
  269. }
  270. /**
  271. * Sets the language strings
  272. *
  273. * @param string $dashletClassname classname of the dashlet
  274. * @param string $dashletDirectory directory path of the dashlet
  275. */
  276. public function loadLanguage($dashletClassname, $dashletDirectory = 'modules/Home/Dashlets/')
  277. {
  278. global $current_language, $dashletStrings;
  279. if(!isset($dashletStrings[$dashletClassname])) {
  280. // load current language strings for current language, else default to english
  281. if(is_file($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php')
  282. || is_file('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php') ) {
  283. if(is_file($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php')) {
  284. require($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php');
  285. }
  286. if(is_file('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php')) {
  287. require('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.' . $current_language . '.lang.php');
  288. }
  289. }
  290. else {
  291. if(is_file($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php')) {
  292. require($dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php');
  293. }
  294. if(is_file('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php')) {
  295. require('custom/' . $dashletDirectory . $dashletClassname . '/' . $dashletClassname . '.en_us.lang.php');
  296. }
  297. }
  298. }
  299. $this->dashletStrings = $dashletStrings[$dashletClassname];
  300. }
  301. /**
  302. * Generic way to store an options array into UserPreferences
  303. *
  304. * @param array $optionsArray the array to save
  305. */
  306. public function storeOptions($optionsArray)
  307. {
  308. global $current_user;
  309. $dashletDefs = $current_user->getPreference('dashlets', 'Home'); // load user's dashlets config
  310. $dashletDefs[$this->id]['options'] = $optionsArray;
  311. $current_user->setPreference('dashlets', $dashletDefs, 0, 'Home');
  312. }
  313. /**
  314. * Generic way to retrieve options array from UserPreferences
  315. *
  316. * @return array options array stored in UserPreferences
  317. */
  318. public function loadOptions()
  319. {
  320. global $current_user;
  321. $dashletDefs = $current_user->getPreference('dashlets', 'Home'); // load user's dashlets config
  322. if(isset($dashletDefs[$this->id]['options']))
  323. return $dashletDefs[$this->id]['options'];
  324. else
  325. return array();
  326. }
  327. /**
  328. * Override this in the subclass. It is used to determine whether the dashlet can be displayed.
  329. *
  330. * @return bool indicating whether or not the current user has access to display this Dashlet.
  331. */
  332. public function hasAccess()
  333. {
  334. return true;
  335. }
  336. /**
  337. * Returns the available auto refresh settings you can set a dashlet to
  338. *
  339. * @return array options available
  340. */
  341. protected function getAutoRefreshOptions()
  342. {
  343. $options = $GLOBALS['app_list_strings']['dashlet_auto_refresh_options'];
  344. if ( isset($GLOBALS['sugar_config']['dashlet_auto_refresh_min']) ) {
  345. foreach ( $options as $time => $desc ) {
  346. if ( $time != -1 && $time < $GLOBALS['sugar_config']['dashlet_auto_refresh_min'] ) {
  347. unset($options[$time]);
  348. }
  349. }
  350. }
  351. return $options;
  352. }
  353. /**
  354. * Returns true if the dashlet is auto refreshable
  355. *
  356. * @return bool
  357. */
  358. protected function isAutoRefreshable()
  359. {
  360. return $this->isRefreshable &&
  361. ( isset($GLOBALS['sugar_config']['dashlet_auto_refresh_min']) ?
  362. $GLOBALS['sugar_config']['dashlet_auto_refresh_min'] != -1 : true );
  363. }
  364. }