/** * Constructor * * Create a new SelectList * * @param FormHandler $form The form where this field is located on * @param string $name The name of the field * @param array $options The options of the field * @return \FormHandler\Field\SelectList * @author Teye Heimans * @author Marien den Besten */ public function __construct(FormHandler $form, $name, $options = array()) { $this->field_values = new \FormHandler\Field\Hidden($form, $name); $this->field_values->setDefaultValue(array()); static $bSetJS = false; // needed javascript included yet ? if (!$bSetJS) { $bSetJS = true; $form->_setJS(\FormHandler\Configuration::get('fhtml_dir') . "js/listfield.js", true); } // make the fields of the SelectList $this->field_on = new \FormHandler\Field\Select($form, $name . '_ListOn'); $this->field_off = new \FormHandler\Field\Select($form, $name . '_ListOff'); $this->field_on->setMultiple(true); $this->field_off->setMultiple(true); return parent::__construct($form, $name)->setOptions($options)->useArrayKeyAsValue(\FormHandler\Configuration::get('default_usearraykey'))->setSize(\FormHandler\Configuration::get('default_listfield_size'))->setOffTitle(\FormHandler\Language::get(29))->setOnTitle(\FormHandler\Language::get(30))->setFocusName($name . '_ListOn'); }
/** * Constructor * * Create a new file field * * @param FormHandler $form The form where this field is located on * @param string $name The name of the field * @return \FormHandler\Field\File * @author Teye Heimans * @author Marien den Besten */ public function __construct(FormHandler $form, $name) { static $bSetJS = false; // needed javascript included yet ? if (!$bSetJS) { // include the needed javascript $bSetJS = true; $form->_setJS(\FormHandler\Configuration::get('fhtml_dir') . "js/upload.js", true); } parent::__construct($form, $name); $form->setEncoding(FormHandler::ENCODING_MULTIPART); //upload fields are required by default $this->setRequired(true); $this->accept = array(); $this->drop_zone_enabled = false; $this->drop_zone_language = array('drop_file' => 'Drop your file here', 'too_large' => 'Given file is too large (Given: %given%, allowed: %allowed%)'); $this->async = !is_null(filter_input(INPUT_POST, $name . '_submit')); $this->button_upload = new Button($form, $name . '_button'); $this->button_upload->setCaption('Choose File')->setExtra('style="display:none;" class="upload"'); $this->button_edit = new \FormHandler\Button\Submit($form, $name . '_change'); $this->button_edit->setCaption('Change file')->setExtra('class="upload"')->onClick(function (FormHandler $form) use($name) { $form->setValidationDisabled(true); $form->setIsCorrect(false); $form->getField($name)->updateState(\FormHandler\Field\File::STATE_EMPTY, null, null); }); //read all open uploads $status = $this->readOpenUploads(); //by default generate a new token $generated_token = $this->generateToken(); //create field $this->token = new \FormHandler\Field\Hidden($form, $name . '_token'); $this->token->setDefaultValue($generated_token); $this->filename = new \FormHandler\Field\Hidden($form, $name . '_filename'); $this->filename->setDefaultValue(''); $this->state = new \FormHandler\Field\Hidden($form, $name . '_state'); $this->state->setDefaultValue(self::STATE_EMPTY); //process token $unsecure_token = $this->token->getValue(); $token = $this->form_object->isPosted() && !array_key_exists($unsecure_token, $status) ? $generated_token : $unsecure_token; $this->token->setValue($token); $data = $this->readState(); try { $result = $this->processUpload(); } catch (Exception $e) { $result = false; $data = $this->updateState($e->getCode(), null, null); } if ($result !== false && is_array($result)) { $data = $this->updateState(self::STATE_UPLOADED, $result[0], $result[1]); } if ($this->async) { $ajax = array('state' => $data['state'], 'name' => $data['last_processed']); //there was an javascript file submit, return state //do not send json header because IE will behave strange echo json_encode($ajax); exit; } //incorporate max file size if (!$form->fieldExists('MAX_FILE_SIZE')) { \FormHandler\Field\Hidden::set($form, 'MAX_FILE_SIZE')->setValue($this->getMaxUploadSize(), true)->hideFromOnCorrect(); } return $this; }