* @param string
* @return string
*/
function _closeTags($string, $open = false){
// Put all opened tags into an array
String::regexp_match_all("#<([a-z]+)( .*)?(?!/)>#iU", $string, $result);
$openedtags = $result[1];
// Put all closed tags into an array
String::regexp_match_all("#([a-z]+)>#iU", $string, $result);
$closedtags = $result[1];
$len_opened = count($openedtags);
$len_closed = count($closedtags);
// All tags are closed
if(count($closedtags) == $len_opened){
return $string;
}
$openedtags = array_reverse($openedtags);
$closedtags = array_reverse($closedtags);
if ($open) {
// Open tags
for($i=0; $i < $len_closed; $i++) {
if (!in_array($closedtags[$i],$openedtags)){
$string = '<'.$closedtags[$i].'>' . $string;
} else {
unset($openedtags[array_search($closedtags[$i],$openedtags)]);
}
}
return $string;
} else {
// Close tags
for($i=0; $i < $len_opened; $i++) {
if (!in_array($openedtags[$i],$closedtags)){
$string .= ''.$openedtags[$i].'>';
} else {
unset($closedtags[array_search($openedtags[$i],$closedtags)]);
}
}
return $string;
}
}
/**
* Split the supplied string by the supplied separator.
*/
function smartyExplode($string, $separator) {
return explode($separator, $string);
}
/**
* Assign a value to a template variable.
*/
function smartyAssign($value, $varName, $passThru = false) {
if (isset($varName)) {
// NOTE: CANNOT use $this, as it's actually
// a COPY of the real template manager for some PHPs!
// FIXME: Track this bug down. (Smarty?)
$templateMgr =& TemplateManager::getManager();
$templateMgr->assign($varName, $value);
}
if ($passThru) return $value;
}
/**
* Smarty usage: {sort_heading key="localization.key.name" sort="foo"}
*
* Custom Smarty function for creating heading links to sort tables by
* @param $params array associative array
* @param $smarty Smarty
* @return string heading link to sort table by
*/
function smartySortHeading($params, &$smarty) {
if (isset($params) && !empty($params)) {
$sortParams = $this->request->getQueryArray();
isset($params['sort'])? ($sortParams['sort'] = $params['sort']) : null;
$sortDirection = $smarty->get_template_vars('sortDirection');
$sort = $smarty->get_template_vars('sort');
// Invert sort direction
if($params['sort'] == $sort) {
if ($sortDirection == SORT_DIRECTION_ASC) {
$sortParams['sortDirection'] = SORT_DIRECTION_DESC;
} else {
$sortParams['sortDirection'] = SORT_DIRECTION_ASC;
}
} else {
$sortParams['sortDirection'] = SORT_DIRECTION_ASC;
}
$link = $this->request->url(null, null, null, $this->request->getRequestedArgs(), $sortParams, null, true);
$text = isset($params['key']) ? __($params['key']) : '';
$style = (isset($sort) && isset($params['sort']) && ($sort == $params['sort'])) ? ' style="font-weight:bold"' : '';
return "$text";
}
}
/**
* Smarty usage: {sort_search key="localization.key.name" sort="foo"}
*
* Custom Smarty function for creating heading links to sort search-generated tables
* @param $params array associative array
* @param $smarty Smarty
* @return string heading link to sort table by
*/
function smartySortSearch($params, &$smarty) {
if (isset($params) && !empty($params)) {
$sort = $smarty->get_template_vars('sort');
$sortDirection = $smarty->get_template_vars('sortDirection');
// Invert sort direction
if($params['sort'] == $sort) {
if ($sortDirection == SORT_DIRECTION_ASC) {
$direction = SORT_DIRECTION_DESC;
} else {
$direction = SORT_DIRECTION_ASC;
}
} else {
$direction = SORT_DIRECTION_ASC;
}
// Escape variables for JS inclusion
foreach (array('heading', 'direction') as $varName) {
$$varName = $this->smartyEscape($$varName, 'javascript');
}
$heading = isset($params['sort']) ? $params['sort'] : $sort;
$text = isset($params['key']) ? __($params['key']) : '';
$style = (isset($sort) && isset($params['sort']) && ($sort == $params['sort'])) ? ' style="font-weight:bold"' : '';
return "$text";
}
}
/**
* Smarty usage: {load_url_in_div id="someHtmlId" url="http://the.url.to.be.loaded.into.the.grid"}
*
* Custom Smarty function for loading a URL via AJAX into a DIV
* @param $params array associative array
* @param $smarty Smarty
* @return string of HTML/Javascript
*/
function smartyLoadUrlInDiv($params, &$smarty) {
// Required Params
if (!isset($params['url'])) {
$smarty->trigger_error("url parameter is missing from load_url_in_div");
}
if (!isset($params['id'])) {
$smarty->trigger_error("id parameter is missing from load_url_in_div");
}
// clear this variable, since it appears to carry over from previous load_url_in_div template assignments.
$this->clear_assign(array('inDivClass'));
$this->assign('inDivUrl', $params['url']);
$this->assign('inDivDivId', $params['id']);
if (isset($params['class'])) $this->assign('inDivClass', $params['class']);
if (isset($params['loadMessageId'])) {
$loadMessageId = $params['loadMessageId'];
unset($params['url'], $params['id'], $params['loadMessageId'], $params['class']);
$this->assign('inDivLoadMessage', __($loadMessageId, $params));
} else {
$this->assign('inDivLoadMessage', $this->fetch('common/loadingContainer.tpl'));
}
return $this->fetch('common/urlInDiv.tpl');
}
/**
* Smarty usage: {modal url=$dialogUrl actOnId="#gridName" button="#dialogButton"}
*
* Custom Smarty function for creating jQuery-based modals
* @param $params array associative array
* @param $smarty Smarty
* @return string Call to modal function with specified parameters
*/
function smartyModal($params, &$smarty) {
// Required Params
if (!isset($params['url'])) {
$smarty->trigger_error("URL parameter is missing from modal");
} elseif (!isset($params['actOnId'])) {
$smarty->trigger_error("actOnId parameter is missing from modal");
} elseif (!isset($params['button'])) {
$smarty->trigger_error("Button parameter is missing from modal");
} else {
$url = $params['url'];
$actOnType = isset($params['actOnType'])?$params['actOnType']:'';
$actOnId = $params['actOnId'];
$button = $params['button'];
$dialogTitle = isset($params['dialogTitle'])?$params['dialogTitle']: false;
}
// Translate modal submit/cancel buttons
$submitButton = __('common.ok');
$cancelButton = __('common.cancel');
// Escape variables for JS inclusion
foreach (array('submitButton', 'cancelButton', 'url', 'actOnType', 'actOnId', 'button') as $varName) {
$$varName = $this->smartyEscape($$varName, 'javascript');
}
// Add the modal javascript to the header
$dialogTitle = isset($dialogTitle) ? ", '$dialogTitle'" : "";
$modalCode = "\n";
return $modalCode;
}
/**
* Smarty usage: {confirm url=$dialogUrl dialogText="example.locale.key" button="#dialogButton"}
* Custom Smarty function for creating simple yes/no dialogs (or to just send an AJAX post)
* NB: -Leave out 'url' parameter to just display a message
* -Leave out 'dialogText' parameter to immediately submit an AJAX request
* @param $params array associative array
* @param $smarty Smarty
* @return string Call to modal function with specified parameters
*/
function smartyConfirm($params, &$smarty) {
// Required params
if (!isset($params['button'])) {
$smarty->trigger_error("Button parameter is missing from confirm");
} else {
$button = $params['button'];
}
// Non-required params
$url = isset($params['url']) ? $params['url'] : null;
$actOnType = isset($params['actOnType']) ? $params['actOnType'] : '';
$actOnId = isset($params['actOnId'])?$params['actOnId']:'';
if (isset($params['dialogText'])) {
$showDialog = true;
if(isset($params['translate']) && $params['translate'] == false) {
$dialogText = $params['dialogText'];
} else {
$dialogText = __($params['dialogText']);
}
} else {
$showDialog = false;
}
if (!$showDialog && !$url) {
$smarty->trigger_error("Both URL and dialogText parameters are missing from confirm");
}
// Translate modal submit/cancel buttons
$submitButton = __('common.ok');
$cancelButton = __('common.cancel');
// Properly escape variables for inclusion in Javascript
foreach (array('button', 'url, actOnType, actOnId, dialogText, submitButton, cancelButton') as $varName) {
$$varName = $this->smartyEscape($$varName, 'javascript');
}
if ($showDialog) {
$confirmCode = "\n";
} else {
$confirmCode = "";
}
return $confirmCode;
}
function smartyModalTitle($params, &$smarty) {
// Required params
// Id must be child of div that is next-sibling of title div
if (!isset($params['id'])) {
$smarty->trigger_error("Selector missing for title bar initialization");
} else {
$id = $params['id'];
}
// Non-required params
$iconClass = isset($params['iconClass']) ? $params['iconClass'] : '';
if(isset($params['iconClass'])) {
$iconClass = $params['iconClass'];
$iconHtml = "";
} else $iconHtml = "";
if(isset($params['key'])) {
$keyHtml = "" . __($params['key']) . "";
} elseif(isset($params['keyTranslated'])) {
$keyHtml = "" . $params['keyTranslated'] . "";
} else $keyHtml = "";
if(isset($params['canClose'])) {
$canClose = $params['canClose'];
$canCloseHtml = "close";
} else $canCloseHtml = "";
// WARNING: The div here MUST be synced with ModalHandler.js
// as part of the title bar fix code to work around JQueryUI.
return "
" .
$iconHtml .
$keyHtml .
$canCloseHtml .
"
";
}
}
?>