1: <?php
2:
3: 4: 5:
6: abstract class Quform_Element
7: {
8: 9: 10: 11:
12: protected $id;
13:
14: 15: 16: 17:
18: protected $form;
19:
20: 21: 22: 23:
24: protected $isConditionallyHidden = false;
25:
26: 27: 28: 29:
30: protected $hasNonVisibleAncestor = false;
31:
32: 33: 34: 35:
36: protected $config = array();
37:
38: 39: 40: 41:
42: protected $uniqueId = '';
43:
44: 45: 46: 47:
48: public function __construct($id, Quform_Form $form)
49: {
50: $this->id = $id;
51: $this->form = $form;
52: }
53:
54: 55: 56: 57: 58: 59:
60: abstract public function render(array $context = array());
61:
62: 63: 64: 65: 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: 101: 102: 103:
104: public function setId($id)
105: {
106: $this->id = $id;
107: }
108:
109: 110: 111: 112: 113:
114: public function getId()
115: {
116: return $this->id;
117: }
118:
119: 120: 121: 122: 123:
124: public function setForm(Quform_Form $form)
125: {
126: $this->form = $form;
127: }
128:
129: 130: 131: 132: 133:
134: public function getForm()
135: {
136: return $this->form;
137: }
138:
139: 140: 141: 142: 143: 144: 145: 146: 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: 161: 162: 163: 164: 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: 181: 182: 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: 197: 198: 199:
200: public function getName()
201: {
202: return sprintf('quform_%s', $this->getIdentifier());
203: }
204:
205: 206: 207: 208: 209:
210: public function getIdentifier()
211: {
212: return sprintf('%d_%d', $this->form->getId(), $this->getId());
213: }
214:
215: 216: 217:
218: public function setUniqueId($uniqueId)
219: {
220: $this->uniqueId = $uniqueId;
221: }
222:
223: 224: 225:
226: public function getUniqueId()
227: {
228: return $this->uniqueId;
229: }
230:
231: 232: 233: 234: 235:
236: public function setConditionallyHidden($flag)
237: {
238: $this->isConditionallyHidden = $flag;
239: }
240:
241: 242: 243: 244: 245:
246: public function isConditionallyHidden()
247: {
248: return $this->isConditionallyHidden;
249: }
250:
251: 252: 253: 254: 255:
256: public function isHidden()
257: {
258: return $this->isConditionallyHidden();
259: }
260:
261: 262: 263: 264: 265:
266: public function setHasNonVisibleAncestor($flag)
267: {
268: $this->hasNonVisibleAncestor = $flag;
269: }
270:
271: 272: 273: 274: 275:
276: public function hasNonVisibleAncestor()
277: {
278: return $this->hasNonVisibleAncestor;
279: }
280:
281: 282: 283: 284: 285: 286:
287: public function getCss(array $context = array())
288: {
289: $context = $this->prepareContext($context);
290:
291: return $this->renderCss($context);
292: }
293:
294: 295: 296: 297: 298: 299: 300: 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: 321: 322: 323: 324: 325:
326: protected function getCssSelectors()
327: {
328: return array();
329: }
330:
331: 332: 333: 334: 335: 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: 352: 353: 354: 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: