| 1: | <?php |
| 2: | namespace TIC\TwigBundle\Extension; |
| 3: | |
| 4: | use TIC\TwigBundle\Base\TICTwigExtension as BaseExtension; |
| 5: | use Symfony\Contracts\Translation\TranslatorInterface; |
| 6: | use Symfony\Component\HttpFoundation\UrlHelper; |
| 7: | use Twig\TwigFunction; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | class DatatablesExtension extends BaseExtension |
| 14: | { |
| 15: | |
| 16: | public function getFunctions(): array |
| 17: | { |
| 18: | return [ |
| 19: | new TwigFunction('dtFilters', [$this, 'dtFiltersFunction'], ['is_safe' => ['html']] ), |
| 20: | new TwigFunction('dtGenerate', [$this, 'dtGenerateFunction'], ['is_safe' => ['html']] ), |
| 21: | ]; |
| 22: | } |
| 23: | |
| 24: | public function __construct(UrlHelper $urlHelper, TranslatorInterface $translator = null) |
| 25: | { |
| 26: | $this->urlHelper = $urlHelper; |
| 27: | $this->translator = $translator; |
| 28: | } |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | public function dtFiltersFunction(string $id = null, bool $noreset = false): string |
| 44: | { |
| 45: | if (empty($id)) $id = "itemlist"; |
| 46: | |
| 47: | $buttons = ''; |
| 48: | if (empty($noreset)) { |
| 49: | |
| 50: | $html = '<button id="%s-filters-reset" class="btn btn-secondary btn-sm dt-filters-reset" title="%s" type="button">%s</button>'; |
| 51: | $buttons.= \sprintf($html, $id, $this->trans('btn.unfilter', true), $this->getIcon('bi-eraser-fill')); |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | } |
| 60: | |
| 61: | $html = '<div id="%s-filters" class="form-group d-none dt-filters"><label>%s</label> :%s</div>'; |
| 62: | return \sprintf($html, $id, \htmlspecialchars($this->trans('widget.datatable.filters')), $buttons); |
| 63: | } |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | public function dtGenerateFunction(array $options = []): string |
| 91: | { |
| 92: | |
| 93: | $options+= array( |
| 94: | 'id' => "itemlist", |
| 95: | 'orders' => "1", |
| 96: | 'persist' => false, |
| 97: | 'filters' => false, |
| 98: | 'search' => false, |
| 99: | 'paging' => false, |
| 100: | 'info' => false, |
| 101: | 'buttons' => false, |
| 102: | 'responsive' => false, |
| 103: | 'select' => false, |
| 104: | 'ajax' => false, |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | 'options' => [], |
| 110: | ); |
| 111: | |
| 112: | |
| 113: | if (\is_string($options['buttons'])) $options['buttons'] = \explode(",", \str_replace(' ', '', $options['buttons'])); |
| 114: | if (empty($options['buttons'])) $options['buttons'] = []; |
| 115: | |
| 116: | |
| 117: | $jsopts = array( |
| 118: | 'order' => $this->makeOrders( $options['orders'] ), |
| 119: | 'applyFilter' => (bool)$options['filters'], |
| 120: | 'stateSave' => (bool)$options['persist'], |
| 121: | 'searching' => (bool)$options['filters'] || (bool)$options['search'], |
| 122: | 'paging' => (bool)$options['paging'], |
| 123: | 'info' => (bool)$options['info'], |
| 124: | 'buttons' => $options['buttons'], |
| 125: | 'select' => empty($options['select']) ? ["info" => false] : $options['select'], |
| 126: | 'responsive' => (bool)$options['responsive'], |
| 127: | 'dom' => $this->makeLayout($options['search'], $options['paging'], ! empty($options['buttons'])), |
| 128: | 'language' => [ "url" => $this->findLanguageURL() ], |
| 129: | ); |
| 130: | |
| 131: | if ($options['ajax']) $jsopts+= array( |
| 132: | 'ajax' => [ "type" => "POST", "url" => $options['ajax'] ], |
| 133: | 'serverSide' => true, |
| 134: | 'processing' => true, |
| 135: | ); |
| 136: | foreach ($options['options'] as $key => $val) $jsopts[ $key ] = $val; |
| 137: | |
| 138: | |
| 139: | return \sprintf("var dt_%s = $('#%s').DataTable(%s);", $options['id'], $options['id'], \json_encode($jsopts)); |
| 140: | } |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | protected function makeOrders(mixed $orders = 0): array |
| 155: | { |
| 156: | $option = []; |
| 157: | if (! \is_array($orders)) $orders = \explode(',', $orders); |
| 158: | foreach ($orders as $order) { |
| 159: | if (! ($order = \intval($order))) continue; |
| 160: | $option[] = [ \abs($order) - 1, ($order < 0) ? "desc" : "asc" ]; |
| 161: | } |
| 162: | return $option; |
| 163: | } |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | protected function makeLayout(bool $search = false, bool $paging = false, bool $buttons = false): string |
| 179: | { |
| 180: | $top = "'row tic-dt-top d-print-none'"; |
| 181: | $lst = "'row tic-dt-lst'<'col-sm-12'tr>"; |
| 182: | $end = "'row tic-dt-end'"; |
| 183: | |
| 184: | if ($paging) { |
| 185: | if ($search) { |
| 186: | if ($buttons) $top.= "<'col-md-6 col-lg-4'l>" . "<'col-md-6 col-lg-4 order-lg-last'f>" . "<'col-lg-4 text-center'B>"; |
| 187: | else $top.= "<'col-md-6'l>" . "<'col-md-6'f>"; |
| 188: | } else { |
| 189: | if ($buttons) $top.= "<'col-md-6'l>" . "<'col-md-6'B>"; |
| 190: | else $top.= "<'col-sm-12'l>"; |
| 191: | } |
| 192: | } else { |
| 193: | if ($search) { |
| 194: | if ($buttons) $top.= "<'col-md-6'B>" . "<'col-md-6'f>"; |
| 195: | else $top.= "<'col-sm-12'f>"; |
| 196: | } else { |
| 197: | if ($buttons) $top.= "<'col-sm-12'B>"; |
| 198: | else $top.= ""; |
| 199: | } |
| 200: | } |
| 201: | |
| 202: | $end.= empty($paging) ? "<'col-sm-12'i>" : "<'col-sm-5'i><'col-sm-7'p>"; |
| 203: | |
| 204: | return \sprintf('<%s><%s><%s>', $top, $lst, $end); |
| 205: | } |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | protected function findLanguageURL(?string $locale = null): string |
| 214: | { |
| 215: | $url = $this->urlHelper->getAbsoluteUrl("/bundles/tictwig/datatables/i18n/"); |
| 216: | |
| 217: | if ($locale === null) $locale = \Locale::getDefault(); |
| 218: | $locale = \substr(\strtolower($locale), 0, 2); |
| 219: | |
| 220: | switch ($locale) { |
| 221: | case 'fr' : $file = "fr_fr.json"; break; |
| 222: | case 'de' : $file = "de_de.json"; break; |
| 223: | case 'it' : $file = "it_it.json"; break; |
| 224: | case 'es' : $file = "es_es.json"; break; |
| 225: | case 'en' : $file = "en-en.json"; break; |
| 226: | default : $file = "en-en.json"; |
| 227: | } |
| 228: | return $url . $file; |
| 229: | } |
| 230: | |
| 231: | } |
| 232: | |