PageRenderTime 185ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/include/export_utils.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 819 lines | 725 code | 27 blank | 67 comment | 33 complexity | 885a0dd6cda051658520a90f522f787f MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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-2012 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. /*********************************************************************************
  38. * Description:
  39. * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc. All Rights
  40. * Reserved. Contributor(s): ______________________________________..
  41. * *******************************************************************************/
  42. /**
  43. * gets the system default delimiter or an user-preference based override
  44. * @return string the delimiter
  45. */
  46. function getDelimiter() {
  47. global $sugar_config;
  48. global $current_user;
  49. $delimiter = ','; // default to "comma"
  50. $userDelimiter = $current_user->getPreference('export_delimiter');
  51. $delimiter = empty($sugar_config['export_delimiter']) ? $delimiter : $sugar_config['export_delimiter'];
  52. $delimiter = empty($userDelimiter) ? $delimiter : $userDelimiter;
  53. return $delimiter;
  54. }
  55. /**
  56. * builds up a delimited string for export
  57. * @param string type the bean-type to export
  58. * @param array records an array of records if coming directly from a query
  59. * @return string delimited string for export
  60. */
  61. function export($type, $records = null, $members = false, $sample=false) {
  62. global $beanList;
  63. global $beanFiles;
  64. global $current_user;
  65. global $app_strings;
  66. global $app_list_strings;
  67. global $timedate;
  68. global $mod_strings;
  69. global $current_language;
  70. $sampleRecordNum = 5;
  71. $contact_fields = array(
  72. "id"=>"Contact ID"
  73. ,"lead_source"=>"Lead Source"
  74. ,"date_entered"=>"Date Entered"
  75. ,"date_modified"=>"Date Modified"
  76. ,"first_name"=>"First Name"
  77. ,"last_name"=>"Last Name"
  78. ,"salutation"=>"Salutation"
  79. ,"birthdate"=>"Lead Source"
  80. ,"do_not_call"=>"Do Not Call"
  81. ,"email_opt_out"=>"Email Opt Out"
  82. ,"title"=>"Title"
  83. ,"department"=>"Department"
  84. ,"birthdate"=>"Birthdate"
  85. ,"do_not_call"=>"Do Not Call"
  86. ,"phone_home"=>"Phone (Home)"
  87. ,"phone_mobile"=>"Phone (Mobile)"
  88. ,"phone_work"=>"Phone (Work)"
  89. ,"phone_other"=>"Phone (Other)"
  90. ,"phone_fax"=>"Fax"
  91. ,"email1"=>"Email"
  92. ,"email2"=>"Email (Other)"
  93. ,"assistant"=>"Assistant"
  94. ,"assistant_phone"=>"Assistant Phone"
  95. ,"primary_address_street"=>"Primary Address Street"
  96. ,"primary_address_city"=>"Primary Address City"
  97. ,"primary_address_state"=>"Primary Address State"
  98. ,"primary_address_postalcode"=>"Primary Address Postalcode"
  99. ,"primary_address_country"=>"Primary Address Country"
  100. ,"alt_address_street"=>"Other Address Street"
  101. ,"alt_address_city"=>"Other Address City"
  102. ,"alt_address_state"=>"Other Address State"
  103. ,"alt_address_postalcode"=>"Other Address Postalcode"
  104. ,"alt_address_country"=>"Other Address Country"
  105. ,"description"=>"Description"
  106. );
  107. $account_fields = array(
  108. "id"=>"Account ID",
  109. "name"=>"Account Name",
  110. "website"=>"Website",
  111. "industry"=>"Industry",
  112. "account_type"=>"Type",
  113. "ticker_symbol"=>"Ticker Symbol",
  114. "employees"=>"Employees",
  115. "ownership"=>"Ownership",
  116. "phone_office"=>"Phone",
  117. "phone_fax"=>"Fax",
  118. "phone_alternate"=>"Other Phone",
  119. "email1"=>"Email",
  120. "email2"=>"Other Email",
  121. "rating"=>"Rating",
  122. "sic_code"=>"SIC Code",
  123. "annual_revenue"=>"Annual Revenue",
  124. "billing_address_street"=>"Billing Address Street",
  125. "billing_address_city"=>"Billing Address City",
  126. "billing_address_state"=>"Billing Address State",
  127. "billing_address_postalcode"=>"Billing Address Postalcode",
  128. "billing_address_country"=>"Billing Address Country",
  129. "shipping_address_street"=>"Shipping Address Street",
  130. "shipping_address_city"=>"Shipping Address City",
  131. "shipping_address_state"=>"Shipping Address State",
  132. "shipping_address_postalcode"=>"Shipping Address Postalcode",
  133. "shipping_address_country"=>"Shipping Address Country",
  134. "description"=>"Description"
  135. );
  136. //Array of fields that should not be exported, and are only used for logic
  137. $remove_from_members = array("ea_deleted", "ear_deleted", "primary_address");
  138. $focus = 0;
  139. $content = '';
  140. $bean = $beanList[$type];
  141. require_once($beanFiles[$bean]);
  142. $focus = new $bean;
  143. $searchFields = array();
  144. $db = DBManagerFactory::getInstance();
  145. if($records) {
  146. $records = explode(',', $records);
  147. $records = "'" . implode("','", $records) . "'";
  148. $where = "{$focus->table_name}.id in ($records)";
  149. } elseif (isset($_REQUEST['all']) ) {
  150. $where = '';
  151. } else {
  152. if(!empty($_REQUEST['current_post'])) {
  153. $ret_array = generateSearchWhere($type, $_REQUEST['current_post']);
  154. $where = $ret_array['where'];
  155. $searchFields = $ret_array['searchFields'];
  156. } else {
  157. $where = '';
  158. }
  159. }
  160. $order_by = "";
  161. if($focus->bean_implements('ACL')){
  162. if(!ACLController::checkAccess($focus->module_dir, 'export', true)){
  163. ACLController::displayNoAccess();
  164. sugar_die('');
  165. }
  166. if(ACLController::requireOwner($focus->module_dir, 'export')){
  167. if(!empty($where)){
  168. $where .= ' AND ';
  169. }
  170. $where .= $focus->getOwnerWhere($current_user->id);
  171. }
  172. }
  173. // Export entire list was broken because the where clause already has "where" in it
  174. // and when the query is built, it has a "where" as well, so the query was ill-formed.
  175. // Eliminating the "where" here so that the query can be constructed correctly.
  176. if($members == true){
  177. $query = $focus->create_export_members_query($records);
  178. }else{
  179. $beginWhere = substr(trim($where), 0, 5);
  180. if ($beginWhere == "where")
  181. $where = substr(trim($where), 5, strlen($where));
  182. $ret_array = create_export_query_relate_link_patch($type, $searchFields, $where);
  183. if(!empty($ret_array['join'])) {
  184. $query = $focus->create_export_query($order_by,$ret_array['where'],$ret_array['join']);
  185. } else {
  186. $query = $focus->create_export_query($order_by,$ret_array['where']);
  187. }
  188. }
  189. $result = '';
  190. $populate = false;
  191. if($sample) {
  192. $result = $db->limitQuery($query, 0, $sampleRecordNum, true, $app_strings['ERR_EXPORT_TYPE'].$type.": <BR>.".$query);
  193. if( $focus->_get_num_rows_in_query($query)<1 ){
  194. $populate = true;
  195. }
  196. }
  197. else {
  198. $result = $db->query($query, true, $app_strings['ERR_EXPORT_TYPE'].$type.": <BR>.".$query);
  199. }
  200. $fields_array = $db->getFieldsArray($result,true);
  201. //set up the order on the header row
  202. $fields_array = get_field_order_mapping($focus->module_dir, $fields_array);
  203. //set up labels to be used for the header row
  204. $field_labels = array();
  205. foreach($fields_array as $key=>$dbname){
  206. //Remove fields that are only used for logic
  207. if($members && (in_array($dbname, $remove_from_members)))
  208. continue;
  209. //default to the db name of label does not exist
  210. $field_labels[$key] = translateForExport($dbname,$focus);
  211. }
  212. // setup the "header" line with proper delimiters
  213. $content = "\"".implode("\"".getDelimiter()."\"", array_values($field_labels))."\"\r\n";
  214. $pre_id = '';
  215. if($populate){
  216. //this is a sample request with no data, so create fake datarows
  217. $content .= returnFakeDataRow($focus,$fields_array,$sampleRecordNum);
  218. }else{
  219. //process retrieved record
  220. while($val = $db->fetchByAssoc($result, false)) {
  221. //order the values in the record array
  222. $val = get_field_order_mapping($focus->module_dir,$val);
  223. $new_arr = array();
  224. if($members){
  225. if($pre_id == $val['id'])
  226. continue;
  227. if($val['ea_deleted']==1 || $val['ear_deleted']==1){
  228. $val['primary_email_address'] = '';
  229. }
  230. unset($val['ea_deleted']);
  231. unset($val['ear_deleted']);
  232. unset($val['primary_address']);
  233. }
  234. $pre_id = $val['id'];
  235. foreach ($val as $key => $value)
  236. {
  237. //getting content values depending on their types
  238. $fieldNameMapKey = $fields_array[$key];
  239. if (isset($focus->field_name_map[$fieldNameMapKey]) && $focus->field_name_map[$fieldNameMapKey]['type'])
  240. {
  241. $fieldType = $focus->field_name_map[$fieldNameMapKey]['type'];
  242. switch ($fieldType)
  243. {
  244. //if our value is a currency field, then apply the users locale
  245. case 'currency':
  246. require_once('modules/Currencies/Currency.php');
  247. $value = currency_format_number($value);
  248. break;
  249. //if our value is a datetime field, then apply the users locale
  250. case 'datetime':
  251. case 'datetimecombo':
  252. $value = $timedate->to_display_date_time($db->fromConvert($value, 'datetime'));
  253. $value = preg_replace('/([pm|PM|am|AM]+)/', ' \1', $value);
  254. break;
  255. //kbrill Bug #16296
  256. case 'date':
  257. $value = $timedate->to_display_date($db->fromConvert($value, 'date'), false);
  258. break;
  259. // Bug 32463 - Properly have multienum field translated into something useful for the client
  260. case 'multienum':
  261. $value = str_replace("^","",$value);
  262. if (isset($focus->field_name_map[$fields_array[$key]]['options']) && isset($app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']]) )
  263. {
  264. $valueArray = explode(",",$value);
  265. foreach ($valueArray as $multikey => $multivalue )
  266. {
  267. if (isset($app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']][$multivalue]) )
  268. {
  269. $valueArray[$multikey] = $app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']][$multivalue];
  270. }
  271. }
  272. $value = implode(",",$valueArray);
  273. }
  274. break;
  275. }
  276. }
  277. array_push($new_arr, preg_replace("/\"/","\"\"", $value));
  278. }
  279. $line = implode("\"".getDelimiter()."\"", $new_arr);
  280. $line = "\"" .$line;
  281. $line .= "\"\r\n";
  282. $content .= $line;
  283. }
  284. }
  285. return $content;
  286. }
  287. function generateSearchWhere($module, $query) {//this function is similar with function prepareSearchForm() in view.list.php
  288. $seed = loadBean($module);
  289. if(file_exists('modules/'.$module.'/SearchForm.html')){
  290. if(file_exists('modules/' . $module . '/metadata/SearchFields.php')) {
  291. require_once('include/SearchForm/SearchForm.php');
  292. $searchForm = new SearchForm($module, $seed);
  293. }
  294. elseif(!empty($_SESSION['export_where'])) { //bug 26026, sometimes some module doesn't have a metadata/SearchFields.php, the searchfrom is generated in the ListView.php.
  295. // Currently, massupdate will not generate the where sql. It will use the sql stored in the SESSION. But this will cause bug 24722, and it cannot be avoided now.
  296. $where = $_SESSION['export_where'];
  297. $whereArr = explode (" ", trim($where));
  298. if ($whereArr[0] == trim('where')) {
  299. $whereClean = array_shift($whereArr);
  300. }
  301. $where = implode(" ", $whereArr);
  302. //rrs bug: 31329 - previously this was just returning $where, but the problem is the caller of this function
  303. //expects the results in an array, not just a string. So rather than fixing the caller, I felt it would be best for
  304. //the function to return the results in a standard format.
  305. $ret_array['where'] = $where;
  306. $ret_array['searchFields'] =array();
  307. return $ret_array;
  308. }
  309. else {
  310. return;
  311. }
  312. }
  313. else{
  314. require_once('include/SearchForm/SearchForm2.php');
  315. if(file_exists('custom/modules/'.$module.'/metadata/metafiles.php')){
  316. require('custom/modules/'.$module.'/metadata/metafiles.php');
  317. }elseif(file_exists('modules/'.$module.'/metadata/metafiles.php')){
  318. require('modules/'.$module.'/metadata/metafiles.php');
  319. }
  320. if (file_exists('custom/modules/'.$module.'/metadata/searchdefs.php'))
  321. {
  322. require_once('custom/modules/'.$module.'/metadata/searchdefs.php');
  323. }
  324. elseif (!empty($metafiles[$module]['searchdefs']))
  325. {
  326. require_once($metafiles[$module]['searchdefs']);
  327. }
  328. elseif (file_exists('modules/'.$module.'/metadata/searchdefs.php'))
  329. {
  330. require_once('modules/'.$module.'/metadata/searchdefs.php');
  331. }
  332. //fixing bug #48483: Date Range search on custom date field then export ignores range filter
  333. // first of all custom folder should be checked
  334. if(file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
  335. {
  336. require_once('custom/modules/'.$module.'/metadata/SearchFields.php');
  337. }
  338. elseif(!empty($metafiles[$module]['searchfields']))
  339. {
  340. require_once($metafiles[$module]['searchfields']);
  341. }
  342. elseif(file_exists('modules/'.$module.'/metadata/SearchFields.php'))
  343. {
  344. require_once('modules/'.$module.'/metadata/SearchFields.php');
  345. }
  346. if(empty($searchdefs) || empty($searchFields)) {
  347. //for some modules, such as iframe, it has massupdate, but it doesn't have search function, the where sql should be empty.
  348. return;
  349. }
  350. $searchForm = new SearchForm($seed, $module);
  351. $searchForm->setup($searchdefs, $searchFields, 'SearchFormGeneric.tpl');
  352. }
  353. $searchForm->populateFromArray(unserialize(base64_decode($query)));
  354. $where_clauses = $searchForm->generateSearchWhere(true, $module);
  355. if (count($where_clauses) > 0 )$where = '('. implode(' ) AND ( ', $where_clauses) . ')';
  356. $GLOBALS['log']->info("Export Where Clause: {$where}");
  357. $ret_array['where'] = $where;
  358. $ret_array['searchFields'] = $searchForm->searchFields;
  359. return $ret_array;
  360. }
  361. /**
  362. * calls export method to build up a delimited string and some sample instructional text on how to use this file
  363. * @param string type the bean-type to export
  364. * @return string delimited string for export with some tutorial text
  365. */
  366. function exportSample($type) {
  367. global $app_strings;
  368. //first grab the
  369. $_REQUEST['all']=true;
  370. //retrieve the export content
  371. $content = export($type, null, false, true);
  372. //add details on removing the sample data
  373. return $content . $app_strings['LBL_IMPORT_SAMPLE_FILE_TEXT'];
  374. }
  375. //this function will take in the bean and field mapping and return a proper value
  376. function returnFakeDataRow($focus,$field_array,$rowsToReturn = 5){
  377. if(empty($focus) || empty($field_array))
  378. return ;
  379. //include the file that defines $sugar_demodata
  380. include('install/demoData.en_us.php');
  381. $person_bean = false;
  382. if( isset($focus->first_name)){
  383. $person_bean = true;
  384. }
  385. global $timedate;
  386. $returnContent = '';
  387. $counter = 0;
  388. $new_arr = array();
  389. //iterate through the record creation process as many times as defined. Each iteration will create a new row
  390. while($counter < $rowsToReturn){
  391. $counter++;
  392. //go through each field and populate with dummy data if possible
  393. foreach($field_array as $field_name){
  394. if(empty($focus->field_name_map[$field_name]) || empty($focus->field_name_map[$field_name]['type'])){
  395. //type is not set, fill in with empty string and continue;
  396. $returnContent .= '"",';
  397. continue;
  398. }
  399. $field = $focus->field_name_map[$field_name];
  400. //fill in value according to type
  401. $type = $field['type'];
  402. switch ($type) {
  403. case "id":
  404. case "assigned_user_name":
  405. //return new guid string
  406. $returnContent .= '"'.create_guid().'",';
  407. break;
  408. case "int":
  409. //return random number`
  410. $returnContent .= '"'.mt_rand(0,4).'",';
  411. break;
  412. case "name":
  413. //return first, last, user name, or random name string
  414. if($field['name'] == 'first_name'){
  415. $count = count($sugar_demodata['first_name_array']) - 1;
  416. $returnContent .= '"'.$sugar_demodata['last_name_array'][mt_rand(0,$count)].'",';
  417. }elseif($field['name'] == 'last_name'){
  418. $count = count($sugar_demodata['last_name_array']) - 1;
  419. $returnContent .= '"'.$sugar_demodata['last_name_array'][mt_rand(0,$count)].'",';
  420. }elseif($field['name'] == 'user_name'){
  421. $count = count($sugar_demodata['first_name_array']) - 1;
  422. $returnContent .= '"'.$sugar_demodata['last_name_array'][mt_rand(0,$count)].'_'.mt_rand(1,111).'",';
  423. }else{
  424. //return based on bean
  425. if($focus->module_dir =='Accounts'){
  426. $count = count($sugar_demodata['company_name_array']) - 1;
  427. $returnContent .= '"'.$sugar_demodata['company_name_array'][mt_rand(0,$count)].'",';
  428. }elseif($focus->module_dir =='Bugs'){
  429. $count = count($sugar_demodata['bug_seed_names']) - 1;
  430. $returnContent .= '"'.$sugar_demodata['bug_seed_names'][mt_rand(0,$count)].'",';
  431. }elseif($focus->module_dir =='Notes'){
  432. $count = count($sugar_demodata['note_seed_names_and_Descriptions']) - 1;
  433. $returnContent .= '"'.$sugar_demodata['note_seed_names_and_Descriptions'][mt_rand(0,$count)].'",';
  434. }elseif($focus->module_dir =='Calls'){
  435. $count = count($sugar_demodata['call_seed_data_names']) - 1;
  436. $returnContent .= '"'.$sugar_demodata['call_seed_data_names'][mt_rand(0,$count)].'",';
  437. }elseif($focus->module_dir =='Tasks'){
  438. $count = count($sugar_demodata['task_seed_data_names']) - 1;
  439. $returnContent .= '"'.$sugar_demodata['task_seed_data_names'][mt_rand(0,$count)].'",';
  440. }elseif($focus->module_dir =='Meetings'){
  441. $count = count($sugar_demodata['meeting_seed_data_names']) - 1;
  442. $returnContent .= '"'.$sugar_demodata['meeting_seed_data_names'][mt_rand(0,$count)].'",';
  443. }elseif($focus->module_dir =='ProductCategories'){
  444. $count = count($sugar_demodata['productcategory_seed_data_names']) - 1;
  445. $returnContent .= '"'.$sugar_demodata['productcategory_seed_data_names'][mt_rand(0,$count)].'",';
  446. }elseif($focus->module_dir =='ProductTypes'){
  447. $count = count($sugar_demodata['producttype_seed_data_names']) - 1;
  448. $returnContent .= '"'.$sugar_demodata['producttype_seed_data_names'][mt_rand(0,$count)].'",';
  449. }elseif($focus->module_dir =='ProductTemplates'){
  450. $count = count($sugar_demodata['producttemplate_seed_data']) - 1;
  451. $returnContent .= '"'.$sugar_demodata['producttemplate_seed_data'][mt_rand(0,$count)].'",';
  452. }else{
  453. $returnContent .= '"Default Name for '.$focus->module_dir.'",';
  454. }
  455. }
  456. break;
  457. case "relate":
  458. if($field['name'] == 'team_name'){
  459. //apply team names and user_name
  460. $teams_count = count($sugar_demodata['teams']) - 1;
  461. $users_count = count($sugar_demodata['users']) - 1;
  462. $returnContent .= '"'.$sugar_demodata['teams'][mt_rand(0,$teams_count)]['name'].','.$sugar_demodata['users'][mt_rand(0,$users_count)]['user_name'].'",';
  463. }else{
  464. //apply GUID
  465. $returnContent .= '"'.create_guid().'",';
  466. }
  467. break;
  468. case "bool":
  469. //return 0 or 1
  470. $returnContent .= '"'.mt_rand(0,1).'",';
  471. break;
  472. case "text":
  473. //return random text
  474. $returnContent .= '"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna",';
  475. break;
  476. case "team_list":
  477. $teams_count = count($sugar_demodata['teams']) - 1;
  478. //give fake team names (East,West,North,South)
  479. $returnContent .= '"'.$sugar_demodata['teams'][mt_rand(0,$teams_count)]['name'].'",';
  480. break;
  481. case "date":
  482. //return formatted date
  483. $timeStamp = strtotime('now');
  484. $value = date($timedate->dbDayFormat, $timeStamp);
  485. $returnContent .= '"'.$timedate->to_display_date_time($value).'",';
  486. break;
  487. case "datetime":
  488. case "datetimecombo":
  489. //return formatted date time
  490. $timeStamp = strtotime('now');
  491. //Start with db date
  492. $value = date($timedate->dbDayFormat.' '.$timedate->dbTimeFormat, $timeStamp);
  493. //use timedate to convert to user display format
  494. $value = $timedate->to_display_date_time($value);
  495. //finally forma the am/pm to have a space so it can be recognized as a date field in excel
  496. $value = preg_replace('/([pm|PM|am|AM]+)/', ' \1', $value);
  497. $returnContent .= '"'.$value.'",';
  498. break;
  499. case "phone":
  500. $value = '('.mt_rand(300,999).') '.mt_rand(300,999).'-'.mt_rand(1000,9999);
  501. $returnContent .= '"'.$value.'",';
  502. break;
  503. case "varchar":
  504. //process varchar for possible values
  505. if($field['name'] == 'first_name'){
  506. $count = count($sugar_demodata['first_name_array']) - 1;
  507. $returnContent .= '"'.$sugar_demodata['last_name_array'][mt_rand(0,$count)].'",';
  508. }elseif($field['name'] == 'last_name'){
  509. $count = count($sugar_demodata['last_name_array']) - 1;
  510. $returnContent .= '"'.$sugar_demodata['last_name_array'][mt_rand(0,$count)].'",';
  511. }elseif($field['name'] == 'user_name'){
  512. $count = count($sugar_demodata['first_name_array']) - 1;
  513. $returnContent .= '"'.$sugar_demodata['last_name_array'][mt_rand(0,$count)].'_'.mt_rand(1,111).'",';
  514. }elseif($field['name'] == 'title'){
  515. $count = count($sugar_demodata['titles']) - 1;
  516. $returnContent .= '"'.$sugar_demodata['titles'][mt_rand(0,$count)].'",';
  517. }elseif(strpos($field['name'],'address_street')>0){
  518. $count = count($sugar_demodata['street_address_array']) - 1;
  519. $returnContent .= '"'.$sugar_demodata['street_address_array'][mt_rand(0,$count)].'",';
  520. }elseif(strpos($field['name'],'address_city')>0){
  521. $count = count($sugar_demodata['city_array']) - 1;
  522. $returnContent .= '"'.$sugar_demodata['city_array'][mt_rand(0,$count)].'",';
  523. }elseif(strpos($field['name'],'address_state')>0){
  524. $state_arr = array('CA','NY','CO','TX','NV');
  525. $count = count($state_arr) - 1;
  526. $returnContent .= '"'.$state_arr[mt_rand(0,$count)].'",';
  527. }elseif(strpos($field['name'],'address_postalcode')>0){
  528. $returnContent .= '"'.mt_rand(12345,99999).'",';
  529. }else{
  530. $returnContent .= '"",';
  531. }
  532. break;
  533. case "url":
  534. $returnContent .= '"https://www.sugarcrm.com",';
  535. break;
  536. case "enum":
  537. //get the associated enum if available
  538. global $app_list_strings;
  539. if(isset($focus->field_name_map[$field_name]['type']) && !empty($focus->field_name_map[$field_name]['options'])){
  540. if ( !empty($app_list_strings[$focus->field_name_map[$field_name]['options']]) ) {
  541. //get the values into an array
  542. $dd_values = $app_list_strings[$focus->field_name_map[$field_name]['options']];
  543. $dd_values = array_values($dd_values);
  544. //grab the count
  545. $count = count($dd_values) - 1;
  546. //choose one at random
  547. $returnContent .= '"'.$dd_values[mt_rand(0,$count)].'",';
  548. } else{
  549. //name of enum options array was found but is empty, return blank
  550. $returnContent .= '"",';
  551. }
  552. }else{
  553. //name of enum options array was not found on field, return blank
  554. $returnContent .= '"",';
  555. }
  556. break;
  557. default:
  558. //type is not matched, fill in with empty string and continue;
  559. $returnContent .= '"",';
  560. }
  561. }
  562. $returnContent .= "\r\n";
  563. }
  564. return $returnContent;
  565. }
  566. //expects the field name to translate and a bean of the type being translated (to access field map and mod_strings)
  567. function translateForExport($field_db_name,$focus){
  568. global $mod_strings,$app_strings;
  569. if (empty($field_db_name) || empty($focus)){
  570. return false;
  571. }
  572. //grab the focus module strings
  573. $temp_mod_strings = $mod_strings;
  574. global $current_language;
  575. $mod_strings = return_module_language($current_language, $focus->module_dir);
  576. $fieldLabel = '';
  577. //!! first check to see if we are overriding the label for export.
  578. if (!empty($mod_strings['LBL_EXPORT_'.strtoupper($field_db_name)])){
  579. //entry exists which means we are overriding this value for exporting, use this label
  580. $fieldLabel = $mod_strings['LBL_EXPORT_'.strtoupper($field_db_name)];
  581. }
  582. //!! next check to see if we are overriding the label for export on app_strings.
  583. elseif (!empty($app_strings['LBL_EXPORT_'.strtoupper($field_db_name)])){
  584. //entry exists which means we are overriding this value for exporting, use this label
  585. $fieldLabel = $app_strings['LBL_EXPORT_'.strtoupper($field_db_name)];
  586. }//check to see if label exists in mapping and in mod strings
  587. elseif (!empty($focus->field_name_map[$field_db_name]['vname']) && !empty($mod_strings[$focus->field_name_map[$field_db_name]['vname']])){
  588. $fieldLabel = $mod_strings[$focus->field_name_map[$field_db_name]['vname']];
  589. }//check to see if label exists in mapping and in app strings
  590. elseif (!empty($focus->field_name_map[$field_db_name]['vname']) && !empty($app_strings[$focus->field_name_map[$field_db_name]['vname']])){
  591. $fieldLabel = $app_strings[$focus->field_name_map[$field_db_name]['vname']];
  592. }//field is not in mapping, so check to see if db can be uppercased and found in mod strings
  593. elseif (!empty($mod_strings['LBL_'.strtoupper($field_db_name)])){
  594. $fieldLabel = $mod_strings['LBL_'.strtoupper($field_db_name)];
  595. }//check to see if db can be uppercased and found in app strings
  596. elseif (!empty($app_strings['LBL_'.strtoupper($field_db_name)])){
  597. $fieldLabel = $app_strings['LBL_'.strtoupper($field_db_name)];
  598. }else{
  599. //we could not find the label in mod_strings or app_strings based on either a mapping entry
  600. //or on the db_name itself or being overwritten, so default to the db name as a last resort
  601. $fieldLabel = $field_db_name;
  602. }
  603. //strip the label of any columns
  604. $fieldLabel= preg_replace("/([:]|\xEF\xBC\x9A)[\\s]*$/", '', trim($fieldLabel));
  605. //reset the bean mod_strings back to original import strings
  606. $mod_strings = $temp_mod_strings;
  607. return $fieldLabel;
  608. }
  609. //call this function to return the desired order to display columns for export in.
  610. //if you pass in an array, it will reorder the array and send back to you. It expects the array
  611. //to have the db names as key values, or as labels
  612. function get_field_order_mapping($name='',$reorderArr = '', $exclude = true){
  613. //define the ordering of fields, note that the key value is what is important, and should be the db field name
  614. $field_order_array = array();
  615. $field_order_array['accounts'] = array( 'name'=>'Name', 'id'=>'ID', 'website'=>'Website', 'email_address' =>'Email Address', 'phone_office' =>'Office Phone', 'phone_alternate' => 'Alternate Phone', 'phone_fax' => 'Fax', 'billing_address_street' => 'Billing Street', 'billing_address_city' => 'Billing City', 'billing_address_state' => 'Billing State', 'billing_address_postalcode' => 'Billing Postal Code', 'billing_address_country' => 'Billing Country', 'shipping_address_street' => 'Shipping Street', 'shipping_address_city' => 'Shipping City', 'shipping_address_state' => 'Shipping State', 'shipping_address_postalcode' => 'Shipping Postal Code', 'shipping_address_country' => 'Shipping Country', 'description' => 'Description', 'account_type' => 'Type', 'industry' =>'Industry', 'annual_revenue' => 'Annual Revenue', 'employees' => 'Employees', 'sic_code' => 'SIC Code', 'ticker_symbol' => 'Ticker Symbol', 'parent_id' => 'Parent Account ID', 'ownership' =>'Ownership', 'campaign_id' =>'Campaign ID', 'rating' =>'Rating', 'assigned_user_name' =>'Assigned to', 'assigned_user_id' =>'Assigned User ID', 'team_id' =>'Team Id', 'team_name' =>'Teams', 'team_set_id' =>'Team Set ID', 'date_entered' =>'Date Created', 'date_modified' =>'Date Modified', 'modified_user_id' =>'Modified By', 'created_by' =>'Created By', 'deleted' =>'Deleted');
  616. $field_order_array['contacts'] = array( 'first_name' => 'First Name', 'last_name' => 'Last Name', 'id'=>'ID', 'salutation' => 'Salutation', 'title' => 'Title', 'department' => 'Department', 'account_name' => 'Account Name', 'email_address' => 'Email Address', 'phone_mobile' => 'Phone Mobile','phone_work' => 'Phone Work', 'phone_home' => 'Phone Home', 'phone_other' => 'Phone Other','phone_fax' => 'Phone Fax', 'primary_address_street' => 'Primary Address Street', 'primary_address_city' => 'Primary Address City', 'primary_address_state' => 'Primary Address State', 'primary_address_postalcode' => 'Primary Address Postal Code', 'primary_address_country' => 'Primary Address Country', 'alt_address_street' => 'Alternate Address Street', 'alt_address_city' => 'Alternate Address City', 'alt_address_state' => 'Alternate Address State', 'alt_address_postalcode' => 'Alternate Address Postal Code', 'alt_address_country' => 'Alternate Address Country', 'description' => 'Description', 'birthdate' => 'Birthdate', 'lead_source' => 'Lead Source', 'campaign_id' => 'campaign_id', 'do_not_call' => 'Do Not Call', 'portal_name' => 'Portal Name', 'portal_active' => 'Portal Active', 'portal_password' => 'Portal Password', 'portal_app' => 'Portal Application', 'reports_to_id' => 'Reports to ID', 'assistant' => 'Assistant', 'assistant_phone' => 'Assistant Phone', 'picture' => 'Picture', 'assigned_user_name' => 'Assigned User Name', 'assigned_user_id' => 'Assigned User ID', 'team_name' => 'Teams', 'team_id' => 'Team id', 'team_set_id' => 'Team Set ID', 'date_entered' =>'Date Created', 'date_modified' =>'Date Modified', 'modified_user_id' =>'Modified By', 'created_by' =>'Created By', 'deleted' =>'Deleted');
  617. $field_order_array['leads'] = array( 'first_name' => 'First Name', 'last_name' => 'Last Name', 'id'=>'ID', 'salutation' => 'Salutation', 'title' => 'Title', 'department' => 'Department', 'account_name' => 'Account Name', 'account_description' => 'Account Description', 'website' => 'Website', 'email_address' => 'Email Address', 'phone_mobile' => 'Phone Mobile', 'phone_work' => 'Phone Work', 'phone_home' => 'Phone Home', 'phone_other' => 'Phone Other', 'phone_fax' => 'Phone Fax', 'primary_address_street' => 'Primary Address Street', 'primary_address_city' => 'Primary Address City', 'primary_address_state' => 'Primary Address State', 'primary_address_postalcode' => 'Primary Address Postal Code', 'primary_address_country' => 'Primary Address Country', 'alt_address_street' => 'Alt Address Street', 'alt_address_city' => 'Alt Address City', 'alt_address_state' => 'Alt Address State', 'alt_address_postalcode' => 'Alt Address Postalcode', 'alt_address_country' => 'Alt Address Country', 'status' => 'Status', 'status_description' => 'Status Description', 'lead_source' => 'Lead Source', 'lead_source_description' => 'Lead Source Description', 'description'=>'Description', 'converted' => 'Converted', 'opportunity_name' => 'Opportunity Name', 'opportunity_amount' => 'Opportunity Amount', 'refered_by' => 'Referred By', 'campaign_id' => 'campaign_id', 'do_not_call' => 'Do Not Call', 'portal_name' => 'Portal Name', 'portal_app' => 'Portal Application', 'reports_to_id' => 'Reports To ID', 'assistant' => 'Assistant', 'assistant_phone' => 'Assistant Phone', 'birthdate'=>'Birthdate', 'contact_id' => 'Contact ID', 'account_id' => 'Account ID', 'opportunity_id' => 'Opportunity ID', 'assigned_user_name' => 'Assigned User Name', 'assigned_user_id' => 'Assigned User ID', 'team_name' => 'Teams', 'team_id' => 'Team id', 'team_set_id' => 'Team Set ID', 'date_entered' => 'Date Created', 'date_modified' => 'Date Modified', 'created_by' => 'Created By ID', 'modified_user_id' => 'Modified By ID', 'deleted' => 'Deleted');
  618. $field_order_array['opportunities'] = array( 'name' => 'Opportunity Name', 'id'=>'ID', 'amount' => 'Opportunity Amount', 'currency_id' => 'Currency', 'date_closed' => 'Expected Close Date', 'sales_stage' => 'Sales Stage', 'probability' => 'Probability (%)', 'next_step' => 'Next Step', 'opportunity_type' => 'Opportunity Type', 'account_name' => 'Account Name', 'description' => 'Description', 'amount_usdollar' => 'Amount', 'lead_source' => 'Lead Source', 'campaign_id' => 'campaign_id', 'assigned_user_name' => 'Assigned User Name', 'assigned_user_id' => 'Assigned User ID', 'team_name' => 'Teams', 'team_id' => 'Team id', 'team_set_id' => 'Team Set ID', 'date_entered' => 'Date Created', 'date_modified' => 'Date Modified', 'created_by' => 'Created By ID', 'modified_user_id' => 'Modified By ID', 'deleted' => 'Deleted');
  619. $field_order_array['notes'] = array( 'name' => 'Name', 'id'=>'ID', 'description' => 'Description', 'filename' => 'Attachment', 'parent_type' => 'Parent Type', 'parent_id' => 'Parent ID', 'contact_id' => 'Contact ID', 'portal_flag' => 'Display in Portal?', 'assigned_user_name' =>'Assigned to', 'assigned_user_id' => 'assigned_user_id', 'team_id' => 'Team id', 'team_set_id' => 'Team Set ID', 'date_entered' => 'Date Created', 'date_modified' => 'Date Modified', 'created_by' => 'Created By ID', 'modified_user_id' => 'Modified By ID', 'deleted' => 'Deleted' );
  620. $field_order_array['bugs'] = array('bug_number' => 'Bug Number', 'id'=>'ID', 'name' => 'Subject', 'description' => 'Description', 'status' => 'Status', 'type' => 'Type', 'priority' => 'Priority', 'resolution' => 'Resolution', 'work_log' => 'Work Log', 'found_in_release' => 'Found In Release', 'fixed_in_release' => 'Fixed In Release', 'found_in_release_name' => 'Found In Release Name', 'fixed_in_release_name' => 'Fixed In Release', 'product_category' => 'Category', 'source' => 'Source', 'portal_viewable' => 'Portal Viewable', 'system_id' => 'System ID', 'assigned_user_id' => 'Assigned User ID', 'assigned_user_name' => 'Assigned User Name', 'team_name'=>'Teams', 'team_id' => 'Team id', 'team_set_id' => 'Team Set ID', 'date_entered' =>'Date Created', 'date_modified' =>'Date Modified', 'modified_user_id' =>'Modified By', 'created_by' =>'Created By', 'deleted' =>'Deleted');
  621. $field_order_array['tasks'] = array( 'name'=>'Subject', 'id'=>'ID', 'description'=>'Description', 'status'=>'Status', 'date_start'=>'Date Start', 'date_due'=>'Date Due','priority'=>'Priority', 'parent_type'=>'Parent Type', 'parent_id'=>'Parent ID', 'contact_id'=>'Contact ID', 'assigned_user_name' =>'Assigned to', 'assigned_user_id'=>'Assigned User ID', 'team_name'=>'Teams', 'team_id'=>'Team id', 'team_set_id'=>'Team Set ID', 'date_entered'=>'Date Created', 'date_modified'=>'Date Modified', 'created_by'=>'Created By ID', 'modified_user_id'=>'Modified By ID', 'deleted'=>'Deleted');
  622. $field_order_array['calls'] = array( 'name'=>'Subject', 'id'=>'ID', 'description'=>'Description', 'status'=>'Status', 'direction'=>'Direction', 'date_start'=>'Date', 'date_end'=>'Date End', 'duration_hours'=>'Duration Hours', 'duration_minutes'=>'Duration Minutes', 'reminder_time'=>'Reminder Time', 'parent_type'=>'Parent Type', 'parent_id'=>'Parent ID', 'outlook_id'=>'Outlook ID', 'assigned_user_name' =>'Assigned to', 'assigned_user_id'=>'Assigned User ID', 'team_name'=>'Teams', 'team_id'=>'Team id', 'team_set_id'=>'Team Set ID', 'date_entered'=>'Date Created', 'date_modified'=>'Date Modified', 'created_by'=>'Created By ID', 'modified_user_id'=>'Modified By ID', 'deleted'=>'Deleted');
  623. $field_order_array['meetings'] =array( 'name'=>'Subject', 'id'=>'ID', 'description'=>'Description', 'status'=>'Status', 'location'=>'Location', 'date_start'=>'Date', 'date_end'=>'Date End', 'duration_hours'=>'Duration Hours', 'duration_minutes'=>'Duration Minutes', 'reminder_time'=>'Reminder Time', 'type'=>'Meeting Type', 'external_id'=>'External ID', 'password'=>'Meeting Password', 'join_url'=>'Join Url', 'host_url'=>'Host Url', 'displayed_url'=>'Displayed Url', 'creator'=>'Meeting Creator', 'parent_type'=>'Related to', 'parent_id'=>'Related to', 'outlook_id'=>'Outlook ID','assigned_user_name' =>'Assigned to','assigned_user_id' => 'Assigned User ID', 'team_name' => 'Teams', 'team_id' => 'Team id', 'team_set_id' => 'Team Set ID', 'date_entered' => 'Date Created', 'date_modified' => 'Date Modified', 'created_by' => 'Created By ID', 'modified_user_id' => 'Modified By ID', 'deleted' => 'Deleted');
  624. $field_order_array['cases'] =array( 'case_number'=>'Case Number', 'id'=>'ID', 'name'=>'Subject', 'description'=>'Description', 'status'=>'Status', 'type'=>'Type', 'priority'=>'Priority', 'resolution'=>'Resolution', 'work_log'=>'Work Log', 'portal_viewable'=>'Portal Viewable', 'account_name'=>'Account Name', 'account_id'=>'Account ID', 'assigned_user_id'=>'Assigned User ID', 'team_name'=>'Teams', 'team_id'=>'Team id', 'team_set_id'=>'Team Set ID', 'date_entered'=>'Date Created', 'date_modified'=>'Date Modified', 'created_by'=>'Created By ID', 'modified_user_id'=>'Modified By ID', 'deleted'=>'Deleted');
  625. $field_order_array['prospects'] =array( 'first_name'=>'First Name', 'last_name'=>'Last Name', 'id'=>'ID', 'salutation'=>'Salutation', 'title'=>'Title', 'department'=>'Department', 'account_name'=>'Account Name', 'email_address'=>'Email Address', 'phone_mobile' => 'Phone Mobile', 'phone_work' => 'Phone Work', 'phone_home' => 'Phone Home', 'phone_other' => 'Phone Other', 'phone_fax' => 'Phone Fax', 'primary_address_street' => 'Primary Address Street', 'primary_address_city' => 'Primary Address City', 'primary_address_state' => 'Primary Address State', 'primary_address_postalcode' => 'Primary Address Postal Code', 'primary_address_country' => 'Primary Address Country', 'alt_address_street' => 'Alternate Address Street', 'alt_address_city' => 'Alternate Address City', 'alt_address_state' => 'Alternate Address State', 'alt_address_postalcode' => 'Alternate Address Postal Code', 'alt_address_country' => 'Alternate Address Country', 'description' => 'Description', 'birthdate' => 'Birthdate', 'assistant'=>'Assistant', 'assistant_phone'=>'Assistant Phone', 'campaign_id'=>'campaign_id', 'tracker_key'=>'Tracker Key', 'do_not_call'=>'Do Not Call', 'lead_id'=>'Lead Id', 'assigned_user_name'=>'Assigned User Name', 'assigned_user_id'=>'Assigned User ID', 'team_id' =>'Team Id', 'team_name' =>'Teams', 'team_set_id' =>'Team Set ID', 'date_entered' =>'Date Created', 'date_modified' =>'Date Modified', 'modified_user_id' =>'Modified By', 'created_by' =>'Created By', 'deleted' =>'Deleted');
  626. $fields_to_exclude = array();
  627. $fields_to_exclude['accounts'] = array('account_name');
  628. $fields_to_exclude['bugs'] = array('system_id');
  629. $fields_to_exclude['cases'] = array('system_id', 'modified_by_name', 'modified_by_name_owner', 'modified_by_name_mod', 'created_by_name', 'created_by_name_owner', 'created_by_name_mod', 'assigned_user_name', 'assigned_user_name_owner', 'assigned_user_name_mod', 'team_count', 'team_count_owner', 'team_count_mod', 'team_name_owner', 'team_name_mod', 'account_name_owner', 'account_name_mod', 'modified_user_name', 'modified_user_name_owner', 'modified_user_name_mod');
  630. $fields_to_exclude['notes'] = array('first_name','last_name', 'file_mime_type','embed_flag');
  631. $fields_to_exclude['tasks'] = array('date_start_flag', 'date_due_flag');
  632. //of array is passed in for reordering, process array
  633. if(!empty($name) && !empty($reorderArr) && is_array($reorderArr)){
  634. //make sure reorderArr has values as keys, if not then iterate through and assign the value as the key
  635. $newReorder = array();
  636. foreach($reorderArr as $rk=> $rv){
  637. if(is_int($rk)){
  638. $newReorder[$rv]=$rv;
  639. }else{
  640. $newReorder[$rk]=$rv;
  641. }
  642. }
  643. //if module is not defined, lets default the order to another module of the same type
  644. //this would apply mostly to custom modules
  645. if(!isset($field_order_array[strtolower($name)]) && isset($_REQUEST['module'])){
  646. $exemptModuleList = array('ProspectLists');
  647. if(in_array($name, $exemptModuleList))
  648. return $newReorder;
  649. //get an instance of the bean
  650. global $beanList;
  651. global $beanFiles;
  652. $bean = $beanList[$_REQUEST['module']];
  653. require_once($beanFiles[$bean]);
  654. $focus = new $bean;
  655. //if module is of type person
  656. if($focus instanceof Person){
  657. $name = 'contacts';
  658. }
  659. //if module is of type company
  660. else if ($focus instanceof Company){
  661. $name = 'accounts';
  662. }
  663. //if module is of type Sale
  664. else if ($focus instanceof Sale){
  665. $name = 'opportunities';
  666. }//if module is of type File
  667. else if ($focus instanceof Issue){
  668. $name = 'bugs';
  669. }//all others including type File can use basic
  670. else{
  671. $name = 'Notes';
  672. }
  673. }
  674. //lets iterate through and create a reordered temporary array using
  675. //the newly formatted copy of passed in array
  676. $temp_result_arr = array();
  677. $lname = strtolower($name);
  678. if(!empty($field_order_array[$lname])) {
  679. foreach($field_order_array[$lname] as $fk=> $fv){
  680. //if the value exists as a key in the passed in array, add to temp array and remove from reorder array.
  681. //Do not force into the temp array as we don't want to violate acl's
  682. if(array_key_exists($fk,$newReorder)){
  683. $temp_result_arr[$fk] = $newReorder[$fk];
  684. unset($newReorder[$fk]);
  685. }
  686. }
  687. }
  688. //add in all the left over values that were not in our ordered list
  689. //array_splice($temp_result_arr, count($temp_result_arr), 0, $newReorder);
  690. foreach($newReorder as $nrk=>$nrv){
  691. $temp_result_arr[$nrk] = $nrv;
  692. }
  693. if($exclude){
  694. //Some arrays have values we wish to exclude
  695. if (isset($fields_to_exclude[$lname])){
  696. foreach($fields_to_exclude[$lname] as $exclude_field){
  697. unset($temp_result_arr[$exclude_field]);
  698. }
  699. }
  700. }
  701. //return temp ordered list
  702. return $temp_result_arr;
  703. }
  704. //if no array was passed in, pass back either the list of ordered columns by module, or the entireorder array
  705. if(empty($name)){
  706. return $field_order_array;
  707. }else{
  708. return $field_order_array[strtolower($name)];
  709. }
  710. }
  711. ?>