mlanthaler: Bugfix: Fixed E_NOTICE error "Trying to get property of non-object".

(merged from branches/gsoc)


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41955 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2007-09-15 21:40:28 +00:00
parent 35c5b08b37
commit 44a74482e0

View File

@ -4,20 +4,28 @@
* A field that will upload attached images. * A field that will upload attached images.
*/ */
class ImageField extends FileField { class ImageField extends FileField {
public function Field($id = null) { public function Field($id = null) {
$data = $this->form->getRecord(); $data = $this->form->getRecord();
$parentID = ($id && is_numeric($id)) ? $id : (isset($data) ? $data->ID : 0); if($id && is_numeric($id)) {
$parentID = $id;
} elseif($data) {
$parentID = $data->ID;
} else {
$parentID = null;
}
if($data && $parentID && is_numeric($parentID)) { if($data && $parentID && is_numeric($parentID)) {
$idxField = $this->name . 'ID'; $idxField = $this->name . 'ID';
$hiddenField = "<input class=\"hidden\" type=\"hidden\" id=\"" . $this->id() . "\" name=\"$idxField\" value=\"" . $this->attrValue() . "\" />"; $hiddenField = "<input class=\"hidden\" type=\"hidden\" id=\"" .
$this->id() . "\" name=\"$idxField\" value=\"" . $this->attrValue() . "\" />";
$parentClass = $data->class; $parentClass = $data->class;
$parentField = $this->name; $parentField = $this->name;
$iframe = "<iframe name=\"{$this->name}_iframe\" src=\"images/iframe/$parentClass/$parentID/$parentField\" style=\"height: 152px; width: 600px; border-style: none;\"></iframe>"; $iframe = "<iframe name=\"{$this->name}_iframe\" src=\"images/iframe/$parentClass/$parentID/$parentField\" style=\"height: 152px; width: 600px; border-style: none;\"></iframe>";
return $iframe . $hiddenField; return $iframe . $hiddenField;
} else { } else {
$this->value = 'You can add images once you have saved for the first time.'; $this->value = 'You can add images once you have saved for the first time.';
@ -25,21 +33,27 @@ class ImageField extends FileField {
} }
} }
public function saveInto($record) { public function saveInto($record) {
$data = $this->form->getRecord(); $data = $this->form->getRecord();
// if the record was written for the first time (has an arbitrary "new"-ID), update the imagefield to enable uploading // if the record was written for the first time (has an arbitrary "new"-ID),
if($record->ID && substr($data->ID,0,3) == 'new') { // update the imagefield to enable uploading
if($record->ID && substr($data->ID, 0, 3) == 'new') {
FormResponse::update_dom_id($this->id(), $this->Field($record->ID)); FormResponse::update_dom_id($this->id(), $this->Field($record->ID));
} }
} }
/** /**
* Returns a readonly version of this field * Returns a readonly version of this field
*/ */
function performReadonlyTransformation() { function performReadonlyTransformation() {
$field = new SimpleImageField_Disabled($this->name, $this->title, $this->value); $field = new SimpleImageField_Disabled($this->name, $this->title,
$this->value);
$field->setForm($this->form); $field->setForm($this->form);
return $field; return $field;
} }
} }
?> ?>