123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515 |
- <?php
- class FormValidator extends HTML_QuickForm
- {
- const LAYOUT_HORIZONTAL = 'horizontal';
- const LAYOUT_INLINE = 'inline';
- const LAYOUT_BOX = 'box';
- const LAYOUT_BOX_NO_LABEL = 'box-no-label';
- public $with_progress_bar = false;
- private $layout;
-
- public function __construct(
- $name,
- $method = 'post',
- $action = '',
- $target = '',
- $attributes = array(),
- $layout = self::LAYOUT_HORIZONTAL,
- $trackSubmit = true
- ) {
-
- if (is_array($attributes) && !isset($attributes['class']) || empty($attributes)) {
- $attributes['class'] = 'form-horizontal';
- }
- if (isset($attributes['class']) && strpos($attributes['class'], 'form-search') !== false) {
- $layout = 'inline';
- }
- $this->setLayout($layout);
- switch ($layout) {
- case self::LAYOUT_HORIZONTAL:
- $attributes['class'] = 'form-horizontal';
- break;
- case self::LAYOUT_INLINE:
- case self::LAYOUT_BOX:
- $attributes['class'] = 'form-inline';
- break;
- }
- parent::__construct($name, $method, $action, $target, $attributes, $trackSubmit);
-
- $renderer = & $this->defaultRenderer();
-
- $formTemplate = $this->getFormTemplate();
- $renderer->setFormTemplate($formTemplate);
-
- if (isset($attributes['class']) && $attributes['class'] == 'form-inline') {
- $elementTemplate = ' {label} {element} ';
- $renderer->setElementTemplate($elementTemplate);
- } elseif (isset($attributes['class']) && $attributes['class'] == 'form-search') {
- $elementTemplate = ' {label} {element} ';
- $renderer->setElementTemplate($elementTemplate);
- } else {
- $renderer->setElementTemplate($this->getDefaultElementTemplate());
-
- $templateSimple = '<div class="form-actions">{label} {element}</div>';
- $renderer->setElementTemplate($templateSimple, 'submit_in_actions');
-
- $templateBottom = '<div class="form-actions bottom_actions bg-form">{label} {element}</div>';
- $renderer->setElementTemplate($templateBottom, 'submit_fixed_in_bottom');
-
-
- $renderer->setElementTemplate($templateSimple, 'buttons_in_action');
- $templateSimpleRight = '<div class="form-actions"> <div class="pull-right">{label} {element}</div></div>';
- $renderer->setElementTemplate($templateSimpleRight, 'buttons_in_action_right');
- }
-
- $renderer->setHeaderTemplate('<legend>{header}</legend>');
-
- $this->setRequiredNote('<span class="form_required">*</span> <small>' . get_lang('ThisFieldIsRequired') . '</small>');
- $noteTemplate = <<<EOT
- <div class="form-group">
- <div class="col-sm-offset-2 col-sm-10">{requiredNote}</div>
- </div>
- EOT;
- $renderer->setRequiredNoteTemplate($noteTemplate);
- }
-
- public function getFormTemplate()
- {
- return '<form{attributes}>
- <fieldset>
- {content}
- </fieldset>
- {hidden}
- </form>';
- }
-
- public function getDefaultElementTemplate()
- {
- return '
- <div class="form-group {error_class}">
- <label {label-for} class="col-sm-2 control-label {extra_label_class}" >
- <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
- {label}
- </label>
- <div class="col-sm-8">
- {icon}
- {element}
- <!-- BEGIN label_2 -->
- <p class="help-block">{label_2}</p>
- <!-- END label_2 -->
- <!-- BEGIN error -->
- <span class="help-inline">{error}</span>
- <!-- END error -->
- </div>
- <div class="col-sm-2">
- <!-- BEGIN label_3 -->
- {label_3}
- <!-- END label_3 -->
- </div>
- </div>';
- }
-
- public function getLayout()
- {
- return $this->layout;
- }
-
- public function setLayout($layout)
- {
- $this->layout = $layout;
- }
-
- public function addText($name, $label, $required = true, $attributes = array())
- {
- $this->addElement('text', $name, $label, $attributes);
- $this->applyFilter($name, 'trim');
- if ($required) {
- $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
- }
- }
-
- public function addDateRangePicker($name, $label, $required = true, $attributes = array())
- {
- $this->addElement('date_range_picker', $name, $label, $attributes);
- $this->addElement('hidden', $name.'_start');
- $this->addElement('hidden', $name.'_end');
- if ($required) {
- $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
- }
- }
-
- public function addDatePicker($name, $label, $attributes = [])
- {
- return $this->addElement('DatePicker', $name, $label, $attributes);
- }
-
- public function addSelectLanguage($name, $label, $options = [], $attributes = [])
- {
- return $this->addElement('SelectLanguage', $name, $label, $options, $attributes);
- }
-
- public function addSelectAjax($name, $label, $options = [], $attributes = [])
- {
- if (!isset($attributes['url'])) {
- throw new \Exception('select_ajax needs an URL');
- }
- $this->addElement(
- 'select_ajax',
- $name,
- $label,
- $options,
- $attributes
- );
- }
-
- public function addDateTimePicker($name, $label, $attributes = [])
- {
- return $this->addElement('DateTimePicker', $name, $label, $attributes);
- }
-
- public function addHidden($name, $value)
- {
- $this->addElement('hidden', $name, $value);
- }
-
- public function addTextarea($name, $label, $attributes = array())
- {
- return $this->addElement('textarea', $name, $label, $attributes);
- }
-
- public function addButton(
- $name,
- $label,
- $icon = 'check',
- $style = 'default',
- $size = 'default',
- $class = null,
- $attributes = array(),
- $createElement = false
- ) {
- if ($createElement) {
- return $this->createElement(
- 'button',
- $name,
- $label,
- $icon,
- $style,
- $size,
- $class,
- $attributes
- );
- }
- return $this->addElement(
- 'button',
- $name,
- $label,
- $icon,
- $style,
- $size,
- $class,
- $attributes
- );
- }
-
- public function addButtonSave($label, $name = 'submit', $createElement = false)
- {
- return $this->addButton(
- $name,
- $label,
- 'check',
- 'primary',
- null,
- null,
- array(),
- $createElement
- );
- }
-
- public function addButtonCancel($label, $name = 'submit', $createElement = false)
- {
- return $this->addButton(
- $name,
- $label,
- 'times',
- 'danger',
- null,
- null,
- array(),
- $createElement
- );
- }
-
- public function addButtonCreate($label, $name = 'submit', $createElement = false, $attributes = array())
- {
- return $this->addButton(
- $name,
- $label,
- 'plus',
- 'primary',
- null,
- null,
- $attributes,
- $createElement
- );
- }
-
- public function addButtonUpdate($label, $name = 'submit', $createElement = false)
- {
- return $this->addButton(
- $name,
- $label,
- 'pencil',
- 'primary',
- null,
- null,
- array(),
- $createElement
- );
- }
-
- public function addButtonDelete($label, $name = 'submit', $createElement = false)
- {
- return $this->addButton(
- $name,
- $label,
- 'trash',
- 'danger',
- null,
- null,
- array(),
- $createElement
- );
- }
-
- public function addButtonSend($label, $name = 'submit', $createElement = false, $attributes = array())
- {
- return $this->addButton(
- $name,
- $label,
- 'paper-plane',
- 'primary',
- null,
- null,
- $attributes,
- $createElement
- );
- }
-
- public function addButtonSearch($label = null, $name = 'submit')
- {
- if (empty($label)) {
- $label = get_lang('Search');
- }
- return $this->addButton($name, $label, 'search', 'default');
- }
-
- public function addButtonNext($label, $name = 'submit', $attributes = array())
- {
- return $this->addButton($name, $label, 'arrow-right', 'primary', null, null, $attributes);
- }
-
- public function addButtonImport($label, $name = 'submit', $createElement = false)
- {
- return $this->addButton(
- $name,
- $label,
- 'check',
- 'primary',
- null,
- null,
- array(),
- $createElement
- );
- }
-
- public function addButtonExport($label, $name = 'submit', $createElement = false)
- {
- return $this->addButton(
- $name,
- $label,
- 'check',
- 'primary',
- null,
- null,
- array(),
- $createElement
- );
- }
-
- public function addButtonFilter($label, $name = 'submit', $createElement = false)
- {
- return $this->addButton(
- $name,
- $label,
- 'filter',
- 'primary',
- null,
- null,
- array(),
- $createElement
- );
- }
-
- public function addButtonReset($label, $name = 'reset', $createElement = false)
- {
- $icon = 'eraser';
- $style = 'default';
- $size = 'default';
- $class = null;
- $attributes = array();
- if ($createElement) {
- return $this->createElement(
- 'reset',
- $name,
- $label,
- $icon,
- $style,
- $size,
- $class,
- $attributes
- );
- }
- return $this->addElement(
- 'reset',
- $name,
- $label,
- $icon,
- $style,
- $size,
- $class,
- $attributes
- );
- }
-
- public function addButtonUpload($label, $name = 'submit', $createElement = false)
- {
- return $this->addButton(
- $name,
- $label,
- 'upload',
- 'primary',
- null,
- null,
- array(),
- $createElement
- );
- }
-
- public function addButtonDownload($label, $name = 'submit', $createElement = false)
- {
- return $this->addButton(
- $name,
- $label,
- 'download',
- 'primary',
- null,
- null,
- array(),
- $createElement
- );
- }
-
- public function addButtonPreview($label, $name = 'submit', $createElement = false)
- {
- return $this->addButton(
- $name,
- $label,
- 'search',
- 'primary',
- null,
- null,
- array(),
- $createElement
- );
- }
-
- public function addButtonCopy($label, $name = 'submit', $createElement = false)
- {
- return $this->addButton(
- $name,
- $label,
- 'copy',
- 'primary',
- null,
- null,
- array(),
- $createElement
- );
- }
-
- public function addCheckBox($name, $label, $text = '', $attributes = array())
- {
- return $this->addElement('checkbox', $name, $label, $text, $attributes);
- }
-
- public function addCheckBoxGroup($name, $label, $options = array(), $attributes = array())
- {
- $group = array();
- foreach ($options as $value => $text) {
- $attributes['value'] = $value;
- $group[] = $this->createElement('checkbox', $value, null, $text, $attributes);
- }
- return $this->addGroup($group, $name, $label);
- }
-
- public function addRadio($name, $label, $options = array(), $attributes = array())
- {
- $group = array();
- foreach ($options as $key => $value) {
- $group[] = $this->createElement('radio', null, null, $value, $key, $attributes);
- }
- return $this->addGroup($group, $name, $label);
- }
-
- public function addSelect($name, $label, $options = array(), $attributes = array())
- {
- return $this->addElement('select', $name, $label, $options, $attributes);
- }
-
- public function addSelectFromCollection(
- $name,
- $label,
- $collection,
- $attributes = array(),
- $addNoneOption = false,
- $textCallable = ''
- ) {
- $options = [];
- if ($addNoneOption) {
- $options[0] = get_lang('None');
- }
- if (!empty($collection)) {
- foreach ($collection as $item) {
- $text = $item;
- if (!empty($textCallable)) {
- $text = $item->$textCallable();
- }
- $options[$item->getId()] = $text;
- }
- }
- return $this->addElement('select', $name, $label, $options, $attributes);
- }
-
- public function addLabel($label, $text)
- {
- return $this->addElement('label', $label, $text);
- }
-
- public function addHeader($text)
- {
- $this->addElement('header', $text);
- }
-
- public function addFile($name, $label, $attributes = array())
- {
- $this->addElement('file', $name, $label, $attributes);
- }
-
- public function addHtml($snippet)
- {
- $this->addElement('html', $snippet);
- }
-
- public function addHtmlEditor($name, $label, $required = true, $fullPage = false, $config = array(), $style = false)
- {
- $config['rows'] = isset($config['rows']) ? $config['rows'] : 15;
- $config['cols'] = isset($config['cols']) ? $config['cols'] : 80;
- $this->addElement('html_editor', $name, $label, $config, $style);
- $this->applyFilter($name, 'trim');
- if ($required) {
- $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
- }
-
- $element = $this->getElement($name);
- if ($style) {
- $config['style'] = true;
- }
- if ($fullPage) {
- $config['fullPage'] = true;
- }
- if ($element->editor) {
- $element->editor->processConfig($config);
- }
- }
-
- public function addButtonAdvancedSettings($name, $label = '')
- {
- $label = !empty($label) ? $label : get_lang('AdvancedParameters');
- return $this->addElement('advanced_settings', $name, $label);
- }
-
- public function add_progress_bar($delay = 2, $label = '')
- {
- if (empty($label)) {
- $label = get_lang('PleaseStandBy');
- }
- $this->with_progress_bar = true;
- $this->updateAttributes("onsubmit=\"javascript: myUpload.start('dynamic_div','".Display::returnIconPath('progress_bar.gif')."','" . $label . "','" . $this->getAttribute('id') . "')\"");
- $this->addElement('html', '<script language="javascript" src="' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/upload.js" type="text/javascript"></script>');
- $this->addElement('html', '<script type="text/javascript">var myUpload = new upload(' . (abs(intval($delay)) * 1000) . ');</script>');
- }
-
- public function add_real_progress_bar($upload_id, $element_after, $delay = 2, $wait_after_upload = false)
- {
- if (!function_exists('uploadprogress_get_info')) {
- $this->add_progress_bar($delay);
- return;
- }
- $xajax_upload = new xajax(api_get_path(WEB_LIBRARY_PATH) . 'upload.xajax.php');
- $xajax_upload->registerFunction('updateProgress');
-
- $el = $this->insertElementBefore(FormValidator::createElement('html', '<input type="hidden" name="UPLOAD_IDENTIFIER" value="' . $upload_id . '" />'), $element_after);
- $this->addElement('html', '<br />');
-
- $this->addElement(
- 'html',
- '<div id="dynamic_div_container" style="display:none">
- <div id="dynamic_div_label">' . get_lang('UploadFile') . '</div>
- <div id="dynamic_div_frame" style="width:214px; height:12px; border:1px solid grey; background-image:url(' . Display::returnIconPath('real_upload_frame.gif').');">
- <div id="dynamic_div_filled" style="width:0%;height:100%;background-image:url(' . Display::returnIconPath('real_upload_step.gif').');background-repeat:repeat-x;background-position:center;"></div>
- </div>
- </div>'
- );
- if ($wait_after_upload) {
- $this->addElement('html', '
- <div id="dynamic_div_waiter_container" style="display:none">
- <div id="dynamic_div_waiter_label">
- ' . get_lang('SlideshowConversion') . '
- </div>
- <div id="dynamic_div_waiter_frame">
- '.Display::return_icon('real_upload_frame.gif').'
- </div>
- </div>
- ');
- }
-
- $this->addElement('html', $xajax_upload->getJavascript(api_get_path(WEB_LIBRARY_PATH) . 'xajax'));
-
- $this->addElement('html', '<script language="javascript" src="' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/upload.js" type="text/javascript"></script>');
- $this->addElement('html', '<script type="text/javascript">var myUpload = new upload(' . (abs(intval($delay)) * 1000) . ');</script>');
- if (!$wait_after_upload) {
- $wait_after_upload = 0;
- }
-
- $this->updateAttributes("onsubmit=\"javascript: myUpload.startRealUpload('dynamic_div','" . $upload_id . "','" . $this->getAttribute('id') . "'," . $wait_after_upload . ")\"");
- }
-
- public function add_multiple_required_rule($elements, $message)
- {
- $this->_required[] = $elements[0];
- $this->addRule($elements, $message, 'multiple_required');
- }
-
- public function display()
- {
- echo $this->returnForm();
- }
-
- public function returnForm()
- {
- $returnValue = '';
-
- foreach ($this->_elements as $element) {
- $elementError = parent::getElementError($element->getName());
- if (!is_null($elementError)) {
- $returnValue .= Display::return_message($elementError, 'warning').'<br />';
- break;
- }
- }
- $returnValue .= parent::toHtml();
-
- if (isset($this->with_progress_bar) && $this->with_progress_bar) {
- $returnValue .= '<div id="dynamic_div" style="display:block; margin-left:40%; margin-top:10px; height:50px;"></div>';
- }
- return $returnValue;
- }
-
- public function return_form()
- {
- return $this->returnForm();
- }
-
- public static function create($form_data)
- {
- if (empty($form_data)) {
- return null;
- }
- $form_name = isset($form_data['name']) ? $form_data['name'] : 'form';
- $form_method = isset($form_data['method']) ? $form_data['method'] : 'POST';
- $form_action = isset($form_data['action']) ? $form_data['action'] : '';
- $form_target = isset($form_data['target']) ? $form_data['target'] : '';
- $form_attributes = isset($form_data['attributes']) ? $form_data['attributes'] : null;
- $form_track_submit = isset($form_data['track_submit']) ? $form_data['track_submit'] : true;
- $reset = null;
- $result = new FormValidator($form_name, $form_method, $form_action, $form_target, $form_attributes, $form_track_submit);
- $defaults = array();
- foreach ($form_data['items'] as $item) {
- $name = $item['name'];
- $type = isset($item['type']) ? $item['type'] : 'text';
- $label = isset($item['label']) ? $item['label'] : '';
- if ($type == 'wysiwyg') {
- $element = $result->addHtmlEditor($name, $label);
- } else {
- $element = $result->addElement($type, $name, $label);
- }
- if (isset($item['attributes'])) {
- $attributes = $item['attributes'];
- $element->setAttributes($attributes);
- }
- if (isset($item['value'])) {
- $value = $item['value'];
- $element->setValue($value);
- }
- if (isset($item['default'])) {
- $defaults[$name] = $item['default'];
- }
- if (isset($item['rules'])) {
- $rules = $item['rules'];
- foreach ($rules as $rule) {
- $message = $rule['message'];
- $type = $rule['type'];
- $format = isset($rule['format']) ? $rule['format'] : null;
- $validation = isset($rule['validation']) ? $rule['validation'] : 'server';
- $force = isset($rule['force']) ? $rule['force'] : false;
- $result->addRule($name, $message, $type, $format, $validation, $reset, $force);
- }
- }
- }
- $result->setDefaults($defaults);
- return $result;
- }
-
- public static function getDefaultRenderer()
- {
- return
- isset($GLOBALS['_HTML_QuickForm_default_renderer']) ?
- $GLOBALS['_HTML_QuickForm_default_renderer'] : null;
- }
-
- public function addUrl($name, $label, $required = true, $attributes = array())
- {
- $this->addElement('url', $name, $label, $attributes);
- $this->applyFilter($name, 'trim');
- $this->addRule($name, get_lang('InsertAValidUrl'), 'url');
- if ($required) {
- $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
- }
- }
-
- public function addTextLettersOnly(
- $name,
- $label,
- $required = false,
- $attributes = []
- ) {
- $attributes = array_merge(
- $attributes,
- [
- 'pattern' => '[a-zA-ZñÑ]+',
- 'title' => get_lang('OnlyLetters')
- ]
- );
- $this->addElement(
- 'text',
- $name,
- [
- $label,
- get_lang('OnlyLetters')
- ],
- $attributes
- );
- $this->applyFilter($name, 'trim');
- if ($required) {
- $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
- }
- $this->addRule(
- $name,
- get_lang('OnlyLetters'),
- 'regex',
- '/^[a-zA-ZñÑ]+$/'
- );
- }
-
- public function addTextAlphanumeric(
- $name,
- $label,
- $required = false,
- $attributes = []
- ) {
- $attributes = array_merge(
- $attributes,
- [
- 'pattern' => '[a-zA-Z0-9ñÑ]+',
- 'title' => get_lang('OnlyLettersAndNumbers')
- ]
- );
- $this->addElement(
- 'text',
- $name,
- [
- $label,
- get_lang('OnlyLettersAndNumbers')
- ],
- $attributes
- );
- $this->applyFilter($name, 'trim');
- if ($required) {
- $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
- }
- $this->addRule(
- $name,
- get_lang('OnlyLettersAndNumbers'),
- 'regex',
- '/^[a-zA-Z0-9ÑÑ]+$/'
- );
- }
-
- public function addTextLettersAndSpaces(
- $name,
- $label,
- $required = false,
- $attributes = []
- ) {
- $attributes = array_merge(
- $attributes,
- [
- 'pattern' => '[a-zA-ZñÑ\s]+',
- 'title' => get_lang('OnlyLettersAndSpaces')
- ]
- );
- $this->addElement(
- 'text',
- $name,
- [
- $label,
- get_lang('OnlyLettersAndSpaces')
- ],
- $attributes
- );
- $this->applyFilter($name, 'trim');
- if ($required) {
- $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
- }
- $this->addRule(
- $name,
- get_lang('OnlyLettersAndSpaces'),
- 'regex',
- '/^[a-zA-ZñÑ\s]+$/'
- );
- }
-
- public function addTextAlphanumericAndSpaces(
- $name,
- $label,
- $required = false,
- $attributes = []
- ) {
- $attributes = array_merge(
- $attributes,
- [
- 'pattern' => '[a-zA-Z0-9ñÑ\s]+',
- 'title' => get_lang('OnlyLettersAndNumbersAndSpaces')
- ]
- );
- $this->addElement(
- 'text',
- $name,
- [
- $label,
- get_lang('OnlyLettersAndNumbersAndSpaces')
- ],
- $attributes
- );
- $this->applyFilter($name, 'trim');
- if ($required) {
- $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
- }
- $this->addRule(
- $name,
- get_lang('OnlyLettersAndNumbersAndSpaces'),
- 'regex',
- '/^[a-zA-Z0-9ñÑ\s]+$/'
- );
- }
-
- public function addMultipleUpload($url)
- {
- $inputName = 'input_file_upload';
- $this->addMultipleUploadJavascript($url, $inputName);
- $this->addHtml('
- <div class="description-upload">'.get_lang('ClickToSelectOrDragAndDropMultipleFilesOnTheUploadField').'</div>
- <span class="btn btn-success fileinput-button">
- <i class="glyphicon glyphicon-plus"></i>
- <span>'.get_lang('AddFiles').'</span>
- <!-- The file input field used as target for the file upload widget -->
- <input id="'.$inputName.'" type="file" name="files[]" multiple>
- </span>
- <br />
- <br />
- <div id="dropzone">
- <div class="button-load">
- '.get_lang('UploadFiles').'
- </div>
- </div>
- <br />
- <!-- The global progress bar -->
- <div id="progress" class="progress">
- <div class="progress-bar progress-bar-success"></div>
- </div>
- <div id="files" class="files"></div>
- ');
- }
-
- private function addMultipleUploadJavascript($url, $inputName)
- {
- $this->addHtml("
- <script>
- $(function () {
- 'use strict';
- $('#".$this->getAttribute('id')."').submit(function(){
- return false;
- });
- $('#dropzone').on('click', function() {
- $('#".$inputName."').click();
- });
- var url = '".$url."';
- var uploadButton = $('<button/>')
- .addClass('btn btn-primary')
- .prop('disabled', true)
- .text('".get_lang('Loading')."')
- .on('click', function () {
- var \$this = $(this),
- data = \$this.data();
- \$this
- .off('click')
- .text('".get_lang('Cancel')."')
- .on('click', function () {
- \$this.remove();
- data.abort();
- });
- data.submit().always(function () {
- \$this.remove();
- });
- });
- $('#".$inputName."').fileupload({
- url: url,
- dataType: 'json',
- autoUpload: true,
- // Enable image resizing, except for Android and Opera,
- // which actually support image resizing, but fail to
- // send Blob objects via XHR requests:
- disableImageResize: /Android(?!.*Chrome)|Opera/.test(window.navigator.userAgent),
- previewMaxWidth: 100,
- previewMaxHeight: 100,
- previewCrop: true,
- dropzone: $('#dropzone')
- }).on('fileuploadadd', function (e, data) {
- data.context = $('<div/>').appendTo('#files');
- $.each(data.files, function (index, file) {
- var node = $('<p/>').append($('<span/>').text(file.name));
- /*if (!index) {
- node
- .append('<br>')
- .append(uploadButton.clone(true).data(data));
- }*/
- node.appendTo(data.context);
- }
- );
- }).on('fileuploadprocessalways', function (e, data) {
- var index = data.index,
- file = data.files[index],
- node = $(data.context.children()[index]);
- if (file.preview) {
- node
- .prepend('<br>')
- .prepend(file.preview);
- }
- if (file.error) {
- node
- .append('<br>')
- .append($('<span class=\"text-danger\"/>').text(file.error));
- }
- if (index + 1 === data.files.length) {
- data.context.find('button')
- .text('Upload')
- .prop('disabled', !!data.files.error);
- }
- }).on('fileuploadprogressall', function (e, data) {
- var progress = parseInt(data.loaded / data.total * 100, 10);
- $('#progress .progress-bar').css(
- 'width',
- progress + '%'
- );
- }).on('fileuploaddone', function (e, data) {
- $.each(data.result.files, function (index, file) {
- if (file.url) {
- var link = $('<a>')
- .attr('target', '_blank')
- .prop('href', file.url);
- $(data.context.children()[index]).wrap(link);
- } else if (file.error) {
- var error = $('<span class=\"text-danger\"/>').text(file.error);
- $(data.context.children()[index])
- .append('<br>')
- .append(error);
- }
- });
- }).on('fileuploadfail', function (e, data) {
- $.each(data.files, function (index) {
- var error = $('<span class=\"text-danger\"/>').text('".get_lang('Failed')."');
- $(data.context.children()[index])
- .append('<br>')
- .append(error);
- });
- }).prop('disabled', !$.support.fileInput)
- .parent().addClass($.support.fileInput ? undefined : 'disabled');
- $('.fileinput-button').hide();
- });
- </script>"
- );
- }
- }
- function html_filter($html, $mode = NO_HTML)
- {
- $allowed_tags = HTML_QuickForm_Rule_HTML::get_allowed_tags($mode);
- $cleaned_html = kses($html, $allowed_tags);
- return $cleaned_html;
- }
- function html_filter_teacher($html)
- {
- return html_filter($html, TEACHER_HTML);
- }
- function html_filter_student($html)
- {
- return html_filter($html, STUDENT_HTML);
- }
- function html_filter_teacher_fullpage($html)
- {
- return html_filter($html, TEACHER_HTML_FULLPAGE);
- }
- function html_filter_student_fullpage($html)
- {
- return html_filter($html, STUDENT_HTML_FULLPAGE);
- }
- function mobile_phone_number_filter($mobilePhoneNumber)
- {
- $mobilePhoneNumber = str_replace(array('+', '(', ')'), '', $mobilePhoneNumber);
- return ltrim($mobilePhoneNumber, '0');
- }
|