', '"'), array('\', '<', '>', '"'), $result);
return $result;
}
else if (is_array($_string)) {
$result = array();
foreach ($_string as $key => $string)
$result[$key] = str_replace('&', '&', $result[$key]);
$result[$key] = str_replace(array('<', '>', '"'), array('<', '>', '"'), $string);
return $result;
}
else
return $_string;
}
public static function cleanId($_string, $_separator='-', $_remove_last=true) {
$result = preg_replace('/[^a-zA-Z0-9]+|\s+/', $_separator, strtolower($_string));
if ($_remove_last === true && self::endsWith($result, '-'))
$result = rtrim($result, '-');
return $result;
}
public static function removeBreakLines($_string) {
return preg_replace("/\n\r|\r\n|\n|\r/", '{{ break_line }}', $_string);
}
public static function htmlBreakLines($_string) {
return preg_replace("/\n\r|\r\n|\n|\r/", '
', $_string);
}
public static function stripBreakLinesAndTabs($_string) {
$result = self::stripBreakLines($_string);
return str_replace("\t", '', $result);
}
public static function stripBreakLines($_string, $_flag=true) {
if ($_flag)
return preg_replace("/\n\r|\r\n|\n|\r/", '', $_string);
else
return preg_replace("/(\n\r|\r\n|\n|\r){2,}/", '', $_string);
}
private static $_disabled_magic_quotes_flag = false;
private final static function _disable_magic_quotes() {
if (self::$_disabled_magic_quotes_flag)
return;
if (get_magic_quotes_gpc()) {
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process)) {
foreach ($val as $k => $v) {
unset($process[$key][$k]);
if (is_array($v)) {
$process[$key][stripslashes($k)] = $v;
$process[] = &$process[$key][stripslashes($k)];
} else {
$process[$key][stripslashes($k)] = stripslashes($v);
}
}
}
unset($process);
}
self::$_disabled_magic_quotes_flag = true;
}
public static function getAlternateVariable($_var_names, $_method='POST', $_escape_html=true) {
foreach ($_var_names as $var_name) {
$result = self::getVariable($var_name, $_method, $_escape_html);
if ($result !== false)
break;
}
return $result;
}
public static function getVariableSet($_var_base_name, $_variants=array(), $_discard_empty=true, $_method='POST', $_escape_html=true) {
$result = array();
for ($i=0; $i';
return self::mergeText($_message, $images);
}
public static function mergeText($_message, $_new_values_array) {
$match_array = array();
for ($i = 0; $i < count($_new_values_array); $i++)
$match_array[] = "[$i]";
return str_replace($match_array, $_new_values_array, $_message);
}
public static function mergeArrays($_array_1, $_array_2) {
foreach ($_array_2 as $key => $value) {
if (!is_array($_array_1)) {
continue;
var_dump('Array 1 is not an array!');
var_dump($_array_1);
die();
}
if (!is_array($_array_2)) {
continue;
var_dump('Array 2 is not an array!');
var_dump($_array_2);
die();
}
if (array_key_exists($key, $_array_1) && is_array($value))
$_array_1[$key] = self::mergeArrays($_array_1[$key], $_array_2[$key]);
else
$_array_1[$key] = $value;
}
return $_array_1;
}
public static function chain($primary_field, $parent_field, $sort_field, $rows, $root_id=0, $maxlevel=250000) {
$chain = new PrisnaGWTChain($primary_field, $parent_field, $sort_field, $rows, $root_id, $maxlevel);
return $chain->get();
}
/**
*
* render object methods
*
*/
protected static $_render_object_cache;
protected static function _initialize_template_cache() {
if (!is_array(self::$_render_object_cache))
self::$_render_object_cache = array();
}
protected static function _set_template($_file, $_content) {
self::$_render_object_cache[$_file] = $_content;
}
protected static function _get_template($_file) {
return array_key_exists($_file, self::$_render_object_cache) ? self::$_render_object_cache[$_file] : false;
}
public static function renderObject($_object, $_options=null, $_htmlencode=false) {
self::_initialize_template_cache();
if ($_options['type'] == 'file')
$template = PRISNA_GWT__TEMPLATES . $_options['content'];
else if ($_options['type'] == 'html')
$html = $_options['content'];
else {
var_dump('--------');
print_r($_options);
var_dump('--------');
return 'template type error';
}
if (array_key_exists('meta_tag_rules', $_options))
$meta_tag_rules = $_options['meta_tag_rules'];
else
$meta_tag_rules = null;
if (!is_array($_object)) {
if ($_options['type'] == 'file') {
$result = self::_get_template($template);
if ($result !== false)
self::_set_template($template, $result);
else {
ob_start();
if (is_file($template))
include $template;
else {
echo "$template does not exist!
";
#var_dump('Error: ');
#print_r($_options);
}
$result = ob_get_clean();
}
}
else
$result = $html;
if ($_object != null)
foreach ($_object as $property => $value)
$result = self::stampCustomValue("{{ $property }}", $value, $result, $_htmlencode);
if (is_array($meta_tag_rules))
$result = self::displayHideMetaTags($_object, $meta_tag_rules, $result);
}
else {
$result = '';
foreach ($_object as $single_object) {
$temp_object = is_array($single_object) ? (object) $single_object : $single_object;
$result .= self::renderObject($temp_object, $_options, $_htmlencode);
}
}
return $result;
}
protected static function displayHideMetaTags($_object, $_meta_tag_rules, $_html) {
$result = $_html;
foreach ($_meta_tag_rules as $meta_tag_rule) {
if (array_key_exists('property', $meta_tag_rule))
$_expression = ($_object->{$meta_tag_rule['property']} == $meta_tag_rule['value']);
else if (array_key_exists('expression', $meta_tag_rule))
$_expression = $meta_tag_rule['expression'];
$result = self::displayHideMetaTag($_expression, $meta_tag_rule['tag'], $result);
}
return $result;
}
public static function displayHideMetaTag($_expression, $_tag, $_html) {
if ($_expression) {
$_html = self::displayHideBlock("$_tag.true", $_html, true);
$_html = self::displayHideBlock("$_tag.false", $_html, false);
}
else {
$_html = self::displayHideBlock("$_tag.true", $_html, false);
$_html = self::displayHideBlock("$_tag.false", $_html, true);
}
return $_html;
}
protected static function displayHideBlock($_name, $_html, $_state) {
if ($_state) {
$_names = array (
"{{ $_name:begin }}",
"{{ $_name:end }}"
);
$results = str_replace($_names, '', $_html);
}
else {
$occurrence_ini = strpos($_html, "{{ $_name:begin }}");
$occurrence_end = strpos($_html, "{{ $_name:end }}", $occurrence_ini);
$last_occurrence_ini = 0;
$positions = array ();
$results = $_html;
while ((!PrisnaGWTValidator::isEmpty($occurrence_ini)) && (PrisnaGWTValidator::isInteger($occurrence_ini)) && (!PrisnaGWTValidator::isEmpty($occurrence_end)) && (PrisnaGWTValidator::isInteger($occurrence_end))) {
$positions[] = array (
$occurrence_ini,
$occurrence_end
);
$occurrence_ini = strpos($_html, "{{ $_name:begin }}", $occurrence_end);
$occurrence_end = strpos($_html, "{{ $_name:end }}", $occurrence_ini);
}
$_name_length = strlen("{{ $_name:end }}");
$results = $_html;
rsort($positions);
foreach ($positions as $position) {
$results = substr_replace($results, '', $position[0], $position[1] - $position[0] + $_name_length);
}
}
return $results;
}
public static function stampCustomValue($_tag, $_value, $_html, $_htmlencode=false) {
if (is_string($_value) || is_int($_value) || is_float($_value) || is_null($_value))
$result = str_replace($_tag, $_htmlencode ? utf8_decode($_value) : $_value, $_html);
else
$result = $_html;
return $result;
}
}
class PrisnaGWTUI extends WP_Widget {
public function __construct() {
parent::__construct(PrisnaGWTConfig::getWidgetName(true), PrisnaGWTConfig::getWidgetName(), array(
'description' => sprintf(__('Add the %s.', 'prisna-gwt'), PrisnaGWTConfig::getName(false, true))
));
}
public function form($_instance) {
$display_mode = PrisnaGWTConfig::getSettingValue('display_mode');
$style = $display_mode != 'automatic' ? PrisnaGWTConfig::getSettingValue('style_' . $display_mode) : null;
$class_name = 'prisna_gwt_widget_container_';
if ($display_mode == 'automatic') {
$result = __('Automatic', 'prisna-gwt');
$class_name .= 'text';
}
else {
$style_setting = PrisnaGWTConfig::getSetting('style_' . $display_mode);
$path = $style_setting['values'][$style_setting['value']];
$result = '
';
$class_name .= 'image';
}
if ($display_mode == 'inline') {
$title = isset($_instance['title']) ? $_instance['title'] : '';
$class_name .= ' prisna_gwt_widget_has_title';
echo '';
}
echo '' . $result . '
';
if ($display_mode != 'inline')
return 'noform';
}
protected function _add_class($_html, $_class_name) {
$result = $_html;
$pattern = '/\bclass\=\".*?\"/';
preg_match($pattern, $_html, $matches);
if (empty($matches))
$result = str_replace('>', ' class="' . $_class_name . '">', $result);
else {
$class_attribute = substr($matches[0], 0, -1) . ' ' . $_class_name . '"';
$result = str_replace($matches[0], $class_attribute, $result);
}
return $result;
}
public function widget($_arguments, $_instance) {
$display_mode = PrisnaGWTConfig::getSettingValue('display_mode');
$title = array_key_exists('title', $_instance) ? apply_filters('widget_title', $_instance['title']) : null;
extract($_arguments, EXTR_SKIP);
echo $before_widget;
if ($display_mode == 'inline' && !empty($title))
echo $this->_add_class($_arguments['before_title'], 'prisna-gwt-align-' . PrisnaGWTConfig::getSettingValue('align_mode')) . $title . $_arguments['after_title'];
echo do_shortcode('[' . PrisnaGWTConfig::getWidgetName(true) . ']');
echo $after_widget;
}
public static function isAvailable() {
if (PrisnaGWTConfig::getSettingValue('test_mode') == 'true' && !current_user_can('administrator'))
return false;
return true;
}
public static function _initialize_widget() {
if (!self::isAvailable())
return;
register_widget('PrisnaGWTUI');
}
}
add_action('widgets_init', array('PrisnaGWTUI', '_initialize_widget'));
class PrisnaGWTValidator {
public static function isInteger($_number) {
if (!self::isEmpty($_number))
return ((string) $_number) === ((string) (int) $_number);
else
return true;
}
public static function isEmpty($_string) {
return (empty($_string) && strlen($_string) == 0);
}
public static function isBool($_string) {
return ($_string === 'true' || $_string === 'false' || $_string === true || $_string === false);
}
}
class PrisnaGWTChain {
public $table;
public $rows;
public $chain_table;
public $primary_field;
public $parent_field;
public $sort_field;
public function __construct($primary_field, $parent_field, $sort_field, $rows, $root_id, $maxlevel) {
$this->rows = $rows;
$this->primary_field = $primary_field;
$this->parent_field = $parent_field;
$this->sort_field = $sort_field;
$this->_build_chain($root_id,$maxlevel);
}
public function get() {
return $this->chain_table;
}
protected function _build_chain($rootcatid, $maxlevel) {
foreach($this->rows as $row)
$this->table[$row[$this->parent_field]][ $row[$this->primary_field]] = $row;
$this->_make_branch($rootcatid,0,$maxlevel);
}
protected function _make_branch($parent_id, $level, $maxlevel) {
$rows = $this->table[$parent_id];
if (empty($rows))
return;
foreach($rows as $key => $value)
$rows[$key]['key'] = $this->sort_field;
usort($rows, array('self', 'cmp'));
foreach($rows as $item) {
$item['indent'] = $level;
$this->chain_table[] = $item;
if (isset($this->table[$item[$this->primary_field]]) && ($maxlevel>$level+1) || ($maxlevel==0))
$this->_make_branch($item[$this->primary_field], $level+1, $maxlevel);
}
}
public static function cmp($a, $b) {
if ($a[$a['key']] == $b[$b['key']])
return 0;
return $a[$a['key']] < $b[$b['key']] ? -1 : 1;
}
}
?>