From d6b0637e44e4096e55a6df224674ecb5e70b3cbc Mon Sep 17 00:00:00 2001 From: helpfulrobot Date: Fri, 1 Jan 2016 05:50:47 +1300 Subject: [PATCH] Converted to PSR-2 --- code/AlternativeField.php | 278 ++++++++++++++++++---------------- code/AlternativeFormField.php | 200 ++++++++++++------------ 2 files changed, 254 insertions(+), 224 deletions(-) diff --git a/code/AlternativeField.php b/code/AlternativeField.php index 25aa710..0ff0ce6 100644 --- a/code/AlternativeField.php +++ b/code/AlternativeField.php @@ -1,130 +1,154 @@ 'Varchar(10)', - 'AlternativeValue' => 'Varchar(30)' - ); - - function setValue($value, $record = null, $markChanged = true) { - - if($value instanceof AlternativeField && $value->exists()) { - $this->setSelectedValue($value->getSelectedValue(), $markChanged); - $this->setAlternativeValue($value->getAlternativeValue(), $markChanged); - if($markChanged) $this->isChanged = true; - } else if ($record && (isset($record[$this->name . 'SelectedValue']) || isset($record[$this->name . 'AlternativeValue']))) { - //var_dump($record[$this->name . 'SelectedValue']); - //exit(); - $this->setSelectedValue((isset($record[$this->name . 'SelectedValue'])) ? $record[$this->name . 'SelectedValue'] : null, $markChanged); - $this->setAlternativeValue((isset($record[$this->name . 'AlternativeValue'])) ? $record[$this->name . 'AlternativeValue'] : null, $markChanged); - if($markChanged) $this->isChanged = true; - } else if(is_array($value)) { - if(array_key_exists('SelectedValue', $value)) { - $this->setSelectedValue($value['SelectedValue'], $markChanged); - } - if(array_key_exists('AlternativeValue', $value)) { - $this->setAlternativeValue($value['AlternativeValue'], $markChanged); - } - if($markChanged) $this->isChanged = true; - } - - // if not using the default then set alternative to null - if($this->getSelectedValue() != 'Other') { - $this->setAlternativeValue(null); - } - - } - - function requireField(){ - $fields = $this->compositeDatabaseFields(); - if($fields) foreach($fields as $name => $type){ - DB::requireField($this->tableName, $this->name.$name, $type); - } - } - - function writeToManipulation(&$manipulation) { - if($this->getSelectedValue()) { - $manipulation['fields'][$this->name.'SelectedValue'] = $this->prepValueForDB($this->getSelectedValue()); - } else { - $manipulation['fields'][$this->name.'SelectedValue'] = DBField::create_field('Varchar', $this->getSelectedValue())->nullValue(); - } - - if($this->getAlternativeValue()) { - $manipulation['fields'][$this->name.'AlternativeValue'] = $this->prepValueForDB($this->getAlternativeValue()); - } else { - $manipulation['fields'][$this->name.'AlternativeValue'] = DBField::create_field('Varchar', $this->getAlternativeValue())->nullValue(); - } - } - - function addToQuery(&$query) { - parent::addToQuery($query); - } - - function compositeDatabaseFields(){ - return static::$composite_db; - } - - function isChanged(){ - return $this->isChanged; - } - - function exists(){ - return ($this->selectedValue !== null || $this->alternativeValue !== null); - } - - public function getSelectedValue() { - return $this->selectedValue; - } - - public function setSelectedValue($selectedValue, $markChanged = true) { - - $this->isChanged = $markChanged; - $this->selectedValue = $selectedValue; - } - - public function getAlternativeValue() { - return $this->alternativeValue; - } - - public function setAlternativeValue($alternativeValue, $markChanged = true) { - $this->isChanged = $markChanged; - $this->alternativeValue = $alternativeValue; - } - - public function scaffoldFormField($title = null) { - $field = new AlternativeFormField($this->name); - return $field; - } - - public function actualValue() { - if($this->selectedValue == 'Other') return $this->alternativeValue; - else if($this->selectedValue) return $this->selectedValue; - else return null; - } - - public function __toString() { - return $this->getActualValue(); - } - - function forTemplate() { - return $this->getActualValue(); - } - +class AlternativeField extends DBField implements CompositeDBField +{ + + /** + * @var string The selected value for this field + */ + protected $selectedValue; + + + /** + * @var string The alternative value for this field + */ + protected $alternativeValue; + + /** + * @var boolean Is this record changed or not? + */ + protected $isChanged = false; + + public static $composite_db = array( + 'SelectedValue' => 'Varchar(10)', + 'AlternativeValue' => 'Varchar(30)' + ); + + public function setValue($value, $record = null, $markChanged = true) + { + if ($value instanceof AlternativeField && $value->exists()) { + $this->setSelectedValue($value->getSelectedValue(), $markChanged); + $this->setAlternativeValue($value->getAlternativeValue(), $markChanged); + if ($markChanged) { + $this->isChanged = true; + } + } elseif ($record && (isset($record[$this->name . 'SelectedValue']) || isset($record[$this->name . 'AlternativeValue']))) { + //var_dump($record[$this->name . 'SelectedValue']); + //exit(); + $this->setSelectedValue((isset($record[$this->name . 'SelectedValue'])) ? $record[$this->name . 'SelectedValue'] : null, $markChanged); + $this->setAlternativeValue((isset($record[$this->name . 'AlternativeValue'])) ? $record[$this->name . 'AlternativeValue'] : null, $markChanged); + if ($markChanged) { + $this->isChanged = true; + } + } elseif (is_array($value)) { + if (array_key_exists('SelectedValue', $value)) { + $this->setSelectedValue($value['SelectedValue'], $markChanged); + } + if (array_key_exists('AlternativeValue', $value)) { + $this->setAlternativeValue($value['AlternativeValue'], $markChanged); + } + if ($markChanged) { + $this->isChanged = true; + } + } + + // if not using the default then set alternative to null + if ($this->getSelectedValue() != 'Other') { + $this->setAlternativeValue(null); + } + } + + public function requireField() + { + $fields = $this->compositeDatabaseFields(); + if ($fields) { + foreach ($fields as $name => $type) { + DB::requireField($this->tableName, $this->name.$name, $type); + } + } + } + + public function writeToManipulation(&$manipulation) + { + if ($this->getSelectedValue()) { + $manipulation['fields'][$this->name.'SelectedValue'] = $this->prepValueForDB($this->getSelectedValue()); + } else { + $manipulation['fields'][$this->name.'SelectedValue'] = DBField::create_field('Varchar', $this->getSelectedValue())->nullValue(); + } + + if ($this->getAlternativeValue()) { + $manipulation['fields'][$this->name.'AlternativeValue'] = $this->prepValueForDB($this->getAlternativeValue()); + } else { + $manipulation['fields'][$this->name.'AlternativeValue'] = DBField::create_field('Varchar', $this->getAlternativeValue())->nullValue(); + } + } + + public function addToQuery(&$query) + { + parent::addToQuery($query); + } + + public function compositeDatabaseFields() + { + return static::$composite_db; + } + + public function isChanged() + { + return $this->isChanged; + } + + public function exists() + { + return ($this->selectedValue !== null || $this->alternativeValue !== null); + } + + public function getSelectedValue() + { + return $this->selectedValue; + } + + public function setSelectedValue($selectedValue, $markChanged = true) + { + $this->isChanged = $markChanged; + $this->selectedValue = $selectedValue; + } + + public function getAlternativeValue() + { + return $this->alternativeValue; + } + + public function setAlternativeValue($alternativeValue, $markChanged = true) + { + $this->isChanged = $markChanged; + $this->alternativeValue = $alternativeValue; + } + + public function scaffoldFormField($title = null) + { + $field = new AlternativeFormField($this->name); + return $field; + } + + public function actualValue() + { + if ($this->selectedValue == 'Other') { + return $this->alternativeValue; + } elseif ($this->selectedValue) { + return $this->selectedValue; + } else { + return null; + } + } + + public function __toString() + { + return $this->getActualValue(); + } + + public function forTemplate() + { + return $this->getActualValue(); + } } diff --git a/code/AlternativeFormField.php b/code/AlternativeFormField.php index 95cbeaf..14258eb 100644 --- a/code/AlternativeFormField.php +++ b/code/AlternativeFormField.php @@ -1,99 +1,105 @@ fieldSelectedValue = DropdownField::create("{$name}[SelectedValue]", '', $this->getOptions(), '', $form); - if($this->getEmptyString()) { - $this->fieldSelectedValue->setEmptyString($this->getEmptyString()); - } - $this->fieldAlternativeValue = new TextField("{$name}[AlternativeValue]", $this->getAlternativeFieldTitle(), '', 30, $form); - $this->fieldSelectedValue->setForm($form); - parent::__construct($name, $title, $value, $form); - } - - public function getOptions() { - $options = $this->config()->get('options'); - return array_combine($options, $options); - } - - public function getEmptyString() { - return $this->config()->get('empty_string'); - } - - public function getAlternativeFieldTitle() { - return $this->config()->get('alternative_field_title'); - } - - function setForm($form) { - $this->fieldSelectedValue->setForm($form); - $this->fieldAlternativeValue->setForm($form); - return parent::setForm($form); - } - - function setName($name){ - $this->fieldSelectedValue->setName("{$name}[SelectedValue]"); - $this->fieldAlternativeValue->setName("{$name}[AlternativeValue]"); - return parent::setName($name); - } - - function setValue($val) { - $this->value = $val; - if(is_array($val)) { - $this->fieldSelectedValue->setValue($val['SelectedValue']); - $this->fieldAlternativeValue->setValue($val['AlternativeValue']); - } elseif($val instanceof AlternativeField) { - $this->fieldSelectedValue->setValue($val->getSelectedValue()); - $this->fieldAlternativeValue->setValue($val->getAlternativeValue()); - } - } - - /** - * @return string - */ - function Field($properties = array()) { - Requirements::javascript(self::$module_dir . '/js/AlternativeFormField.js'); - Requirements::css(self::$module_dir . '/css/AlternativeFormField.css'); - return "
" . - "
" . - $this->fieldSelectedValue->SmallFieldHolder() . - "
" . - "
" . - $this->fieldAlternativeValue->SmallFieldHolder() . - "
" . - "
"; - } - - /** - * SaveInto checks if set-methods are available and use them instead of setting the values directly. saveInto - * initiates a new LinkField class object to pass through the values to the setter method. - */ - function saveInto(DataObjectInterface $dataObject) { - - $fieldName = $this->name; - if($dataObject->hasMethod("set$fieldName")) { - $dataObject->$fieldName = DBField::create('AlternativeField', array( - "SelectedValue" => $this->fieldSelectedValue->Value(), - "AlternativeValue" => $this->fieldAlternativeValue->Value() - )); - } else { - $dataObject->$fieldName->setSelectedValue($this->fieldSelectedValue->Value()); - $dataObject->$fieldName->setAlternativeValue($this->fieldAlternativeValue->Value()); - } - } - - - -} \ No newline at end of file +class AlternativeFormField extends FormField +{ + + public static $module_dir = ''; // This is initially set in _config.php + + private static $options = array(); + + private static $empty_string = '-- Select --'; + + private static $alternative_field_title = 'Please state:'; + + protected $fieldSelectedValue = null; + + protected $fieldAlternativeValue = null; + + public function __construct($name, $title = null, $value = null, $form = null) + { + $this->fieldSelectedValue = DropdownField::create("{$name}[SelectedValue]", '', $this->getOptions(), '', $form); + if ($this->getEmptyString()) { + $this->fieldSelectedValue->setEmptyString($this->getEmptyString()); + } + $this->fieldAlternativeValue = new TextField("{$name}[AlternativeValue]", $this->getAlternativeFieldTitle(), '', 30, $form); + $this->fieldSelectedValue->setForm($form); + parent::__construct($name, $title, $value, $form); + } + + public function getOptions() + { + $options = $this->config()->get('options'); + return array_combine($options, $options); + } + + public function getEmptyString() + { + return $this->config()->get('empty_string'); + } + + public function getAlternativeFieldTitle() + { + return $this->config()->get('alternative_field_title'); + } + + public function setForm($form) + { + $this->fieldSelectedValue->setForm($form); + $this->fieldAlternativeValue->setForm($form); + return parent::setForm($form); + } + + public function setName($name) + { + $this->fieldSelectedValue->setName("{$name}[SelectedValue]"); + $this->fieldAlternativeValue->setName("{$name}[AlternativeValue]"); + return parent::setName($name); + } + + public function setValue($val) + { + $this->value = $val; + if (is_array($val)) { + $this->fieldSelectedValue->setValue($val['SelectedValue']); + $this->fieldAlternativeValue->setValue($val['AlternativeValue']); + } elseif ($val instanceof AlternativeField) { + $this->fieldSelectedValue->setValue($val->getSelectedValue()); + $this->fieldAlternativeValue->setValue($val->getAlternativeValue()); + } + } + + /** + * @return string + */ + public function Field($properties = array()) + { + Requirements::javascript(self::$module_dir . '/js/AlternativeFormField.js'); + Requirements::css(self::$module_dir . '/css/AlternativeFormField.css'); + return "
" . + "
" . + $this->fieldSelectedValue->SmallFieldHolder() . + "
" . + "
" . + $this->fieldAlternativeValue->SmallFieldHolder() . + "
" . + "
"; + } + + /** + * SaveInto checks if set-methods are available and use them instead of setting the values directly. saveInto + * initiates a new LinkField class object to pass through the values to the setter method. + */ + public function saveInto(DataObjectInterface $dataObject) + { + $fieldName = $this->name; + if ($dataObject->hasMethod("set$fieldName")) { + $dataObject->$fieldName = DBField::create('AlternativeField', array( + "SelectedValue" => $this->fieldSelectedValue->Value(), + "AlternativeValue" => $this->fieldAlternativeValue->Value() + )); + } else { + $dataObject->$fieldName->setSelectedValue($this->fieldSelectedValue->Value()); + $dataObject->$fieldName->setAlternativeValue($this->fieldAlternativeValue->Value()); + } + } +}