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