Overview

Namespaces

  • None
  • Quform
    • Traduttore_Registry

Classes

  • Quform
  • Quform_Admin_InsertForm
  • Quform_Admin_Page
  • Quform_Admin_Page_Controller
  • Quform_Admin_Page_Dashboard
  • Quform_Admin_Page_Entries
  • Quform_Admin_Page_Entries_Edit
  • Quform_Admin_Page_Entries_List
  • Quform_Admin_Page_Entries_View
  • Quform_Admin_Page_Factory
  • Quform_Admin_Page_Forms_Add
  • Quform_Admin_Page_Forms_Edit
  • Quform_Admin_Page_Forms_List
  • Quform_Admin_Page_Help
  • Quform_Admin_Page_Preview
  • Quform_Admin_Page_Settings
  • Quform_Admin_Page_Tools
  • Quform_Admin_Page_Tools_ExportEntries
  • Quform_Admin_Page_Tools_ExportForm
  • Quform_Admin_Page_Tools_Home
  • Quform_Admin_Page_Tools_ImportForm
  • Quform_Admin_Page_Tools_Migrate
  • Quform_Admin_Page_Tools_Uninstall
  • Quform_Api
  • Quform_Block
  • Quform_Builder
  • Quform_Captcha
  • Quform_ClassLoader
  • Quform_Confirmation
  • Quform_Container
  • Quform_Dashboard_Widget
  • Quform_Dispatcher
  • Quform_Element
  • Quform_Element_Captcha
  • Quform_Element_Checkbox
  • Quform_Element_Column
  • Quform_Element_Container
  • Quform_Element_Container_Iterator
  • Quform_Element_Date
  • Quform_Element_Email
  • Quform_Element_Factory
  • Quform_Element_Field
  • Quform_Element_File
  • Quform_Element_Group
  • Quform_Element_Hidden
  • Quform_Element_Honeypot
  • Quform_Element_Html
  • Quform_Element_Multi
  • Quform_Element_Multiselect
  • Quform_Element_Name
  • Quform_Element_Page
  • Quform_Element_Password
  • Quform_Element_Radio
  • Quform_Element_Recaptcha
  • Quform_Element_Row
  • Quform_Element_Select
  • Quform_Element_Submit
  • Quform_Element_Text
  • Quform_Element_Textarea
  • Quform_Element_Time
  • Quform_Entry_Controller
  • Quform_Entry_Exporter
  • Quform_Entry_List_Settings
  • Quform_Entry_List_Table
  • Quform_Entry_Processor
  • Quform_Entry_UserSearcher
  • Quform_Filter_Abstract
  • Quform_Filter_Alpha
  • Quform_Filter_AlphaNumeric
  • Quform_Filter_Digits
  • Quform_Filter_Regex
  • Quform_Filter_Static
  • Quform_Filter_StripTags
  • Quform_Filter_Trim
  • Quform_Form
  • Quform_Form_Controller
  • Quform_Form_Exporter
  • Quform_Form_Factory
  • Quform_Form_Importer
  • Quform_Form_Iterator
  • Quform_Form_List_Settings
  • Quform_Form_List_Table
  • Quform_Form_Processor
  • Quform_License
  • Quform_Migrator
  • Quform_NonceRefresher
  • Quform_Notification
  • Quform_Notification_Resender
  • Quform_Options
  • Quform_Permissions
  • Quform_Repository
  • Quform_ScriptLoader
  • Quform_Session
  • Quform_Settings
  • Quform_Shortcode
  • Quform_Themes
  • Quform_TokenReplacer
  • Quform_Toolbar
  • Quform_Translations
  • Quform_Updater
  • Quform_Upgrader
  • Quform_Uploader
  • Quform_Validator_Abstract
  • Quform_Validator_Alpha
  • Quform_Validator_AlphaNumeric
  • Quform_Validator_Array
  • Quform_Validator_Captcha
  • Quform_Validator_Date
  • Quform_Validator_Digits
  • Quform_Validator_Duplicate
  • Quform_Validator_Email
  • Quform_Validator_FileUpload
  • Quform_Validator_GreaterThan
  • Quform_Validator_Honeypot
  • Quform_Validator_Identical
  • Quform_Validator_InArray
  • Quform_Validator_Length
  • Quform_Validator_LessThan
  • Quform_Validator_Recaptcha
  • Quform_Validator_Regex
  • Quform_Validator_Required
  • Quform_Validator_Static
  • Quform_Validator_Time
  • Quform_View
  • Quform_ViewFactory
  • Quform_Widget_Form
  • Quform_Widget_Popup

Interfaces

  • Quform_Attachable
  • Quform_Element_Editable
  • Quform_Filter_Interface
  • Quform_Validator_Interface

Constants

  • Quform\Traduttore_Registry\TRANSIENT_KEY_PLUGIN
  • Quform\Traduttore_Registry\TRANSIENT_KEY_THEME

Functions

  • Quform\Traduttore_Registry\add_project
  • Quform\Traduttore_Registry\clean_translations_cache
  • Quform\Traduttore_Registry\get_available_locales
  • Quform\Traduttore_Registry\get_installed_translations
  • Quform\Traduttore_Registry\get_translations
  • Quform\Traduttore_Registry\register_clean_translations_cache
  • Quform\Traduttore_Registry\sanitize_date
  • Overview
  • Namespace
  • Class
   1: <?php
   2: 
   3: /**
   4:  * @copyright Copyright (c) 2009-2022 ThemeCatcher (https://www.themecatcher.net)
   5:  */
   6: abstract class Quform_Element_Field extends Quform_Element
   7: {
   8:     /**
   9:      * The default value
  10:      * @var mixed
  11:      */
  12:     protected $defaultValue;
  13: 
  14:     /**
  15:      * Element value
  16:      * @var mixed
  17:      */
  18:     protected $value;
  19: 
  20:     /**
  21:      * Element filters
  22:      * @var array
  23:      */
  24:     protected $filters = array();
  25: 
  26:     /**
  27:      * Element validators
  28:      * @var array
  29:      */
  30:     protected $validators = array();
  31: 
  32:     /**
  33:      * The current validation errors
  34:      * @var array
  35:      */
  36:     protected $errors = array();
  37: 
  38:     /**
  39:      * Is the element multiple input e.g. multiple select
  40:      * @var boolean
  41:      */
  42:     protected $isMultiple = false;
  43: 
  44:     /**
  45:      * The name of the element that this belongs to
  46:      * @var Quform_Element
  47:      */
  48:     protected $belongsTo;
  49: 
  50:     /**
  51:      * Get the unique element ID
  52:      *
  53:      * @return string
  54:      */
  55:     public function getUniqueId()
  56:     {
  57:         return sprintf('quform_%s_%s', $this->getIdentifier(), $this->form->getUniqueId());
  58:     }
  59: 
  60:     /**
  61:      * Get the name of the element
  62:      *
  63:      * @return string
  64:      */
  65:     public function getName()
  66:     {
  67:         if ($this->getBelongsTo() instanceof Quform_Element) {
  68:             return $this->getId();
  69:         }
  70: 
  71:         return parent::getName();
  72:     }
  73: 
  74:     /**
  75:      * Get the element identifier (e.g. 1_1)
  76:      *
  77:      * @return string
  78:      */
  79:     public function getIdentifier()
  80:     {
  81:         if ($this->getBelongsTo() instanceof Quform_Element) {
  82:             return sprintf('%d_%d_%d', $this->form->getId(), $this->getBelongsTo()->getId(), $this->getId());
  83:         }
  84: 
  85:         return parent::getIdentifier();
  86:     }
  87: 
  88:     /**
  89:      * Get the fully qualified name of the element
  90:      *
  91:      * @return string
  92:      */
  93:     public function getFullyQualifiedName()
  94:     {
  95:         $name = $this->getName();
  96: 
  97:         if ($this->getBelongsTo() instanceof Quform_Element) {
  98:             $name = sprintf('%s[%s]', $this->getBelongsTo()->getName(), $name);
  99:         }
 100: 
 101:         if ($this->isMultiple()) {
 102:             $name .= '[]';
 103:         }
 104: 
 105:         return $name;
 106:     }
 107: 
 108:     /**
 109:      * Get the HTML for the element
 110:      *
 111:      * @param   array   $context
 112:      * @return  string
 113:      */
 114:     public function render(array $context = array())
 115:     {
 116:         $context = $this->prepareContext($context);
 117:         $output = '';
 118: 
 119:         if ($this->isVisible()) {
 120:             $output .= sprintf('<div class="%s">', Quform::escape(Quform::sanitizeClass($this->getElementClasses($context))));
 121:             $output .= '<div class="quform-spacer">';
 122:             $output .= $this->getLabelHtml($context);
 123:             $output .= sprintf('<div class="%s">', Quform::escape(Quform::sanitizeClass($this->getInnerClasses($context))));
 124:             $output .= $this->getDescriptionHtml('above') . $this->getSubLabelHtml('above');
 125:             $output .= $this->getInputHtml($context);
 126:             $output .= $this->getErrorHtml() . $this->getSubLabelHtml() . $this->getDescriptionHtml();
 127:             $output .= '</div></div></div>';
 128:         } else if ($this->shouldConvertToHidden()) {
 129:             $output .= Quform::getHtmlTag('input', array(
 130:                 'type' => 'hidden',
 131:                 'name' => $this->getFullyQualifiedName(),
 132:                 'value' => $this->getValue()
 133:             ));
 134:         }
 135: 
 136:         return $output;
 137:     }
 138: 
 139:     /**
 140:      * Get the classes for the outermost element wrapper
 141:      *
 142:      * @param   array  $context
 143:      * @return  array
 144:      */
 145:     protected function getElementClasses(array $context = array())
 146:     {
 147:         $classes = array(
 148:             'quform-element',
 149:             sprintf('quform-element-%s', $this->config('type')),
 150:             sprintf('quform-element-%s', $this->getIdentifier()),
 151:             'quform-cf'
 152:         );
 153: 
 154:         if (Quform::isNonEmptyString($context['labelPosition'])) {
 155:             $classes[] = sprintf('quform-labels-%s', $context['labelPosition']);
 156:         }
 157: 
 158:         if ($this->isRequired()) {
 159:             $classes[] = 'quform-element-required';
 160:         } else {
 161:             $classes[] = 'quform-element-optional';
 162:         }
 163: 
 164:         if (Quform::isNonEmptyString($this->config('customElementClass'))) {
 165:             $classes[] = $this->config('customElementClass');
 166:         }
 167: 
 168:         $classes = apply_filters('quform_element_classes', $classes, $this, $context);
 169:         $classes = apply_filters('quform_element_classes_' . $this->getIdentifier(), $classes, $this, $context);
 170: 
 171:         return $classes;
 172:     }
 173: 
 174:     /**
 175:      * Get the HTML for the element label
 176:      *
 177:      * @param   array        $context
 178:      * @param   string|bool  $forAttribute  Set the "for" attribute to the element unique ID
 179:      * @param   bool         $id            Add a unique ID to the label
 180:      * @return  string
 181:      */
 182:     protected function getLabelHtml(array $context = array(), $forAttribute = true, $id = false)
 183:     {
 184:         $label = $this->getLabel($context);
 185: 
 186:         if ( ! Quform::isNonEmptyString($label)) {
 187:             return '';
 188:         }
 189: 
 190:         if ($forAttribute === true) {
 191:             $forValue = $this->getUniqueId();
 192:         } elseif (Quform::isNonEmptyString($forAttribute)) {
 193:             $forValue = $forAttribute;
 194:         }
 195: 
 196:         $output = sprintf('<div class="quform-label quform-label-%s">', $this->getIdentifier());
 197: 
 198:         if (Quform::isNonEmptyString($this->config('labelIcon'))) {
 199:             $output .= sprintf('<span class="quform-label-icon"><i class="%s"></i></span>', $this->config('labelIcon'));
 200:         }
 201: 
 202:         $output .= sprintf(
 203:             '<label class="quform-label-text"%s%s>%s%s</label>',
 204:             ($id ? ' id="' . Quform::escape($this->getUniqueId() . '_label') . '"' : ''),
 205:             (isset($forValue) ? ' for="' . Quform::escape($forValue) . '"' : ''),
 206:             $label,
 207:             $this->isRequired() && Quform::isNonEmptyString($requiredText = $this->form->config('requiredText')) ? '<span class="quform-required">' . esc_html($requiredText) . '</span>' : ''
 208:         );
 209: 
 210:         if ($this->form->config('tooltipsEnabled') && Quform::isNonEmptyString($this->form->config('tooltipIcon')) && Quform::isNonEmptyString($tooltip = $this->getTooltip()) && $context['tooltipType'] == 'icon') {
 211:             $output .= sprintf('<div class="quform-tooltip-icon quform-tooltip-icon-%s">', esc_attr($context['tooltipEvent']));
 212:             $output .= sprintf('<i class="%s"></i>', $this->form->config('tooltipIcon'));
 213:             $output .= sprintf('<div class="quform-tooltip-icon-content">%s</div>', $tooltip);
 214:             $output .= '</div>';
 215:         }
 216: 
 217:         $output .= '</div>';
 218: 
 219:         $output = apply_filters('quform_field_label_html_' . $this->getIdentifier(), $output, $this, $this->getForm(), $context);
 220: 
 221:         return $output;
 222:     }
 223: 
 224:     /**
 225:      * Get the field label text
 226:      *
 227:      * @param   array   $context
 228:      * @return  string
 229:      */
 230:     public function getLabel(array $context = array())
 231:     {
 232:         return apply_filters('quform_field_label_' . $this->getIdentifier(), $this->config('label'), $this, $this->getForm(), $context);
 233:     }
 234: 
 235:     /**
 236:      * Get the classes for the element inner wrapper
 237:      *
 238:      * @param   array  $context
 239:      * @return  array
 240:      */
 241:     protected function getInnerClasses(array $context = array())
 242:     {
 243:         $classes = array(
 244:             'quform-inner',
 245:             sprintf('quform-inner-%s', $this->config('type')),
 246:             sprintf('quform-inner-%s', $this->getIdentifier())
 247:         );
 248: 
 249:         if (Quform::isNonEmptyString($context['fieldSize'])) {
 250:             $classes[] = sprintf('quform-field-size-%s', $context['fieldSize']);
 251:         }
 252: 
 253:         if (Quform::isNonEmptyString($context['fieldWidth']) && $context['fieldWidth'] != 'custom') {
 254:             $classes[] = sprintf('quform-field-width-%s', $context['fieldWidth']);
 255:         }
 256: 
 257:         return $classes;
 258:     }
 259: 
 260:     /**
 261:      * Get the HTML for the element description
 262:      *
 263:      * @param   string  $which  Which description to get ('above' or 'below')
 264:      * @return  string
 265:      */
 266:     protected function getDescriptionHtml($which = 'below')
 267:     {
 268:         $output = '';
 269:         $description = $which == 'above' ? $this->config('descriptionAbove') : $this->config('description');
 270: 
 271:         if (Quform::isNonEmptyString($description)) {
 272:             $output = sprintf(
 273:                 '<p class="quform-description quform-description-%s">%s</p>',
 274:                 esc_attr($which),
 275:                 do_shortcode($description)
 276:             );
 277:         }
 278: 
 279:         return $output;
 280:     }
 281: 
 282:     /**
 283:      * Get the HTML for the element input wrapper
 284:      *
 285:      * @param   array   $context
 286:      * @return  string
 287:      */
 288:     protected function getInputHtml(array $context = array())
 289:     {
 290:         $output = sprintf('<div class="%s">', Quform::escape(Quform::sanitizeClass($this->getInputClasses($context))));
 291:         $output .= $this->getFieldHtml($context);
 292:         $output .= $this->getFieldIconsHtml();
 293: 
 294:         if ($this->form->config('tooltipsEnabled') && Quform::isNonEmptyString($tooltip = $this->getTooltip()) && Quform::get($context, 'tooltipType') == 'field') {
 295:             $output .= sprintf('<div class="quform-tooltip-content">%s</div>', $tooltip);
 296:         }
 297: 
 298:         $output .= '</div>';
 299: 
 300:         return $output;
 301:     }
 302: 
 303:     /**
 304:      * Get the field tooltip
 305:      *
 306:      * @return  string
 307:      */
 308:     protected function getTooltip() {
 309:         return apply_filters('quform_field_tooltip_' . $this->getIdentifier(), $this->config('tooltip'), $this, $this->getForm());
 310:     }
 311: 
 312:     /**
 313:      * Get the classes for the element input wrapper
 314:      *
 315:      * @param   array  $context
 316:      * @return  array
 317:      */
 318:     protected function getInputClasses(array $context = array())
 319:     {
 320:         $classes = array(
 321:             'quform-input',
 322:             sprintf('quform-input-%s', $this->config('type')),
 323:             sprintf('quform-input-%s', $this->getIdentifier()),
 324:             'quform-cf'
 325:         );
 326: 
 327:         if (Quform::isNonEmptyString($this->config('fieldIconLeft'))) {
 328:             $classes[] = 'quform-has-field-icon-left';
 329:         }
 330: 
 331:         if (Quform::isNonEmptyString($this->config('fieldIconRight'))) {
 332:             $classes[] = 'quform-has-field-icon-right';
 333:         }
 334: 
 335:         return $classes;
 336:     }
 337: 
 338:     /**
 339:      * Get the HTML for the field
 340:      *
 341:      * @param   array  $context
 342:      * @return  string
 343:      */
 344:     abstract protected function getFieldHtml(array $context = array());
 345: 
 346:     /**
 347:      * Get the HTML for the field icons
 348:      *
 349:      * @return string
 350:      */
 351:     protected function getFieldIconsHtml()
 352:     {
 353:         $output = '';
 354: 
 355:         if (Quform::isNonEmptyString($this->config('fieldIconLeft'))) {
 356:             $output .= '<span class="quform-field-icon quform-field-icon-left">';
 357:             $output .= sprintf('<i class="%s"></i>', $this->config('fieldIconLeft'));
 358:             $output .= '</span>';
 359:         }
 360: 
 361:         if (Quform::isNonEmptyString($this->config('fieldIconRight'))) {
 362:             $output .= '<span class="quform-field-icon quform-field-icon-right">';
 363:             $output .= sprintf('<i class="%s"></i>', $this->config('fieldIconRight'));
 364:             $output .= '</span>';
 365:         }
 366: 
 367:         return $output;
 368:     }
 369: 
 370:     /**
 371:      * Get the admin label
 372:      *
 373:      * @return string
 374:      */
 375:     public function getAdminLabel()
 376:     {
 377:         $adminLabel = apply_filters('quform_field_admin_label_' . $this->getIdentifier(), $this->config('adminLabel'), $this, $this->getForm());
 378: 
 379:         if ( ! Quform::isNonEmptyString($adminLabel)) {
 380:             $adminLabel = $this->getLabel();
 381:         }
 382: 
 383:         return $adminLabel;
 384:     }
 385: 
 386:     /**
 387:      * @return string
 388:      */
 389:     public function getEditLabelHtml()
 390:     {
 391:         $output = Quform::escape($this->getAdminLabel());
 392: 
 393:         if ($this->isRequired()) {
 394:             $output .= '<span class="qfb-required">*</span>';
 395:         }
 396: 
 397:         return $output;
 398:     }
 399: 
 400:     /**
 401:      * Set the flag that the element can have multiple values.
 402:      *
 403:      * @param boolean $flag
 404:      */
 405:     public function setIsMultiple($flag = true)
 406:     {
 407:         $this->isMultiple = (bool) $flag;
 408:     }
 409: 
 410:     /**
 411:      * Does this element have multiple values?
 412:      *
 413:      * @return boolean
 414:      */
 415:     public function isMultiple()
 416:     {
 417:         return $this->isMultiple;
 418:     }
 419: 
 420:     /**
 421:      * Set the parent element to which this one belongs to
 422:      *
 423:      * @param   Quform_Element  $belongsTo
 424:      * @return  $this
 425:      */
 426:     public function setBelongsTo($belongsTo)
 427:     {
 428:         $this->belongsTo = $belongsTo;
 429: 
 430:         return $this;
 431:     }
 432: 
 433:     /**
 434:      * Get the name of the parent element to which this one belongs
 435:      *
 436:      * @return Quform_Element
 437:      */
 438:     public function getBelongsTo()
 439:     {
 440:         return $this->belongsTo;
 441:     }
 442: 
 443:     /**
 444:      * Add a filter
 445:      *
 446:      * @param Quform_Filter_Interface $filter The instance of the filter
 447:      */
 448:     public function addFilter(Quform_Filter_Interface $filter)
 449:     {
 450:         $name = get_class($filter);
 451:         $this->filters[$name] = $filter;
 452:     }
 453: 
 454:     /**
 455:      * Remove all filters
 456:      */
 457:     public function clearFilters()
 458:     {
 459:         $this->filters = array();
 460:     }
 461: 
 462:     /**
 463:      * Does this element have filters?
 464:      *
 465:      * @return bool
 466:      */
 467:     public function hasFilters()
 468:     {
 469:         return count($this->getFilters()) > 0;
 470:     }
 471: 
 472:     /**
 473:      * Get the filters
 474:      *
 475:      * @return array The array of filters
 476:      */
 477:     public function getFilters()
 478:     {
 479:         return $this->filters;
 480:     }
 481: 
 482:     /**
 483:      * Does the element have the given filter?
 484:      *
 485:      * @param   string|Quform_Filter_Interface  $filter  The name or instance of the filter
 486:      * @return  bool
 487:      */
 488:     public function hasFilter($filter)
 489:     {
 490:         $result = false;
 491: 
 492:         if ($filter instanceof Quform_Filter_Interface) {
 493:             $filter = get_class($filter);
 494:         }
 495: 
 496:         if (is_string($filter)) {
 497:             if (strpos($filter, 'Quform_Filter_') === false) {
 498:                 $filter = 'Quform_Filter_' . ucfirst($filter);
 499:             }
 500: 
 501:             $result = array_key_exists($filter, $this->getFilters());
 502:         }
 503: 
 504:         return $result;
 505:     }
 506: 
 507:     /**
 508:      * Get the filter with the given name
 509:      *
 510:      * @param string $filter The name of the filter
 511:      * @return Quform_Filter_Interface|null The filter or null if the element does not have the filter
 512:      */
 513:     public function getFilter($filter)
 514:     {
 515:         $instance = null;
 516: 
 517:         if (strpos($filter, 'Quform_Filter_') === false) {
 518:             $filter = 'Quform_Filter_' . ucfirst($filter);
 519:         }
 520: 
 521:         $filters = $this->getFilters();
 522:         if (array_key_exists($filter, $filters)) {
 523:             $instance = $filters[$filter];
 524:         }
 525: 
 526:         return $instance;
 527:     }
 528: 
 529:     /**
 530:      * Remove a filter with the given name
 531:      *
 532:      * @param  string  $filter  The name of the filter
 533:      */
 534:     public function removeFilter($filter)
 535:     {
 536:         if (strpos($filter, 'Quform_Filter_') === false) {
 537:             $filter = 'Quform_Filter_' . ucfirst($filter);
 538:         }
 539: 
 540:         if (array_key_exists($filter, $this->filters)) {
 541:             unset($this->filters[$filter]);
 542:         }
 543:     }
 544: 
 545:     /**
 546:      * Add a validator
 547:      *
 548:      * @param   Quform_Validator_Interface  $validator  The validator instance to add
 549:      */
 550:     public function addValidator(Quform_Validator_Interface $validator)
 551:     {
 552:         $name = get_class($validator);
 553:         $this->validators[$name] = $validator;
 554:     }
 555: 
 556:     /**
 557:      * Remove all validators
 558:      */
 559:     public function clearValidators()
 560:     {
 561:         $this->validators = array();
 562:     }
 563: 
 564:     /**
 565:      * Does the element have any validators?
 566:      *
 567:      * @return bool
 568:      */
 569:     public function hasValidators()
 570:     {
 571:         return count($this->getValidators()) > 0;
 572:     }
 573: 
 574:     /**
 575:      * Get the validators
 576:      *
 577:      * @return Quform_Validator_Abstract[] The validators
 578:      */
 579:     public function getValidators()
 580:     {
 581:         return $this->validators;
 582:     }
 583: 
 584:     /**
 585:      * Does the element have the given validator?
 586:      *
 587:      * @param   string|Quform_Validator_Abstract  $validator  The name or instance of the validator
 588:      * @return  boolean
 589:      */
 590:     public function hasValidator($validator)
 591:     {
 592:         $result = false;
 593: 
 594:         if ($validator instanceof Quform_Validator_Interface) {
 595:             $validator = get_class($validator);
 596:         }
 597: 
 598:         if (is_string($validator)) {
 599:             if (strpos($validator, 'Quform_Validator_') === false) {
 600:                 $validator = 'Quform_Validator_' . ucfirst($validator);
 601:             }
 602: 
 603:             $result = array_key_exists($validator, $this->getValidators());
 604:         }
 605: 
 606:         return $result;
 607:     }
 608: 
 609:     /**
 610:      * Get the validator with the given name
 611:      *
 612:      * @param   string $validator               The name of the validator
 613:      * @return  Quform_Validator_Abstract|null  The validator or null if the element does not have the validator
 614:      */
 615:     public function getValidator($validator)
 616:     {
 617:         $instance = null;
 618: 
 619:         if (strpos($validator, 'Quform_Validator_') === false) {
 620:             $validator = 'Quform_Validator_' . ucfirst($validator);
 621:         }
 622: 
 623:         $validators = $this->getValidators();
 624:         if (array_key_exists($validator, $validators)) {
 625:             $instance = $validators[$validator];
 626:         }
 627: 
 628:         return $instance;
 629:     }
 630: 
 631:     /**
 632:      * Remove a validator with the given name
 633:      *
 634:      * @param string $validator The name of the validator
 635:      */
 636:     public function removeValidator($validator)
 637:     {
 638:         if (strpos($validator, 'Quform_Validator_') === false) {
 639:             $validator = 'Quform_Validator_' . ucfirst($validator);
 640:         }
 641: 
 642:         if (array_key_exists($validator, $this->validators)) {
 643:             unset($this->validators[$validator]);
 644:         }
 645:     }
 646: 
 647:     /**
 648:      * Gets whether the element is required or not
 649:      *
 650:      * @return boolean
 651:      */
 652:     public function isRequired()
 653:     {
 654:         return $this->hasValidator('required');
 655:     }
 656: 
 657:     /**
 658:      * @param mixed $value
 659:      */
 660:     public function setDefaultValue($value)
 661:     {
 662:         $this->defaultValue = $value;
 663:     }
 664: 
 665:     /**
 666:      * @return mixed
 667:      */
 668:     public function getDefaultValue()
 669:     {
 670:         return apply_filters('quform_field_default_value_' . $this->getIdentifier(), $this->defaultValue, $this, $this->getForm());
 671:     }
 672: 
 673:     /**
 674:      * @return bool
 675:      */
 676:     public function hasDefaultValue()
 677:     {
 678:         return $this->getDefaultValue() !== $this->getEmptyValue();
 679:     }
 680: 
 681:     /**
 682:      * Set the value
 683:      *
 684:      * @param mixed $value
 685:      */
 686:     public function setValue($value)
 687:     {
 688:         $this->value = $this->isValidValue($value) ? $value : $this->getEmptyValue();
 689:     }
 690: 
 691:     /**
 692:      * Set the value from the database storage
 693:      *
 694:      * @param   string  $value
 695:      * @return  $this
 696:      */
 697:     public function setValueFromStorage($value)
 698:     {
 699:         $value = apply_filters('quform_set_value_from_storage', $value, $this, $this->form);
 700:         $value = apply_filters('quform_set_value_from_storage_' . $this->form->getId(), $value, $this, $this->form);
 701:         $value = apply_filters('quform_set_value_from_storage_' . $this->getIdentifier(), $value, $this, $this->form);
 702: 
 703:         $value = $this->convertValueFromStorage($value);
 704: 
 705:         $this->setValue($value);
 706: 
 707:         return $this;
 708:     }
 709: 
 710:     /**
 711:      * Convert the value from storage format to element format
 712:      *
 713:      * @param   string  $value
 714:      * @return  string
 715:      */
 716:     protected function convertValueFromStorage($value)
 717:     {
 718:         return $value;
 719:     }
 720: 
 721:     /**
 722:      * Is the given value valid for this element type
 723:      *
 724:      * @param   string  $value
 725:      * @return  bool
 726:      */
 727:     protected function isValidValue($value)
 728:     {
 729:         return is_string($value);
 730:     }
 731: 
 732:     /**
 733:      * Get the unfiltered (raw) value
 734:      *
 735:      * @return string|array
 736:      */
 737:     public function getValueRaw()
 738:     {
 739:         $value = apply_filters('quform_get_value_raw_' . $this->getIdentifier(), $this->value, $this, $this->getForm());
 740: 
 741:         return $value;
 742:     }
 743: 
 744:     /**
 745:      * Get the filtered value
 746:      *
 747:      * @return string
 748:      */
 749:     public function getValue()
 750:     {
 751:         $value = (string) $this->value;
 752: 
 753:         $this->filterValue($value);
 754: 
 755:         $value = apply_filters('quform_get_value_' . $this->getIdentifier(), $value, $this, $this->getForm());
 756: 
 757:         return $value;
 758:     }
 759: 
 760:     /**
 761:      * Get the value formatted in HTML
 762:      *
 763:      * @return string
 764:      */
 765:     public function getValueHtml()
 766:     {
 767:         $value = Quform::escape($this->getValue());
 768: 
 769:         $value = apply_filters('quform_get_value_html_' . $this->getIdentifier(), $value, $this, $this->getForm());
 770: 
 771:         return $value;
 772:     }
 773: 
 774:     /**
 775:      * Get the value formatted in plain text
 776:      *
 777:      * @param   string  $separator  The separator for array types (used by child classes)
 778:      * @return  string
 779:      */
 780:     public function getValueText($separator = ', ')
 781:     {
 782:         $value = $this->getValue();
 783: 
 784:         $value = apply_filters('quform_get_value_text_' . $this->getIdentifier(), $value, $this, $this->getForm());
 785: 
 786:         return $value;
 787:     }
 788: 
 789:     /**
 790:      * Get the value for storage in the database
 791:      *
 792:      * @return string
 793:      */
 794:     public function getValueForStorage()
 795:     {
 796:         $value = $this->getConvertedValueForStorage();
 797: 
 798:         $value = apply_filters('quform_get_value_for_storage', $value, $this, $this->form);
 799:         $value = apply_filters('quform_get_value_for_storage_' . $this->form->getId(), $value, $this, $this->form);
 800:         $value = apply_filters('quform_get_value_for_storage_' . $this->getIdentifier(), $value, $this, $this->form);
 801: 
 802:         return $value;
 803:     }
 804: 
 805:     /**
 806:      * Convert the value from element format to storage format
 807:      *
 808:      * @return string
 809:      */
 810:     protected function getConvertedValueForStorage()
 811:     {
 812:         return $this->getValue();
 813:     }
 814: 
 815:     /**
 816:      * Should this element be converted to a hidden field?
 817:      *
 818:      * Currently, this only applies when non-visible fields have a dynamic default value
 819:      *
 820:      * @return bool
 821:      */
 822:     protected function shouldConvertToHidden()
 823:     {
 824:         return ! $this->isVisible() && $this->config('dynamicDefaultValue') && Quform::isNonEmptyString($this->config('dynamicKey'));
 825:     }
 826: 
 827:     /**
 828:      * Is the data given for this element valid?
 829:      *
 830:      * @return boolean True if valid, false otherwise
 831:      */
 832:     public function isValid()
 833:     {
 834:         $this->clearErrors();
 835:         $skipValidation = false;
 836:         $valid = true;
 837: 
 838:         // Skip validation if the value is empty and the element is not required
 839:         if ( ! $this->hasValidator('required') && $this->getValueText() === '') {
 840:             $skipValidation = true;
 841:         }
 842: 
 843:         // Skip validation if the element is conditionally hidden, or the element is not visible (e.g. admin only)
 844:         if ($this->isConditionallyHidden() || ! $this->isVisible()) {
 845:             $skipValidation = true;
 846:         }
 847: 
 848:         if ( ! $skipValidation) {
 849:             $value = $this->getValue();
 850: 
 851:             foreach ($this->getValidators() as $validator) {
 852:                 if ($validator->isValid($value)) {
 853:                     continue;
 854:                 }
 855: 
 856:                 $this->addError($validator->getMessage());
 857:                 $valid = false;
 858:                 break;
 859:             }
 860: 
 861:             $valid = apply_filters('quform_element_valid', $valid, $value, $this);
 862:             $valid = apply_filters('quform_element_valid_' . $this->getIdentifier(), $valid, $value, $this);
 863:         }
 864: 
 865:         return $valid;
 866:     }
 867: 
 868:     /**
 869:      * Does the element have a validation error?
 870:      *
 871:      * @return boolean
 872:      */
 873:     public function hasError()
 874:     {
 875:         return count($this->errors) > 0;
 876:     }
 877: 
 878:     /**
 879:      * Set the validation error message
 880:      *
 881:      * @deprecated 2.2.0 Use addError
 882:      * @param string $message
 883:      */
 884:     public function setError($message)
 885:     {
 886:         _deprecated_function(__METHOD__, '2.2.0', 'addError');
 887: 
 888:         $this->addError($message);
 889:     }
 890: 
 891:     /**
 892:      * Add a validation error message
 893:      *
 894:      * @param string $message
 895:      */
 896:     public function addError($message)
 897:     {
 898:         $this->errors[] = $message;
 899:     }
 900: 
 901:     /**
 902:      * @deprecated 2.2.0 Use clearErrors
 903:      */
 904:     public function clearError()
 905:     {
 906:         _deprecated_function(__METHOD__, '2.2.0', 'clearErrors');
 907: 
 908:         $this->clearErrors();
 909:     }
 910: 
 911:     /**
 912:      * Clear the validation error messages
 913:      */
 914:     public function clearErrors()
 915:     {
 916:         $this->errors = array();
 917:     }
 918: 
 919:     /**
 920:      * Get the first validation error message
 921:      *
 922:      * @return string
 923:      */
 924:     public function getError()
 925:     {
 926:         $errors = $this->getErrors();
 927: 
 928:         return count($errors) > 0 ? $errors[0] : '';
 929:     }
 930: 
 931:     /**
 932:      * Get the validation error messages
 933:      *
 934:      * @return array
 935:      */
 936:     public function getErrors()
 937:     {
 938:         return $this->errors;
 939:     }
 940: 
 941:     /**
 942:      * Get the validation error message for the front JS to process
 943:      */
 944:     public function getErrorArray()
 945:     {
 946:         return array($this->getIdentifier() => $this->getError());
 947:     }
 948: 
 949:     /**
 950:      * Reset the value to default
 951:      */
 952:     public function reset()
 953:     {
 954:         $this->setValue($this->getDefaultValue());
 955:     }
 956: 
 957:     /**
 958:      * Sets the default value dynamically
 959:      *
 960:      * @param string $key
 961:      */
 962:     public function setDynamicDefaultValue($key)
 963:     {
 964:         $value = '';
 965: 
 966:         $dynamicValues = $this->form->getDynamicValues();
 967: 
 968:         if (isset($dynamicValues[$key])) {
 969:             $value = $dynamicValues[$key];
 970:         }
 971: 
 972:         if (isset($_GET[$key])) {
 973:             $value = $_GET[$key];
 974:         }
 975: 
 976:         $value = $this->prepareDynamicValue($value);
 977: 
 978:         $value = apply_filters('quform_element_value', $value, $key);
 979:         $value = apply_filters('quform_element_value_' . $key, $value, $key);
 980: 
 981:         if ($this->isValidValue($value) && $value !== $this->getEmptyValue()) {
 982:             $this->setDefaultValue($value, false);
 983:             $this->setValue($this->getDefaultValue());
 984:         }
 985:     }
 986: 
 987:     /**
 988:      * Subclasses can alter the dynamic default value to suit
 989:      *
 990:      * @param   string  $value
 991:      * @return  string  $value
 992:      */
 993:     public function prepareDynamicValue($value)
 994:     {
 995:         return $value;
 996:     }
 997: 
 998:     /**
 999:      * Filter the given value by reference
1000:      *
1001:      * @param string $value
1002:      */
1003:     protected function filterValue(&$value)
1004:     {
1005:         foreach ($this->getFilters() as $filter) {
1006:             $value = $filter->filter($value);
1007:         }
1008:     }
1009: 
1010:     /**
1011:      * Recursively filter the given value by reference
1012:      *
1013:      * @param array $value
1014:      */
1015:     protected function filterValueRecursive(&$value)
1016:     {
1017:         if (is_array($value)) {
1018:             array_walk($value, array($this, 'filterValueRecursive'));
1019:         } else {
1020:             $this->filterValue($value);
1021:         }
1022:     }
1023: 
1024:     /**
1025:      * Does this element have the given value?
1026:      *
1027:      * @param mixed $value
1028:      * @return boolean
1029:      */
1030:     public function hasValue($value)
1031:     {
1032:         return $this->getValue() === $value;
1033:     }
1034: 
1035:     /**
1036:      * @return string
1037:      */
1038:     public function getEmptyValue()
1039:     {
1040:         return '';
1041:     }
1042: 
1043:     /**
1044:      * Does this element have an empty value?
1045:      *
1046:      * @return boolean
1047:      */
1048:     public function isEmpty()
1049:     {
1050:         return $this->getValue() === $this->getEmptyValue();
1051:     }
1052: 
1053:     /**
1054:      * Get the HTML for the sub label
1055:      *
1056:      * @param string $which
1057:      * @return string
1058:      */
1059:     protected function getSubLabelHtml($which = 'below')
1060:     {
1061:         $output = '';
1062:         $subLabel = $which == 'above' ? $this->config('subLabelAbove') : $this->config('subLabel');
1063: 
1064:         if (Quform::isNonEmptyString($subLabel)) {
1065:             $output = sprintf(
1066:                 '<label id="%1$s_sub_label_%2$s" class="quform-sub-label quform-sub-label-%2$s">%3$s</label>',
1067:                 esc_attr($this->getUniqueId()),
1068:                 esc_attr($which),
1069:                 do_shortcode($subLabel)
1070:             );
1071:         }
1072: 
1073:         return $output;
1074:     }
1075: 
1076:     /**
1077:      * Get the HTML for this element's error (only shown when useAjax is false)
1078:      *
1079:      * @return string
1080:      */
1081:     protected function getErrorHtml()
1082:     {
1083:         if ( ! $this->hasError()) {
1084:             return '';
1085:         }
1086: 
1087:         $output = '<div class="quform-error quform-cf"><div class="quform-error-inner">';
1088: 
1089:         if (Quform::isNonEmptyString($this->form->config('errorsIcon'))) {
1090:             $output .= sprintf('<span class="quform-error-icon"><i class="%s"></i></span>', Quform::escape($this->form->config('errorsIcon')));
1091:         }
1092: 
1093:         $output .= sprintf('<span class="quform-error-text">%s</span>', $this->getError());
1094: 
1095:         $output .= '</div></div>';
1096: 
1097:         return $output;
1098:     }
1099: 
1100:     /**
1101:      * Render the CSS for this field
1102:      *
1103:      * @param   array   $context
1104:      * @return  string
1105:      */
1106:     protected function renderCss(array $context = array())
1107:     {
1108:         $css = '';
1109: 
1110:         if ($context['labelPosition'] == 'left' && $context['labelWidth'] != '150px') {
1111:             $css .= sprintf('.quform-labels-left.quform-element-%1$s > .quform-spacer > .quform-label, .quform-rtl .quform-labels-left.quform-element-%1$s > .quform-spacer > .quform-inner { width: %2$s; }', $this->getIdentifier(), Quform::addCssUnit($context['labelWidth']));
1112:             $css .= sprintf('.quform-labels-left.quform-element-%1$s > .quform-spacer > .quform-inner { margin-left: %2$s; }', $this->getIdentifier(), Quform::addCssUnit($context['labelWidth']));
1113:             $css .= sprintf('.quform-rtl .quform-labels-left.quform-element-%1$s > .quform-spacer > .quform-inner { margin-right: %2$s; margin-left: 0; }', $this->getIdentifier(), Quform::addCssUnit($context['labelWidth']));
1114:         }
1115: 
1116:         $css .= parent::renderCss($context);
1117: 
1118:         return $css;
1119:     }
1120: 
1121:     /**
1122:      * Get the list of CSS selectors
1123:      *
1124:      * @return array
1125:      */
1126:     protected function getCssSelectors()
1127:     {
1128:         return array(
1129:             'element' => '%s .quform-element-%s',
1130:             'elementSpacer' => '%s .quform-element-%s > .quform-spacer',
1131:             'elementLabel' => '%s .quform-label-%s',
1132:             'elementLabelText' => '%s .quform-label-%s > label',
1133:             'elementRequiredText' => '%s .quform-label-%s > label > .quform-required',
1134:             'elementInner' => '%s .quform-inner-%s',
1135:             'elementInput' => '%s .quform-input-%s',
1136:             'elementText' => '%s .quform-field-%s',
1137:             'elementTextHover' => '%s .quform-field-%s:hover',
1138:             'elementTextFocus' => '%1$s .quform-field-%2$s:focus, %1$s .quform-field-%2$s:active',
1139:             'elementTextarea' => '%s .quform-field-%s',
1140:             'elementTextareaHover' => '%s .quform-field-%s:hover',
1141:             'elementTextareaFocus' => '%1$s .quform-field-%2$s:focus, %1$s .quform-field-%2$s:active',
1142:             'elementSelect' => '%s .quform-field-%s',
1143:             'elementSelectHover' => '%s .quform-field-%s:hover',
1144:             'elementSelectFocus' => '%1$s .quform-field-%2$s:focus, %1$s .quform-field-%2$s:active',
1145:             'elementIcon' => '%s .quform-field-icon',
1146:             'elementIconHover' => '%s .quform-field-icon:hover',
1147:             'elementSubLabel' => '%s .quform-element-%s .quform-sub-label',
1148:             'elementDescription' => '%s .quform-element-%s .quform-description'
1149:         );
1150:     }
1151: 
1152:     /**
1153:      * Does the given logic rule match the current value?
1154:      *
1155:      * This is overridden in child classes
1156:      *
1157:      * @param   array  $rule
1158:      * @return  bool
1159:      */
1160:     public function isLogicRuleMatch(array $rule)
1161:     {
1162:         return $this->isLogicValueMatch($this->getValue(), $rule);
1163:     }
1164: 
1165:     /**
1166:      * Does the given logic rule match the given value?
1167:      *
1168:      * @param   mixed  $value
1169:      * @param   array  $rule
1170:      * @return  bool
1171:      */
1172:     protected function isLogicValueMatch($value, array $rule)
1173:     {
1174:         switch ($rule['operator']) {
1175:             case 'eq':
1176:                 return $value === $rule['value'];
1177:             case 'neq':
1178:                 return $value !== $rule['value'];
1179:             case 'empty':
1180:                 return $value === $this->getEmptyValue();
1181:             case 'not_empty':
1182:                 return $value !== $this->getEmptyValue();
1183:             case 'gt':
1184:                 return is_numeric($value) && is_numeric($rule['value']) && (float) $value > (float) $rule['value'];
1185:             case 'lt':
1186:                 return is_numeric($value) && is_numeric($rule['value']) && (float) $value < (float) $rule['value'];
1187:             case 'contains':
1188:                 return preg_match('/' . preg_quote($rule['value'], '/') . '/', $value);
1189:             case 'starts_with':
1190:                 return preg_match('/^' . preg_quote($rule['value'], '/') . '/', $value);
1191:             case 'ends_with':
1192:                 return preg_match('/' . preg_quote($rule['value'], '/') . '$/', $value);
1193:         }
1194: 
1195:         return false;
1196:     }
1197: }
1198: 
API documentation generated by ApiGen