2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Object;
|
|
|
|
use SilverStripe\Dev\Debug;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* This class represents "transformations" of a form - such as making it printable or making it readonly.
|
|
|
|
* The idea is that sometimes you will want to make your own such transformations, and you shouldn't have
|
|
|
|
* to edit the underlying code to support this.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* The first step in creating a transformation is subclassing FormTransformation. After that, you have two
|
|
|
|
* ways of defining specific functionality:
|
|
|
|
* - Define performMyTransformation() methods on each applicable FormField() object.
|
|
|
|
* - Define transformFieldType($field) methods on your subclass of FormTransformation.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* To actually perform the transformation, call $form->transform(new MyTransformation());
|
|
|
|
*/
|
|
|
|
class FormTransformation extends Object {
|
2012-09-19 12:07:39 +02:00
|
|
|
public function transform(FormField $field) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Look for a performXXTransformation() method on the field itself.
|
|
|
|
// performReadonlyTransformation() is a pretty commonly applied method.
|
|
|
|
// Otherwise, look for a transformXXXField() method on this object.
|
|
|
|
// This is more commonly done in custom transformations
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// We iterate through each array simultaneously, looking at [0] of both, then [1] of both.
|
|
|
|
// This provides a more natural failover scheme.
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$transNames = array_reverse(array_map(
|
|
|
|
function($name) { return ClassInfo::shortName($name); },
|
|
|
|
array_values(ClassInfo::ancestry($this->class))
|
|
|
|
));
|
|
|
|
$fieldClasses = array_reverse(array_map(
|
|
|
|
function($name) { return ClassInfo::shortName($name); },
|
|
|
|
array_values(ClassInfo::ancestry($field->class))
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$len = max(sizeof($transNames), sizeof($fieldClasses));
|
2016-08-19 00:51:35 +02:00
|
|
|
for($i=0; $i<$len; $i++) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// This is lets fieldClasses be longer than transNames
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!empty($transNames[$i])) {
|
|
|
|
$funcName = 'perform' . $transNames[$i];
|
|
|
|
if($field->hasMethod($funcName)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
//echo "<li>$field->class used $funcName";
|
|
|
|
return $field->$funcName($this);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
// And this one does the reverse.
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!empty($fieldClasses[$i])) {
|
|
|
|
$funcName = 'transform' . $fieldClasses[$i];
|
|
|
|
if($this->hasMethod($funcName)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
//echo "<li>$field->class used $funcName";
|
|
|
|
return $this->$funcName($field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
throw new \BadMethodCallException("FormTransformation:: Can't perform '$this->class' on '$field->class'");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|