admin
11 years ago
actions.php
11 years ago
class-TCM-check.php
11 years ago
class-TCM-cron.php
11 years ago
class-TCM-form.php
11 years ago
class-TCM-language.php
11 years ago
class-TCM-logger.php
11 years ago
class-TCM-manager.php
11 years ago
class-TCM-options.php
11 years ago
class-TCM-tracking.php
11 years ago
class-TCM-utils.php
11 years ago
core.php
11 years ago
install.php
11 years ago
uninstall.php
11 years ago
class-TCM-form.php
298 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Created by PhpStorm. |
| 4 | * User: alessio |
| 5 | * Date: 28/03/2015 |
| 6 | * Time: 10:20 |
| 7 | */ |
| 8 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 9 | |
| 10 | class TCM_Form { |
| 11 | var $prefix='Form'; |
| 12 | var $labels=TRUE; |
| 13 | var $leftLabels=TRUE; |
| 14 | var $newline; |
| 15 | |
| 16 | var $leftTags=FALSE; |
| 17 | |
| 18 | public function __construct() { |
| 19 | } |
| 20 | |
| 21 | //args can be a string or an associative array if you want |
| 22 | private function parseArgs($args, $defaults) { |
| 23 | $result=$args; |
| 24 | if(is_array($result) && count($result)>0) { |
| 25 | $result=''; |
| 26 | foreach($args as $k=>$v) { |
| 27 | $result.=' '.$k.'="'.$v.'"'; |
| 28 | } |
| 29 | } elseif(!$args) { |
| 30 | $result=''; |
| 31 | } |
| 32 | if(is_array($defaults) && count($defaults)>0) { |
| 33 | foreach($defaults as $k=>$v) { |
| 34 | if(stripos($result, $k.'=')===FALSE) { |
| 35 | $result.=' '.$k.'="'.$v.'"'; |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | return $result; |
| 40 | } |
| 41 | |
| 42 | public function label($name, $args='') { |
| 43 | global $tcm; |
| 44 | $defaults=array('class'=>''); |
| 45 | $other=$this->parseArgs($args, $defaults); |
| 46 | |
| 47 | $k=$this->prefix.'.'.$name; |
| 48 | $label=$tcm->Lang->L($k); |
| 49 | |
| 50 | //check if is a mandatory field by checking the .txt language file |
| 51 | $k=$this->prefix.'.'.$name.'.check'; |
| 52 | if($tcm->Lang->H($k)) { |
| 53 | $label.=' (*)'; |
| 54 | } |
| 55 | |
| 56 | $aClass=''; |
| 57 | ?> |
| 58 | <label for="<?php echo $name?>" <?php echo $other?> > |
| 59 | <span style="float:left; margin-right:5px;" class="<?php echo $aClass?>"><?php echo $label?></span> |
| 60 | </label> |
| 61 | <?php } |
| 62 | |
| 63 | public function leftInput($name, $args='') { |
| 64 | if(!$this->labels) return; |
| 65 | if($this->leftLabels) { |
| 66 | $this->label($name, $args); |
| 67 | } |
| 68 | |
| 69 | if($this->newline) { |
| 70 | $this->newline(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public function newline() { |
| 75 | ?><div class="tcm-form-newline"></div><?php |
| 76 | } |
| 77 | |
| 78 | public function rightInput($name, $args='') { |
| 79 | if(!$this->labels) return; |
| 80 | if (!$this->leftLabels) { |
| 81 | $this->label($name, $args); |
| 82 | } |
| 83 | $this->newline(); |
| 84 | } |
| 85 | |
| 86 | public function formStarts($method='post', $action='', $args=NULL) { |
| 87 | //$defaults=array('style'=>'margin:1em 0; padding:1px 1em; background:#fff; border:1px solid #ccc;' |
| 88 | $defaults=array('class'=>'tcm-form'); |
| 89 | $other=$this->parseArgs($args, $defaults); |
| 90 | ?> |
| 91 | <form method="<?php echo $method?>" action="<?php echo $action?>" <?php echo $other?> > |
| 92 | <?php } |
| 93 | |
| 94 | public function formEnds() { ?> |
| 95 | </form> |
| 96 | <?php } |
| 97 | |
| 98 | public function divStarts($args=array()) { |
| 99 | $defaults=array(); |
| 100 | $other=$this->parseArgs($args, $defaults); |
| 101 | ?> |
| 102 | <div <?php echo $other?>> |
| 103 | <?php } |
| 104 | public function divEnds() { ?> |
| 105 | </div> |
| 106 | <div style="clear:both;"></div> |
| 107 | <?php } |
| 108 | |
| 109 | public function p($message, $v1=NULL, $v2=NULL, $v3=NULL, $v4=NULL, $v5=NULL) { |
| 110 | global $tcm; |
| 111 | ?> |
| 112 | <p style="font-weight:bold;"> |
| 113 | <?php |
| 114 | $tcm->Lang->P($message, $v1, $v2, $v3, $v4, $v5); |
| 115 | if($tcm->Lang->H($message.'Subtitle')) { ?> |
| 116 | <br/> |
| 117 | <span style="font-weight:normal;"> |
| 118 | <?php $tcm->Lang->P($message.'Subtitle', $v1, $v2, $v3, $v4, $v5)?> |
| 119 | </span> |
| 120 | <?php } ?> |
| 121 | </p> |
| 122 | <?php } |
| 123 | |
| 124 | public function textarea($name, $value='', $args=NULL) { |
| 125 | if(is_array($value) && isset($value[$name])) { |
| 126 | $value=$value[$name]; |
| 127 | } |
| 128 | $defaults=array('rows'=>10, 'class'=>'tcm-textarea'); |
| 129 | $other=$this->parseArgs($args, $defaults); |
| 130 | |
| 131 | $args=array('class'=>'tcm-label', 'style'=>'width:auto;'); |
| 132 | $this->newline=TRUE; |
| 133 | $this->leftInput($name, $args); |
| 134 | ?> |
| 135 | <textarea dir="ltr" dirname="ltr" id="<?php echo $name ?>" name="<?php echo $name?>" <?php echo $other?> ><?php echo $value ?></textarea> |
| 136 | <?php |
| 137 | $this->newline=FALSE; |
| 138 | $this->rightInput($name, $args); |
| 139 | } |
| 140 | |
| 141 | public function text($name, $value='', $args=NULL) { |
| 142 | if(is_array($value) && isset($value[$name])) { |
| 143 | $value=$value[$name]; |
| 144 | } |
| 145 | $defaults=array('class'=>'tcm-text'); |
| 146 | $other=$this->parseArgs($args, $defaults); |
| 147 | |
| 148 | $args=array('class'=>'tcm-label'); |
| 149 | $this->leftInput($name, $args); |
| 150 | ?> |
| 151 | <input type="text" id="<?php echo $name ?>" name="<?php echo $name ?>" value="<?php echo $value ?>" <?php echo $other?> /> |
| 152 | <?php |
| 153 | $this->rightInput($name, $args); |
| 154 | } |
| 155 | |
| 156 | public function hidden($name, $value='', $args=NULL) { |
| 157 | if(is_array($value) && isset($value[$name])) { |
| 158 | $value=$value[$name]; |
| 159 | } |
| 160 | $defaults=array(); |
| 161 | $other=$this->parseArgs($args, $defaults); |
| 162 | ?> |
| 163 | <input type="hidden" id="<?php echo $name ?>" name="<?php echo $name ?>" value="<?php echo $value ?>" <?php echo $other?> /> |
| 164 | <?php } |
| 165 | |
| 166 | public function nonce($action=-1, $name='_wpnonce', $referer=true, $echo=true) { |
| 167 | wp_nonce_field($action, $name, $referer, $echo); |
| 168 | } |
| 169 | |
| 170 | public function select($name, $value, $options, $multiple, $args=NULL) { |
| 171 | global $tcm; |
| 172 | if(is_array($value) && isset($value[$name])) { |
| 173 | $value=$value[$name]; |
| 174 | } |
| 175 | $defaults=array('class'=>'tcm-select tcmTags'); |
| 176 | $other=$this->parseArgs($args, $defaults); |
| 177 | |
| 178 | if(!is_array($value)) { |
| 179 | $value=array($value); |
| 180 | } |
| 181 | if(is_string($options)) { |
| 182 | $options=explode(',', $options); |
| 183 | } |
| 184 | if(is_array($options) && count($options)>0) { |
| 185 | if(!isset($options[0]['id'])) { |
| 186 | //this is a normal array so I use the values for "id" field and the "name" into the txt file |
| 187 | $temp=array(); |
| 188 | foreach($options as $v) { |
| 189 | $temp[]=array('id'=>$v, 'name'=>$tcm->Lang->L($this->prefix.'.'.$name.'.'.$v)); |
| 190 | } |
| 191 | $options=$temp; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | $args=array('class'=>'tcm-label'); |
| 196 | $this->leftInput($name, $args); |
| 197 | ?> |
| 198 | <select id="<?php echo $name ?>" name="<?php echo $name?><?php echo ($multiple ? '[]' : '')?>" <?php echo ($multiple ? 'multiple' : '')?> <?php echo $other?> > |
| 199 | <?php |
| 200 | foreach($options as $v) { |
| 201 | $selected=''; |
| 202 | if(in_array($v['id'], $value)) { |
| 203 | $selected=' selected="selected"'; |
| 204 | } |
| 205 | ?> |
| 206 | <option value="<?php echo $v['id']?>" <?php echo $selected?>><?php echo $v['name']?></option> |
| 207 | <?php } ?> |
| 208 | </select> |
| 209 | <?php |
| 210 | $this->rightInput($name, $args); |
| 211 | } |
| 212 | |
| 213 | public function submit($value='', $args=NULL) { |
| 214 | global $tcm; |
| 215 | $defaults=array(); |
| 216 | $other=$this->parseArgs($args, $defaults); |
| 217 | if($value=='') { |
| 218 | $value='Send'; |
| 219 | } |
| 220 | $this->newline(); |
| 221 | ?> |
| 222 | <input type="submit" class="button-primary tcm-button tcm-submit" value="<?php $tcm->Lang->P($value)?>" <?php echo $other?>/> |
| 223 | <?php } |
| 224 | |
| 225 | public function delete($id, $action='delete', $args=NULL) { |
| 226 | global $tcm; |
| 227 | $defaults=array(); |
| 228 | $other=$this->parseArgs($args, $defaults); |
| 229 | ?> |
| 230 | <input type="button" class="button tcm-button" value="<?php $tcm->Lang->P('Delete?')?>" onclick="if (confirm('<?php $tcm->Lang->P('Are you sure you want to delete?')?>') ) window.location='<?php echo TCM_TAB_MANAGER_URI?>&action=<?php echo $action?>&id=<?php echo $id ?>&tcm_nonce=<?php echo esc_attr(wp_create_nonce('tcm_delete')); ?>';" <?php echo $other?> /> |
| 231 | |
| 232 | <?php |
| 233 | } |
| 234 | |
| 235 | public function checkbox($name, $current=1, $value=1, $args=NULL) { |
| 236 | global $tcm; |
| 237 | if(is_array($current) && isset($current[$name])) { |
| 238 | $current=$current[$name]; |
| 239 | } |
| 240 | $defaults=array('class'=>'tcm-checkbox', 'style'=>'margin:0px; margin-right:4px;'); |
| 241 | $other=$this->parseArgs($args, $defaults); |
| 242 | $prev=$this->leftLabels; |
| 243 | $this->leftLabels=FALSE; |
| 244 | |
| 245 | $args=array('class'=>'', 'style'=>'margin-top:-1px;'); |
| 246 | $this->leftInput($name, $args); |
| 247 | ?> |
| 248 | <input type="checkbox" id="<?php echo $name ?>" name="<?php echo $name?>" value="<?php echo $value?>" <?php echo($current!='' && $current==$value ? 'checked' : '') ?> <?php echo $other?> > |
| 249 | <?php |
| 250 | $this->rightInput($name, $args); |
| 251 | $this->leftLabels=$prev; |
| 252 | } |
| 253 | |
| 254 | public function checkText($nameActive, $nameText, $value) { |
| 255 | global $tcm; |
| 256 | |
| 257 | $args=array('class'=>'tcm-hideShow tcm-checkbox' |
| 258 | , 'tcm-hideIfTrue'=>'false' |
| 259 | , 'tcm-hideShow'=>$nameText.'Text'); |
| 260 | $this->checkbox($nameActive, $value, 1, $args); |
| 261 | ?> |
| 262 | <div id="<?php echo $nameText?>Text" style="float:left;"> |
| 263 | <?php |
| 264 | $prev=$this->labels; |
| 265 | $this->labels=FALSE; |
| 266 | $args=array(); |
| 267 | $this->text($nameText, $value, $args); |
| 268 | $this->labels=$prev; |
| 269 | ?> |
| 270 | </div> |
| 271 | <?php } |
| 272 | |
| 273 | //create a checkbox with a left select visible only when the checkbox is selected |
| 274 | public function checkSelect($nameActive, $nameArray, $value, $values) { |
| 275 | global $tcm; |
| 276 | ?> |
| 277 | <div id="<?php echo $nameArray?>Box" style="float:left;"> |
| 278 | <?php |
| 279 | $args=array('class'=>'tcm-hideShow tcm-checkbox' |
| 280 | , 'tcm-hideIfTrue'=>'false' |
| 281 | , 'tcm-hideShow'=>$nameArray.'Tags'); |
| 282 | $this->checkbox($nameActive, $value, 1, $args); |
| 283 | if(TRUE) { ?> |
| 284 | <div id="<?php echo $nameArray?>Tags" style="float:left;"> |
| 285 | <?php |
| 286 | $prev=$this->labels; |
| 287 | $this->labels=FALSE; |
| 288 | $args=array('class'=>'tcm-select tcmLineTags'); |
| 289 | $this->select($nameArray, $value, $values, TRUE, $args); |
| 290 | $this->labels=$prev; |
| 291 | ?> |
| 292 | </div> |
| 293 | <?php } ?> |
| 294 | </div> |
| 295 | <?php |
| 296 | $this->newline(); |
| 297 | } |
| 298 | } |