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