protected function parse_conditional($type, $opening_tag, $condition_contents, $exact_match, $template_contents, $content_vars, $index_in_group = false) { // IF if ($type == 'if') { $tag = new PerchXMLTag($opening_tag); $positive = $condition_contents; $negative = ''; // else condition if (strpos($condition_contents, 'perch:else') > 0) { $parts = preg_split('/<perch:else\\s*\\/>/', $condition_contents); if (is_array($parts) && count($parts) > 1) { $positive = $parts[0]; $negative = $parts[1]; } } // exists and not-exists if ($tag->exists() || $tag->not_exists()) { $exists_string = $tag->exists(); // Not-exists - just swaps the pos and neg over. if ($tag->not_exists()) { $exists_string = $tag->not_exists(); // swap pos and neg $tmp = $positive; $positive = $negative; $negative = $tmp; $tmp = null; } // do we have spaces? Then it could be a logic string if (strpos(trim($exists_string), ' ') !== false) { $operators = array('AND', 'OR', 'XOR'); preg_match_all('#!?\\b[A-Za-z0-9_]+\\b#', $exists_string, $ids, PREG_SET_ORDER); if (PerchUtil::count($ids)) { $logic_string = $exists_string; foreach ($ids as $id) { $id = $id[0]; if (!in_array($id, $operators)) { // Flip the operator? if (substr($id, 0, 1) == '!') { $id = substr($id, 1); $flip_operator = true; } else { $flip_operator = false; } if (array_key_exists($id, $content_vars) && $this->_resolve_to_value($content_vars[$id]) != '') { $op = 'true'; if ($flip_operator) { $op = 'false'; } $logic_string = preg_replace('#!?\\b' . preg_quote($id, '#') . '\\b#', $op, $logic_string); } else { $op = 'false'; if ($flip_operator) { $op = 'true'; } $logic_string = preg_replace('#!?\\b' . preg_quote($id, '#') . '\\b#', $op, $logic_string); } } } $LogicString = new PerchLogicString($logic_string); if ($LogicString->parse()) { $template_contents = str_replace($exact_match, $positive, $template_contents); } else { $template_contents = str_replace($exact_match, $negative, $template_contents); } return $template_contents; } } else { if (array_key_exists($exists_string, $content_vars) && $this->_resolve_to_value($content_vars[$exists_string]) != '') { $template_contents = str_replace($exact_match, $positive, $template_contents); } else { $template_contents = str_replace($exact_match, $negative, $template_contents); } return $template_contents; } } // different if ($tag->different()) { $prev_value = ''; $new_value = ''; if (array_key_exists($tag->different(), $this->_previous_item)) { $prev_value = $this->_resolve_to_value($this->_previous_item[$tag->different()]); if ($tag->format()) { $prev_value = $this->_format($tag, $prev_value); } } if (array_key_exists($tag->different(), $content_vars)) { $new_value = $this->_resolve_to_value($content_vars[$tag->different()]); if ($tag->format()) { $new_value = $this->_format($tag, $new_value); } } if ($tag->case() == 'insensitive') { $prev_value = strtolower($prev_value); $new_value = strtolower($new_value); } if ($prev_value != $new_value) { $template_contents = str_replace($exact_match, $positive, $template_contents); } else { $template_contents = str_replace($exact_match, $negative, $template_contents); } return $template_contents; } // id if ($tag->id()) { $matched = false; $sideA = false; $sideB = false; if (is_array($content_vars) && array_key_exists($tag->id(), $content_vars) && $this->_resolve_to_value($content_vars[$tag->id()]) != '') { $sideA = $this->_resolve_to_value($content_vars[$tag->id()]); if ($tag->format()) { $sideA = $this->_format($tag, $sideA); } } $comparison = 'eq'; if ($tag->match()) { $comparison = $tag->match(); } if ($tag->value()) { $sideB = $tag->value(); } // parse sideB? if ($sideB && substr($sideB, 0, 1) == '{' && substr($sideB, -1, 1) == '}') { $sideB = str_replace(array('{', '}'), '', $sideB); if (isset($content_vars[$sideB])) { $sideB = $this->_resolve_to_value($content_vars[$sideB]); } else { $sideB = false; } if ($tag->format() && $tag->format_both()) { $sideB = $this->_format($tag, $sideB); } } switch ($comparison) { case 'eq': case 'is': case 'exact': if ($sideA == $sideB) { $matched = true; } break; case 'neq': case 'ne': case 'not': if ($sideA != $sideB) { $matched = true; } break; case 'gt': if ($sideA > $sideB) { $matched = true; } break; case 'gte': if ($sideA >= $sideB) { $matched = true; } break; case 'lt': if ($sideA < $sideB) { $matched = true; } break; case 'lte': if ($sideA <= $sideB) { $matched = true; } break; case 'contains': if (preg_match('/\\b' . $sideB . '\\b/i', $sideA)) { $matched = true; } break; case 'regex': case 'regexp': if (preg_match($sideB, $sideA)) { $matched = true; } break; case 'between': case 'betwixt': $vals = explode(',', $sideB); if (PerchUtil::count($vals) == 2) { if ($sideA > trim($vals[0]) && $sideA < trim($vals[1])) { $matched = true; } } break; case 'eqbetween': case 'eqbetwixt': $vals = explode(',', $sideB); if (PerchUtil::count($vals) == 2) { if ($sideA >= trim($vals[0]) && $sideA <= trim($vals[1])) { $matched = true; } } break; case 'in': case 'within': $vals = explode(',', $sideB); if (PerchUtil::count($vals)) { foreach ($vals as $value) { if ($sideA == trim($value)) { $matched = true; break; } } } break; } if ($matched) { $template_contents = str_replace($exact_match, $positive, $template_contents); } else { $template_contents = str_replace($exact_match, $negative, $template_contents); } return $template_contents; } } // BEFORE if ($type == 'before') { if (array_key_exists('perch_item_first', $content_vars)) { $template_contents = str_replace($exact_match, $condition_contents, $template_contents); } else { $template_contents = str_replace($exact_match, '', $template_contents); } return $template_contents; } // AFTER if ($type == 'after') { if (array_key_exists('perch_item_last', $content_vars)) { $template_contents = str_replace($exact_match, $condition_contents, $template_contents); } else { $template_contents = str_replace($exact_match, '', $template_contents); } return $template_contents; } return $template_contents; }