/**
  * The constructor
  * @param string $subpanelName
  * @param string $loadedModule - Accounts
  * @param string $client - base
  */
 public function __construct($subpanelName, $loadedModule, $client = 'base')
 {
     $GLOBALS['log']->debug(get_class($this) . "->__construct({$subpanelName} , {$loadedModule})");
     $this->mdc = new MetaDataConverter();
     $this->loadedModule = $loadedModule;
     $this->setViewClient($client);
     $linkName = $this->linkName = $this->getLinkName($subpanelName, $loadedModule);
     // get the link and the related module name as the module we need the subpanel from
     $bean = BeanFactory::getBean($loadedModule);
     // Get the linkdef, but make sure to tell VardefManager to use name instead by passing true
     $linkDef = VardefManager::getLinkFieldForRelationship($bean->module_dir, $bean->object_name, $subpanelName, true);
     if ($bean->load_relationship($linkName)) {
         $link = $bean->{$linkName};
     } else {
         $link = new Link2($linkName, $bean);
     }
     $this->_moduleName = $link->getRelatedModuleName();
     $this->bean = BeanFactory::getBean($this->_moduleName);
     $subpanelFixed = true;
     if (empty($this->bean)) {
         $subpanelFixed = $this->fixUpSubpanel();
     }
     if (empty($linkDef['name']) && (!$subpanelFixed && isModuleBWC($this->loadedModule))) {
         $GLOBALS['log']->error("Cannot find a link for {$subpanelName} on {$loadedModule}");
         return true;
     }
     // Handle validation up front that will throw exceptions
     if (empty($this->bean) && !$subpanelFixed) {
         throw new Exception("No valid parent bean found for {$this->linkName} on {$this->loadedModule}");
     }
     $this->setUpSubpanelViewDefFileInfo();
     include $this->loadedSubpanelFileName;
     // Prepare to load the history file. This will be available in cases when
     // a layout is restored.
     $this->historyPathname = 'custom/history/modules/' . $this->_moduleName . '/clients/' . $this->getViewClient() . '/views/' . $this->sidecarSubpanelName . '/' . self::HISTORYFILENAME;
     $this->_history = new History($this->historyPathname);
     if (file_exists($this->historyPathname)) {
         // load in the subpanelDefOverride from the history file
         $GLOBALS['log']->debug(get_class($this) . ": loading from history");
         require $this->historyPathname;
     }
     $this->_viewdefs = !empty($viewdefs) ? $this->getNewViewDefs($viewdefs) : array();
     $this->_fielddefs = $this->bean->field_defs;
     $this->_mergeFielddefs($this->_fielddefs, $this->_viewdefs);
     $this->_language = '';
     // don't attempt to access the template_instance property if our subpanel represents a collection, as it won't be there - the sub-sub-panels get this value instead
     if (isset($this->_viewdefs['type']) && $this->_viewdefs['type'] != 'collection') {
         $this->_language = $this->bean->module_dir;
     }
     // Make sure the paneldefs are proper if there are any
     $this->_paneldefs = isset($this->_viewdefs['panels']) ? $this->_viewdefs['panels'] : array();
 }