key; } public function getComponentName() { if(!$name = $this->getName()) { $name = singleton($this->controllerClass)->getReverseAssociation($this->getComponentClass()); } return $name; } public function setControllerClass($class) { $this->controllerClass = $class; } public function getControllerClass() { return $this->controllerClass; } public function getInterface() { $i = $this->get('Interface'); if(is_string($i)) { user_error("Invalid value for Interface: $i. Did you mean Type: $i?",E_USER_ERROR); return; } return $i; } public function getLabel() { if($this->get('Label')) { return $this->get('Label'); } return $this->getComponentName(); } public function getTab() { $fullPage = (is_subclass_of($this->controllerClass,"SiteTree")); $t = $this->get('Tab'); if(!$t && !$fullPage) { return false; } if(!$fullPage) { $tab = ($t) ? "Root.".$t : "Root.Main"; } else { $tab = ($t) ? $t : "Root.Content.{$this->getComponentName()}"; if(false === stristr($tab,"Root.")) { $tab = "Root.Content.{$tab}"; } } return $tab; } /** * Given a class configuration, figure out the best name for a relation to a given class. * If a name has been specified in the config, use that. * * @param string $component The related class * @param BedrockSetting $config The config for the parent class * @return string */ public function generateRelationName() { if($this->getName()) return BedrockUtil::proper_form($this->getName()); if($type = $this->getType()) { switch($type) { case "many": case "manymany": return (class_exists($this->getComponentClass())) ? BedrockUtil::proper_form($this->getComponentClass()).'s' : $this->getComponentClass(); break; case "one": return $this->getComponentClass(); break; } } } /** * Update the relation keys for the extra statics array in the decorator, based * on the "Type" property defined for this object * * @param array $statics The extra statics to add to the decorator. * @param string $class The class that owns the component */ public function updateRelations(&$statics, $class) { if($type = $this->getType()) { $name = $this->generateRelationName(); if($type == "many") { $statics['has_many'][$name] = $this->getComponentClass(); Bedrock::message("Found has_many relation $name on $class","good"); // Reciprocate the has_one Object::add_static_var($this->getComponentClass(),'has_one',array($class => $class)); } elseif($type == "manymany") { $statics['many_many'][$name] = $this->getComponentClass(); // Reciprocate the belongs_many_many // if($belongs = Object::get_static($this->getComponentClass(),'belongs_many_many')) { // if(!array_key_exists($class, $belongs)) { Object::add_static_var($this->getComponentClass(),'belongs_many_many',array($class => $class)); // echo "belongs_many_many $class"; // } // } Bedrock::message("Found many_many relation $name on $class","good"); } elseif($type == "one") { $statics['has_one'][$name] = $this->getComponentClass(); Bedrock::message("Found has_one relation $name on $class","good"); } } else user_error("Tried to add component $component, but no type is set.",E_USER_ERROR); } public function buildInterface($controller) { if($interface = $this->getInterface()) { $interface->setController($controller); return $interface->build($this); } } }