27/04/2013 1:48am

PHP | Problem -


<?php


/**
* Provides shortcuts to adding fields to the main tab.
*
* Usage:
* <code>
* $fields->text("MyField");
* // Result: $fields->addFieldToTab("Root.Main", TextField::create("MyField", "My Field"));
*
* $fields->dropdown("MyField", "My label", $source);
* // Result: $fields->addFieldToTab("Root.Main", DropdownField::create("MyField", "My label", $source));
*
* </code>
* Method names are defined by the FormField class, without the word "Field", with a lowercase first letter.
* e.g.
* CurrencyField -> currency()
* TreeDropdownField -> treeDropdown()
*
* @todo Allow specification of tab other than Root.Main
*
* @author Uncle Cheese <unclecheese@leftandmain.com>
* @package FieldListDecorator
*
*/
class FieldListDecorator extends Extension {

public function __call($method, $args) {
$formFieldClass = ucfirst($method)."Field";
if(is_subclass_of($formFieldClass, "FormField")) {
if(!isset($args[0])) {
user_error("FieldList::{$method} -- Missing argument 1 for field name", E_ERROR);
}
$label = isset($args[1])) ? $args[1] : FormField::name_to_label($args[0]);
$value = isset($args[2])) ? $args[2] : null;
$this->owner->addFieldToTab("Root.Main",Object::create($formFieldClass, $args[0], $label, $value));
}
else {
user_error("FieldList::{$method} -- $formFieldClass is not a FormField.",E_ERROR);
}
}



public function allMethodNames() {
$methods = array ();
foreach(SS_ClassLoader::instance()->getManifest()->getDescendantsOf("FormField") as $field) {
$methods[] = lcfirst(str_replace("Field","",$field));
}

return $methods;
}


}


Post Comment