Skip to content

Commit

Permalink
Merge pull request #29232 from eileenmcnaughton/survery_move
Browse files Browse the repository at this point in the history
Fold functions that only support one form back into that form (survey)
  • Loading branch information
colemanw authored Feb 5, 2024
2 parents 8bbff81 + 7479e9a commit 7cb644e
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 7 deletions.
153 changes: 147 additions & 6 deletions CRM/Campaign/Form/Survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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);
Expand All @@ -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}"));
}
Expand All @@ -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;
}

}
17 changes: 16 additions & 1 deletion CRM/Campaign/Form/Survey/TabHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 7cb644e

Please sign in to comment.