Orion PHP  0.11.12
The PHP5.3 framework
image.php
Go to the documentation of this file.
00001 <?php
00002 
00003 namespace Orion\Core\Model;
00004 
00005 use \Orion\Core;
00006 
00007 class Image extends Field
00008 {
00009 
00010     protected $image = null;
00011     protected $prefix;
00012 
00013     public function __construct( $bind='image', $label='Image', $prefix='', $required=false )
00014     {
00015         $this->type = 'image';
00016         $this->multipart = true;
00017         $this->bind = $bind;
00018         $this->label = $label;
00019         $this->prefix = $prefix;
00020         $this->required = $required;
00021         $this->allowed = $allowed;
00022     }
00023 
00024     public function prepare( $value )
00025     {
00026         if ( $this->image != null )
00027             return "'" . $this->image->getIdentifier() . "'";
00028         elseif ( empty( $_FILES[ $this->bind ][ 'tmp_name' ] ) )
00029             return null;
00030         else
00031             throw new Core\Exception( 'Error while uploading image, unable to retreive identifier.', E_USER_ERROR );
00032     }
00033 
00034     public function onSave( $value )
00035     {
00036         if ( empty( $_FILES[ $this->bind ][ 'tmp_name' ] ) )
00037             return;
00038 
00039         try
00040         {
00041             $this->image = new Core\Upload( $this->bind, Upload::IMAGE_UPLOAD_DIR );
00042             $this->image->restrict( Core\Upload::JPEG, Core\Upload::PNG, Core\Upload::GIF );
00043             $this->image->setPrefix( $this->prefix );
00044             $this->image->upload();
00045         }
00046         catch ( Core\Exception $e )
00047         {
00048             throw $e;
00049         }
00050     }
00051 
00052     public function onDelete( $oldvalue )
00053     {
00054         if ( file_exists( $oldvalue ) )
00055             @unlink( $oldvalue );
00056     }
00057 
00058     public function onUpdate( $oldvalue, $newvalue )
00059     {
00060         if ( !empty( $newvalue ) )
00061         {
00062             $this->onDelete( $oldvalue );
00063             $this->onSave( $newvalue );
00064         }
00065     }
00066 
00067     public function toHtml( $XHTML=true )
00068     {
00069         if ( $XHTML )
00070             $tag = ' /';
00071         else
00072             $tag = '';
00073 
00074         return '<div class="form-row"><label for="' . $this->bind . '">' . $this->label . '</label>' . "\n"
00075                 . '<div class="form-container"><input class="form-element" type="file" name="' . $this->bind . '" ' . $tag . '></div></div>';
00076     }
00077 
00078 }
00079 
00080 ?>