| @@ -1,103 +1,114 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace AcyMailing\Helpers; |
| 4 | 4 | |
| 5 | +use AcyMailing\Classes\MailClass; | |
| 5 | 6 | use AcyMailing\Core\AcymObject; |
| 6 | 7 | |
| 7 | 8 | class WorkflowHelper extends AcymObject |
| 8 | 9 | { |
| 10 | + // Disable all the steps after this one | |
| 9 | 11 | public string $disabledAfter = ''; |
| 10 | 12 | |
| 11 | - public function display(array $steps, string $currentStep, bool $editionMode = true, bool $needTabs = false, string $linkParameters = '', string $idName = 'id'): string | |
| 12 | - { | |
| 13 | + /** | |
| 14 | + * Call this helper in an element with the class acym__content | |
| 15 | + * The steps are defined in the Main view class | |
| 16 | + * Put acym_formOptions(true, 'edit', 'CURRENT STEP') at the end of each step | |
| 17 | + */ | |
| 18 | + public function display( | |
| 19 | + array $steps, | |
| 20 | + string $currentStep, | |
| 21 | + bool $editionMode = true, | |
| 22 | + bool $needTabs = false, | |
| 23 | + string $linkParameters = '', | |
| 24 | + string $idName = 'id' | |
| 25 | + ): void { | |
| 13 | 26 | $ctrl = acym_getVar('cmd', 'ctrl'); |
| 14 | 27 | $id = acym_getVar('int', $idName, 0); |
| 15 | 28 | |
| 16 | - $workflow = []; | |
| 29 | + echo '<ul id="workflow">'; | |
| 30 | + | |
| 17 | 31 | $disableTabs = false; |
| 32 | + $step = 0; | |
| 18 | 33 | foreach ($steps as $task => $title) { |
| 19 | - $title = acym_translation($title); | |
| 34 | + $class = 'step'; | |
| 35 | + if ($disableTabs) { | |
| 36 | + $class .= ' disabled_step'; | |
| 37 | + } | |
| 38 | + if ($currentStep === $task) { | |
| 39 | + $class .= ' current_step'; | |
| 40 | + } | |
| 20 | 41 | |
| 21 | - $class = 'step'; | |
| 22 | - if ($disableTabs) $class .= ' disabled_step'; | |
| 23 | - if ($currentStep === $task) $class .= ' current_step'; | |
| 42 | + if ($step !== 0) { | |
| 43 | + echo '<li class="step_separator '.esc_attr($needTabs ? '' : 'acymicon-keyboard-arrow-right').'"></li>'; | |
| 44 | + } | |
| 45 | + echo '<li class="'.esc_attr($class).'">'; | |
| 24 | 46 | |
| 25 | 47 | if (!$disableTabs) { |
| 26 | - if ($editionMode) { | |
| 27 | - $link = $ctrl.'&task=edit&step='.$task.'&'.$idName.'='.$id; | |
| 28 | - } else { | |
| 29 | - $link = $ctrl.'&task='.$task; | |
| 30 | - } | |
| 31 | - $title = '<a href="'.acym_completeLink($link.$linkParameters).'">'.$title.'</a>'; | |
| 48 | + $link = $editionMode ? $ctrl.'&task=edit&step='.$task.'&'.$idName.'='.$id : $ctrl.'&task='.$task; | |
| 49 | + echo '<a href="'.esc_url(acym_completeLink($link.$linkParameters)).'">'; | |
| 32 | 50 | } |
| 51 | + echo esc_html(acym_translation($title)); | |
| 52 | + if (!$disableTabs) { | |
| 53 | + echo '</a>'; | |
| 54 | + } | |
| 55 | + echo '</li>'; | |
| 33 | 56 | |
| 34 | - $workflow[] = '<li class="'.$class.'">'.$title.'</li>'; | |
| 35 | - $workflow[] = '<li class="step_separator '.($needTabs ? '' : 'acymicon-keyboard-arrow-right').'"></li>'; | |
| 36 | - | |
| 37 | 57 | if ($task === $this->disabledAfter) { |
| 38 | 58 | $disableTabs = true; |
| 39 | 59 | } |
| 60 | + | |
| 61 | + $step++; | |
| 40 | 62 | } |
| 41 | 63 | |
| 42 | - array_pop($workflow); | |
| 43 | - | |
| 44 | - $result = '<ul id="workflow">'; | |
| 45 | - $result .= implode('', $workflow); | |
| 46 | - $result .= '</ul>'; | |
| 47 | - | |
| 48 | - return $result; | |
| 64 | + echo '</ul>'; | |
| 49 | 65 | } |
| 50 | 66 | |
| 51 | - public function displayNew(array $steps, string $currentStep, bool $edition = true, string $linkParameters = '', string $idName = 'id'): string | |
| 67 | + public function displayNew(array $steps, string $currentStep, bool $edition = true, string $linkParameters = '', string $idName = 'id'): void | |
| 52 | 68 | { |
| 53 | 69 | $ctrl = acym_getVar('cmd', 'ctrl'); |
| 54 | 70 | $id = acym_getVar('int', $idName, 0); |
| 55 | 71 | |
| 56 | - $workflow = []; | |
| 72 | + echo '<div class="acym__workflow">'; | |
| 73 | + | |
| 57 | 74 | $disableTabs = false; |
| 58 | 75 | foreach ($steps as $task => $title) { |
| 59 | 76 | if ($disableTabs) { |
| 60 | 77 | continue; |
| 61 | 78 | } |
| 62 | - $title = acym_translation($title); | |
| 63 | 79 | |
| 64 | 80 | $classCurrentStep = $currentStep === $task ? 'acym__workflow__step__active' : ''; |
| 65 | 81 | $classDisabled = $disableTabs ? 'acym__workflow__step__disabled' : ''; |
| 66 | 82 | |
| 67 | - $label = $title; | |
| 83 | + echo '<div class="acym__workflow__step '.esc_attr($classDisabled.' '.$classCurrentStep).'">'; | |
| 68 | 84 | if (!$disableTabs) { |
| 69 | - if ($edition) { | |
| 70 | - $link = $ctrl.'&task=edit&step='.$task.'&'.$idName.'='.$id; | |
| 71 | - } else { | |
| 72 | - $link = $ctrl.'&task='.$task; | |
| 73 | - } | |
| 74 | - $label = '<a href="'.acym_completeLink($link.$linkParameters).'">'.$title.'</a>'; | |
| 85 | + $link = $edition ? $ctrl.'&task=edit&step='.$task.'&'.$idName.'='.$id : $ctrl.'&task='.$task; | |
| 86 | + echo '<a href="'.esc_url(acym_completeLink($link.$linkParameters)).'">'; | |
| 75 | 87 | } |
| 88 | + echo esc_html(acym_translation($title)); | |
| 89 | + if (!$disableTabs) { | |
| 90 | + echo '</a>'; | |
| 91 | + } | |
| 92 | + echo '</div>'; | |
| 76 | 93 | |
| 77 | - $step = '<div class="acym__workflow__step '.$classDisabled.' '.$classCurrentStep.'">'.$label.'</div>'; | |
| 78 | - | |
| 79 | - $workflow[] = $step; | |
| 80 | - | |
| 81 | 94 | if ($task === $this->disabledAfter) { |
| 82 | 95 | $disableTabs = true; |
| 83 | 96 | } |
| 84 | 97 | } |
| 85 | 98 | |
| 86 | - $result = '<div class="acym__workflow">'; | |
| 87 | - $result .= implode('', $workflow); | |
| 88 | - $result .= '</div>'; | |
| 89 | - | |
| 90 | - return $result; | |
| 99 | + echo '</div>'; | |
| 91 | 100 | } |
| 92 | 101 | |
| 93 | - public function displayTabs(array $steps, string $currentStep, array $options = []): string | |
| 102 | + public function displayTabs(array $steps, string $currentStep, array $options = []): void | |
| 94 | 103 | { |
| 95 | 104 | $ctrl = acym_getVar('cmd', 'ctrl'); |
| 96 | 105 | |
| 106 | + echo '<ul class="tabs" id="workflow">'; | |
| 107 | + | |
| 108 | + $mailClass = new MailClass(); | |
| 97 | 109 | $mailTypes = ['mailbox_action']; |
| 98 | 110 | |
| 99 | - $workflow = []; | |
| 100 | 111 | foreach ($steps as $task => $title) { |
| 101 | 112 | if (in_array($task, $mailTypes)) { |
| 102 | 113 | $searchSettings = [ |
| 103 | 114 | 'offset' => 0, |
| @@ -103,19 +114,15 @@ | ||
| 103 | 114 | 'offset' => 0, |
| 104 | 115 | 'mailsPerPage' => 1, |
| 105 | 116 | 'key' => '', |
| 106 | 117 | ]; |
| 107 | - $mailClass = new \AcyMailing\Classes\MailClass(); | |
| 108 | 118 | $existingMailbox = $mailClass->getMailsByType($task, $searchSettings); |
| 119 | + | |
| 109 | 120 | if (empty($existingMailbox['mails'])) { |
| 110 | 121 | continue; |
| 111 | 122 | } |
| 112 | 123 | } |
| 113 | 124 | |
| 114 | - $title = acym_translation($title); | |
| 115 | - | |
| 116 | - $linkAttribute = $currentStep === $task ? 'aria-selected="true"' : ''; | |
| 117 | - | |
| 118 | 125 | if (!empty($options['query'])) { |
| 119 | 126 | $link = $ctrl.$options['query'].$task; |
| 120 | 127 | } else { |
| 121 | 128 | $link = $ctrl.'&task='.$task; |
| @@ -122,21 +129,23 @@ | ||
| 122 | 129 | } |
| 123 | 130 | |
| 124 | 131 | if (!empty($options['disableTabs']) && in_array($task, $options['disableTabs'])) { |
| 125 | 132 | $link = ''; |
| 126 | - $linkAttribute = 'aria-disabled="true"'; | |
| 127 | 133 | } |
| 128 | 134 | |
| 129 | - $hrefAttribute = $link ? 'href="'.acym_completeLink($link).'"' : ''; | |
| 130 | - | |
| 131 | - $title = '<a class="acym_tab acym__color__medium-gray" '.$linkAttribute.' '.$hrefAttribute.'>'.$title.'</a>'; | |
| 132 | - | |
| 133 | - $workflow[] = '<li class="tabs-title">'.$title.'</li>'; | |
| 135 | + echo '<li class="tabs-title"><a class="acym_tab acym__color__medium-gray"'; | |
| 136 | + if (!empty($link)) { | |
| 137 | + echo ' href="'.esc_url(acym_completeLink($link)).'"'; | |
| 138 | + if ($currentStep === $task) { | |
| 139 | + echo ' aria-selected="true"'; | |
| 140 | + } | |
| 141 | + } else { | |
| 142 | + echo ' aria-disabled="true"'; | |
| 143 | + } | |
| 144 | + echo '>'; | |
| 145 | + echo esc_html(acym_translation($title)); | |
| 146 | + echo '</a></li>'; | |
| 134 | 147 | } |
| 135 | 148 | |
| 136 | - $result = '<ul class="tabs" id="workflow">'; | |
| 137 | - $result .= implode('', $workflow); | |
| 138 | - $result .= '</ul>'; | |
| 139 | - | |
| 140 | - return $result; | |
| 149 | + echo '</ul>'; | |
| 141 | 150 | } |
| 142 | 151 | } |