| 1 |
<?php |
| 2 |
namespace MaxButtons; |
| 3 |
|
| 4 |
defined('ABSPATH') or die('No direct access permitted'); |
| 5 |
|
| 6 |
class simpleTemplate |
| 7 |
{ |
| 8 |
private $version = '1.1.1'; |
| 9 |
|
| 10 |
public static function parse($template_file, $object) |
| 11 |
{ |
| 12 |
if ($template_file == '') |
| 13 |
{ |
| 14 |
return false; |
| 15 |
} |
| 16 |
$template = file_get_contents($template_file); |
| 17 |
|
| 18 |
|
| 19 |
$template = static::checkfor($template, $object); |
| 20 |
$template = static::checkif($template, $object); |
| 21 |
$template = static::checkvar($template, $object); |
| 22 |
|
| 23 |
|
| 24 |
return $template; |
| 25 |
} |
| 26 |
|
| 27 |
public static function checkvar($template, $object) |
| 28 |
{ |
| 29 |
preg_match_all('/%%(.*?)%%/im', $template, $matches); |
| 30 |
if(isset($matches[1]) && count($matches[1]) > 0) |
| 31 |
{ |
| 32 |
for($i = 0; $i < count($matches[1]); $i++) |
| 33 |
{ |
| 34 |
$match = $matches[1][$i]; |
| 35 |
$replace = $matches[0][$i]; |
| 36 |
|
| 37 |
if (! isset($object->$match)) |
| 38 |
continue; |
| 39 |
|
| 40 |
$template = str_replace($replace, $object->$match, $template); |
| 41 |
} |
| 42 |
} |
| 43 |
return $template; |
| 44 |
} |
| 45 |
|
| 46 |
public static function checkif($template, $object) |
| 47 |
{ |
| 48 |
$count = preg_match_all('/{if:(.*?)}(.*){\/if:(\1)}/i', $template, $matches); |
| 49 |
|
| 50 |
if (! isset($matches[0]) || $count == 0) |
| 51 |
return $template; // no statements; |
| 52 |
|
| 53 |
/* matches[0] = full statement |
| 54 |
matches[1] = name of field + possible value |
| 55 |
matches[2] = inner content |
| 56 |
*/ |
| 57 |
|
| 58 |
for($i = 0; $i < $count; $i++) |
| 59 |
{ |
| 60 |
|
| 61 |
$full = $matches[0][$i]; |
| 62 |
$field = $matches[1][$i]; |
| 63 |
$value = null; |
| 64 |
|
| 65 |
if (strstr($field, '=')) // check for value construct |
| 66 |
{ |
| 67 |
list($field, $value) = explode('=', $field); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
$content = $matches[2][$i]; |
| 72 |
|
| 73 |
if (isset($object->$field) && ($value == null || $object->$field == $value) ) |
| 74 |
{ // match |
| 75 |
|
| 76 |
$template = str_replace($full, $content, $template); |
| 77 |
} |
| 78 |
else |
| 79 |
{ // no match |
| 80 |
$template = str_replace($full, '', $template); |
| 81 |
} |
| 82 |
} |
| 83 |
return $template; |
| 84 |
} |
| 85 |
|
| 86 |
public static function checkfor($template, $object) |
| 87 |
{ |
| 88 |
$count = preg_match_all('/\{for:(.*)\}(.*)\{\/for:(.*)\}/isU', $template, $matches); |
| 89 |
|
| 90 |
if (! isset($matches[0]) || $count == 0) |
| 91 |
return $template; // no statements; |
| 92 |
|
| 93 |
for ($i = 0; $i < $count; $i++) |
| 94 |
{ |
| 95 |
$content = ''; |
| 96 |
$field = $matches[1][$i]; |
| 97 |
$repeatline = $matches[2][$i]; |
| 98 |
|
| 99 |
if (isset($object->$field)) |
| 100 |
{ |
| 101 |
foreach($object->$field as $key => $item) |
| 102 |
{ |
| 103 |
if (is_array($item)) |
| 104 |
{ |
| 105 |
|
| 106 |
$line = $repeatline; |
| 107 |
|
| 108 |
foreach($item as $subkey => $subitem) |
| 109 |
{ |
| 110 |
$line = str_replace('%%' . $subkey . '%%',$subitem, $line); |
| 111 |
$line = static::checkif($line, $object); |
| 112 |
} |
| 113 |
$line = str_replace('%%key%%', $key, $line); |
| 114 |
$content .= $line; |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
$line = str_replace('%%key%%',$key, $repeatline); |
| 119 |
|
| 120 |
$line = static::checkif($line, $object); // check if statements in the loop. |
| 121 |
$content .= str_replace('%%item%%', $item, $line); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
$template = str_replace($matches[0][$i], $content, $template); |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
return $template; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
} |
| 135 |
|