.
*/
class CandPhoto extends Cand {
/* Constructor
* Make sure you set the name, and, when needed, if the field is
* compulsory
*/
public function __construct($candidate_id) {
$this->candidate_id = $candidate_id;
$this->name = "Photo";
$this->compulsory = false;
}
/* get_print_name()
* Return the name of the field (caption) to be shown in the application
* form
*/
public function get_print_name() {
return "Photo";
}
/* get_print_name_short()
* Return the name of the field (caption) to be shown in a table
*/
public function get_print_name_short() {
return "Photo";
}
/* get_print_value($readonly)
* Return the value to be printed in the form (with text boxes etc)
* @param: readonly: if the field can be changed (false) or not (true)
*/
public function get_print_value($readonly=false) {
global $setup;
if( $this->candidate_id>0 ) $r = "candidate_id."\" alt=\"Photo\" /> ";
else $r = "";
if( !$readonly ) $r .= "name."\" size=\"40\" /> You can only upload JPEG images, with a maximum size of ".$this->get_bytesize($setup['CandidatePhotoMaxUploadSize']*1024).". Please note that uploading the photo can take up to several minutes, depending on the size of the image and the speed of your connection.";
return $r;
}
public function check() {
$this->error = false;
return $this->set( $this->value );
}
/* set($value)
* Check the supplied value and assign it if ok (return true), otherwise
* set an error and return false
* @param: value: the value to be assigned and checked
* @return: true on ok, false on error
*/
public function set($value) {
global $setup;
switch( $_FILES[$this->name]['error'] ) {
case 0:
// Ok, handle this upload
if( $_FILES[$this->name]['type']=="image/jpeg" OR $_FILES[$this->name]['type']=="image/jpg" OR $_FILES[$this->name]['type']=="image/pjpeg" ) {
if( $_FILES[$this->name]['size']<=($setup['CandidatePhotoMaxUploadSize']*1024) ) {
return true;
}else {
$this->error = true;
$this->error_txt = "The uploaded photo is too big (".$this->get_bytesize($_FILES[$this->name]['size'])." instead of ".$this->get_bytesize($setup['CandidatePhotoMaxUploadSize']*1024).")";
return false;
}
}else {
$this->error = true;
$this->error_txt = "The uploaded photo has an invalid file type (".$_FILES[$this->name]['type']."). You can only upload JPEG files.";
return false;
}
break;
case 1: // File exceeds max size as defined in PHP config
case 2: // File exceeds max size as defined in form
$this->error = true;
$this->error_txt = "The uploaded photo is too big (".$this->get_bytesize($_FILES[$this->name]['size'])." instead of ".$this->get_bytesize($setup['CandidatePhotoMaxUploadSize']*1024).")";
return false;
break;
case 3: // File only partially uploaded
$this->error = true;
$this->error_txt = "The photo was only partially uploaded";
return false;
case 4: // Nothing uploaded
return true;
default:
$this->error = true;
$this->error_txt = "Unknow error while processing photo";
return false;
}
}
public function init($value) {
$this->oldvalue = $value;
}
/* get()
* Return the value of this field.
* Normally the plain value is returned, if you need different behaviour,
* override this function
*/
public function get($oldvalue=false) {
return "candidate_id."\" alt=\"Photo\">";
}
/* Normally a field is saved to MySQL. If you don't want to save it, or want to do something else before data is saved, override this function.
* Make sure you return an array (can be empty)
*/
public function get_sql($new=true) {
global $FULL_PATH, $setup;
if( $this->get_access($new)==Cand::ACCESS_RW ) {
if( $_FILES[$this->name]['error']==0 ) {
if( $src_im=ImageCreateFromJPEG( $_FILES[$this->name]['tmp_name'] ) ) {
$orig_x = imagesx($src_im);
$orig_y = imagesy($src_im);
if( $orig_x/$setup['CandidatePhotoMaxWidth'] > $orig_y/$setup['CandidatePhotoMaxHeight'] ) {
$new_x = min($setup['CandidatePhotoMaxWidth'], $orig_x);
$new_y = $orig_y / ($orig_x / $new_x);
}else {
$new_y = min($setup['CandidatePhotoMaxHeight'], $orig_y);
$new_x = $orig_x / ($orig_y / $new_y);
}
if( !($dst_im=imagecreatetruecolor($new_x, $new_y)) ) return array();
if( !imagecopyresampled($dst_im, $src_im, 0, 0, 0, 0, $new_x, $new_y, $orig_x, $orig_y) ) return array();
if( !($tempfile=tempnam($FULL_PATH."tmp", "photo")) ) return array();
if( !imagejpeg($dst_im, $tempfile) ) return array();
imagedestroy($dst_im);
if( !($fp=fopen($tempfile, "rb")) ) return array();
if( !($this->value=fread($fp, filesize($tempfile))) ) return array();
fclose($fp);
unlink($tempfile);
return array($this->name => $this->value);
}else {
return array();
}
}else {
return array();
}
}else {
return array();
}
}
public function get_value($new=true) {
$r = array();
if( !$new ) {
if( strlen($this->oldvalue)>0 ) $r['old'][$this->get_print_name()] = "Photo";
else $r['old'][$this->get_print_name()] = "No photo";
}
if( $this->get_access($new)==Cand::ACCESS_RW OR ($new AND $this->get_access($new)==Cand::ACCESS_R) ) {
if( strlen($this->value)>0 ) {
if( strlen($this->oldvalue) > 0 && !isset($_FILES[$this->name]['tmp_name']) ) $r['new'][$this->get_print_name()] = "Photo";
else $r['new'][$this->get_print_name()] = "New photo";
}elseif( $r['old'][$this->get_print_name()]=="No photo" ) {
$r['new'][$this->get_print_name()] = "No photo";
}else {
$r['new'][$this->get_print_name()] = "Photo";
}
}
return $r;
}
private function get_bytesize($size) {
// Return the size, converted to B, KB, MB, GB or TB
// Input is in bytes
$SIZE_LABEL=array(0=>"B", 1=>"KB", 2=>"MB", 3=>"GB", 4=>"TB");
$size_loop=0;
while( $size>950 ) {
$size_loop++;
$size=$size/1024;
}
if( $size>99.5 )
return number_format($size, 0, ",", ".")." ".$SIZE_LABEL[$size_loop];
else
return number_format($size, 1, ",", ".")." ".$SIZE_LABEL[$size_loop];
}
}
?>