PageRenderTime 67ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/include/connectors/utils/ConnectorUtils.php

https://github.com/nerdystudmuffin/dashlet-subpanels
PHP | 779 lines | 452 code | 163 blank | 164 comment | 109 complexity | 5af30150fb754ea37795becd14eee2e3 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  3. /*********************************************************************************
  4. * SugarCRM is a customer relationship management program developed by
  5. * SugarCRM, Inc. Copyright (C) 2004 - 2009 SugarCRM Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU 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 General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU 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 General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU 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. define('CONNECTOR_DISPLAY_CONFIG_FILE', 'custom/modules/Connectors/metadata/display_config.php');
  38. require_once('include/connectors/ConnectorFactory.php');
  39. class ConnectorUtils {
  40. public static function getConnector($id, $refresh=false) {
  41. $s = self::getConnectors($refresh);
  42. return !empty($s[$id]) ? $s[$id] : null;
  43. }
  44. /**
  45. * getViewDefs
  46. * Returns an Array of the merge definitions used by the Connector module to
  47. * merge values into the bean instance
  48. *
  49. * @param mixed $filter_sources Array optional Array value of sources to only use
  50. * @return mixed $mergedefs Array of the merge definitions
  51. */
  52. public static function getViewDefs($filter_sources=array()) {
  53. //Go through all connectors and get their mapping keys and merge them across each module
  54. $connectors = self::getConnectors();
  55. $modules_sources = self::getDisplayConfig();
  56. $view_defs = array();
  57. foreach($connectors as $id=>$ds) {
  58. if(!empty($filter_sources) && !isset($filter_sources[$id])) {
  59. continue;
  60. }
  61. if(file_exists('custom/' . $ds['directory'] . '/mapping.php')) {
  62. require('custom/' . $ds['directory'] . '/mapping.php');
  63. } else if(file_exists($ds['directory'] . '/mapping.php')) {
  64. require($ds['directory'] . '/mapping.php');
  65. }
  66. if(!empty($mapping['beans'])) {
  67. foreach($mapping['beans'] as $module=>$map) {
  68. if(!empty($modules_sources[$module][$id])) {
  69. if(!empty($view_defs['Connector']['MergeView'][$module])) {
  70. $view_defs['Connector']['MergeView'][$module] = array_merge($view_defs['Connector']['MergeView'][$module], array_flip($map));
  71. } else {
  72. $view_defs['Connector']['MergeView'][$module] = array_flip($map);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. if(!empty($view_defs['Connector']['MergeView'])) {
  79. foreach($view_defs['Connector']['MergeView'] as $module=>$map) {
  80. $view_defs['Connector']['MergeView'][$module] = array_keys($view_defs['Connector']['MergeView'][$module]);
  81. }
  82. }
  83. return $view_defs;
  84. }
  85. /**
  86. * getMergeViewDefs
  87. * Returns an Array of the merge definitions used by the Connector module to
  88. * merge values into the bean instance
  89. *
  90. * @deprecated This method has been replaced by getViewDefs
  91. * @param boolean $refresh boolean value to manually refresh the mergeview definitions
  92. * @return mixed $mergedefs Array of the merge definitions
  93. */
  94. public static function getMergeViewDefs($refresh=false) {
  95. if($refresh || !file_exists('custom/modules/Connectors/metadata/mergeviewdefs.php')) {
  96. //Go through all connectors and get their mapping keys and merge them across each module
  97. $connectors = self::getConnectors($refresh);
  98. $modules_sources = self::getDisplayConfig();
  99. $view_defs = array();
  100. foreach($connectors as $id=>$ds) {
  101. if(file_exists('custom/' . $ds['directory'] . '/mapping.php')) {
  102. require('custom/' . $ds['directory'] . '/mapping.php');
  103. } else if(file_exists($ds['directory'] . '/mapping.php')) {
  104. require($ds['directory'] . '/mapping.php');
  105. }
  106. if(!empty($mapping['beans'])) {
  107. foreach($mapping['beans'] as $module=>$map) {
  108. if(!empty($modules_sources[$module][$id])) {
  109. if(!empty($view_defs['Connector']['MergeView'][$module])) {
  110. $view_defs['Connector']['MergeView'][$module] = array_merge($view_defs['Connector']['MergeView'][$module], array_flip($map));
  111. } else {
  112. $view_defs['Connector']['MergeView'][$module] = array_flip($map);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. if(!empty($view_defs['Connector']['MergeView'])) {
  119. foreach($view_defs['Connector']['MergeView'] as $module=>$map) {
  120. $view_defs['Connector']['MergeView'][$module] = array_keys($view_defs['Connector']['MergeView'][$module]);
  121. }
  122. }
  123. if(!file_exists('custom/modules/Connectors/metadata')) {
  124. mkdir_recursive('custom/modules/Connectors/metadata');
  125. }
  126. if(!write_array_to_file('viewdefs', $view_defs, 'custom/modules/Connectors/metadata/mergeviewdefs.php')) {
  127. $GLOBALS['log']->fatal("Cannot write file custom/modules/Connectors/metadata/mergeviewdefs.php");
  128. return array();
  129. }
  130. }
  131. require('custom/modules/Connectors/metadata/mergeviewdefs.php');
  132. return $viewdefs;
  133. }
  134. /**
  135. * getConnectors
  136. * Returns an Array of the connectors that have been loaded into the system
  137. * along with attributes pertaining to each connector.
  138. *
  139. * @param boolean $refresh boolean flag indicating whether or not to force rewriting the file; defaults to false
  140. * @returns mixed $connectors Array of the connector entries found
  141. */
  142. public static function getConnectors($refresh=false) {
  143. if($refresh || !file_exists('custom/modules/Connectors/metadata/connectors.php')) {
  144. $sources = array_merge(self::getSources('modules/Connectors/connectors/sources'), self::getSources('custom/modules/Connectors/connectors/sources'));
  145. if(!file_exists('custom/modules/Connectors/metadata')) {
  146. mkdir_recursive('custom/modules/Connectors/metadata');
  147. }
  148. if(!write_array_to_file('connectors', $sources, 'custom/modules/Connectors/metadata/connectors.php')) {
  149. //Log error and return empty array
  150. $GLOBALS['log']->fatal("Cannot write sources to file");
  151. return array();
  152. }
  153. } //if
  154. require('custom/modules/Connectors/metadata/connectors.php');
  155. return $connectors;
  156. }
  157. /**
  158. * getSources
  159. * Returns an Array of source entries found under the given directory
  160. * @param String $directory The directory to search
  161. * @return mixed $sources An Array of source entries
  162. */
  163. private static function getSources($directory='modules/Connectors/connectors/sources') {
  164. if(file_exists($directory)) {
  165. $files = array();
  166. $files = findAllFiles($directory, $files, false, 'config\.php');
  167. $start = strrpos($directory, '/') == strlen($directory)-1 ? strlen($directory) : strlen($directory) + 1;
  168. $sources = array();
  169. foreach($files as $file) {
  170. require($file);
  171. $end = strrpos($file, '/') - $start;
  172. $source = array();
  173. $source['id'] = str_replace('/', '_', substr($file, $start, $end));
  174. $source['name'] = !empty($config['name']) ? $config['name'] : $source['id'];
  175. $source['enabled'] = true;
  176. $source['directory'] = $directory . '/' . str_replace('_', '/', $source['id']);
  177. $instance = ConnectorFactory::getInstance($source['id']);
  178. $mapping = $instance->getMapping();
  179. $modules = array();
  180. if(!empty($mapping['beans'])) {
  181. foreach($mapping['beans'] as $module=>$mapping_entry) {
  182. $modules[]=$module;
  183. }
  184. }
  185. $source['modules'] = $modules;
  186. $sources[$source['id']] = $source;
  187. }
  188. return $sources;
  189. }
  190. return array();
  191. }
  192. /**
  193. * getDisplayConfig
  194. *
  195. */
  196. public static function getDisplayConfig($refresh = false){
  197. if(!file_exists(CONNECTOR_DISPLAY_CONFIG_FILE) || $refresh) {
  198. $sources = self::getConnectors();
  199. $modules_sources = array();
  200. //Make the directory for the config file
  201. if(!file_exists('custom/modules/Connectors/metadata')) {
  202. mkdir_recursive('custom/modules/Connectors/metadata');
  203. }
  204. if(!write_array_to_file('modules_sources', $modules_sources, CONNECTOR_DISPLAY_CONFIG_FILE)) {
  205. //Log error and return empty array
  206. $GLOBALS['log']->fatal("Cannot write \$modules_sources to " . CONNECTOR_DISPLAY_CONFIG_FILE);
  207. }
  208. }
  209. require(CONNECTOR_DISPLAY_CONFIG_FILE);
  210. return $modules_sources;
  211. }
  212. /**
  213. * getModuleConnectors
  214. *
  215. * @param String $module the module to get the connectors for
  216. * @param mixed $connectors Array of connectors mapped to the module or empty if none
  217. * @return unknown
  218. */
  219. public static function getModuleConnectors($module){
  220. $modules_sources = self::getDisplayConfig();
  221. if(!empty($modules_sources) && !empty($modules_sources[$module])){
  222. $sources = array();
  223. foreach($modules_sources[$module] as $index => $id){
  224. $sources[$id] = self::getConnector($id);
  225. }
  226. return $sources;
  227. }else{
  228. return array();
  229. }
  230. }
  231. /**
  232. * isModuleEnabled
  233. * Given a module name, checks to see if the module is enabled to be serviced by the connector module
  234. * @param String $module String name of the module
  235. * @return boolean $enabled boolean value indicating whether or not the module is enabled to be serviced by the connector module
  236. */
  237. public static function isModuleEnabled($module) {
  238. $modules_sources = self::getDisplayConfig();
  239. return !empty($modules_sources) && !empty($modules_sources[$module]) ? true : false;
  240. }
  241. /**
  242. * isSourceEnabled
  243. * Given a source id, checks to see if the source is enabled for at least one module
  244. * @param String $source String name of the source
  245. * @return boolean $enabled boolean value indicating whether or not the source is displayed in at least one module
  246. */
  247. public static function isSourceEnabled($source) {
  248. $modules_sources = self::getDisplayConfig();
  249. foreach($modules_sources as $module=>$mapping) {
  250. foreach($mapping as $s) {
  251. if($s == $source) {
  252. return true;
  253. }
  254. }
  255. }
  256. return false;
  257. }
  258. /**
  259. * updateMetaDataFiles
  260. * This method updates the metadata files (detailviewdefs.php) according to the settings in display_config.php
  261. * @return $result boolean value indicating whether or not the method successfully completed.
  262. */
  263. public static function updateMetaDataFiles() {
  264. if(file_exists(CONNECTOR_DISPLAY_CONFIG_FILE)) {
  265. require(CONNECTOR_DISPLAY_CONFIG_FILE);
  266. if(!empty($modules_sources)) {
  267. foreach($modules_sources as $module=>$mapping) {
  268. $metadata_file = file_exists("custom/modules/{$module}/metadata/detailviewdefs.php") ? "custom/modules/{$module}/metadata/detailviewdefs.php" : "modules/{$module}/metadata/detailviewdefs.php";
  269. require($metadata_file);
  270. $insertConnectorButton = true;
  271. self::removeHoverField($viewdefs, $module);
  272. //Insert the hover field if available
  273. if(!empty($mapping)) {
  274. require_once('include/connectors/formatters/FormatterFactory.php');
  275. $shown_formatters = array();
  276. foreach($mapping as $id) {
  277. $source = SourceFactory::getSource($id, false);
  278. if($source->isEnabledInHover() && $source->isRequiredConfigFieldsForButtonSet()) {
  279. $shown_formatters[$id] = FormatterFactory::getInstance($id);
  280. }
  281. }
  282. //Now we have to decide which field to put it on... use the first one for now
  283. if(!empty($shown_formatters)) {
  284. foreach($shown_formatters as $id=>$formatter) {
  285. $added_field = false;
  286. $formatter_mapping = $formatter->getSourceMapping();
  287. $source = $formatter->getComponent()->getSource();
  288. //go through the mapping and add the hover to every field define in the mapping
  289. //1) check for hover fields
  290. $hover_fields = $source->getFieldsWithParams('hover', true);
  291. foreach($hover_fields as $key => $def){
  292. if(!empty($formatter_mapping['beans'][$module][$key])){
  293. $added_field = self::setHoverField($viewdefs, $module, $formatter_mapping['beans'][$module][$key], $id);
  294. }
  295. }
  296. //2) check for first mapping field
  297. if(!$added_field && !empty($formatter_mapping['beans'][$module])) {
  298. foreach($formatter_mapping['beans'][$module] as $key => $val){
  299. $added_field = self::setHoverField($viewdefs, $module, $val, $id);
  300. if($added_field){
  301. break;
  302. }
  303. }
  304. }
  305. } //foreach
  306. //Log an error message
  307. if(!$added_field) {
  308. $GLOBALS['log']->fatal("Unable to place hover field link on metadata for module {$module}");
  309. }
  310. }
  311. }
  312. //Make the directory for the metadata file
  313. if(!file_exists("custom/modules/{$module}/metadata")) {
  314. mkdir_recursive("custom/modules/{$module}/metadata");
  315. }
  316. if(!write_array_to_file('viewdefs', $viewdefs, "custom/modules/{$module}/metadata/detailviewdefs.php")) {
  317. $GLOBALS['log']->fatal("Cannot update file custom/modules/{$module}/metadata/detailviewdefs.php");
  318. return false;
  319. }
  320. if(file_exists("{$GLOBALS['sugar_config']['cache_dir']}modules/{$module}/DetailView.tpl") && !unlink("{$GLOBALS['sugar_config']['cache_dir']}modules/{$module}/DetailView.tpl")) {
  321. $GLOBALS['log']->fatal("Cannot delete file {$GLOBALS['sugar_config']['cache_dir']}modules/{$module}/DetailView.tpl");
  322. return false;
  323. }
  324. }
  325. }
  326. }
  327. return true;
  328. }
  329. public function removeHoverField(&$viewdefs, $module) {
  330. foreach($viewdefs[$module]['DetailView']['panels'] as $panel_id=>$panel) {
  331. foreach($panel as $row_id=>$row) {
  332. foreach($row as $field_id=>$field) {
  333. if(is_array($field) && !empty($field['displayParams']['enableConnectors'])) {
  334. unset($field['displayParams']['enableConnectors']);
  335. unset($field['displayParams']['module']);
  336. unset($field['displayParams']['connectors']);
  337. $viewdefs[$module]['DetailView']['panels'][$panel_id][$row_id][$field_id] = $field;
  338. }
  339. } //foreach
  340. } //foreach
  341. } //foreach
  342. return false;
  343. }
  344. public function setHoverField(&$viewdefs, $module, $hover_field, $source_id) {
  345. //Check for metadata files that aren't correctly created
  346. require_once('include/SugarFields/Parsers/MetaParser.php');
  347. $metaParser = new MetaParser();
  348. if(!$metaParser->hasMultiplePanels($viewdefs[$module]['DetailView']['panels'])) {
  349. $keys = array_keys($viewdefs[$module]['DetailView']['panels']);
  350. if(!empty($keys) && count($keys) != 1) {
  351. $viewdefs[$module]['DetailView']['panels'] = array('default'=>$viewdefs[$module]['DetailView']['panels']);
  352. }
  353. }
  354. foreach($viewdefs[$module]['DetailView']['panels'] as $panel_id=>$panel) {
  355. foreach($panel as $row_id=>$row) {
  356. foreach($row as $field_id=>$field) {
  357. $name = is_array($field) ? $field['name'] : $field;
  358. if($name == $hover_field) {
  359. if(is_array($field)) {
  360. if(!empty($viewdefs[$module]['DetailView']['panels'][$panel_id][$row_id][$field_id]['displayParams'])) {
  361. $newDisplayParam = $viewdefs[$module]['DetailView']['panels'][$panel_id][$row_id][$field_id]['displayParams'];
  362. $newDisplayParam['module'] = $module;
  363. $newDisplayParam['enableConnectors'] = true;
  364. if(!is_null($source_id) && !in_array($source_id, $newDisplayParam['connectors'])){
  365. $newDisplayParam['connectors'][] = $source_id;
  366. }
  367. $viewdefs[$module]['DetailView']['panels'][$panel_id][$row_id][$field_id]['displayParams'] = $newDisplayParam;
  368. } else {
  369. $field['displayParams'] = array('enableConnectors'=>true, 'module'=>$module, 'connectors' => array(0 => $source_id));
  370. $viewdefs[$module]['DetailView']['panels'][$panel_id][$row_id][$field_id] = $field;
  371. }
  372. } else {
  373. $viewdefs[$module]['DetailView']['panels'][$panel_id][$row_id][$field_id] = array ('name'=>$field, 'displayParams'=>array('enableConnectors'=>true, 'module'=>$module, 'connectors' => array(0 => $source_id)));
  374. }
  375. return true;
  376. }
  377. }
  378. }
  379. }
  380. return false;
  381. }
  382. /**
  383. * setDefaultHoverField
  384. * Sets the hover field to the first element in the detailview screen
  385. *
  386. * @param Array $viewdefs the metadata of the detailview
  387. * @param String $module the Module to which the hover field should be added to
  388. * @return boolean True if field was added; false otherwise
  389. */
  390. private function setDefaultHoverField(&$viewdefs, $module, $source_id) {
  391. foreach($viewdefs[$module]['DetailView']['panels'] as $panel_id=>$panel) {
  392. foreach($panel as $row_id=>$row) {
  393. foreach($row as $field_id=>$field) {
  394. if(is_array($field)) {
  395. if(!empty($viewdefs[$module]['DetailView']['panels'][$panel_id][$row_id][$field_id]['displayParams'])) {
  396. $viewdefs[$module]['DetailView']['panels'][$panel_id][$row_id][$field_id]['displayParams']['enableConnectors'] = true;
  397. $viewdefs[$module]['DetailView']['panels'][$panel_id][$row_id][$field_id]['displayParams']['module'] = $module;
  398. if(!is_null($source_id) && !in_array($source_id, $viewdefs[$module]['DetailView']['panels'][$panel_id][$row_id][$field_id]['displayParams']['connectors'])){
  399. $viewdefs[$module]['DetailView']['panels'][$panel_id][$row_id][$field_id]['displayParams']['connectors'][] = $source_id;
  400. }
  401. } else {
  402. $field['displayParams'] = array('enableConnectors'=>true, 'module'=>$module, 'connectors' => array(0 => $source_id));
  403. $viewdefs[$module]['DetailView']['panels'][$panel_id][$row_id][$field_id] = $field;
  404. }
  405. } else {
  406. $viewdefs[$module]['DetailView']['panels'][$panel_id][$row_id][$field_id] = array ('name'=>$field, 'displayParams'=>array('enableConnectors'=>true, 'module'=>$module, 'connectors' => array(0 => $source_id)));
  407. }
  408. return true;
  409. } //foreach
  410. } //foreach
  411. } //foreach
  412. return false;
  413. }
  414. /**
  415. * getConnectorButtonScript
  416. * This method builds the HTML code for the hover link field
  417. *
  418. * @param mixed $displayParams Array value of display parameters passed from the SugarField code
  419. * @param mixed $smarty The Smarty object from the calling smarty code
  420. * @return String $code The HTML code for the hover link
  421. */
  422. public static function getConnectorButtonScript($displayParams, $smarty) {
  423. $module = $displayParams['module'];
  424. require_once('include/connectors/utils/ConnectorUtils.php');
  425. $modules_sources = self::getDisplayConfig();
  426. global $current_language, $app_strings;
  427. $mod_strings = return_module_language($current_language, 'Connectors');
  428. $menuParams = 'var menuParams = "';
  429. $shown_sources = array();
  430. if(!empty($module) && !empty($displayParams['connectors'])) {
  431. foreach($displayParams['connectors'] as $id) {
  432. if(!empty($modules_sources[$module]) && in_array($id, $modules_sources[$module])){
  433. $shown_sources[] = $id;
  434. }
  435. }
  436. if(empty($shown_sources)) {
  437. return '';
  438. }
  439. require_once('include/connectors/formatters/FormatterFactory.php');
  440. $code = '';
  441. //If there is only one source, just show the icon or some standalone view
  442. if(count($shown_sources) == 1) {
  443. $formatter = FormatterFactory::getInstance($shown_sources[0]);
  444. $formatter->setModule($module);
  445. $formatter->setSmarty($smarty);
  446. $formatter_code = $formatter->getDetailViewFormat();
  447. if(!empty($formatter_code)) {
  448. $iconFilePath = $formatter->getIconFilePath();
  449. $iconFilePath = empty($iconFilePath) ? 'themes/default/images/icon_Connectors.gif' : $iconFilePath;
  450. $code = '<img id="dswidget_img" border="0" src="' . $iconFilePath .'" alt="' . $shown_sources[0] .'" onmouseover="show_' . $shown_sources[0] . '(event);">';
  451. $code .= "<link rel='stylesheet' type='text/css' href='include/javascript/yui/build/container/assets/container.css'>";
  452. $code .= "<script type='text/javascript' src='{sugar_getjspath file='include/connectors/formatters/default/company_detail.js'}'></script>";
  453. $code .= $formatter->getDetailViewFormat();
  454. $code .= $formatter_code;
  455. }
  456. return $code;
  457. } else {
  458. $formatterCode = '';
  459. $sourcesDisplayed = 0;
  460. $singleIcon = '';
  461. foreach($shown_sources as $id) {
  462. $formatter = FormatterFactory::getInstance($id);
  463. $formatter->setModule($module);
  464. $formatter->setSmarty($smarty);
  465. $buttonCode = $formatter->getDetailViewFormat();
  466. if(!empty($buttonCode)) {
  467. $sourcesDisplayed++;
  468. $singleIcon = $formatter->getIconFilePath();
  469. $source = SourceFactory::getSource($id);
  470. $config = $source->getConfig();
  471. $name = !empty($config['name']) ? $config['name'] : $id;
  472. //Create the menu item to call show_[source id] method in javascript
  473. $menuParams .= '<a href=\'#\' style=\'width:150px\' class=\'menuItem\' onmouseover=\'hiliteItem(this,\"yes\");\' onmouseout=\'unhiliteItem(this);\' onclick=\'show_' . $id . '(event);\'>' . $name . '</a>';
  474. $formatterCode .= $buttonCode;
  475. }
  476. } //for
  477. if(!empty($formatterCode)) {
  478. if($sourcesDisplayed > 1) {
  479. $code = '<img id="dswidget_img" src="themes/default/images/MoreDetail.png" width="8" height="7" border="0" alt="connectors_popups" onmouseover="return showConnectorMenu2();" onmouseout="return nd(1000);">';
  480. } else {
  481. $singleIcon = empty($singleIcon) ? "themes/default/images/icon_Connectors.gif" : $singleIcon;
  482. $code = '<img id="dswidget_img" border="0" src="' . $singleIcon . '" alt="connectors_popups" onmouseover="return showConnectorMenu2();" onmouseout="return nd(1000);">';
  483. }
  484. $code .= "{overlib_includes}\n";
  485. $code .= "<link rel='stylesheet' type='text/css' href='include/javascript/yui/build/container/assets/container.css'>\n";
  486. $code .= "<script type='text/javascript' src='{sugar_getjspath file='include/connectors/formatters/default/company_detail.js'}'></script>\n";
  487. $code .= "<script type='text/javascript'>\n";
  488. $code .= "function showConnectorMenu2() {literal} { {/literal}\n";
  489. $menuParams .= '";';
  490. $code .= $menuParams . "\n";
  491. $code .= "return overlib(menuParams, CENTER, STICKY, MOUSEOFF, 3000, WIDTH, 110, FGCLASS, 'olOptionsFgClass', CGCLASS, 'olOptionsCgClass', BGCLASS, 'olBgClass', TEXTFONTCLASS, 'olFontClass', CAPTIONFONTCLASS, 'olOptionsCapFontClass', CLOSEFONTCLASS, 'olOptionsCloseFontClass');\n";
  492. $code .= "{literal} } {/literal}\n";
  493. $code .= "</script>\n";
  494. $code .= $formatterCode;
  495. }
  496. return $code;
  497. } //if-else
  498. } //if
  499. }
  500. /**
  501. * getConnectorStrings
  502. * This method returns the language Strings for a given connector instance
  503. *
  504. * @param String $source_id String value of the connector id to retrive language strings for
  505. * @param String $language optional String value for the language to use (defaults to $GLOBALS['current_language'])
  506. */
  507. public static function getConnectorStrings($source_id, $language='') {
  508. $lang = empty($language) ? $GLOBALS['current_language'] : $language;
  509. $lang .= '.lang.php';
  510. $dir = str_replace('_', '/', $source_id);
  511. if(file_exists("custom/modules/Connectors/connectors/sources/{$dir}/language/{$lang}")) {
  512. require("custom/modules/Connectors/connectors/sources/{$dir}/language/{$lang}");
  513. return !empty($connector_strings) ? $connector_strings : array();
  514. } else if(file_exists("modules/Connectors/connectors/sources/{$dir}/language/{$lang}")){
  515. require("modules/Connectors/connectors/sources/{$dir}/language/{$lang}");
  516. return !empty($connector_strings) ? $connector_strings : array();
  517. } else {
  518. $GLOBALS['log']->error("Unable to locate language string file for source {$source_id}");
  519. return array();
  520. }
  521. }
  522. /**
  523. * installSource
  524. * Install the name of the source (called from ModuleInstaller.php). Modifies the files in the custom
  525. * directory to add the new source in.
  526. *
  527. * @param String $source String value of the id of the connector to install
  528. * @return boolean $result boolean value indicating whether or not connector was installed
  529. */
  530. public static function installSource($source) {
  531. if(empty($source)) {
  532. return false;
  533. }
  534. //Add the source to the connectors.php file
  535. self::getConnectors(true);
  536. //Get the display config file
  537. self::getDisplayConfig();
  538. //Update the display_config.php file to show this new source
  539. require(CONNECTOR_DISPLAY_CONFIG_FILE);
  540. foreach($modules_sources as $module=>$mapping) {
  541. foreach($mapping as $id=>$src) {
  542. if($src == $source) {
  543. unset($modules_sources[$module][$id]);
  544. break;
  545. }
  546. }
  547. }
  548. //Make the directory for the config file
  549. if(!file_exists('custom/modules/Connectors/metadata')) {
  550. mkdir_recursive('custom/modules/Connectors/metadata');
  551. }
  552. if(!write_array_to_file('modules_sources', $modules_sources, CONNECTOR_DISPLAY_CONFIG_FILE)) {
  553. //Log error and return empty array
  554. $GLOBALS['log']->fatal("Cannot write \$modules_sources to " . CONNECTOR_DISPLAY_CONFIG_FILE);
  555. }
  556. return true;
  557. }
  558. /**
  559. * uninstallSource
  560. *
  561. * @param String $source String value of the id of the connector to un-install
  562. * @return boolean $result boolean value indicating whether or not connector was un-installed
  563. */
  564. public static function uninstallSource($source) {
  565. if(empty($source)) {
  566. return false;
  567. }
  568. //Remove the source from the connectors.php file
  569. self::getConnectors(true);
  570. //Update the display_config.php file to remove this source
  571. require(CONNECTOR_DISPLAY_CONFIG_FILE);
  572. foreach($modules_sources as $module=>$mapping) {
  573. foreach($mapping as $id=>$src) {
  574. if($src == $source) {
  575. unset($modules_sources[$module][$id]);
  576. }
  577. }
  578. }
  579. //Make the directory for the config file
  580. if(!file_exists('custom/modules/Connectors/metadata')) {
  581. mkdir_recursive('custom/modules/Connectors/metadata');
  582. }
  583. if(!write_array_to_file('modules_sources', $modules_sources, CONNECTOR_DISPLAY_CONFIG_FILE)) {
  584. //Log error and return empty array
  585. $GLOBALS['log']->fatal("Cannot write \$modules_sources to " . CONNECTOR_DISPLAY_CONFIG_FILE);
  586. return false;
  587. }
  588. return true;
  589. }
  590. /**
  591. * hasWizardSourceEnabledForModule
  592. * This is a private method that returns a boolean value indicating whether or not at least one
  593. * source is enabled for a given module. By enabled we mean that the source has the neccessary
  594. * configuration properties set as determined by the isRequiredConfigFieldsForButtonSet method. In
  595. * addition, a check is made to ensure that it is a source that has been enabled for the wizard.
  596. *
  597. * @param String $module String value of module to check
  598. * @return boolean $enabled boolean value indicating whether or not module has at least one source enabled
  599. */
  600. private static function hasWizardSourceEnabledForModule($module='') {
  601. if(file_exists(CONNECTOR_DISPLAY_CONFIG_FILE)) {
  602. require(CONNECTOR_DISPLAY_CONFIG_FILE);
  603. if(!empty($modules_sources) && !empty($modules_sources[$module])) {
  604. foreach($modules_sources[$module] as $id) {
  605. $source = SourceFactory::getSource($id, false);
  606. if($source->isEnabledInWizard() && $source->isRequiredConfigFieldsForButtonSet()) {
  607. return true;
  608. }
  609. }
  610. }
  611. return false;
  612. }
  613. return false;
  614. }
  615. }
  616. ?>