Ejemplo n.º 1
0
 /**
  *	Esta funcion devuelve los tags disponibles para el PDF
  */
 public static function getTags($plantilla_id)
 {
     require_once "modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php";
     $res = array();
     $plantilla_bean = BeanFactory::getBean('opalo_plantillas', $plantilla_id);
     $modulo = $plantilla_bean->pariente;
     $relaciones = unencodeMultienum($plantilla_bean->relaciones);
     ///CARGA LOS CAMPOS DEL MODULO
     $bean = BeanFactory::newBean($modulo);
     foreach ($bean->field_name_map as $campo => $data) {
         $res[$campo] = $data['type'];
     }
     $x = new DeployedRelationships($modulo);
     ///CARGA LOS CAMPOS DE LA RELACION <relationship_name>::<field_name>
     foreach ($relaciones as $relation) {
         $rel = $bean->field_name_map[$relation]['relationship'];
         $def = $x->get($rel)->getDefinition();
         $otro_modulo = $def['rhs_module'];
         if ($modulo == $def['rhs_module']) {
             $otro_modulo = $def['lhs_module'];
         }
         $bean_seg = BeanFactory::newBean($otro_modulo);
         foreach ($bean_seg->field_name_map as $campo => $data) {
             $res[$relation . "::" . $campo] = $data['type'];
         }
         $bean->load_relationship($relation);
     }
     return $res;
 }
/**
 * Searches through the installed relationships to find broken self referencing one-to-many relationships 
 * (wrong field used in the subpanel, and the left link not marked as left)
 */
function upgrade_custom_relationships($modules = array())
{
    global $current_user, $moduleList;
    if (!is_admin($current_user)) {
        sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
    }
    require_once "modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php";
    require_once "modules/ModuleBuilder/parsers/relationships/OneToManyRelationship.php";
    if (empty($modules)) {
        $modules = $moduleList;
    }
    foreach ($modules as $module) {
        $depRels = new DeployedRelationships($module);
        $relList = $depRels->getRelationshipList();
        foreach ($relList as $relName) {
            $relObject = $depRels->get($relName);
            $def = $relObject->getDefinition();
            //We only need to fix self referencing one to many relationships
            if ($def['lhs_module'] == $def['rhs_module'] && $def['is_custom'] && $def['relationship_type'] == "one-to-many") {
                $layout_defs = array();
                if (!is_dir("custom/Extension/modules/{$module}/Ext/Layoutdefs") || !is_dir("custom/Extension/modules/{$module}/Ext/Vardefs")) {
                    continue;
                }
                //Find the extension file containing the vardefs for this relationship
                foreach (scandir("custom/Extension/modules/{$module}/Ext/Vardefs") as $file) {
                    if (substr($file, 0, 1) != "." && strtolower(substr($file, -4)) == ".php") {
                        $dictionary = array($module => array("fields" => array()));
                        $filePath = "custom/Extension/modules/{$module}/Ext/Vardefs/{$file}";
                        include $filePath;
                        if (isset($dictionary[$module]["fields"][$relName])) {
                            $rhsDef = $dictionary[$module]["fields"][$relName];
                            //Update the vardef for the left side link field
                            if (!isset($rhsDef['side']) || $rhsDef['side'] != 'left') {
                                $rhsDef['side'] = 'left';
                                $fileContents = file_get_contents($filePath);
                                $out = preg_replace('/\\$dictionary[\\w"\'\\[\\]]*?' . $relName . '["\'\\[\\]]*?\\s*?=\\s*?array\\s*?\\(.*?\\);/s', '$dictionary["' . $module . '"]["fields"]["' . $relName . '"]=' . var_export_helper($rhsDef) . ";", $fileContents);
                                file_put_contents($filePath, $out);
                            }
                        }
                    }
                }
                //Find the extension file containing the subpanel definition for this relationship
                foreach (scandir("custom/Extension/modules/{$module}/Ext/Layoutdefs") as $file) {
                    if (substr($file, 0, 1) != "." && strtolower(substr($file, -4)) == ".php") {
                        $layout_defs = array($module => array("subpanel_setup" => array()));
                        $filePath = "custom/Extension/modules/{$module}/Ext/Layoutdefs/{$file}";
                        include $filePath;
                        foreach ($layout_defs[$module]["subpanel_setup"] as $key => $subDef) {
                            if ($layout_defs[$module]["subpanel_setup"][$key]['get_subpanel_data'] == $relName) {
                                $fileContents = file_get_contents($filePath);
                                $out = preg_replace('/[\'"]get_subpanel_data[\'"]\\s*=>\\s*[\'"]' . $relName . '[\'"],/s', "'get_subpanel_data' => '{$def["join_key_lhs"]}',", $fileContents);
                                file_put_contents($filePath, $out);
                            }
                        }
                    }
                }
            }
        }
    }
}
Ejemplo n.º 3
0
 public static function getRelationship($modulo)
 {
     $bean = BeanFactory::newBean($modulo);
     require_once "modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php";
     $res = array();
     $x = new DeployedRelationships($modulo);
     $res = array();
     //print_r($bean->field_name_map);
     foreach ($bean->field_name_map as $field => $data) {
         if ($data['type'] === 'link') {
             if ($def = $x->get($data['relationship'])) {
                 $def = $def->getDefinition();
                 if ($def['relationship_type'] == 'many-to-many') {
                     continue;
                 } elseif ($def['lhs_module'] == $modulo && $def['relationship_type'] == 'one-to-many') {
                     continue;
                 } else {
                     $res[$data['name']] = "{$data['name']} : {$def['lhs_module']} {$def['relationship_type']} {$def['rhs_module']}";
                 }
                 //print_r($def);
             }
         }
     }
     return $res;
 }
Ejemplo n.º 4
0
function repair_long_relationship_names($path = '')
{
    logThis("Begin repair_long_relationship_names", $path);
    require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php';
    $GLOBALS['mi_remove_tables'] = false;
    $touched = array();
    foreach ($GLOBALS['moduleList'] as $module) {
        $relationships = new DeployedRelationships($module);
        foreach ($relationships->getRelationshipList() as $rel_name) {
            if (strlen($rel_name) > 27 && empty($touched[$rel_name])) {
                logThis("Rebuilding relationship fields for {$rel_name}", $path);
                $touched[$rel_name] = true;
                $rel_obj = $relationships->get($rel_name);
                $rel_obj->setReadonly(false);
                $relationships->delete($rel_name);
                $relationships->save();
                $relationships->add($rel_obj);
                $relationships->save();
                $relationships->build();
            }
        }
    }
    logThis("End repair_long_relationship_names", $path);
}
Ejemplo n.º 5
0
 function action_SaveRelationshipLabel()
 {
     $selected_lang = !empty($_REQUEST['relationship_lang']) ? $_REQUEST['relationship_lang'] : $_SESSION['authenticated_user_language'];
     if (empty($_REQUEST['view_package'])) {
         require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php';
         $relationships = new DeployedRelationships($_REQUEST['view_module']);
         if (!empty($_REQUEST['relationship_name'])) {
             if ($relationship = $relationships->get($_REQUEST['relationship_name'])) {
                 $metadata = $relationship->buildLabels(true);
                 require_once 'modules/ModuleBuilder/parsers/parser.label.php';
                 $parser = new ParserLabel($_REQUEST['view_module']);
                 $parser->handleSaveRelationshipLabels($metadata, $selected_lang);
             }
         }
     } else {
         //TODO FOR MB
     }
     $this->view = 'relationships';
 }
Ejemplo n.º 6
0
 /**
  * Removes all custom relationships containing this module
  * 
  * @return html output record of the files deleted
  */
 function removeCustomRelationships()
 {
     require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php';
     $out = "";
     $madeChanges = false;
     $relationships = new DeployedRelationships($this->module);
     foreach ($relationships->getRelationshipList() as $relationshipName) {
         $rel = $relationships->get($relationshipName)->getDefinition();
         if ($rel['is_custom'] || isset($rel['from_studio']) && $rel['from_studio']) {
             $relationships->delete($relationshipName);
             $out .= "Removed relationship {$relationshipName}<br/>";
         }
     }
     if ($madeChanges) {
         $relationships->save();
     }
     return $out;
 }
Ejemplo n.º 7
0
 public function action_SaveRelationshipLabel()
 {
     global $locale;
     if (!empty($_REQUEST['relationship_lang'])) {
         $selected_lang = $_REQUEST['relationship_lang'];
     } else {
         $selected_lang = $locale->getAuthenticatedUserLanguage();
     }
     if (empty($_REQUEST['view_package'])) {
         $relationships = new DeployedRelationships($_REQUEST['view_module']);
         if (!empty($_REQUEST['relationship_name'])) {
             if ($relationship = $relationships->get($_REQUEST['relationship_name'])) {
                 $metadata = $relationship->buildLabels(true);
                 $parser = new ParserLabel($_REQUEST['view_module']);
                 $parser->handleSaveRelationshipLabels($metadata, $selected_lang);
             }
         }
     } else {
         //TODO FOR MB
     }
     $this->view = 'relationships';
 }
/**
 * Searches through the installed relationships to find broken self referencing one-to-many relationships 
 * (wrong field used in the subpanel, and the left link not marked as left)
 */
function upgrade_custom_relationships($modules = array())
{
    global $current_user, $moduleList;
    if (!is_admin($current_user)) {
        sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
    }
    require_once "modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php";
    require_once "modules/ModuleBuilder/parsers/relationships/OneToManyRelationship.php";
    if (empty($modules)) {
        $modules = $moduleList;
    }
    foreach ($modules as $module) {
        $depRels = new DeployedRelationships($module);
        $relList = $depRels->getRelationshipList();
        foreach ($relList as $relName) {
            $relObject = $depRels->get($relName);
            $def = $relObject->getDefinition();
            //We only need to fix self referencing one to many relationships
            if ($def['lhs_module'] == $def['rhs_module'] && $def['is_custom'] && $def['relationship_type'] == "one-to-many") {
                $layout_defs = array();
                if (!is_dir("custom/Extension/modules/{$module}/Ext/Layoutdefs") || !is_dir("custom/Extension/modules/{$module}/Ext/Vardefs")) {
                    continue;
                }
                //Find the extension file containing the vardefs for this relationship
                foreach (scandir("custom/Extension/modules/{$module}/Ext/Vardefs") as $file) {
                    if (substr($file, 0, 1) != "." && strtolower(substr($file, -4)) == ".php") {
                        $dictionary = array($module => array("fields" => array()));
                        $filePath = "custom/Extension/modules/{$module}/Ext/Vardefs/{$file}";
                        include $filePath;
                        if (isset($dictionary[$module]["fields"][$relName])) {
                            $rhsDef = $dictionary[$module]["fields"][$relName];
                            //Update the vardef for the left side link field
                            if (!isset($rhsDef['side']) || $rhsDef['side'] != 'left') {
                                $rhsDef['side'] = 'left';
                                $rhsDef['link-type'] = 'one';
                                $fileContents = file_get_contents($filePath);
                                $out = preg_replace('/\\$dictionary[\\w"\'\\[\\]]*?' . $relName . '["\'\\[\\]]*?\\s*?=\\s*?array\\s*?\\(.*?\\);/s', '$dictionary["' . $module . '"]["fields"]["' . $relName . '"]=' . var_export_helper($rhsDef) . ";", $fileContents);
                                file_put_contents($filePath, $out);
                            }
                        }
                    }
                }
                //Find the extension file containing the subpanel definition for this relationship
                foreach (scandir("custom/Extension/modules/{$module}/Ext/Layoutdefs") as $file) {
                    if (substr($file, 0, 1) != "." && strtolower(substr($file, -4)) == ".php") {
                        $layout_defs = array($module => array("subpanel_setup" => array()));
                        $filePath = "custom/Extension/modules/{$module}/Ext/Layoutdefs/{$file}";
                        include $filePath;
                        $bean = BeanFactory::getBean($module);
                        $fields = $bean->getFieldDefinitions();
                        foreach ($layout_defs[$module]["subpanel_setup"] as $key => $subDef) {
                            if (isset($layout_defs[$module]["subpanel_setup"][$key]['get_subpanel_data']) && $layout_defs[$module]["subpanel_setup"][$key]['get_subpanel_data'] == $relName && isset($fields[$relName]) && $fields[$relName]['type'] != 'link') {
                                $fileContents = file_get_contents($filePath);
                                $out = preg_replace('/[\'"]get_subpanel_data[\'"]\\s*=>\\s*[\'"]' . $relName . '[\'"],/s', "'get_subpanel_data' => '{$def["join_key_lhs"]}',", $fileContents);
                                file_put_contents($filePath, $out);
                            }
                        }
                    }
                }
            }
        }
    }
    // Phase 2: Module builder has been incorrectly adding the id
    // field attributes to created relationships
    foreach (glob('custom/Extension/modules/*/Ext/Vardefs/*.php') as $fileToFix) {
        // continue to the next if it's not an existing file or it's a directory
        if (!file_exists($fileToFix) || is_dir($fileToFix)) {
            continue;
        }
        $filename = basename($fileToFix);
        $dictionary = array();
        require $fileToFix;
        $tmp = array_keys($dictionary);
        if (count($tmp) < 1) {
            // Empty dictionary
            continue;
        }
        $dictKey = $tmp[0];
        if (!isset($dictionary[$dictKey]['fields'])) {
            // Not modifying any fields, this isn't a relationship
            continue;
        }
        $isBadRelate = false;
        $idName = '';
        $linkField = null;
        $relateField = null;
        foreach ($dictionary[$dictKey]['fields'] as $fieldName => $field) {
            if (isset($field['id_name']) && $fieldName != $field['id_name']) {
                if (isset($field['type']) && $field['type'] == 'link') {
                    // This looks promising
                    if (isset($dictionary[$dictKey]['fields'][$field['id_name']])) {
                        $idField = $dictionary[$dictKey]['fields'][$field['id_name']];
                        if (isset($idField['type']) && $idField['type'] == 'link') {
                            // This looks like a winner
                            $idName = $field['id_name'];
                            $isBadRelate = true;
                            $linkField = $field;
                        }
                    }
                }
                if (isset($field['type']) && $field['type'] == 'relate') {
                    $relateField = $field;
                }
            }
        }
        if (!$isBadRelate) {
            continue;
        }
        $depRels = new DeployedRelationships($dictKey);
        $relObj = $depRels->get($linkField['relationship']);
        if (!$relObj) {
            // The system doesn't know about the relationship object.
            $linkMetadataLocation = 'custom/metadata/' . $linkField['relationship'] . 'MetaData.php';
            if (file_exists($linkMetadataLocation)) {
                require $linkMetadataLocation;
                $linkDef = $dictionary[$linkField['relationship']];
                $relObj = RelationshipFactory::newRelationship($linkDef);
            }
        }
        $newIdField = array('name' => $idName, 'type' => 'id', 'source' => 'non-db', 'vname' => $idField['vname'], 'id_name' => $idName, 'link' => $relateField['link'], 'table' => $relateField['table'], 'module' => $relateField['module'], 'rname' => 'id', 'reportable' => false, 'massupdate' => false, 'duplicate_merge' => 'disabled', 'hideacl' => true);
        if ($relObj && $relObj->getLhsModule() == $relObj->getRhsModule()) {
            $selfReferencing = true;
        } else {
            $selfReferencing = false;
        }
        if ($selfReferencing) {
            $newLinkField = array('name' => $relateField['link'] . '_right', 'type' => 'link', 'relationship' => $linkField['relationship'], 'source' => 'non-db', 'vname' => $idField['vname'], 'id_name' => $relObj->getJoinKeyRHS(), 'side' => 'right', 'link-type' => 'many');
        }
        $replaceString = '$dictionary["' . $dictKey . '"]["fields"]["' . $idName . '"]=' . var_export_helper($newIdField) . ";\n";
        if ($selfReferencing) {
            $replaceString .= '$dictionary["' . $dictKey . '"]["fields"]["' . $newLinkField['name'] . '"]=' . var_export_helper($newLinkField) . ";\n";
        }
        $fileContents = file_get_contents($fileToFix);
        $out = preg_replace('/\\$dictionary[\\w"\'\\[\\]]*?' . $idName . '["\'\\[\\]]*?\\s*?=\\s*?array\\s*?\\(.*?\\);/s', $replaceString, $fileContents);
        if ($selfReferencing) {
            $out = preg_replace('/\\$dictionary[\\w"\'\\[\\]]*?' . $relateField['name'] . '["\'\\[\\]]*?\\s*?=\\s*?array\\s*?\\(.*?\\);/s', '$dictionary["' . $dictKey . '"]["fields"]["' . $relateField['name'] . '"]=' . var_export_helper($relateField) . ";\n", $out);
        }
        file_put_contents($fileToFix, $out);
        if ($selfReferencing) {
            // Now to fix bad layouts in self-linking relationships
            // Go to the Layoutdefs path
            $layoutPath = dirname(dirname($fileToFix)) . '/Layoutdefs';
            foreach (glob($layoutPath . '/*.php') as $layoutToCheck) {
                // See if they match the id I just changed.
                $layout_defs = array();
                include $layoutToCheck;
                if (isset($layout_defs[$dictKey]['subpanel_setup'][$newIdField['name']])) {
                    $newLayout[$dictKey]['subpanel_setup'][$relateField['link']] = $layout_defs[$dictKey]['subpanel_setup'][$newIdField['name']];
                    $newLayout[$dictKey]['subpanel_setup'][$relateField['link']]['get_subpanel_data'] = $newLinkField['relationship'] . '_right';
                    write_array_to_file('layout_defs', $newLayout, $layoutToCheck);
                }
            }
        }
    }
}
Ejemplo n.º 9
0
 /**
  * Convert a piece of a subpanel layoutdef to the new style
  * @param array $layoutdef old style layout
  * @return array new style layout for this piece
  */
 public function fromLegacySubpanelLayout(array $layoutdef)
 {
     $viewdefs = array('layout' => 'subpanel');
     // we aren't upgrading collections
     if (!empty($layoutdef['collection_list'])) {
         return $viewdefs;
     }
     foreach ($layoutdef as $key => $value) {
         if ($key == 'override_subpanel_name') {
             $subpanelFileName = $value;
             if (substr_count($value, '_') > 1 && stristr($value, 'subpanel')) {
                 $parts = explode('_subpanel_', $value);
                 $beanNameParts = explode('_', $parts[0]);
                 $subPanelBeanName = '';
                 foreach ($beanNameParts as $part) {
                     $subPanelBeanName .= ucwords($part);
                 }
                 // case is not the actually object name, it's aCase
                 if ($subPanelBeanName == 'Case') {
                     $subPanelBeanName = 'aCase';
                 }
                 $focus = BeanFactory::newBeanByName($subPanelBeanName);
                 if ($focus) {
                     $field = $focus->getFieldDefinition($parts[1]);
                     if ($field && $field['type'] == 'link') {
                         // since we have a valid link, we need to test the relationship to see if it's custom relationship
                         $relationships = new DeployedRelationships($focus->module_name);
                         $relationship = $relationships->get($parts[1]);
                         $relDef = array();
                         if ($relationship) {
                             $relDef = $relationship->getDefinition();
                         }
                         if (!empty($relDef['is_custom']) && !empty($relDef['from_studio']) && (!empty($relDef['name']) || !empty($relDef['relationship_name']))) {
                             $name = !empty($relDef['name']) ? $relDef['name'] : $relDef['relationship_name'];
                             $subpanelFileName = "For{$name}";
                         } else {
                             $subpanelFileName = "For{$focus->module_name}";
                         }
                     } else {
                         $subpanelFileName = "For{$focus->module_name}";
                     }
                 }
             }
             $viewdefs['override_subpanel_list_view'] = array('view' => $this->fromLegacySubpanelName($subpanelFileName), 'link' => isset($layoutdef['get_subpanel_data']) ? $layoutdef['get_subpanel_data'] : '');
         } elseif ($key == 'title_key') {
             $viewdefs['label'] = $value;
         } elseif ($key == 'get_subpanel_data') {
             $viewdefs['context']['link'] = $value;
         }
     }
     return $viewdefs;
 }