/ src / Components.php
Components.php
  1  <?php
  2  
  3  namespace ButA2SaeS3;
  4  
  5  use ButA2SaeS3\dto\AdherentDto;
  6  use ButA2SaeS3\entities\Adherent;
  7  
  8  class Components
  9  {
 10      public static function BackToLink($label = "Retour à l'accueil", $url = "/admin")
 11      {
 12          $button = '<a href="' . $url . '" class="flex gap-4 items-center text-blue-600 hover:text-blue-800 ">
 13              <svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 16 16"
 14                  class="w-10 border-2 rounded-full p-1">
 15                  <path fill-rule="evenodd"
 16                      d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8" />
 17              </svg>
 18              <p class="text-xl -ml-1 -mt-1 underline">' . $label . '</p>
 19          </a>';
 20          return $button;
 21      }
 22  
 23  
 24      public static function Heading2($content, $id = "")
 25      {
 26          $classes = "text-3xl text-shadow-2xs mb-4";
 27          $id_str = empty($id) ? "" : 'id="' . $id . '"';
 28          return '<h1 ' . $id_str . '  class="' . $classes . '">' . $content . '</h1>';
 29      }
 30  
 31      public static function Button($text, $variant = 'fage', $type = 'button', $class = '', $attributes = [])
 32      {
 33          $variants = [
 34              'fage' => ['bg' => 'bg-fage-700', 'hover' => 'hover:bg-fage-800'],
 35              'green' => ['bg' => 'bg-green-600', 'hover' => 'hover:bg-green-700'],
 36              'red' => ['bg' => 'bg-red-600', 'hover' => 'hover:bg-red-700'],
 37              'yellow' => ['bg' => 'bg-yellow-500', 'hover' => 'hover:bg-yellow-600'],
 38              'gray' => ['bg' => 'bg-gray-500', 'hover' => 'hover:bg-gray-600'],
 39          ];
 40  
 41          $color = $variants[$variant] ?? $variants['fage'];
 42  
 43          $attr_string = '';
 44          foreach ($attributes as $attr => $value) {
 45              $attr_string .= ' ' . $attr . '="' . htmlspecialchars($value) . '"';
 46          }
 47  
 48          $classes = 'rounded-full py-2 px-6 text-white shadow-sm ' . $color['bg'] . ' ' . $color['hover'] . ' ' . $class;
 49  
 50          if ($type === 'link') {
 51              return '<a href="' . ($attributes['href'] ?? '#') . '" class="' . $classes . '"' . $attr_string . '>' . $text . '</a>';
 52          }
 53  
 54          return '<button type="' . $type . '" class="' . $classes . '"' . $attr_string . '>' . $text . '</button>';
 55      }
 56  
 57      public static function OutlineButton($text, $variant = 'fage', $type = 'button', $class = '', $attributes = [])
 58      {
 59          $variants = [
 60              'fage' => ["text" => "text-fage-800", 'outline' => 'border-fage-700', 'hover' => 'hover:border-fage-800'],
 61              'green' => ["text" => "text-green-700", 'outline' => 'border-green-600', 'hover' => 'hover:border-green-700'],
 62              'red' => ["text" => "text-red-700", 'outline' => 'border-red-600', 'hover' => 'hover:border-red-700'],
 63              'yellow' => ["text" => "text-yellow-600", 'outline' => 'border-yellow-500', 'hover' => 'hover:border-yellow-600'],
 64              'gray' => ["text" => "text-gray-600", 'outline' => 'border-gray-500', 'hover' => 'hover:border-gray-600'],
 65          ];
 66  
 67          $color = $variants[$variant] ?? $variants['fage'];
 68  
 69          $attr_string = '';
 70          foreach ($attributes as $attr => $value) {
 71              $attr_string .= ' ' . $attr . '="' . htmlspecialchars($value) . '"';
 72          }
 73  
 74          $classes = 'rounded-full py-2 px-6 bg-white border-2 text-shadow-2xs ' . $color['text'] . ' ' . $color['outline'] . ' ' . $color['hover'] . ' ' . $class;
 75  
 76          if ($type === 'link') {
 77              return '<a href="' . ($attributes['href'] ?? '#') . '" class="' . $classes . '"' . $attr_string . '>' . $text . '</a>';
 78          }
 79  
 80          return '<button type="' . $type . '" class="' . $classes . '"' . $attr_string . '>' . $text . '</button>';
 81      }
 82  
 83      public static function SmallButton($text, $variant = 'fage', $type = 'button', $class = '', $attributes = [])
 84      {
 85          $variants = [
 86              'fage' => ['bg' => 'bg-fage-600', 'hover' => 'hover:bg-fage-700'],
 87              'green' => ['bg' => 'bg-green-500', 'hover' => 'hover:bg-green-700'],
 88              'red' => ['bg' => 'bg-red-500', 'hover' => 'hover:bg-red-700'],
 89              'blue' => ['bg' => 'bg-blue-500', 'hover' => 'hover:bg-blue-700'],
 90              'yellow' => ['bg' => 'bg-yellow-500', 'hover' => 'hover:bg-yellow-600'],
 91              'gray' => ['bg' => 'bg-gray-500', 'hover' => 'hover:bg-gray-700']
 92          ];
 93  
 94          $color = $variants[$variant] ?? $variants['fage'];
 95  
 96          $attr_string = '';
 97          foreach ($attributes as $attr => $value) {
 98              $attr_string .= ' ' . $attr . '="' . htmlspecialchars($value) . '"';
 99          }
100  
101          $classes = 'rounded px-2 py-1 text-sm text-white ' . $color['bg'] . ' ' . $color['hover'] . ' ' . $class;
102  
103          if ($type === 'link') {
104              return '<a href="' . ($attributes['href'] ?? '#') . '" class="' . $classes . '"' . $attr_string . '>' . $text . '</a>';
105          }
106  
107          return '<button type="' . $type . '" class="' . $classes . '"' . $attr_string . '>' . $text . '</button>';
108      }
109  
110      public static function IconButton($text, $variant = 'fage', $type = 'button', $class = '', $attributes = [])
111      {
112          $variants = [
113              'fage' => ['bg' => 'bg-fage-600', 'hover' => 'hover:bg-fage-700'],
114              'green' => ['bg' => 'bg-green-600', 'hover' => 'hover:bg-green-700'],
115              'red' => ['bg' => 'bg-red-600', 'hover' => 'hover:bg-red-700'],
116              'blue' => ['bg' => 'bg-blue-600', 'hover' => 'hover:bg-blue-700'],
117              'yellow' => ['bg' => 'bg-yellow-500', 'hover' => 'hover:bg-yellow-600'],
118              'gray' => ['bg' => 'bg-gray-500', 'hover' => 'hover:bg-gray-600']
119          ];
120  
121          $color = $variants[$variant] ?? $variants['fage'];
122  
123          $attr_string = '';
124          foreach ($attributes as $attr => $value) {
125              $attr_string .= ' ' . $attr . '="' . htmlspecialchars($value) . '"';
126          }
127  
128          $classes = 'rounded px-4 py-2 text-white transition-colors duration-200 ' . $color['bg'] . ' ' . $color['hover'] . ' ' . $class;
129  
130          if ($type === 'link') {
131              return '<a href="' . ($attributes['href'] ?? '#') . '" class="' . $classes . '"' . $attr_string . '>' . $text . '</a>';
132          }
133  
134          return '<button type="' . $type . '" class="' . $classes . '"' . $attr_string . '>' . $text . '</button>';
135      }
136  
137      public static function FormInput($name, $label, $type = 'text', $value = '', $required = false, $class = '', $attributes = [])
138      {
139          $error = $attributes['error'] ?? null;
140          $containerClass = $attributes['container-class'] ?? '';
141          unset($attributes['container-class'], $attributes['error']);
142  
143          $requiredAttr = $required ? 'required' : '';
144          $attrString = '';
145          foreach ($attributes as $attr => $attr_value) {
146              if ($attr !== 'value') {
147                  $attrString .= ' ' . $attr . '="' . htmlspecialchars($attr_value) . '"';
148              }
149          }
150  
151          $inputClasses = 'border-2 shadow-sm rounded-full px-4 py-1 bg-[#fafafa] focus:outline-none focus:ring-2 focus:ring-fage-300 ' . $class;
152          if ($error) {
153              $inputClasses .= ' border-red-500 focus:ring-red-300';
154          }
155  
156          $html = '<div class="flex flex-col ' . htmlspecialchars($containerClass) . '">';
157          $html .= '<label for="' . htmlspecialchars($name) . '" class="text-lg">' . htmlspecialchars($label);
158          if ($required) {
159              $html .= ' <span class="text-red-500">*</span>';
160          }
161          $html .= '</label>';
162          $html .= '<input type="' . htmlspecialchars($type) . '" name="' . htmlspecialchars($name) . '" class="' . $inputClasses . '" value="' . htmlspecialchars($value) . '" ' . $requiredAttr . $attrString . '>';
163          if ($error) {
164              $html .= '<span class="error text-red-500 text-sm mt-1">' . htmlspecialchars($error) . '</span>';
165          }
166          $html .= '</div>';
167  
168          return $html;
169      }
170  
171      public static function FormSelect($name, $label = "", $options = [], $selected = '', $class = '', $attributes = [])
172      {
173          $attr_string = '';
174          $required_attr = '';
175          foreach ($attributes as $attr => $value) {
176              if ($attr !== 'required') {
177                  $attr_string .= ' ' . $attr . '="' . htmlspecialchars($value ?? "") . '"';
178              } elseif ($value) {
179                  $required_attr = 'required';
180              }
181          }
182  
183          $classes = 'border-2 rounded-full pl-2 py-1 bg-[#fafafa] focus:outline-none focus:ring-2 focus:ring-fage-300 appearance-none pr-8 ' . $class;
184  
185          $options_html = '';
186          foreach ($options as $value => $option_text) {
187              $selected_attr = ($value == $selected) ? 'selected' : '';
188              $options_html .= '<option value="' . htmlspecialchars($value) . '" ' . $selected_attr . '>' . htmlspecialchars($option_text) . '</option>';
189          }
190  
191          return '<div class="flex flex-col justify-end ' . ($attributes['container-class'] ?? '') . '">
192                      <label for="' . $name . '" class="text-lg">' . $label . '</label>
193                      <select id="' . $name . '" name="' . $name . '" class="' . $classes . '"' . $attr_string . ' ' . $required_attr . '>
194                          ' . $options_html . '
195                      </select>
196                  </div>';
197      }
198  
199      public static function Message($text, $type = 'success')
200      {
201          $colors = [
202              'success' => 'text-green-500',
203              'error' => 'text-red-500',
204              'warning' => 'text-yellow-600',
205              'info' => 'text-blue-500'
206          ];
207  
208          $color = $colors[$type] ?? $colors['success'];
209  
210          return '<span class="' . $color . ' text-center">' . $text . '</span>';
211      }
212  
213      public static function Badge(string $text, string $variant = 'fage', string $class = ''): string
214      {
215          $variants = [
216              'fage' => 'bg-fage-800 text-white',
217              'success' => 'bg-green-700 text-white',
218              'warning' => 'bg-yellow-600 text-white',
219              'danger' => 'bg-red-700 text-white',
220              'muted' => 'bg-gray-700 text-white',
221              'info' => 'bg-blue-700 text-white'
222          ];
223  
224          $colors = $variants[$variant] ?? $variants['fage'];
225          $classes = trim("inline-flex items-center px-2 py-1 rounded-xl shadow-sm text-xs {$colors} {$class}");
226  
227          return '<span class="' . htmlspecialchars($classes) . '">' . htmlspecialchars($text) . '</span>';
228      }
229  
230      public static function Link($text, $url, $variant = 'default', $class = '')
231      {
232          $variants = [
233              'default' => 'text-blue-600 hover:text-blue-800 underline',
234              'danger' => 'text-red-600 hover:text-red-800 underline',
235              'muted' => 'text-gray-600 hover:text-gray-800 underline'
236          ];
237  
238          $color = $variants[$variant] ?? $variants['default'];
239          $classes = $color . ' ' . $class;
240  
241          return '<a href="' . $url . '" class="' . $classes . '">' . $text . '</a>';
242      }
243  
244      public static function AdherantTableRow(AdherentDto|Adherent $adherant, bool $alternate)
245      {
246          $bg_colors = $alternate ? "bg-gray-200 " : "bg-gray-50";
247          $editUrl = '/edit_adherent?id=' . urlencode((string)$adherant->id) . '#adherents-table';
248          $deleteForm = self::ActionButton('Supprimer', 'delete_id', $adherant->id, 'danger', true);
249  
250          $tr = '<tr class="hover:bg-gray-300 ' . $bg_colors . '">
251                      <td class="border-2 px-4 py-2">' . htmlspecialchars($adherant->nom) . '</td>
252                      <td class="border-2 px-4 py-2">' . htmlspecialchars($adherant->prenom) . '</td>
253                      <td class="border-2 px-4 py-2">' . htmlspecialchars($adherant->adresse) . '</td>
254                      <td class="border-2 px-4 py-2">' . htmlspecialchars($adherant->profession) . '</td>
255                      <td class="border-2 px-4 py-2">' . htmlspecialchars($adherant->age) . '</td>
256                      <td class="border-2 px-4 py-2">' . htmlspecialchars($adherant->ville) . '</td>
257                      <td class="border-2 px-4 py-2">
258                          <a href="' . htmlspecialchars($editUrl) . '" class="text-blue-600 underline">Modifier</a>
259                          ' . $deleteForm . '
260                      </td>
261                  </tr>';
262          return $tr;
263      }
264  
265      public static function ActionButton(string $text, string $action, int $id, string $style = 'primary', bool $confirm = false): string
266      {
267          $confirmAttr = $confirm ? 'onclick="return confirm(\'Êtes-vous sûr ?\')"' : '';
268          $colorClasses = [
269              'primary' => 'text-blue-600 hover:text-blue-800',
270              'danger' => 'text-red-600 hover:text-red-800',
271              'secondary' => 'text-gray-600 hover:text-gray-800'
272          ];
273  
274          $class = $colorClasses[$style] ?? $colorClasses['primary'];
275          $buttonClasses = 'bg-transparent border-0 underline cursor-pointer p-0 font-inherit ' . $class;
276  
277          $location = strtok($_SERVER['REQUEST_URI'], '?');
278  
279          return sprintf(
280              '
281          <form method="POST" action="%s" style="display: inline;">
282              <input type="hidden" name="%s" value="%d">
283              <button type="submit" class="%s" %s>%s</button>
284          </form>',
285              htmlspecialchars($location),
286              htmlspecialchars($action),
287              $id,
288              $buttonClasses,
289              $confirmAttr,
290              htmlspecialchars($text)
291          );
292      }
293  
294  
295      public static function FormDateTime($name, $label, $type = 'date', $value = '', $required = false, $class = '', $attributes = [])
296      {
297          $required_attr = $required ? 'required' : '';
298          $attr_string = '';
299          foreach ($attributes as $attr => $attr_value) {
300              if ($attr !== 'value') {
301                  $attr_string .= ' ' . $attr . '="' . htmlspecialchars($attr_value) . '"';
302              }
303          }
304  
305          $classes = 'border-2 rounded-full px-4  py-1 bg-[#fafafa] focus:outline-none focus:ring-2 focus:ring-fage-300 w-full ' . $class;
306  
307          return '<div class="flex flex-col ' . ($attributes['container-class'] ?? '') . '">
308                      <label for="' . $name . '" class="text-lg">' . $label . '</label>
309                      <input type="' . $type . '" name="' . $name . '" id="' . $name . '" class="' . $classes . '" value="' . htmlspecialchars($value) . '" ' . $required_attr . $attr_string . '>
310                  </div>';
311      }
312  
313      public static function Textarea($name, $label = "", $value = "", $required = false, $class = "", $attributes = [])
314      {
315          $error = $attributes['error'] ?? null;
316          unset($attributes['error']);
317  
318          $required_attr = $required ? 'required' : '';
319          $attr_string = '';
320          foreach ($attributes as $attr => $attr_value) {
321              if ($attr !== 'value') {
322                  $attr_string .= ' ' . $attr . '="' . htmlspecialchars($attr_value ?? "") . '"';
323              }
324          }
325  
326          $classes = 'border-2 shadow-sm rounded-2xl pl-2 py-1 bg-[#fafafa] focus:outline-none focus:ring-2 focus:ring-fage-300 w-full ' . $class;
327          if ($error) {
328              $classes .= ' border-red-500 focus:ring-red-300';
329          }
330  
331          $label_html = !empty($label) ? '<label for="' . $name . '" class="text-lg">' . $label . '</label>' : '';
332  
333          $html = '<div class="flex flex-col ' . ($attributes['container-class'] ?? '') . '">
334                      ' . $label_html . '
335                      <textarea name="' . $name . '" id="' . $name . '" class="' . $classes . '" ' . $required_attr . $attr_string . '>' . htmlspecialchars($value) . '</textarea>
336                  ';
337  
338          if ($error) {
339              $html .= '<span class="error text-red-500 text-sm mt-1">' . htmlspecialchars($error) . '</span>';
340          }
341  
342          $html .= '</div>';
343          return $html;
344      }
345  }