2007-07-19 12:40:28 +02:00
< ? php
/**
2008-10-01 20:33:30 +02:00
* SimpleImageField provides an easy way of uploading images to { @ link Image } has_one relationships .
* These relationships are auto - detected if you name the field accordingly .
2008-09-28 15:08:56 +02:00
* Unlike { @ link ImageField }, it doesn ' t use an iframe .
*
2008-10-01 20:33:30 +02:00
* Restricts the upload size to 2 MB by default , and only allows upload
* of files with the extension 'jpg' , 'gif' or 'png' .
*
2010-07-11 12:44:59 +02:00
* < b > Usage </ b >
*
2008-10-01 20:33:30 +02:00
* < code >
* class Article extends DataObject {
* static $has_one = array ( 'MyImage' => 'Image' );
* }
* // use in your form constructor etc.
* $myField = new SimpleImageField ( 'MyImage' );
* </ code >
*
2010-07-11 12:44:59 +02:00
* < b > Usage within a controller </ b >
*
* First add your $has_one relationship :
*
* < code >
* static $has_one = array (
* 'FileName' => 'FileType'
* );
* </ code >
* ( i . e . Image for a FileType )
*
* Then add your Field into your form :
*
* < code >
* function Form () {
2011-05-11 09:51:54 +02:00
* return new Form ( $this , " Form " , new FieldList (
2010-07-11 12:44:59 +02:00
* new SimpleImageField (
* $name = " FileTypeID " ,
* $title = " Upload your FileType "
* )
2011-05-11 09:51:54 +02:00
* ), new FieldList (
2010-07-11 12:44:59 +02:00
*
* // List the action buttons here - doform executes the function 'doform' below
* new FormAction ( " doform " , " Submit " )
*
* // List the required fields here
* ), new RequiredFields (
* " FileTypeID "
* ));
* }
* // Then make sure that the file is saved into the assets area:
* function doform ( $data , $form ) {
* $file = new File ();
* $file -> loadUploaded ( $_FILES [ 'FileTypeID' ]);
*
* // Redirect to a page thanking people for registering
2012-05-23 11:50:02 +02:00
* $this -> redirect ( 'thanks-for-your-submission/' );
2010-07-11 12:44:59 +02:00
* }
* </ code >
*
* Your file should be now in the uploads directory
*
2008-01-09 05:18:36 +01:00
* @ package forms
* @ subpackage fields - files
2007-07-19 12:40:28 +02:00
*/
2010-03-04 20:52:19 +01:00
2012-02-02 16:26:19 +01:00
/**
* @ deprecated 3.0 Use UploadField with $myField -> allowedExtensions = array ( 'jpg' , 'gif' , 'png' )
*/
2007-07-19 12:40:28 +02:00
class SimpleImageField extends FileField {
2008-10-01 20:33:30 +02:00
2012-09-19 12:07:39 +02:00
public function __construct ( $name , $title = null , $value = null ) {
2012-07-13 11:37:35 +02:00
Deprecation :: notice ( '3.0' , " SimpleImageField is deprecated. Use UploadField with \$ myField->allowedExtensions = array('jpg', 'gif', 'png') " , Deprecation :: SCOPE_CLASS );
2012-02-02 16:26:19 +01:00
2012-07-13 11:37:35 +02:00
if ( count ( func_get_args ()) > 3 ) Deprecation :: notice ( '3.0' , 'Use setRightTitle() and setFolderName() instead of constructor arguments' , Deprecation :: SCOPE_GLOBAL );
2012-01-02 17:43:36 +01:00
parent :: __construct ( $name , $title , $value );
2010-03-04 20:52:19 +01:00
2010-03-04 05:42:41 +01:00
$this -> getValidator () -> setAllowedExtensions ( array ( 'jpg' , 'gif' , 'png' ));
}
2012-09-19 12:07:39 +02:00
public function Field ( $properties = array ()) {
2009-04-29 01:55:53 +02:00
if ( $this -> form ) $record = $this -> form -> getRecord ();
2008-04-06 06:05:23 +02:00
$fieldName = $this -> name ;
2009-04-29 01:55:53 +02:00
if ( isset ( $record ) && $record ) {
2008-04-06 06:05:23 +02:00
$imageField = $record -> $fieldName ();
} else {
$imageField = " " ;
}
2008-10-14 23:46:52 +02:00
$html = " <div class= \" simpleimage \" > " ;
if ( $imageField && $imageField -> exists ()) {
$html .= '<div class="thumbnail">' ;
if ( $imageField -> hasMethod ( 'Thumbnail' ) && $imageField -> Thumbnail ()) {
2009-12-03 01:38:52 +01:00
$html .= " <img src= \" " . $imageField -> Thumbnail () -> getURL () . " \" /> " ;
2008-10-14 23:46:52 +02:00
} else if ( $imageField -> CMSThumbnail ()) {
2009-12-03 01:38:52 +01:00
$html .= " <img src= \" " . $imageField -> CMSThumbnail () -> getURL () . " \" /> " ;
2008-10-14 23:46:52 +02:00
}
$html .= '</div>' ;
}
$html .= $this -> createTag ( " input " ,
2008-04-09 13:15:02 +02:00
array (
" type " => " file " ,
" name " => $this -> name ,
" id " => $this -> id (),
2012-03-08 16:32:03 +01:00
" tabindex " => $this -> getAttribute ( 'tabindex' ),
2009-04-29 01:55:53 +02:00
'disabled' => $this -> disabled
2008-04-09 13:15:02 +02:00
)
2008-05-09 04:38:09 +02:00
);
2008-04-09 13:15:02 +02:00
$html .= $this -> createTag ( " input " ,
2008-10-14 23:46:52 +02:00
array (
" type " => " hidden " ,
" name " => " MAX_FILE_SIZE " ,
2010-03-04 05:42:41 +01:00
" value " => $this -> getValidator () -> getAllowedMaxFileSize (),
2012-03-08 16:32:03 +01:00
" tabindex " => $this -> getAttribute ( 'tabindex' ),
2008-10-14 23:46:52 +02:00
)
);
$html .= " </div> " ;
return $html ;
2008-04-06 06:05:23 +02:00
}
2007-07-19 12:40:28 +02:00
/**
* Returns a readonly version of this field
*/
2012-09-19 12:07:39 +02:00
public function performReadonlyTransformation () {
2007-07-19 12:40:28 +02:00
$field = new SimpleImageField_Disabled ( $this -> name , $this -> title , $this -> value );
$field -> setForm ( $this -> form );
2008-08-12 04:58:48 +02:00
$field -> setReadonly ( true );
2007-07-19 12:40:28 +02:00
return $field ;
}
}
2008-01-10 01:34:18 +01:00
/**
* Disabled version of { @ link SimpleImageField } .
* @ package forms
* @ subpackage fields - files
*/
2007-07-19 12:40:28 +02:00
class SimpleImageField_Disabled extends FormField {
2008-12-04 23:38:32 +01:00
protected $disabled = true ;
protected $readonly = true ;
2012-09-19 12:07:39 +02:00
public function Field ( $properties = array ()) {
2007-07-19 12:40:28 +02:00
$record = $this -> form -> getRecord ();
$fieldName = $this -> name ;
2011-08-29 22:21:44 +02:00
2007-07-19 12:40:28 +02:00
$field = " <div class= \" simpleimage \" > " ;
2011-08-29 22:21:44 +02:00
if ( $this -> value ) {
// Only the case for DataDifferencer
$field .= $this -> value ;
} else {
if ( $record ) $imageField = $record -> $fieldName ();
if ( $imageField && $imageField -> exists ()) {
if ( $imageField -> hasMethod ( 'Thumbnail' )) $field .= " <img src= \" " . $imageField -> Thumbnail () -> URL . " \" /> " ;
elseif ( $imageField -> CMSThumbnail ()) $field .= " <img src= \" " . $imageField -> CMSThumbnail () -> URL . " \" /> " ;
else {} // This shouldn't be called but it sometimes is for some reason, so we don't do anything
} else {
$field .= " <label> " . _t ( 'SimpleImageField.NOUPLOAD' , 'No Image Uploaded' ) . " </label> " ;
}
}
2007-07-19 12:40:28 +02:00
$field .= " </div> " ;
2011-08-29 22:21:44 +02:00
2007-07-19 12:40:28 +02:00
return $field ;
}
2012-03-24 04:04:52 +01:00
}