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