示例#1
0
/**
 * upgradeTeamColumn
 * Helper function to create a team_set_id column and also set team_set_id column
 * to have the value of the $column_name parameter
 *
 * @param $bean SugarBean which we are adding team_set_id column to
 * @param $column_name The name of the column containing the default team_set_id value
 */
function upgradeTeamColumn($bean, $column_name)
{
    //first let's check to ensure that the team_set_id field is defined, if not it could be the case that this is an older
    //module that does not use the SugarObjects
    if (empty($bean->field_defs['team_set_id']) && $bean->module_dir != 'Trackers') {
        //at this point we could assume that since we have a team_id defined and not a team_set_id that we need to
        //add that field and the corresponding relationships
        $object = $bean->object_name;
        $module = $bean->module_dir;
        $object_name = $object;
        $_object_name = strtolower($object_name);
        if (!empty($GLOBALS['dictionary'][$object]['table'])) {
            $table_name = $GLOBALS['dictionary'][$object]['table'];
        } else {
            $table_name = strtolower($module);
        }
        $path = 'include/SugarObjects/implements/team_security/vardefs.php';
        require $path;
        //go through each entry in the vardefs from team_security and unset anything that is already set in the core module
        //this will ensure we have the proper ordering.
        $fieldDiff = array_diff_assoc($vardefs['fields'], $GLOBALS['dictionary'][$bean->object_name]['fields']);
        $file = 'custom/Extension/modules/' . $bean->module_dir . '/Ext/Vardefs/teams.php';
        $contents = "<?php\n";
        if (!empty($fieldDiff)) {
            foreach ($fieldDiff as $key => $val) {
                $contents .= "\n\$GLOBALS['dictionary']['" . $object . "']['fields']['" . $key . "']=" . var_export_helper($val) . ";";
            }
        }
        $relationshipDiff = array_diff_assoc($vardefs['relationships'], $GLOBALS['dictionary'][$bean->object_name]['relationships']);
        if (!empty($relationshipDiff)) {
            foreach ($relationshipDiff as $key => $val) {
                $contents .= "\n\$GLOBALS['dictionary']['" . $object . "']['relationships']['" . $key . "']=" . var_export_helper($val) . ";";
            }
        }
        $indexDiff = array_diff_assoc($vardefs['indices'], $GLOBALS['dictionary'][$bean->object_name]['indices']);
        if (!empty($indexDiff)) {
            foreach ($indexDiff as $key => $val) {
                $contents .= "\n\$GLOBALS['dictionary']['" . $object . "']['indices']['" . $key . "']=" . var_export_helper($val) . ";";
            }
        }
        if ($fh = @sugar_fopen($file, 'wt')) {
            fputs($fh, $contents);
            fclose($fh);
        }
        //we have written out the teams.php into custom/Extension/modules/{$module_dir}/Ext/Vardefs/teams.php'
        //now let's merge back into vardefs.ext.php
        require_once 'ModuleInstall/ModuleInstaller.php';
        $mi = new ModuleInstaller();
        $mi->merge_files('Ext/Vardefs/', 'vardefs.ext.php');
        VardefManager::loadVardef($bean->module_dir, $bean->object_name, true);
        $bean->field_defs = $GLOBALS['dictionary'][$bean->object_name]['fields'];
    }
    if (isset($bean->field_defs['team_set_id'])) {
        //Create the team_set_id column
        $FieldArray = $GLOBALS['db']->helper->get_columns($bean->table_name);
        if (!isset($FieldArray['team_set_id'])) {
            $GLOBALS['db']->addColumn($bean->table_name, $bean->field_defs['team_set_id']);
        }
        $indexArray = $GLOBALS['db']->helper->get_indices($bean->table_name);
        $indexName = getValidDBName('idx_' . strtolower($bean->table_name) . '_tmst_id', true, 34);
        $indexDef = array(array('name' => $indexName, 'type' => 'index', 'fields' => array('team_set_id')));
        if (!isset($indexArray[$indexName])) {
            $GLOBALS['db']->addIndexes($bean->table_name, $indexDef);
        }
        //Update the table's team_set_id column to have the same values as team_id
        $GLOBALS['db']->query("UPDATE {$bean->table_name} SET team_set_id = {$column_name}");
    }
}
/**
 * write_to_modules_ext_php
 * Writes the given module, class and path values to custom/Extensions/application/Include directory
 * for the module
 * @param $class String value of the class name of the module
 * @param $module String value of the name of the module entry
 * @param $path String value of the path of the module class file
 * @param $show Boolean value to determine whether or not entry should be added to moduleList or modInvisList Array
 */
function write_to_modules_ext_php($class, $module, $path, $show = false)
{
    include 'include/modules.php';
    global $beanList, $beanFiles;
    if (!isset($beanFiles[$class])) {
        $str = "<?php \n //WARNING: The contents of this file are auto-generated\n";
        if (!empty($module) && !empty($class) && !empty($path)) {
            $str .= "\$beanList['{$module}'] = '{$class}';\n";
            $str .= "\$beanFiles['{$class}'] = '{$path}';\n";
            if ($show) {
                $str .= "\$moduleList[] = '{$module}';\n";
            } else {
                $str .= "\$modules_exempt_from_availability_check['{$module}'] = '{$module}';\n";
                $str .= "\$modInvisList[] = '{$module}';\n";
            }
        }
        $str .= "\n?>";
        if (!file_exists("custom/Extension/application/Ext/Include")) {
            mkdir_recursive("custom/Extension/application/Ext/Include", true);
        }
        $out = sugar_fopen("custom/Extension/application/Ext/Include/{$module}.php", 'w');
        fwrite($out, $str);
        fclose($out);
        require_once 'ModuleInstall/ModuleInstaller.php';
        $moduleInstaller = new ModuleInstaller();
        $moduleInstaller->merge_files('Ext/Include', 'modules.ext.php', '', true);
    }
}