<?php

class CVForm extends Form {

    function __construct($controller, $name) {

        // Create fields       
        $file_validator = new Upload_Validator(); 
        $file_validator->setAllowedExtensions(array('doc','docx','pdf', 'rtf', 'jpg', 'jpeg', 'gif', 'png')); 
        //$cvfield = new FileField('Cvfield', _t('LangEditor.CVFORM_CV', 'Jūsų CV', null, null, null, 'cvtemp'));
        $cvfield = new FileField('Cvfield', _t('LangEditor.CVFORM_CV', 'Jūsų CV'), null, null, null, 'cvtemp');
        $cvfield->setValidator($file_validator); 

        $offer = JobOffersHolder::JobOfferByURLParam();
        $offer_id = ($offer) ? $offer->Xmlidn : null;
        
        $fields = new FieldSet(
                        new TextField('Name', _t('LangEditor.CVFORM_NAME', 'Vardas, Pavardė 5')),
                        new TextField('Phone', _t('LangEditor.CVFORM_PHONE', 'Telefonas')),
                        new EmailField('Email', _t('LangEditor.CVFORM_EMAIL', 'El. paštas')),
                        new TextareaField('Additional', _t('LangEditor.CVFORM_ADDITIONAL', 'Kita')),
                        $cvfield,
                        new CaptchaField('SecurityCode', _t('LangEditor.CONTACTFORM_SECURITYCODE', 'Saugos kodas')),
                        new HiddenField('offerid', 'offerid', $offer_id)
        );

        // Create action
        $actions = new FieldSet(
                        new FormAction('SendCVForm', _t('LangEditor.CVFORM_SEND', 'Siųsti'))
        );

        // Create Validators
        $validator = new RequiredFields('Name', 'Email', 'SecurityCode');

        parent::__construct($controller, $name, $fields, $actions, $validator);
    }

    function forTemplate() {
        return $this->renderWith(array(
                    $this->class,
                    'Form'
                ));
    }

    function SendCVForm($data, $form) {
        
        $securityCode = Session::get('verifycode');
        
        if($securityCode != $data['SecurityCode']){
            $form->addErrorMessage('SecurityCode', _t('LangEditor.CONTACTFORM_ERRSECURITYCODE', 'Apsaugos kodas nesutampa su žemiau esančiu'), 'bad');
            $form->sessionMessage(_t('LangEditor.CONTACTFORM_ERRSECURITYCODE', 'Apsaugos kodas nesutampa su žemiau esančiu'), 'bad');
            Director::redirectBack();
            return false;
        }
        
        //$offer = DataObject::get_by_id('JobOffersData', (int)$data['offerid']);
        $offer = DataObject::get_one('JobOffersData', "Xmlidn='".(int)$data['offerid']."'");
        
        if($offer) {
            
            $Subject = _t('LangEditor.CVFORM_SUBJECT', 'Aplikavima i skelbima').': '.$offer->Title;
            $From = $data['Email'];

            $data['Link'] = $offer->offerURL();

            
            if($offer->Xmlidn == (int)$data['offerid'] && $offer->Email) {
                $To = $offer->Email;
            } else {
                $settings = DataObject::get_one('JobOffersHolder', '', false);
                if($settings) {
                    if($offer->Type==0 && $settings->DefaultTemporaryEmail) {
                        $To = $settings->DefaultTemporaryEmail;
                    } else {
                        $To = $settings->DefaultPermanentEmail;
                    }
                }
            }
            
            $email = new Email('no-reply@webserver8.balt.net', $To, $Subject);
			$email->replyTo($From);
            //set template
            $email->setTemplate('CVEmail');
            //populate template
            $email->populateTemplate($data);
            // thats the tricky part for the files attaced
            if (isset($_FILES["Cvfield"]) && is_uploaded_file($_FILES["Cvfield"]["tmp_name"])) {
                $email->attachFile($_FILES["Cvfield"]["tmp_name"], $_FILES["Cvfield"]["name"]);
            }
            //send mail
            $email->send();
            //return to submitted message

            Session::set('ActionStatus', 'success');
            Session::set('ActionMessage', _t('LangEditor.CVFORM_SUCCESS', 'Ačiū, netrukus su Jumis susisieksime.'));
        }

        Director::redirectBack();
    }

}
