Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev 17002 Rev 17251
Línea 24... Línea 24...
24
    /**
24
    /**
25
     *
25
     *
26
     * @var \Laminas\Db\Adapter\AdapterInterface
26
     * @var \Laminas\Db\Adapter\AdapterInterface
27
     */
27
     */
28
    private $adapter;
28
    private $adapter;
29
    
29
 
30
    /**
30
    /**
31
     *
31
     *
32
     * @var \LeadersLinked\Cache\CacheInterface
32
     * @var \LeadersLinked\Cache\CacheInterface
33
     */
33
     */
34
    private $cache;
34
    private $cache;
35
    
35
 
36
    
36
 
37
    /**
37
    /**
38
     *
38
     *
39
     * @var \Laminas\Log\LoggerInterface
39
     * @var \Laminas\Log\LoggerInterface
40
     */
40
     */
41
    private $logger;
41
    private $logger;
42
    
42
 
43
    /**
43
    /**
44
     *
44
     *
45
     * @var array
45
     * @var array
46
     */
46
     */
47
    private $config;
47
    private $config;
48
    
48
 
49
    
49
 
50
    /**
50
    /**
51
     *
51
     *
52
     * @var \Laminas\Mvc\I18n\Translator
52
     * @var \Laminas\Mvc\I18n\Translator
53
     */
53
     */
54
    private $translator;
54
    private $translator;
55
    
55
 
56
    
56
 
57
    /**
57
    /**
58
     *
58
     *
59
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
59
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
60
     * @param \LeadersLinked\Cache\CacheInterface $cache
60
     * @param \LeadersLinked\Cache\CacheInterface $cache
61
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
61
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
Línea 71... Línea 71...
71
        $this->translator   = $translator;
71
        $this->translator   = $translator;
72
    }
72
    }
Línea 73... Línea 73...
73
 
73
 
74
    public function indexAction()
74
    public function indexAction()
75
    {
-
 
-
 
75
    {
76
        
76
        // Obtiene el plugin del usuario actual y la compañía
77
        $currentUserPlugin = $this->plugin('currentUserPlugin');
77
        $currentUserPlugin = $this->plugin('currentUserPlugin');
78
        $currentUser = $currentUserPlugin->getUser();
78
        $currentUser = $currentUserPlugin->getUser();
Línea -... Línea 79...
-
 
79
        $currentCompany = $currentUserPlugin->getCompany();
79
        $currentCompany = $currentUserPlugin->getCompany();
80
 
-
 
81
        // Obtiene la solicitud actual
80
 
82
        $request = $this->getRequest();
Línea 81... Línea -...
81
        $request = $this->getRequest();
-
 
-
 
83
        // Verifica si la solicitud es de tipo GET
82
        if ($request->isGet()) {
84
        if ($request->isGet()) {
Línea -... Línea 85...
-
 
85
 
83
 
86
            // Obtiene los encabezados de la solicitud
84
 
87
            $headers  = $request->getHeaders();
85
            $headers  = $request->getHeaders();
88
 
Línea 86... Línea 89...
86
 
89
            // Determina si la respuesta debe ser JSON
Línea 97... Línea 100...
97
                        $isJson = strpos($raw, 'json');
100
                        $isJson = strpos($raw, 'json');
98
                    }
101
                    }
99
                }
102
                }
100
            }
103
            }
Línea 101... Línea -...
101
 
-
 
-
 
104
 
102
      
105
            // Si la respuesta debe ser JSON
-
 
106
            if ($isJson) {
103
            if ($isJson) {
107
                // Obtiene y sanitiza los parámetros de la consulta
104
                $search = $this->params()->fromQuery('search');
108
                $search = $this->params()->fromQuery('search');
Línea 105... Línea 109...
105
                $search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);
109
                $search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);
106
 
110
 
107
                $page               = intval($this->params()->fromQuery('start', 1), 10);
111
                $page               = intval($this->params()->fromQuery('start', 1), 10);
108
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
112
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
109
                $order =  $this->params()->fromQuery('order', []);
113
                $order =  $this->params()->fromQuery('order', []);
Línea -... Línea 114...
-
 
114
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
110
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
115
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : Functions::sanitizeFilterString(filter_var($order[0]['dir']));
111
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : Functions::sanitizeFilterString(filter_var($order[0]['dir']));
116
 
Línea 112... Línea 117...
112
 
117
                // Define los campos de ordenación
113
                $fields =  ['name'];
118
                $fields =  ['name'];
114
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
119
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
Línea -... Línea 120...
-
 
120
 
115
 
121
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
116
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
122
                    $order_direction = 'ASC';
Línea -... Línea 123...
-
 
123
                }
117
                    $order_direction = 'ASC';
124
 
118
                }
125
                // Obtiene el mapeador de categorías de medios y los datos paginados
119
 
126
                $mediaCategoryMapper = MediaCategoryMapper::getInstance($this->adapter);
120
                $mediaCategoryMapper = MediaCategoryMapper::getInstance($this->adapter);
127
                $paginator = $mediaCategoryMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
121
                $paginator = $mediaCategoryMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
128
 
Línea 132... Línea 139...
132
                    ];
139
                    ];
Línea 133... Línea 140...
133
 
140
 
134
                    array_push($items, $item);
141
                    array_push($items, $item);
Línea -... Línea 142...
-
 
142
                }
135
                }
143
 
136
 
144
                // Devuelve un modelo JSON con los datos
137
                return new JsonModel([
145
                return new JsonModel([
138
                    'success' => true,
146
                    'success' => true,
139
                    'data' => [
147
                    'data' => [
140
                        'items' => $items,
148
                        'items' => $items,
141
                        'total' => $paginator->getTotalItemCount(),
149
                        'total' => $paginator->getTotalItemCount(),
142
                    ]
150
                    ]
-
 
151
                ]);
143
                ]);
152
            } else {
Línea 144... Línea 153...
144
            } else {
153
                // Si no es JSON, devuelve el formulario y la vista
145
                $form = new CategoryForm();
154
                $form = new CategoryForm();
146
 
155
 
147
                $this->layout()->setTemplate('layout/layout-backend');
156
                $this->layout()->setTemplate('layout/layout-backend');
148
                $viewModel = new ViewModel();
157
                $viewModel = new ViewModel();
149
                $viewModel->setTemplate('leaders-linked/media/categories.phtml');
158
                $viewModel->setTemplate('leaders-linked/media/categories.phtml');
150
                $viewModel->setVariable('form', $form);
159
                $viewModel->setVariable('form', $form);
-
 
160
                return $viewModel;
151
                return $viewModel;
161
            }
152
            }
162
        } else {
153
        } else {
163
            // Si la solicitud no es GET, devuelve un error
154
            return new JsonModel([
164
            return new JsonModel([
155
                'success' => false,
165
                'success' => false,
Línea 171... Línea 181...
171
 
181
 
Línea 172... Línea 182...
172
            $form->setData($dataPost);
182
            $form->setData($dataPost);
173
 
183
 
174
            if ($form->isValid()) {
184
            if ($form->isValid()) {
175
                $dataPost = (array) $form->getData();
185
                $dataPost = (array) $form->getData();
176
             
186
 
177
                $hydrator = new ObjectPropertyHydrator();
187
                $hydrator = new ObjectPropertyHydrator();
178
                $mediaCategory = new MediaCategory();
188
                $mediaCategory = new MediaCategory();
Línea 179... Línea 189...
179
                $mediaCategory->company_id = $currentCompany->id;
189
                $mediaCategory->company_id = $currentCompany->id;
Línea 180... Línea 190...
180
                $hydrator->hydrate($dataPost, $mediaCategory);
190
                $hydrator->hydrate($dataPost, $mediaCategory);
181
 
191
 
Línea 249... Línea 259...
249
        if (!$mediaCategory) {
259
        if (!$mediaCategory) {
250
            $data = [
260
            $data = [
251
                'success'   => false,
261
                'success'   => false,
252
                'data'   => 'ERROR_RECORD_NOT_FOUND'
262
                'data'   => 'ERROR_RECORD_NOT_FOUND'
253
            ];
263
            ];
254
            
264
 
255
            return new JsonModel($data);
265
            return new JsonModel($data);
256
        }
266
        }
257
        
267
 
258
        if($mediaCategory->company_id != $currentCompany->id) {
268
        if ($mediaCategory->company_id != $currentCompany->id) {
259
            return new JsonModel([
269
            return new JsonModel([
260
                'success' => false,
270
                'success' => false,
261
                'data' => 'ERROR_UNAUTHORIZED'
271
                'data' => 'ERROR_UNAUTHORIZED'
262
            ]);
272
            ]);
263
 
-
 
264
        }
273
        }
265
        
274
 
Línea 266... Línea 275...
266
 
275
 
267
        if ($request->isPost()) {
276
        if ($request->isPost()) {
268
            $form = new  CategoryForm();
277
            $form = new  CategoryForm();
Línea 351... Línea 360...
351
                'data'   => 'ERROR_RECORD_NOT_FOUND'
360
                'data'   => 'ERROR_RECORD_NOT_FOUND'
352
            ];
361
            ];
Línea 353... Línea 362...
353
 
362
 
354
            return new JsonModel($data);
363
            return new JsonModel($data);
355
        }
364
        }
356
        
365
 
357
        
366
 
358
        if($mediaCategory->company_id != $currentCompany->id) {
367
        if ($mediaCategory->company_id != $currentCompany->id) {
359
            return new JsonModel([
368
            return new JsonModel([
360
                'success' => false,
369
                'success' => false,
361
                'data' => 'ERROR_UNAUTHORIZED'
370
                'data' => 'ERROR_UNAUTHORIZED'
362
            ]);
-
 
363
            
371
            ]);
364
        }
372
        }
Línea 365... Línea 373...
365
        
373
 
366
 
374
 
367
        if ($request->isPost()) {
375
        if ($request->isPost()) {
368
            
376
 
369
            $mediaFileMapper = \LeadersLinked\Mapper\MediaFileMapper::getInstance($this->adapter);
377
            $mediaFileMapper = \LeadersLinked\Mapper\MediaFileMapper::getInstance($this->adapter);
370
            $total = $mediaFileMapper->fetchTotalCountByCategoryid($mediaCategory->id);
378
            $total = $mediaFileMapper->fetchTotalCountByCategoryid($mediaCategory->id);
371
            
379
 
372
            if($total > 0) {
380
            if ($total > 0) {
373
                return new JsonModel([
381
                return new JsonModel([
374
                    'success' => false,
382
                    'success' => false,
375
                    'data' => 'ERROR_SQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW'
383
                    'data' => 'ERROR_SQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW'
376
                ]);
384
                ]);
377
            }
385
            }
378
            
386
 
379
            
387
 
380
            
388
 
381
            $result = $mediaCategoryMapper->delete($mediaCategory);
389
            $result = $mediaCategoryMapper->delete($mediaCategory);
Línea 382... Línea 390...
382
            if ($result) {
390
            if ($result) {