/**
  * @dataProvider providerCustomCode
  */
 public function testCustomCode($customCode, $expected_parsed_array, $expected_customCode)
 {
     //Test for parseHtmlTag
     $this->assertEquals($expected_parsed_array, SugarHtml::parseHtmlTag($customCode));
     $params = array('module' => 'Accounts', 'view' => 'DetailView', 'id' => array('customCode' => $customCode), 'form_id' => 'DetailView');
     //Test for smarty_function_sugar_button for customCode
     $this->assertEquals($expected_customCode, smarty_function_sugar_button($params, $this->_smarty));
 }
function smarty_function_sugar_menu($params, &$smarty)
{
    $root_options = array("id" => array_key_exists('id', $params) ? $params['id'] : "");
    if (array_key_exists('htmlOptions', $params)) {
        foreach ($params['htmlOptions'] as $attr => $value) {
            $root_options[$attr] = $value;
        }
    }
    $output = SugarHtml::createOpenTag("ul", $root_options);
    foreach ($params['items'] as $item) {
        if (strpos($item['html'], "</") === 0) {
            $output .= $item['html'];
            continue;
        }
        $output .= SugarHtml::createOpenTag('li', !empty($params['itemOptions']) ? $params['itemOptions'] : array()) . $item['html'];
        if (isset($item['items']) && count($item['items'])) {
            $output .= smarty_function_sugar_menu(array('items' => $item['items'], 'htmlOptions' => !empty($params['submenuHtmlOptions']) ? $params['submenuHtmlOptions'] : (!empty($item['submenuHtmlOptions']) ? $item['submenuHtmlOptions'] : array())), $smarty);
        }
        $output .= SugarHtml::createCloseTag("li");
    }
    $output .= SugarHtml::createCloseTag("ul");
    return $output;
}
/**
 * smarty_function_sugar_button
 * This is the constructor for the Smarty plugin.
 *
 * @param $params The runtime Smarty key/value arguments
 * @param $smarty The reference to the Smarty object used in this invocation
 */
function smarty_function_sugar_button($params, $smarty)
{
    if (empty($params['module'])) {
        $smarty->trigger_error("sugar_button: missing required param (module)");
    } else {
        if (empty($params['id'])) {
            $smarty->trigger_error("sugar_button: missing required param (id)");
        } else {
            if (empty($params['view'])) {
                $smarty->trigger_error("sugar_button: missing required param (view)");
            }
        }
    }
    $js_form = empty($params['form_id']) ? "var _form = (this.form) ? this.form : document.forms[0];" : "var _form = document.getElementById('{$params['form_id']}');";
    $type = $params['id'];
    $location = empty($params['location']) ? "" : "_" . $params['location'];
    if (isset($GLOBALS['sugar_config']['enable_action_menu']) && $GLOBALS['sugar_config']['enable_action_menu'] === false) {
        $enable_action_menu = false;
    } else {
        $enable_action_menu = true;
    }
    if (!is_array($type)) {
        $module = $params['module'];
        $view = $params['view'];
        switch (strtoupper($type)) {
            case "SEARCH":
                $output = '<input tabindex="2" title="{$APP.LBL_SEARCH_BUTTON_TITLE}" onclick="SUGAR.savedViews.setChooser();" class="button" type="submit" name="button" value="{$APP.LBL_SEARCH_BUTTON_LABEL}" id="search_form_submit"/>&nbsp;';
                break;
            case "CANCEL":
                $cancelButton = '{capture name="cancelReturnUrl" assign="cancelReturnUrl"}';
                $cancelButton .= '{if !empty($smarty.request.return_action) && $smarty.request.return_action == "DetailView" && !empty($fields.id.value) && empty($smarty.request.return_id)}';
                $cancelButton .= 'parent.SUGAR.App.router.buildRoute(\'{$smarty.request.return_module|escape:"url"}\', \'{$fields.id.value|escape:"url"}\', \'{$smarty.request.return_action|escape:"url"}\')';
                $cancelButton .= '{elseif !empty($smarty.request.return_module) || !empty($smarty.request.return_action) || !empty($smarty.request.return_id)}';
                $cancelButton .= 'parent.SUGAR.App.router.buildRoute(\'{$smarty.request.return_module|escape:"url"}\', \'{$smarty.request.return_id|escape:"url"}\', \'{$smarty.request.return_action|escape:"url"}\')';
                $cancelButton .= '{else}';
                $cancelButton .= "parent.SUGAR.App.router.buildRoute('{$module}')";
                $cancelButton .= '{/if}';
                $cancelButton .= '{/capture}';
                $cancelButton .= '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="parent.SUGAR.App.bwc.revertAttributes();parent.SUGAR.App.router.navigate({$cancelReturnUrl}, {literal}{trigger: true}{/literal}); return false;" type="button" name="button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}" id="' . $type . $location . '"> ';
                $output = $cancelButton;
                break;
            case "DELETE":
                $output = '{if $bean->aclAccess("delete")}<input title="{$APP.LBL_DELETE_BUTTON_TITLE}" accessKey="{$APP.LBL_DELETE_BUTTON_KEY}" class="button" onclick="' . $js_form . ' _form.return_module.value=\'' . $module . '\'; _form.return_action.value=\'ListView\'; _form.action.value=\'Delete\'; if(confirm(\'{$APP.NTC_DELETE_CONFIRMATION}\')) SUGAR.ajaxUI.submitForm(_form);" type="submit" name="Delete" value="{$APP.LBL_DELETE_BUTTON_LABEL}" id="delete_button">{/if} ';
                break;
            case "DUPLICATE":
                $output = '{if $bean->aclAccess("edit")}<input title="{$APP.LBL_DUPLICATE_BUTTON_TITLE}" accessKey="{$APP.LBL_DUPLICATE_BUTTON_KEY}" class="button" onclick="' . $js_form . ' _form.return_module.value=\'' . $module . '\'; _form.return_action.value=\'DetailView\'; _form.isDuplicate.value=true; _form.action.value=\'' . $view . '\'; _form.return_id.value=\'{$id}\';SUGAR.ajaxUI.submitForm(_form);" type="button" name="Duplicate" value="{$APP.LBL_DUPLICATE_BUTTON_LABEL}" id="duplicate_button">{/if} ';
                break;
            case "EDIT":
                $output = '{if $bean->aclAccess("edit")}<input title="{$APP.LBL_EDIT_BUTTON_TITLE}" accessKey="{$APP.LBL_EDIT_BUTTON_KEY}" class="button primary" onclick="' . $js_form . ' _form.return_module.value=\'' . $module . '\'; _form.return_action.value=\'DetailView\'; _form.return_id.value=\'{$id}\'; _form.action.value=\'EditView\';SUGAR.ajaxUI.submitForm(_form);" type="button" name="Edit" id="edit_button" value="{$APP.LBL_EDIT_BUTTON_LABEL}">{/if} ';
                break;
            case "FIND_DUPLICATES":
                $output = '{if $bean->aclAccess("edit") && $bean->aclAccess("delete")}<input title="{$APP.LBL_DUP_MERGE}" class="button" onclick="' . $js_form . ' _form.return_module.value=\'' . $module . '\'; _form.return_action.value=\'DetailView\'; _form.return_id.value=\'{$id}\'; _form.action.value=\'Step1\'; _form.module.value=\'MergeRecords\';SUGAR.ajaxUI.submitForm(_form);" type="button" name="Merge" value="{$APP.LBL_DUP_MERGE}" id="merge_duplicate_button">{/if} ';
                break;
            case "SAVE":
                $view = $_REQUEST['action'] == 'EditView' ? 'EditView' : ($view == 'EditView' ? 'EditView' : $view);
                $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}" accessKey="{$APP.LBL_SAVE_BUTTON_KEY}" class="button primary" onclick="' . $js_form . ' {if $isDuplicate}_form.return_id.value=\'\'; {/if}_form.action.value=\'Save\'; if(check_form(\'' . $view . '\'))SUGAR.ajaxUI.submitForm(_form);return false;" type="submit" name="button" value="{$APP.LBL_SAVE_BUTTON_LABEL}" id="' . $type . $location . '">{/if} ';
                break;
            case "SUBPANELSAVE":
                if ($view == 'QuickCreate' || isset($_REQUEST['target_action']) && strtolower($_REQUEST['target_action']) == 'quickcreate') {
                    $view = "form_SubpanelQuickCreate_{$module}";
                }
                $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}"  class="button" onclick="' . $js_form . ' _form.action.value=\'Save\';if(check_form(\'' . $view . '\'))return SUGAR.subpanelUtils.inlineSave(_form.id, \'' . $params['module'] . '_subpanel_save_button\');return false;" type="submit" name="' . $params['module'] . '_subpanel_save_button" id="' . $params['module'] . '_subpanel_save_button" value="{$APP.LBL_SAVE_BUTTON_LABEL}">{/if} ';
                break;
            case "SUBPANELCANCEL":
                $output = '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" class="button" onclick="return SUGAR.subpanelUtils.cancelCreate($(this).attr(\'id\'));return false;" type="submit" name="' . $params['module'] . '_subpanel_cancel_button" id="' . $params['module'] . '_subpanel_cancel_button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}"> ';
                break;
            case "SUBPANELFULLFORM":
                $output = '<input title="{$APP.LBL_FULL_FORM_BUTTON_TITLE}" class="button" onclick="' . $js_form . ' _form.return_action.value=\'DetailView\'; _form.action.value=\'EditView\'; if(typeof(_form.to_pdf)!=\'undefined\') _form.to_pdf.value=\'0\';" type="submit" name="' . $params['module'] . '_subpanel_full_form_button" id="' . $params['module'] . '_subpanel_full_form_button" value="{$APP.LBL_FULL_FORM_BUTTON_LABEL}"> ';
                $output .= '<input type="hidden" name="full_form" value="full_form">';
                break;
            case "DCMENUCANCEL":
                $output = '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="javascript:lastLoadedMenu=undefined;DCMenu.closeOverlay();return false;" type="submit" name="' . $params['module'] . '_dcmenu_cancel_button" id="' . $params['module'] . '_dcmenu_cancel_button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}"> ';
                break;
            case "DCMENUSAVE":
                if ($view == 'QuickCreate') {
                    $view = "form_DCQuickCreate_{$module}";
                } else {
                    if ($view == 'EditView') {
                        $view = "form_DCEditView_{$module}";
                    }
                }
                $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}" accessKey="{$APP.LBL_SAVE_BUTTON_KEY}" class="button primary" onclick="' . $js_form . ' _form.action.value=\'Save\';if(check_form(\'' . $view . '\'))return DCMenu.save(_form.id, \'' . $params['module'] . '_subpanel_save_button\');return false;" type="submit" name="' . $params['module'] . '_dcmenu_save_button" id="' . $params['module'] . '_dcmenu_save_button" value="{$APP.LBL_SAVE_BUTTON_LABEL}">{/if} ';
                break;
            case "DCMENUFULLFORM":
                $output = '<input title="{$APP.LBL_FULL_FORM_BUTTON_TITLE}" accessKey="{$APP.LBL_FULL_FORM_BUTTON_KEY}" class="button" onclick="' . $js_form . ' _form.return_action.value=\'DetailView\'; _form.action.value=\'EditView\'; _form.return_module.value=\'' . $params['module'] . '\';_form.return_id.value=_form.record.value;if(typeof(_form.to_pdf)!=\'undefined\') _form.to_pdf.value=\'0\';SUGAR.ajaxUI.submitForm(_form,null,true);DCMenu.closeOverlay();" type="button" name="' . $params['module'] . '_subpanel_full_form_button" id="' . $params['module'] . '_subpanel_full_form_button" value="{$APP.LBL_FULL_FORM_BUTTON_LABEL}"> ';
                $output .= '<input type="hidden" name="full_form" value="full_form">';
                $output .= '<input type="hidden" name="is_admin" value="">';
                break;
            case "POPUPSAVE":
                $view = $view == 'QuickCreate' ? "form_QuickCreate_{$module}" : $view;
                $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}" accessKey="{$APP.LBL_SAVE_BUTTON_KEY}" ' . 'class="button primary" onclick="' . $js_form . ' _form.action.value=\'Popup\';' . 'return check_form(\'' . $view . '\')" type="submit" name="' . $params['module'] . '_popupcreate_save_button" id="' . $params['module'] . '_popupcreate_save_button" value="{$APP.LBL_SAVE_BUTTON_LABEL}">{/if} ';
                break;
            case "POPUPCANCEL":
                $output = '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" ' . 'class="button" onclick="toggleDisplay(\'addform\');return false;" ' . 'name="' . $params['module'] . '_popup_cancel_button" type="submit"' . 'id="' . $params['module'] . '_popup_cancel_button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}"> ';
                break;
            case "AUDIT":
                $popup_request_data = array('call_back_function' => 'set_return', 'form_name' => 'EditView', 'field_to_name_array' => array());
                $json = getJSONobj();
                require_once 'include/SugarFields/Parsers/MetaParser.php';
                $encoded_popup_request_data = MetaParser::parseDelimiters($json->encode($popup_request_data));
                $audit_link = '<input id="btn_view_change_log" title="{$APP.LNK_VIEW_CHANGE_LOG}" class="button" onclick=\'open_popup("Audit", "600", "400", "&record={$fields.id.value}&module_name=' . $params['module'] . '", true, false, ' . $encoded_popup_request_data . '); return false;\' type="button" value="{$APP.LNK_VIEW_CHANGE_LOG}">';
                $output = '{if $bean->aclAccess("detail")}{if !empty($fields.id.value) && $isAuditEnabled}' . $audit_link . '{/if}{/if}';
                break;
                //Button for the Connector intergration wizard
            //Button for the Connector intergration wizard
            case "CONNECTOR":
                require_once 'include/connectors/utils/ConnectorUtils.php';
                require_once 'include/connectors/sources/SourceFactory.php';
                $modules_sources = ConnectorUtils::getDisplayConfig();
                if (!is_null($modules_sources) && !empty($modules_sources)) {
                    foreach ($modules_sources as $mod => $entry) {
                        if ($mod == $module && !empty($entry)) {
                            foreach ($entry as $source_id) {
                                $source = SourceFactory::getSource($source_id);
                                if ($source->isEnabledInWizard()) {
                                    $output = '<input title="{$APP.LBL_MERGE_CONNECTORS}" type="button" class="button" onClick="document.location=\'index.php?module=Connectors&action=Step1&record={$fields.id.value}&merge_module={$module}\'" name="merge_connector" value="{$APP.LBL_MERGE_CONNECTORS}">';
                                    if (isset($params['appendTo'])) {
                                        $smarty->append($params['appendTo'], $output);
                                        return;
                                    }
                                    return $output;
                                }
                            }
                        }
                    }
                }
                return '';
            case "PDFVIEW":
                $output = '{sugar_button module="$module" id="REALPDFVIEW" view="$view" form_id="formDetailView" record=$fields.id.value}';
                break;
            case "REALPDFVIEW":
                if (empty($params['record'])) {
                    $smarty->trigger_error("sugar_button: missing required param (record)");
                }
                $record = $params['record'];
                require_once 'modules/PdfManager/PdfManagerHelper.php';
                $pdfManagerList = PdfManagerHelper::getPublishedTemplatesForModule($module);
                //quote legacy templates
                if ($module == "Quotes") {
                    require_once 'modules/Quotes/Layouts.php';
                    $tplLayouts = get_layouts();
                }
                $output = '';
                if (!empty($pdfManagerList) || !empty($tplLayouts)) {
                    if (SugarThemeRegistry::current()->name != "Classic") {
                        if ($enable_action_menu) {
                            $output = '
                            <input id="pdfview_button" value="' . translate('LBL_PDF_VIEW') . '" type="button" class="button"  />';
                        }
                        $pdfItems = array();
                        if (!empty($pdfManagerList)) {
                            foreach ($pdfManagerList as $pdfTemplate) {
                                if (!$enable_action_menu) {
                                    $urlParams[] = array('module' => $module, 'record' => $record, 'action' => 'sugarpdf', 'sugarpdf' => 'pdfmanager', 'pdf_template_id' => $pdfTemplate->id, 'name' => $pdfTemplate->name);
                                } else {
                                    $urlParams = array('module' => $module, 'record' => $record, 'action' => 'sugarpdf', 'sugarpdf' => 'pdfmanager', 'pdf_template_id' => $pdfTemplate->id);
                                    $pdfItems[] = array('html' => '<a id="' . $pdfTemplate->name . '_pdfview" href="index.php?' . http_build_query($urlParams, '', '&') . '">' . $pdfTemplate->name . '</a>', 'items' => array());
                                }
                            }
                        }
                        //quote legacy templates
                        if ($module == "Quotes") {
                            foreach ($tplLayouts as $sugarpdf => $path) {
                                if (!$enable_action_menu) {
                                    $urlParams[] = array('module' => $module, 'record' => $record, 'action' => 'sugarpdf', 'sugarpdf' => $sugarpdf, 'email_action' => '', 'name' => $path);
                                } else {
                                    $urlParams = array('module' => $module, 'record' => $record, 'action' => 'sugarpdf', 'sugarpdf' => $sugarpdf, 'email_action' => '');
                                    $pdfItems[] = array('html' => '<a href="index.php?' . http_build_query($urlParams, '', '&') . '">' . $GLOBALS['app_strings']['LBL_EXISTING'] . '_' . $path . '</a>', 'items' => array());
                                }
                            }
                        }
                        sort($pdfItems);
                        if (!$enable_action_menu) {
                            if (!empty($urlParams)) {
                                $output .= '<ul class="clickMenu fancymenu">';
                                $output .= '<li class="sugar_action_button">';
                                $output .= '<a>' . translate('LBL_PDF_VIEW') . '</a>';
                                $output .= '<ul class="subnav" style="display: none;">';
                                foreach ($urlParams as $tplButton) {
                                    if (isset($tplButton['pdf_template_id'])) {
                                        $parentLocation = 'index.php?module=' . $module . '&record=' . $tplButton['record'] . '&action=' . $tplButton['action'] . '&sugarpdf=' . $tplButton['sugarpdf'] . '&pdf_template_id=' . $tplButton['pdf_template_id'];
                                        $output .= '<li><a id="' . $tplButton['name'] . '_pdfview" ' . 'href="' . $parentLocation . '">' . $tplButton['name'] . '</a></li>';
                                    } else {
                                        // legacy templates
                                        $parentLocation = 'index.php?module=' . $module . '&record=' . $tplButton['record'] . '&action=' . $tplButton['action'] . '&sugarpdf=' . $tplButton['sugarpdf'] . '&email_action=' . $tplButton['email_action'];
                                        $output .= '<li><a id="' . $tplButton['name'] . '_pdfview" ' . 'href="' . $parentLocation . '">' . $GLOBALS['app_strings']['LBL_EXISTING'] . '_' . $tplButton['name'] . '</a></li>';
                                    }
                                }
                                $output .= '</ul><span class="ab"></span></li></ul>';
                            }
                        } else {
                            require_once 'include/SugarSmarty/plugins/function.sugar_menu.php';
                            $output .= smarty_function_sugar_menu(array('id' => "pdfview_action_menu", 'items' => $pdfItems, 'htmlOptions' => array('class' => 'subnav-sub'), 'itemOptions' => array(), 'submenuHtmlOptions' => array()), $smarty);
                        }
                    } else {
                        $output = '
                            <script type="text/javascript">
                                function display_pdf_list(el) {
                                    var menu = \'';
                        if (!empty($pdfManagerList)) {
                            foreach ($pdfManagerList as $pdfTemplate) {
                                $urlParams = array('module' => $module, 'record' => $record, 'action' => 'sugarpdf', 'sugarpdf' => 'pdfmanager', 'pdf_template_id' => $pdfTemplate->id);
                                $output .= '<a style="width: 150px" class="menuItem" onmouseover="hiliteItem(this,\\\'yes\\\');" onmouseout="unhiliteItem(this);" onclick="" href="index.php?' . http_build_query($urlParams, '', '&') . '">' . $pdfTemplate->name . '</a>';
                            }
                        }
                        //quote legacy templates
                        if ($module == "Quotes") {
                            require_once 'modules/Quotes/Layouts.php';
                            $tplLayouts = get_layouts();
                            foreach ($tplLayouts as $sugarpdf => $path) {
                                $urlParams = array('module' => $module, 'record' => $record, 'action' => 'sugarpdf', 'sugarpdf' => $sugarpdf, 'email_action' => '');
                                $output .= '<a style="width: 150px" class="menuItem" onmouseover="hiliteItem(this,\\\'yes\\\');" onmouseout="unhiliteItem(this);" onclick="" href="index.php?' . http_build_query($urlParams, '', '&') . '">' . $path . '</a>';
                            }
                        }
                        $output .= '\';
                                SUGAR.util.showHelpTips(el,menu);
                                }
                            </script>
                            <a onclick="display_pdf_list(this);" />' . translate('LBL_PDF_VIEW') . '</a>
                          ';
                    }
                }
                break;
            case "PDFEMAIL":
                $output = '{sugar_button module="$module" id="REALPDFEMAIL" view="$view" form_id="formDetailView" record=$fields.id.value}';
                break;
            case "REALPDFEMAIL":
                $output = '';
                global $current_user, $sugar_config;
                $userPref = $current_user->getPreference('email_link_type');
                $defaultPref = $sugar_config['email_default_client'];
                if ($userPref != '') {
                    $client = $userPref;
                } else {
                    $client = $defaultPref;
                }
                if ($client == 'sugar') {
                    if (empty($params['record'])) {
                        $smarty->trigger_error("sugar_button: missing required param (record)");
                    }
                    $record = $params['record'];
                    require_once 'modules/PdfManager/PdfManagerHelper.php';
                    $pdfManagerList = PdfManagerHelper::getPublishedTemplatesForModule($module);
                    //quote legacy templates
                    if ($module == "Quotes") {
                        require_once 'modules/Quotes/Layouts.php';
                        $tplLayouts = get_layouts();
                    }
                    if (!empty($pdfManagerList) || !empty($tplLayouts)) {
                        if (SugarThemeRegistry::current()->name != "Classic") {
                            if ($enable_action_menu) {
                                $output = '
                            <input id="pdfemail_button" value="' . translate('LBL_PDF_EMAIL') . '" type="button" class="button"  />';
                            }
                            $pdfItems = array();
                            if (!empty($pdfManagerList)) {
                                foreach ($pdfManagerList as $pdfTemplate) {
                                    if (!$enable_action_menu) {
                                        $urlParams[] = array('module' => $module, 'record' => $record, 'action' => 'sugarpdf', 'sugarpdf' => 'pdfmanager', 'pdf_template_id' => $pdfTemplate->id, 'to_email' => "1", 'name' => $pdfTemplate->name);
                                    } else {
                                        $urlParams = array('module' => $module, 'record' => $record, 'action' => 'sugarpdf', 'sugarpdf' => 'pdfmanager', 'pdf_template_id' => $pdfTemplate->id, 'to_email' => "1");
                                        $pdfItems[] = array('html' => '<a id="' . $pdfTemplate->name . '_pdfemail" href="index.php?' . http_build_query($urlParams, '', '&') . '">' . $pdfTemplate->name . '</a>', 'items' => array());
                                    }
                                }
                            }
                            //quote legacy templates
                            if ($module == "Quotes") {
                                foreach ($tplLayouts as $sugarpdf => $path) {
                                    if (!$enable_action_menu) {
                                        $urlParams[] = array('module' => $module, 'record' => $record, 'action' => 'sugarpdf', 'sugarpdf' => $sugarpdf, 'email_action' => 'EmailLayout', 'name' => $path);
                                    } else {
                                        $urlParams = array('module' => $module, 'record' => $record, 'action' => 'sugarpdf', 'sugarpdf' => $sugarpdf, 'email_action' => 'EmailLayout');
                                        $pdfItems[] = array('html' => '<a href="index.php?' . http_build_query($urlParams, '', '&') . '">' . $GLOBALS['app_strings']['LBL_EXISTING'] . '_' . $path . '</a>', 'items' => array());
                                    }
                                }
                            }
                            sort($pdfItems);
                            if (!$enable_action_menu) {
                                if (!empty($urlParams)) {
                                    $output .= '<ul class="clickMenu fancymenu">';
                                    $output .= '<li class="sugar_action_button">';
                                    $output .= '<a>' . translate('LBL_PDF_EMAIL') . '</a>';
                                    $output .= '<ul class="subnav" style="display: none;">';
                                    foreach ($urlParams as $tplButton) {
                                        if (isset($tplButton['pdf_template_id'])) {
                                            $parentLocation = 'index.php?module=' . $module . '&record=' . $tplButton['record'] . '&action=' . $tplButton['action'] . '&sugarpdf=' . $tplButton['sugarpdf'] . '&pdf_template_id=' . $tplButton['pdf_template_id'] . '&to_email=' . $tplButton['to_email'];
                                            $output .= '<li><a id="' . $tplButton['name'] . '_pdfemail" ' . 'href="' . $parentLocation . '">' . $tplButton['name'] . '</a></li>';
                                        } else {
                                            // legacy templates
                                            $parentLocation = 'index.php?module=' . $module . '&record=' . $tplButton['record'] . '&action=' . $tplButton['action'] . '&sugarpdf=' . $tplButton['sugarpdf'] . '&email_action=' . $tplButton['email_action'];
                                            $output .= '<li><a id="' . $tplButton['name'] . '_pdfemail" ' . 'href="' . $parentLocation . '">' . $GLOBALS['app_strings']['LBL_EXISTING'] . '_' . $tplButton['name'] . '</a></li>';
                                        }
                                    }
                                    $output .= '</ul><span class="ab"></span></li></ul>';
                                }
                            } else {
                                require_once 'include/SugarSmarty/plugins/function.sugar_menu.php';
                                $output .= smarty_function_sugar_menu(array('id' => "pdfview_action_menu", 'items' => $pdfItems, 'htmlOptions' => array('class' => 'subnav-sub'), 'itemOptions' => array(), 'submenuHtmlOptions' => array()), $smarty);
                            }
                        } else {
                            $output = '
                            <script language="javascript">
                                function display_pdf_email_list(el) {
                                    var menu = \'';
                            if (!empty($pdfManagerList)) {
                                foreach ($pdfManagerList as $pdfTemplate) {
                                    $urlParams = array('module' => $module, 'record' => $record, 'action' => 'sugarpdf', 'sugarpdf' => 'pdfmanager', 'pdf_template_id' => $pdfTemplate->id, 'to_email' => "1");
                                    $output .= '<a style="width: 150px" class="menuItem" onmouseover="hiliteItem(this,\\\'yes\\\');" onmouseout="unhiliteItem(this);" onclick="" href="index.php?' . http_build_query($urlParams, '', '&') . '">' . $pdfTemplate->name . '</a>';
                                }
                            }
                            //quote legacy templates
                            if ($module == "Quotes") {
                                require_once 'modules/Quotes/Layouts.php';
                                $tplLayouts = get_layouts();
                                foreach ($tplLayouts as $sugarpdf => $path) {
                                    $urlParams = array('module' => $module, 'record' => $record, 'action' => 'sugarpdf', 'sugarpdf' => $sugarpdf, 'email_action' => 'EmailLayout');
                                    $output .= '<a style="width: 150px" class="menuItem" onmouseover="hiliteItem(this,\\\'yes\\\');" onmouseout="unhiliteItem(this);" onclick="" href="index.php?' . http_build_query($urlParams, '', '&') . '">' . $sugarpdf . '</a>';
                                }
                            }
                            $output .= '\';
                                SUGAR.util.showHelpTips(el,menu);
                                }
                            </script>
                                <a onclick="display_pdf_email_list(this);" />' . translate('LBL_PDF_EMAIL') . '</a>
                          ';
                        }
                    }
                }
                break;
            case 'SHARE':
                // TODO we shouldn't rely on the name field only, but we don't
                // have this information anywhere and this is BWC code...
                $shareButton = <<<ENDB
<form>
<input type="hidden" id="share_button_name" value="{\$fields.name.value}">
<input title="{\$APP.LBL_SHARE_BUTTON_TITLE}" accessKey="{\$APP.LBL_SHARE_BUTTON_KEY}"
  class="button" onclick="parent.SUGAR.App.bwc.shareRecord('{$params['module']}', '{\$fields.id.value}', this.form.share_button_name.value)" type="button" name="button" value="{\$APP.LBL_SHARE_BUTTON_LABEL}">
</form>
ENDB;
                $output = $shareButton;
                break;
        }
        //switch
        if (isset($params['appendTo'])) {
            $smarty->append($params['appendTo'], $output);
            return;
        }
        return $output;
    } else {
        if (is_array($type) && isset($type['sugar_html'])) {
            require_once 'include/SugarHtml/SugarHtml.php';
            $dom_tree = SugarHtml::parseSugarHtml($type['sugar_html']);
            replaceFormClick($dom_tree, $js_form);
            $output = SugarHtml::createHtml($dom_tree);
            if (isset($params['appendTo'])) {
                $smarty->append($params['appendTo'], $output);
                return;
            }
            return $output;
        } else {
            if (is_array($type) && isset($type['customCode'])) {
                require_once 'include/SugarHtml/SugarHtml.php';
                $dom_tree = SugarHtml::parseHtmlTag($type['customCode']);
                $hidden_exists = false;
                replaceFormClick($dom_tree, $js_form, $hidden_exists);
                if ($hidden_exists) {
                    //If the customCode contains hidden fields, the extracted hidden fields need to append in the original form
                    $form = $smarty->get_template_vars('form');
                    $hidden_fields = $dom_tree;
                    extractHiddenInputs($hidden_fields);
                    if (!isset($form)) {
                        $form = array();
                    }
                    if (!isset($form['hidden'])) {
                        $form['hidden'] = array();
                    }
                    $form['hidden'][] = SugarHtml::createHtml($hidden_fields);
                    $smarty->assign('form', $form);
                }
                $output = SugarHtml::createHtml($dom_tree);
                if (isset($params['appendTo'])) {
                    $smarty->append($params['appendTo'], $output);
                    return;
                }
                return $output;
            }
        }
    }
}
/**
 * smarty_function_sugar_button
 * This is the constructor for the Smarty plugin.
 *
 * @param $params The runtime Smarty key/value arguments
 * @param $smarty The reference to the Smarty object used in this invocation
 */
function smarty_function_sugar_button($params, &$smarty)
{
    if (empty($params['module'])) {
        $smarty->trigger_error("sugar_button: missing required param (module)");
    } else {
        if (empty($params['id'])) {
            $smarty->trigger_error("sugar_button: missing required param (id)");
        } else {
            if (empty($params['view'])) {
                $smarty->trigger_error("sugar_button: missing required param (view)");
            }
        }
    }
    $js_form = empty($params['form_id']) ? "var _form = (this.form) ? this.form : document.forms[0];" : "var _form = document.getElementById('{$params['form_id']}');";
    $type = $params['id'];
    $location = empty($params['location']) ? "" : "_" . $params['location'];
    if (!is_array($type)) {
        $module = $params['module'];
        $view = $params['view'];
        switch (strtoupper($type)) {
            case "SEARCH":
                $output = '<input tabindex="2" title="{$APP.LBL_SEARCH_BUTTON_TITLE}" onclick="SUGAR.savedViews.setChooser();" class="button" type="submit" name="button" value="{$APP.LBL_SEARCH_BUTTON_LABEL}" id="search_form_submit"/>&nbsp;';
                break;
            case "CANCEL":
                $cancelButton = '{if !empty($smarty.request.return_action) && ($smarty.request.return_action == "DetailView" && !empty($smarty.request.return_id))}';
                $cancelButton .= '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="SUGAR.ajaxUI.loadContent(\'index.php?action=DetailView&module={$smarty.request.return_module|escape:"url"}&record={$smarty.request.return_id|escape:"url"}\'); return false;" name="button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}" type="button" id="' . $type . $location . '"> ';
                $cancelButton .= '{elseif !empty($smarty.request.return_action) && ($smarty.request.return_action == "DetailView" && !empty($fields.id.value))}';
                $cancelButton .= '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="SUGAR.ajaxUI.loadContent(\'index.php?action=DetailView&module={$smarty.request.return_module|escape:"url"}&record={$fields.id.value}\'); return false;" type="button" name="button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}" id="' . $type . $location . '"> ';
                $cancelButton .= '{elseif empty($smarty.request.return_action) || empty($smarty.request.return_id) && !empty($fields.id.value)}';
                $cancelButton .= '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="SUGAR.ajaxUI.loadContent(\'index.php?action=index&module=' . $module . '\'); return false;" type="button" name="button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}" id="' . $type . $location . '"> ';
                $cancelButton .= '{else}';
                $cancelButton .= '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="SUGAR.ajaxUI.loadContent(\'index.php?action=index&module={$smarty.request.return_module|escape:"url"}&record={$smarty.request.return_id|escape:"url"}\'); return false;" type="button" name="button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}" id="' . $type . $location . '"> ';
                $cancelButton .= '{/if}';
                $output = $cancelButton;
                break;
            case "DELETE":
                $output = '{if $bean->aclAccess("delete")}<input title="{$APP.LBL_DELETE_BUTTON_TITLE}" accessKey="{$APP.LBL_DELETE_BUTTON_KEY}" class="button" onclick="' . $js_form . ' _form.return_module.value=\'' . $module . '\'; _form.return_action.value=\'ListView\'; _form.action.value=\'Delete\'; if(confirm(\'{$APP.NTC_DELETE_CONFIRMATION}\')) SUGAR.ajaxUI.submitForm(_form);" type="submit" name="Delete" value="{$APP.LBL_DELETE_BUTTON_LABEL}" id="delete_button">{/if} ';
                break;
            case "DUPLICATE":
                $output = '{if $bean->aclAccess("edit")}<input title="{$APP.LBL_DUPLICATE_BUTTON_TITLE}" accessKey="{$APP.LBL_DUPLICATE_BUTTON_KEY}" class="button" onclick="' . $js_form . ' _form.return_module.value=\'' . $module . '\'; _form.return_action.value=\'DetailView\'; _form.isDuplicate.value=true; _form.action.value=\'' . $view . '\'; _form.return_id.value=\'{$id}\';SUGAR.ajaxUI.submitForm(_form);" type="button" name="Duplicate" value="{$APP.LBL_DUPLICATE_BUTTON_LABEL}" id="duplicate_button">{/if} ';
                break;
            case "EDIT":
                $output = '{if $bean->aclAccess("edit")}<input title="{$APP.LBL_EDIT_BUTTON_TITLE}" accessKey="{$APP.LBL_EDIT_BUTTON_KEY}" class="button primary" onclick="' . $js_form . ' _form.return_module.value=\'' . $module . '\'; _form.return_action.value=\'DetailView\'; _form.return_id.value=\'{$id}\'; _form.action.value=\'EditView\';SUGAR.ajaxUI.submitForm(_form);" type="button" name="Edit" id="edit_button" value="{$APP.LBL_EDIT_BUTTON_LABEL}">{/if} ';
                break;
            case "FIND_DUPLICATES":
                $output = '{if $bean->aclAccess("edit") && $bean->aclAccess("delete")}<input title="{$APP.LBL_DUP_MERGE}" class="button" onclick="' . $js_form . ' _form.return_module.value=\'' . $module . '\'; _form.return_action.value=\'DetailView\'; _form.return_id.value=\'{$id}\'; _form.action.value=\'Step1\'; _form.module.value=\'MergeRecords\';SUGAR.ajaxUI.submitForm(_form);" type="button" name="Merge" value="{$APP.LBL_DUP_MERGE}" id="merge_duplicate_button">{/if} ';
                break;
            case "SAVE":
                $view = $_REQUEST['action'] == 'EditView' ? 'EditView' : ($view == 'EditView' ? 'EditView' : $view);
                $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}" accessKey="{$APP.LBL_SAVE_BUTTON_KEY}" class="button primary" onclick="' . $js_form . ' {if $isDuplicate}_form.return_id.value=\'\'; {/if}_form.action.value=\'Save\'; if(check_form(\'' . $view . '\'))SUGAR.ajaxUI.submitForm(_form);return false;" type="submit" name="button" value="{$APP.LBL_SAVE_BUTTON_LABEL}" id="' . $type . $location . '">{/if} ';
                break;
            case "SUBPANELSAVE":
                if ($view == 'QuickCreate' || isset($_REQUEST['target_action']) && strtolower($_REQUEST['target_action']) == 'quickcreate') {
                    $view = "form_SubpanelQuickCreate_{$module}";
                }
                $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}"  class="button" onclick="' . $js_form . ' disableOnUnloadEditView(); _form.action.value=\'Save\';if(check_form(\'' . $view . '\'))return SUGAR.subpanelUtils.inlineSave(_form.id, \'' . $params['module'] . '_subpanel_save_button\');return false;" type="submit" name="' . $params['module'] . '_subpanel_save_button" id="' . $params['module'] . '_subpanel_save_button" value="{$APP.LBL_SAVE_BUTTON_LABEL}">{/if} ';
                break;
            case "SUBPANELCANCEL":
                $output = '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" class="button" onclick="return SUGAR.subpanelUtils.cancelCreate($(this).attr(\'id\'));return false;" type="submit" name="' . $params['module'] . '_subpanel_cancel_button" id="' . $params['module'] . '_subpanel_cancel_button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}"> ';
                break;
            case "SUBPANELFULLFORM":
                $output = '<input title="{$APP.LBL_FULL_FORM_BUTTON_TITLE}" class="button" onclick="' . $js_form . ' disableOnUnloadEditView(_form); _form.return_action.value=\'DetailView\'; _form.action.value=\'EditView\'; if(typeof(_form.to_pdf)!=\'undefined\') _form.to_pdf.value=\'0\';" type="submit" name="' . $params['module'] . '_subpanel_full_form_button" id="' . $params['module'] . '_subpanel_full_form_button" value="{$APP.LBL_FULL_FORM_BUTTON_LABEL}"> ';
                $output .= '<input type="hidden" name="full_form" value="full_form">';
                break;
            case "DCMENUCANCEL":
                $output = '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="javascript:lastLoadedMenu=undefined;DCMenu.closeOverlay();return false;" type="submit" name="' . $params['module'] . '_dcmenu_cancel_button" id="' . $params['module'] . '_dcmenu_cancel_button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}"> ';
                break;
            case "DCMENUSAVE":
                if ($view == 'QuickCreate') {
                    $view = "form_DCQuickCreate_{$module}";
                } else {
                    if ($view == 'EditView') {
                        $view = "form_DCEditView_{$module}";
                    }
                }
                $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}" accessKey="{$APP.LBL_SAVE_BUTTON_KEY}" class="button primary" onclick="' . $js_form . ' _form.action.value=\'Save\';if(check_form(\'' . $view . '\'))return DCMenu.save(_form.id, \'' . $params['module'] . '_subpanel_save_button\');return false;" type="submit" name="' . $params['module'] . '_dcmenu_save_button" id="' . $params['module'] . '_dcmenu_save_button" value="{$APP.LBL_SAVE_BUTTON_LABEL}">{/if} ';
                break;
            case "DCMENUFULLFORM":
                $output = '<input title="{$APP.LBL_FULL_FORM_BUTTON_TITLE}" accessKey="{$APP.LBL_FULL_FORM_BUTTON_KEY}" class="button" onclick="' . $js_form . ' disableOnUnloadEditView(_form); _form.return_action.value=\'DetailView\'; _form.action.value=\'EditView\'; _form.return_module.value=\'' . $params['module'] . '\';_form.return_id.value=_form.record.value;if(typeof(_form.to_pdf)!=\'undefined\') _form.to_pdf.value=\'0\';SUGAR.ajaxUI.submitForm(_form,null,true);DCMenu.closeOverlay();" type="button" name="' . $params['module'] . '_subpanel_full_form_button" id="' . $params['module'] . '_subpanel_full_form_button" value="{$APP.LBL_FULL_FORM_BUTTON_LABEL}"> ';
                $output .= '<input type="hidden" name="full_form" value="full_form">';
                $output .= '<input type="hidden" name="is_admin" value="">';
                break;
            case "POPUPSAVE":
                $view = $view == 'QuickCreate' ? "form_QuickCreate_{$module}" : $view;
                $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}" accessKey="{$APP.LBL_SAVE_BUTTON_KEY}" ' . 'class="button primary" onclick="' . $js_form . ' _form.action.value=\'Popup\';' . 'return check_form(\'' . $view . '\')" type="submit" name="' . $params['module'] . '_popupcreate_save_button" id="' . $params['module'] . '_popupcreate_save_button" value="{$APP.LBL_SAVE_BUTTON_LABEL}">{/if} ';
                break;
            case "POPUPCANCEL":
                $output = '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" ' . 'class="button" onclick="toggleDisplay(\'addform\');return false;" ' . 'name="' . $params['module'] . '_popup_cancel_button" type="submit"' . 'id="' . $params['module'] . '_popup_cancel_button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}"> ';
                break;
            case "AUDIT":
                $popup_request_data = array('call_back_function' => 'set_return', 'form_name' => 'EditView', 'field_to_name_array' => array());
                $json = getJSONobj();
                require_once 'include/SugarFields/Parsers/MetaParser.php';
                $encoded_popup_request_data = MetaParser::parseDelimiters($json->encode($popup_request_data));
                $audit_link = '<input id="btn_view_change_log" title="{$APP.LNK_VIEW_CHANGE_LOG}" class="button" onclick=\'open_popup("Audit", "600", "400", "&record={$fields.id.value}&module_name=' . $params['module'] . '", true, false, ' . $encoded_popup_request_data . '); return false;\' type="button" value="{$APP.LNK_VIEW_CHANGE_LOG}">';
                $output = '{if $bean->aclAccess("detail")}{if !empty($fields.id.value) && $isAuditEnabled}' . $audit_link . '{/if}{/if}';
                break;
        }
        //switch
        if (isset($params['appendTo'])) {
            $smarty->append($params['appendTo'], $output);
            return;
        }
        return $output;
    } else {
        if (is_array($type) && isset($type['sugar_html'])) {
            require_once 'include/SugarHtml/SugarHtml.php';
            $dom_tree = SugarHtml::parseSugarHtml($type['sugar_html']);
            replaceFormClick($dom_tree, $js_form);
            $output = SugarHtml::createHtml($dom_tree);
            if (isset($params['appendTo'])) {
                $smarty->append($params['appendTo'], $output);
                return;
            }
            return $output;
        } else {
            if (is_array($type) && isset($type['customCode'])) {
                require_once 'include/SugarHtml/SugarHtml.php';
                $dom_tree = SugarHtml::parseHtmlTag($type['customCode']);
                $hidden_exists = false;
                replaceFormClick($dom_tree, $js_form, $hidden_exists);
                if ($hidden_exists) {
                    //If the customCode contains hidden fields, the extracted hidden fields need to append in the original form
                    $form = $smarty->get_template_vars('form');
                    $hidden_fields = $dom_tree;
                    extractHiddenInputs($hidden_fields);
                    if (!isset($form)) {
                        $form = array();
                    }
                    if (!isset($form['hidden'])) {
                        $form['hidden'] = array();
                    }
                    $form['hidden'][] = SugarHtml::createHtml($hidden_fields);
                    $smarty->assign('form', $form);
                }
                $output = SugarHtml::createHtml($dom_tree);
                if (isset($params['appendTo'])) {
                    $smarty->append($params['appendTo'], $output);
                    return;
                }
                return $output;
            }
        }
    }
}