Form.php
489 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class TCMP_Form { |
| 8 | var $prefix = 'Form'; |
| 9 | var $labels = true; |
| 10 | var $left_labels = true; |
| 11 | var $newline; |
| 12 | |
| 13 | var $tags = true; |
| 14 | var $only_premium = true; |
| 15 | var $left_tags = false; |
| 16 | var $premium = false; |
| 17 | var $tag_new = false; |
| 18 | |
| 19 | public function __construct() { |
| 20 | } |
| 21 | |
| 22 | //args can be a string or an associative array if you want |
| 23 | private function get_text_args( $args, $defaults, $excludes = array() ) { |
| 24 | $result = $args; |
| 25 | |
| 26 | if ( is_string( $excludes ) ) { |
| 27 | $excludes = explode( ',', $excludes ); |
| 28 | } |
| 29 | |
| 30 | if ( is_array( $result ) && count( $result ) > 0 ) { |
| 31 | $result = ''; |
| 32 | foreach ( $args as $k => $v ) { |
| 33 | if ( 0 == count( $excludes ) || ! in_array( $k, $excludes, false ) ) { |
| 34 | $result .= ' ' . $k . '="' . $v . '"'; |
| 35 | } |
| 36 | } |
| 37 | } elseif ( ! $args ) { |
| 38 | $result = ''; |
| 39 | } |
| 40 | if ( is_array( $defaults ) && count( $defaults ) > 0 ) { |
| 41 | foreach ( $defaults as $k => $v ) { |
| 42 | if ( 0 == count( $excludes ) || ! in_array( $k, $excludes, false ) ) { |
| 43 | if ( false == stripos( $result, $k . '=' ) ) { |
| 44 | $result .= ' ' . $k . '="' . $v . '"'; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | return $result; |
| 50 | } |
| 51 | |
| 52 | public function tag( $override_premium = false ) { |
| 53 | if ( ! $this->tags || ! $this->tag_new ) { |
| 54 | return; |
| 55 | } |
| 56 | ?> |
| 57 | <div style="float:left;" class="tcmp-tag tcmp-tag-free">NEW!</div> |
| 58 | <?php |
| 59 | } |
| 60 | |
| 61 | public function label( $name, $options = '' ) { |
| 62 | global $tcmp; |
| 63 | $defaults = array( 'class' => '' ); |
| 64 | $other_text = $this->get_text_args( $options, $defaults, array( 'label', 'id' ) ); |
| 65 | |
| 66 | $k = $this->prefix . '.' . $name; |
| 67 | if ( ! is_array( $options ) ) { |
| 68 | $options = array(); |
| 69 | } |
| 70 | if ( isset( $options['label'] ) && $options['label'] ) { |
| 71 | $k = $options['label']; |
| 72 | } |
| 73 | |
| 74 | $label = $tcmp->lang->L( $k ); |
| 75 | $for = ( isset( $options['id'] ) ? $options['id'] : $name ); |
| 76 | |
| 77 | //check if is a mandatory field by checking the .txt language file |
| 78 | $k = $this->prefix . '.' . $name . '.check'; |
| 79 | if ( $tcmp->lang->H( $k ) ) { |
| 80 | $label .= ' (*)'; |
| 81 | } |
| 82 | |
| 83 | $a_class = ''; |
| 84 | |
| 85 | ?> |
| 86 | <label for="<?php echo esc_attr( $for ); ?>" <?php echo wp_kses_post( $other_text ); ?> > |
| 87 | <?php |
| 88 | if ( $this->left_tags ) { |
| 89 | $this->tag(); |
| 90 | } |
| 91 | ?> |
| 92 | <span style="float:left; margin-right:5px;" class="<?php echo esc_attr( $a_class ); ?>"><?php echo wp_kses_post( $label ); ?></span> |
| 93 | <?php |
| 94 | if ( ! $this->left_tags ) { |
| 95 | $this->tag(); |
| 96 | } |
| 97 | ?> |
| 98 | </label> |
| 99 | <?php |
| 100 | } |
| 101 | |
| 102 | public function left_input( $name, $options = '' ) { |
| 103 | if ( ! $this->labels ) { |
| 104 | return; |
| 105 | } |
| 106 | if ( $this->left_labels ) { |
| 107 | $this->label( $name, $options ); |
| 108 | } |
| 109 | |
| 110 | if ( $this->newline ) { |
| 111 | $this->newline(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | public function newline() { |
| 116 | ?> |
| 117 | <div class="tcmp-form-newline"></div> |
| 118 | <?php |
| 119 | } |
| 120 | |
| 121 | public function right_input( $name, $args = '' ) { |
| 122 | if ( ! $this->labels ) { |
| 123 | return; |
| 124 | } |
| 125 | if ( ! $this->left_labels ) { |
| 126 | $this->label( $name, $args ); |
| 127 | } |
| 128 | $this->newline(); |
| 129 | } |
| 130 | |
| 131 | public function form_starts( $method = 'post', $action = '', $args = null ) { |
| 132 | $defaults = array( 'class' => 'tcmp-form' ); |
| 133 | $other = $this->get_text_args( $args, $defaults ); |
| 134 | ?> |
| 135 | <form method="<?php echo esc_attr( $method ); ?>" action="<?php echo esc_attr( $action ); ?>" <?php echo wp_kses( $other, array() ); ?> > |
| 136 | <?php |
| 137 | } |
| 138 | |
| 139 | public function form_ends() { |
| 140 | ?> |
| 141 | </form> |
| 142 | <?php |
| 143 | } |
| 144 | |
| 145 | public function div_starts( $args = array() ) { |
| 146 | $defaults = array(); |
| 147 | $other = $this->get_text_args( $args, $defaults ); |
| 148 | ?> |
| 149 | <div <?php echo wp_kses( $other, array() ); ?>> |
| 150 | <?php |
| 151 | } |
| 152 | public function div_ends() { |
| 153 | ?> |
| 154 | </div> |
| 155 | <div style="clear:both;"></div> |
| 156 | <?php |
| 157 | } |
| 158 | |
| 159 | public function p( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) { |
| 160 | global $tcmp; |
| 161 | ?> |
| 162 | <p style="font-weight:bold;"> |
| 163 | <?php |
| 164 | $tcmp->lang->P( $message, $v1, $v2, $v3, $v4, $v5 ); |
| 165 | if ( $tcmp->lang->H( $message . 'Subtitle' ) ) { |
| 166 | ?> |
| 167 | <br/> |
| 168 | <span style="font-weight:normal;"> |
| 169 | <?php $tcmp->lang->P( $message . 'Subtitle', $v1, $v2, $v3, $v4, $v5 ); ?> |
| 170 | </span> |
| 171 | <?php } ?> |
| 172 | </p> |
| 173 | <?php |
| 174 | } |
| 175 | public function i( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) { |
| 176 | global $tcmp; |
| 177 | ?> |
| 178 | <i><?php $tcmp->lang->P( $message, $v1, $v2, $v3, $v4, $v5 ); ?></i> |
| 179 | <?php |
| 180 | } |
| 181 | |
| 182 | public function editor( $name, $value = '', $options = null ) { |
| 183 | global $tcmp; |
| 184 | |
| 185 | $defaults = array( |
| 186 | 'editor' => 'html', |
| 187 | 'theme' => 'monokai', |
| 188 | 'ui-visible' => '', |
| 189 | 'height' => 350, |
| 190 | 'width' => 700, |
| 191 | ); |
| 192 | $options = $tcmp->utils->parseArgs( $options, $defaults ); |
| 193 | $value = $tcmp->utils->get( $value, $name, $value ); |
| 194 | |
| 195 | $args = array( |
| 196 | 'class' => 'tcmp-label', |
| 197 | 'style' => 'width:auto;', |
| 198 | ); |
| 199 | $this->newline = true; |
| 200 | $this->left_input( $name, $args ); |
| 201 | |
| 202 | $id = $name; |
| 203 | switch ( $options['editor'] ) { |
| 204 | case 'wp': |
| 205 | case 'WordPress': |
| 206 | $settings = array( |
| 207 | 'wpautop' => true, |
| 208 | 'media_buttons' => true, |
| 209 | 'drag_drop_upload' => false, |
| 210 | 'editor_height' => $options['height'], |
| 211 | ); |
| 212 | wp_editor( $value, $id, $settings ); |
| 213 | break; |
| 214 | case 'html': |
| 215 | case 'text': |
| 216 | case 'javascript': |
| 217 | case 'css': |
| 218 | $ace = 'ACE_' . $id; |
| 219 | $text = $value; |
| 220 | $text = str_replace( '<', '<', $text ); |
| 221 | $text = str_replace( '>', '>', $text ); |
| 222 | ?> |
| 223 | <div id="<?php echo esc_attr( $id ); ?>Ace" style="height:<?php echo esc_attr( $options['height'] ) + 50; ?>px; width: <?php echo esc_attr( $options['width'] ); ?>px;"><?php echo esc_html( $text ); ?></div> |
| 224 | <textarea id="<?php echo esc_attr( $id ); ?>" name="<?php echo esc_attr( $id ); ?>" ui-visible="<?php echo esc_attr( $options['ui-visible'] ); ?>" style="display: none;"></textarea> |
| 225 | |
| 226 | <?php |
| 227 | break; |
| 228 | } |
| 229 | $this->newline = false; |
| 230 | $this->right_input( $name, $args ); |
| 231 | } |
| 232 | |
| 233 | public function textarea( $name, $value = '', $args = null ) { |
| 234 | if ( is_array( $value ) && isset( $value[ $name ] ) ) { |
| 235 | $value = $value[ $name ]; |
| 236 | } |
| 237 | $defaults = array( |
| 238 | 'rows' => 10, |
| 239 | 'class' => 'tcmp-textarea', |
| 240 | ); |
| 241 | $other = $this->get_text_args( $args, $defaults ); |
| 242 | |
| 243 | $args = array( |
| 244 | 'class' => 'tcmp-label', |
| 245 | 'style' => 'width:auto;', |
| 246 | ); |
| 247 | $this->newline = true; |
| 248 | $this->left_input( $name, $args ); |
| 249 | ?> |
| 250 | <textarea dir="ltr" dirname="ltr" id="<?php echo esc_attr( $name ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php echo wp_kses( $other, array() ); ?> ><?php echo wp_kses_post( $value ); ?></textarea> |
| 251 | <?php |
| 252 | $this->newline = false; |
| 253 | $this->right_input( $name, $args ); |
| 254 | } |
| 255 | |
| 256 | public function number( $name, $value = '', $options = null ) { |
| 257 | if ( ! $options ) { |
| 258 | $options = array(); |
| 259 | } |
| 260 | $options['type'] = 'number'; |
| 261 | $options['autocomplete'] = 'off'; |
| 262 | $options['style'] = 'width:100px;'; |
| 263 | if ( ! isset( $options['min'] ) ) { |
| 264 | $options['min'] = 0; |
| 265 | } |
| 266 | |
| 267 | return $this->text( $name, $value, $options ); |
| 268 | } |
| 269 | |
| 270 | public function text( $name, $value = '', $options = null ) { |
| 271 | if ( is_array( $value ) && isset( $value[ $name ] ) ) { |
| 272 | $value = $value[ $name ]; |
| 273 | } |
| 274 | |
| 275 | $type = 'text'; |
| 276 | if ( isset( $options['type'] ) ) { |
| 277 | $type = $options['type']; |
| 278 | } |
| 279 | |
| 280 | $defaults = array( 'class' => 'tcmp-' . $type ); |
| 281 | $other = $this->get_text_args( $options, $defaults, 'type' ); |
| 282 | |
| 283 | $args = array( 'class' => 'tcmp-label' ); |
| 284 | $this->left_input( $name, $args ); |
| 285 | ?> |
| 286 | <input type="<?php echo esc_attr( $type ); ?>" id="<?php echo esc_attr( $name ); ?>" name="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $value ); ?>" <?php echo wp_kses( $other, array() ); ?> /> |
| 287 | <?php |
| 288 | $this->right_input( $name, $args ); |
| 289 | } |
| 290 | |
| 291 | public function hidden( $name, $value = '', $args = null ) { |
| 292 | if ( is_array( $value ) && isset( $value[ $name ] ) ) { |
| 293 | $value = $value[ $name ]; |
| 294 | } |
| 295 | $defaults = array(); |
| 296 | $other = $this->get_text_args( $args, $defaults ); |
| 297 | ?> |
| 298 | <input type="hidden" id="<?php echo esc_attr( $name ); ?>" name="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $value ); ?>" <?php echo wp_kses( $other, array() ); ?> /> |
| 299 | <?php |
| 300 | } |
| 301 | |
| 302 | public function nonce( $action = -1, $name = '_wpnonce', $referer = true, $echo = true ) { |
| 303 | wp_nonce_field( $action, $name, $referer, $echo ); |
| 304 | } |
| 305 | |
| 306 | public function dropdown( $name, $value, $options, $multiple = false, $args = null ) { |
| 307 | global $tcmp; |
| 308 | if ( is_array( $value ) && isset( $value[ $name ] ) ) { |
| 309 | $value = $value[ $name ]; |
| 310 | } |
| 311 | $defaults = array( 'class' => 'tcmp-select tcmTags tcmp-dropdown' ); |
| 312 | $other = $this->get_text_args( $args, $defaults ); |
| 313 | |
| 314 | if ( ! is_array( $value ) ) { |
| 315 | $value = array( $value ); |
| 316 | } |
| 317 | if ( is_string( $options ) ) { |
| 318 | $options = explode( ',', $options ); |
| 319 | } |
| 320 | if ( is_array( $options ) && count( $options ) > 0 ) { |
| 321 | if ( ! isset( $options[0]['id'] ) ) { |
| 322 | //this is a normal array so I use the values for "id" field and the "name" into the txt file |
| 323 | $temp = array(); |
| 324 | foreach ( $options as $v ) { |
| 325 | $temp[] = array( |
| 326 | 'id' => $v, |
| 327 | 'name' => $tcmp->lang->L( $this->prefix . '.' . $name . '.' . $v ), |
| 328 | ); |
| 329 | } |
| 330 | $options = $temp; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | echo '<div id="' . esc_attr( $name ) . '-box">'; |
| 335 | $args = array( 'class' => 'tcmp-label' ); |
| 336 | $this->left_input( $name, $args ); |
| 337 | ?> |
| 338 | <select id="<?php echo esc_attr( $name ); ?>" name="<?php echo esc_attr( $name ); ?><?php echo ( $multiple ? '[]' : '' ); ?>" <?php echo ( $multiple ? 'multiple' : '' ); ?> <?php echo wp_kses( $other, array() ); ?> > |
| 339 | <?php |
| 340 | foreach ( $options as $v ) { |
| 341 | $selected = ''; |
| 342 | if ( in_array( $v['id'], $value, false ) ) { |
| 343 | $selected = ' selected="selected"'; |
| 344 | } |
| 345 | ?> |
| 346 | <option value="<?php echo esc_attr( $v['id'] ); ?>" <?php echo wp_kses( $selected, array() ); ?>><?php echo esc_html( $v['name'] ); ?></option> |
| 347 | <?php } ?> |
| 348 | </select> |
| 349 | <?php |
| 350 | $this->right_input( $name, $args ); |
| 351 | echo '</div>'; |
| 352 | } |
| 353 | |
| 354 | public function br() { |
| 355 | ?> |
| 356 | <br/> |
| 357 | <?php |
| 358 | } |
| 359 | |
| 360 | public function submit( $value = '', $args = null ) { |
| 361 | global $tcmp; |
| 362 | $defaults = array(); |
| 363 | $other = $this->get_text_args( $args, $defaults ); |
| 364 | if ( '' == $value ) { |
| 365 | $value = 'Send'; |
| 366 | } |
| 367 | $this->newline(); |
| 368 | ?> |
| 369 | <input type="submit" class="button-primary tcmp-button tcmp-submit" value="<?php $tcmp->lang->P( $value ); ?>" <?php echo wp_kses( $other, array() ); ?>/> |
| 370 | <?php |
| 371 | } |
| 372 | |
| 373 | public function delete( $id, $action = 'delete', $args = null ) { |
| 374 | global $tcmp; |
| 375 | $defaults = array(); |
| 376 | $other = $this->get_text_args( $args, $defaults ); |
| 377 | ?> |
| 378 | <input type="button" class="button tcmp-button" value="<?php $tcmp->lang->P( 'Delete?' ); ?>" onclick="if (confirm('<?php $tcmp->lang->P( 'Question.DeleteQuestion' ); ?>') ) window.location='<?php echo TCMP_TAB_MANAGER_URI; ?>&action=<?php echo esc_attr( $action ); ?>&id=<?php echo esc_attr( $id ); ?>&tcmp_nonce=<?php echo esc_attr( wp_create_nonce( 'tcmp_delete' ) ); ?>';" <?php echo wp_kses( $other, array() ); ?> /> |
| 379 | |
| 380 | <?php |
| 381 | } |
| 382 | |
| 383 | public function radio( $name, $current = 1, $value = 1, $options = null ) { |
| 384 | if ( ! is_array( $options ) ) { |
| 385 | $options = array(); |
| 386 | } |
| 387 | $options['radio'] = true; |
| 388 | $options['id'] = $name . '_' . $value; |
| 389 | return $this->checkbox( $name, $current, $value, $options ); |
| 390 | } |
| 391 | public function checkbox( $name, $current = 1, $value = 1, $options = null ) { |
| 392 | global $tcmp; |
| 393 | if ( is_array( $current ) && isset( $current[ $name ] ) ) { |
| 394 | $current = $current[ $name ]; |
| 395 | } |
| 396 | |
| 397 | if ( ! is_array( $options ) ) { |
| 398 | $options = array(); |
| 399 | } |
| 400 | |
| 401 | $label = $name; |
| 402 | $type = 'checkbox'; |
| 403 | if ( isset( $options['radio'] ) && $options['radio'] ) { |
| 404 | $type = 'radio'; |
| 405 | $label .= '_' . $value; |
| 406 | } |
| 407 | |
| 408 | $defaults = array( |
| 409 | 'class' => 'tcmp-checkbox', |
| 410 | 'style' => 'margin:0px; margin-right:4px;', |
| 411 | 'id' => $name, |
| 412 | ); |
| 413 | $other = $this->get_text_args( $options, $defaults, array( 'radio', 'label' ) ); |
| 414 | $prev = $this->left_labels; |
| 415 | $this->left_labels = false; |
| 416 | |
| 417 | $label = ( isset( $options['label'] ) ? $options['label'] : $this->prefix . '.' . $label ); |
| 418 | $id = ( isset( $options['id'] ) ? $options['id'] : $name ); |
| 419 | $options = array( |
| 420 | 'class' => '', |
| 421 | 'style' => 'margin-top:-1px;', |
| 422 | 'label' => $label, |
| 423 | 'id' => $id, |
| 424 | ); |
| 425 | $this->left_input( $name, $options ); |
| 426 | ?> |
| 427 | <input type="<?php echo esc_attr( $type ); ?>" name="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $value ); ?>" <?php echo( $current == $value ? 'checked="checked"' : '' ); ?> <?php echo wp_kses( $other, array() ); ?> > |
| 428 | <?php |
| 429 | $this->right_input( $name, $options ); |
| 430 | $this->left_labels = $prev; |
| 431 | } |
| 432 | |
| 433 | public function check_text( $name_active, $name_text, $value ) { |
| 434 | global $tcmp; |
| 435 | |
| 436 | $args = array( |
| 437 | 'class' => 'tcmp-hideShow tcmp-checkbox', |
| 438 | 'tcmp-hideIfTrue' => 'false', |
| 439 | 'tcmp-hideShow' => $name_text . 'Text', |
| 440 | ); |
| 441 | $this->checkbox( $name_active, $value, 1, $args ); |
| 442 | if ( $this->premium ) { |
| 443 | return; |
| 444 | } |
| 445 | ?> |
| 446 | <div id="<?php echo esc_attr( $name_text ); ?>Text" style="float:left;"> |
| 447 | <?php |
| 448 | $prev = $this->labels; |
| 449 | $this->labels = false; |
| 450 | $args = array(); |
| 451 | $this->text( $name_text, $value, $args ); |
| 452 | $this->labels = $prev; |
| 453 | ?> |
| 454 | </div> |
| 455 | <?php |
| 456 | } |
| 457 | |
| 458 | //create a checkbox with a left select visible only when the checkbox is selected |
| 459 | public function check_select( $name_active, $name_array, $value, $values, $options = null ) { |
| 460 | global $tcmp; |
| 461 | ?> |
| 462 | <div id="<?php echo esc_attr( $name_array ); ?>Box" style="float:left;"> |
| 463 | <?php |
| 464 | $defaults = array( |
| 465 | 'class' => 'tcmp-hideShow tcmp-checkbox', |
| 466 | 'tcmp-hideIfTrue' => 'false', |
| 467 | 'tcmp-hideShow' => $name_array . 'Tags', |
| 468 | ); |
| 469 | $options = $tcmp->utils->parseArgs( $options, $defaults ); |
| 470 | $this->checkbox( $name_active, $value, 1, $options ); |
| 471 | /*if(!$this->premium || $tcmp->License->hasPremium()) { ?>*/ |
| 472 | if ( true ) { |
| 473 | ?> |
| 474 | <div id="<?php echo esc_attr( $name_array ); ?>Tags" style="float:left;"> |
| 475 | <?php |
| 476 | $prev = $this->labels; |
| 477 | $this->labels = false; |
| 478 | $options = array( 'class' => 'tcmp-select tcmLineTags' ); |
| 479 | $this->dropdown( $name_array, $value, $values, true, $options ); |
| 480 | $this->labels = $prev; |
| 481 | ?> |
| 482 | </div> |
| 483 | <?php } ?> |
| 484 | </div> |
| 485 | <?php |
| 486 | $this->newline(); |
| 487 | } |
| 488 | } |
| 489 |