11<?php
2+
23namespace Mouf \Html \Widgets \EvoluGrid \Controllers ;
34
45use Mouf \Html \Template \TemplateInterface ;
5-
66use SQLParser \Query \StatementFactory ;
7-
8- use SQLParser \SQLParser ;
9-
7+ use PHPSQLParser \PHPSQLParser ;
108use Mouf \MoufManager ;
11-
129use Mouf \Mvc \Splash \Controllers \Controller ;
13-
1410use Mouf \Html \HtmlElement \HtmlBlock ;
1511use Mouf \Database \QueryWriter \Utils \FindParametersService ;
1612
2117 * @author David Négrier <d.negrier@thecodingmachine.com>
2218 * @Logged
2319 */
24- class EvolugridAdminController extends Controller {
25-
26- /**
27- * @var TemplateInterface
28- */
29- public $ template ;
30-
31- /**
32- * @var HtmlBlock
33- */
34- public $ content ;
35-
36- protected $ instanceName ;
37- protected $ sql ;
38- protected $ url ;
39- protected $ editMode = false ;
40- protected $ errorMessage ;
41-
42- /**
43- * Admin page used to display the DAO generation form.
44- *
45- * @Action
46- */
47- public function newEvolugrid ($ selfedit ="false " ) {
48- $ this ->content ->addFile (dirname (__FILE__ )."/../../../../../views/createEvolugrid.php " , $ this );
49- $ this ->template ->toHtml ();
50- }
51-
52- /**
53- * This action generates the objects from the SQL query and creates a new SELECT instance.
54- *
55- * @Action
56- * @param string $name
57- * @param string $sql
58- * @param string $selfedit
59- */
60- public function doCreateEvolugrid ($ name , $ sql , $ url , $ buildSearchForm = 0 , $ selfedit ="false " ) {
61- if (empty ($ url )) {
62- $ url = "evolugrid/ " .urlencode ($ name );
63- }
64-
65- $ parser = new SQLParser ();
66- $ parsed = $ parser ->parse ($ sql );
67- $ select = StatementFactory::toObject ($ parsed );
68-
69- $ moufManager = MoufManager::getMoufManagerHiddenInstance ();
70- $ dbConnectionInstanceDescriptor = $ moufManager ->getInstanceDescriptor ("dbalConnection " );
71-
72- $ selectInstanceDescriptor = $ select ->toInstanceDescriptor ($ moufManager );
73- $ parameters = FindParametersService::findParameters ($ selectInstanceDescriptor );
74-
75- $ queryResultInstanceDescriptor = $ moufManager ->createInstance ("Mouf \\Database \\QueryWriter \\QueryResult " );
76- $ queryResultInstanceDescriptor ->getProperty ("select " )->setValue ($ selectInstanceDescriptor );
77- $ queryResultInstanceDescriptor ->getProperty ("connection " )->setValue ($ dbConnectionInstanceDescriptor );
78-
79- $ countNbResultsInstanceDescriptor = $ moufManager ->createInstance ("Mouf \\Database \\QueryWriter \\CountNbResult " );
80- $ countNbResultsInstanceDescriptor ->getProperty ("queryResult " )->setValue ($ queryResultInstanceDescriptor );
81- $ countNbResultsInstanceDescriptor ->getProperty ("connection " )->setValue ($ dbConnectionInstanceDescriptor );
82-
83- $ evolugridResultSetInstanceDescriptor = $ moufManager ->createInstance ("Mouf \\Html \\Widgets \\EvoluGrid \\EvoluGridResultSet " );
84- $ evolugridResultSetInstanceDescriptor ->getProperty ("results " )->setValue ($ queryResultInstanceDescriptor );
85- $ evolugridResultSetInstanceDescriptor ->getProperty ("totalRowsCount " )->setValue ($ countNbResultsInstanceDescriptor );
86- $ evolugridResultSetInstanceDescriptor ->getProperty ("url " )->setValue ($ url );
87-
88- if ($ moufManager ->instanceExists ($ name )) {
89- $ evolugridInstanceDescriptor = $ moufManager ->getInstanceDescriptor ($ name );
90- } else {
91- $ evolugridInstanceDescriptor = $ moufManager ->createInstance ("Mouf \\Html \\Widgets \\EvoluGrid \\EvoluGrid " );
92- $ evolugridInstanceDescriptor ->setName ($ name );
93- $ evolugridInstanceDescriptor ->getProperty ("class " )->setValue ("table table-striped table-hover " );
94- }
95- $ evolugridInstanceDescriptor ->getProperty ("url " )->setValue ($ evolugridResultSetInstanceDescriptor );
96-
97- if ($ buildSearchForm && $ evolugridInstanceDescriptor ->getProperty ('searchForm ' )->getValue () == null ) {
98- $ searchFormInstanceDescriptor = $ moufManager ->createInstance ("Mouf \\Html \\HtmlElement \\HtmlFromFile " );
99- $ searchFormInstanceDescriptor ->getProperty ('fileName ' )->setValue ('vendor/mouf/html.widgets.evolugrid/src/views/searchForm.php ' );
100- $ searchFormInstanceDescriptor ->getProperty ('relativeToRootPath ' )->setValue (true );
101- $ evolugridInstanceDescriptor ->getProperty ('searchForm ' )->setValue ($ searchFormInstanceDescriptor );
102- }
103-
104- $ parameterInstanceDescriptors = array ();
105- foreach ($ parameters as $ parameter ) {
106- $ parameterInstanceDescriptor = $ moufManager ->createInstance ("Mouf \\Utils \\Value \\RequestParam " );
107- $ parameterInstanceDescriptor ->getProperty ("paramName " )->setValue ($ parameter );
108- $ parameterInstanceDescriptors [$ parameter ] = $ parameterInstanceDescriptor ;
109- }
110- $ queryResultInstanceDescriptor ->getProperty ("parameters " )->setValue ($ parameterInstanceDescriptors );
111-
112- $ moufManager ->rewriteMouf ();
113-
114- header ("Location: " .ROOT_URL ."ajaxinstance/?name= " .urlencode ($ name )."&selfedit= " .$ selfedit );
115- }
116-
117- /**
118- * Admin page used to display the edit form for the Evolugrid.
119- *
120- * @Action
121- */
122- public function updateFromSql ($ name , $ selfedit ="false " ) {
123- $ moufManager = MoufManager::getMoufManagerHiddenInstance ();
124- $ evolugridInstanceDescriptor = $ moufManager ->getInstanceDescriptor ($ name );
125- $ this ->instanceName = $ name ;
126- $ this ->editMode = true ;
127-
128- $ evolugridResultSetInstanceDescriptor = $ evolugridInstanceDescriptor ->getProperty ("url " )->getValue ();
129- if ($ evolugridResultSetInstanceDescriptor == null ) {
130- $ this ->newEvolugrid ($ selfedit );
131- return ;
132- }
133-
134- $ this ->errorMessage = "Sorry, Evolugrid does not support initializing from SQL query if the 'url' parameter
20+ class EvolugridAdminController extends Controller
21+ {
22+ /**
23+ * @var TemplateInterface
24+ */
25+ public $ template ;
26+
27+ /**
28+ * @var HtmlBlock
29+ */
30+ public $ content ;
31+
32+ protected $ instanceName ;
33+ protected $ sql ;
34+ protected $ url ;
35+ protected $ editMode = false ;
36+ protected $ errorMessage ;
37+
38+ /**
39+ * Admin page used to display the DAO generation form.
40+ *
41+ * @Action
42+ */
43+ public function newEvolugrid ($ selfedit = 'false ' )
44+ {
45+ $ this ->content ->addFile (dirname (__FILE__ ).'/../../../../../views/createEvolugrid.php ' , $ this );
46+ $ this ->template ->toHtml ();
47+ }
48+
49+ /**
50+ * This action generates the objects from the SQL query and creates a new SELECT instance.
51+ *
52+ * @Action
53+ *
54+ * @param string $name
55+ * @param string $sql
56+ * @param string $selfedit
57+ */
58+ public function doCreateEvolugrid ($ name , $ sql , $ url , $ buildSearchForm = 0 , $ selfedit = 'false ' )
59+ {
60+ if (empty ($ url )) {
61+ $ url = 'evolugrid/ ' .urlencode ($ name );
62+ }
63+
64+ $ parser = new PHPSQLParser ();
65+ $ parsed = $ parser ->parse ($ sql );
66+ $ select = StatementFactory::toObject ($ parsed );
67+
68+ $ moufManager = MoufManager::getMoufManagerHiddenInstance ();
69+ $ dbConnectionInstanceDescriptor = $ moufManager ->getInstanceDescriptor ('dbalConnection ' );
70+
71+ $ selectInstanceDescriptor = $ select ->toInstanceDescriptor ($ moufManager );
72+ $ parameters = FindParametersService::findParameters ($ selectInstanceDescriptor );
73+
74+ $ queryResultInstanceDescriptor = $ moufManager ->createInstance ('Mouf \\Database \\QueryWriter \\QueryResult ' );
75+ $ queryResultInstanceDescriptor ->getProperty ('select ' )->setValue ($ selectInstanceDescriptor );
76+ $ queryResultInstanceDescriptor ->getProperty ('connection ' )->setValue ($ dbConnectionInstanceDescriptor );
77+
78+ $ countNbResultsInstanceDescriptor = $ moufManager ->createInstance ('Mouf \\Database \\QueryWriter \\CountNbResult ' );
79+ $ countNbResultsInstanceDescriptor ->getProperty ('queryResult ' )->setValue ($ queryResultInstanceDescriptor );
80+ $ countNbResultsInstanceDescriptor ->getProperty ('connection ' )->setValue ($ dbConnectionInstanceDescriptor );
81+
82+ $ evolugridResultSetInstanceDescriptor = $ moufManager ->createInstance ('Mouf \\Html \\Widgets \\EvoluGrid \\EvoluGridResultSet ' );
83+ $ evolugridResultSetInstanceDescriptor ->getProperty ('results ' )->setValue ($ queryResultInstanceDescriptor );
84+ $ evolugridResultSetInstanceDescriptor ->getProperty ('totalRowsCount ' )->setValue ($ countNbResultsInstanceDescriptor );
85+ $ evolugridResultSetInstanceDescriptor ->getProperty ('url ' )->setValue ($ url );
86+
87+ if ($ moufManager ->instanceExists ($ name )) {
88+ $ evolugridInstanceDescriptor = $ moufManager ->getInstanceDescriptor ($ name );
89+ } else {
90+ $ evolugridInstanceDescriptor = $ moufManager ->createInstance ('Mouf \\Html \\Widgets \\EvoluGrid \\EvoluGrid ' );
91+ $ evolugridInstanceDescriptor ->setName ($ name );
92+ $ evolugridInstanceDescriptor ->getProperty ('class ' )->setValue ('table table-striped table-hover ' );
93+ }
94+ $ evolugridInstanceDescriptor ->getProperty ('url ' )->setValue ($ evolugridResultSetInstanceDescriptor );
95+
96+ if ($ buildSearchForm && $ evolugridInstanceDescriptor ->getProperty ('searchForm ' )->getValue () == null ) {
97+ $ searchFormInstanceDescriptor = $ moufManager ->createInstance ('Mouf \\Html \\HtmlElement \\HtmlFromFile ' );
98+ $ searchFormInstanceDescriptor ->getProperty ('fileName ' )->setValue ('vendor/mouf/html.widgets.evolugrid/src/views/searchForm.php ' );
99+ $ searchFormInstanceDescriptor ->getProperty ('relativeToRootPath ' )->setValue (true );
100+ $ evolugridInstanceDescriptor ->getProperty ('searchForm ' )->setValue ($ searchFormInstanceDescriptor );
101+ }
102+
103+ $ parameterInstanceDescriptors = array ();
104+ foreach ($ parameters as $ parameter ) {
105+ $ parameterInstanceDescriptor = $ moufManager ->createInstance ('Mouf \\Utils \\Value \\RequestParam ' );
106+ $ parameterInstanceDescriptor ->getProperty ('paramName ' )->setValue ($ parameter );
107+ $ parameterInstanceDescriptors [$ parameter ] = $ parameterInstanceDescriptor ;
108+ }
109+ $ queryResultInstanceDescriptor ->getProperty ('parameters ' )->setValue ($ parameterInstanceDescriptors );
110+
111+ $ moufManager ->rewriteMouf ();
112+
113+ header ('Location: ' .ROOT_URL .'ajaxinstance/?name= ' .urlencode ($ name ).'&selfedit= ' .$ selfedit );
114+ }
115+
116+ /**
117+ * Admin page used to display the edit form for the Evolugrid.
118+ *
119+ * @Action
120+ */
121+ public function updateFromSql ($ name , $ selfedit = 'false ' )
122+ {
123+ $ moufManager = MoufManager::getMoufManagerHiddenInstance ();
124+ $ evolugridInstanceDescriptor = $ moufManager ->getInstanceDescriptor ($ name );
125+ $ this ->instanceName = $ name ;
126+ $ this ->editMode = true ;
127+
128+ $ evolugridResultSetInstanceDescriptor = $ evolugridInstanceDescriptor ->getProperty ('url ' )->getValue ();
129+ if ($ evolugridResultSetInstanceDescriptor == null ) {
130+ $ this ->newEvolugrid ($ selfedit );
131+
132+ return ;
133+ }
134+
135+ $ this ->errorMessage = "Sorry, Evolugrid does not support initializing from SQL query if the 'url' parameter
135136 is not first set to <em>null</em>. This is to avoid you loosing important configuration settings
136137 by using the wizard. If you are sure you want to use this SQL query wizard, you can set the 'url'
137138 parameter to <em>null</em> and click again the 'Generate from SQL' button. " ;
138- $ this ->content ->addFile (dirname (__FILE__ )."/../../../../../views/unableEvolugrid.php " , $ this );
139- $ this ->template ->toHtml ();
140- }
141-
142- }
139+ $ this ->content ->addFile (dirname (__FILE__ ).'/../../../../../views/unableEvolugrid.php ' , $ this );
140+ $ this ->template ->toHtml ();
141+ }
142+ }
0 commit comments