1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the jquery-datatables-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\JQuery\DataTablesBundle\Twig\Extension; |
13
|
|
|
|
14
|
|
|
use Twig_SimpleFunction; |
15
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesWrapper; |
16
|
|
|
use WBW\Library\Core\Utility\Argument\ArrayUtility; |
17
|
|
|
use WBW\Library\Core\Utility\Argument\StringUtility; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* DataTables Twig extension. |
21
|
|
|
* |
22
|
|
|
* @author webeweb <https://github.com/webeweb/> |
23
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Twig\Extension |
24
|
|
|
*/ |
25
|
|
|
class DataTablesTwigExtension extends AbstractDataTablesTwigExtension { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Service name. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
const SERVICE_NAME = "webeweb.bundle.datatablesbundle.twig.extension.datatables"; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Directory. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
const JS_TEMPLATE = "<script type=\"text/javascript\"> |
40
|
|
|
\t$('__selector__').DataTable({ |
41
|
|
|
\t\tajax: { |
42
|
|
|
\t\t\ttype: '__method__', |
43
|
|
|
\t\t\turl: '__url__' |
44
|
|
|
\t\t}, |
45
|
|
|
\t\tcolumns: __columns__, |
46
|
|
|
\t\torder: __order__, |
47
|
|
|
\t\tprocessing: __processing__, |
48
|
|
|
\t\tserverSide: __serverSide__ |
49
|
|
|
\t}); |
50
|
|
|
</script>"; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor. |
54
|
|
|
*/ |
55
|
|
|
public function __construct() { |
56
|
|
|
parent::__construct(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Displays a DataTables HTML. |
61
|
|
|
* |
62
|
|
|
* @param DataTablesWrapper $dtWrapper The wrapper. |
63
|
|
|
* @param array $args The arguments. |
64
|
|
|
* @return string Returns the DataTables HTML. |
65
|
|
|
*/ |
66
|
|
|
public function dataTablesHTMLFunction(DataTablesWrapper $dtWrapper, array $args = []) { |
67
|
|
|
return $this->dataTablesTable($dtWrapper, ArrayUtility::get($args, "class"), ArrayUtility::get($args, "thead", true), ArrayUtility::get($args, "tfoot", true)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Diplays a DataTables JS. |
72
|
|
|
* |
73
|
|
|
* @param DataTablesWrapper $dtWrapper The wrapper. |
74
|
|
|
* @param array $args The arguments. |
75
|
|
|
* @return string Returns the DataTables JS. |
76
|
|
|
*/ |
77
|
|
|
public function dataTablesJSFunction(DataTablesWrapper $dtWrapper, array $args = []) { |
78
|
|
|
|
79
|
|
|
// Initialize the parameters. |
80
|
|
|
$selector = ArrayUtility::get($args, "selector", ".table"); |
81
|
|
|
$method = $dtWrapper->getMethod(); |
82
|
|
|
$url = $dtWrapper->getUrl(); |
83
|
|
|
$columns = json_encode(array_values($dtWrapper->getColumns())); |
84
|
|
|
$orders = json_encode(array_values($dtWrapper->getOrder())); |
85
|
|
|
$processing = StringUtility::parseBoolean($dtWrapper->getProcessing()); |
86
|
|
|
$serverSide = StringUtility::parseBoolean($dtWrapper->getServerSide()); |
87
|
|
|
|
88
|
|
|
// Return the Javascript. |
89
|
|
|
return StringUtility::replace(self::JS_TEMPLATE, ["__selector__", "__method__", "__url__", "__columns__", "__order__", "__processing__", "__serverSide__"], [$selector, $method, $url, $columns, $orders, $processing, $serverSide]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the Twig functions. |
94
|
|
|
* |
95
|
|
|
* @return array Returns the Twig functions. |
96
|
|
|
*/ |
97
|
|
|
public function getFunctions() { |
98
|
|
|
return [ |
99
|
|
|
new Twig_SimpleFunction("dataTablesHTML", [$this, "dataTablesHTMLFunction"], ["is_safe" => ["html"]]), |
100
|
|
|
new Twig_SimpleFunction("dataTablesJS", [$this, "dataTablesJSFunction"], ["is_safe" => ["html"]]), |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|