<?php
/*********************************************************************************
 * SugarCRM is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004 - 2009 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
 ********************************************************************************/
require_once ('modules/ModuleBuilder/MB/AjaxCompose.php') ;
require_once ('modules/ModuleBuilder/MB/ModuleBuilder.php') ;
require_once ('modules/ModuleBuilder/Module/StudioModule.php') ;
require_once ('modules/ModuleBuilder/Module/StudioBrowser.php') ;
require_once ('modules/ModuleBuilder/parsers/constants.php') ;
require_once 'modules/ModuleBuilder/parsers/relationships/RelationshipFactory.php' ;

class ViewRelationship extends SugarView
{

    function ViewRelationship ()
    {
        parent::SugarView () ;
    }

    function overrideDefinitionFromPOST ($definition)
    {
        require_once 'modules/ModuleBuilder/parsers/relationships/AbstractRelationship.php' ;
        foreach ( AbstractRelationship::$definitionKeys as $key )
        {
            if (! empty ( $_POST [ $key ] ))
            {
                $definition [ $key ] = $_POST [ $key ] ;
            }
        }
        return $definition ;
    }

    function display ()
    {
        $this->smarty = new Sugar_Smarty ( ) ;
        $ac = new AjaxCompose ( ) ;
        $this->fromModuleBuilder = isset ( $_REQUEST [ 'MB' ] ) || (!empty ( $_REQUEST [ 'view_package' ] ) && $_REQUEST [ 'view_package' ] != 'studio') ;
        $this->smarty->assign ( 'fromModuleBuilder', $this->fromModuleBuilder ) ;
        
        if (!$this->fromModuleBuilder)
        {
            $module = new StudioModule ( $_REQUEST [ 'view_module' ] ) ;
            $moduleName = $_REQUEST [ 'view_module' ] ;
            $fields = $module->fields ;
            require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
            $relatableModules = DeployedRelationships::findRelatableModules () ;
        } else
        {
            $mb = new ModuleBuilder ( ) ;
			$mb->getPackages();
			//display the latest module name rather than what is in or not in the loaded app_list_strings.
			$mb->getPackage($_REQUEST ['view_package'])->loadModuleTitles();
            $module = $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
            $moduleName = empty($module->key_name) ? $module->getModuleName() : $module->key_name;
            $this->smarty->assign ( 'view_package', $_REQUEST [ 'view_package' ] ) ;
            $mbvardefs = $module->getVardefs () ;
            $fields = $mbvardefs [ 'fields' ] ;
            require_once 'modules/ModuleBuilder/parsers/relationships/UndeployedRelationships.php' ;
            $relatableModules = UndeployedRelationships::findRelatableModules () ;
        }
        
        ksort( $relatableModules ) ;
        $lhs_subpanels = $module->getProvidedSubpanels () ;
        // Fix to re-add sorting of the subpanel names so that the 'default' subpanel always appears first in the list. This assumes that subpanels are usually named ForXYZ which is the case currently, and hence 'default' will be sorted first. If this assumption is incorrect, then a better solution would be to remove 'default' from the subpanel list, then sort, and finally array_unshift it back on.
        natcasesort($lhs_subpanels);
                
        $cardinality = array ( MB_ONETOONE => translate ( 'LBL_ONETOONE' ) , MB_ONETOMANY => translate ( 'LBL_ONETOMANY' ) , MB_MANYTOMANY => translate ( 'LBL_MANYTOMANY' ) ) ;
        $relationships = $module->getRelationships () ;
        
        // if a description for this relationship already exists, then load it so it can be modified
        if (! empty ( $_REQUEST [ 'relationship_name' ] ))
        {
            $relationship = $relationships->get ( $_REQUEST [ 'relationship_name' ] ) ;
            $definition = $relationship->getDefinition();
        } else
        {
            $definition = array ( ) ;
            $firstModuleDefinition = each ( $relatableModules ) ;
            $definition [ 'rhs_module' ] = $firstModuleDefinition [ 'key' ] ;
            $definition [ 'lhs_module' ] = $moduleName ;
            $definition [ 'lhs_label' ] = translate ($moduleName) ;
            $definition [ 'relationship_type' ] = MB_MANYTOMANY ;
        }
        // load the relationship from post - required as we can call view.relationship.php from Ajax when changing the rhs_module for example           
        $definition = $this->overrideDefinitionFromPOST ( $definition ) ;
        if ($definition [ 'relationship_type' ] == MB_MANYTOMANY && empty($definition ['rhs_label']))
        {
        	$definition ['rhs_label'] = translate ($definition [ 'rhs_module' ]);
        }
        $relationship = RelationshipFactory::newRelationship ( $definition ) ;
        
        $rhs_subpanels = $relatableModules [ $relationship->rhs_module ] ;
        // Fix to re-add sorting of the subpanel names so that the 'default' subpanel always appears first in the list. This assumes that subpanels are usually named ForXYZ which is the case currently, and hence 'default' will be sorted first. If this assumption is incorrect, then a better solution would be to remove 'default' from the subpanel list, then sort, and finally array_unshift it back on.
        natcasesort($rhs_subpanels);
        
        if (empty ( $_REQUEST [ 'relationship_name' ] ))
        {
            // tidy up the options for the view based on the modules participating in the relationship and the cardinality
            // some modules (e.g., Knowledge Base/KBDocuments) lack subpanels. That means they can't be the lhs of a 1-many or many-many, or the rhs of a many-many for example          

            // fix up the available cardinality options
            $relationship_type = $relationship->getType () ;
            if (count ( $lhs_subpanels ) == 0 || count ( $rhs_subpanels ) == 0)
            {
                unset ( $cardinality [ MB_MANYTOMANY ] ) ;
            }
            if (count ( $rhs_subpanels ) == 0)
            {
                unset ( $cardinality [ MB_ONETOMANY ] ) ;
            }

            if (isset ($definition['rhs_module']) && $definition['rhs_module'] == 'Activities') {
            	unset ( $cardinality [ MB_MANYTOMANY ] ) ;
            	unset ( $cardinality [ MB_ONETOONE ] ) ;
            }
            
            //Bug 23139, Campaigns module current cannot display custom subpanels, so we need to ban it from any
            //relationships that would require a new subpanel to be shown in Campaigns.
        	if (isset ($definition['lhs_module']) && $definition['lhs_module'] == 'Campaigns') {
            	unset ( $cardinality [ MB_MANYTOMANY ] ) ;
            	unset ( $cardinality [ MB_ONETOMANY ] ) ;
            }
        	if (isset ($definition['rhs_module']) && $definition['rhs_module'] == 'Campaigns' && isset($cardinality [ MB_MANYTOMANY ])) {
            	unset ( $cardinality [ MB_MANYTOMANY ] ) ;
            }
            
            if (! isset($cardinality[$relationship->getType()]))
            {
                end ($cardinality) ;
                $definition [ 'relationship_type' ] = key ( $cardinality ) ;
                $relationship = RelationshipFactory::newRelationship ( $definition ) ;
            }
        }
        
        //Remove Activities if one-to-many is not availible
    	if (!isset($cardinality [ MB_ONETOMANY ]) && isset ($relatableModules['Activities'])) {
        	unset ($relatableModules['Activities']);
        }
        
        
        // now enforce the relationship_only requirement - that is, only construct the underlying relationship and link fields, and not the UI, if the subpanel code will have troubles displaying the UI                
        $relationships->enforceRelationshipOnly ( $relationship ) ;
        
        $this->smarty->assign ( 'view_module', $_REQUEST['view_module'] ) ;
        $this->smarty->assign ( 'rel', $relationship->getDefinition () ) ;
        $this->smarty->assign ( 'mod_strings', $GLOBALS [ 'mod_strings' ] ) ;
        $this->smarty->assign ( 'module_key', $relationship->lhs_module ) ;
        $this->smarty->assign ( 'cardinality', array_keys ( $cardinality ) ) ;
        $this->smarty->assign ( 'translated_cardinality', $cardinality ) ;
        $this->smarty->assign ( 'selected_cardinality', translate ( $relationship->getType () ) ) ;
        
        $relatable = array ( ) ;
        foreach ( $relatableModules as $name => $dummy )
        {
            $relatable [ $name ] = translate ( $name ) ;
        }
        unset($relatable [ 'KBDocuments' ] ) ;
        natcasesort ( $relatable ) ;
        $this->smarty->assign ( 'relatable', array_keys ( $relatable ) ) ;
        $this->smarty->assign ( 'translated_relatable', $relatable ) ;
        $this->smarty->assign ( 'rhspanels', $rhs_subpanels ) ;
        $this->smarty->assign ( 'lhspanels', $lhs_subpanels ) ;
        
        switch ( $relationship->relationship_type)
        {
            case MB_ONETOONE :
                break ;
            
            case MB_ONETOMANY :
                if (empty ( $relationship->relationship_column_name ))
                {
                    $validRoleColumnFields = array ( ) ;
                    foreach ( $fields as $field )
                    {
                        $validRoleColumnFields [] = $field ;
                    }
                    $this->smarty->assign ( 'relationship_role_column_enum', $validRoleColumnFields ) ;
                }
                if (! empty ( $relationship->relationship_role_column_value ))
                    $this->smarty->assign ( 'relationship_role_column_value', $relationship->relationship_role_column_value ) ;
                break ;
            case MB_MANYTOMANY :
                if (! empty ( $relationship->relationship_role_column_value ))
                    $this->smarty->assign ( 'relationship_role_column_value', $relationship->relationship_role_column_value ) ;
                break ;
        }
        
        //see if we use the new system
        if (isset ( $_REQUEST [ 'json' ] ) && $_REQUEST [ 'json' ] == 'false')
        {
            echo $this->smarty->fetch ( 'modules/ModuleBuilder/tpls/studioRelationship.tpl' ) ;
        } else
        {
            $ac->addSection ( 'east', $module->name . ' ' . $GLOBALS [ 'mod_strings' ] [ 'LBL_RELATIONSHIPS' ], $this->smarty->fetch ( 'modules/ModuleBuilder/tpls/studioRelationship.tpl' ) ) ;
            echo $ac->getJavascript () ;
        }
    }

}
