Blame view

src/app/backend/controllers/NewsController.php 17.3 KB
1ea3b987   Administrator   maby first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
  <?php

  

  namespace controllers;

  

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  

  class NewsController extends \Phalcon\Mvc\Controller

  {

      function indexAction()

      {

  

          if( !$this->session->get('isAdminAuth') )

          {

              return $this->response->redirect([ 'for' => 'admin_login' ]);

          }

          $params         = $this->dispatcher->getParams();

  

          $page           = !empty( $params['page']  ) ? $params['page'] : 1;

  

          $total = $this->models->getNews()->countData2();

  

          if( $total['0']['total'] > \config::get( 'limits/items') )

          {

  

              $paginate = $this->common->paginate(

                  [

                      'page'              => $page,

                      'items_per_page'    => \config::get( 'limits/admin_orders', 5),

                      'total_items'       => $total[0]['total'],

                      'url_for'           => [ 'for' => 'news_index_paged', 'page' => $page ],

                      'index_page'       => 'news_index'

                  ], true

              );

  

  

          }

  

  

          $news = $this->models->getNews()->getAllNewsData(1,$page );

  

          $this->view->setVars([

              'info' => $news,

              'paginate' => !empty($paginate['output']) ? $paginate['output'] : '' ,

          ]);

      }

  

      function deleteAction($id)

      {

          if( !$this->session->get('isAdminAuth') )

          {

              return $this->response->redirect([ 'for' => 'admin_login' ]);

          }

          $this->models->getNews()->deleteNews($id);

          return $this->response->redirect([ 'for' => 'news_index' ]);

      }

  

      function updateAction($id)

      {

  

          if( !$this->session->get('isAdminAuth') )

          {

              return $this->response->redirect([ 'for' => 'admin_login' ]);

          }

  

          if( $this->request->isPost() )

          {

  

  

              $page_add['title']['1']            = $this->request->getPost('title_1', 'string', NULL );

              $page_add['alias']['1']            = $this->request->getPost('alias_1', 'string', NULL );

              $page_add['content']['1']          = $this->request->getPost('page_content_text_1', NULL );

              $page_add['meta_title']['1']       = $this->request->getPost('meta_title_1', 'string', NULL );

              $page_add['meta_keywords']['1']    = $this->request->getPost('meta_keywords_1', 'string', NULL );

              $page_add['meta_description']['1'] = $this->request->getPost('meta_description_1', 'string', NULL );

              $page_add['video']['1']            = $this->request->getPost('video_1', 'string', NULL);

  			$page_add['video_sort']['1']       = $this->request->getPost('video_sort_1', 'string', NULL);

              $page_add['status']['1']           = $this->request->getPost('status_1', 'int', NULL );

              $page_add['options']['1']          = $this->request->getPost('options_1', 'string', NULL );

              $page_add['abstract']['1']         = $this->request->getPost('abstract_1', 'string', NULL );

  

  

              $page_add['title']['2']            = $this->request->getPost('title_2', 'string', NULL );

              $page_add['alias']['2']            = $this->request->getPost('alias_2', 'string', NULL );

              $page_add['content']['2']          = $this->request->getPost('page_content_text_2', NULL );

              $page_add['meta_title']['2']       = $this->request->getPost('meta_title_2', 'string', NULL );

              $page_add['meta_keywords']['2']    = $this->request->getPost('meta_keywords_2', 'string', NULL );

              $page_add['meta_description']['2'] = $this->request->getPost('meta_description_2', 'string', NULL );

              $page_add['video']['2']            = $this->request->getPost('video_2', 'string', NULL);

  			$page_add['video_sort']['2']       = $this->request->getPost('video_sort_2', 'string', NULL);

              $page_add['status']['2']           = $this->request->getPost('status_2', 'int', NULL );

              $page_add['options']['2']          = $this->request->getPost('options_2', 'string', NULL );

              $page_add['abstract']['2']         = $this->request->getPost('abstract_2', 'string', NULL );

  			

  			$page_add['rubric']['1']           = $this->request->getPost('rubric', 'int', NULL );

  			$page_add['rubric']['2']           = $this->request->getPost('rubric', 'int', NULL );

  

              $page_add['pic_status']['2']       = $page_add['pic_status']['1']       = $this->request->getPost('pic_status', 'string', NULL );

              $page['covers']                    = $this->request->getPost('cover', 'string', NULL );

              $page_add['cover']['1']            = $page_add['cover']['2'] = $page['covers'] ? $page['covers'] : '79846ba311e2be434940b8e570642e98';

  

              $page_add['status']['1']           = empty( $page_add['status']['1'] ) ? 0 : 1;

              $page_add['status']['2']           = empty( $page_add['status']['2'] ) ? 0 : 1;

              $page_add['pic_status']['1']       = empty( $page_add['pic_status']['1'] ) ? 0 : 1;

              $page_add['pic_status']['2']       = empty( $page_add['pic_status']['2'] ) ? 0 : 1;

  

              if(empty($page_add['video']['1']) ){

                  $page_add['video']['1'] = NULL;

              }

              if(empty($page_add['video']['2']) ){

                  $page_add['video']['2'] = NULL;

              }

              if(empty($page_add['video_sort']['1']) ){

                  $page_add['video_sort']['1'] = 0;

              }

              if(empty($page_add['video_sort']['2']) ){

                  $page_add['video_sort']['2'] = 0;

              }			

  

              $group_id = $this->request->getPost('group_id' );

              $page_add['products_id']['1'] = $page_add['products_id']['2'] = !empty($group_id)? '{'.implode(',',array_unique($group_id)).'}' : null;

  

              $page_add_with_lang                = [];

  

              if ($this->request->hasFiles() == true) {

  

                  foreach ($this->request->getUploadedFiles() as $file){

  

                      $allowed_filetypes = array('.jpg','.JPG');

  

                      $ext = substr($file->getName() ,strpos($file->getName() ,'.'),strlen($file->getName() )-1);

                      if(in_array($ext,$allowed_filetypes))

                      {

                          $path = $file->getTempName();

  

                          $md5_file = md5_file($path);

  

                          $image_path = $this->storage->getPhotoPath( 'news', $md5_file, 'original' );

                          if(!file_exists($image_path))

                          {

                              $this->storage->mkdir( 'news', $md5_file );

                              $file->moveTo($image_path);

                              $this->storage->imageResizeWithCrop( [], $md5_file, 'news' );

                          }

                          $page_add['cover']['1'] = $page_add['cover']['2']  = $md5_file;

                      } else {

                          $this->flash->error( 'Произошла ошибка. Не верный формат файла.' );

                      }

  

                  }

              }

  

              if( !empty($page_add['title']['1']) && !empty($page_add['alias']['1']) && !empty($page_add['content']['1']) )

              {

  

                  foreach( $page_add as $k => $p )

                  {

                      $page_add_with_lang['1'][$k]           = $p['1'];

                  }

              }

  

              if( !empty($page_add['title']['2']) && !empty($page_add['alias']['2']) && !empty($page_add['content']['2']) )

              {

                  foreach( $page_add as $k => $p )

                  {

                      $page_add_with_lang['2'][$k] = $p['2'];

                  }

              }

  

              //p($page_add_with_lang,1);

  

              if( !empty( $page_add_with_lang ) )

              {

                  if( $this->models->getNews()->updateData( $page_add_with_lang, $id ) )

                  {

                      $this->flash->success( 'Вы удачно обновили стаью' );

                      return $this->response->redirect([ 'for' => 'news_index' ]);

                  }

                  else

                  {

                      $this->flash->error( 'Произошла ошибка во время добавления статьи. Повторите попытку позже' );

                  }

              }

          }

          $news_lang = '';

          $news = $this->models->getNews()->getOneNewsData( $id);

          $groups_data = '';

          if(!empty($news[0]['group_id'])) {

              $group_id = $this->etc->int2arr($news[0]['group_id'],true);

  

              $groups_data = $this->models->getItems()->getGroupLike(implode("','",$group_id));

          }

  

          $this->view->pick( 'news/addEdit' );

          foreach($news as $p )

          {

              $news_lang[$p['lang_id']] = $p;

          }

          $rubrics = $this->models->getRubricsNews()->getAllRubrics();

  		$this->view->setVars([

              'rubrics' => $rubrics,

  			'page' => $news_lang,

              'group' => $groups_data

          ]);

  

      }

  

      function addAction()

      {

          if( !$this->session->get('isAdminAuth') )

          {

              return $this->response->redirect([ 'for' => 'admin_login' ]);

          }

  

          if( $this->request->isPost() )

          {

  

              $page_add['title']['1']            = $this->request->getPost('title_1', 'string', NULL );

              $page_add['alias']['1']            = $this->request->getPost('alias_1', 'string', NULL );

              $page_add['content']['1']          = $this->request->getPost('page_content_text_1', NULL );

              $page_add['meta_title']['1']       = $this->request->getPost('meta_title_1', 'string', NULL );

              $page_add['meta_keywords']['1']    = $this->request->getPost('meta_keywords_1', 'string', NULL );

              $page_add['meta_description']['1'] = $this->request->getPost('meta_description_1', 'string', NULL );

              $page_add['video']['1']            = $this->request->getPost('video_1', 'string', NULL );

  			$page_add['video_sort']['1']            = $this->request->getPost('video_sort_1', 'string', NULL);

              $page_add['status']['1']           = $this->request->getPost('status_1', 'int', NULL );

              $page_add['options']['1']          = $this->request->getPost('options_1', 'string', NULL );

              $page_add['abstract']['1']         = $this->request->getPost('abstract_1', 'string', NULL );

  

              $page_add['title']['2']            = $this->request->getPost('title_2', 'string', NULL );

              $page_add['alias']['2']            = $this->request->getPost('alias_2', 'string', NULL );

              $page_add['video']['2']            = $this->request->getPost('video_2', 'string', NULL );

  			$page_add['video_sort']['2']            = $this->request->getPost('video_sort_2', 'string', NULL);

              $page_add['status']['2']           = $this->request->getPost('status_2', 'int', NULL );

              $page_add['options']['2']          = $this->request->getPost('options_2', 'string', NULL );

              $page_add['content']['2']          = $this->request->getPost('page_content_text_2', NULL );

              $page_add['abstract']['2']         = $this->request->getPost('abstract_2', 'string', NULL );

              $page_add['meta_title']['2']       = $this->request->getPost('meta_title_2', 'string', NULL );

              $page_add['meta_keywords']['2']    = $this->request->getPost('meta_keywords_2', 'string', NULL );

              $page_add['meta_description']['2'] = $this->request->getPost('meta_description_2', 'string', NULL );

  			

  			$page_add['rubric']['1']           = $this->request->getPost('rubric', 'int', NULL );

  			$page_add['rubric']['2']           = $this->request->getPost('rubric', 'int', NULL );

  

  

              $group_id = $this->request->getPost('group_id' );

  

              if(empty($page_add['video']['1']) ){

                  $page_add['video']['1'] = NULL;

              }

              if(empty($page_add['video']['2']) ){

                  $page_add['video']['2'] = NULL;

              }

              if(empty($page_add['video_sort']['1']) ){

                  $page_add['video_sort']['1'] = 0;

              }

              if(empty($page_add['video_sort']['2']) ){

                  $page_add['video_sort']['2'] = 0;

              }			

  

              $page_add['pic_status']['2']       = $page_add['pic_status']['1'] = $this->request->getPost('pic_status', 'string', NULL );

  

              $page_add['products_id']['1'] = $page_add['products_id']['2'] = !empty($group_id)? '{'.implode(',',array_unique($group_id)).'}' : null;

  

              $page['covers']                    = $this->request->getPost('cover', 'string', NULL );

              $page_add['cover']['1']            = $page_add['cover']['2'] = $page['covers'] ? $page['covers'] : '79846ba311e2be434940b8e570642e98';

  

              $page_add['status']['1']           = empty( $page_add['status']['1'] ) ? 0 : 1;

              $page_add['status']['2']           = empty( $page_add['status']['2'] ) ? 0 : 1;

              $page_add['pic_status']['1']       = empty( $page_add['pic_status']['1'] ) ? 0 : 1;

              $page_add['pic_status']['2']       = empty( $page_add['pic_status']['2'] ) ? 0 : 1;

  

  

              $page_add_with_lang                     = [];

  

              if ($this->request->hasFiles() == true) {

  

                  foreach ($this->request->getUploadedFiles() as $file){

  

                      $allowed_filetypes = array('.jpg','.JPG');

  

                      $ext = substr($file->getName() ,strpos($file->getName() ,'.'),strlen($file->getName() )-1);

                      if(in_array($ext,$allowed_filetypes))

                      {

                          $path = $file->getTempName();

  

                          $md5_file = md5_file($path);

  

                          $image_path = $this->storage->getPhotoPath( 'news', $md5_file, 'original' );

                          if(!file_exists(substr($image_path ,0,strlen($image_path )-13)))

                          {

                              $this->storage->mkdir( 'news', $md5_file );

                              $file->moveTo($image_path);

                              $this->storage->imageResizeWithCrop( [], $md5_file, 'news' );

                          }

                          $page_add['cover']['1'] = $page_add['cover']['2']  = $md5_file;

                      } else {

                          $this->flash->error( 'Произошла ошибка. Не верный формат файла.' );

                      }

  

  

                  }

              }

  

              if( !empty($page_add['title']['1']) && !empty($page_add['alias']['1']) && !empty($page_add['content']['1']) )

              {

  

                  foreach( $page_add as $k => $p )

                  {

  

                      $page_add_with_lang['1'][$k]           = $p['1'];

                  }

              }

  

              if( !empty($page_add['title']['2']) && !empty($page_add['alias']['2']) && !empty($page_add['content']['2']) )

              {

                  foreach( $page_add as $k => $p )

                  {

                      $page_add_with_lang['2'][$k] = $p['2'];

                  }

              }

  

              //p($page_add_with_lang,1);

  

              if( !empty( $page_add_with_lang ) )

              {

                  if( $this->models->getNews()->addData( $page_add_with_lang ) )

                  {

                      $this->flash->success( 'Вы удачно добавили статическую страницу' );

                      return $this->response->redirect([ 'for' => 'news_index' ]);

                  }

                  else

                  {

                      $this->flash->error( 'Произошла ошибка во время добавления страницы. Повторите попытку позже' );

                  }

              }

          }

  

          $this->view->pick( 'news/addEdit' );

  		$rubrics = $this->models->getRubricsNews()->getAllRubrics();

          $this->view->setVars([

  			'rubrics' => $rubrics,

          ]);

  

      }

  

      public function getProductLikeAction()

      {

          $like = $this->request->getPost('like', 'string', NULL );

          $data = $this->models->getItems()->getProductLike($like);

          $result = json_encode($data);

          $this->view->disableLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT);

          echo $result;

      }

  	

  	

  	

  	

  	

  	

  	

  	

  	

  	

      function rubricsAction()

      {

          $params         = $this->dispatcher->getParams();

  		$lang_id = (!empty($params['lang_id'])) ? $params['lang_id'] : 1;

  		$rubric = array();

  		

  		if(!empty($_POST['name_rus'])){

  			if(!empty($_POST['update_id']))

  			$this->models->getRubricsNews()->updateRubric($_POST);

  			else

  			$this->models->getRubricsNews()->addRubric($_POST);

  			

  			return $this->response->redirect([ 'for' => 'news_rubrics' ]);

  		}

  		

  		if(!empty($params['id'])){

  			$rubric = $this->models->getRubricsNews()->getOneRubric($params['id'],$lang_id);

  		}

  		$rubrics = $this->models->getRubricsNews()->getAllRubrics();

  		$this->view->setVars([

  			'rubrics' => $rubrics,

  			'rubric' => $rubric,

          ]);

  	}

  	

  	public function rubrics_deleteAction(){

  		$params         = $this->dispatcher->getParams();

  		$this->models->getRubricsNews()->deleteRubric($params['id']);

  		return $this->response->redirect([ 'for' => 'news_rubrics' ]);

  	}

  

  	

  	

  	

  	

  	

  }