Manager.php
657 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | |
| 4 | class TCMP_Manager { |
| 5 | public function __construct() { |
| 6 | } |
| 7 | public function init() { |
| 8 | add_action('wp_ajax_TCMP_changeOrder', array(&$this, 'changeOrder')); |
| 9 | } |
| 10 | public function isLimitReached($notice=TRUE) { |
| 11 | global $tcmp; |
| 12 | $cnt=$this->codesCount(); |
| 13 | $result=($cnt>=TCMP_SNIPPETS_LIMIT); |
| 14 | if ($result && $notice) { |
| 15 | $tcmp->Options->pushWarningMessage('SnippetsLimitReached', TCMP_SNIPPETS_LIMIT, TCMP_PAGE_PREMIUM); |
| 16 | } elseif($notice && $cnt>0) { |
| 17 | $tcmp->Options->pushSuccessMessage('SnippetsLimitNotice', $cnt, TCMP_SNIPPETS_LIMIT, TCMP_PAGE_PREMIUM); |
| 18 | } |
| 19 | return $result; |
| 20 | } |
| 21 | public function changeOrder() { |
| 22 | global $tcmp; |
| 23 | if(!isset($_POST['order'])) { |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | $data=array(); |
| 28 | parse_str(TCMP_SQS('order'), $data); |
| 29 | |
| 30 | if (isset($data['row'])) { |
| 31 | $snippets=$this->values(); |
| 32 | foreach($snippets as $id=>$v) { |
| 33 | $v['order']=0; |
| 34 | $snippets[$id]=$v; |
| 35 | } |
| 36 | |
| 37 | $index=1; |
| 38 | foreach($data['row'] as $order=>$id) { |
| 39 | $v=$snippets[$id]; |
| 40 | $v['order']=$index; |
| 41 | $snippets[$id]=$v; |
| 42 | ++$index; |
| 43 | } |
| 44 | |
| 45 | foreach($snippets as $id=>$v) { |
| 46 | $this->put($id, $v); |
| 47 | } |
| 48 | } |
| 49 | echo 'OK'; |
| 50 | wp_die(); |
| 51 | } |
| 52 | |
| 53 | public function matchDeviceType($snippet) { |
| 54 | global $tcmp; |
| 55 | $deviceType=$tcmp->Utils->get($snippet, 'deviceType', FALSE); |
| 56 | $deviceType=$tcmp->Utils->toArray($deviceType); |
| 57 | if($deviceType===FALSE || count($deviceType)==0) { |
| 58 | return TRUE; |
| 59 | } |
| 60 | |
| 61 | $detect=new TCMP_Mobile_Detect(); |
| 62 | if ($detect->isMobile()) { |
| 63 | $type=TCMP_DEVICE_TYPE_MOBILE; |
| 64 | } elseif($detect->isTablet()){ |
| 65 | $type=TCMP_DEVICE_TYPE_TABLET; |
| 66 | } else { //if(!$detect->isMobile() && !$detect->isTablet()) { |
| 67 | $type=TCMP_DEVICE_TYPE_DESKTOP; |
| 68 | } |
| 69 | |
| 70 | $result=FALSE; |
| 71 | if(in_array(TCMP_DEVICE_TYPE_ALL, $deviceType) || in_array($type, $deviceType)) { |
| 72 | $result=TRUE; |
| 73 | } |
| 74 | return $result; |
| 75 | } |
| 76 | public function isModeScript($snippet) { |
| 77 | global $tcmp; |
| 78 | $result=$tcmp->Utils->iget($snippet, 'trackMode', 0); |
| 79 | return ($result==0); |
| 80 | } |
| 81 | public function isModeConversion($snippet) { |
| 82 | global $tcmp; |
| 83 | $result=$tcmp->Utils->iget($snippet, 'trackMode', 0); |
| 84 | return ($result!=0); |
| 85 | } |
| 86 | public function isPageEverywhere($snippet) { |
| 87 | global $tcmp; |
| 88 | if(!$this->isModeScript($snippet)) { |
| 89 | return FALSE; |
| 90 | } |
| 91 | |
| 92 | $result=$tcmp->Utils->iget($snippet, 'trackPage', 0); |
| 93 | return ($result==TCMP_TRACK_PAGE_ALL); |
| 94 | } |
| 95 | public function isPageSpecific($snippet) { |
| 96 | global $tcmp; |
| 97 | if(!$this->isModeScript($snippet)) { |
| 98 | return FALSE; |
| 99 | } |
| 100 | |
| 101 | $result=$tcmp->Utils->iget($snippet, 'trackPage', 0); |
| 102 | return ($result==TCMP_TRACK_PAGE_SPECIFIC); |
| 103 | } |
| 104 | |
| 105 | public function exists($name) { |
| 106 | $snippets=$this->values(); |
| 107 | $result=NULL; |
| 108 | $name=strtoupper($name); |
| 109 | if (isset($snippets[$name])) { |
| 110 | $result=$snippets[$name]; |
| 111 | } |
| 112 | return $result; |
| 113 | } |
| 114 | |
| 115 | //get a code snippet |
| 116 | public function get($id, $new=FALSE) { |
| 117 | global $tcmp; |
| 118 | |
| 119 | $snippet=$tcmp->Options->getSnippet($id); |
| 120 | if (!$snippet && $new) { |
| 121 | $snippet=array(); |
| 122 | $snippet['active']=1; |
| 123 | $snippet['trackMode']=-1; |
| 124 | $snippet['trackPage']=-1; |
| 125 | } |
| 126 | |
| 127 | $snippet=$this->sanitize($id, $snippet); |
| 128 | return $snippet; |
| 129 | } |
| 130 | |
| 131 | public function sanitize($id, $snippet) { |
| 132 | global $tcmp; |
| 133 | if($snippet==NULL || !is_array($snippet)) return NULL; |
| 134 | |
| 135 | $page=0; |
| 136 | if(isset($snippet['includeEverywhereActive'])) { |
| 137 | $page=(intval($snippet['includeEverywhereActive']==1) ? 0 : 1); |
| 138 | } |
| 139 | $defaults=array( |
| 140 | 'id'=>$id |
| 141 | , 'active'=>0 |
| 142 | , 'name'=>'' |
| 143 | , 'code'=>'' |
| 144 | , 'order'=>1000 |
| 145 | , 'position'=>TCMP_POSITION_HEAD |
| 146 | , 'trackMode'=>TCMP_TRACK_MODE_CODE |
| 147 | , 'trackPage'=>$page |
| 148 | , 'includeEverywhereActive'=>0 |
| 149 | , 'includeCategoriesActive'=>0 |
| 150 | , 'includeCategories'=>array() |
| 151 | , 'includeTagsActive'=>0 |
| 152 | , 'includeTags'=>array() |
| 153 | , 'exceptCategoriesActive'=>0 |
| 154 | , 'exceptCategories'=>array() |
| 155 | , 'exceptTagsActive'=>0 |
| 156 | , 'exceptTags'=>array() |
| 157 | , 'deviceType'=>TCMP_DEVICE_TYPE_ALL |
| 158 | ); |
| 159 | |
| 160 | $types=$tcmp->Utils->query(TCMP_QUERY_POST_TYPES); |
| 161 | foreach($types as $v) { |
| 162 | $defaults['includePostsOfType_'.$v['id'].'_Active']=0; |
| 163 | $defaults['includePostsOfType_'.$v['id']]=array(); |
| 164 | $defaults['exceptPostsOfType_'.$v['id'].'_Active']=0; |
| 165 | $defaults['exceptPostsOfType_'.$v['id']]=array(); |
| 166 | } |
| 167 | |
| 168 | $types=$tcmp->Utils->query(TCMP_QUERY_CONVERSION_PLUGINS); |
| 169 | foreach($types as $v) { |
| 170 | //CP stands for ConversionTrackingCode |
| 171 | //$defaults['CTC_'.$v['id'].'_Active']=0; |
| 172 | $defaults['CTC_'.$v['id'].'_ProductsIds']=array(); |
| 173 | $defaults['CTC_'.$v['id'].'_CategoriesIds']=array(); |
| 174 | $defaults['CTC_'.$v['id'].'_TagsIds']=array(); |
| 175 | } |
| 176 | $snippet=$tcmp->Utils->parseArgs($snippet, $defaults); |
| 177 | |
| 178 | foreach ($snippet as $k => $v) { |
| 179 | if (stripos($k, 'active') !== FALSE) { |
| 180 | $snippet[$k]=intval($v); |
| 181 | } elseif (is_array($v)) { |
| 182 | switch ($k) { |
| 183 | /* |
| 184 | case 'includePostsTypes': |
| 185 | case 'excludePostsTypes': |
| 186 | //keys are string and not number |
| 187 | $result=$this->uarray($snippet, $k, FALSE); |
| 188 | break; |
| 189 | */ |
| 190 | default: |
| 191 | //keys are number |
| 192 | $result=$this->uarray($snippet, $k, TRUE); |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | $snippet['code']=trim($snippet['code']); |
| 198 | $snippet['position']=intval($snippet['position']); |
| 199 | if($snippet['trackMode']==='') { |
| 200 | $snippet['trackMode']=TCMP_TRACK_MODE_CODE; |
| 201 | } else { |
| 202 | $snippet['trackMode']=intval($snippet['trackMode']); |
| 203 | } |
| 204 | if($snippet['trackPage']==='') { |
| 205 | $snippet['trackPage']=$page; |
| 206 | } else { |
| 207 | $snippet['trackPage']=intval($snippet['trackPage']); |
| 208 | } |
| 209 | |
| 210 | $snippet['includeEverywhereActive']=0; |
| 211 | if($snippet['trackPage']==TCMP_TRACK_PAGE_ALL) { |
| 212 | $snippet['includeEverywhereActive']=1; |
| 213 | } |
| 214 | |
| 215 | $code=strtolower($snippet['code']); |
| 216 | $cnt=substr_count($code, '<iframe')+substr_count($code, '<script'); |
| 217 | if($cnt<=0) { |
| 218 | $cnt=1; |
| 219 | } |
| 220 | $snippet['codesCount']=$cnt; |
| 221 | return $snippet; |
| 222 | } |
| 223 | private function uarray($snippet, $key, $isInteger=TRUE) { |
| 224 | $array=$snippet[$key]; |
| 225 | if (!is_array($array)) { |
| 226 | $array=explode(',', $array); |
| 227 | } |
| 228 | |
| 229 | if ($isInteger) { |
| 230 | for ($i=0; $i < count($array); $i++) { |
| 231 | $array[$i]=intval($array[$i]); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | $array=array_unique($array); |
| 236 | $snippet[$key]=$array; |
| 237 | return $snippet; |
| 238 | } |
| 239 | |
| 240 | //add or update a snippet (html tracking code) |
| 241 | public function put($id, $snippet) { |
| 242 | global $tcmp; |
| 243 | |
| 244 | if ($id == '' || intval($id) <= 0) { |
| 245 | //if is a new code create a new unique id |
| 246 | $id=$this->getLastId() + 1; |
| 247 | $snippet['id']=$id; |
| 248 | } |
| 249 | $snippet=$this->sanitize($id, $snippet); |
| 250 | $tcmp->Options->setSnippet($id, $snippet); |
| 251 | |
| 252 | $keys=$this->keys(); |
| 253 | if (is_array($keys) && !in_array($id, $keys)) { |
| 254 | $keys[]=$id; |
| 255 | $this->keys($keys); |
| 256 | } |
| 257 | return $snippet; |
| 258 | } |
| 259 | |
| 260 | //remove the id snippet |
| 261 | public function remove($id) { |
| 262 | global $tcmp; |
| 263 | $tcmp->Options->removeSnippet($id); |
| 264 | $keys=$this->keys(); |
| 265 | $result=FALSE; |
| 266 | if (is_array($keys) && in_array($id, $keys)) { |
| 267 | $keys=array_diff($keys, array($id)); |
| 268 | $this->keys($keys); |
| 269 | $result=TRUE; |
| 270 | } |
| 271 | return $result; |
| 272 | } |
| 273 | |
| 274 | //verify if match with this snippet |
| 275 | private function matchSnippet($postId, $postType, $categoriesIds, $tagsIds, $prefix, $snippet) { |
| 276 | global $tcmp; |
| 277 | if(!$this->matchDeviceType($snippet)) { |
| 278 | return FALSE; |
| 279 | } |
| 280 | |
| 281 | $include=FALSE; |
| 282 | $postId=intval($postId); |
| 283 | if($postId>0) { |
| 284 | $what=$prefix.'PostsOfType_'.$postType; |
| 285 | if(!$include && isset($snippet[$what.'_Active']) && isset($snippet[$what])&& $snippet[$what.'_Active'] && $tcmp->Utils->inAllArray($postId, $snippet[$what])) { |
| 286 | $tcmp->Log->debug('MATCH=%s SNIPPET=%s[%s] DUE TO POST=%s OF TYPE=%s IN [%s]' |
| 287 | , $prefix, $snippet['id'], $snippet['name'], $postId, $postType, $snippet[$what]); |
| 288 | $include=TRUE; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return $include; |
| 293 | } |
| 294 | |
| 295 | public function writeCodes($position) { |
| 296 | global $tcmp; |
| 297 | |
| 298 | $text=''; |
| 299 | switch ($position) { |
| 300 | case TCMP_POSITION_HEAD: |
| 301 | $text='HEAD'; |
| 302 | break; |
| 303 | case TCMP_POSITION_BODY: |
| 304 | $text='BODY'; |
| 305 | break; |
| 306 | case TCMP_POSITION_FOOTER: |
| 307 | $text='FOOTER'; |
| 308 | break; |
| 309 | case TCMP_POSITION_CONVERSION: |
| 310 | $text='CONVERSION'; |
| 311 | break; |
| 312 | } |
| 313 | |
| 314 | $post=$tcmp->Options->getPostShown(); |
| 315 | $args=array('field'=>'code'); |
| 316 | $codes=$tcmp->Manager->getCodes($position, $post, $args); |
| 317 | if(is_array($codes) && count($codes)>0) { |
| 318 | ob_start(); |
| 319 | echo "\n<!--BEGIN: TRACKING CODE MANAGER BY INTELLYWP.COM IN $text//-->"; |
| 320 | foreach($codes as $v) { |
| 321 | echo "\n$v"; |
| 322 | } |
| 323 | echo "\n<!--END: https://wordpress.org/plugins/tracking-code-manager IN $text//-->"; |
| 324 | $text=ob_get_contents(); |
| 325 | ob_end_clean(); |
| 326 | |
| 327 | $purchase=$tcmp->Options->getEcommercePurchase(); |
| 328 | if($purchase!==FALSE && intval($tcmp->Options->getLicenseSiteCount())>0) { |
| 329 | //retrieve user data |
| 330 | $purchase->userId=intval($purchase->userId); |
| 331 | if($purchase->userId>0) { |
| 332 | $user=get_user_by('id', $purchase->userId); |
| 333 | if(!is_null($user) && $user!==FALSE && get_class($user)=='WP_User') { |
| 334 | /* @var $user WP_User */ |
| 335 | $purchase->email=$user->user_email; |
| 336 | $purchase->fullname=$user->user_firstname; |
| 337 | if($user->user_lastname!='') { |
| 338 | $purchase->fullname.=' '.$user->user_lastname; |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | $purchase->total=floatval($purchase->total); |
| 344 | $purchase->amount=floatval($purchase->amount); |
| 345 | $purchase->tax=floatval($purchase->tax); |
| 346 | |
| 347 | $fields=array( |
| 348 | 'ORDERID'=>$purchase->orderId |
| 349 | , 'CURRENCY'=>$purchase->currency |
| 350 | , 'FULLNAME'=>$purchase->fullname |
| 351 | , 'EMAIL'=>$purchase->email |
| 352 | , 'PRODUCTS'=>$purchase->products |
| 353 | , 'AMOUNT'=>$purchase->amount |
| 354 | , 'TOTAL'=>$purchase->total |
| 355 | , 'TAX'=>$purchase->tax |
| 356 | ); |
| 357 | |
| 358 | $sep='@@'; |
| 359 | $buffer=''; |
| 360 | $previous=0; |
| 361 | $start=strpos($text, $sep); |
| 362 | if($start===FALSE) { |
| 363 | $buffer=$text; |
| 364 | } else { |
| 365 | while($start!==FALSE) { |
| 366 | $buffer.=$tcmp->Utils->substr($text, $previous, $start); |
| 367 | $end=strpos($text, $sep, $start+strlen($sep)); |
| 368 | if($end!==FALSE) { |
| 369 | $code=$tcmp->Utils->substr($text, $start+strlen($sep), $end); |
| 370 | $code=$tcmp->Utils->toArray($code); |
| 371 | if(count($code)==1) { |
| 372 | $code[]=''; |
| 373 | } |
| 374 | |
| 375 | $v=FALSE; |
| 376 | if(isset($fields[$code[0]])) { |
| 377 | $v=$fields[$code[0]]; |
| 378 | } |
| 379 | if(is_null($v) || $v===FALSE) { |
| 380 | $v=$code[1]; |
| 381 | } |
| 382 | if(is_numeric($v)) { |
| 383 | $v=floatval($v); |
| 384 | $v=round($v, 2); |
| 385 | switch ($code[0]) { |
| 386 | case 'TOTAL': |
| 387 | case 'AMOUNT': |
| 388 | case 'TAX': |
| 389 | $v=number_format($v, 2, '.', ''); |
| 390 | break; |
| 391 | default: |
| 392 | $v=intval($v); |
| 393 | break; |
| 394 | } |
| 395 | } elseif(is_array($v)) { |
| 396 | $a=''; |
| 397 | foreach($v as $t) { |
| 398 | $t=str_replace(',', '', $t); |
| 399 | if($a!='') { |
| 400 | $a.=','; |
| 401 | } |
| 402 | $a.=$t; |
| 403 | } |
| 404 | $v=$a; |
| 405 | } |
| 406 | $v=str_replace("'", '', $v); |
| 407 | $v=str_replace('"', '', $v); |
| 408 | $buffer.=$v; |
| 409 | |
| 410 | $previous=$end+strlen($sep); |
| 411 | $start=strpos($text, $sep, $previous); |
| 412 | } else { |
| 413 | $buffer.=$tcmp->Utils->substr($text, $start); |
| 414 | $previous=FALSE; |
| 415 | $start=FALSE; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | if($previous!==FALSE && $previous<strlen($text)) { |
| 420 | $code=$tcmp->Utils->substr($text, $previous); |
| 421 | $buffer.=$code; |
| 422 | } |
| 423 | $text=$buffer; |
| 424 | } |
| 425 | echo $text; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | //return snippets that match with options |
| 430 | public function getConversionSnippets($options=NULL) { |
| 431 | global $tcmp; |
| 432 | |
| 433 | $defaults=array( |
| 434 | 'pluginId'=>0 |
| 435 | , 'categoriesIds'=>array() |
| 436 | , 'productsIds'=>array() |
| 437 | , 'tagsIds'=>array() |
| 438 | ); |
| 439 | $options=$tcmp->Utils->parseArgs($options, $defaults); |
| 440 | |
| 441 | $result=array(); |
| 442 | $pluginId=intval($options['pluginId']); |
| 443 | $values=$this->values(); |
| 444 | |
| 445 | foreach($values as $snippet) { |
| 446 | $snippet['trackMode']=intval($snippet['trackMode']); |
| 447 | if($snippet && $snippet['trackMode']>0 && $snippet['trackMode']==$pluginId) { |
| 448 | $match=FALSE; |
| 449 | |
| 450 | $match=($match || $this->matchConversion($snippet, $pluginId, 'ProductsIds', $options['productsIds'])); |
| 451 | $match=($match && $this->matchDeviceType($snippet)); |
| 452 | if(!$match) { |
| 453 | //no selected so..all match! :) |
| 454 | if(count($snippet['CTC_'.$pluginId.'_ProductsIds'])==0 |
| 455 | && count($snippet['CTC_'.$pluginId.'_CategoriesIds'])==0 |
| 456 | && count($snippet['CTC_'.$pluginId.'_TagsIds'])==0) { |
| 457 | $match=TRUE; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | if($match) { |
| 462 | $result[]=$snippet; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | return $result; |
| 467 | } |
| 468 | private function matchConversion($snippet, $pluginId, $suffix, $currentIds) { |
| 469 | global $tcmp; |
| 470 | |
| 471 | $settingsIds='CTC_'.$pluginId.'_'.$suffix; |
| 472 | if(isset($snippet[$settingsIds])) { |
| 473 | $settingsIds=$snippet[$settingsIds]; |
| 474 | } else { |
| 475 | $settingsIds=array(); |
| 476 | } |
| 477 | |
| 478 | $result=$tcmp->Utils->inAllArray($currentIds, $settingsIds); |
| 479 | return $result; |
| 480 | } |
| 481 | |
| 482 | //from a post retrieve the html code that is needed to insert into the page code |
| 483 | public function getCodes($position, $post, $args=array()) { |
| 484 | global $tcmp; |
| 485 | |
| 486 | $defaults=array('field'=>'code'); |
| 487 | $args=$tcmp->Utils->parseArgs($args, $defaults); |
| 488 | |
| 489 | $postId=0; |
| 490 | $postType='page'; |
| 491 | $tagsIds=array(); |
| 492 | $categoriesIds=array(); |
| 493 | if($post) { |
| 494 | $postId=$tcmp->Utils->get($post, 'ID', FALSE); |
| 495 | if($postId===FALSE) { |
| 496 | $postId=$tcmp->Utils->get($post, 'post_ID'); |
| 497 | } |
| 498 | $postType=$tcmp->Utils->get($post, 'post_type'); |
| 499 | |
| 500 | $options=array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'ids'); |
| 501 | if(isset($post->ID)) { |
| 502 | $tagsIds=wp_get_post_tags($post->ID, $options); |
| 503 | $categoriesIds=wp_get_post_categories($post->ID); |
| 504 | } else { |
| 505 | $tagsIds=array(); |
| 506 | $categoriesIds=array(); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | $tcmp->Options->clearSnippetsWritten(); |
| 511 | if($position==TCMP_POSITION_CONVERSION) { |
| 512 | //write snippets previously appended |
| 513 | $ids=$tcmp->Options->getConversionSnippetIds(); |
| 514 | if($ids!==FALSE && count($ids)>0) { |
| 515 | foreach($ids as $id) { |
| 516 | $snippet=$tcmp->Manager->get($id); |
| 517 | if($snippet) { |
| 518 | $tcmp->Options->pushSnippetWritten($snippet); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | } else { |
| 523 | $snippets=$this->values(); |
| 524 | foreach ($snippets as $v) { |
| 525 | if(!$v || ($position>-1 && $v['position']!=$position) || $v['code']=='' || !$v['active']) { |
| 526 | continue; |
| 527 | } |
| 528 | if ($v['trackMode']!=TCMP_TRACK_MODE_CODE) { |
| 529 | continue; |
| 530 | } |
| 531 | if($tcmp->Options->hasSnippetWritten($v)) { |
| 532 | $tcmp->Log->debug('SKIPPED SNIPPET=%s[%s] DUE TO ALREADY WRITTEN', $v['id'], $v['name']); |
| 533 | continue; |
| 534 | } |
| 535 | |
| 536 | $match=FALSE; |
| 537 | if (!$match && ($v['trackPage']==TCMP_TRACK_PAGE_ALL || $v['includeEverywhereActive'])) { |
| 538 | $tcmp->Log->debug('INCLUDED SNIPPET=%s[%s] DUE TO EVERYWHERE', $v['id'], $v['name']); |
| 539 | $match=TRUE; |
| 540 | } |
| 541 | if(!$match && $postId>0 && $this->matchSnippet($postId, $postType, $categoriesIds, $tagsIds, 'include', $v)) { |
| 542 | $match=TRUE; |
| 543 | } |
| 544 | |
| 545 | if($match && $postId>0) { |
| 546 | if($this->matchSnippet($postId, $postType, $categoriesIds, $tagsIds, 'except', $v)) { |
| 547 | $tcmp->Log->debug('FOUND AT LEAST ON EXCEPT TO EXCLUDE SNIPPET=%s [%s]', $v['id'], $v['name']); |
| 548 | $match=FALSE; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if ($match) { |
| 553 | $tcmp->Options->pushSnippetWritten($v); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | //obtain result as snippets or array of one field (tipically "id") |
| 559 | $result=$tcmp->Options->getSnippetsWritten(); |
| 560 | if ($args['field']!='all') { |
| 561 | $array=array(); |
| 562 | foreach($result as $k=>$v) { |
| 563 | $k=$args['field']; |
| 564 | if(isset($v[$k])) { |
| 565 | $array[]=$v[$k]; |
| 566 | } else { |
| 567 | $tcmp->Log->error('SNIPPET=%s [%s] WITHOUT FIELD=%s', $v['id'], $v['name'], $k); |
| 568 | } |
| 569 | } |
| 570 | $result=$array; |
| 571 | } |
| 572 | return $result; |
| 573 | } |
| 574 | |
| 575 | //ottiene o salva tutte le chiavi dei tracking code utilizzati ordinati per id |
| 576 | public function keys($keys=NULL) { |
| 577 | global $tcmp; |
| 578 | |
| 579 | if (is_array($keys)) { |
| 580 | $tcmp->Options->setSnippetList($keys); |
| 581 | $result=$keys; |
| 582 | } else { |
| 583 | $result=$tcmp->Options->getSnippetList(); |
| 584 | } |
| 585 | |
| 586 | if (!is_array($result)) { |
| 587 | $result=array(); |
| 588 | } else { |
| 589 | sort($result); |
| 590 | } |
| 591 | return $result; |
| 592 | } |
| 593 | |
| 594 | //ottiene il conteggio attuale dei tracking code |
| 595 | public function count() { |
| 596 | $result=count($this->keys()); |
| 597 | return $result; |
| 598 | } |
| 599 | public function codesCount() { |
| 600 | $result=0; |
| 601 | $ids=$this->keys(); |
| 602 | foreach($ids as $id) { |
| 603 | $snippet=$this->get($id); |
| 604 | if($snippet) { |
| 605 | $result+=1; |
| 606 | /* |
| 607 | if($snippet['codesCount']>0) { |
| 608 | $result+=intval($snippet['codesCount']); |
| 609 | } else { |
| 610 | $result+=1; |
| 611 | } |
| 612 | */ |
| 613 | } |
| 614 | } |
| 615 | return $result; |
| 616 | } |
| 617 | public function getLastId() { |
| 618 | $result=0; |
| 619 | $list=$this->keys(); |
| 620 | foreach ($list as $v) { |
| 621 | $v=intval($v); |
| 622 | if ($v>$result) { |
| 623 | $result=$v; |
| 624 | } |
| 625 | } |
| 626 | return $result; |
| 627 | } |
| 628 | |
| 629 | public function values() { |
| 630 | $keys=$this->keys(); |
| 631 | $array=array(); |
| 632 | foreach ($keys as $k) { |
| 633 | $v=$this->get($k); |
| 634 | $array[]=$v; |
| 635 | } |
| 636 | usort($array, array($this, 'values_Compare')); |
| 637 | |
| 638 | $result=array(); |
| 639 | foreach($array as $v) { |
| 640 | $id=$v['id']; |
| 641 | $result[$id]=$v; |
| 642 | } |
| 643 | return $result; |
| 644 | } |
| 645 | public function values_Compare($o1, $o2) { |
| 646 | global $tcmp; |
| 647 | $v1=$tcmp->Utils->iget($o1, 'order', FALSE); |
| 648 | $v2=$tcmp->Utils->iget($o2, 'order', FALSE); |
| 649 | $result=($v1-$v2); |
| 650 | if($result==0) { |
| 651 | $v1=$tcmp->Utils->get($o1, 'name', FALSE); |
| 652 | $v2=$tcmp->Utils->get($o2, 'name', FALSE); |
| 653 | $result=strcasecmp($v1, $v2); |
| 654 | } |
| 655 | return $result; |
| 656 | } |
| 657 | } |