1
+ <?php
2
+ class ControllerModuleAutoBanner extends Controller {
3
+ private $ error = array ();
4
+ public $ MODULENAME = 'autobanner ' ;
5
+
6
+ function _make_static_data () {
7
+ $ this ->load ->language ("module/ {$ this ->MODULENAME }" );
8
+ $ this ->document ->setTitle ($ this ->language ->get ('heading_title ' ));
9
+ $ this ->data ['heading_title ' ] = $ this ->language ->get ('heading_title ' );
10
+
11
+ /* Breadcrumbs */
12
+ $ this ->data ['breadcrumbs ' ] = array ();
13
+
14
+ $ this ->data ['breadcrumbs ' ][] = array (
15
+ 'text ' => $ this ->language ->get ('text_home ' ),
16
+ 'href ' => $ this ->url ->link ('common/home ' , 'token= ' . $ this ->session ->data ['token ' ], 'SSL ' ),
17
+ 'separator ' => FALSE
18
+ );
19
+
20
+ $ this ->data ['breadcrumbs ' ][] = array (
21
+ 'text ' => $ this ->language ->get ('text_module ' ),
22
+ 'href ' => $ this ->url ->link ('extension/module ' , 'token= ' . $ this ->session ->data ['token ' ], 'SSL ' ),
23
+ 'separator ' => ' :: '
24
+ );
25
+
26
+ $ this ->data ['breadcrumbs ' ][] = array (
27
+ 'text ' => $ this ->language ->get ('heading_title ' ),
28
+ 'href ' => $ this ->url ->link ("module/ {$ this ->MODULENAME }" , 'token= ' . $ this ->session ->data ['token ' ], 'SSL ' ),
29
+ 'separator ' => ' :: '
30
+ );
31
+
32
+ /* Error messages */
33
+ $ error_msgs = array ();
34
+ if (isset ($ this ->session ->data ['error ' ])) {
35
+ $ error_msgs [] = $ this ->session ->data ['error ' ];
36
+ }
37
+ if (isset ($ this ->error ['warning ' ])) {
38
+ $ error_msgs [] = $ this ->error ['warning ' ];
39
+ }
40
+ $ this ->data ['error_warning ' ] = join ('<br /> ' , $ error_msgs );
41
+
42
+ if (isset ($ this ->session ->data ['success ' ])) {
43
+ $ this ->data ['success ' ] = $ this ->session ->data ['success ' ];
44
+ }
45
+ else {
46
+ $ this ->data ['success ' ] = '' ;
47
+ }
48
+ $ this ->session ->data ['error ' ] = '' ;
49
+ $ this ->session ->data ['success ' ] = '' ;
50
+
51
+ $ this ->children = array (
52
+ 'common/header ' ,
53
+ 'common/footer '
54
+ );
55
+ }
56
+
57
+ function _get_banner_info ($ banner_id ) {
58
+ $ banner_info = $ this ->model_design_banner ->getBanner ($ banner_id );
59
+
60
+ $ token = $ this ->session ->data ['token ' ];
61
+ $ banner_info ['edit_link ' ] = $ this ->url ->link (
62
+ "module/ {$ this ->MODULENAME }/update " ,
63
+ "token= $ token&banner_id= $ banner_id " , 'SSL ' );
64
+
65
+ return $ banner_info ;
66
+ }
67
+
68
+ public function index () {
69
+ $ this ->load ->model ('module/autobanner ' );
70
+ $ this ->load ->model ('design/banner ' );
71
+
72
+ $ this ->_make_static_data ();
73
+
74
+ /* Button */
75
+ $ this ->data ['button_insert ' ] = $ this ->language ->get ('button_insert ' );
76
+ $ this ->data ['button_delete ' ] = $ this ->language ->get ('button_delete ' );
77
+ $ this ->data ['insert ' ] = $ this ->url ->link ("module/ {$ this ->MODULENAME }/insert " , 'token= ' . $ this ->session ->data ['token ' ], 'SSL ' );
78
+ $ this ->data ['delete ' ] = $ this ->url ->link ("module/ {$ this ->MODULENAME }/delete " , 'token= ' . $ this ->session ->data ['token ' ], 'SSL ' );
79
+
80
+ /* Get banners which were created by this module */
81
+ $ own_banners = $ this ->model_module_autobanner ->getBannerIds ();
82
+ $ this ->data ['own_banners ' ] = array_map (array ($ this , '_get_banner_info ' ),
83
+ $ own_banners );
84
+
85
+ $ this ->template = "module/ {$ this ->MODULENAME }.tpl " ;
86
+ $ this ->response ->setOutput ($ this ->render ());
87
+ }
88
+
89
+ function _make_form_data ($ banner_id =0 ) {
90
+ $ this ->data ['text_enabled ' ] = $ this ->language ->get ('text_enabled ' );
91
+ $ this ->data ['text_disabled ' ] = $ this ->language ->get ('text_disabled ' );
92
+ $ this ->data ['text_default ' ] = $ this ->language ->get ('text_default ' );
93
+ $ this ->data ['text_image_manager ' ] = $ this ->language ->get ('text_image_manager ' );
94
+ $ this ->data ['text_browse ' ] = $ this ->language ->get ('text_browse ' );
95
+ $ this ->data ['text_clear ' ] = $ this ->language ->get ('text_clear ' );
96
+
97
+ $ this ->data ['entry_name ' ] = $ this ->language ->get ('entry_name ' );
98
+ $ this ->data ['entry_title ' ] = $ this ->language ->get ('entry_title ' );
99
+ $ this ->data ['entry_link ' ] = $ this ->language ->get ('entry_link ' );
100
+ $ this ->data ['entry_image ' ] = $ this ->language ->get ('entry_image ' );
101
+ $ this ->data ['entry_status ' ] = $ this ->language ->get ('entry_status ' );
102
+
103
+
104
+ if (isset ($ this ->error ['name ' ])) {
105
+ $ this ->data ['error_name ' ] = $ this ->error ['name ' ];
106
+ } else {
107
+ $ this ->data ['error_name ' ] = '' ;
108
+ }
109
+
110
+ if ($ banner_id ) {
111
+ $ this ->load ->model ('design/banner ' );
112
+ $ banner_info = $ this ->model_design_banner ->getBanner ($ banner_id );
113
+ $ this ->data ['banner_id ' ] = $ banner_id ;
114
+ }
115
+
116
+ if (isset ($ this ->request ->post ['name ' ])) {
117
+ $ this ->data ['name ' ] = $ this ->request ->post ['name ' ];
118
+ } elseif (!empty ($ banner_info )) {
119
+ $ this ->data ['name ' ] = $ banner_info ['name ' ];
120
+ } else {
121
+ $ this ->data ['name ' ] = '' ;
122
+ }
123
+
124
+ if (isset ($ this ->request ->post ['status ' ])) {
125
+ $ this ->data ['status ' ] = $ this ->request ->post ['status ' ];
126
+ } elseif (!empty ($ banner_info )) {
127
+ $ this ->data ['status ' ] = $ banner_info ['status ' ];
128
+ } else {
129
+ $ this ->data ['status ' ] = true ;
130
+ }
131
+ }
132
+
133
+ function _show_form ($ banner_id =0 ) {
134
+ /* Button */
135
+ $ this ->data ['button_save ' ] = $ this ->language ->get ('button_save ' );
136
+ $ this ->data ['button_cancel ' ] = $ this ->language ->get ('button_cancel ' );
137
+
138
+ /* Target for form */
139
+ $ token = $ this ->session ->data ['token ' ];
140
+ if ($ banner_id ) {
141
+ $ this ->data ['action ' ] = $ this ->url ->link (
142
+ "module/ {$ this ->MODULENAME }/update " ,
143
+ "token= $ token&banner_id= $ banner_id " , 'SSL ' );
144
+ }
145
+ else {
146
+ $ this ->data ['action ' ] = $ this ->url ->link (
147
+ "module/ {$ this ->MODULENAME }/insert " ,
148
+ "token= $ token " , 'SSL ' );
149
+ }
150
+
151
+ /* Cancel button's link target */
152
+ $ this ->data ['cancel ' ] = $ this ->url ->link ("module/ {$ this ->MODULENAME }" ,
153
+ "token= $ token " , 'SSL ' );
154
+
155
+ $ this ->_make_form_data ($ banner_id );
156
+
157
+ $ this ->document ->addScript ('view/javascript/jquery/ui/jquery.dataTables.min.js ' );
158
+ $ this ->document ->addStyle ('view/stylesheet/autobanner.css ' );
159
+
160
+ $ this ->load ->model ('catalog/product ' );
161
+ $ this ->load ->model ('tool/image ' );
162
+
163
+ if (!$ banner_id ) {
164
+ $ criteria = array ('filter_status ' => TRUE );
165
+ $ allproducts = $ this ->model_catalog_product ->getProducts ($ criteria );
166
+ $ ownproducts = array ();
167
+ }
168
+ else {
169
+ $ prd_ids = $ this ->model_module_autobanner ->getProductIds ($ banner_id );
170
+ $ allproducts = $ this ->model_module_autobanner ->getProductsByIds ($ prd_ids ,
171
+ FALSE );
172
+ $ ownproducts = $ this ->model_module_autobanner ->getProductsByIds ($ prd_ids ,
173
+ TRUE );
174
+ }
175
+
176
+ $ allproducts = array_map (array ($ this , '_reduce_product ' ), $ allproducts );
177
+ $ ownproducts = array_map (array ($ this , '_reduce_product ' ), $ ownproducts );
178
+ $ this ->data ['allproducts ' ] = $ allproducts ;
179
+ $ this ->data ['ownproducts ' ] = $ ownproducts ;
180
+
181
+ $ this ->template = "module/ {$ this ->MODULENAME }_form.tpl " ;
182
+ $ this ->response ->setOutput ($ this ->render ());
183
+ }
184
+
185
+ public function insert () {
186
+ $ this ->load ->model ('module/autobanner ' );
187
+ $ this ->_make_static_data ();
188
+
189
+ if ($ this ->request ->server ['REQUEST_METHOD ' ] == 'POST ' ) {
190
+ $ this ->_process_submitted_data ();
191
+ }
192
+
193
+ $ this ->_show_form ();
194
+ }
195
+
196
+ public function update () {
197
+ $ this ->load ->model ('module/autobanner ' );
198
+ $ this ->_make_static_data ();
199
+
200
+ if ($ this ->request ->server ['REQUEST_METHOD ' ] == 'POST ' ) {
201
+ $ this ->_process_submitted_data ();
202
+ }
203
+
204
+ $ id = (int )$ this ->request ->get ['banner_id ' ];
205
+ $ this ->_show_form ($ id );
206
+ }
207
+
208
+ function _reduce_product ($ prd ) {
209
+ $ img = $ prd ['image ' ];
210
+ if ($ img && file_exists (DIR_IMAGE . $ img )) {
211
+ $ image = $ this ->model_tool_image ->resize ($ img , 40 , 40 );
212
+ }
213
+ else {
214
+ $ image = $ this ->model_tool_image ->resize ('no_image.jpg ' , 40 , 40 );
215
+ }
216
+ return array (
217
+ 'product_id ' => $ prd ['product_id ' ],
218
+ 'name ' => $ prd ['name ' ],
219
+ 'model ' => $ prd ['model ' ],
220
+ 'image ' => $ image
221
+ );
222
+ }
223
+
224
+ function _desc_in_langs ($ desc , $ languages ) {
225
+ $ d = array ();
226
+ foreach ($ languages as $ l ) {
227
+ $ lid = $ l ['language_id ' ];
228
+ $ d [$ lid ] = array ('title ' => $ desc );
229
+ }
230
+ return $ d ;
231
+ }
232
+ function _process_submitted_data () {
233
+ if (!$ this ->_validate ()) {
234
+ return ;
235
+ }
236
+
237
+ /* Data OK */
238
+ $ banner = array (
239
+ 'name ' => $ this ->request ->post ['name ' ],
240
+ 'status ' => $ this ->request ->post ['status ' ]
241
+ );
242
+ if (isset ($ this ->request ->get ['banner_id ' ])) {
243
+ $ banner_id = (int )$ this ->request ->get ['banner_id ' ];
244
+ }
245
+ $ this ->load ->model ('catalog/product ' );
246
+ $ this ->load ->model ('localisation/language ' );
247
+ $ languages = $ this ->model_localisation_language ->getLanguages ();
248
+ $ products = $ this ->request ->post ['selected ' ];
249
+ $ banner_image = array ();
250
+ foreach ($ products as $ id ) {
251
+ $ product_info = $ this ->model_catalog_product ->getProduct ($ id );
252
+ $ banner_image_description = $ this ->_desc_in_langs ($ product_info ['name ' ],
253
+ $ languages );
254
+ $ image = $ product_info ['image ' ];
255
+ $ cat = $ this ->model_catalog_product ->getProductCategories ($ id )[0 ];
256
+ $ link = $ this ->url ->link ('product/product ' , "path= $ cat&product_id= $ id " );
257
+ $ banner_image [] = compact ('banner_image_description ' , 'image ' , 'link ' );
258
+ }
259
+ $ banner ['banner_image ' ] = $ banner_image ;
260
+
261
+ /* Save to database */
262
+ $ this ->load ->model ('design/banner ' );
263
+ if (isset ($ banner_id )) {
264
+ $ this ->model_design_banner ->editBanner ($ banner_id , $ banner );
265
+ /* Update the list of banners created by this module */
266
+ $ this ->model_module_autobanner ->editBanner ($ banner_id , $ products );
267
+ }
268
+ else {
269
+ $ banner_id = $ this ->model_design_banner ->addBanner ($ banner );
270
+ /* Update the list of banners created by this module */
271
+ $ this ->model_module_autobanner ->addBanner ($ banner_id , $ products );
272
+ }
273
+ $ this ->session ->data ['success ' ] = $ this ->language ->get ('text_success ' );
274
+
275
+ /* Redirect to parent page */
276
+ $ this ->redirect ($ this ->url ->link ('module/autobanner ' , 'token= ' . $ this ->session ->data ['token ' ], 'SSL ' ));
277
+ }
278
+
279
+ function _validate () {
280
+ $ this ->request ->post ['name ' ] = trim ($ this ->request ->post ['name ' ]);
281
+ if (!$ this ->user ->hasPermission ('modify ' , 'design/banner ' )) {
282
+ $ this ->error ['warning ' ] = $ this ->language ->get ('error_permission ' );
283
+ }
284
+
285
+ if ((utf8_strlen ($ this ->request ->post ['name ' ]) < 3 ) || (utf8_strlen ($ this ->request ->post ['name ' ]) > 64 )) {
286
+ $ this ->error ['name ' ] = $ this ->language ->get ('error_name ' );
287
+ }
288
+
289
+ if (!$ this ->error ) {
290
+ return true ;
291
+ } else {
292
+ return false ;
293
+ }
294
+ }
295
+
296
+ public function install () {
297
+ $ this ->load ->language ('extension/module ' );
298
+
299
+ /* Though permission checking has been done in extesion/module already,
300
+ * we still have to check again because this function is public,
301
+ * one may call it via URL. */
302
+ if (!$ this ->user ->hasPermission ('modify ' , 'extension/module ' )) {
303
+ $ this ->session ->data ['error ' ] = $ this ->language ->get ('error_permission ' );
304
+ }
305
+ else {
306
+ /*Permisson is OK */
307
+ $ this ->load ->model ('module/autobanner ' );
308
+ $ this ->model_module_autobanner ->install ();
309
+ }
310
+ }
311
+
312
+ public function uninstall () {
313
+ $ this ->load ->language ('extension/module ' );
314
+
315
+ /* Though permission checking has been done in extesion/module already,
316
+ * we still have to check again because this function is public,
317
+ * one may call it via URL. */
318
+ if (!$ this ->user ->hasPermission ('modify ' , 'extension/module ' )) {
319
+ $ this ->session ->data ['error ' ] = $ this ->language ->get ('error_permission ' );
320
+ }
321
+ else {
322
+ /*Permisson is OK */
323
+ $ this ->load ->model ('module/autobanner ' );
324
+ $ this ->model_module_autobanner ->uninstall ();
325
+ }
326
+ }
327
+ }
328
+ ?>
0 commit comments