Overview

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
  • Overview
  • Class
  1: <?php
  2: 
  3: /**
  4:  * @copyright Copyright (c) 2009-2022 ThemeCatcher (https://www.themecatcher.net)
  5:  */
  6: abstract class Quform_Element
  7: {
  8:     /**
  9:      * Element ID
 10:      * @var int
 11:      */
 12:     protected $id;
 13: 
 14:     /**
 15:      * The form this element belongs to
 16:      * @var Quform_Form
 17:      */
 18:     protected $form;
 19: 
 20:     /**
 21:      * Is the element conditionally hidden?
 22:      * @var boolean
 23:      */
 24:     protected $isConditionallyHidden = false;
 25: 
 26:     /**
 27:      * Is the element a child of a non-visible container?
 28:      * @var boolean
 29:      */
 30:     protected $hasNonVisibleAncestor = false;
 31: 
 32:     /**
 33:      * Element config storage
 34:      * @var array
 35:      */
 36:     protected $config = array();
 37: 
 38:     /**
 39:      * Element unique ID
 40:      * @var string
 41:      */
 42:     protected $uniqueId = '';
 43: 
 44:     /**
 45:      * @param  int          $id
 46:      * @param  Quform_Form  $form
 47:      */
 48:     public function __construct($id, Quform_Form $form)
 49:     {
 50:         $this->id = $id;
 51:         $this->form = $form;
 52:     }
 53: 
 54:     /**
 55:      * Render the element and return the HTML
 56:      *
 57:      * @param   array   $context
 58:      * @return  string
 59:      */
 60:     abstract public function render(array $context = array());
 61: 
 62:     /**
 63:      * Should this element be visible in the form?
 64:      *
 65:      * @return bool
 66:      */
 67:     public function isVisible()
 68:     {
 69:         if ($this->hasNonVisibleAncestor) {
 70:             return false;
 71:         }
 72: 
 73:         $visible = true;
 74: 
 75:         switch ($this->config('visibility')) {
 76:             case 'admin-only':
 77:                 if ( ! in_array($this->form->config('environment'), array('viewEntry', 'editEntry', 'listEntry'))) {
 78:                     $visible = false;
 79:                 }
 80:                 break;
 81:             case 'logged-in-only':
 82:                 if ($this->form->config('environment') == 'frontend' && ! is_user_logged_in()) {
 83:                     $visible = false;
 84:                 }
 85:                 break;
 86:             case 'logged-out-only':
 87:                 if ($this->form->config('environment') == 'frontend' && is_user_logged_in()) {
 88:                     $visible = false;
 89:                 }
 90:                 break;
 91:         }
 92: 
 93:         $visible = apply_filters('quform_element_visible', $visible, $this, $this->form);
 94:         $visible = apply_filters('quform_element_visible_' . $this->getIdentifier(), $visible, $this, $this->form);
 95: 
 96:         return $visible;
 97:     }
 98: 
 99:     /**
100:      * Set the ID of the element
101:      *
102:      * @param int $id
103:      */
104:     public function setId($id)
105:     {
106:         $this->id = $id;
107:     }
108: 
109:     /**
110:      * Get the ID of the element
111:      *
112:      * @return int
113:      */
114:     public function getId()
115:     {
116:         return $this->id;
117:     }
118: 
119:     /**
120:      * Set the form the element belongs to
121:      *
122:      * @param Quform_Form $form
123:      */
124:     public function setForm(Quform_Form $form)
125:     {
126:         $this->form = $form;
127:     }
128: 
129:     /**
130:      * Get the form the element belongs to
131:      *
132:      * @return Quform_Form
133:      */
134:     public function getForm()
135:     {
136:         return $this->form;
137:     }
138: 
139:     /**
140:      * Returns the config value for the given $key
141:      *
142:      * If the value is null, the default will be returned
143:      *
144:      * @param   string|null  $key      The config key
145:      * @param   null|mixed   $default  The default value to return if the value key not exist
146:      * @return  mixed                  The config value or $default if not set
147:      */
148:     public function config($key = null, $default = null)
149:     {
150:         $value = Quform::get($this->config, $key, $default);
151: 
152:         if ($value === null) {
153:             $value = Quform::get(call_user_func(array(get_class($this), 'getDefaultConfig')), $key, $default);
154:         }
155: 
156:         return $value;
157:     }
158: 
159:     /**
160:      * Set the config value for the given $key or multiple values using an array
161:      *
162:      * @param   string|array  $key    Key or array of key/values
163:      * @param   mixed         $value  Value or null if $key is array
164:      * @return  $this
165:      */
166:     public function setConfig($key, $value = null)
167:     {
168:         if (is_array($key)) {
169:             foreach ($key as $k => $v) {
170:                 $this->config[$k] = $v;
171:             }
172:         } else {
173:             $this->config[$key] = $value;
174:         }
175: 
176:         return $this;
177:     }
178: 
179:     /**
180:      * @param   string  $key
181:      * @param   string  $default
182:      * @return  string
183:      */
184:     public function getTranslation($key, $default = '')
185:     {
186:         $string = $this->config($key);
187: 
188:         if (Quform::isNonEmptyString($string)) {
189:             return $string;
190:         }
191: 
192:         return $default;
193:     }
194: 
195:     /**
196:      * Get the name of the element
197:      *
198:      * @return string
199:      */
200:     public function getName()
201:     {
202:         return sprintf('quform_%s', $this->getIdentifier());
203:     }
204: 
205:     /**
206:      * Get the element identifier (e.g. 1_1)
207:      *
208:      * @return string
209:      */
210:     public function getIdentifier()
211:     {
212:         return sprintf('%d_%d', $this->form->getId(), $this->getId());
213:     }
214: 
215:     /**
216:      * @param string $uniqueId
217:      */
218:     public function setUniqueId($uniqueId)
219:     {
220:         $this->uniqueId = $uniqueId;
221:     }
222: 
223:     /**
224:      * @return string
225:      */
226:     public function getUniqueId()
227:     {
228:         return $this->uniqueId;
229:     }
230: 
231:     /**
232:      * Set whether the element is hidden by the conditional logic rules
233:      *
234:      * @param boolean $flag
235:      */
236:     public function setConditionallyHidden($flag)
237:     {
238:         $this->isConditionallyHidden = $flag;
239:     }
240: 
241:     /**
242:      * Get whether the element is hidden by the conditional logic rules
243:      *
244:      * @return boolean
245:      */
246:     public function isConditionallyHidden()
247:     {
248:         return $this->isConditionallyHidden;
249:     }
250: 
251:     /**
252:      * Get whether the element is hidden or not
253:      *
254:      * @return boolean
255:      */
256:     public function isHidden()
257:     {
258:         return $this->isConditionallyHidden();
259:     }
260: 
261:     /**
262:      * Set whether the element is a child of a non-visible container
263:      *
264:      * @param boolean $flag
265:      */
266:     public function setHasNonVisibleAncestor($flag)
267:     {
268:         $this->hasNonVisibleAncestor = $flag;
269:     }
270: 
271:     /**
272:      * Get whether the element is a child of a non-visible container
273:      *
274:      * @return boolean
275:      */
276:     public function hasNonVisibleAncestor()
277:     {
278:         return $this->hasNonVisibleAncestor;
279:     }
280: 
281:     /**
282:      * Get the CSS for this element
283:      *
284:      * @param   array   $context
285:      * @return  string
286:      */
287:     public function getCss(array $context = array())
288:     {
289:         $context = $this->prepareContext($context);
290: 
291:         return $this->renderCss($context);
292:     }
293: 
294:     /**
295:      * Render the CSS for this element
296:      *
297:      * Override in sub classes to add element-specific CSS
298:      *
299:      * @param   array   $context
300:      * @return  string
301:      */
302:     protected function renderCss(array $context = array())
303:     {
304:         $css = '';
305: 
306:         if (is_array($this->config('styles'))) {
307:             foreach ($this->config('styles') as $style) {
308:                 $selector = $this->getCssSelector($style['type']);
309: 
310:                 if (Quform::isNonEmptyString($selector) && Quform::isNonEmptyString($style['css'])) {
311:                     $css .= sprintf('%s { %s }', $selector, $style['css']);
312:                 }
313:             }
314:         }
315: 
316:         return $css;
317:     }
318: 
319:     /**
320:      * Get the CSS selectors for this element
321:      *
322:      * Override in sub classes to add element-specific CSS selectors
323:      *
324:      * @return array
325:      */
326:     protected function getCssSelectors()
327:     {
328:         return array();
329:     }
330: 
331:     /**
332:      * Get the CSS selector for the given style type
333:      *
334:      * @param   string  $type
335:      * @return  string
336:      */
337:     protected function getCssSelector($type)
338:     {
339:         $selector = '';
340:         $selectors = $this->getCssSelectors();
341: 
342:         if (array_key_exists($type, $selectors)) {
343:             $prefix = sprintf('.quform-%d', $this->form->getId());
344:             $selector = sprintf($selectors[$type], $prefix, $this->getIdentifier());
345:         }
346: 
347:         return $selector;
348:     }
349: 
350:     /**
351:      * Inherit settings from this element into the context
352:      *
353:      * @param   array  $context
354:      * @return  array
355:      */
356:     protected function prepareContext(array $context = array())
357:     {
358:         if (is_string($this->config('fieldSize')) && $this->config('fieldSize') != 'inherit') {
359:             $context['fieldSize'] = $this->config('fieldSize');
360:         }
361: 
362:         if (is_string($this->config('fieldWidth')) && $this->config('fieldWidth') != 'inherit') {
363:             $context['fieldWidth'] = $this->config('fieldWidth');
364: 
365:             if ($this->config('fieldWidth') == 'custom' && Quform::isNonEmptyString($this->config('fieldWidthCustom'))) {
366:                 $context['fieldWidthCustom'] = $this->config('fieldWidthCustom');
367:             }
368:         }
369: 
370:         if (is_string($this->config('buttonStyle')) && $this->config('buttonStyle') != 'inherit') {
371:             $context['buttonStyle'] = $this->config('buttonStyle');
372:         }
373: 
374:         if (is_string($this->config('buttonSize')) && $this->config('buttonSize') != 'inherit') {
375:             $context['buttonSize'] = $this->config('buttonSize');
376:         }
377: 
378:         if (is_string($this->config('buttonWidth')) && $this->config('buttonWidth') != 'inherit') {
379:             $context['buttonWidth'] = $this->config('buttonWidth');
380: 
381:             if ($this->config('buttonWidth') == 'custom' && Quform::isNonEmptyString($this->config('buttonWidthCustom'))) {
382:                 $context['buttonWidthCustom'] = $this->config('buttonWidthCustom');
383:             }
384:         }
385: 
386:         if (is_string($this->config('labelPosition')) && $this->config('labelPosition') != 'inherit') {
387:             $context['labelPosition'] = $this->config('labelPosition');
388: 
389:             if ($this->config('labelPosition') == 'left' && Quform::isNonEmptyString($this->config('labelWidth'))) {
390:                 $context['labelWidth'] = $this->config('labelWidth');
391:             }
392:         }
393: 
394:         if (Quform::isNonEmptyString($this->config('tooltipType')) && $this->config('tooltipType') != 'inherit') {
395:             $context['tooltipType'] = $this->config('tooltipType');
396:         }
397: 
398:         if (Quform::isNonEmptyString($this->config('tooltipEvent')) && $this->config('tooltipEvent') != 'inherit') {
399:             $context['tooltipEvent'] = $this->config('tooltipEvent');
400:         }
401: 
402:         return $context;
403:     }
404: }
405: 
API documentation generated by ApiGen