Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15351 | Rev 16766 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 15351 Rev 15559
Línea 1... Línea 1...
1
<?php
1
<?php
-
 
2
 
2
declare(strict_types=1);
3
declare(strict_types=1);
Línea 3... Línea 4...
3
 
4
 
Línea 4... Línea 5...
4
namespace LeadersLinked\Controller;
5
namespace LeadersLinked\Controller;
Línea 22... Línea 23...
22
    /**
23
    /**
23
     *
24
     *
24
     * @var AdapterInterface
25
     * @var AdapterInterface
25
     */
26
     */
26
    private $adapter;
27
    private $adapter;
27
    
28
 
28
    
29
 
29
    /**
30
    /**
30
     *
31
     *
31
     * @var AbstractAdapter
32
     * @var AbstractAdapter
32
     */
33
     */
33
    private $cache;
34
    private $cache;
34
    
35
 
35
    /**
36
    /**
36
     *
37
     *
37
     * @var  LoggerInterface
38
     * @var  LoggerInterface
38
     */
39
     */
39
    private $logger;
40
    private $logger;
Línea 40... Línea 41...
40
 
41
 
41
    
42
 
42
    /**
43
    /**
43
     *
44
     *
44
     * @var array
45
     * @var array
45
     */
46
     */
Línea 50... Línea 51...
50
     * @param AdapterInterface $adapter
51
     * @param AdapterInterface $adapter
51
     * @param AbstractAdapter $cache
52
     * @param AbstractAdapter $cache
52
     * @param LoggerInterface $logger
53
     * @param LoggerInterface $logger
53
     * @param array $config
54
     * @param array $config
54
     */
55
     */
55
    public function __construct($adapter, $cache , $logger, $config)
56
    public function __construct($adapter, $cache, $logger, $config)
56
    {
57
    {
57
        $this->adapter      = $adapter;
58
        $this->adapter      = $adapter;
58
        $this->cache        = $cache;
59
        $this->cache        = $cache;
59
        $this->logger       = $logger;
60
        $this->logger       = $logger;
60
        $this->config       = $config;
61
        $this->config       = $config;
61
        
-
 
62
    }
62
    }
63
    
63
 
64
    public function indexAction()
64
    public function indexAction()
65
    {
65
    {
66
        $currentUserPlugin = $this->plugin('currentUserPlugin');
66
        $currentUserPlugin = $this->plugin('currentUserPlugin');
67
        $currentUser = $currentUserPlugin->getUser();
67
        $currentUser = $currentUserPlugin->getUser();
68
        
68
 
69
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
69
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
70
        $network = $currentNetworkPlugin->getNetwork();
70
        $network = $currentNetworkPlugin->getNetwork();
71
        
71
 
72
        $request = $this->getRequest();
72
        $request = $this->getRequest();
73
        if($request->isGet()) {
73
        if ($request->isGet()) {
74
            
74
 
75
            
75
 
76
            $headers  = $request->getHeaders();
76
            $headers  = $request->getHeaders();
77
            
77
 
78
            $isJson = false;
78
            $isJson = false;
79
            if($headers->has('Accept')) {
79
            if ($headers->has('Accept')) {
80
                $accept = $headers->get('Accept');
80
                $accept = $headers->get('Accept');
81
                
81
 
82
                $prioritized = $accept->getPrioritized();
82
                $prioritized = $accept->getPrioritized();
83
                
83
 
84
                foreach($prioritized as $key => $value) {
84
                foreach ($prioritized as $key => $value) {
85
                    $raw = trim($value->getRaw());
85
                    $raw = trim($value->getRaw());
86
                    
86
 
87
                    if(!$isJson) {
87
                    if (!$isJson) {
88
                        $isJson = strpos($raw, 'json');
88
                        $isJson = strpos($raw, 'json');
89
                    }
89
                    }
90
                    
-
 
91
                }
90
                }
92
            }
91
            }
93
            
92
 
94
            if($isJson) {
93
            if ($isJson) {
95
                
94
 
Línea 96... Línea 95...
96
 
95
 
97
                $search = $this->params()->fromQuery('search', []);
96
                $search = $this->params()->fromQuery('search', []);
98
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
97
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
99
                
98
 
100
                $page               = intval($this->params()->fromQuery('start', 1), 10);
99
                $page               = intval($this->params()->fromQuery('start', 1), 10);
101
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
100
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
102
                $order =  $this->params()->fromQuery('order', []);
101
                $order =  $this->params()->fromQuery('order', []);
103
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
102
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
104
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
103
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
105
    
104
 
106
                $fields =  ['title', 'date'];
105
                $fields =  ['title', 'date'];
107
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
106
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
108
                
107
 
109
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
108
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
110
                    $order_direction = 'ASC';
109
                    $order_direction = 'ASC';
111
                }
110
                }
112
    
111
 
113
                $postMapper = PostMapper::getInstance($this->adapter);
112
                $postMapper = PostMapper::getInstance($this->adapter);
114
                $paginator = $postMapper->fetchAllDataTable($search, $currentUser->network_id, $page, $records_x_page, $order_field, $order_direction);
113
                $paginator = $postMapper->fetchAllDataTable($search, $currentUser->network_id, $page, $records_x_page, $order_field, $order_direction);
115
                
114
 
116
                $items = [];
115
                $items = [];
117
                $records = $paginator->getCurrentItems();
116
                $records = $paginator->getCurrentItems();
118
                foreach($records as $record)
-
 
119
                {
117
                foreach ($records as $record) {
120
                    $dt = \DateTime::createFromFormat('Y-m-d', $record->date);
118
                    $dt = \DateTime::createFromFormat('Y-m-d', $record->date);
121
                    
119
 
122
                    $item = [
120
                    $item = [
123
                        'title' => $record->title,
121
                        'title' => $record->title,
124
                        'active' => $record->status ,
122
                        'active' => $record->status,
125
                        'date' => $dt->format('d/m/Y'),
123
                        'date' => $dt->format('d/m/Y'),
126
                        'actions' => [
124
                        'actions' => [
127
                            'link_edit' => $this->url()->fromRoute('publications/posts/edit', ['id' => $record->uuid ]),
125
                            'link_edit' => $this->url()->fromRoute('publications/posts/edit', ['id' => $record->uuid]),
128
                            'link_delete' => $this->url()->fromRoute('publications/posts/delete', ['id' => $record->uuid ]),
126
                            'link_delete' => $this->url()->fromRoute('publications/posts/delete', ['id' => $record->uuid]),
129
                            'link_view' => 'https://'. $network->main_hostname . '/post/' . $record->uuid,
127
                            'link_view' => 'https://' . $network->main_hostname . '/post/' . $record->uuid,
130
                        ] 
128
                        ]
131
                    ];
129
                    ];
132
                    
130
 
133
                    array_push($items, $item);
131
                    array_push($items, $item);
134
                }
132
                }
135
                
133
 
136
                return new JsonModel([
134
                return new JsonModel([
137
                    'success' => true,
135
                    'success' => true,
138
                    'data' => [
136
                    'data' => [
139
                        'items' => $items,
137
                        'items' => $items,
140
                        'total' => $paginator->getTotalItemCount(),
138
                        'total' => $paginator->getTotalItemCount(),
141
                    ]
139
                    ]
142
                ]);
140
                ]);
143
            } else  {
141
            } else {
144
                $image_size = $this->config['leaderslinked.image_sizes.post'];
142
                $image_size = $this->config['leaderslinked.image_sizes.post'];
145
                
143
 
146
                $formAdd = new PostCreateForm();
144
                $formAdd = new PostCreateForm();
147
                $formEdit = new PostEditForm();
145
                $formEdit = new PostEditForm();
148
                
146
 
149
                $this->layout()->setTemplate('layout/layout-backend');
147
                $this->layout()->setTemplate('layout/layout-backend');
150
                $viewModel = new ViewModel();
148
                $viewModel = new ViewModel();
151
                $viewModel->setTemplate('leaders-linked/posts/index.phtml');
149
                $viewModel->setTemplate('leaders-linked/posts/index.phtml');
152
                $viewModel->setVariables([
150
                $viewModel->setVariables([
153
                    'formAdd' => $formAdd,
151
                    'formAdd' => $formAdd,
154
                    'formEdit' => $formEdit,    
152
                    'formEdit' => $formEdit,
155
                    'image_size' => $image_size,
153
                    'image_size' => $image_size,
156
                    
154
 
157
                ]);
155
                ]);
158
                return $viewModel ;
156
                return $viewModel;
159
            }
157
            }
160
        } else {
158
        } else {
161
            return new JsonModel([
159
            return new JsonModel([
162
                'success' => false,
160
                'success' => false,
163
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
161
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
164
            ]);;
162
            ]);;
165
        }
163
        }
166
    }
164
    }
167
    
165
 
168
    
166
 
169
    
167
 
170
    public function addAction()
168
    public function addAction()
171
    {
169
    {
172
        
170
 
173
        $currentUserPlugin = $this->plugin('currentUserPlugin');
171
        $currentUserPlugin = $this->plugin('currentUserPlugin');
174
        $currentUser = $currentUserPlugin->getUser();
172
        $currentUser = $currentUserPlugin->getUser();
175
        
173
 
176
        $request    = $this->getRequest();
174
        $request    = $this->getRequest();
177
        if($request->isPost()) {
175
        if ($request->isPost()) {
178
            
176
 
179
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
177
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
180
            $dataPost['status'] = empty($dataPost['status']) ? Post::STATUS_INACTIVE : $dataPost['status'];
178
            $dataPost['status'] = empty($dataPost['status']) ? Post::STATUS_INACTIVE : $dataPost['status'];
181
            
179
 
182
            
180
 
183
            $form = new PostCreateForm();
181
            $form = new PostCreateForm();
184
            $form->setData($dataPost);
182
            $form->setData($dataPost);
185
            
183
 
186
            if($form->isValid()) {
184
            if ($form->isValid()) {
187
                $dataPost = (array) $form->getData();
185
                $dataPost = (array) $form->getData();
188
                
186
 
189
                $hydrator = new ObjectPropertyHydrator();
187
                $hydrator = new ObjectPropertyHydrator();
190
                $post = new Post();
188
                $post = new Post();
Línea 191... Línea 189...
191
                $hydrator->hydrate($dataPost, $post);
189
                $hydrator->hydrate($dataPost, $post);
192
 
190
 
193
                
191
 
194
                $dt = \DateTime::createFromFormat('d/m/Y', $post->date);
192
                $dt = \DateTime::createFromFormat('d/m/Y', $post->date);
195
                $post->date = $dt->format('Y-m-d');
193
                $post->date = $dt->format('Y-m-d');
196
                $post->network_id = $currentUser->network_id;
194
                $post->network_id = $currentUser->network_id;
Línea 197... Línea 195...
197
                $post->image = '';
195
                $post->image = '';
198
                $post->file  = '';
196
                $post->file  = '';
199
 
197
 
200
                
198
 
201
                $postMapper = PostMapper::getInstance($this->adapter);
199
                $postMapper = PostMapper::getInstance($this->adapter);
202
                if($postMapper->insert($post)) {
200
                if ($postMapper->insert($post)) {
203
                    $post = $postMapper->fetchOne($post->id);
201
                    $post = $postMapper->fetchOne($post->id);
204
                    
202
 
205
                    
203
 
206
                    $files = $this->getRequest()->getFiles()->toArray();
204
                    $files = $this->getRequest()->getFiles()->toArray();
207
                    
205
 
208
                    $target_path = $this->config['leaderslinked.fullpath.post'] .  $post->uuid;
206
                    $target_path = $this->config['leaderslinked.fullpath.post'] .  $post->uuid;
209
                    if(!file_exists($target_path)) {
207
                    if (!file_exists($target_path)) {
210
                        mkdir($target_path, 0755, true);
208
                        mkdir($target_path, 0755, true);
211
                    }
209
                    }
212
                    
210
 
213
                    
211
 
Línea 214... Línea 212...
214
                    if(isset($files['file']) && empty($files['file']['error'])) {
212
                    if (isset($files['file']) && empty($files['file']['error'])) {
215
                        $tmp_filename       = $files['file']['tmp_name'];
213
                        $tmp_filename       = $files['file']['tmp_name'];
216
                        $original_filename  = trim(strtolower($files['file']['name']));
214
                        $original_filename  = trim(strtolower($files['file']['name']));
217
 
215
 
218
                        $original_filename = Functions::normalizeString($original_filename);
216
                        $original_filename = Functions::normalizeString($original_filename);
219
                        
217
 
220
                        $parts = explode('.', $original_filename);
-
 
221
                        $original_filename = $parts[0] . '.' . $parts[ count($parts) - 1 ];
218
                        $parts = explode('.', $original_filename);
222
                        
219
                        $original_filename = $parts[0] . '.' . $parts[count($parts) - 1];
223
                        try 
220
 
224
                        {
221
                        try {
225
                            $full_filename = $target_path  . DIRECTORY_SEPARATOR .$original_filename;
222
                            $full_filename = $target_path  . DIRECTORY_SEPARATOR . $original_filename;
226
                            if(move_uploaded_file($tmp_filename, $full_filename)) {
223
                            if (move_uploaded_file($tmp_filename, $full_filename)) {
227
                                $post->file = basename($full_filename);
224
                                $post->file = basename($full_filename);
228
                                $post->update($post);
225
                                $post->update($post);
229
                            }
226
                            }
230
                        } catch(\Throwable $e) {
227
                        } catch (\Throwable $e) {
231
                            error_log($e->getTraceAsString());
228
                            error_log($e->getTraceAsString());
232
                        }
229
                        }
233
                    }
230
                    }
234
                    
231
 
235
                    if(isset($files['image']) && empty($files['image']['error'])) {
232
                    if (isset($files['image']) && empty($files['image']['error'])) {
236
                        $tmp_filename  = $files['image']['tmp_name'];
233
                        $tmp_filename  = $files['image']['tmp_name'];
237
                        
234
 
238
                        try {
235
                        try {
239
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.post']);
236
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.post']);
240
                            
237
 
241
                            $filename = 'image-' .uniqid() . '.png';
238
                            $filename = 'image-' . uniqid() . '.png';
242
                           // $crop_to_dimensions = true;
239
                            // $crop_to_dimensions = true;
243
                            // if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
240
                            // if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
244
                            
241
 
245
                            if(Image::uploadImageWithoutCompletationSize($tmp_filename, $target_path, $filename, $target_width, $target_height)) {
242
                            if (Image::uploadImageWithoutCompletationSize($tmp_filename, $target_path, $filename, $target_width, $target_height)) {
246
                                $post->image = $filename;
243
                                $post->image = $filename;
247
                                $postMapper->update($post);
244
                                $postMapper->update($post);
248
                            }
245
                            }
249
                        } catch(\Throwable $e) {
246
                        } catch (\Throwable $e) {
250
                            error_log($e->getTraceAsString());
247
                            error_log($e->getTraceAsString());
251
                        }
248
                        }
252
                    }
249
                    }
253
                    
250
 
254
                    $this->logger->info('Se agrego la noticia ' . $post->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
251
                    $this->logger->info('Se agrego la noticia ' . $post->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
255
                    
252
 
256
                    $data = [
253
                    $data = [
257
                        'success'   => true,
254
                        'success'   => true,
258
                        'data'   => 'LABEL_RECORD_ADDED'
255
                        'data'   => 'LABEL_RECORD_ADDED'
259
                    ];
256
                    ];
260
                } else {
257
                } else {
261
                    $data = [
-
 
262
                        'success'   => false,
258
                    $data = [
263
                        'data'      => $postMapper->getError()
259
                        'success'   => false,
264
                    ];
260
                        'data'      => $postMapper->getError()
265
                    
-
 
266
                }
261
                    ];
267
                
262
                }
268
                return new JsonModel($data);
263
 
269
                
264
                return new JsonModel($data);
270
            } else {
-
 
271
                $messages = [];
265
            } else {
272
                $form_messages = (array) $form->getMessages();
266
                $messages = [];
273
                foreach($form_messages  as $fieldname => $field_messages)
267
                $form_messages = (array) $form->getMessages();
274
                {
268
                foreach ($form_messages  as $fieldname => $field_messages) {
275
                    
269
 
276
                    $messages[$fieldname] = array_values($field_messages);
270
                    $messages[$fieldname] = array_values($field_messages);
277
                }
271
                }
278
                
272
 
279
                return new JsonModel([
273
                return new JsonModel([
280
                    'success'   => false,
-
 
281
                    'data'   => $messages
274
                    'success'   => false,
282
                ]);
275
                    'data'   => $messages
283
            }
276
                ]);
284
            
277
            }
285
        } else {
278
        } else {
286
            $data = [
279
            $data = [
287
                'success' => false,
280
                'success' => false,
288
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
281
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
289
            ];
282
            ];
290
            
283
 
291
            return new JsonModel($data);
284
            return new JsonModel($data);
292
        }
285
        }
293
        
286
 
294
        return new JsonModel($data);
287
        return new JsonModel($data);
295
    }
288
    }
296
    
289
 
297
    /**
290
    /**
298
     *
291
     *
299
     * Borrar un perfil excepto el público
292
     * Borrar un perfil excepto el público
300
     * @return \Laminas\View\Model\JsonModel
293
     * @return \Laminas\View\Model\JsonModel
301
     */
294
     */
302
    public function deleteAction()
295
    public function deleteAction()
303
    {
296
    {
304
        $currentUserPlugin = $this->plugin('currentUserPlugin');
297
        $currentUserPlugin = $this->plugin('currentUserPlugin');
305
        $currentUser = $currentUserPlugin->getUser();
298
        $currentUser = $currentUserPlugin->getUser();
306
        
299
 
307
        $request    = $this->getRequest();
300
        $request    = $this->getRequest();
308
        $id = $this->params()->fromRoute('id');
301
        $id = $this->params()->fromRoute('id');
309
        
302
 
310
        
303
 
311
        
304
 
312
        $postMapper = PostMapper::getInstance($this->adapter);
305
        $postMapper = PostMapper::getInstance($this->adapter);
313
        $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
306
        $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
314
        
307
 
315
        if(!$post) {
308
        if (!$post) {
316
            return new JsonModel([
-
 
317
                'success' => false,
309
            return new JsonModel([
318
                'data' => 'ERROR_POST_NOT_FOUND'
310
                'success' => false,
319
            ]);
311
                'data' => 'ERROR_POST_NOT_FOUND'
320
            
312
            ]);
321
        }
313
        }
322
        
314
 
323
        
315
 
324
        if($request->isPost()) {
316
        if ($request->isPost()) {
325
            
317
 
326
            $post->status = Post::STATUS_DELETE;
318
            $post->status = Post::STATUS_DELETE;
327
            
319
 
328
            $result =  $postMapper->update($post);
320
            $result =  $postMapper->update($post);
329
            if($result) {
321
            if ($result) {
330
                $this->logger->info('Se borro la noticia : ' .  $post->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
322
                $this->logger->info('Se borro la noticia : ' .  $post->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
331
              
323
 
332
                
324
 
333
                $data = [
325
                $data = [
334
                    'success' => true,
326
                    'success' => true,
335
                    'data' => 'LABEL_POST_DELETED'
327
                    'data' => 'LABEL_POST_DELETED'
336
                ];
328
                ];
337
            } else {
329
            } else {
338
                
330
 
339
                $data = [
331
                $data = [
340
                    'success'   => false,
332
                    'success'   => false,
341
                    'data'      => $postMapper->getError()
333
                    'data'      => $postMapper->getError()
342
                ];
-
 
343
                
334
                ];
344
                return new JsonModel($data);
335
 
345
            }
336
                return new JsonModel($data);
346
            
337
            }
347
        } else {
338
        } else {
348
            $data = [
339
            $data = [
349
                'success' => false,
340
                'success' => false,
350
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
341
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
351
            ];
342
            ];
352
            
343
 
353
            return new JsonModel($data);
344
            return new JsonModel($data);
354
        }
345
        }
355
        
346
 
356
        return new JsonModel($data);
347
        return new JsonModel($data);
357
    }
348
    }
358
    
349
 
359
    
350
 
360
    public function editAction()
351
    public function editAction()
361
    {
352
    {
362
        $currentUserPlugin = $this->plugin('currentUserPlugin');
353
        $currentUserPlugin = $this->plugin('currentUserPlugin');
363
        $currentUser = $currentUserPlugin->getUser();
354
        $currentUser = $currentUserPlugin->getUser();
364
        
355
 
365
        $request    = $this->getRequest();
356
        $request    = $this->getRequest();
366
        $id = $this->params()->fromRoute('id');
357
        $id = $this->params()->fromRoute('id');
367
        
358
 
368
        $postMapper = PostMapper::getInstance($this->adapter);
359
        $postMapper = PostMapper::getInstance($this->adapter);
369
        $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
360
        $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
370
        
361
 
371
        if(!$post) {
362
        if (!$post) {
372
            return new JsonModel([
-
 
373
                'success' => false,
363
            return new JsonModel([
374
                'data' => 'ERROR_POST_NOT_FOUND'
364
                'success' => false,
375
            ]);
365
                'data' => 'ERROR_POST_NOT_FOUND'
376
            
366
            ]);
377
        }
367
        }
378
       
368
 
379
        
369
 
380
        if($request->isGet()) {
370
        if ($request->isGet()) {
381
            
371
 
382
            $dt = \DateTime::createFromFormat('Y-m-d', $post->date);
372
            $dt = \DateTime::createFromFormat('Y-m-d', $post->date);
383
            
373
 
384
            $data = [
374
            $data = [
385
                'success' => true,
375
                'success' => true,
386
                'data' => [
376
                'data' => [
387
                    'title' => $post->title,
377
                    'title' => $post->title,
388
                    'status' => $post->status,
378
                    'status' => $post->status,
389
                    'description' => $post->description,
379
                    'description' => $post->description,
390
                    'url' => $post->url,
380
                    'url' => $post->url,
391
                    'date' => $dt->format('d/m/Y'),
381
                    'date' => $dt->format('d/m/Y'),
392
                ]
-
 
393
            ];
382
                ]
394
            
383
            ];
395
            return new JsonModel($data);
384
 
396
        }
385
            return new JsonModel($data);
397
        else if($request->isPost()) {
386
        } else if ($request->isPost()) {
398
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
387
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
399
            $dataPost['status'] = empty($dataPost['status']) ? Post::STATUS_INACTIVE : $dataPost['status'];
388
            $dataPost['status'] = empty($dataPost['status']) ? Post::STATUS_INACTIVE : $dataPost['status'];
400
            
389
 
401
            $form = new PostEditForm();
390
            $form = new PostEditForm();
402
            $form->setData($dataPost);
391
            $form->setData($dataPost);
Línea 403... Línea 392...
403
            
392
 
404
            if($form->isValid()) {
393
            if ($form->isValid()) {
405
                $dataPost = (array) $form->getData();
394
                $dataPost = (array) $form->getData();
406
                
395
 
407
 
396
 
408
                $post->title        = $dataPost['title'];
397
                $post->title        = $dataPost['title'];
409
                $post->description  = $dataPost['description'];
398
                $post->description  = $dataPost['description'];
410
                $post->url          = isset($dataPost['url']) ? $dataPost['url'] : '';
399
                $post->url          = isset($dataPost['url']) ? $dataPost['url'] : '';
411
                $post->status       = isset($dataPost['status']) ? $dataPost['status'] : Post::STATUS_INACTIVE;
400
                $post->status       = isset($dataPost['status']) ? $dataPost['status'] : Post::STATUS_INACTIVE;
412
                    
401
 
413
                $dt = \DateTime::createFromFormat('d/m/Y',$dataPost['date']);
402
                $dt = \DateTime::createFromFormat('d/m/Y', $dataPost['date']);
414
                $post->date = $dt->format('Y-m-d');
403
                $post->date = $dt->format('Y-m-d');
415
                
404
 
416
                
405
 
417
                
406
 
418
                
407
 
419
                $files = $this->getRequest()->getFiles()->toArray();
408
                $files = $this->getRequest()->getFiles()->toArray();
420
                
409
 
421
                $target_path = $this->config['leaderslinked.fullpath.post'] .  $post->uuid;
410
                $target_path = $this->config['leaderslinked.fullpath.post'] .  $post->uuid;
422
                if(!file_exists($target_path)) {
411
                if (!file_exists($target_path)) {
423
                    mkdir($target_path, 0755, true);
412
                    mkdir($target_path, 0755, true);
424
                }
413
                }
425
                
414
 
426
                
415
 
427
                if(isset($files['file']) && empty($files['file']['error'])) {
416
                if (isset($files['file']) && empty($files['file']['error'])) {
428
                    
417
 
429
                    if($post->file) {
418
                    if ($post->file) {
430
                        Functions::deleteFilename($target_path, $post->file);
419
                        Functions::deleteFilename($target_path, $post->file);
431
                    }
420
                    }
432
                    
421
 
433
                    
422
 
434
                    $tmp_filename       = $files['file']['tmp_name'];
423
                    $tmp_filename       = $files['file']['tmp_name'];
435
                    $original_filename  = trim(strtolower($files['file']['name']));
424
                    $original_filename  = trim(strtolower($files['file']['name']));
436
                    
425
 
437
                    $original_filename = Functions::normalizeString($original_filename);
426
                    $original_filename = Functions::normalizeString($original_filename);
438
                    
-
 
439
                    $parts = explode('.', $original_filename);
427
 
440
                    $original_filename = $parts[0] . '.' . $parts[ count($parts) - 1 ];
428
                    $parts = explode('.', $original_filename);
441
                    
429
                    $original_filename = $parts[0] . '.' . $parts[count($parts) - 1];
442
                    try
430
 
443
                    {
431
                    try {
444
                        $full_filename = $target_path  . DIRECTORY_SEPARATOR .$original_filename;
432
                        $full_filename = $target_path  . DIRECTORY_SEPARATOR . $original_filename;
445
                        if(move_uploaded_file($tmp_filename, $full_filename)) {
433
                        if (move_uploaded_file($tmp_filename, $full_filename)) {
446
                            $post->file = basename($full_filename);
434
                            $post->file = basename($full_filename);
447
                            $post->update($post);
435
                            $post->update($post);
448
                        }
436
                        }
449
                    } catch(\Throwable $e) {
437
                    } catch (\Throwable $e) {
450
                        error_log($e->getTraceAsString());
438
                        error_log($e->getTraceAsString());
451
                    }
439
                    }
452
                }
440
                }
453
                
441
 
454
                if(isset($files['image']) && empty($files['image']['error'])) {
442
                if (isset($files['image']) && empty($files['image']['error'])) {
455
                    
443
 
456
                    if($post->image) {
444
                    if ($post->image) {
457
                        Functions::deleteFilename($target_path, $post->image);
445
                        Functions::deleteFilename($target_path, $post->image);
458
                    }
446
                    }
459
                    
447
 
460
                    
448
 
461
                    $tmp_filename  = $files['image']['tmp_name'];
449
                    $tmp_filename  = $files['image']['tmp_name'];
462
                    
450
 
463
                    try {
451
                    try {
464
                        list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.post']);
452
                        list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.post']);
465
                        
453
 
466
                        $filename = 'image-' .uniqid() . '.png';
454
                        $filename = 'image-' . uniqid() . '.png';
467
                        $crop_to_dimensions = true;
455
                        $crop_to_dimensions = true;
468
                        if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
456
                        if (Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
469
                            $post->image = $filename;
457
                            $post->image = $filename;
470
                            $postMapper->update($post);
458
                            $postMapper->update($post);
-
 
459
                        }
Línea 471... Línea -...
471
                        }
-
 
472
                    } catch(\Throwable $e) {
460
                    } catch (\Throwable $e) {
473
                        error_log($e->getTraceAsString());
461
                        error_log($e->getTraceAsString());
474
                    }
462
                    }
475
                } 
463
                }
476
 
464
 
477
                
465
 
478
                
466
 
479
                if($postMapper->update($post)) {
467
                if ($postMapper->update($post)) {
480
                    $this->logger->info('Se edito la noticia ' . $post->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
468
                    $this->logger->info('Se edito la noticia ' . $post->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
481
                    
469
 
482
                    $data = [
470
                    $data = [
483
                        'success'   => true,
471
                        'success'   => true,
484
                        'data'   => 'LABEL_RECORD_UPDATED'
472
                        'data'   => 'LABEL_RECORD_UPDATED'
485
                    ];
-
 
486
                } else {
473
                    ];
487
                    $data = [
474
                } else {
488
                        'success'   => false,
475
                    $data = [
489
                        'data'      => $postMapper->getError()
-
 
490
                    ];
476
                        'success'   => false,
491
                    
477
                        'data'      => $postMapper->getError()
492
                }
478
                    ];
493
                
479
                }
494
                return new JsonModel($data);
-
 
495
                
480
 
496
            } else {
481
                return new JsonModel($data);
497
                $messages = [];
482
            } else {
498
                $form_messages = (array) $form->getMessages();
483
                $messages = [];
499
                foreach($form_messages  as $fieldname => $field_messages)
484
                $form_messages = (array) $form->getMessages();
500
                {
485
                foreach ($form_messages  as $fieldname => $field_messages) {
501
                    
486
 
502
                    $messages[$fieldname] = array_values($field_messages);
487
                    $messages[$fieldname] = array_values($field_messages);
503
                }
488
                }
504
                
489
 
505
                return new JsonModel([
490
                return new JsonModel([
506
                    'success'   => false,
491
                    'success'   => false,
507
                    'data'   => $messages
492
                    'data'   => $messages
508
                ]);
493
                ]);
509
            }
494
            }
510
        } else {
495
        } else {
511
            $data = [
496
            $data = [
512
                'success' => false,
497
                'success' => false,
513
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
498
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
514
            ];
499
            ];
515
            
-
 
516
            return new JsonModel($data);
-
 
517
        }
500