Skip to content

Instantly share code, notes, and snippets.

@patpawlowski
Last active July 14, 2017 14:36
Show Gist options
  • Save patpawlowski/10954eaa219e08eb35ac899f9be63bad to your computer and use it in GitHub Desktop.
Save patpawlowski/10954eaa219e08eb35ac899f9be63bad to your computer and use it in GitHub Desktop.
PHP Script to create a csv of Sugar module names, fields, and labels
<?php
/**
* Created by NetBeans.
* User: patpawlowski
* Date: Jul 14, 2017 at 10:09:28 AM
* File: sugar_create_field_list.php
* Directions: just execute the file in the root of a Sugar CRM instance.
* Tested on Sugar 7.8; I doubt it will work on 6.x.
* Execute: php sugar_create_field_list.php
*
* There are some paramenters at the beginning of the class that you may want to change.
* private $FileName = 'SugarFieldsAndLabels.csv'; // You can change the output file name here.
* private $ModulesToSkip = array('SugarBox'); // Add modules to skip here. SugarBox caused an error so I skipped it.
* private $ModulesToInclude = array('Accounts','Contacts'); // You can limit the modules here. An empty array will output all modules though.
*
* Sample Output:
* Module,Table,Field,Label
* Accounts,accounts,id,ID
* Accounts,accounts,name,Name
* Accounts,accounts,date_entered,"Date Created:"
* Accounts,accounts,date_modified,"Date Modified:"
* Accounts,accounts,modified_user_id,"Modified by"
*
*/
if(!defined('sugarEntry'))define('sugarEntry', true);
/*
* Your installation or use of this SugarCRM file is subject to the applicable
* terms available at
* http://support.sugarcrm.com/Resources/Master_Subscription_Agreements/.
* If you do not agree to all of the applicable terms or do not have the
* authority to bind the entity as an authorized representative, then do not
* install or use this SugarCRM file.
*
* Copyright (C) SugarCRM Inc. All rights reserved.
*/
//change directories to where this file is located.
chdir(dirname(__FILE__));
define('ENTRY_POINT_TYPE', 'api');
require_once('include/entryPoint.php');
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) != 'cli') {
sugar_die("cron.php is CLI only.");
}
SugarMetric_Manager::getInstance()->setMetricClass('background')->setTransactionName('cron');
if(empty($current_language)) {
$current_language = $sugar_config['default_language'];
}
$app_list_strings = return_app_list_strings_language($current_language);
$app_strings = return_application_language($current_language);
global $current_user;
$current_user = BeanFactory::getBean('Users');
$current_user->getSystemUser();
// Do stuff
require_once('include/utils.php');
class CSVCreator {
private $FileName = 'SugarFieldsAndLabels.csv';
private $ModulesToSkip = array('SugarBox');
private $ModulesToInclude = array();
private $FieldsArray = array();
private $app_strings;
public function run($app_strings) {
$this->app_strings = $app_strings;
$this->createFieldsArray();
$this->writeFieldsArrayToFile();
}
private function createFieldsArray() {
$module_list = array_intersect($GLOBALS['moduleList'],array_keys($GLOBALS['beanList']));
$this->FieldsArray[] = array('Module', 'Table', 'Field', 'Label');
foreach($module_list as $module_name) {
if(in_array($module_name, $this->ModulesToSkip)) {continue;}
if(count($this->ModulesToInclude) > 0 && !in_array($module_name, $this->ModulesToInclude)) {continue;}
$bean = BeanFactory::getBean($module_name);
foreach($bean->field_name_map as $field){
$label = $this->getLabel($field);
$table = substr($field['name'], -2) == "_c" ? $bean->table_name."_cstm":$bean->table_name;
$this->FieldsArray[] = array($module_name, $table, $field['name'], $label);
}
}
}
private function writeFieldsArrayToFile() {
$OutFile = fopen($this->FileName, 'w');
foreach ($this->FieldsArray as $Field) {
fputcsv($OutFile, $Field);
}
fclose($OutFile);
}
private function getLabel($field) {
if(isset($field['labelValue'])){
return $field['labelValue'];
}elseif(isset($field['vname'])){
return translate($field['vname']);
}else{
return '';
}
}
}
$CSVCreator = new CSVCreator();
$CSVCreator->run($app_strings);
// Stop doing stuff and clean up
$exit_on_cleanup = true;
sugar_cleanup(false);
// some jobs have annoying habit of calling sugar_cleanup(), and it can be called only once
// but job results can be written to DB after job is finished, so we have to disconnect here again
// just in case we couldn't call cleanup
if(class_exists('DBManagerFactory')) {
$db = DBManagerFactory::getInstance();
$db->disconnect();
}
// If we have a session left over, destroy it
if(session_id()) {
session_destroy();
}
$exit_on_cleanup;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment