From 7479e9a4ae0c74faef53ac91b12fc43799db325a Mon Sep 17 00:00:00 2001 From: eileen Date: Sun, 4 Feb 2024 18:25:28 +1300 Subject: [PATCH] Fold functions that only support one form back into that form (surver) --- CRM/Campaign/Form/Survey.php | 153 ++++++++++++++++++++++++- CRM/Campaign/Form/Survey/TabHeader.php | 17 ++- 2 files changed, 163 insertions(+), 7 deletions(-) diff --git a/CRM/Campaign/Form/Survey.php b/CRM/Campaign/Form/Survey.php index 74d1563dad45..15942585c03e 100644 --- a/CRM/Campaign/Form/Survey.php +++ b/CRM/Campaign/Form/Survey.php @@ -90,13 +90,125 @@ public function preProcess() { CRM_UF_Page_ProfileEditor::registerProfileScripts(); CRM_UF_Page_ProfileEditor::registerSchemas(['IndividualModel', 'ActivityModel']); - CRM_Campaign_Form_Survey_TabHeader::build($this); + $this->build(); + } + + /** + * Build tab header. + * + * @return array + */ + private function build() { + $form = $this; + $tabs = $form->get('tabHeader'); + if (!$tabs || empty($_GET['reset'])) { + $tabs = $this->processSurveyForm(); + $form->set('tabHeader', $tabs); + } + $form->assign('tabHeader', $tabs); + CRM_Core_Resources::singleton() + ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header') + ->addSetting([ + 'tabSettings' => [ + 'active' => $this->getCurrentTab($tabs), + ], + ]); + return $tabs; + } + + /** + * @param array $tabs + * + * @return int|string + */ + private function getCurrentTab($tabs) { + static $current = FALSE; + + if ($current) { + return $current; + } + + if (is_array($tabs)) { + foreach ($tabs as $subPage => $pageVal) { + if ($pageVal['current'] === TRUE) { + $current = $subPage; + break; + } + } + } + + $current = $current ?: 'main'; + return $current; + } + + /** + * + * @return array + */ + private function processSurveyForm() { + $form = $this; + if ($form->getVar('_surveyId') <= 0) { + return NULL; + } + + $tabs = [ + 'main' => [ + 'title' => ts('Main Information'), + 'link' => NULL, + 'valid' => FALSE, + 'active' => FALSE, + 'current' => FALSE, + ], + 'questions' => [ + 'title' => ts('Questions'), + 'link' => NULL, + 'valid' => FALSE, + 'active' => FALSE, + 'current' => FALSE, + ], + 'results' => [ + 'title' => ts('Results'), + 'link' => NULL, + 'valid' => FALSE, + 'active' => FALSE, + 'current' => FALSE, + ], + ]; + + $surveyID = $form->getVar('_surveyId'); + $class = $form->getVar('_name'); + $class = CRM_Utils_String::getClassName($class); + $class = strtolower($class); + + if (array_key_exists($class, $tabs)) { + $tabs[$class]['current'] = TRUE; + $qfKey = $form->get('qfKey'); + if ($qfKey) { + $tabs[$class]['qfKey'] = "&qfKey={$qfKey}"; + } + } + + if ($surveyID) { + $reset = !empty($_GET['reset']) ? 'reset=1&' : ''; + + foreach ($tabs as $key => $value) { + if (!isset($tabs[$key]['qfKey'])) { + $tabs[$key]['qfKey'] = NULL; + } + + $tabs[$key]['link'] = CRM_Utils_System::url("civicrm/survey/configure/{$key}", + "{$reset}action=update&id={$surveyID}{$tabs[$key]['qfKey']}" + ); + $tabs[$key]['active'] = $tabs[$key]['valid'] = TRUE; + } + } + return $tabs; } /** * Build the form object. */ - public function buildQuickForm() { + public function buildQuickForm(): void { $session = CRM_Core_Session::singleton(); if ($this->_surveyId) { $buttons = [ @@ -131,7 +243,7 @@ public function endPostProcess() { // make submit buttons keep the current working tab opened. if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) { $tabTitle = $className = CRM_Utils_String::getClassName($this->_name); - if ($tabTitle == 'Main') { + if ($tabTitle === 'Main') { $tabTitle = 'Main settings'; } $subPage = strtolower($className); @@ -143,11 +255,11 @@ public function endPostProcess() { CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/survey/configure/questions", "action=update&reset=1&id={$this->_surveyId}")); } - if ($this->controller->getButtonName('submit') == "_qf_{$className}_upload_done") { + if ($this->controller->getButtonName('submit') === "_qf_{$className}_upload_done") { CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey')); } - elseif ($this->controller->getButtonName('submit') == "_qf_{$className}_upload_next") { - $subPage = CRM_Campaign_Form_Survey_TabHeader::getNextTab($this); + elseif ($this->controller->getButtonName('submit') === "_qf_{$className}_upload_next") { + $subPage = $this->getNextTab(); CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/survey/configure/{$subPage}", "action=update&reset=1&id={$this->_surveyId}")); } @@ -170,4 +282,33 @@ public function getTemplateFileName(): string { return 'CRM/Campaign/Form/Survey/Tab.tpl'; } + /** + * + * @return int|string + */ + private function getNextTab() { + $form = $this; + static $next = FALSE; + if ($next) { + return $next; + } + + $tabs = $form->get('tabHeader'); + if (is_array($tabs)) { + $current = FALSE; + foreach ($tabs as $subPage => $pageVal) { + if ($current) { + $next = $subPage; + break; + } + if ($pageVal['current'] === TRUE) { + $current = $subPage; + } + } + } + + $next = $next ?: 'main'; + return $next; + } + } diff --git a/CRM/Campaign/Form/Survey/TabHeader.php b/CRM/Campaign/Form/Survey/TabHeader.php index 2856c09a3e7a..eba2bbc4c1be 100644 --- a/CRM/Campaign/Form/Survey/TabHeader.php +++ b/CRM/Campaign/Form/Survey/TabHeader.php @@ -16,18 +16,21 @@ */ /** - * Helper class to build navigation links + * @deprecated since 5.71 will be removed around 5.77 */ class CRM_Campaign_Form_Survey_TabHeader { /** * Build tab header. * + * @deprecated since 5.71 will be removed around 5.77 + * * @param CRM_Core_Form $form * * @return array */ public static function build(&$form) { + CRM_Core_Error::deprecatedFunctionWarning('no alternative'); $tabs = $form->get('tabHeader'); if (!$tabs || empty($_GET['reset'])) { $tabs = self::process($form); @@ -48,8 +51,11 @@ public static function build(&$form) { * @param CRM_Core_Form $form * * @return array + * + * @deprecated since 5.71 will be removed around 5.77 */ public static function process(&$form) { + CRM_Core_Error::deprecatedFunctionWarning('no alternative'); if ($form->getVar('_surveyId') <= 0) { return NULL; } @@ -110,8 +116,11 @@ public static function process(&$form) { /** * @param CRM_Core_Form $form + * + * @deprecated since 5.71 will be removed around 5.77 */ public static function reset(&$form) { + CRM_Core_Error::deprecatedFunctionWarning('no alternative'); $tabs = self::process($form); $form->set('tabHeader', $tabs); } @@ -120,8 +129,11 @@ public static function reset(&$form) { * @param array $tabs * * @return int|string + * + * @deprecated since 5.71 will be removed around 5.77 */ public static function getCurrentTab($tabs) { + CRM_Core_Error::deprecatedFunctionWarning('no alternative'); static $current = FALSE; if ($current) { @@ -145,8 +157,11 @@ public static function getCurrentTab($tabs) { * @param CRM_Core_Form $form * * @return int|string + * + * @deprecated since 5.71 will be removed around 5.77 */ public static function getNextTab(&$form) { + CRM_Core_Error::deprecatedFunctionWarning('no alternative'); static $next = FALSE; if ($next) { return $next;