- <? php
- class Application_Form_Decorators_File extends Zend_Form_Decorator_Abstract
- {
- public function buildLabel ( )
- {
- $ element = $ this -> getElement ( ) ;
- $ label = $ element -> getLabel ( ) ;
- if ( $ translator = $ element -> getTranslator ( ) ) {
- $ label = $ translator -> translate ( $ label ) ;
- }
- if ( $ element -> isRequired ( ) ) {
- $ label . = '*' ;
- }
- $ label . = ':' ;
- return $ element -> getView ( )
- -> formLabel ( $ element -> getName ( ) , $ label ) ;
- }
- public function buildInput ( )
- {
- $ element = $ this -> getElement ( ) ;
- $ helper = $ element -> helper ;
- return $ element -> getView ( ) -> $ helper (
- $ element -> getName ( ) ,
- $ element -> getValue ( ) ,
- $ element -> getAttribs ( ) ,
- $ element -> options
- ) ;
- }
- public function buildErrors ( )
- {
- $ element = $ this -> getElement ( ) ;
- $ messages = $ element -> getMessages ( ) ;
- if ( empty ( $ messages ) ) {
- return '' ;
- }
- return '<div class = "errors">' .
- $ element -> getView ( ) -> formErrors ( $ messages ) . '</ div>' ;
- }
- public function buildDescription ( )
- {
- $ element = $ this -> getElement ( ) ;
- $ desc = $ element -> getDescription ( ) ;
- if ( empty ( $ desc ) ) {
- return '' ;
- }
- return '<div class = "description">' . $ desc . '</ div>' ;
- }
- public function render ( $ content )
- {
- $ element = $ this -> getElement ( ) ;
- if ( ! $ element instanceof Zend_Form_Element ) {
- return $ content ;
- }
- if ( null === $ element -> getView ( ) ) {
- return $ content ;
- }
- $ separator = $ this -> getSeparator ( ) ;
- $ placement = $ this -> getPlacement ( ) ;
- $ label = $ this -> buildLabel ( ) ;
- $ input = $ this -> buildInput ( ) ;
- $ errors = $ this -> buildErrors ( ) ;
- $ desc = $ this -> buildDescription ( ) ;
- $ renderedContent = $ element -> getView ( ) -> partial (
- 'decorators / file.phtml' ,
- array ( 'element' => $ element ) ) ;
- $ output = '<tr> <td>'
- . $ label . '</ td> <td>' . $ renderedContent
- . $ input
- . $ errors
- . $ desc
- . '</ td> </ tr>' ;
- switch ( $ placement ) {
- case ( self :: PREPEND ) :
- return $ output . $ separator . $ content ;
- case ( self :: APPEND ) :
- default :
- return $ content . $ separator . $ output ;
- }
- }
- }
- $ renderedContent = $ element -> getView ( ) -> partial (
- 'decorators / file.phtml' ,
- array ( 'element' => $ element ) ) ;
- You uploaded a photo ' <? Php echo $ this -> element -> getDescription ( ) ; ?> '. <br>
- You can change the photo by specifying a new file. If a photo change is not required, leave this field blank. <br>
- To remove a photo, check the box:
- <input name = " <? php echo $ this -> element -> getName ( ) ; ?> _checkbox" type = "checkbox" value = "on" /> <br />
- if ( $ this -> _options [ 'type' ] == 'edit' && ! is_null ( $ this -> _options [ 'photo_file' ] ) ) {
- $ photo -> setDescription ( $ this -> _options [ 'photo_file' ] ) ;
- $ photo -> setDecorators ( array (
- array ( 'ViewScript' , array ( 'viewScript' => 'decorators / file.phtml' ) ) ,
- 'File' ,
- 'Errors' ,
- array ( array ( 'data' => 'HtmlTag' ) , array ( 'tag' => 'td' , 'class' => 'element' ) ) ,
- array ( 'Label' , array ( 'tag' => 'td' ) ) ,
- array ( array ( 'row' => 'HtmlTag' ) , array ( 'tag' => 'tr' ) )
- ) )
- } else {
- $ photo -> setDecorators ( array (
- 'File' ,
- 'Errors' ,
- array ( array ( 'data' => 'HtmlTag' ) , array ( 'tag' => 'td' , 'class' => 'element' ) ) ,
- array ( 'Label' , array ( 'tag' => 'td' ) ) ,
- array ( array ( 'row' => 'HtmlTag' ) , array ( 'tag' => 'tr' ) )
- ) )
- }
- // if a check mark for deletion is set, then delete the file and food in the database null
- if ( ! is_null ( $ this -> getRequest ( ) -> getParam ( 'photo_checkbox' ) ) && $ this -> getRequest ( ) -> getParam ( 'photo_checkbox' ) == 'on' ) {
- if ( file_exists ( PUBLIC_PATH . '/ userfiles / images / full /' . $ item -> photo ) )
- unlink ( PUBLIC_PATH . '/ userfiles / images / full /' . $ item -> photo ) ;
- if ( file_exists ( PUBLIC_PATH . '/ userfiles / images / thumb /' . $ item -> photo ) )
- unlink ( PUBLIC_PATH . '/ userfiles / images / thumb /' . $ item -> photo ) ;
- $ item -> photo = null ;
- }
- // if a new file is specified and it is loaded, then delete the old file and write a new one to the database.
- // if the File element is empty, $ form-> photo-> getFileName () == array (null);
- if ( ! is_null ( $ form -> photo -> getFileName ( ) ) && count ( $ form -> photo -> getFileName ( ) ) ! = 0 ) {
- if ( file_exists ( PUBLIC_PATH . '/ userfiles / images / full /' . $ item -> photo ) ) {
- unlink ( PUBLIC_PATH . '/ userfiles / images / full /' . $ item -> photo ) ;
- }
- if ( file_exists ( PUBLIC_PATH . '/ userfiles / images / thumb /' . $ item -> photo ) ) {
- unlink ( PUBLIC_PATH . '/ userfiles / images / thumb /' . $ item -> photo ) ;
- }
- $ item -> photo = $ form -> getValue ( 'photo' ) ;
- }
Source: https://habr.com/ru/post/136438/
All Articles