| @@ -5,78 +5,91 @@ | ||
| 5 | 5 | use AcyMailing\Core\AcymObject; |
| 6 | 6 | |
| 7 | 7 | class EntitySelectHelper extends AcymObject |
| 8 | 8 | { |
| 9 | - private string $svg; | |
| 10 | 9 | private array $columnsHeaderNotToDisplay; |
| 11 | 10 | |
| 12 | 11 | public function __construct() |
| 13 | 12 | { |
| 14 | 13 | parent::__construct(); |
| 15 | - $this->svg = acym_loaderLogo(false); | |
| 16 | 14 | $this->columnsHeaderNotToDisplay = ['color']; |
| 17 | 15 | } |
| 18 | 16 | |
| 19 | - public function entitySelect( | |
| 20 | - string $entity, | |
| 21 | - array $entityParams = [], | |
| 22 | - array $columnsToDisplay = ['name'], | |
| 23 | - array $buttonSubmit = [ | |
| 17 | + public function entitySelect(array $options): string | |
| 18 | + { | |
| 19 | + $options['columnsToDisplay'] = $options['columnsToDisplay'] ?? ['name']; | |
| 20 | + $options['buttonSubmit'] = $options['buttonSubmit'] ?? [ | |
| 24 | 21 | 'text' => '', |
| 25 | 22 | 'action' => '', |
| 26 | 23 | 'class' => '', |
| 27 | - ], | |
| 28 | - bool $displaySelected = true, | |
| 29 | - string $additionalData = '', | |
| 30 | - string $displayedName = '' | |
| 31 | - ): string { | |
| 24 | + ]; | |
| 25 | + $options['displaySelected'] = $options['displaySelected'] ?? true; | |
| 26 | + $options['displayedName'] = $options['displayedName'] ?? ''; | |
| 27 | + | |
| 32 | 28 | $columnJoin = ''; |
| 33 | - if (!empty($columnsToDisplay['join'])) { | |
| 34 | - $columnJoin = explode('.', $columnsToDisplay['join']); | |
| 29 | + if (!empty($options['columnsToDisplay']['join'])) { | |
| 30 | + $columnJoin = explode('.', $options['columnsToDisplay']['join']); | |
| 35 | 31 | } |
| 36 | 32 | |
| 37 | - unset($columnsToDisplay['join']); | |
| 33 | + unset($options['columnsToDisplay']['join']); | |
| 38 | 34 | |
| 39 | - if (empty($entityParams['elementsPerPage']) || $entityParams['elementsPerPage'] < 1) { | |
| 40 | - $paginationHelper = new PaginationHelper(); | |
| 41 | - $entityParams['elementsPerPage'] = $paginationHelper->getListLimit(); | |
| 35 | + if (empty($options['display'])) { | |
| 36 | + ob_start(); | |
| 42 | 37 | } |
| 43 | 38 | |
| 44 | - if (!empty($columnJoin)) $columnJoin = 'data-column-join="'.$columnJoin[1].'" data-table-join="'.$columnJoin[0].'"'; | |
| 45 | - $display = '<div | |
| 46 | - style="display: none;" | |
| 47 | - id="acym__entity_select" | |
| 48 | - class="acym__entity_select cell grid-x" | |
| 49 | - data-display-selected="'.($displaySelected ? 'true' : 'false').'" | |
| 50 | - data-entity="'.acym_escape($entity).'" | |
| 51 | - data-type="select" | |
| 52 | - data-columns="'.implode(',', array_keys($columnsToDisplay)).'" | |
| 53 | - data-columns-class="'.acym_escape(json_encode($columnsToDisplay)).'" | |
| 54 | - data-join="'.$entityParams['join'].'" | |
| 55 | - '.$columnJoin.'>'; | |
| 39 | + echo '<div | |
| 40 | + style="display: none;" | |
| 41 | + id="acym__entity_select" | |
| 42 | + class="acym__entity_select cell grid-x" | |
| 43 | + data-display-selected="'.($options['displaySelected'] ? 'true' : 'false').'" | |
| 44 | + data-entity="'.esc_attr($options['entity']).'" | |
| 45 | + data-type="select" | |
| 46 | + data-columns="'.esc_attr(implode(',', array_keys($options['columnsToDisplay']))).'" | |
| 47 | + data-columns-class="'.esc_attr(json_encode($options['columnsToDisplay'])).'" | |
| 48 | + data-join="'.esc_attr($options['entityParams']['join'] ?? '').'" '; | |
| 56 | 49 | |
| 57 | - $display .= $this->getListing('available', 'select', $entity, $columnsToDisplay, $displayedName); | |
| 58 | - $display .= '<div class="cell medium-shrink text-center grid-x acym_vcenter"><i class="acymicon-arrows-h cell"></i></div>'; | |
| 59 | - $display .= $this->getListing('selected', 'unselect', $entity, $columnsToDisplay, $displayedName); | |
| 60 | - $display .= $additionalData; | |
| 50 | + if (!empty($columnJoin)) { | |
| 51 | + echo ' data-column-join="'.esc_attr($columnJoin[1]).'" data-table-join="'.esc_attr($columnJoin[0]).'"'; | |
| 52 | + } | |
| 53 | + echo '>'; | |
| 61 | 54 | |
| 62 | - if (!empty($buttonSubmit['text'])) { | |
| 63 | - $class = !empty($buttonSubmit['action']) ? 'acy_button_submit' : 'acym__entity_select__button__close'; | |
| 64 | - if (!empty($buttonSubmit['class'])) $class .= ' '.$buttonSubmit['class']; | |
| 65 | - $buttonSubmit['action'] = !empty($buttonSubmit['action']) ? 'data-task="'.$buttonSubmit['action'].'"' : ''; | |
| 66 | - $display .= '<div class="cell grid-x align-center margin-top-1">'; | |
| 67 | - $display .= '<button | |
| 68 | - type="button" | |
| 69 | - id="acym__entity_select__button__submit" | |
| 70 | - class="cell shrink grid-x '.$class.' button" '.$buttonSubmit['action'].'>'.$buttonSubmit['text'].'</button>'; | |
| 71 | - $display .= '</div>'; | |
| 55 | + $this->displayListing('available', 'select', $options['entity'], $options['columnsToDisplay'], $options['displayedName']); | |
| 56 | + echo '<div class="cell medium-shrink text-center grid-x acym_vcenter"><i class="acymicon-arrows-h cell"></i></div>'; | |
| 57 | + $this->displayListing('selected', 'unselect', $options['entity'], $options['columnsToDisplay'], $options['displayedName']); | |
| 58 | + | |
| 59 | + // Callable rendering extra content | |
| 60 | + if (!empty($options['additionalData']) && !is_string($options['additionalData']) && is_callable($options['additionalData'])) { | |
| 61 | + $options['additionalData'](); | |
| 72 | 62 | } |
| 73 | 63 | |
| 74 | - $display .= '<input type="hidden" class="acym__entity_select__selected" name="acym__entity_select__selected" value="">'; | |
| 75 | - $display .= '<input type="hidden" class="acym__entity_select__unselected" name="acym__entity_select__unselected" value="">'; | |
| 76 | - $display .= '</div>'; | |
| 64 | + if (!empty($options['buttonSubmit']['text'])) { | |
| 65 | + $class = !empty($options['buttonSubmit']['action']) ? 'acy_button_submit' : 'acym__entity_select__button__close'; | |
| 66 | + if (!empty($options['buttonSubmit']['class'])) { | |
| 67 | + $class .= ' '.$options['buttonSubmit']['class']; | |
| 68 | + } | |
| 69 | + echo '<div class="cell grid-x align-center margin-top-1">'; | |
| 70 | + echo '<button | |
| 71 | + type="button" | |
| 72 | + id="acym__entity_select__button__submit" | |
| 73 | + class="cell shrink grid-x '.esc_attr($class).' button" '; | |
| 74 | + if (!empty($options['buttonSubmit']['action'])) { | |
| 75 | + echo ' data-task="'.esc_attr($options['buttonSubmit']['action']).'"'; | |
| 76 | + } | |
| 77 | + echo '>'; | |
| 78 | + echo esc_html($options['buttonSubmit']['text']); | |
| 79 | + echo '</button>'; | |
| 80 | + echo '</div>'; | |
| 81 | + } | |
| 77 | 82 | |
| 78 | - return $display; | |
| 83 | + echo '<input type="hidden" class="acym__entity_select__selected" name="acym__entity_select__selected" value="">'; | |
| 84 | + echo '<input type="hidden" class="acym__entity_select__unselected" name="acym__entity_select__unselected" value="">'; | |
| 85 | + echo '</div>'; | |
| 86 | + | |
| 87 | + if (empty($options['display'])) { | |
| 88 | + return ob_get_clean(); | |
| 89 | + } | |
| 90 | + | |
| 91 | + return ''; | |
| 79 | 92 | } |
| 80 | 93 | |
| 81 | 94 | public function getColumnsForList(string $join = '', bool $small = false): array |
| 82 | 95 | { |
| @@ -106,63 +119,92 @@ | ||
| 106 | 119 | |
| 107 | 120 | return $columns; |
| 108 | 121 | } |
| 109 | 122 | |
| 110 | - private function getListing(string $type, string $allSelector, string $entity, array $columnsToDisplay = [], string $displayedName = ''): string | |
| 111 | - { | |
| 123 | + private function displayListing( | |
| 124 | + string $type, | |
| 125 | + string $allSelector, | |
| 126 | + string $entity, | |
| 127 | + array $columnsToDisplay = [], | |
| 128 | + string $displayedName = '' | |
| 129 | + ): void { | |
| 112 | 130 | if (empty($displayedName)) { |
| 113 | 131 | $displayedName = $entity; |
| 114 | 132 | } |
| 115 | 133 | |
| 116 | - $display = '<div class="cell medium-auto grid-x acym_area acym__entity_select__'.$type.'"> | |
| 117 | - <h5 class="cell font-bold acym__title acym__title__secondary text-center">'.acym_translation('ACYM_'.strtoupper($type).'_'.strtoupper($displayedName)).'</h5> | |
| 118 | - <div class="cell grid-x"> | |
| 119 | - <div class="cell grid-x acym__entity_select__header"> | |
| 120 | - <div class="cell grid-x"> | |
| 121 | - <div class="cell margin-bottom-1"><input type="text" v-model="'.$type.'Search" placeholder="'.acym_translation('ACYM_SEARCH').'"></div> | |
| 122 | - <div class="cell align-right grid-x acym__entity_select__select__all"> | |
| 123 | - <button type="button" v-show="!loading" v-if="displaySelectAll_'.$type.'" v-on:click="moveAll('.acym_escapeDB( | |
| 124 | - $type | |
| 125 | - ).')" class="cell shrink acym__entity_select__select__all__button acym__entity_select__select__all__button__'.$type.'">'.acym_translation( | |
| 126 | - 'ACYM_'.strtoupper($allSelector).'_ALL' | |
| 134 | + echo '<div class="cell medium-auto grid-x acym_area acym__entity_select__'.esc_attr($type).'"> | |
| 135 | + <h5 class="cell font-bold acym__title acym__title__secondary text-center">'.esc_html( | |
| 136 | + acym_translation('ACYM_'.strtoupper($type).'_'.strtoupper($displayedName)) | |
| 137 | + ).'</h5> | |
| 138 | + <div class="cell grid-x"> | |
| 139 | + <div class="cell grid-x acym__entity_select__header"> | |
| 140 | + <div class="cell grid-x"> | |
| 141 | + <div class="cell margin-bottom-1"><input type="text" v-model="'.esc_attr($type).'Search" placeholder="'.esc_attr(acym_translation('ACYM_SEARCH')).'"></div> | |
| 142 | + <div class="cell align-right grid-x acym__entity_select__select__all"> | |
| 143 | + <button type="button" | |
| 144 | + v-show="!loading" | |
| 145 | + v-if="displaySelectAll_'.esc_attr($type).'" | |
| 146 | + v-on:click="moveAll('.esc_attr(json_encode($type)).')" | |
| 147 | + class="cell shrink acym__entity_select__select__all__button acym__entity_select__select__all__button__'.esc_attr($type).'">'.esc_html( | |
| 148 | + acym_translation('ACYM_'.strtoupper($allSelector).'_ALL') | |
| 127 | 149 | ).'</button> |
| 128 | 150 | </div> |
| 129 | 151 | </div> |
| 130 | 152 | </div> |
| 131 | - <div v-infinite-scroll="loadMoreEntity'.ucfirst( | |
| 153 | + <div v-infinite-scroll="loadMoreEntity'.esc_attr( | |
| 154 | + ucfirst($type) | |
| 155 | + ).'" :infinite-scroll-disabled="busy" class="acym__listing cell acym__entity_select__'.esc_attr($type).'__listing acym__content" infinite-scroll-distance="10">'; | |
| 156 | + $emptyMessage = acym_translation($type === 'available' ? 'ACYM_NOTHING_TO_SHOW_HERE_RIGHT_PANEL' : 'ACYM_PLEASE_CLICK_ON_THE_LEFT_PANEL'); | |
| 157 | + echo '<div class="cell text-center acym__entity_select__title margin-top-2" v-show="Object.keys(entitiesToDisplay_'.esc_attr( | |
| 132 | 158 | $type |
| 133 | - ).'" :infinite-scroll-disabled="busy" class="acym__listing cell acym__entity_select__'.$type.'__listing acym__content" infinite-scroll-distance="10">'; | |
| 134 | - $emptyMessage = acym_translation($type === 'available' ? 'ACYM_NOTHING_TO_SHOW_HERE_RIGHT_PANEL' : 'ACYM_PLEASE_CLICK_ON_THE_LEFT_PANEL'); | |
| 135 | - $display .= '<div class="cell text-center acym__entity_select__title margin-top-2" v-show="Object.keys(entitiesToDisplay_'.$type.').length == 0 && !loading">'.$emptyMessage.'</div> | |
| 136 | - <div class="cell acym_vcenter acym__listing__row grid-x acym__listing__row__header" v-if="Object.keys(entitiesToDisplay_'.$type.').length != 0">'; | |
| 159 | + ).').length == 0 && !loading">'.esc_html($emptyMessage).'</div> | |
| 160 | + <div class="cell acym_vcenter acym__listing__row grid-x acym__listing__row__header" v-if="Object.keys(entitiesToDisplay_'.esc_attr($type).').length != 0">'; | |
| 137 | 161 | |
| 162 | + //Start the listing | |
| 138 | 163 | |
| 139 | - if ($type !== 'available') $display .= '<div class="cell small-1"></div>'; | |
| 164 | + //if it's the select listing we display the - at the start | |
| 165 | + if ($type !== 'available') { | |
| 166 | + echo '<div class="cell small-1"></div>'; | |
| 167 | + } | |
| 140 | 168 | |
| 169 | + //We lists all the columns | |
| 141 | 170 | foreach ($columnsToDisplay as $column => $class) { |
| 142 | - $display .= '<div class="cell grid-x '.$class.'">'.(in_array($column, $this->columnsHeaderNotToDisplay) ? '' : acym_translation('ACYM_'.strtoupper($column))).'</div>'; | |
| 171 | + echo '<div class="cell grid-x '.esc_attr($class).'">'.esc_html( | |
| 172 | + in_array($column, $this->columnsHeaderNotToDisplay) ? '' : acym_translation('ACYM_'.strtoupper($column)) | |
| 173 | + ).'</div>'; | |
| 143 | 174 | } |
| 144 | 175 | |
| 145 | - if ($type === 'available') $display .= '<div class="cell small-1"></div>'; | |
| 146 | - $display .= '</div>'; | |
| 176 | + //if it's the available listing we display the + at the end | |
| 177 | + if ($type === 'available') { | |
| 178 | + echo '<div class="cell small-1"></div>'; | |
| 179 | + } | |
| 180 | + //end of listing header | |
| 181 | + echo '</div>'; | |
| 147 | 182 | |
| 148 | - $functionClick = $type === 'available' ? 'v-on:click="selectEntity(entity.id)"' : 'v-on:click="unselectEntity(entity.id)"'; | |
| 149 | - $display .= '<div '.$functionClick.' v-for="(entity, index) in entitiesToDisplay_'.$type.'" class="cell acym_vcenter acym__listing__row grid-x acym__entity_select__'.$type.'__listing__row" >'; | |
| 183 | + //trigger the good function depending if were in available or selected listing | |
| 184 | + $functionClick = $type === 'available' ? 'selectEntity(entity.id)' : 'unselectEntity(entity.id)'; | |
| 185 | + //each row of the listing | |
| 186 | + echo '<div v-on:click="'.esc_attr($functionClick).'" v-for="(entity, index) in entitiesToDisplay_'.esc_attr( | |
| 187 | + $type | |
| 188 | + ).'" class="cell acym_vcenter acym__listing__row grid-x acym__entity_select__'.esc_attr($type).'__listing__row" >'; | |
| 150 | 189 | |
| 190 | + //if it's the select listing we display the - at the start | |
| 151 | 191 | if ($type !== 'available') { |
| 152 | - $display .= '<div class="cell small-1 vertical-align-middle text-center"> | |
| 153 | - <div class="plus-container acym__entity_select__selected__listing__row__unselect"> | |
| 154 | - <div class="top-plus plus-bar"></div> | |
| 155 | - <div class="plus plus-bar"></div> | |
| 156 | - <div class="bottom-plus plus-bar"></div> | |
| 157 | - </div> | |
| 158 | - </div>'; | |
| 192 | + echo '<div class="cell small-1 vertical-align-middle text-center"> | |
| 193 | + <div class="plus-container acym__entity_select__selected__listing__row__unselect"> | |
| 194 | + <div class="top-plus plus-bar"></div> | |
| 195 | + <div class="plus plus-bar"></div> | |
| 196 | + <div class="bottom-plus plus-bar"></div> | |
| 197 | + </div> | |
| 198 | + </div>'; | |
| 159 | 199 | } |
| 160 | 200 | |
| 161 | - $display .= '<div v-for="(column, index) in columnsToDisplay" class="cell align-center acym__entity_select__columns" :class="getClass(column)" v-html="entity[column]"></div>'; | |
| 201 | + //Display all the value for the columns | |
| 202 | + echo '<div v-for="(column, index) in columnsToDisplay" class="cell align-center acym__entity_select__columns" :class="getClass(column)" v-html="entity[column]"></div>'; | |
| 162 | 203 | |
| 204 | + //if it's the available listing we display the + at the end | |
| 163 | 205 | if ($type === 'available') { |
| 164 | - $display .= '<div class="cell small-1 vertical-align-middle text-center"> | |
| 206 | + echo '<div class="cell small-1 vertical-align-middle text-center"> | |
| 165 | 207 | <div class="plus-container acym__entity_select__available__listing__row__select"> |
| 166 | 208 | <div class="top-plus plus-bar"></div> |
| 167 | 209 | <div class="plus plus-bar"></div> |
| 168 | 210 | <div class="bottom-plus plus-bar"></div> |
| @@ -169,15 +211,16 @@ | ||
| 169 | 211 | </div> |
| 170 | 212 | </div>'; |
| 171 | 213 | } |
| 172 | 214 | |
| 173 | - $display .= '</div> | |
| 215 | + //End of listing with message loading and loading logo | |
| 216 | + echo '</div> | |
| 174 | 217 | <div class="cell grid-x align-center acym__entity_select__loading margin-top-1" v-show="loading"><div class="cell text-center acym__entity_select__title">'; |
| 175 | - $display .= acym_translation('ACYM_WE_ARE_LOADING_YOUR_DATA'); | |
| 176 | - $display .= '</div><div class="cell grid-x shrink margin-top-1">'.$this->svg.'</div></div>'; | |
| 177 | - $display .= '</div>'; | |
| 178 | - $display .= '</div> | |
| 179 | - </div>'; | |
| 180 | - | |
| 181 | - return $display; | |
| 218 | + echo esc_html(acym_translation('ACYM_WE_ARE_LOADING_YOUR_DATA')); | |
| 219 | + echo '</div><div class="cell grid-x shrink margin-top-1">'; | |
| 220 | + acym_loaderLogo(false); | |
| 221 | + echo '</div></div>'; | |
| 222 | + echo '</div>'; | |
| 223 | + echo '</div> | |
| 224 | + </div>'; | |
| 182 | 225 | } |
| 183 | 226 | } |