Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16766 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
16768 efrain 7
 
1 www 8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
use LeadersLinked\Library\Functions;
13
use LeadersLinked\Mapper\LogMapper;
14
use LeadersLinked\Model\Log;
15
use LeadersLinked\Mapper\DeviceMapper;
16
 
17
class DeviceController extends AbstractActionController
18
{
19
    /**
20
     *
21
     * @var AdapterInterface
22
     */
23
    private $adapter;
24
 
25
    /**
26
     *
27
     * @var  LoggerInterface
28
     */
29
    private $logger;
30
 
31
    /**
32
     *
33
     * @var array
34
     */
35
    private $config;
36
 
37
    /**
38
     *
39
     * @param AdapterInterface $adapter
40
     * @param LoggerInterface $logger
41
     * @param array $config
42
     */
16768 efrain 43
    public function __construct($adapter, $logger, $config)
1 www 44
    {
16768 efrain 45
        $this->adapter = $adapter;
46
        $this->logger = $logger;
47
        $this->config = $config;
1 www 48
    }
49
 
50
    public function indexAction()
51
    {
52
        $request = $this->getRequest();
53
 
54
        if($request->isPost())
55
        {
56
            $search = $this->params()->fromQuery('search', []);
16766 efrain 57
            $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
1 www 58
 
59
            $page               = intval($this->params()->fromQuery('start', 1), 10);
60
            $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
61
            $order =  $this->params()->fromQuery('order', []);
62
            $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 63
            $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
1 www 64
 
65
            $fields =  ['device_id', 'device', 'model', 'product', 'osversion', 'user', 'email'];
66
            $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'device_id';
67
 
68
            if(!in_array($order_direction, ['ASC', 'DESC'])) {
69
                $order_direction = 'ASC';
70
            }
71
 
72
 
73
 
74
 
75
            $deviceMapper = DeviceMapper::getInstance($this->adapter);
76
            $paginator = $deviceMapper->fetchAllDataTable($search, $page, $records_x_page, $order_field, $order_direction);
77
 
78
 
79
            $items = [];
80
            $records = $paginator->getCurrentItems();
81
            foreach($records as $record)
82
            {
83
 
84
                $item = [
85
                    'user' => $record->user,
86
                    'email' => $record->email,
87
                    'device_id' => $record->device_id,
88
                    'device' => $record->device,
89
                    'model' => $record->model,
90
                    'product' => $record->product,
91
                    'osversion' => $record->osversion,
92
                    'updated_on' => $record->updated_on,
93
                    'push'  => $record->token ? true : false,
94
 
95
                ];
96
 
97
                array_push($items, $item);
98
            }
99
            return new JsonModel([
100
                'success' => true,
101
                'data' => [
102
                    'items' => $items,
103
                    'total' => $paginator->getTotalItemCount(),
104
                ]
105
            ]);
106
 
107
 
108
        }
109
        else if($request->isGet()) {
110
 
111
            $this->layout()->setTemplate('layout/layout.phtml');
112
            $viewModel = new ViewModel();
113
            $viewModel->setTemplate('leaders-linked/devices/index.phtml');
114
            return $viewModel ;
115
 
116
        } else {
117
            return new JsonModel([
118
                'success' => false,
119
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
120
            ]);;
121
        }
122
    }
123
 
124
 
125
}