1: <?php
  2: 
  3:   4:   5: 
  6: class Quform_Element_Submit extends Quform_Element
  7: {
  8:       9:  10:  11:  12:  13: 
 14:     public function render(array $context = array())
 15:     {
 16:         $context = $this->prepareContext($context);
 17: 
 18:         if ( ! $this->isVisible()) {
 19:             return '';
 20:         }
 21: 
 22:         $output = sprintf('<div class="%s">', Quform::escape(Quform::sanitizeClass($this->getElementClasses($context))));
 23: 
 24:         if ($this->form->hasPages()) {
 25:             if ( ! $this->isOnFirstPage() && $this->form->config('backLocation') != 'after') {
 26:                 $output .= $this->getButtonHtml('back', __('Back', 'quform'));
 27:             }
 28: 
 29:             if ( ! $this->isOnLastPage()) {
 30:                 $output .= $this->getButtonHtml('next', __('Next', 'quform'));
 31:             } else {
 32:                 $output .= $this->getButtonHtml('submit', __('Send', 'quform'));
 33:             }
 34: 
 35:             if ( ! $this->isOnFirstPage() && $this->form->config('backLocation') == 'after') {
 36:                 $output .= $this->getButtonHtml('back', __('Back', 'quform'));
 37:             }
 38: 
 39:         } else {
 40:             $output .= $this->getButtonHtml('submit', __('Send', 'quform'));
 41:         }
 42: 
 43:         $output .= $this->getLoadingHtml();
 44: 
 45:         $output .= '</div>';
 46: 
 47:         return $output;
 48:     }
 49: 
 50:      51:  52:  53:  54:  55: 
 56:     protected function getElementClasses(array $context = array())
 57:     {
 58:         $classes = array(
 59:             'quform-element',
 60:             'quform-element-submit',
 61:             sprintf('quform-element-%s', $this->getIdentifier()),
 62:             'quform-cf'
 63:         );
 64: 
 65:         if (Quform::isNonEmptyString($context['buttonStyle'])) {
 66:             $classes[] = sprintf('quform-button-style-%s', $context['buttonStyle']);
 67:         }
 68: 
 69:         if (Quform::isNonEmptyString($context['buttonSize'])) {
 70:             $classes[] = sprintf('quform-button-size-%s', $context['buttonSize']);
 71:         }
 72: 
 73:         if (Quform::isNonEmptyString($context['buttonWidth']) && $context['buttonWidth'] != 'custom') {
 74:             $classes[] = sprintf('quform-button-width-%s', $context['buttonWidth']);
 75:         }
 76: 
 77:         if (Quform::isNonEmptyString($this->config('customElementClass'))) {
 78:             $classes[] = $this->config('customElementClass');
 79:         }
 80: 
 81:         $classes = apply_filters('quform_element_classes', $classes, $this, $context);
 82:         $classes = apply_filters('quform_element_classes_' . $this->getIdentifier(), $classes, $this, $context);
 83: 
 84:         return $classes;
 85:     }
 86: 
 87:      88:  89:  90:  91:  92:  93: 
 94:     protected function getButtonHtml($which, $defaultText)
 95:     {
 96:         if ($this->config($which . 'Type') == 'inherit') {
 97:             $config = array(
 98:                 'type' => $this->form->config($which . 'Type'),
 99:                 'text' => $this->form->config($which . 'Text'),
100:                 'icon' => $this->form->config($which . 'Icon'),
101:                 'iconPosition' => $this->form->config($which . 'IconPosition'),
102:                 'image' => $this->form->config($which . 'Image'),
103:                 'html' => $this->form->config($which . 'Html'),
104:             );
105:         } else {
106:             $config = array(
107:                 'type' => $this->config($which . 'Type'),
108:                 'text' => $this->config($which . 'Text'),
109:                 'icon' => $this->config($which . 'Icon'),
110:                 'iconPosition' => $this->config($which . 'IconPosition') == 'inherit' ? $this->form->config($which . 'IconPosition') : $this->config($which . 'IconPosition'),
111:                 'image' => $this->config($which . 'Image'),
112:                 'html' => $this->config($which . 'Html'),
113:             );
114:         }
115: 
116:         $classes = array(
117:             sprintf('quform-button-%s', $which),
118:             sprintf('quform-button-%s-%s', $which, $config['type']),
119:             sprintf('quform-button-%s-%s', $which, $this->getIdentifier())
120:         );
121: 
122:         if ($config['type'] == 'default' && Quform::isNonEmptyString($config['icon'])) {
123:             $classes[] = sprintf('quform-button-icon-%s', $config['iconPosition']);
124:         }
125: 
126:         $output = sprintf('<div class="%s"%s>',
127:             esc_attr(join(' ', $classes)),
128:             Quform::isNonEmptyString($this->form->config('buttonAnimation')) ? sprintf(' data-animation="%s"', esc_attr($this->form->config('buttonAnimation'))) : ''
129:         );
130: 
131:         $output .= '<button name="quform_submit" type="submit" class="quform-' . esc_attr($which) . '" value="' . ($which == 'back' ? 'back' : 'submit') . '">';
132: 
133:         if ($config['type'] == 'default') {
134:             $text = sprintf(
135:                 '<span class="%s">%s</span>',
136:                 esc_attr(sprintf('quform-button-text quform-button-%s-text', $which)),
137:                 esc_html(Quform::isNonEmptyString($config['text']) ? $config['text'] : $defaultText)
138:             );
139: 
140:             if (Quform::isNonEmptyString($config['icon'])) {
141:                 $icon = sprintf(
142:                     '<span class="%s"><i class="%s"></i></span>',
143:                     esc_attr(sprintf('quform-button-icon quform-button-%s-icon', $which)),
144:                     esc_attr($config['icon'])
145:                 );
146: 
147:                 if ($config['iconPosition'] == 'right') {
148:                     $output .= $text . $icon;
149:                 } else {
150:                     $output .= $icon . $text;
151:                 }
152:             } else {
153:                 $output .= $text;
154:             }
155:         } elseif ($config['type'] == 'image') {
156:             $output .= '<img src="' . esc_url($config['image']) . '" alt="' . esc_attr($which) . '">';
157:         } elseif ($config['type'] == 'html') {
158:             $output .= do_shortcode($config['html']);
159:         }
160: 
161:         $output .= '</button></div>';
162: 
163:         return $output;
164:     }
165: 
166:     167: 168: 169: 170: 
171:     protected function getLoadingHtml()
172:     {
173:         if ( ! Quform::isNonEmptyString($this->form->config('loadingType')) || $this->form->config('loadingPosition') == 'over-form') {
174:             return '';
175:         }
176: 
177:         $classes = array(
178:             'quform-loading',
179:             sprintf('quform-loading-position-%s', $this->form->config('loadingPosition'))
180:         );
181: 
182:         if ($this->config('loadingType') != 'custom') {
183:             $classes[] = sprintf('quform-loading-type-%s', $this->form->config('loadingType'));
184:         }
185: 
186:         $output = sprintf('<div class="%s">', esc_attr(join(' ', $classes)));
187: 
188:         $output .= '<div class="quform-loading-inner">';
189: 
190:         if ($this->form->config('loadingType') == 'custom') {
191:             $output .= do_shortcode($this->form->config('loadingCustom'));
192:         } else {
193:             $output .= '<div class="quform-loading-spinner"><div class="quform-loading-spinner-inner"></div></div>';
194:         }
195: 
196:         $output .= '</div></div>';
197: 
198:         return $output;
199:     }
200: 
201:     202: 203: 204: 205: 
206:     public function isOnFirstPage()
207:     {
208:         return $this->isOnPage($this->form->getFirstPage());
209:     }
210: 
211:     212: 213: 214: 215: 
216:     public function isOnLastPage()
217:     {
218:         return $this->isOnPage($this->form->getLastPage());
219:     }
220: 
221:     222: 223: 224: 225: 226: 
227:     protected function isOnPage($page)
228:     {
229:         if ($page instanceof Quform_Element_Page) {
230:             foreach ($page->getRecursiveIterator(RecursiveIteratorIterator::SELF_FIRST) as $child) {
231:                 if ($child->getName() == $this->getName()) {
232:                     return true;
233:                 }
234:             }
235:         }
236: 
237:         return false;
238:     }
239: 
240:     241: 242: 243: 244: 245: 
246:     protected function renderCss(array $context = array())
247:     {
248:         $css = parent::renderCss($context);
249: 
250:         if ($context['buttonWidth'] == 'custom' && Quform::isNonEmptyString($context['buttonWidthCustom'])) {
251:             $css .= sprintf('.quform-element-submit.quform-element-%s button { width: %s; }', $this->getIdentifier(), Quform::addCssUnit($context['buttonWidthCustom']));
252:         }
253: 
254:         return $css;
255:     }
256: 
257:     258: 259: 260: 261: 
262:     protected function getCssSelectors()
263:     {
264:         return array(
265:             'submit' => '%s .quform-element-submit.quform-element-%s',
266:             'submitInner' => '%s .quform-button-submit.quform-button-submit-%s',
267:             'submitButton' => '%1$s .quform-button-submit.quform-button-submit-%2$s button, %1$s .quform-element-submit.quform-button-style-theme .quform-button-submit.quform-button-submit-%2$s button',
268:             'submitButtonHover' => '%1$s .quform-button-submit.quform-button-submit-%2$s button:hover, %1$s .quform-element-submit.quform-button-style-theme .quform-button-submit.quform-button-submit-%2$s button:hover',
269:             'submitButtonActive' => '%1$s .quform-button-submit.quform-button-submit-%2$s button:active, %1$s .quform-element-submit.quform-button-style-theme .quform-button-submit.quform-button-submit-%2$s button:active',
270:             'submitButtonText' => '%1$s .quform-button-submit.quform-button-submit-%2$s button .quform-button-text, %1$s .quform-element-submit.quform-button-style-theme .quform-button-submit.quform-button-submit-%2$s button .quform-button-text',
271:             'submitButtonTextHover' => '%1$s .quform-button-submit.quform-button-submit-%2$s button:hover .quform-button-text, %1$s .quform-element-submit.quform-button-style-theme .quform-button-submit.quform-button-submit-%2$s button:hover .quform-button-text',
272:             'submitButtonTextActive' => '%1$s .quform-button-submit.quform-button-submit-%2$s button:active .quform-button-text, %1$s .quform-element-submit.quform-button-style-theme .quform-button-submit.quform-button-submit-%2$s button:active .quform-button-text',
273:             'submitButtonIcon' => '%1$s .quform-button-submit.quform-button-submit-%2$s button .quform-button-icon, %1$s .quform-element-submit.quform-button-style-theme .quform-button-submit.quform-button-submit-%2$s button .quform-button-icon',
274:             'submitButtonIconHover' => '%1$s .quform-button-submit.quform-button-submit-%2$s button:hover .quform-button-icon, %1$s .quform-element-submit.quform-button-style-theme .quform-button-submit.quform-button-submit-%2$s button:hover .quform-button-icon',
275:             'submitButtonIconActive' => '%1$s .quform-button-submit.quform-button-submit-%2$s button:active .quform-button-icon, %1$s .quform-element-submit.quform-button-style-theme .quform-button-submit.quform-button-submit-%2$s button:active .quform-button-icon',
276:             'backInner' => '%s .quform-button-back.quform-button-back-%s',
277:             'backButton' => '%1$s .quform-button-back.quform-button-back-%2$s button, %1$s .quform-element-submit.quform-button-style-theme .quform-button-back.quform-button-back-%2$s button',
278:             'backButtonHover' => '%1$s .quform-button-back.quform-button-back-%2$s button:hover, %1$s .quform-element-submit.quform-button-style-theme .quform-button-back.quform-button-back-%2$s button:hover',
279:             'backButtonActive' => '%1$s .quform-button-back.quform-button-back-%2$s button:active, %1$s .quform-element-submit.quform-button-style-theme .quform-button-back.quform-button-back-%2$s button:active',
280:             'backButtonText' => '%1$s .quform-button-back.quform-button-back-%2$s button .quform-button-text, %1$s .quform-element-submit.quform-button-style-theme .quform-button-back.quform-button-back-%2$s button .quform-button-text',
281:             'backButtonTextHover' => '%1$s .quform-button-back.quform-button-back-%2$s button:hover .quform-button-text, %1$s .quform-element-submit.quform-button-style-theme .quform-button-back.quform-button-back-%2$s button:hover .quform-button-text',
282:             'backButtonTextActive' => '%1$s .quform-button-back.quform-button-back-%2$s button:active .quform-button-text, %1$s .quform-element-submit.quform-button-style-theme .quform-button-back.quform-button-back-%2$s button:active .quform-button-text',
283:             'backButtonIcon' => '%1$s .quform-button-back.quform-button-back-%2$s button .quform-button-icon, %1$s .quform-element-submit.quform-button-style-theme .quform-button-back.quform-button-back-%2$s button .quform-button-icon',
284:             'backButtonIconHover' => '%1$s .quform-button-back.quform-button-back-%2$s button:hover .quform-button-icon, %1$s .quform-element-submit.quform-button-style-theme .quform-button-back.quform-button-back-%2$s button:hover .quform-button-icon',
285:             'backButtonIconActive' => '%1$s .quform-button-back.quform-button-back-%2$s button:active .quform-button-icon, %1$s .quform-element-submit.quform-button-style-theme .quform-button-back.quform-button-back-%2$s button:active .quform-button-icon',
286:             'nextInner' => '%s .quform-button-next.quform-button-next-%s',
287:             'nextButton' => '%1$s .quform-button-next.quform-button-next-%2$s button, %1$s .quform-element-submit.quform-button-style-theme .quform-button-next.quform-button-next-%2$s button',
288:             'nextButtonHover' => '%1$s .quform-button-next.quform-button-next-%2$s button:hover, %1$s .quform-element-submit.quform-button-style-theme .quform-button-next.quform-button-next-%2$s button:hover',
289:             'nextButtonActive' => '%1$s .quform-button-next.quform-button-next-%2$s button:active, %1$s .quform-element-submit.quform-button-style-theme .quform-button-next.quform-button-next-%2$s button:active',
290:             'nextButtonText' => '%1$s .quform-button-next.quform-button-next-%2$s button .quform-button-text, %1$s .quform-element-submit.quform-button-style-theme .quform-button-next.quform-button-next-%2$s button .quform-button-text',
291:             'nextButtonTextHover' => '%1$s .quform-button-next.quform-button-next-%2$s button:hover .quform-button-text, %1$s .quform-element-submit.quform-button-style-theme .quform-button-next.quform-button-next-%2$s button:hover .quform-button-text',
292:             'nextButtonTextActive' => '%1$s .quform-button-next.quform-button-next-%2$s button:active .quform-button-text, %1$s .quform-element-submit.quform-button-style-theme .quform-button-next.quform-button-next-%2$s button:active .quform-button-text',
293:             'nextButtonIcon' => '%1$s .quform-button-next.quform-button-next-%2$s button .quform-button-icon, %1$s .quform-element-submit.quform-button-style-theme .quform-button-next.quform-button-next-%2$s button .quform-button-icon',
294:             'nextButtonIconHover' => '%1$s .quform-button-next.quform-button-next-%2$s button:hover .quform-button-icon, %1$s .quform-element-submit.quform-button-style-theme .quform-button-next.quform-button-next-%2$s button:hover .quform-button-icon',
295:             'nextButtonIconActive' => '%1$s .quform-button-next.quform-button-next-%2$s button:active .quform-button-icon, %1$s .quform-element-submit.quform-button-style-theme .quform-button-next.quform-button-next-%2$s button:active .quform-button-icon'
296:         );
297:     }
298: 
299:     300: 301: 302: 303: 304: 
305:     public static function getDefaultConfig($key = null)
306:     {
307:         $config = apply_filters('quform_default_config_submit', array(
308:             
309:             'label' => __('Submit', 'quform'),
310:             'submitType' => 'inherit',
311:             'submitText' => '',
312:             'submitIcon' => '',
313:             'submitIconPosition' => 'inherit',
314:             'submitImage' => '',
315:             'submitHtml' => '',
316:             'nextType' => 'inherit',
317:             'nextText' => '',
318:             'nextIcon' => '',
319:             'nextIconPosition' => 'inherit',
320:             'nextImage' => '',
321:             'nextHtml' => '',
322:             'backType' => 'inherit',
323:             'backText' => '',
324:             'backIcon' => '',
325:             'backIconPosition' => 'inherit',
326:             'backImage' => '',
327:             'backHtml' => '',
328: 
329:             
330:             'buttonStyle' => 'inherit',
331:             'buttonSize' => 'inherit',
332:             'buttonWidth' => 'inherit',
333:             'buttonWidthCustom' => '',
334:             'customElementClass' => '',
335:             'styles' => array(),
336: 
337:             
338:             'logicEnabled' => false,
339:             'logicAction' => true,
340:             'logicMatch' => 'all',
341:             'logicRules' => array(),
342: 
343:             
344:             'visibility' => ''
345:         ));
346: 
347:         $config['type'] = 'submit';
348: 
349:         if (Quform::isNonEmptyString($key)) {
350:             return Quform::get($config, $key);
351:         }
352: 
353:         return $config;
354:     }
355: }
356: